1
0
Fork 0
mirror of https://github.com/yunluo/gdk.git synced 2024-05-24 09:22:21 +02:00
This commit is contained in:
云落 2020-01-25 22:50:40 +08:00
parent 6b84ad7765
commit 03ed816d96
7 changed files with 399 additions and 362 deletions

14
framework/load.php Normal file
View file

@ -0,0 +1,14 @@
<?php
//require ( 'shortcode.php' );//短代码
include('plugin-options.php');//优化措施
//require ( 'avatar.php' );//头像功能
//require ( 'server.php' );//第三方服务
//require ( 'user.php' );//用户服务
//require ( 'seo.php' );//seo功能
//require ( 'email.php' );//邮箱功能

View file

@ -3,7 +3,7 @@
* 主题选项配置文件
*/
$theme_options = array(
$gdk_options = array(
'常规选项' => array(
array(
'name' => '滚动内容来源',
@ -838,7 +838,7 @@ $theme_options = array(
'desc' => '如果您想使用自定义的RSS地址请在这里输入您期望的地址。',
'id' => 'git_rss',
'type' => 'text',
'std' => get_bloginfo('rss2_url')
'std' => '',
),
array(
'name' => '新浪微博',
@ -1503,7 +1503,7 @@ $theme_options = array(
'desc' => '填写一个正方形的图片,显示在推送信息左侧,默认为默认头像',
'id' => 'git_notification_icon',
'type' => 'text',
'std' => deel_avatar_default()
'std' => '',
),
array(
'name' => 'HTML5推送链接【可选】',

View file

@ -8,43 +8,40 @@
}
$current_theme = wp_get_theme();
$default_options = array();
$theme_options = array();
require('options-config.php');
$current_options = get_option('git_options_setup', array());
$gdk_default_options = [];
$gdk_options = [];
include('options-config.php');
$gdk_current_options = get_option('gdk_options_setup', []);
function git_update_options() {
global $default_options, $theme_options, $current_options;
$default_options = array();
$current_options = get_option('git_options_setup', array());
foreach ($theme_options as $panel) {
function gdk_update_options() {
global $gdk_default_options, $gdk_options, $gdk_current_options;
foreach ($gdk_options as $panel) {
foreach ($panel as $option) {
$id = $option['id'];
$type = $option['type'];
if ( !$id ) continue;
$default_options[$id] = $option['std'];
if ( isset($current_options[$id]) ) continue;
$current_options[$id] = isset( $option['std'] ) ? $option['std'] : '';
$gdk_default_options[$id] = $option['std'];
if ( isset($gdk_current_options[$id]) ) continue;
$gdk_current_options[$id] = isset( $option['std'] ) ? $option['std'] : '';
}
}
}
git_update_options();
gdk_update_options();
//获取设置选项
function git_get_option($id, $returnDefault = false) {
global $default_options, $current_options;
return stripslashes( $returnDefault ? $default_options[$id] : $current_options[$id] );
function gdk_get_option($id, $returnDefault = false) {
global $gdk_default_options, $gdk_current_options;
return stripslashes( $returnDefault ? $gdk_default_options[$id] : $gdk_current_options[$id] );
}
//设置页面模板
function git_theme_options_page() {
global $theme_options, $current_theme;
function gdk_options_page() {
global $gdk_options;
?>
<div class="wrap">
<h2>Git 主题选项 <a href="https://support.qq.com/products/51158" class="feedback add-new-h2" target="_blank">问题反馈</a></h2>
<h2>GDK选项</h2>
<div class="wp-filter">
<p>您的网站分类列表:<?php echo Bing_category(); ?></p>
</div>
<?php
if ($_GET['update']) echo '<div class="updated"><p><strong>设置已保存。</strong></p></div>';
@ -56,7 +53,7 @@ function git_theme_options_page() {
<ul class="filter-links">
<?php
$activePanelIdx = empty($_GET['panel']) ? 0 : $_GET['panel'];
foreach ( array_keys($theme_options) as $i => $name ) {
foreach ( array_keys($gdk_options) as $i => $name ) {
echo '<li><a href="#panel_' . $i . '" data-panel="' . $i . '" ' . ( $i == $activePanelIdx ? 'class="current"' : '' ) . '>' . $name . '</a></li>';
}
?>
@ -68,7 +65,7 @@ foreach ( array_keys($theme_options) as $i => $name ) {
<form method="post">
<?php
$index = 0;
foreach ( $theme_options as $panel ) {
foreach ( $gdk_options as $panel ) {
echo '<div class="panel" id="panel_' . $index . '" ' . ( $index == $activePanelIdx ? ' style="display:block"' : '' ) . '><table class="form-table">';
foreach ( $panel as $option ) {
$type = $option['type'];
@ -93,7 +90,7 @@ switch ( $type ) {
case 'text':
?>
<label>
<input name="<?php echo $id; ?>" class="regular-text" id="<?php echo $id; ?>" type="text" value="<?php echo esc_attr(git_get_option( $id )) ?>" />
<input name="<?php echo $id; ?>" class="regular-text" id="<?php echo $id; ?>" type="text" value="<?php echo esc_attr(gdk_get_option( $id )) ?>" />
</label>
<p class="description"><?php echo $option['desc']; ?></p>
<?php
@ -102,7 +99,7 @@ switch ( $type ) {
?>
<label>
<span class="description"><?php echo $option['before']; ?></span>
<input name="<?php echo $id; ?>" class="small-text" id="<?php echo $id; ?>" type="number" value="<?php echo esc_attr(git_get_option( $id )) ?>" />
<input name="<?php echo $id; ?>" class="small-text" id="<?php echo $id; ?>" type="number" value="<?php echo esc_attr(gdk_get_option( $id )) ?>" />
<span class="description"><?php echo $option['desc']; ?></span>
</label>
<?php
@ -110,7 +107,7 @@ switch ( $type ) {
case 'textarea':
?>
<p><label for="<?php echo $id; ?>"><?php echo $option['desc']; ?></label></p>
<p><textarea name="<?php echo $id; ?>" id="<?php echo $id; ?>" rows="10" cols="50" class="large-text code"><?php echo esc_textarea(git_get_option( $id )) ?></textarea></p>
<p><textarea name="<?php echo $id; ?>" id="<?php echo $id; ?>" rows="10" cols="50" class="large-text code"><?php echo esc_textarea(gdk_get_option( $id )) ?></textarea></p>
<?php
break;
case 'radio':
@ -118,7 +115,7 @@ switch ( $type ) {
<fieldset>
<?php foreach ($option['options'] as $val => $name) : ?>
<label>
<input type="radio" name="<?php echo $id; ?>" id="<?php echo $id . '_' . $val; ?>" value="<?php echo $val; ?>" <?php checked( git_get_option( $id ), $val); ?>>
<input type="radio" name="<?php echo $id; ?>" id="<?php echo $id . '_' . $val; ?>" value="<?php echo $val; ?>" <?php checked( gdk_get_option( $id ), $val); ?>>
<?php echo $name; ?>
</label>
<?php endforeach; ?>
@ -129,7 +126,7 @@ switch ( $type ) {
case 'checkbox':
?>
<label>
<input type='checkbox' name="<?php echo $id; ?>" id="<?php echo $id; ?>" value="1" <?php echo checked(git_get_option($id)); ?> />
<input type='checkbox' name="<?php echo $id; ?>" id="<?php echo $id; ?>" value="1" <?php echo checked(gdk_get_option($id)); ?> />
<span><?php echo $option['desc']; ?></span>
</label>
<?php
@ -137,8 +134,8 @@ switch ( $type ) {
case 'checkboxs':
?>
<fieldset>
<?php $checkboxValues = git_get_option( $id );
if ( !is_array($checkboxValues) ) $checkboxValues = array();
<?php $checkboxValues = gdk_get_option( $id );
if ( !is_array($checkboxValues) ) $checkboxValues = [];
foreach ( $option['options'] as $id => $name ) : ?>
<label>
<input type="checkbox" name="<?php echo $id; ?>[]" id="<?php echo $id; ?>[]" value="<?php echo $id; ?>" <?php checked( in_array($id, $checkboxValues), true); ?>>
@ -152,7 +149,7 @@ switch ( $type ) {
default:
?>
<label>
<input name="<?php echo $id; ?>" class="regular-text" id="<?php echo $id; ?>" type="<?php echo $type; ?>" value="<?php echo esc_attr(git_get_option( $id )) ?>" />
<input name="<?php echo $id; ?>" class="regular-text" id="<?php echo $id; ?>" type="<?php echo $type; ?>" value="<?php echo esc_attr(gdk_get_option( $id )) ?>" />
</label>
<p class="description"><?php echo $option['desc']; ?></p>
<?php
@ -170,7 +167,7 @@ switch ( $type ) {
<tr>
<th><h4>云落小贴士</h4></th>
<td>
<p><?php echo get_Yunluo_Notice(); ?></p>
<p>哈哈</p>
</td>
</tr>
<tr>
@ -221,7 +218,7 @@ switch ( $type ) {
</form>
<form method="post">
<p class="submit">
<input name="reset" type="submit" class="button button-secondary" value="重置选项" onclick="return confirm('你确定要重置主题选项吗?');"/>
<input name="reset" type="submit" class="button button-secondary" value="重置选项" onclick="return confirm('你确定要重置选项吗?');"/>
<input type="hidden" name="action" value="reset" />
</p>
</form>
@ -247,6 +244,10 @@ switch ( $type ) {
margin-bottom: 0;
}
.filter-links .current {
border-bottom: 4px solid #6b48ff;
}
.wp-filter .drawer-toggle:before {
content: "\f463";
color: #fff!important;
@ -319,36 +320,31 @@ jQuery(function ($) {
<?php
}
//主题激活提示
function git_theme_activated_tip() {
if ( !get_option('git_options_setup') ) echo '<div class="error"><p><b>新主题已启用。该主题支持选项,请访问<a href="admin.php?page=git-theme-options">主题选项</a>页面进行配置。<a href="admin.php?page=git-theme-options">立即配置</a></b></p></div>';
}
add_action('admin_footer', 'git_theme_activated_tip');
function git_add_theme_options_page() {
global $theme_options;
if ( isset($_POST['action']) && isset($_GET['page']) && $_GET['page'] == 'git-theme-options' ) {
function gdk_add_options_page() {
global $gdk_options;
if ( isset($_POST['action']) && isset($_GET['page']) && $_GET['page'] == 'gdk-options' ) {
$action = $_POST['action'];
switch ( $action ) {
case 'update':
$_POST['uid'] = uniqid();
update_option('git_options_setup', $_POST);
git_update_options();
header('Location: admin.php?page=git-theme-options&update=true&panel=' . $_POST['panel']);
update_option('gdk_options_setup', $_POST);
gdk_update_options();
header('Location: admin.php?page=gdk-options&update=true&panel=' . $_POST['panel']);
break;
case 'reset':
delete_option('git_options_setup');
git_update_options();
header('Location: admin.php?page=git-theme-options&reset=true&panel=' . $_POST['panel']);
delete_option('gdk_options_setup');
gdk_update_options();
header('Location: admin.php?page=gdk-options&reset=true&panel=' . $_POST['panel']);
break;
case 'test':
wp_mail( get_bloginfo( 'admin_email' ) ,'[TEST]SMTP测试邮件','SMTP测试内容当您收到这封邮件的时候证明您的网站SMTP配置已成功');
header('Location: admin.php?page=git-theme-options&test=true&panel=' . $_POST['panel']);
header('Location: admin.php?page=gdk-options&test=true&panel=' . $_POST['panel']);
break;
}
exit;
}
add_menu_page( 'Git 主题选项', '主题选项', 'manage_options', 'git-theme-options', 'git_theme_options_page','dashicons-universal-access-alt' );
add_menu_page( 'GDK选项', 'GDK选项', 'manage_options', 'gdk-options', 'gdk_options_page','dashicons-buddicons-replies' );
}
add_action( 'admin_menu', 'git_add_theme_options_page' );
add_action( 'admin_menu', 'gdk_add_options_page' );

View file

@ -2,8 +2,7 @@
//require ( 'shortcode.php' );//短代码
require ( 'optimization.php' );//优化措施
include('optimization.php');//优化措施
//require ( 'avatar.php' );//头像功能
//require ( 'server.php' );//第三方服务

View file

@ -1,319 +1,11 @@
<?php
//去除头部冗余代码
remove_action('wp_head', 'feed_links_extra', 3);
remove_action('wp_head', 'feed_links', 2, 1);
remove_action('wp_head', 'rsd_link'); //移除离线编辑器开放接口
remove_action('wp_head', 'wlwmanifest_link'); //移除离线编辑器开放接口
remove_action('wp_head', 'index_rel_link'); //本页链接
remove_action('wp_head', 'parent_post_rel_link'); //清除前后文信息
remove_action('wp_head', 'start_post_rel_link'); //清除前后文信息
remove_action('wp_head', 'adjacent_posts_rel_link_wp_head');
remove_action('wp_head', 'rel_canonical'); //本页链接
remove_action('wp_head', 'wp_generator'); //移除WordPress版本号
remove_action('wp_head', 'wp_shortlink_wp_head', 10, 0); //本页短链接
add_filter('xmlrpc_enabled', '__return_false');
add_filter('embed_oembed_discover', '__return_false');
remove_action('wp_head', 'wp_oembed_add_discovery_links');
remove_action('wp_head', 'wp_oembed_add_host_js');
remove_filter('pre_oembed_result', 'wp_filter_pre_oembed_result', 10);
//阻止站内PingBack
function deel_noself_ping(&$links) {
$home = home_url();
foreach ($links as $l => $link) if (0 === strpos($link, $home)) unset($links[$l]);
}
add_action('pre_ping', 'deel_noself_ping');
//移除自动保存和修订版本
add_action('wp_print_scripts', 'disable_autosave');
function disable_autosave() {
wp_deregister_script('autosave');
}
add_filter('wp_revisions_to_keep', 'specs_wp_revisions_to_keep', 10, 2);
function specs_wp_revisions_to_keep($num, $post) {
return 0;
}
// 屏蔽 REST API
function git_disable_rest_api($access){
return new WP_Error('rest_cannot_acess', '无访问权限', array('status' => 403));
}
add_filter('rest_authentication_errors', 'git_disable_rest_api');
remove_action('wp_head', 'rest_output_link_wp_head', 10);
remove_action('template_redirect', 'rest_output_link_header', 11);
//禁止 s.w.org
function git_remove_dns_prefetch($hints, $relation_type) {
if ('dns-prefetch' === $relation_type) {
return array_diff(wp_dependencies_unique_hosts() , $hints);
}
return $hints;
}
add_filter('wp_resource_hints', 'git_remove_dns_prefetch', 10, 2);
//禁用WordPress活动
function git_dweandw_remove() {
remove_meta_box('dashboard_primary', get_current_screen() , 'side');
}
add_action('wp_network_dashboard_setup', 'git_dweandw_remove', 20);
add_action('wp_user_dashboard_setup', 'git_dweandw_remove', 20);
add_action('wp_dashboard_setup', 'git_dweandw_remove', 20);
//禁用谷歌字体
function git_remove_open_sans() {
wp_deregister_style('open-sans');
wp_register_style('open-sans', false);
wp_enqueue_style('open-sans', '');
}
add_action('init', 'git_remove_open_sans');
//免插件去除Category
add_action('load-themes.php', 'no_category_base_refresh_rules');
add_action('created_category', 'no_category_base_refresh_rules');
add_action('edited_category', 'no_category_base_refresh_rules');
add_action('delete_category', 'no_category_base_refresh_rules');
function no_category_base_refresh_rules() {
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
add_action('init', 'no_category_base_permastruct');
function no_category_base_permastruct() {
global $wp_rewrite;
$wp_rewrite->extra_permastructs['category']['struct'] = '%category%';
}
add_filter('category_rewrite_rules', 'no_category_base_rewrite_rules');
function no_category_base_rewrite_rules($category_rewrite) {
$category_rewrite = array();
$categories = get_categories(array(
'hide_empty' => false
));
foreach ($categories as $category) {
$category_nicename = $category->slug;
if ($category->parent == $category->cat_ID) // recursive recursion
$category->parent = 0;
elseif ($category->parent != 0) $category_nicename = get_category_parents($category->parent, false, '/', true) . $category_nicename;
$category_rewrite['(' . $category_nicename . ')/(?:feed/)?(feed|rdf|rss|rss2|atom)/?$'] = 'index.php?category_name=$matches[1]&feed=$matches[2]';
$category_rewrite['(' . $category_nicename . ')/page/?([0-9]{1,})/?$'] = 'index.php?category_name=$matches[1]&paged=$matches[2]';
$category_rewrite['(' . $category_nicename . ')/?$'] = 'index.php?category_name=$matches[1]';
}
global $wp_rewrite;
$old_category_base = get_option('category_base') ? get_option('category_base') : 'category';
$old_category_base = trim($old_category_base, '/');
$category_rewrite[$old_category_base . '/(.*)$'] = 'index.php?category_redirect=$matches[1]';
return $category_rewrite;
}
add_filter('query_vars', 'no_category_base_query_vars');
function no_category_base_query_vars($public_query_vars) {
$public_query_vars[] = 'category_redirect';
return $public_query_vars;
}
add_filter('request', 'no_category_base_request');
function no_category_base_request($query_vars) {
if (isset($query_vars['category_redirect'])) {
$catlink = trailingslashit(home_url()) . user_trailingslashit($query_vars['category_redirect'], 'category');
status_header(301);
header("Location: $catlink");
exit();
}
return $query_vars;
}
//禁用响应式图片
function msiw(){
return 1;
}
add_filter('max_srcset_image_width', 'msiw');
//移除默认的图片宽度以及高度
function remove_wps_width($html) {
$html = preg_replace('/(width|height)=\"\d*\"\s/', "", $html);
return $html;
}
add_filter('post_thumbnail_html', 'remove_wps_width', 10);
add_filter('image_send_to_editor', 'remove_wps_width', 10);
//取消后台登陆错误的抖动提示
function git_wps_login_error() {
remove_action('login_head', 'wp_shake_js', 12);
}
add_action('login_head', 'git_wps_login_error');
//取消静态资源的版本查询
function _remove_script_version($src){
$parts = explode('?ver', $src);
return $parts[0];
}
add_filter('script_loader_src', '_remove_script_version', 15, 1);
add_filter('style_loader_src', '_remove_script_version', 15, 1);
//禁用新版编辑器
add_filter('use_block_editor_for_post', '__return_false');
remove_action( 'wp_enqueue_scripts', 'wp_common_block_scripts_and_styles' );
//屏蔽顶部工具栏
add_filter('show_admin_bar', '__return_false');
//清除wp_footer带入的embed.min.js
function git_deregister_embed_script() {
wp_deregister_script('wp-embed');
}
add_action('wp_footer', 'git_deregister_embed_script');
//新标签打开顶部网站链接
function blank_my_site_bar( $wp_admin_bar ) {
$node = $wp_admin_bar->get_node('view-site');
$node->meta['target'] = '_blank';
$wp_admin_bar->add_node($node);
}
add_action( 'admin_bar_menu', 'blank_my_site_bar', 80 );
function lhu(){
return home_url();
}
add_filter('login_headerurl', 'lhu');
function lht(){
return get_bloginfo('name');
}
add_filter('login_headertext', 'lht');
//固化插入图片选项
function git_attachment_display_settings() {
update_option('image_default_align', 'center'); //居中显示
update_option('image_default_link_type', 'file'); //连接到媒体文件本身
update_option('image_default_size', 'full'); //完整尺寸
}
add_action('after_setup_theme', 'git_attachment_display_settings');
//禁用默认的附件页面
function git_disable_attachment_pages() {
global $post;
if (is_attachment()) {
if (!empty($post->post_parent)) {
wp_redirect(get_permalink($post->post_parent) , 301);
exit;
} else {
wp_redirect(home_url());
exit;
}
}
}
add_action('template_redirect', 'git_disable_attachment_pages', 1);
//使链接自动可点击
add_filter('the_content', 'make_clickable');
//临时修复文件删除漏洞
function git_rips_unlink_tempfix( $data ) {
if( isset($data['thumb']) ) {
$data['thumb'] = basename($data['thumb']);
}
return $data;
}
add_filter( 'wp_update_attachment_metadata', 'git_rips_unlink_tempfix' );
//自动中英文空格
function content_autospace($data){
$data = preg_replace('/([\\x{4e00}-\\x{9fa5}]+)([A-Za-z0-9_]+)/u', '${1} ${2}', $data);
$data = preg_replace('/([A-Za-z0-9_]+)([\\x{4e00}-\\x{9fa5}]+)/u', '${1} ${2}', $data);
return $data;
}
add_filter('the_content', 'content_autospace');
/*
function disable_emoji9s_tinymce($plugins) {
if (is_array($plugins)) {
return array_diff($plugins, array(
'wpemoji'
));
} else {
return array();
}
}
//取当前主题下img\smilies\下表情图片路径
function custom_gitsmilie_src($old, $img) {
return GIT_URL . '/assets/img/smilies/' . $img;
}
function init_gitsmilie() {
global $wpsmiliestrans;
//默认表情文本与表情图片的对应关系(可自定义修改)
$wpsmiliestrans = array(
':mrgreen:' => 'icon_mrgreen.gif',
':neutral:' => 'icon_neutral.gif',
':twisted:' => 'icon_twisted.gif',
':arrow:' => 'icon_arrow.gif',
':shock:' => 'icon_eek.gif',
':smile:' => 'icon_smile.gif',
':???:' => 'icon_confused.gif',
':cool:' => 'icon_cool.gif',
':evil:' => 'icon_evil.gif',
':grin:' => 'icon_biggrin.gif',
':idea:' => 'icon_idea.gif',
':oops:' => 'icon_redface.gif',
':razz:' => 'icon_razz.gif',
':roll:' => 'icon_rolleyes.gif',
':wink:' => 'icon_wink.gif',
':cry:' => 'icon_cry.gif',
':eek:' => 'icon_surprised.gif',
':lol:' => 'icon_lol.gif',
':mad:' => 'icon_mad.gif',
':sad:' => 'icon_sad.gif',
'8-)' => 'icon_cool.gif',
'8-O' => 'icon_eek.gif',
':-(' => 'icon_sad.gif',
':-)' => 'icon_smile.gif',
':-?' => 'icon_confused.gif',
':-D' => 'icon_biggrin.gif',
':-P' => 'icon_razz.gif',
':-o' => 'icon_surprised.gif',
':-x' => 'icon_mad.gif',
':-|' => 'icon_neutral.gif',
';-)' => 'icon_wink.gif',
'8O' => 'icon_eek.gif',
':(' => 'icon_sad.gif',
':)' => 'icon_smile.gif',
':?' => 'icon_confused.gif',
':D' => 'icon_biggrin.gif',
':P' => 'icon_razz.gif',
':o' => 'icon_surprised.gif',
':x' => 'icon_mad.gif',
':|' => 'icon_neutral.gif',
';)' => 'icon_wink.gif',
':!:' => 'icon_exclaim.gif',
':?:' => 'icon_question.gif',
);
//移除WordPress4.2版本更新所带来的Emoji钩子同时挂上主题自带的表情路径
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_emoji9s_tinymce');
add_filter('smilies_src', 'custom_gitsmilie_src', 10, 2);
}
add_action('init', 'init_gitsmilie', 5);
*/
//分类,标签描述添加图片
remove_filter('pre_term_description', 'wp_filter_kses');
remove_filter('pre_link_description', 'wp_filter_kses');
remove_filter('pre_link_notes', 'wp_filter_kses');
remove_filter('term_description', 'wp_kses_data');
// 友情链接扩展
add_filter('pre_option_link_manager_enabled', '__return_true');

335
functions/utils.php Normal file
View file

@ -0,0 +1,335 @@
<?php
/**
* 字符串截取,支持中文和其他编码
*
* @param string $str 需要转换的字符串
* @param string $start 开始位置
* @param string $length 截取长度
* @param string $charset 编码格式
* @param string $suffix 截断字符串后缀
* @return string
*/
function nc_substr_ext($str, $start = 0, $length = 0, $charset = 'utf-8', $suffix = '')
{
if (function_exists("mb_substr")) {
return mb_substr($str, $start, $length, $charset).$suffix;
} elseif (function_exists('iconv_substr')) {
return iconv_substr($str, $start, $length, $charset).$suffix;
}
$re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
$re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
$re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
$re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
preg_match_all($re[$charset], $str, $match);
$slice = join("", array_slice($match[0], $start, $length));
return $slice.$suffix;
}
function nc_reverse_strrchr($haystack, $needle, $trail)
{
$length = (strrpos($haystack, $needle) + $trail);
return strrpos($haystack, $needle) ? substr($haystack, 0, $length) : false;
}
/**
* 获取完整的句子
*/
function nc_print_excerpt($length, $post = null, $echo = true)
{
global $post;
$text = $post->post_excerpt;
if ('' == $text) {
$text = get_the_content();
$text = strip_shortcodes($text);
$text = apply_filters('the_content', $text);
$text = str_replace(']]>', ']]>', $text);
}
$text = strip_shortcodes($text);
$text = strip_tags($text);
$text = nc_substr_ext($text, 0, $length);
$excerpt = nc_reverse_strrchr($text, '。', 3);
if ($excerpt) {
$result = strip_tags(apply_filters('the_excerpt', $excerpt)).'...';
} else {
$result = strip_tags(apply_filters('the_excerpt', $text)).'...';
}
if ($echo == true) {
echo $result;
} else {
return $result;
}
}
function nc_comment_add_at($comment_text, $comment = '')
{
if (!empty($comment) && $comment->comment_parent > 0) {
$comment_text = '<a rel="nofollow" class="comment_at" href="#comment-' . $comment->comment_parent . '">@'.get_comment_author($comment->comment_parent) . '</a> ' . $comment_text;
}
return $comment_text;
}
function nc_record_visitors()
{
if (is_singular()) {
global $post;
$post_ID = $post->ID;
if ($post_ID) {
$post_views = (int)get_post_meta($post_ID, 'views', true);
if (!update_post_meta($post_ID, 'views', ($post_views+1))) {
add_post_meta($post_ID, 'views', 1, true);
}
}
}
}
function nc_post_views($before = '(点击 ', $after = ' 次)', $echo = 1)
{
global $post;
$post_ID = $post->ID;
$views = (int)get_post_meta($post_ID, 'views', true);
if ($echo) {
echo $before, number_format($views), $after;
} else {
return $views;
}
}
/**
* Load a component into a template while supplying data.
*
* @param string $slug The slug name for the generic template.
* @param array $params An associated array of data that will be extracted into the templates scope
* @param bool $output Whether to output component or return as string.
* @return string
*/
function nc_get_template_part_with_vars($slug, array $params = array(), $output = true)
{
if (!$output) {
ob_start();
}
$template_file = locate_template("{$slug}.php", false, false);
extract(array('template_params' => $params), EXTR_SKIP);
require($template_file);
if (!$output) {
return ob_get_clean();
}
}
function nc_ajax_comment_callback()
{
global $wpdb;
$comment_post_ID = isset($_POST['comment_post_ID']) ? (int) $_POST['comment_post_ID'] : 0;
$post = get_post($comment_post_ID);
$post_author = $post->post_author;
if (empty($post->comment_status)) {
do_action('comment_id_not_found', $comment_post_ID);
nc_ajax_comment_err('Invalid comment status.');
}
$status = get_post_status($post);
$status_obj = get_post_status_object($status);
if (!comments_open($comment_post_ID)) {
do_action('comment_closed', $comment_post_ID);
nc_ajax_comment_err(__('Sorry, comments are closed.', 'jimu'));
} elseif ('trash' == $status) {
do_action('comment_on_trash', $comment_post_ID);
nc_ajax_comment_err(__('Unknown error.', 'jimu'));
} elseif (!$status_obj->public && !$status_obj->private) {
do_action('comment_on_draft', $comment_post_ID);
nc_ajax_comment_err(__('Unknown error.', 'jimu'));
} elseif (post_password_required($comment_post_ID)) {
do_action('comment_on_password_protected', $comment_post_ID);
nc_ajax_comment_err(__('Password protected.', 'jimu'));
} else {
do_action('pre_comment_on_post', $comment_post_ID);
}
$comment_author = (isset($_POST['author'])) ? trim(strip_tags($_POST['author'])) : null;
$comment_author_email = (isset($_POST['email'])) ? trim($_POST['email']) : null;
$comment_author_url = (isset($_POST['url'])) ? trim($_POST['url']) : null;
$comment_content = (isset($_POST['comment'])) ? trim($_POST['comment']) : null;
$user = wp_get_current_user();
$user_ID = $user->ID;
if ($user->exists()) {
if (empty($user->display_name)) {
$user->display_name=$user->user_login;
}
$comment_author = esc_sql($user->display_name);
$comment_author_email = esc_sql($user->user_email);
$comment_author_url = esc_sql($user->user_url);
$user_ID = esc_sql($user->ID);
} else {
if (get_option('comment_registration') || 'private' == $status) {
nc_ajax_comment_err('<p>'.__('Sorry, you must be logged in to leave a comment', 'jimu').'</p>');
} // 抱歉,您必须登录后才能发表评论。
}
$comment_type = '';
if (get_option('require_name_email') && !$user->exists()) {
if (6 > strlen($comment_author_email) || '' == $comment_author) {
nc_ajax_comment_err('<p>'.__('Please fill in the required options (Name, Email).', 'jimu').'</p>');
} // 错误:请填写必须的选项(姓名,电子邮件)。
elseif (!is_email($comment_author_email)) {
nc_ajax_comment_err('<p>'.__('Please input a valid email address.', 'jimu').'</p>');
} // 错误:请输入有效的电子邮件地址。
}
if ('' == $comment_content) {
nc_ajax_comment_err('<p>'.__('Say something...', 'jimu').'</p>');
} // 说点什么吧
$dupe = "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = '$comment_post_ID' AND ( comment_author = '$comment_author' ";
if ($comment_author_email) {
$dupe .= "OR comment_author_email = '$comment_author_email' ";
}
$dupe .= ") AND comment_content = '$comment_content' LIMIT 1";
if ($wpdb->get_var($dupe)) {
nc_ajax_comment_err('<p>'.__('Please do not repeat your comments. :)', 'jimu').'</p>'); // Do not repeat comments aha~似乎说过这句话了
}
if ($lasttime = $wpdb->get_var($wpdb->prepare("SELECT comment_date_gmt FROM $wpdb->comments WHERE comment_author = %s ORDER BY comment_date DESC LIMIT 1", $comment_author))) {
$time_lastcomment = mysql2date('U', $lasttime, false);
$time_newcomment = mysql2date('U', current_time('mysql', 1), false);
$flood_die = apply_filters('comment_flood_filter', false, $time_lastcomment, $time_newcomment);
if ($flood_die) {
nc_ajax_comment_err('<p>'.__('You reply too fast. Take it easy.', 'jimu').'</p>'); // 你回复太快啦。慢慢来。
}
}
$comment_parent = isset($_POST['comment_parent']) ? absint($_POST['comment_parent']) : 0;
$commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_content', 'comment_type', 'comment_parent', 'user_ID');
$comment_id = wp_new_comment($commentdata);
$comment = get_comment($comment_id);
do_action('set_comment_cookies', $comment, $user, true);
$comment_depth = 1;
$tmp_c = $comment;
while ($tmp_c->comment_parent != 0) {
$comment_depth++;
$tmp_c = get_comment($tmp_c->comment_parent);
}
$GLOBALS['comment'] = $comment;
get_template_part('comment'); ?>
<?php
die();
}
function nc_ajax_comment_err($a)
{
header('HTTP/1.0 500 Internal Server Error');
header('Content-Type: text/plain;charset=UTF-8');
echo $a;
exit;
}
function nc_ajax_load_comments()
{
global $wp_query;
$type = sanitize_text_field($_POST['type']);
$paged = sanitize_text_field($_POST['paged']);
$q = sanitize_text_field($_POST['query']);
if ($paged < 1 || $paged > $_POST['commentcount']) {
wp_die();
}
if ($type === 'page') {
$wp_query = new WP_Query(array( 'page_id' => $q, 'cpage' => $paged ));
}
if ($type === 'post') {
$wp_query = new WP_Query(array( 'p' => $q, 'cpage' => $paged ));
}
if (have_posts()) {
while (have_posts()) {
the_post();
comments_template();
}
}
wp_reset_postdata();
wp_die();
}
/**
* 获取评论下一页页码
*/
function nc_get_next_page_number()
{
$page_number = get_comment_pages_count();
if (get_option('default_comments_page') == 'newest') {
$next_page = $page_number - 1;
} else {
$next_page = 2;
}
return $next_page;
}
function nc_like_init($key, $direct = false)
{
// $direct === true 时不计 cookie
$id = $_POST["id"];
$action = $_POST["do_action"];
$lh_raters = get_post_meta($id, $key, true);
$domain = ($_SERVER['HTTP_HOST'] != 'localhost') ? $_SERVER['HTTP_HOST'] : false;
if ($action == 'do') {
$expire = time() + 99999999;
if (!isset($_COOKIE[$key.'_'.$id]) || $direct) {
setcookie($key.'_'.$id, $id, $expire, '/', $domain, false);
if (!$lh_raters || !is_numeric($lh_raters)) {
update_post_meta($id, $key, 1);
} else {
update_post_meta($id, $key, ($lh_raters + 1));
}
}
}
if ($action == 'undo' && !$direct) {
$expire = time() - 1;
if (isset($_COOKIE[$key.'_'.$id])) {
setcookie($key.'_'.$id, $id, $expire, '/', $domain, false);
update_post_meta($id, $key, ($lh_raters - 1));
}
}
echo get_post_meta($id, $key, true);
die;
}
function nc_timeago($ptime = null, $post = null)
{
if ($post === null) {
global $post;
}
$ptime = $ptime ?: get_post_time('G', false, $post);
return human_time_diff($ptime, current_time('timestamp')) . '前';
}
function nc_get_translated_role_name($user_id)
{
$data = get_userdata($user_id);
$roles = $data->roles;
if (in_array('administrator', $roles)) {
return __('Administrator', 'jimu');
} elseif (in_array('editor', $roles)) {
return __('Certified Editor', 'jimu');
} elseif (in_array('author', $roles)) {
return __('Special Author', 'jimu');
} elseif (in_array('subscriber', $roles)) {
return __('Subscriber', 'jimu');
}
return __('Contributor', 'jimu');
}
function nc_get_meta($key, $single = true) {
global $post;
return get_post_meta($post->ID, $key, $single);
}
function nc_the_meta($key, $placeholder = '') {
echo nc_get_meta($key, true) ?: $placeholder;
}

View file

@ -39,6 +39,7 @@ if (version_compare(PHP_VERSION, '7.2.26', '>=')) {
include('modules/base/main.php');
include('functions/func_load.php');
include('framework/load.php');
function nc_store_pluggable_include() {
include( 'modules/pluggable/load.php' );