sakura/functions.php

1913 lines
76 KiB
PHP
Raw Permalink Normal View History

2018-05-26 18:59:29 +08:00
<?php
/**
* Sakura functions and definitions.
*
* @link https://developer.wordpress.org/themes/basics/theme-functions/
*
* @package Sakura
*/
2019-12-27 10:01:04 +08:00
define('SAKURA_VERSION', wp_get_theme()->get('Version'));
define('BUILD_VERSION', '3');
2018-05-26 18:59:29 +08:00
//ini_set('display_errors', true);
2019-12-27 10:01:04 +08:00
//error_reporting(E_ALL);
2019-11-14 19:15:53 +08:00
error_reporting(E_ALL ^ E_NOTICE);
2018-05-26 18:59:29 +08:00
2019-12-27 10:01:04 +08:00
if (!function_exists('akina_setup')):
2018-05-26 18:59:29 +08:00
/**
* Sets up theme defaults and registers support for various WordPress features.
*
* Note that this function is hooked into the after_setup_theme hook, which
* runs before the init hook. The init hook is too late for some features, such
* as indicating support for post thumbnails.
*/
2019-12-27 10:01:04 +08:00
if (!function_exists('optionsframework_init')) {
define('OPTIONS_FRAMEWORK_DIRECTORY', get_template_directory_uri() . '/inc/');
require_once dirname(__FILE__) . '/inc/options-framework.php';
}
function akina_setup()
{
/*
* Make theme available for translation.
* Translations can be filed in the /languages/ directory.
* If you're building a theme based on Akina, use a find and replace
* to change 'akina' to the name of your theme in all the template files.
*/
load_theme_textdomain('sakura', get_template_directory() . '/languages');
/*
* Enable support for Post Thumbnails on posts and pages.
*
* @link https://developer.wordpress.org/themes/functionality/featured-images-post-thumbnails/
*/
add_theme_support('post-thumbnails');
set_post_thumbnail_size(150, 150, true);
// This theme uses wp_nav_menu() in one location.
register_nav_menus(array(
'primary' => __('Nav Menus', 'sakura'), //导航菜单
));
/*
* Switch default core markup for search form, comment form, and comments
* to output valid HTML5.
*/
add_theme_support('html5', array(
'search-form',
'comment-form',
'comment-list',
'gallery',
'caption',
));
/*
* Enable support for Post Formats.
* See https://developer.wordpress.org/themes/functionality/post-formats/
*/
add_theme_support('post-formats', array(
'aside',
'image',
'status',
));
// Set up the WordPress core custom background feature.
add_theme_support('custom-background', apply_filters('akina_custom_background_args', array(
'default-color' => 'ffffff',
'default-image' => '',
)));
add_filter('pre_option_link_manager_enabled', '__return_true');
// 优化代码
//去除头部冗余代码
remove_action('wp_head', 'feed_links_extra', 3);
remove_action('wp_head', 'rsd_link');
remove_action('wp_head', 'wlwmanifest_link');
remove_action('wp_head', 'index_rel_link');
remove_action('wp_head', 'start_post_rel_link', 10, 0);
remove_action('wp_head', 'wp_generator');
remove_action('wp_head', 'wp_generator'); //隐藏wordpress版本
remove_filter('the_content', 'wptexturize'); //取消标点符号转义
//remove_action('rest_api_init', 'wp_oembed_register_route');
//remove_filter('rest_pre_serve_request', '_oembed_rest_pre_serve_request', 10, 4);
//remove_filter('oembed_dataparse', 'wp_filter_oembed_result', 10);
//remove_filter('oembed_response_data', 'get_oembed_response_data_rich', 10, 4);
//remove_action('wp_head', 'wp_oembed_add_discovery_links');
//remove_action('wp_head', 'wp_oembed_add_host_js');
remove_action('template_redirect', 'rest_output_link_header', 11, 0);
function coolwp_remove_open_sans_from_wp_core()
{
wp_deregister_style('open-sans');
wp_register_style('open-sans', false);
wp_enqueue_style('open-sans', '');
}
add_action('init', 'coolwp_remove_open_sans_from_wp_core');
/**
* Disable the emoji's
*/
function disable_emojis()
{
remove_action('wp_head', 'print_emoji_detection_script', 7);
remove_action('admin_print_scripts', 'print_emoji_detection_script');
remove_action('wp_print_styles', 'print_emoji_styles');
remove_action('admin_print_styles', 'print_emoji_styles');
remove_filter('the_content_feed', 'wp_staticize_emoji');
remove_filter('comment_text_rss', 'wp_staticize_emoji');
remove_filter('wp_mail', 'wp_staticize_emoji_for_email');
add_filter('tiny_mce_plugins', 'disable_emojis_tinymce');
}
add_action('init', 'disable_emojis');
/**
* Filter function used to remove the tinymce emoji plugin.
*
* @param array $plugins
* @return array Difference betwen the two arrays
*/
function disable_emojis_tinymce($plugins)
{
if (is_array($plugins)) {
return array_diff($plugins, array('wpemoji'));
} else {
return array();
}
}
// 移除菜单冗余代码
add_filter('nav_menu_css_class', 'my_css_attributes_filter', 100, 1);
add_filter('nav_menu_item_id', 'my_css_attributes_filter', 100, 1);
add_filter('page_css_class', 'my_css_attributes_filter', 100, 1);
function my_css_attributes_filter($var)
{
return is_array($var) ? array_intersect($var, array('current-menu-item', 'current-post-ancestor', 'current-menu-ancestor', 'current-menu-parent')) : '';
}
}
2018-05-26 18:59:29 +08:00
endif;
2019-12-27 10:01:04 +08:00
add_action('after_setup_theme', 'akina_setup');
2018-05-26 18:59:29 +08:00
2019-12-27 10:01:04 +08:00
function admin_lettering()
{
echo '<style type="text/css">body{font-family: Microsoft YaHei;}</style>';
2018-05-26 18:59:29 +08:00
}
add_action('admin_head', 'admin_lettering');
/**
* Set the content width in pixels, based on the theme's design and stylesheet.
*
* Priority 0 to make it available to lower priority callbacks.
*
* @global int $content_width
*/
2019-12-27 10:01:04 +08:00
function akina_content_width()
{
$GLOBALS['content_width'] = apply_filters('akina_content_width', 640);
2018-05-26 18:59:29 +08:00
}
2019-12-27 10:01:04 +08:00
add_action('after_setup_theme', 'akina_content_width', 0);
2018-05-26 18:59:29 +08:00
/**
* Enqueue scripts and styles.
*/
2019-12-27 10:01:04 +08:00
function sakura_scripts()
{
if (akina_option('jsdelivr_cdn_test')) {
wp_enqueue_script('js_lib', get_template_directory_uri() . '/cdn/js/lib.js', array(), SAKURA_VERSION . akina_option('cookie_version', ''), true);
} else {
wp_enqueue_script('js_lib', 'https://cdn.jsdelivr.net/gh/mashirozx/Sakura@' . SAKURA_VERSION . '/cdn/js/lib.min.js', array(), SAKURA_VERSION, true);
2018-05-26 18:59:29 +08:00
}
2019-01-26 11:25:53 +08:00
if (akina_option('app_no_jsdelivr_cdn')) {
2019-12-27 10:01:04 +08:00
wp_enqueue_style('saukra_css', get_stylesheet_uri(), array(), SAKURA_VERSION);
wp_enqueue_script('app', get_template_directory_uri() . '/js/sakura-app.js', array(), SAKURA_VERSION, true);
2019-01-26 11:25:53 +08:00
} else {
2019-12-27 10:01:04 +08:00
wp_enqueue_style('saukra_css', 'https://cdn.jsdelivr.net/gh/mashirozx/Sakura@' . SAKURA_VERSION . '/style.min.css', array(), SAKURA_VERSION);
wp_enqueue_script('app', 'https://cdn.jsdelivr.net/gh/mashirozx/Sakura@' . SAKURA_VERSION . '/js/sakura-app.min.js', array(), SAKURA_VERSION, true);
}
2022-06-01 14:47:16 +08:00
//wp_enqueue_script('github_card', 'https://cdn.jsdelivr.net/github-cards/latest/widget.js', array(), SAKURA_VERSION, true);
2019-12-27 10:01:04 +08:00
if (is_singular() && comments_open() && get_option('thread_comments')) {
wp_enqueue_script('comment-reply');
}
2018-05-26 18:59:29 +08:00
2019-12-27 10:01:04 +08:00
// 20161116 @Louie
$mv_live = akina_option('focus_mvlive') ? 'open' : 'close';
$movies = akina_option('focus_amv') ? array('url' => akina_option('amv_url'), 'name' => akina_option('amv_title'), 'live' => $mv_live) : 'close';
$auto_height = akina_option('focus_height') ? 'fixed' : 'auto';
$code_lamp = 'close';
// if (wp_is_mobile()) {
// $auto_height = 'fixed';
// }
2019-12-27 10:01:04 +08:00
//拦截移动端
version_compare($GLOBALS['wp_version'], '5.1', '>=') ? $reply_link_version = 'new' : $reply_link_version = 'old';
2021-07-22 14:49:30 +08:00
$gravatar_url = akina_option('gravatar_proxy') ?: 'dn-qiniu-avatar.qbox.me/avatar';
2019-12-27 10:01:04 +08:00
wp_localize_script('app', 'Poi', array(
'pjax' => akina_option('poi_pjax'),
'movies' => $movies,
'windowheight' => $auto_height,
'codelamp' => $code_lamp,
'ajaxurl' => admin_url('admin-ajax.php'),
'order' => get_option('comment_order'), // ajax comments
'formpostion' => 'bottom', // ajax comments 默认为bottom如果你的表单在顶部则设置为top。
2019-11-15 09:23:07 +08:00
'reply_link_version' => $reply_link_version,
2019-12-27 10:01:04 +08:00
'api' => esc_url_raw(rest_url()),
'nonce' => wp_create_nonce('wp_rest'),
'google_analytics_id' => akina_option('google_analytics_id', ''),
'gravatar_url' => $gravatar_url
2019-12-27 10:01:04 +08:00
));
2018-05-26 18:59:29 +08:00
}
2019-12-27 10:01:04 +08:00
add_action('wp_enqueue_scripts', 'sakura_scripts');
2018-05-26 18:59:29 +08:00
/**
* load .php.
*/
2019-12-27 10:01:04 +08:00
require get_template_directory() . '/inc/decorate.php';
require get_template_directory() . '/inc/swicher.php';
require get_template_directory() . '/inc/api.php';
2018-05-26 18:59:29 +08:00
/**
* Custom template tags for this theme.
*/
require get_template_directory() . '/inc/template-tags.php';
/**
* Customizer additions.
*/
require get_template_directory() . '/inc/customizer.php';
/**
* function update
*/
require get_template_directory() . '/inc/theme_plus.php';
require get_template_directory() . '/inc/categories-images.php';
//Comment Location Start
2019-12-27 10:01:04 +08:00
function convertip($ip)
{
2019-11-13 20:32:10 +08:00
error_reporting(E_ALL ^ E_NOTICE);
if (!filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) === false) {
$file_contents = file_get_contents('http://ip.taobao.com/outGetIpInfo?accessKey=alibaba-inc&ip='.$ip);
$result = json_decode($file_contents,true);
if ($result['data']['country'] != '中国') {
return $result['data']['country'];
} else {
return $result['data']['region'].'&nbsp;·&nbsp;'.$result['data']['city'].'&nbsp;·&nbsp;'.$result['data']['isp'];
2018-05-26 18:59:29 +08:00
}
} else {
$dat_path = dirname(__FILE__) . '/inc/QQWry.Dat';
if (!$fd = @fopen($dat_path, 'rb')) {
return 'IP date file not exists or access denied';
2018-05-26 18:59:29 +08:00
}
$ip = explode('.', $ip);
$ipNum = intval($ip[0]) * 16777216 + intval($ip[1]) * 65536 + intval($ip[2]) * 256 + intval($ip[3]);
$DataBegin = fread($fd, 4);
$DataEnd = fread($fd, 4);
$ipbegin = implode('', unpack('L', $DataBegin));
if ($ipbegin < 0) {
$ipbegin += pow(2, 32);
2018-05-26 18:59:29 +08:00
}
$ipend = implode('', unpack('L', $DataEnd));
if ($ipend < 0) {
$ipend += pow(2, 32);
2019-12-27 10:01:04 +08:00
}
$ipAllNum = ($ipend - $ipbegin) / 7 + 1;
$BeginNum = 0;
$EndNum = $ipAllNum;
$ip1num = $ip2num = $ipAddr1 = $ipAddr2 = '';
while ($ip1num > $ipNum || $ip2num < $ipNum) {
$Middle = intval(($EndNum + $BeginNum) / 2);
fseek($fd, $ipbegin + 7 * $Middle);
$ipData1 = fread($fd, 4);
if (strlen($ipData1) < 4) {
2018-05-26 18:59:29 +08:00
fclose($fd);
return 'System Error';
2018-05-26 18:59:29 +08:00
}
$ip1num = implode('', unpack('L', $ipData1));
if ($ip1num < 0) {
$ip1num += pow(2, 32);
}
if ($ip1num > $ipNum) {
$EndNum = $Middle;
continue;
}
$DataSeek = fread($fd, 3);
if (strlen($DataSeek) < 3) {
2018-05-26 18:59:29 +08:00
fclose($fd);
return 'System Error';
}
$DataSeek = implode('', unpack('L', $DataSeek . chr(0)));
fseek($fd, $DataSeek);
$ipData2 = fread($fd, 4);
if (strlen($ipData2) < 4) {
fclose($fd);
return 'System Error';
}
$ip2num = implode('', unpack('L', $ipData2));
if ($ip2num < 0) {
$ip2num += pow(2, 32);
}
2019-12-27 10:01:04 +08:00
if ($ip2num < $ipNum) {
if ($Middle == $BeginNum) {
fclose($fd);
return 'Unknown';
}
$BeginNum = $Middle;
}
2019-12-27 10:01:04 +08:00
}
2018-05-26 18:59:29 +08:00
$ipFlag = fread($fd, 1);
if ($ipFlag == chr(1)) {
$ipSeek = fread($fd, 3);
if (strlen($ipSeek) < 3) {
fclose($fd);
return 'System Error';
}
$ipSeek = implode('', unpack('L', $ipSeek . chr(0)));
fseek($fd, $ipSeek);
$ipFlag = fread($fd, 1);
}
2019-12-27 10:01:04 +08:00
if ($ipFlag == chr(2)) {
$AddrSeek = fread($fd, 3);
if (strlen($AddrSeek) < 3) {
2018-05-26 18:59:29 +08:00
fclose($fd);
return 'System Error';
}
$ipFlag = fread($fd, 1);
if ($ipFlag == chr(2)) {
$AddrSeek2 = fread($fd, 3);
if (strlen($AddrSeek2) < 3) {
fclose($fd);
return 'System Error';
}
$AddrSeek2 = implode('', unpack('L', $AddrSeek2 . chr(0)));
fseek($fd, $AddrSeek2);
} else {
fseek($fd, -1, SEEK_CUR);
}
while (($char = fread($fd, 1)) != chr(0)) {
$ipAddr2 .= $char;
}
$AddrSeek = implode('', unpack('L', $AddrSeek . chr(0)));
fseek($fd, $AddrSeek);
while (($char = fread($fd, 1)) != chr(0)) {
$ipAddr1 .= $char;
}
2018-05-26 18:59:29 +08:00
} else {
fseek($fd, -1, SEEK_CUR);
while (($char = fread($fd, 1)) != chr(0)) {
$ipAddr1 .= $char;
}
$ipFlag = fread($fd, 1);
if ($ipFlag == chr(2)) {
$AddrSeek2 = fread($fd, 3);
if (strlen($AddrSeek2) < 3) {
fclose($fd);
return 'System Error';
}
$AddrSeek2 = implode('', unpack('L', $AddrSeek2 . chr(0)));
fseek($fd, $AddrSeek2);
} else {
fseek($fd, -1, SEEK_CUR);
}
while (($char = fread($fd, 1)) != chr(0)) {
$ipAddr2 .= $char;
}
2018-05-26 18:59:29 +08:00
}
fclose($fd);
if (preg_match('/http/i', $ipAddr2)) {
$ipAddr2 = '';
2018-05-26 18:59:29 +08:00
}
$ipaddr = "$ipAddr1 $ipAddr2";
$ipaddr = preg_replace('/CZ88.Net/is', '', $ipaddr);
$ipaddr = preg_replace('/^s*/is', '', $ipaddr);
$ipaddr = preg_replace('/s*$/is', '', $ipaddr);
if (preg_match('/http/i', $ipaddr) || $ipaddr == '') {
$ipaddr = 'Unknown';
}
$ipaddr = iconv('gbk', 'utf-8//IGNORE', $ipaddr);
if ($ipaddr != ' ') {
return $ipaddr;
} else {
$ipaddr = 'Unknown';
}
2018-05-26 18:59:29 +08:00
return $ipaddr;
2019-12-27 10:01:04 +08:00
}
2018-05-26 18:59:29 +08:00
}
//Comment Location End
/**
* COMMENT FORMATTING
*
* 标准的 lazyload 输出头像
* <?php echo str_replace( 'src=', 'src="https://cdn.jsdelivr.net/gh/moezx/cdn@3.0.1/img/svg/loader/index.ajax-spinner-preloader.svg" onerror="imgError(this,1)" data-src=', get_avatar( $comment->comment_author_email, '80', '', get_comment_author(), array( 'class' => array( 'lazyload' ) ) ) ); ?>
*
* 如果不延时是这样的
* <?php echo get_avatar( $comment->comment_author_email, '80', '', get_comment_author() ); ?>
*
*/
2019-12-27 10:01:04 +08:00
if (!function_exists('akina_comment_format')) {
function akina_comment_format($comment, $args, $depth)
{
$GLOBALS['comment'] = $comment;
?>
<li <?php comment_class();?> id="comment-<?php echo esc_attr(comment_ID()); ?>">
2018-05-26 18:59:29 +08:00
<div class="contents">
<div class="comment-arrow">
<div class="main shadow">
<div class="profile">
2019-12-27 10:01:04 +08:00
<a href="<?php comment_author_url();?>" target="_blank" rel="nofollow"><?php echo str_replace('src=', 'src="https://cdn.jsdelivr.net/gh/moezx/cdn@3.0.2/img/svg/loader/trans.ajax-spinner-preloader.svg" onerror="imgError(this,1)" data-src=', get_avatar($comment->comment_author_email, '80', '', get_comment_author(), array('class' => array('lazyload')))); ?></a>
2018-05-26 18:59:29 +08:00
</div>
<div class="commentinfo">
<section class="commeta">
<div class="left">
2019-12-27 10:01:04 +08:00
<h4 class="author"><a href="<?php comment_author_url();?>" target="_blank" rel="nofollow"><?php echo get_avatar($comment->comment_author_email, '24', '', get_comment_author()); ?><span class="bb-comment isauthor" title="<?php _e('Author', 'sakura');?>"><?php _e('Blogger', 'sakura'); /*博主*/?></span> <?php comment_author();?> <?php echo get_author_class($comment->comment_author_email, $comment->user_id); ?></a></h4>
2018-05-26 18:59:29 +08:00
</div>
2019-12-27 10:01:04 +08:00
<?php comment_reply_link(array_merge($args, array('depth' => $depth, 'max_depth' => $args['max_depth'])));?>
2018-05-26 18:59:29 +08:00
<div class="right">
<div class="info"><time datetime="<?php comment_date('Y-m-d');?>"><?php echo poi_time_since(strtotime($comment->comment_date_gmt), true); //comment_date(get_option('date_format')); ?></time><?php echo siren_get_useragent($comment->comment_agent); ?><?php echo mobile_get_useragent_icon($comment->comment_agent); ?>&nbsp;<?php if(akina_option('open_location')){ _e('Location', 'sakura'); /*来自*/?>: <?php echo convertip(get_comment_author_ip());} ?>
2019-12-27 10:01:04 +08:00
<?php if (current_user_can('manage_options') and (wp_is_mobile() == false)) {
$comment_ID = $comment->comment_ID;
$i_private = get_comment_meta($comment_ID, '_private', true);
$flag = '';
2019-12-27 10:01:04 +08:00
$flag .= ' <i class="fa fa-snowflake-o" aria-hidden="true"></i> <a href="javascript:;" data-actionp="set_private" data-idp="' . get_comment_id() . '" id="sp" class="sm" style="color:rgba(0,0,0,.35)">' . __("Private", "sakura") . ': <span class="has_set_private">';
if (!empty($i_private)) {
$flag .= __("Yes", "sakura") . ' <i class="fa fa-lock" aria-hidden="true"></i>';
} else {
$flag .= __("No", "sakura") . ' <i class="fa fa-unlock" aria-hidden="true"></i>';
}
$flag .= '</span></a>';
$flag .= edit_comment_link('<i class="fa fa-pencil-square-o" aria-hidden="true"></i> ' . __("Edit", "mashiro"), ' <span style="color:rgba(0,0,0,.35)">', '</span>');
echo $flag;
}?></div>
2018-05-26 18:59:29 +08:00
</div>
</section>
</div>
<div class="body">
2019-12-27 10:01:04 +08:00
<?php comment_text();?>
2018-05-26 18:59:29 +08:00
</div>
</div>
<div class="arrow-left"></div>
</div>
</div>
<hr>
<?php
2019-12-27 10:01:04 +08:00
}
2018-05-26 18:59:29 +08:00
}
/**
* 获取访客VIP样式
*/
2019-12-27 10:01:04 +08:00
function get_author_class($comment_author_email, $user_id)
{
global $wpdb;
$author_count = count($wpdb->get_results(
2018-05-26 18:59:29 +08:00
"SELECT comment_ID as author_count FROM $wpdb->comments WHERE comment_author_email = '$comment_author_email' "));
2019-12-27 10:01:04 +08:00
if ($author_count >= 1 && $author_count < 5) //数字可自行修改,代表评论次数。
{
echo '<span class="showGrade0" title="Lv0"><img src="https://cdn.jsdelivr.net/gh/moezx/cdn@3.1.9/img/Sakura/images/level/level_0.svg" style="height: 1.5em; max-height: 1.5em; display: inline-block;"></span>';
} else if ($author_count >= 6 && $author_count < 10) {
echo '<span class="showGrade1" title="Lv1"><img src="https://cdn.jsdelivr.net/gh/moezx/cdn@3.1.9/img/Sakura/images/level/level_1.svg" style="height: 1.5em; max-height: 1.5em; display: inline-block;"></span>';
} else if ($author_count >= 10 && $author_count < 20) {
echo '<span class="showGrade2" title="Lv2"><img src="https://cdn.jsdelivr.net/gh/moezx/cdn@3.1.9/img/Sakura/images/level/level_2.svg" style="height: 1.5em; max-height: 1.5em; display: inline-block;"></span>';
} else if ($author_count >= 20 && $author_count < 40) {
echo '<span class="showGrade3" title="Lv3"><img src="https://cdn.jsdelivr.net/gh/moezx/cdn@3.1.9/img/Sakura/images/level/level_3.svg" style="height: 1.5em; max-height: 1.5em; display: inline-block;"></span>';
} else if ($author_count >= 40 && $author_count < 80) {
echo '<span class="showGrade4" title="Lv4"><img src="https://cdn.jsdelivr.net/gh/moezx/cdn@3.1.9/img/Sakura/images/level/level_4.svg" style="height: 1.5em; max-height: 1.5em; display: inline-block;"></span>';
} else if ($author_count >= 80 && $author_count < 160) {
echo '<span class="showGrade5" title="Lv5"><img src="https://cdn.jsdelivr.net/gh/moezx/cdn@3.1.9/img/Sakura/images/level/level_5.svg" style="height: 1.5em; max-height: 1.5em; display: inline-block;"></span>';
} else if ($author_count >= 160) {
echo '<span class="showGrade6" title="Lv6"><img src="https://cdn.jsdelivr.net/gh/moezx/cdn@3.1.9/img/Sakura/images/level/level_6.svg" style="height: 1.5em; max-height: 1.5em; display: inline-block;"></span>';
2018-05-26 18:59:29 +08:00
}
2019-12-27 10:01:04 +08:00
}
2018-05-26 18:59:29 +08:00
/**
2019-06-05 18:04:41 +08:00
* post views
2018-05-26 18:59:29 +08:00
*/
2019-12-27 10:01:04 +08:00
function restyle_text($number)
{
2019-06-05 18:04:41 +08:00
switch (akina_option('statistics_format')) {
case "type_2": //23,333 次访问
return number_format($number);
break;
case "type_3": //23 333 次访问
return number_format($number, 0, '.', ' ');
break;
case "type_4": //23k 次访问
2019-12-27 10:01:04 +08:00
if ($number >= 1000) {
return round($number / 1000, 2) . 'k';
} else {
2019-06-05 18:04:41 +08:00
return $number;
}
break;
default:
return $number;
2018-05-26 18:59:29 +08:00
}
}
2019-12-27 10:01:04 +08:00
function set_post_views()
{
2019-11-22 10:33:36 +08:00
if (is_singular()) {
global $post;
$post_id = intval($post->ID);
2019-12-27 10:01:04 +08:00
if ($post_id) {
$views = (int) get_post_meta($post_id, 'views', true);
if (!update_post_meta($post_id, 'views', ($views + 1))) {
2019-11-22 10:33:36 +08:00
add_post_meta($post_id, 'views', 1, true);
}
2018-05-26 18:59:29 +08:00
}
}
}
2019-11-22 10:33:36 +08:00
2018-05-26 18:59:29 +08:00
add_action('get_header', 'set_post_views');
2019-12-27 10:01:04 +08:00
function get_post_views($post_id)
{
if (akina_option('statistics_api') == 'wp_statistics') {
2019-06-07 17:53:06 +08:00
if (!function_exists('wp_statistics_pages')) {
2019-10-30 21:47:14 +08:00
return __('Please install pulgin <a href="https://wordpress.org/plugins/wp-statistics/" target="_blank">WP-Statistics</a>', 'sakura');
2019-06-05 18:04:41 +08:00
} else {
2019-12-27 10:01:04 +08:00
return restyle_text(wp_statistics_pages('total', 'uri', $post_id));
2019-06-05 18:04:41 +08:00
}
} else {
2019-11-22 10:33:36 +08:00
$views = get_post_meta($post_id, 'views', true);
2019-12-27 10:01:04 +08:00
if ($views == '') {
2019-06-05 18:04:41 +08:00
return 0;
2019-12-27 10:01:04 +08:00
} else {
2019-06-05 18:04:41 +08:00
return restyle_text($views);
}
}
2019-12-27 10:01:04 +08:00
}
2018-05-26 18:59:29 +08:00
/*
* Ajax点赞
*/
add_action('wp_ajax_nopriv_specs_zan', 'specs_zan');
add_action('wp_ajax_specs_zan', 'specs_zan');
2019-12-27 10:01:04 +08:00
function specs_zan()
{
global $wpdb, $post;
2018-05-26 18:59:29 +08:00
$id = $_POST["um_id"];
$action = $_POST["um_action"];
2019-12-27 10:01:04 +08:00
if ($action == 'ding') {
$specs_raters = get_post_meta($id, 'specs_zan', true);
2018-05-26 18:59:29 +08:00
$expire = time() + 99999999;
$domain = ($_SERVER['HTTP_HOST'] != 'localhost') ? $_SERVER['HTTP_HOST'] : false; // make cookies work with localhost
2019-12-27 10:01:04 +08:00
setcookie('specs_zan_' . $id, $id, $expire, '/', $domain, false);
2018-05-26 18:59:29 +08:00
if (!$specs_raters || !is_numeric($specs_raters)) {
update_post_meta($id, 'specs_zan', 1);
2019-12-27 10:01:04 +08:00
} else {
2018-05-26 18:59:29 +08:00
update_post_meta($id, 'specs_zan', ($specs_raters + 1));
}
2019-12-27 10:01:04 +08:00
echo get_post_meta($id, 'specs_zan', true);
}
2018-05-26 18:59:29 +08:00
die;
}
/*
* 友情链接
*/
2019-12-27 10:01:04 +08:00
function get_the_link_items($id = null)
{
$bookmarks = get_bookmarks('orderby=date&category=' . $id);
$output = '';
if (!empty($bookmarks)) {
$output .= '<ul class="link-items fontSmooth">';
foreach ($bookmarks as $bookmark) {
if (empty($bookmark->link_description)) {
$bookmark->link_description = __('This guy is so lazy ╮(╯▽╰)╭', 'sakura');
}
if (empty($bookmark->link_image)) {
$bookmark->link_image = 'https://view.moezx.cc/images/2017/12/30/Transparent_Akkarin.th.jpg';
}
$output .= '<li class="link-item"><a class="link-item-inner effect-apollo" href="' . $bookmark->link_url . '" title="' . $bookmark->link_description . '" target="_blank" rel="friend"><img class="lazyload" onerror="imgError(this,1)" data-src="' . $bookmark->link_image . '" src="https://cdn.jsdelivr.net/gh/moezx/cdn@3.0.2/img/svg/loader/trans.ajax-spinner-preloader.svg"><span class="sitename">' . $bookmark->link_name . '</span><div class="linkdes">' . $bookmark->link_description . '</div></a></li>';
}
$output .= '</ul>';
}
return $output;
2018-05-26 18:59:29 +08:00
}
2019-12-27 10:01:04 +08:00
function get_link_items()
{
$linkcats = get_terms('link_category');
if (!empty($linkcats)) {
foreach ($linkcats as $linkcat) {
$result .= '<h3 class="link-title"><span class="link-fix">' . $linkcat->name . '</span></h3>';
if ($linkcat->description) {
$result .= '<div class="link-description">' . $linkcat->description . '</div>';
}
$result .= get_the_link_items($linkcat->term_id);
}
} else {
$result = get_the_link_items();
}
return $result;
}
2018-05-26 18:59:29 +08:00
/*
* Gravatar头像使用中国服务器
*/
2019-12-27 10:01:04 +08:00
function gravatar_cn($url)
{
2021-07-22 14:49:30 +08:00
$gravatar_url = array('dn-qiniu-avatar.qbox.me/avatar','cdn.v2ex.com/gravatar','gravatar.loli.net/avatar','gravatar.zeruns.tech/avatar');
2020-01-11 23:04:31 +08:00
//return str_replace($gravatar_url, 'cn.gravatar.com', $url);
//官方服务器近期大陆访问 429建议使用镜像
return str_replace( $gravatar_url, akina_option('gravatar_proxy'), $url );
}
if(akina_option('gravatar_proxy')){
add_filter('get_avatar_url', 'gravatar_cn', 4);
2018-05-26 18:59:29 +08:00
}
/*
* 自定义默认头像
*/
2019-12-27 10:01:04 +08:00
add_filter('avatar_defaults', 'mytheme_default_avatar');
2018-05-26 18:59:29 +08:00
2019-12-27 10:01:04 +08:00
function mytheme_default_avatar($avatar_defaults)
{
2018-05-26 18:59:29 +08:00
//$new_avatar_url = get_template_directory_uri() . '/images/default_avatar.png';
2019-12-27 10:01:04 +08:00
$new_avatar_url = 'https://cn.gravatar.com/avatar/b745710ae6b0ce9dfb13f5b7c0956be1';
2018-05-26 18:59:29 +08:00
$avatar_defaults[$new_avatar_url] = 'Default Avatar';
return $avatar_defaults;
}
/*
2019-12-27 10:01:04 +08:00
* 阻止站内文章互相Pingback
2018-05-26 18:59:29 +08:00
*/
2019-12-27 10:01:04 +08:00
function theme_noself_ping(&$links)
{
$home = get_option('home');
foreach ($links as $l => $link) {
if (0 === strpos($link, $home)) {
unset($links[$l]);
}
}
2018-05-26 18:59:29 +08:00
2019-12-27 10:01:04 +08:00
}
add_action('pre_ping', 'theme_noself_ping');
2018-05-26 18:59:29 +08:00
/*
* 订制body类
2019-12-27 10:01:04 +08:00
*/
function akina_body_classes($classes)
{
// Adds a class of group-blog to blogs with more than 1 published author.
if (is_multi_author()) {
$classes[] = 'group-blog';
}
// Adds a class of hfeed to non-singular pages.
if (!is_singular()) {
$classes[] = 'hfeed';
}
// 定制中文字体class
$classes[] = 'chinese-font';
/*if(!wp_is_mobile()) {
$classes[] = 'serif';
}*/
$classes[] = $_COOKIE['dark'.akina_option('cookie_version', '')] == '1' ? 'dark' : ' ';
2019-12-27 10:01:04 +08:00
return $classes;
}
add_filter('body_class', 'akina_body_classes');
2018-05-26 18:59:29 +08:00
/*
* 图片CDN
*/
2019-12-27 10:01:04 +08:00
add_filter('upload_dir', 'wpjam_custom_upload_dir');
function wpjam_custom_upload_dir($uploads)
{
$upload_path = '';
$upload_url_path = akina_option('qiniu_cdn');
if (empty($upload_path) || 'wp-content/uploads' == $upload_path) {
$uploads['basedir'] = WP_CONTENT_DIR . '/uploads';
} elseif (0 !== strpos($upload_path, ABSPATH)) {
$uploads['basedir'] = path_join(ABSPATH, $upload_path);
} else {
$uploads['basedir'] = $upload_path;
}
2018-05-26 18:59:29 +08:00
2019-12-27 10:01:04 +08:00
$uploads['path'] = $uploads['basedir'] . $uploads['subdir'];
2018-05-26 18:59:29 +08:00
2019-12-27 10:01:04 +08:00
if ($upload_url_path) {
$uploads['baseurl'] = $upload_url_path;
$uploads['url'] = $uploads['baseurl'] . $uploads['subdir'];
}
return $uploads;
2018-05-26 18:59:29 +08:00
}
/*
* 删除自带小工具
2019-12-27 10:01:04 +08:00
*/
function unregister_default_widgets()
{
unregister_widget("WP_Widget_Pages");
unregister_widget("WP_Widget_Calendar");
unregister_widget("WP_Widget_Archives");
unregister_widget("WP_Widget_Links");
unregister_widget("WP_Widget_Meta");
unregister_widget("WP_Widget_Search");
2019-11-16 10:13:37 +08:00
//unregister_widget("WP_Widget_Text");
2019-12-27 10:01:04 +08:00
unregister_widget("WP_Widget_Categories");
unregister_widget("WP_Widget_Recent_Posts");
2019-11-16 10:13:37 +08:00
//unregister_widget("WP_Widget_Recent_Comments");
//unregister_widget("WP_Widget_RSS");
//unregister_widget("WP_Widget_Tag_Cloud");
2019-12-27 10:01:04 +08:00
unregister_widget("WP_Nav_Menu_Widget");
2018-05-26 18:59:29 +08:00
}
add_action("widgets_init", "unregister_default_widgets", 11);
/**
* Jetpack setup function.
*
* See: https://jetpack.com/support/infinite-scroll/
* See: https://jetpack.com/support/responsive-videos/
*/
2019-12-27 10:01:04 +08:00
function akina_jetpack_setup()
{
// Add theme support for Infinite Scroll.
add_theme_support('infinite-scroll', array(
'container' => 'main',
'render' => 'akina_infinite_scroll_render',
'footer' => 'page',
));
2018-05-26 18:59:29 +08:00
2019-12-27 10:01:04 +08:00
// Add theme support for Responsive Videos.
add_theme_support('jetpack-responsive-videos');
2018-05-26 18:59:29 +08:00
}
2019-12-27 10:01:04 +08:00
add_action('after_setup_theme', 'akina_jetpack_setup');
2018-05-26 18:59:29 +08:00
/**
* Custom render function for Infinite Scroll.
*/
2019-12-27 10:01:04 +08:00
function akina_infinite_scroll_render()
{
while (have_posts()) {
the_post();
if (is_search()):
get_template_part('tpl/content', 'search');
else:
get_template_part('tpl/content', get_post_format());
endif;
}
2018-05-26 18:59:29 +08:00
}
/*
* 编辑器增强
*/
2019-12-27 10:01:04 +08:00
function enable_more_buttons($buttons)
{
$buttons[] = 'hr';
$buttons[] = 'del';
$buttons[] = 'sub';
$buttons[] = 'sup';
$buttons[] = 'fontselect';
$buttons[] = 'fontsizeselect';
$buttons[] = 'cleanup';
$buttons[] = 'styleselect';
$buttons[] = 'wp_page';
$buttons[] = 'anchor';
$buttons[] = 'backcolor';
return $buttons;
}
2018-05-26 18:59:29 +08:00
add_filter("mce_buttons_3", "enable_more_buttons");
// 下载按钮
2019-12-27 10:01:04 +08:00
function download($atts, $content = null)
{
return '<a class="download" href="' . $content . '" rel="external"
target="_blank" title="下载地址">
<span><i class="iconfont down icon-pulldown"></i>Download</span></a>';}
add_shortcode("download", "download");
add_action('after_wp_tiny_mce', 'bolo_after_wp_tiny_mce');
function bolo_after_wp_tiny_mce($mce_settings)
{
?>
<script type="text/javascript">
2018-05-26 18:59:29 +08:00
QTags.addButton( 'download', '下载按钮', "[download]下载地址[/download]" );
function bolo_QTnextpage_arg1() {
2019-12-27 10:01:04 +08:00
}
</script>
<?php }
2018-05-26 18:59:29 +08:00
/*
* 后台登录页
* @M.J
2019-12-27 10:01:04 +08:00
*/
2018-05-26 18:59:29 +08:00
//Login Page style
2019-12-27 10:01:04 +08:00
function custom_login()
{
//echo '<link rel="stylesheet" type="text/css" href="' . get_bloginfo('template_directory') . '/inc/login.css" />'."\n";
echo '<link rel="stylesheet" type="text/css" href="' . get_template_directory_uri() . '/inc/login.css?' . SAKURA_VERSION . '" />' . "\n";
//echo '<script type="text/javascript" src="'.get_bloginfo('template_directory').'/js/jquery.min.js"></script>'."\n";
2020-08-09 14:49:16 +08:00
echo '<script type="text/javascript" src="https://cdn.jsdelivr.net/gh/jquery/jquery@1.9.0/jquery.min.js"></script>' . "\n";
2018-05-26 18:59:29 +08:00
}
add_action('login_head', 'custom_login');
//Login Page Title
2019-12-27 10:01:04 +08:00
function custom_headertitle($title)
{
return get_bloginfo('name');
2018-05-26 18:59:29 +08:00
}
2019-12-27 10:01:04 +08:00
add_filter('login_headertitle', 'custom_headertitle');
2018-05-26 18:59:29 +08:00
//Login Page Link
2019-12-27 10:01:04 +08:00
function custom_loginlogo_url($url)
{
return esc_url(home_url('/'));
2018-05-26 18:59:29 +08:00
}
2019-12-27 10:01:04 +08:00
add_filter('login_headerurl', 'custom_loginlogo_url');
2018-05-26 18:59:29 +08:00
//Login Page Footer
2019-12-27 10:01:04 +08:00
function custom_html()
{
if (akina_option('login_bg')) {
$loginbg = akina_option('login_bg');
} else {
$loginbg = 'https://cdn.jsdelivr.net/gh/mashirozx/Sakura@3.2.7/images/hd.png';
}
echo '<script type="text/javascript" src="' . get_template_directory_uri() . '/js/login.js"></script>' . "\n";
echo '<script type="text/javascript">' . "\n";
echo 'jQuery("body").prepend("<div class=\"loading\"><img src=\"https://cdn.jsdelivr.net/gh/moezx/cdn@3.1.9/img/Sakura/images/login_loading.gif\" width=\"58\" height=\"10\"></div><div id=\"bg\"><img /></div>");' . "\n";
echo 'jQuery(\'#bg\').children(\'img\').attr(\'src\', \'' . $loginbg . '\').load(function(){' . "\n";
echo ' resizeImage(\'bg\');' . "\n";
echo ' jQuery(window).bind("resize", function() { resizeImage(\'bg\'); });' . "\n";
echo ' jQuery(\'.loading\').fadeOut();' . "\n";
echo '});';
echo '</script>' . "\n";
echo '<script>
2018-05-26 18:59:29 +08:00
function verificationOK(){
var x, y, z = "verification";
var x=$(\'#loginform\').find(\'input[name="verification"]\').val();
//var x=document.forms["loginform"]["verification"].value; //原生js实现
var y=$(\'#registerform\').find(\'input[name="verification"]\').val();
var z=$(\'#lostpasswordform\').find(\'input[name="verification"]\').val();
if (x=="verification" || y=="verification" || z=="verification"){
alert("Please slide the block to verificate!");
return false;
}
}
$(document).ready(function(){
$( \'<p><div id="verification-slider"><div id="slider"><div id="slider_bg"></div><span id="label">»</span><span id="labelTip">Slide to Verificate</span></div><input type="hidden" name="verification" value="verification" /></div><p>\' ).insertBefore( $( ".submit" ) );
$(\'form\').attr(\'onsubmit\',\'return verificationOK();\');
2019-12-27 10:01:04 +08:00
$(\'h1 a\').attr(\'style\',\'background-image: url(' . akina_option('logo_img') . '); \');
2018-05-26 18:59:29 +08:00
$(".forgetmenot").replaceWith(\'<p class="forgetmenot">Remember Me<input name="rememberme" id="rememberme" value="forever" type="checkbox"><label for="rememberme" style="float: right;margin-top: 5px;transform: scale(2);margin-right: -10px;"></label></p>\');
});
</script>';
2019-12-27 10:01:04 +08:00
echo '<script type="text/javascript">
2018-05-26 18:59:29 +08:00
var startTime = 0;
var endTime = 0;
var numTime = 0;
$(function () {
var slider = new SliderUnlock("#slider",{
successLabelTip : "OK"
},function(){
var sli_width = $("#slider_bg").width();
$(\'#verification-slider\').html(\'\').append(\'<input id="verification-ok" class="input" type="text" size="25" value="OK!" name="verification" disabled="true" />\');
2019-12-27 10:01:04 +08:00
2018-05-26 18:59:29 +08:00
endTime = nowTime();
numTime = endTime-startTime;
endTime = 0;
startTime = 0;
// 获取到滑动使用的时间 滑动的宽度
// alert( numTime );
// alert( sli_width );
});
slider.init();
})
/**
* 获取时间精确到毫秒
* @type
*/
function nowTime(){
var myDate = new Date();
var H = myDate.getHours();//获取小时
var M = myDate.getMinutes(); //获取分钟
var S = myDate.getSeconds();//获取秒
var MS = myDate.getMilliseconds();//获取毫秒
var milliSeconds = H * 3600 * 1000 + M * 60 * 1000 + S * 1000 + MS;
return milliSeconds;
}
</script>
2019-12-27 10:01:04 +08:00
<script type="text/javascript" src="' . get_template_directory_uri() . '/user/verification.js"></script>';
2018-05-26 18:59:29 +08:00
}
add_action('login_footer', 'custom_html');
//Login message
//* Add custom message to WordPress login page
2019-12-27 10:01:04 +08:00
function smallenvelop_login_message($message)
{
if (empty($message)) {
2018-05-26 18:59:29 +08:00
return '<p class="message"><strong>You may try 3 times for every 5 minutes!</strong></p>';
} else {
return $message;
}
}
//add_filter( 'login_message', 'smallenvelop_login_message' );
//Fix password reset bug </>
2019-12-27 10:01:04 +08:00
function resetpassword_message_fix($message)
{
2018-05-26 18:59:29 +08:00
$message = str_replace("<", "", $message);
2019-12-27 10:01:04 +08:00
$message = str_replace(">", "", $message);
return $message;
2018-05-26 18:59:29 +08:00
}
2019-12-27 10:01:04 +08:00
add_filter('retrieve_password_message', 'resetpassword_message_fix');
2018-05-26 18:59:29 +08:00
2019-05-19 17:18:32 +08:00
//Fix register email bug </>
2019-12-27 10:01:04 +08:00
function new_user_message_fix($message)
{
$show_register_ip = "注册IP | Registration IP: " . get_the_user_ip() . " (" . convertip(get_the_user_ip()) . ")\r\n\r\n如非本人操作请忽略此邮件 | Please ignore this email if this was not your operation.\r\n\r\n";
$message = str_replace("To set your password, visit the following address:", $show_register_ip . "在此设置密码 | To set your password, visit the following address:", $message);
$message = str_replace("<", "", $message);
$message = str_replace(">", "\r\n\r\n设置密码后在此登陆 | Login here after setting password: ", $message);
return $message;
2019-05-19 17:18:32 +08:00
}
2019-12-27 10:01:04 +08:00
add_filter('wp_new_user_notification_email', 'new_user_message_fix');
2019-05-19 17:18:32 +08:00
2018-05-26 18:59:29 +08:00
/*
* 评论邮件回复
*/
2019-12-27 10:01:04 +08:00
function comment_mail_notify($comment_id)
{
$mail_user_name = akina_option('mail_user_name') ? akina_option('mail_user_name') : 'poi';
2018-05-26 18:59:29 +08:00
$comment = get_comment($comment_id);
$parent_id = $comment->comment_parent ? $comment->comment_parent : '';
$spam_confirmed = $comment->comment_approved;
2019-12-27 10:01:04 +08:00
$mail_notify = akina_option('mail_notify') ? get_comment_meta($parent_id, 'mail_notify', false) : false;
2019-10-30 20:17:35 +08:00
$admin_notify = akina_option('admin_notify') ? '1' : (get_comment($parent_id)->comment_author_email != get_bloginfo('admin_email') ? '1' : '0');
2019-12-27 10:01:04 +08:00
if (($parent_id != '') && ($spam_confirmed != 'spam') && ($admin_notify != '0') && (!$mail_notify)) {
$wp_email = $mail_user_name . '@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME']));
$to = trim(get_comment($parent_id)->comment_author_email);
$subject = '你在 [' . get_option("blogname") . '] 的留言有了回应';
$message = '
2018-05-26 18:59:29 +08:00
<div style="background: white;
width: 95%;
max-width: 800px;
margin: auto auto;
border-radius: 5px;
border:orange 1px solid;
overflow: hidden;
-webkit-box-shadow: 0px 0px 20px 0px rgba(0, 0, 0, 0.12);
box-shadow: 0px 0px 20px 0px rgba(0, 0, 0, 0.18);">
<header style="overflow: hidden;">
<img style="width:100%;z-index: 666;" src="https://cdn.jsdelivr.net/gh/moezx/cdn@3.1.4/img/other/head.jpg">
</header>
<div style="padding: 5px 20px;">
<p style="position: relative;
color: white;
float: left;
z-index: 999;
background: orange;
padding: 5px 30px;
margin: -25px auto 0 ;
box-shadow: 5px 5px 5px rgba(0, 0, 0, 0.30)">Dear&nbsp;' . trim(get_comment($parent_id)->comment_author) . '</p>
<br>
2019-12-27 10:01:04 +08:00
<h3>您有一条来自<a style="text-decoration: none;color: orange " target="_blank" href="' . home_url() . '/">' . get_option("blogname") . '</a>的回复</h3>
2018-05-26 18:59:29 +08:00
<br>
<p style="font-size: 14px;">您在文章《' . get_the_title($comment->comment_post_ID) . '》上发表的评论:</p>
<div style="border-bottom:#ddd 1px solid;border-left:#ddd 1px solid;padding-bottom:20px;background-color:#eee;margin:15px 0px;padding-left:20px;padding-right:20px;border-top:#ddd 1px solid;border-right:#ddd 1px solid;padding-top:20px">'
. trim(get_comment($parent_id)->comment_content) . '</div>
2018-05-26 18:59:29 +08:00
<p style="font-size: 14px;">' . trim($comment->comment_author) . ' 给您的回复如下:</p>
<div style="border-bottom:#ddd 1px solid;border-left:#ddd 1px solid;padding-bottom:20px;background-color:#eee;margin:15px 0px;padding-left:20px;padding-right:20px;border-top:#ddd 1px solid;border-right:#ddd 1px solid;padding-top:20px">'
. trim($comment->comment_content) . '</div>
2018-05-26 18:59:29 +08:00
<div style="text-align: center;">
<img src="https://cdn.jsdelivr.net/gh/moezx/cdn@3.1.4/img/other/hr.png" alt="hr" style="width:100%;
margin:5px auto 5px auto;
display: block;">
<a style="text-transform: uppercase;
text-decoration: none;
font-size: 14px;
border: 2px solid #6c7575;
color: #2f3333;
padding: 10px;
display: inline-block;
margin: 10px auto 0; " target="_blank" href="' . htmlspecialchars(get_comment_link($parent_id)) . '">点击查看回复的完整內容</a>
</div>
<p style="font-size: 12px;text-align: center;color: #999;">本邮件为系统自动发出,请勿直接回复<br>
2019-12-27 10:01:04 +08:00
&copy; ' . date(Y) . ' ' . get_option("blogname") . '</p>
</div>
2018-05-26 18:59:29 +08:00
</div>
';
2019-12-27 10:01:04 +08:00
$message = convert_smilies($message);
$message = str_replace("{{", '<img src="https://cdn.jsdelivr.net/gh/moezx/cdn@2.9.4/img/bili/hd/ic_emoji_', $message);
$message = str_replace("}}", '.png" alt="emoji" style="height: 2em; max-height: 2em;">', $message);
$message = str_replace('{UPLOAD}', 'https://i.loli.net/', $message);
$message = str_replace('[/img][img]', '[/img^img]', $message);
$message = str_replace('[img]', '<img src="', $message);
$message = str_replace('[/img]', '" style="width:80%;display: block;margin-left: auto;margin-right: auto;">', $message);
$message = str_replace('[/img^img]', '" style="width:80%;display: block;margin-left: auto;margin-right: auto;"><img src="', $message);
$from = "From: \"" . get_option('blogname') . "\" <$wp_email>";
$headers = "$from\nContent-Type: text/html; charset=" . get_option('blog_charset') . "\n";
wp_mail($to, $subject, $message, $headers);
}
2018-05-26 18:59:29 +08:00
}
add_action('comment_post', 'comment_mail_notify');
/*
* 链接新窗口打开
*/
2019-12-27 10:01:04 +08:00
function rt_add_link_target($content)
{
$content = str_replace('<a', '<a rel="nofollow"', $content);
// use the <a> tag to split into segments
$bits = explode('<a ', $content);
// loop though the segments
foreach ($bits as $key => $bit) {
// fix the target="_blank" bug after the link
if (strpos($bit, 'href') === false) {
continue;
}
// fix the target="_blank" bug in the codeblock
if (strpos(preg_replace('/code([\s\S]*?)\/code[\s]*/m', 'temp', $content), $bit) === false) {
continue;
}
// find the end of each link
$pos = strpos($bit, '>');
// check if there is an end (only fails with malformed markup)
if ($pos !== false) {
// get a string with just the link's attibutes
$part = substr($bit, 0, $pos);
// for comparison, get the current site/network url
$siteurl = network_site_url();
// if the site url is in the attributes, assume it's in the href and skip, also if a target is present
if (strpos($part, $siteurl) === false && strpos($part, 'target=') === false) {
// add the target attribute
$bits[$key] = 'target="_blank" ' . $bits[$key];
}
}
}
// re-assemble the content, and return it
return implode('<a ', $bits);
2018-05-26 18:59:29 +08:00
}
2019-12-27 10:01:04 +08:00
add_filter('comment_text', 'rt_add_link_target');
2018-05-26 18:59:29 +08:00
// 评论通过BBCode插入图片
2019-12-27 10:01:04 +08:00
function comment_picture_support($content)
{
$content = str_replace('http://', 'https://', $content); // 干掉任何可能的 http
$content = str_replace('{UPLOAD}', 'https://i.loli.net/', $content);
$content = str_replace('[/img][img]', '[/img^img]', $content);
$content = str_replace('[img]', '<br><img src="https://cdn.jsdelivr.net/gh/moezx/cdn@3.0.2/img/svg/loader/trans.ajax-spinner-preloader.svg" data-src="', $content);
$content = str_replace('[/img]', '" class="lazyload comment_inline_img" onerror="imgError(this)"><br>', $content);
$content = str_replace('[/img^img]', '" class="lazyload comment_inline_img" onerror="imgError(this)"><img src="https://cdn.jsdelivr.net/gh/moezx/cdn@3.0.2/img/svg/loader/trans.ajax-spinner-preloader.svg" data-src="', $content);
2018-05-26 18:59:29 +08:00
return $content;
}
2019-12-27 10:01:04 +08:00
add_filter('comment_text', 'comment_picture_support');
2018-05-26 18:59:29 +08:00
/*
* 修改评论表情调用路径
*/
2019-12-27 10:01:04 +08:00
add_filter('smilies_src', 'custom_smilies_src', 1, 10);
function custom_smilies_src($img_src, $img, $siteurl)
{
return 'https://cdn.jsdelivr.net/gh/moezx/cdn@3.1.9/img/Sakura/images/smilies/' . $img;
2018-05-26 18:59:29 +08:00
}
// 简单遍历系统表情库,今后应考虑标识表情包名——使用增加的扩展名,同时保留原有拓展名
// 还有一个思路是根据表情调用路径来判定<-- 此法最好!
// 贴吧
2019-12-27 10:01:04 +08:00
function push_smilies()
{
2018-05-26 18:59:29 +08:00
global $wpsmiliestrans;
foreach ($wpsmiliestrans as $k => $v) {
2019-12-27 10:01:04 +08:00
$Sname = str_replace(":", "", $k);
2018-05-26 18:59:29 +08:00
$Svalue = $v;
2019-12-27 10:01:04 +08:00
$return_smiles = $return_smiles . '<span title="' . $Sname . '" onclick="grin(' . "'" . $Sname . "'" . ')"><img src="https://cdn.jsdelivr.net/gh/moezx/cdn@3.1.9/img/Sakura/images/smilies/' . $Svalue . '" /></span>';
2018-05-26 18:59:29 +08:00
}
2019-12-27 10:01:04 +08:00
return $return_smiles;
2018-05-26 18:59:29 +08:00
}
2019-12-27 10:01:04 +08:00
function smilies_reset()
{
global $wpsmiliestrans;
2018-05-26 18:59:29 +08:00
// don't bother setting up smilies if they are disabled
2019-12-27 10:01:04 +08:00
if (!get_option('use_smilies')) {
return;
}
2018-05-26 18:59:29 +08:00
2019-06-25 21:32:24 +08:00
$wpsmiliestrans = array(
2018-05-26 18:59:29 +08:00
':good:' => 'icon_good.gif',
':han:' => 'icon_han.gif',
':spray:' => 'icon_spray.gif',
':Grievance:' => 'icon_Grievance.gif',
':shui:' => 'icon_shui.gif',
':reluctantly:' => 'icon_reluctantly.gif',
':anger:' => 'icon_anger.gif',
':tongue:' => 'icon_tongue.gif',
':se:' => 'icon_se.gif',
':haha:' => 'icon_haha.gif',
':rmb:' => 'icon_rmb.gif',
':doubt:' => 'icon_doubt.gif',
':tear:' => 'icon_tear.gif',
':surprised2:' => 'icon_surprised2.gif',
':Happy:' => 'icon_Happy.gif',
':ku:' => 'icon_ku.gif',
':surprised:' => 'icon_surprised.gif',
':theblackline:' => 'icon_theblackline.gif',
':smilingeyes:' => 'icon_smilingeyes.gif',
':spit:' => 'icon_spit.gif',
':huaji:' => 'icon_huaji.gif',
':bbd:' => 'icon_bbd.gif',
':hu:' => 'icon_hu.gif',
':shame:' => 'icon_shame.gif',
':naive:' => 'icon_naive.gif',
':rbq:' => 'icon_rbq.gif',
':britan:' => 'icon_britan.gif',
':aa:' => 'icon_aa.gif',
':niconiconi:' => 'icon_niconiconi.gif',
':niconiconi-t:' => 'icon_niconiconi_t.gif',
':niconiconit:' => 'icon_niconiconit.gif',
':awesome:' => 'icon_awesome.gif',
2019-06-25 21:32:24 +08:00
);
2018-05-26 18:59:29 +08:00
}
smilies_reset();
2019-12-27 10:01:04 +08:00
function push_emoji_panel()
{
2018-05-26 18:59:29 +08:00
return '
<a class="emoji-item">(⌒▽⌒)</a>
<a class="emoji-item">( ̄▽ ̄)</a>
<a class="emoji-item">(=・ω・=)</a>
<a class="emoji-item">(`・ω・´)</a>
<a class="emoji-item">(〜 ̄△ ̄)</a>
<a class="emoji-item">(・∀・)</a>
<a class="emoji-item">(°∀°)</a>
<a class="emoji-item">( ̄3 ̄)</a>
<a class="emoji-item">( ̄▽ ̄)</a>
<a class="emoji-item">(´_ゝ)</a>
<a class="emoji-item">←_←</a>
<a class="emoji-item">→_→</a>
<a class="emoji-item">(&lt;_&lt;)</a>
<a class="emoji-item">(&gt;_&gt;)</a>
<a class="emoji-item">(;¬_¬)</a>
<a class="emoji-item">("▔□▔)/</a>
<a class="emoji-item">(゚Д゚≡゚д゚)!?</a>
<a class="emoji-item">Σ(゚д゚;)</a>
<a class="emoji-item">Σ( ̄□ ̄||)</a>
<a class="emoji-item">(’;ω;‘)</a>
<a class="emoji-item">/TДT)/</a>
<a class="emoji-item">(^・ω・^ )</a>
<a class="emoji-item">(。・ω・。)</a>
<a class="emoji-item">(● ̄() ̄●)</a>
<a class="emoji-item">ε=ε=(ノ≧∇≦)</a>
<a class="emoji-item">(・_・)</a>
<a class="emoji-item">(-_-#)</a>
<a class="emoji-item">( ̄へ ̄)</a>
<a class="emoji-item">( ̄ε(# ̄)Σ</a>
<a class="emoji-item">(‘Д’)</a>
<a class="emoji-item">#-_-)┯━┯</a>
<a class="emoji-item">(╯°口°)(┴—┴</a>
<a class="emoji-item">←◡←</a>
<a class="emoji-item">( ♥д♥)</a>
<a class="emoji-item">_(:3」∠)_</a>
<a class="emoji-item">Σ&gt;(〃°ω°〃)♡→</a>
<a class="emoji-item">( ⁄•⁄ω⁄•⁄ )</a>
<a class="emoji-item">(╬゚д゚)▄︻┻┳═一</a>
<a class="emoji-item">*:( ε:)</a>
<a class="emoji-item">()</a>
<a class="emoji-item">()</a>
<a class="emoji-item">()</a>
<a class="emoji-item">(苦笑)</a>
';
}
2019-12-27 10:01:04 +08:00
function get_wp_root_path()
{
2018-05-26 18:59:29 +08:00
$base = dirname(__FILE__);
$path = false;
2019-12-27 10:01:04 +08:00
if (@file_exists(dirname(dirname($base)))) {
2018-05-26 18:59:29 +08:00
$path = dirname(dirname($base));
2019-12-27 10:01:04 +08:00
} else
if (@file_exists(dirname(dirname(dirname($base))))) {
2018-05-26 18:59:29 +08:00
$path = dirname(dirname(dirname($base)));
2019-12-27 10:01:04 +08:00
} else {
$path = false;
2018-05-26 18:59:29 +08:00
}
2019-12-27 10:01:04 +08:00
if ($path != false) {
2018-05-26 18:59:29 +08:00
$path = str_replace("\\", "/", $path);
}
return $path;
}
// bilibili smiles
$bilismiliestrans = array();
2019-12-27 10:01:04 +08:00
function push_bili_smilies()
{
global $bilismiliestrans;
$smiles_path = __DIR__ . "/images/smilies/bili/";
$name = array('baiyan', 'fadai', 'koubi', 'qinqin', 'weiqu', 'bishi', 'fanu', 'kun', 'se', 'weixiao', 'bizui', 'ganga', 'lengmo', 'shengbing', 'wunai', 'chan', 'guilian', 'liubixue', 'shengqi', 'xiaoku', 'daku', 'guzhang', 'liuhan', 'shuizhao', 'xieyanxiao', 'dalao', 'haixiu', 'liulei', 'sikao', 'yiwen', 'dalian', 'heirenwenhao', 'miantian', 'tiaokan', 'yun', 'dianzan', 'huaixiao', 'mudengkoudai', 'tiaopi', 'zaijian', 'doge', 'jingxia', 'nanguo', 'touxiao', 'zhoumei', 'facai', 'keai', 'outu', 'tuxue', 'zhuakuang');
$return_smiles = '';
for ($i = 0; $i < count($name); $i++) {
$img_size = getimagesize($smiles_path . $name[$i] . ".png");
$img_height = $img_size["1"];
// 选择面版
$return_smiles = $return_smiles . '<span class="emotion-secter emotion-item emotion-select-parent" onclick="grin(' . "'" . $name[$i] . "'" . ',type = \'Math\')" style="background-image: url(https://cdn.jsdelivr.net/gh/moezx/cdn@2.9.4/img/bili/hd/ic_emoji_' . $name[$i] . '.png);"><div class="img emotion-select-child" style="background-image: url(https://cdn.jsdelivr.net/gh/moezx/cdn@2.9.4/img/bili/' . $name[$i] . '.png);
animation-duration: ' . ($img_height / 32 * 40) . 'ms;
animation-timing-function: steps(' . ($img_height / 32) . ');
transform: translateY(-' . ($img_height - 32) . 'px);
height: ' . $img_height . 'px;
2018-05-26 18:59:29 +08:00
"></div></span>';
2019-12-27 10:01:04 +08:00
// 正文转换
$bilismiliestrans['{{' . $name[$i] . '}}'] = '<span class="emotion-inline emotion-item"><img src="https://cdn.jsdelivr.net/gh/moezx/cdn@2.9.4/img/bili/' . $name[$i] . '.png" class="img" style="/*background-image: url();*/
animation-duration: ' . ($img_height / 32 * 40) . 'ms;
animation-timing-function: steps(' . ($img_height / 32) . ');
transform: translateY(-' . ($img_height - 32) . 'px);
height: ' . $img_height . 'px;
2018-05-26 18:59:29 +08:00
"></span>';
}
return $return_smiles;
}
push_bili_smilies();
2019-12-27 10:01:04 +08:00
function bili_smile_filter($content)
{
2018-05-26 18:59:29 +08:00
global $bilismiliestrans;
2019-12-27 10:01:04 +08:00
$content = str_replace(array_keys($bilismiliestrans), $bilismiliestrans, $content);
2018-05-26 18:59:29 +08:00
return $content;
}
add_filter('the_content', 'bili_smile_filter'); //替换文章关键词
2019-12-27 10:01:04 +08:00
add_filter('comment_text', 'bili_smile_filter'); //替换评论关键词
2018-05-26 18:59:29 +08:00
2019-12-27 10:01:04 +08:00
function featuredtoRSS($content)
{
2018-05-26 18:59:29 +08:00
global $post;
2019-12-27 10:01:04 +08:00
if (has_post_thumbnail($post->ID)) {
$content = '<div>' . get_the_post_thumbnail($post->ID, 'medium', array('style' => 'margin-bottom: 15px;')) . '</div>' . $content;
2018-05-26 18:59:29 +08:00
}
return $content;
}
add_filter('the_excerpt_rss', 'featuredtoRSS');
add_filter('the_content_feed', 'featuredtoRSS');
//
2019-12-27 10:01:04 +08:00
function bili_smile_filter_rss($content)
{
$content = str_replace("{{", '<img src="https://cdn.jsdelivr.net/gh/moezx/cdn@2.9.4/img/bili/hd/ic_emoji_', $content);
$content = str_replace("}}", '.png" alt="emoji" style="height: 2em; max-height: 2em;">', $content);
$content = str_replace('[img]', '<img src="', $content);
$content = str_replace('[/img]', '" style="display: block;margin-left: auto;margin-right: auto;">', $content);
2018-05-26 18:59:29 +08:00
return $content;
}
2019-12-27 10:01:04 +08:00
add_filter('comment_text_rss', 'bili_smile_filter_rss'); //替换评论rss关键词
2018-05-26 18:59:29 +08:00
2019-12-27 10:01:04 +08:00
function toc_support($content)
{
$content = str_replace('[toc]', '<div class="has-toc have-toc"></div>', $content); // TOC 支持
$content = str_replace('[begin]', '<span class="begin">', $content); // 首字格式支持
$content = str_replace('[/begin]', '</span>', $content); // 首字格式支持
2018-05-26 18:59:29 +08:00
return $content;
}
add_filter('the_content', 'toc_support');
add_filter('the_excerpt_rss', 'toc_support');
add_filter('the_content_feed', 'toc_support');
// 显示访客当前 IP
2019-12-27 10:01:04 +08:00
function get_the_user_ip()
{
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
2018-05-26 18:59:29 +08:00
//check ip from share internet
2019-12-27 10:01:04 +08:00
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
2018-05-26 18:59:29 +08:00
//to check ip is pass from proxy
2019-12-27 10:01:04 +08:00
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ip = $_SERVER['REMOTE_ADDR'];
}
return apply_filters('wpb_get_ip', $ip);
2018-05-26 18:59:29 +08:00
}
2019-12-27 10:01:04 +08:00
add_shortcode('show_ip', 'get_the_user_ip');
2018-05-26 18:59:29 +08:00
/*歌词*/
2019-12-27 10:01:04 +08:00
function hero_get_lyric()
{
/** These are the lyrics to Hero */
$lyrics = "";
2018-05-26 18:59:29 +08:00
2019-12-27 10:01:04 +08:00
// Here we split it into lines
$lyrics = explode("\n", $lyrics);
2018-05-26 18:59:29 +08:00
2019-12-27 10:01:04 +08:00
// And then randomly choose a line
return wptexturize($lyrics[mt_rand(0, count($lyrics) - 1)]);
2018-05-26 18:59:29 +08:00
}
// This just echoes the chosen line, we'll position it later
2019-12-27 10:01:04 +08:00
function hello_hero()
{
$chosen = hero_get_lyric();
echo $chosen;
2018-05-26 18:59:29 +08:00
}
/*私密评论*/
add_action('wp_ajax_nopriv_siren_private', 'siren_private');
add_action('wp_ajax_siren_private', 'siren_private');
2019-12-27 10:01:04 +08:00
function siren_private()
{
2018-05-26 18:59:29 +08:00
$comment_id = $_POST["p_id"];
$action = $_POST["p_action"];
if ($action == 'set_private') {
update_comment_meta($comment_id, '_private', 'true');
$i_private = get_comment_meta($comment_ID, '_private', true);
if (!empty($i_private)) {
echo '否';
} else {
echo '是';
}
}
die;
}
//时间序列
2019-12-27 10:01:04 +08:00
function memory_archives_list()
{
if (true) {
$output = '<div id="archives"><p style="text-align:right;">[<span id="al_expand_collapse">' . __("All expand/collapse", "sakura") /*全部展开/收缩*/ . '</span>]<!-- (注: 点击月份可以展开)--></p>';
$the_query = new WP_Query('posts_per_page=-1&ignore_sticky_posts=1&post_type=post'); //update: 加上忽略置顶文章
$year = 0;
$mon = 0;
$i = 0;
$j = 0;
while ($the_query->have_posts()): $the_query->the_post();
$year_tmp = get_the_time('Y');
2018-05-26 18:59:29 +08:00
$mon_tmp = get_the_time('m');
2019-12-27 10:01:04 +08:00
$y = $year;
$m = $mon;
if ($mon != $mon_tmp && $mon > 0) {
$output .= '</ul></li>';
}
2021-11-13 18:34:05 +08:00
if ($mon == $mon_tmp && $year != $year_tmp ) {
$mon=$mon_tmp + 12;
$output .= '</ul>';
}
2019-12-27 10:01:04 +08:00
if ($year != $year_tmp && $year > 0) {
$output .= '</ul>';
}
2018-05-26 18:59:29 +08:00
if ($year != $year_tmp) {
$year = $year_tmp;
2019-12-27 10:01:04 +08:00
$output .= '<h3 class="al_year">' . $year . __(" ", "year", "sakura") . /*年*/' </h3><ul class="al_mon_list">'; //输出年份
2018-05-26 18:59:29 +08:00
}
if ($mon != $mon_tmp) {
$mon = $mon_tmp;
2019-12-27 10:01:04 +08:00
$output .= '<li class="al_li"><span class="al_mon"><span style="color:#0bf;">' . get_the_time('M') . '</span> (<span id="post-num"></span>' . __(" post(s)", "sakura") /*篇文章*/ . ')</span><ul class="al_post_list">'; //输出月份
2018-05-26 18:59:29 +08:00
}
2019-12-27 10:01:04 +08:00
$output .= '<li>' . '<a href="' . get_permalink() . '"><span style="color:#0bf;">' /*get_the_time('d'.__(" ","sakura")) 日*/ . '</span>' . get_the_title() . ' <span>(' . get_post_views(get_the_ID()) . ' <span class="fa fa-fire" aria-hidden="true"></span> / ' . get_comments_number('0', '1', '%') . ' <span class="fa fa-commenting" aria-hidden="true"></span>)</span></a></li>'; //输出文章日期和标题
2018-05-26 18:59:29 +08:00
endwhile;
wp_reset_postdata();
$output .= '</ul></li></ul> <!--<ul class="al_mon_list"><li><ul class="al_post_list" style="display: block;"><li>博客已经萌萌哒运行了<span id="monitorday"></span>天</li></ul></li></ul>--></div>';
#update_option('memory_archives_list', $output);
2019-12-27 10:01:04 +08:00
}
2018-05-26 18:59:29 +08:00
echo $output;
}
/*
* 隐藏 Dashboard
*/
/* Remove the "Dashboard" from the admin menu for non-admin users */
2019-12-27 10:01:04 +08:00
function remove_dashboard()
{
2018-05-26 18:59:29 +08:00
global $current_user, $menu, $submenu;
2019-11-13 20:32:10 +08:00
wp_get_current_user();
2018-05-26 18:59:29 +08:00
2019-12-27 10:01:04 +08:00
if (!in_array('administrator', $current_user->roles)) {
reset($menu);
$page = key($menu);
while ((__('Dashboard') != $menu[$page][0]) && next($menu)) {
$page = key($menu);
2018-05-26 18:59:29 +08:00
}
2019-12-27 10:01:04 +08:00
if (__('Dashboard') == $menu[$page][0]) {
unset($menu[$page]);
2018-05-26 18:59:29 +08:00
}
reset($menu);
$page = key($menu);
2019-12-27 10:01:04 +08:00
while (!$current_user->has_cap($menu[$page][1]) && next($menu)) {
$page = key($menu);
2018-05-26 18:59:29 +08:00
}
2019-12-27 10:01:04 +08:00
if (preg_match('#wp-admin/?(index.php)?$#', $_SERVER['REQUEST_URI']) &&
('index.php' != $menu[$page][2])) {
wp_redirect(get_option('siteurl') . '/wp-admin/profile.php');
2018-05-26 18:59:29 +08:00
}
}
}
add_action('admin_menu', 'remove_dashboard');
/**
2019-02-16 11:02:15 +08:00
* Filter the except length to 20 words. 限制摘要长度
2018-05-26 18:59:29 +08:00
*
* @param int $length Excerpt length.
* @return int (Maybe) modified excerpt length.
*/
2019-02-16 11:02:15 +08:00
2019-12-27 10:01:04 +08:00
function GBsubstr($string, $start, $length)
{
2019-02-16 11:02:15 +08:00
if (strlen($string) > $length) {
$str = null;
$len = 0;
$i = $start;
2019-12-27 10:01:04 +08:00
while ($len < $length) {
if (ord(substr($string, $i, 1)) > 0xc0) {
$str .= substr($string, $i, 3);
$i += 3;
} elseif (ord(substr($string, $i, 1)) > 0xa0) {
$str .= substr($string, $i, 2);
$i += 2;
} else {
$str .= substr($string, $i, 1);
$i++;
}
$len++;
2019-02-16 11:02:15 +08:00
}
return $str;
2019-12-27 10:01:04 +08:00
} else {
2019-02-16 11:02:15 +08:00
return $string;
}
}
2019-12-27 10:01:04 +08:00
function excerpt_length($exp)
{
if (!function_exists('mb_substr')) {
2019-02-16 11:02:15 +08:00
$exp = GBsubstr($exp, 0, 80);
} else {
/*
* To use mb_substr() function, you should uncomment "extension=php_mbstring.dll" in php.ini
*/
$exp = mb_substr($exp, 0, 80);
}
2019-12-27 10:01:04 +08:00
return $exp;
2018-05-26 18:59:29 +08:00
}
2019-12-27 10:01:04 +08:00
add_filter('the_excerpt', 'excerpt_length');
2018-05-26 18:59:29 +08:00
/*
* 后台路径
*/
/*
add_filter('site_url', 'wpadmin_filter', 10, 3);
2019-12-27 10:01:04 +08:00
function wpadmin_filter( $url, $path, $orig_scheme ) {
$old = array( "/(wp-admin)/");
$admin_dir = WP_ADMIN_DIR;
$new = array($admin_dir);
return preg_replace( $old, $new, $url, 1);
}
*/
function admin_ini()
{
wp_enqueue_style('admin-styles-fix-icon', get_site_url() . '/wp-includes/css/dashicons.css');
wp_enqueue_style('cus-styles-fit', get_template_directory_uri() . '/inc/css/dashboard-fix.css');
wp_enqueue_script('lazyload', 'https://cdn.jsdelivr.net/npm/lazyload@2.0.0-beta.2/lazyload.min.js');
2018-05-26 18:59:29 +08:00
}
2019-05-27 03:48:21 +08:00
add_action('admin_enqueue_scripts', 'admin_ini');
2018-05-26 18:59:29 +08:00
2019-12-27 10:01:04 +08:00
function custom_admin_js()
{
2019-02-16 11:02:15 +08:00
echo '<script>
window.onload=function(){
lazyload();
2019-12-27 10:01:04 +08:00
2019-02-20 00:20:17 +08:00
try{
document.querySelector("#scheme-tip .notice-dismiss").addEventListener("click", function(){
2019-12-27 10:01:04 +08:00
location.href="?scheme-tip-dismissed' . BUILD_VERSION . '";
2019-02-20 00:20:17 +08:00
});
} catch(e){}
2019-02-16 11:02:15 +08:00
}
</script>';
}
add_action('admin_footer', 'custom_admin_js');
2018-05-26 18:59:29 +08:00
/*
* 后台通知
*/
2019-12-27 10:01:04 +08:00
function scheme_tip()
{
$msg = '<b>Why not try the new admin dashboard color scheme <a href="/wp-admin/profile.php">here</a>?</b>';
if (get_user_locale(get_current_user_id()) == "zh_CN") {
$msg = '<b>试一试新后台界面<a href="/wp-admin/profile.php">配色方案</a>吧?</b>';
}
if (get_user_locale(get_current_user_id()) == "zh_TW") {
$msg = '<b>試一試新後台界面<a href="/wp-admin/profile.php">色彩配置</a>吧?</b>';
}
if (get_user_locale(get_current_user_id()) == "ja") {
$msg = '<b>新しい<a href="/wp-admin/profile.php">管理画面の配色</a>を試しますか?</b>';
}
if (get_user_locale(get_current_user_id()) == "ja-JP") {
$msg = '<b>新しい<a href="/wp-admin/profile.php">管理画面の配色</a>を試しますか?</b>';
}
2019-02-16 11:02:15 +08:00
$user_id = get_current_user_id();
2019-12-27 10:01:04 +08:00
if (!get_user_meta($user_id, 'scheme-tip-dismissed' . BUILD_VERSION)) {
echo '<div class="notice notice-success is-dismissible" id="scheme-tip"><p><b>' . $msg . '</b></p></div>';
2019-02-16 11:02:15 +08:00
}
2018-05-26 18:59:29 +08:00
}
2019-05-27 03:48:21 +08:00
2019-12-27 10:01:04 +08:00
add_action('admin_notices', 'scheme_tip');
2019-02-16 11:02:15 +08:00
2019-12-27 10:01:04 +08:00
function scheme_tip_dismissed()
{
2019-02-16 11:02:15 +08:00
$user_id = get_current_user_id();
2019-12-27 10:01:04 +08:00
if (isset($_GET['scheme-tip-dismissed' . BUILD_VERSION])) {
add_user_meta($user_id, 'scheme-tip-dismissed' . BUILD_VERSION, 'true', true);
}
2018-05-26 18:59:29 +08:00
}
2019-12-27 10:01:04 +08:00
add_action('admin_init', 'scheme_tip_dismissed');
2018-05-26 18:59:29 +08:00
2019-05-27 03:48:21 +08:00
//dashboard scheme
2019-12-27 10:01:04 +08:00
function dash_scheme($key, $name, $col1, $col2, $col3, $col4, $base, $focus, $current, $rules = "")
{
$hash = "color_1=" . str_replace("#", "", $col1) .
"&color_2=" . str_replace("#", "", $col2) .
"&color_3=" . str_replace("#", "", $col3) .
"&color_4=" . str_replace("#", "", $col4) .
"&rules=" . urlencode($rules);
2019-05-27 03:48:21 +08:00
wp_admin_css_color(
$key,
$name,
2019-12-27 10:01:04 +08:00
get_template_directory_uri() . "/inc/dash-scheme.php?" . $hash,
array($col1, $col2, $col3, $col4),
array('base' => $base, 'focus' => $focus, 'current' => $current)
2019-05-27 03:48:21 +08:00
);
}
//Sakura
2019-12-27 10:01:04 +08:00
dash_scheme($key = "sakura", $name = "Sakura🌸",
$col1 = '#8fbbb1', $col2 = '#bfd8d2', $col3 = '#fedcd2', $col4 = '#df744a',
$base = "#e5f8ff", $focus = "#fff", $current = "#fff",
$rules = "#adminmenu .wp-has-current-submenu .wp-submenu a,#adminmenu .wp-has-current-submenu.opensub .wp-submenu a,#adminmenu .wp-submenu a,#adminmenu a.wp-has-current-submenu:focus+.wp-submenu a,#wpadminbar .ab-submenu .ab-item,#wpadminbar .quicklinks .menupop ul li a,#wpadminbar .quicklinks .menupop.hover ul li a,#wpadminbar.nojs .quicklinks .menupop:hover ul li a,.folded #adminmenu .wp-has-current-submenu .wp-submenu a{color:#f3f2f1}body{background-image:url(https://view.moezx.cc/images/2018/01/03/sakura.png);background-attachment:fixed;}#wpcontent{background:rgba(255,255,255,.0)}.wp-core-ui .button-primary{background:#bfd8d2!important;border-color:#8fbbb1 #8fbbb1 #8fbbb1!important;color:#fff!important;box-shadow:0 1px 0 #8fbbb1!important;text-shadow:0 -1px 1px #8fbbb1,1px 0 1px #8fbbb1,0 1px 1px #8fbbb1,-1px 0 1px #8fbbb1!important}");
2019-05-27 03:48:21 +08:00
//custom
2019-12-27 10:01:04 +08:00
dash_scheme($key = "custom", $name = "Custom",
$col1 = akina_option('dash_scheme_color_a'), $col2 = akina_option('dash_scheme_color_b'), $col3 = akina_option('dash_scheme_color_c'), $col4 = akina_option('dash_scheme_color_d'),
$base = akina_option('dash_scheme_color_base'), $focus = akina_option('dash_scheme_color_focus'), $current = akina_option('dash_scheme_color_current'),
$rules = akina_option('dash_scheme_css_rules'));
2019-05-27 03:48:21 +08:00
2019-05-28 17:20:41 +08:00
//Set Default Admin Color Scheme for New Users
2019-12-27 10:01:04 +08:00
function set_default_admin_color($user_id)
{
2019-05-28 17:20:41 +08:00
$args = array(
'ID' => $user_id,
2019-12-27 10:01:04 +08:00
'admin_color' => 'sunrise',
2019-05-28 17:20:41 +08:00
);
2019-12-27 10:01:04 +08:00
wp_update_user($args);
2019-05-28 17:20:41 +08:00
}
2019-12-27 10:01:04 +08:00
//add_action('user_register', 'set_default_admin_color');
2019-05-28 17:20:41 +08:00
//Stop Users From Switching Admin Color Schemes
//if ( !current_user_can('manage_options') ) remove_action( 'admin_color_scheme_picker', 'admin_color_scheme_picker' );
2019-05-27 03:48:21 +08:00
// WordPress Custom Font @ Admin
2019-12-27 10:01:04 +08:00
function custom_admin_open_sans_font()
{
2019-05-27 03:48:21 +08:00
echo '<link href="https://fonts.googleapis.com/css?family=Noto+Serif+SC&display=swap" rel="stylesheet">' . PHP_EOL;
echo '<style>body, #wpadminbar *:not([class="ab-icon"]), .wp-core-ui, .media-menu, .media-frame *, .media-modal *{font-family:"Noto Serif SC","Source Han Serif SC","Source Han Serif","source-han-serif-sc","PT Serif","SongTi SC","MicroSoft Yahei",Georgia,serif !important;}</style>' . PHP_EOL;
}
2019-12-27 10:01:04 +08:00
add_action('admin_head', 'custom_admin_open_sans_font');
2019-05-27 03:48:21 +08:00
// WordPress Custom Font @ Admin Frontend Toolbar
2019-12-27 10:01:04 +08:00
function custom_admin_open_sans_font_frontend_toolbar()
{
if (current_user_can('administrator')) {
2019-05-27 03:48:21 +08:00
echo '<link href="https://fonts.googleapis.com/css?family=Noto+Serif+SC&display=swap" rel="stylesheet">' . PHP_EOL;
echo '<style>#wpadminbar *:not([class="ab-icon"]){font-family:"Noto Serif SC","Source Han Serif SC","Source Han Serif","source-han-serif-sc","PT Serif","SongTi SC","MicroSoft Yahei",Georgia,serif !important;}</style>' . PHP_EOL;
}
}
2019-12-27 10:01:04 +08:00
add_action('wp_head', 'custom_admin_open_sans_font_frontend_toolbar');
2019-05-27 03:48:21 +08:00
// WordPress Custom Font @ Admin Login
2019-12-27 10:01:04 +08:00
function custom_admin_open_sans_font_login_page()
{
if (stripos($_SERVER["SCRIPT_NAME"], strrchr(wp_login_url(), '/')) !== false) {
2019-05-27 03:48:21 +08:00
echo '<link href="https://fonts.googleapis.com/css?family=Noto+Serif+SC&display=swap" rel="stylesheet">' . PHP_EOL;
echo '<style>body{font-family:"Noto Serif SC","Source Han Serif SC","Source Han Serif","source-han-serif-sc","PT Serif","SongTi SC","MicroSoft Yahei",Georgia,serif !important;}</style>' . PHP_EOL;
2019-12-27 10:01:04 +08:00
}
2019-05-27 03:48:21 +08:00
}
2019-12-27 10:01:04 +08:00
add_action('login_head', 'custom_admin_open_sans_font_login_page');
2018-05-26 18:59:29 +08:00
// 阻止垃圾注册
2019-12-27 10:01:04 +08:00
add_action('register_post', 'codecheese_register_post', 10, 3);
function codecheese_register_post($sanitized_user_login, $user_email, $errors)
{
// Blocked domains
$domains = array('net.buzzcluby.com',
'buzzcluby.com',
'mail.ru',
'h.captchaeu.info',
'edge.codyting.com');
// Get visitor email domain
$email = explode('@', $user_email);
// Check and display error message for the registration form if exists
if (in_array($email[1], $domains)) {
$errors->add('invalid_email', __('<b>ERROR</b>: This email domain (<b>@' . $email[1] . '</b>) has been blocked. Please use another email.'));
}
2018-05-26 18:59:29 +08:00
}
2019-01-24 17:01:27 +08:00
// html 标签处理器
2019-12-27 10:01:04 +08:00
function html_tag_parser($content)
{
if (!is_feed()) {
if (akina_option('lazyload') && akina_option('lazyload_spinner')) {
$content = preg_replace(
2019-10-28 21:04:01 +08:00
'/<img(.+)src=[\'"]([^\'"]+)[\'"](.*)>/i',
2019-12-27 10:01:04 +08:00
"<img $1 class=\"lazyload\" data-src=\"$2\" src=\"" . akina_option('lazyload_spinner') . "\" onerror=\"imgError(this)\" $3 >\n<noscript>$0</noscript>",
2019-10-28 21:04:01 +08:00
$content
2019-12-27 10:01:04 +08:00
);
}
2019-01-24 17:01:27 +08:00
//Fancybox
2019-12-27 10:01:04 +08:00
/* Markdown Regex Pattern for Matching URLs:
2019-01-24 17:01:27 +08:00
* https://daringfireball.net/2010/07/improved_regex_for_matching_urls
*/
2019-12-27 10:01:04 +08:00
$url_regex = '((?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))';
2019-01-24 17:01:27 +08:00
//With Thumbnail: !{alt}(url)[th_url]
2019-12-27 10:01:04 +08:00
if (preg_match_all('/\!\{.*?\)\[.*?\]/i', $content, $matches)) {
2019-12-27 21:00:34 +08:00
for ($i = 0; $i < sizeof($matches); $i++) {
2019-12-27 10:01:04 +08:00
$content = str_replace($matches[$i], preg_replace(
'/!\{([^\{\}]+)*\}\(' . $url_regex . '\)\[' . $url_regex . '\]/i',
'<a data-fancybox="gallery"
2019-10-28 20:52:29 +08:00
data-caption="$1"
2019-12-27 10:01:04 +08:00
class="fancybox"
href="$2"
alt="$1"
2019-10-28 20:52:29 +08:00
title="$1"><img src="$7" target="_blank" rel="nofollow" class="fancybox"></a>',
2019-11-22 17:47:36 +08:00
$matches[$i]),
2019-12-27 10:01:04 +08:00
$content);
2019-10-28 20:52:29 +08:00
}
}
2019-12-27 10:01:04 +08:00
2019-01-24 17:01:27 +08:00
//Without Thumbnail :!{alt}(url)
2019-12-27 10:01:04 +08:00
$content = preg_replace(
'/!\{([^\{\}]+)*\}\(' . $url_regex . '\)/i',
'<a data-fancybox="gallery"
2019-01-24 17:01:27 +08:00
data-caption="$1"
class="fancybox"
href="$2"
2019-12-27 10:01:04 +08:00
alt="$1"
2019-01-24 17:01:27 +08:00
title="$1"><img src="$2" target="_blank" rel="nofollow" class="fancybox"></a>',
$content
);
2019-12-27 10:01:04 +08:00
2019-10-24 22:26:11 +08:00
//Github cards
2019-12-27 10:01:04 +08:00
$content = preg_replace(
2019-10-24 22:26:11 +08:00
'/\[github repo=[\'"]([^\'"]+)[\'"]\]/i',
'
2019-12-27 10:01:04 +08:00
<iframe frameborder="0" scrolling="0" allowtransparency="true"
src="https://api.2heng.xin/github-card/?repo=$1"
2019-10-24 22:26:11 +08:00
width="400" height="153"
style="margin-left: 50%; transform: translateX(-50%);"></iframe>
',
$content
);
2019-12-27 10:01:04 +08:00
}
2019-11-27 08:31:36 +08:00
//html tag parser for rss
2019-12-27 10:01:04 +08:00
if (is_feed()) {
2019-11-27 08:31:36 +08:00
//Fancybox
2019-12-27 10:01:04 +08:00
$url_regex = '((?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))';
if (preg_match_all('/\!\{.*?\)\[.*?\]/i', $content, $matches)) {
2019-12-27 21:00:34 +08:00
for ($i = 0; $i < sizeof($matches); $i++) {
2019-12-27 10:01:04 +08:00
$content = str_replace(
$matches[$i],
preg_replace('/!\{([^\{\}]+)*\}\(' . $url_regex . '\)\[' . $url_regex . '\]/i', '<a href="$2"><img src="$7" alt="$1" title="$1"></a>', $matches[$i]),
$content
);
2019-11-27 08:31:36 +08:00
}
}
2019-12-27 10:01:04 +08:00
$content = preg_replace('/!\{([^\{\}]+)*\}\(' . $url_regex . '\)/i', '<a href="$2"><img src="$2" alt="$1" title="$1"></a>', $content);
2019-11-27 08:31:36 +08:00
//Github cards
2019-12-27 10:01:04 +08:00
$content = preg_replace(
2019-11-27 08:31:36 +08:00
'/\[github repo=[\'"]([^\'"]+)[\'"]\]/i',
2019-11-27 08:48:12 +08:00
'<a href="https://github.com/$1">',
2019-11-27 08:31:36 +08:00
$content
);
2019-12-27 10:01:04 +08:00
}
return $content;
2018-05-26 18:59:29 +08:00
}
2019-01-24 17:01:27 +08:00
add_filter('the_content', 'html_tag_parser'); //替换文章关键词
//add_filter( 'comment_text', 'html_tag_parser' );//替换评论关键词
2018-05-26 18:59:29 +08:00
/*
* QQ 评论
*/
2019-12-27 10:01:04 +08:00
// 数据库插入评论表单的qq字段
add_action('wp_insert_comment', 'sql_insert_qq_field', 10, 2);
function sql_insert_qq_field($comment_ID, $commmentdata)
{
$qq = isset($_POST['new_field_qq']) ? $_POST['new_field_qq'] : false;
update_comment_meta($comment_ID, 'new_field_qq', $qq); // new_field_qq 是表单name值也是存储在数据库里的字段名字
2018-05-26 18:59:29 +08:00
}
// 后台评论中显示qq字段
2019-12-27 10:01:04 +08:00
add_filter('manage_edit-comments_columns', 'add_comments_columns');
add_action('manage_comments_custom_column', 'output_comments_qq_columns', 10, 2);
function add_comments_columns($columns)
{
$columns['new_field_qq'] = __('QQ'); // 新增列名称
2018-05-26 18:59:29 +08:00
return $columns;
}
2019-12-27 10:01:04 +08:00
function output_comments_qq_columns($column_name, $comment_id)
{
switch ($column_name) {
case "new_field_qq":
// 这是输出值可以拿来在前端输出这里已经在钩子manage_comments_custom_column上输出了
echo get_comment_meta($comment_id, 'new_field_qq', true);
break;
}
2018-05-26 18:59:29 +08:00
}
/**
2019-12-27 10:01:04 +08:00
* 头像调用路径
2018-05-26 18:59:29 +08:00
*/
2019-12-27 10:01:04 +08:00
add_filter('get_avatar', 'change_avatar', 10, 3);
function change_avatar($avatar)
{
global $comment, $sakura_privkey;
2019-11-13 20:32:10 +08:00
if ($comment) {
2019-12-27 10:01:04 +08:00
if (get_comment_meta($comment->comment_ID, 'new_field_qq', true)) {
$qq_number = get_comment_meta($comment->comment_ID, 'new_field_qq', true);
if (akina_option('qq_avatar_link') == 'off') {
return '<img src="https://q2.qlogo.cn/headimg_dl?dst_uin=' . $qq_number . '&spec=100" data-src="' . stripslashes($m[1]) . '" class="lazyload avatar avatar-24 photo" alt="😀" width="24" height="24" onerror="imgError(this,1)">';
} elseif (akina_option('qq_avatar_link') == 'type_3') {
$qqavatar = file_get_contents('http://ptlogin2.qq.com/getface?appid=1006102&imgtype=3&uin=' . $qq_number);
preg_match('/:\"([^\"]*)\"/i', $qqavatar, $matches);
return '<img src="' . $matches[1] . '" data-src="' . stripslashes($m[1]) . '" class="lazyload avatar avatar-24 photo" alt="😀" width="24" height="24" onerror="imgError(this,1)">';
} else {
2019-12-26 19:52:06 +08:00
$iv = str_repeat($sakura_privkey, 2);
2019-12-21 21:18:32 +08:00
$encrypted = openssl_encrypt($qq_number, 'aes-128-cbc', $sakura_privkey, 0, $iv);
2019-12-05 23:07:28 +08:00
$encrypted = urlencode(base64_encode($encrypted));
2019-12-27 10:01:04 +08:00
return '<img src="' . rest_url("sakura/v1/qqinfo/avatar") . '?qq=' . $encrypted . '"class="lazyload avatar avatar-24 photo" alt="😀" width="24" height="24" onerror="imgError(this,1)">';
2019-12-04 20:35:35 +08:00
}
2019-12-27 10:01:04 +08:00
} else {
return $avatar;
2019-11-13 20:32:10 +08:00
}
2019-12-27 10:01:04 +08:00
} else {
return $avatar;
2019-11-13 20:32:10 +08:00
}
2018-05-26 18:59:29 +08:00
}
2019-01-26 11:25:53 +08:00
// default feature image
2019-12-27 10:01:04 +08:00
function DEFAULT_FEATURE_IMAGE()
{
return rest_url('sakura/v1/image/feature') . '?' . rand(1, 1000);
2019-01-26 11:25:53 +08:00
}
2019-10-30 20:14:04 +08:00
//评论回复
2019-12-27 10:01:04 +08:00
function sakura_comment_notify($comment_id)
{
if (!$_POST['mail-notify']) {
update_comment_meta($comment_id, 'mail_notify', 'false');
}
2019-10-30 20:14:04 +08:00
}
2019-10-30 20:24:58 +08:00
add_action('comment_post', 'sakura_comment_notify');
2019-10-30 20:14:04 +08:00
2019-11-15 23:17:20 +08:00
//侧栏小工具
if (akina_option('sakura_widget')) {
if (function_exists('register_sidebar')) {
register_sidebar(array(
2019-12-27 10:01:04 +08:00
'name' => __('Sidebar'), //侧栏
2019-11-15 23:17:20 +08:00
'id' => 'sakura_widget',
'before_widget' => '<div class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<div class="title"><h2>',
2019-12-27 10:01:04 +08:00
'after_title' => '</h2></div>',
2019-11-15 23:17:20 +08:00
));
}
}
2019-11-17 17:00:40 +08:00
// 评论Markdown解析
2019-12-27 10:01:04 +08:00
function markdown_parser($incoming_comment)
{
global $wpdb, $comment_markdown_content;
2019-11-17 17:05:24 +08:00
$re = '/```([\s\S]*?)```[\s]*|`{1,2}[^`](.*?)`{1,2}|\[.*?\]\([\s\S]*?\)/m';
2019-12-27 10:01:04 +08:00
if (preg_replace($re, 'temp', $incoming_comment['comment_content']) != strip_tags(preg_replace($re, 'temp', $incoming_comment['comment_content']))) {
2019-11-17 21:36:51 +08:00
siren_ajax_comment_err('评论只支持Markdown啦见谅╮( ̄▽ ̄)╭<br>Markdown Supported while <i class="fa fa-code" aria-hidden="true"></i> Forbidden');
2019-12-27 10:01:04 +08:00
return ($incoming_comment);
2019-11-17 17:05:24 +08:00
}
2019-11-17 17:00:40 +08:00
$myCustomer = $wpdb->get_row("SELECT * FROM wp_comments");
//Add column if not present.
2019-11-17 21:36:51 +08:00
if (!isset($myCustomer->comment_markdown)) {
2019-11-17 17:00:40 +08:00
$wpdb->query("ALTER TABLE wp_comments ADD comment_markdown text");
}
$comment_markdown_content = $incoming_comment['comment_content'];
include 'inc/Parsedown.php';
$Parsedown = new Parsedown();
2019-12-15 22:33:54 +08:00
$incoming_comment['comment_content'] = $Parsedown->setUrlsLinked(false)->text($incoming_comment['comment_content']);
2019-11-17 17:00:40 +08:00
return $incoming_comment;
}
2019-12-27 10:01:04 +08:00
add_filter('preprocess_comment', 'markdown_parser');
remove_filter( 'comment_text', 'make_clickable', 9 );
2019-11-17 17:00:40 +08:00
//保存Markdown评论
2019-12-27 10:01:04 +08:00
function save_markdown_comment($comment_ID, $comment_approved)
{
global $wpdb, $comment_markdown_content;
2019-11-17 17:00:40 +08:00
$comment = get_comment($comment_ID);
$comment_content = $comment_markdown_content;
//store markdow content
2019-12-27 10:01:04 +08:00
$wpdb->query("UPDATE wp_comments SET comment_markdown='" . $comment_content . "' WHERE comment_ID='" . $comment_ID . "';");
2019-11-17 17:00:40 +08:00
}
add_action('comment_post', 'save_markdown_comment', 10, 2);
//打开评论HTML标签限制
2019-12-27 10:01:04 +08:00
function allow_more_tag_in_comment()
{
global $allowedtags;
$allowedtags['pre'] = array('class' => array());
$allowedtags['code'] = array('class' => array());
$allowedtags['h1'] = array('class' => array());
$allowedtags['h2'] = array('class' => array());
$allowedtags['h3'] = array('class' => array());
$allowedtags['h4'] = array('class' => array());
$allowedtags['h5'] = array('class' => array());
$allowedtags['ul'] = array('class' => array());
$allowedtags['ol'] = array('class' => array());
$allowedtags['li'] = array('class' => array());
$allowedtags['td'] = array('class' => array());
$allowedtags['th'] = array('class' => array());
$allowedtags['tr'] = array('class' => array());
$allowedtags['table'] = array('class' => array());
$allowedtags['thead'] = array('class' => array());
$allowedtags['tbody'] = array('class' => array());
$allowedtags['span'] = array('class' => array());
2019-11-17 17:00:40 +08:00
}
add_action('pre_comment_on_post', 'allow_more_tag_in_comment');
2019-11-28 23:21:24 +08:00
/*
* 随机图
*/
2019-12-27 10:01:04 +08:00
function create_sakura_table()
{
global $wpdb, $sakura_image_array, $sakura_privkey;
$sakura_table_name = $wpdb->base_prefix . 'sakura';
require_once ABSPATH . "wp-admin/includes/upgrade.php";
2019-11-28 23:21:24 +08:00
dbDelta("CREATE TABLE IF NOT EXISTS `" . $sakura_table_name . "` (
2019-12-03 22:35:15 +08:00
`mate_key` varchar(50) COLLATE utf8_bin NOT NULL,
`mate_value` text COLLATE utf8_bin NOT NULL,
PRIMARY KEY (`mate_key`)
2019-11-28 23:21:24 +08:00
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=1 ;");
//default data
2019-12-27 10:01:04 +08:00
if (!$wpdb->get_var("SELECT COUNT(*) FROM $sakura_table_name WHERE mate_key = 'manifest_json'")) {
2019-12-04 20:35:35 +08:00
$manifest = array(
"mate_key" => "manifest_json",
2020-01-09 16:50:07 +08:00
"mate_value" => file_get_contents(get_template_directory() . "/manifest/manifest.json"),
2019-12-04 20:35:35 +08:00
);
2019-12-27 10:01:04 +08:00
$wpdb->insert($sakura_table_name, $manifest);
2019-12-03 22:35:15 +08:00
}
2019-12-27 10:01:04 +08:00
if (!$wpdb->get_var("SELECT COUNT(*) FROM $sakura_table_name WHERE mate_key = 'json_time'")) {
2019-12-04 20:35:35 +08:00
$time = array(
"mate_key" => "json_time",
2019-12-27 10:01:04 +08:00
"mate_value" => date("Y-m-d H:i:s", time()),
2019-12-04 20:35:35 +08:00
);
2019-12-27 10:01:04 +08:00
$wpdb->insert($sakura_table_name, $time);
2019-12-03 22:35:15 +08:00
}
2019-12-27 10:01:04 +08:00
if (!$wpdb->get_var("SELECT COUNT(*) FROM $sakura_table_name WHERE mate_key = 'privkey'")) {
2019-12-04 20:35:35 +08:00
$privkey = array(
"mate_key" => "privkey",
2019-12-27 10:01:04 +08:00
"mate_value" => wp_generate_password(8),
2019-12-04 20:35:35 +08:00
);
2019-12-27 10:01:04 +08:00
$wpdb->insert($sakura_table_name, $privkey);
2019-12-04 20:35:35 +08:00
}
//reduce sql query
2020-01-04 19:33:32 +08:00
$sakura_image_array = $wpdb->get_var("SELECT `mate_value` FROM $sakura_table_name WHERE `mate_key`='manifest_json'");
$sakura_privkey = $wpdb->get_var("SELECT `mate_value` FROM $sakura_table_name WHERE `mate_key`='privkey'");
2019-11-28 23:21:24 +08:00
}
2019-12-27 10:01:04 +08:00
add_action('after_setup_theme', 'create_sakura_table');
2019-11-28 23:21:24 +08:00
2020-01-07 13:03:16 +08:00
//rest api支持
function permalink_tip()
{
if ( !get_option('permalink_structure') ){
$msg = __('<b> For a better experience, please do not set <a href="/wp-admin/options-permalink.php"> permalink </a> as plain. To do this, you may need to configure <a href="https://www.wpdaxue.com/wordpress-rewriterule.html" target="_blank"> pseudo-static </a>. </ b>','sakura'); /*<b>为了更好的使用体验,请不要将<a href="/wp-admin/options-permalink.php">固定链接</a>设置为朴素。为此,您可能需要配置<a href="https://www.wpdaxue.com/wordpress-rewriterule.html" target="_blank">伪静态</a>。</b>*/
echo '<div class="notice notice-success is-dismissible" id="scheme-tip"><p><b>' . $msg . '</b></p></div>';
}
}
add_action('admin_notices', 'permalink_tip');
2019-12-27 10:01:04 +08:00
//code end