File: /www/wwwroot/www.scdc-marine.com/wp-content/themes/heku-cms/functions.php
<?php
/**
* Theme Name: Heku CMS
* Text Domain: 网站模板
* Version: 1.01
* Requires at least: 5.0
* Requires PHP: 7.1.4
* Description: 河枯网站制作,网站模板。
* Tags: 河枯, 自定义模板, 标准开发
* Author: Heku Daleconan
* Author URI: https://www.daleconan.com
* @link https://www.daleconan.com/heku-cms/
*
*/
// /* ------------------------ 定义常量 ------------------------------ */
define( 'HK_DS' , DIRECTORY_SEPARATOR ); // PHP 定义中的 分隔符
define( 'HEKU_HOME_URI' , home_url() );
define( 'HEKU_THEME_DIR' , get_template_directory() );
define( 'HEKU_INCLUDES_PATH' , get_template_directory() . '/includes/' );
define( 'HEKU_THEME_URI' , get_template_directory_uri() );
define( 'HEKU_VERSION' , '1.1.3' );
//add_menu_page( '河枯主题,后台插件界面设置', '界 面 设 置2', 'administrator', 'heku-menu-pages222', "" );
require HEKU_THEME_DIR. HK_DS . 'loader.php';
// /* ------------------------ 定义常量 ------------------------------ */
// /* ------------------------ heku_get_option 自定义参数的 快捷调用方式 ------------------------------ */
function heku_get_option( $where , $key , $circle_id = 0 ){
if(defined('HEKU_OPT_'.strtoupper($where))){
$settings = constant('HEKU_OPT_'.strtoupper($where));
}else{
$settings = get_option('heku_'.$where);
}
if(isset($settings[$key])){
return $settings[$key];
}else{
$class = 'HEKU\Modules\Settings\\'.ucfirst(substr($where,0,strpos($where, '_')));
if(file_exists( HEKU_THEME_DIR.str_replace('\\', HK_DS, str_replace('B2','',$class)).'.php')){
$default = $class::get_default_settings($key);
if($default){
return $default;
}else{
$default = $class::$default_settings;
if(isset($default[$key])){
return $default[$key];
}else{
return '';
}
return '';
}
}
}
return '';
}
//菜单设置项
function heku_get_menu_option( $menu_item_id , $key = '' , $default = null){
$opts = get_option('cmb2_nav_menus', null);
if ($opts) {
$key .= '-' . $menu_item_id;
return (isset($opts[$key])) ? $opts[$key] : $default;
}
return $default;
}
// /* ------------------------ heku_get_option 自定义参数的 快捷调用方式 ------------------------------ */
// /* ------------------------ 清理wordpress头部文件,提高网页加载速度。 ------------------------------ */
function remove_core_updates(){
global $wp_version;return(object) array('last_checked'=> time(),'version_checked'=> $wp_version,);
}
add_filter('pre_site_transient_update_core','remove_core_updates'); //WordPress核心
add_filter('pre_site_transient_update_plugins','remove_core_updates'); //WordPress插件
add_filter('pre_site_transient_update_themes','remove_core_updates'); //WordPress主题
remove_action('admin_init', '_maybe_update_core'); // 禁止 WordPress 检查更新
remove_action('admin_init', '_maybe_update_plugins'); // 禁止 WordPress 更新插件
remove_action('admin_init', '_maybe_update_themes'); // 禁止 WordPress 更新主题
show_admin_bar( false ); // 完全去除wordpress工具条
remove_action( 'pre_post_update', 'wp_save_post_revision' ); // 移除 post 分类的 修订版本
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head' ); // rel=pre
remove_action( 'wp_head', 'feed_links', 2 ); // 去除文章feed
remove_action( 'wp_head', 'feed_links_extra', 3 ); // 额外的feed,例如category, tag页
remove_action( 'wp_head', 'rsd_link' ); // 针对Blog的远程离线编辑器接口
remove_action( 'wp_head', 'wlwmanifest_link' );
remove_action( 'wp_head', 'index_rel_link' );
remove_action( 'wp_head', 'parent_post_rel_link', 10, 0 );
remove_action( 'wp_head', 'start_post_rel_link', 10, 0 );
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0 );
remove_action( 'wp_head', 'locale_stylesheet' );
remove_action( 'publish_future_post', 'check_and_publish_future_post', 10, 1 );
remove_action( 'wp_head', 'noindex', 1 );
remove_action( 'wp_head', 'wp_robots', 1 );
remove_action( 'wp_head', 'wp_resource_hints', 2 );
remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
remove_action( 'wp_head', 'wp_print_styles', 8 );
remove_action( 'wp_enqueue_scripts', 'wp_enqueue_global_styles' );
remove_action( 'wp_enqueue_scripts', 'wp_common_block_scripts_and_styles' );
remove_action( 'wp_print_styles', 'print_emoji_styles');
remove_action( 'wp_head', 'wp_print_head_scripts', 9 );
remove_action( 'wp_head', 'rest_output_link_wp_head', 10 );
remove_action( 'wp_head', 'wp_generator' );
remove_action( 'wp_head', 'wp_oembed_add_discovery_links' );//
//remove_action( 'wp_head', 'rel_canonical' );
remove_action( 'wp_footer', 'wp_print_footer_scripts' );
remove_action( 'wp_head', 'wp_shortlink_wp_head', 10, 0 );
//remove_action( 'template_redirect', 'wp_shortlink_header', 11, 0 );
remove_action( 'wp_head', 'index_rel_link' ); // Removes the index link
remove_action( 'wp_head', 'parent_post_rel_link', 10, 0 ); // Removes the prev link
remove_action( 'wp_head', 'start_post_rel_link', 10, 0 ); // Removes the start link
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0 ); // Removes the relational links for the posts adjacent to the current post.
// /* ------------------------ 清理wordpress头部文件,提高网页加载速度。 ------------------------------ */
// /* ------------------------ /跳转控制 //避免首页 page/1 报错 ------------------------------ */
add_action( 'template_redirect', 'fanly_basic_redirects' );
function fanly_basic_redirects() {
if( is_home() && get_query_var('paged')===1 ) {
wp_redirect( home_url(), 301 );
die;
}
}
// /* ------------------------ /跳转控制 //避免首页 page/1 报错 ------------------------------ */
add_action('init', 'custom_rewrite_rules', 999);
function custom_rewrite_rules() {
global $wp_rewrite;
//修改page的固定链接结构
$wp_rewrite->page_structure = '/%pagename%.html';
//修改tag的固定链接结构
//$wp_rewrite->extra_permastructs['post_tag']['with_front'] = '';
$wp_rewrite->extra_permastructs['news']['struct'] = '/news/%news%.html';
// if (!strpos($wp_rewrite->get_page_permastruct(), '.html')) {
// $wp_rewrite->page_structure = $wp_rewrite->page_structure . '.html';
// }
}
// /* ------------------------ // 禁用自动生成的图片尺寸 ------------------------------ */
function shapeSpace_disable_image_sizes($sizes) {
unset($sizes['thumbnail']); // disable thumbnail size
unset($sizes['medium']); // disable medium size
unset($sizes['large']); // disable large size
unset($sizes['medium_large']); // disable medium-large size
unset($sizes['1536x1536']); // disable 2x medium-large size
unset($sizes['2048x2048']); // disable 2x large size return $sizes;
}
add_action('intermediate_image_sizes_advanced', 'shapeSpace_disable_image_sizes');
// 禁用缩放尺寸
add_filter('big_image_size_threshold', '__return_false');
// 禁用其他图片尺寸
function shapeSpace_disable_other_image_sizes() {
remove_image_size('post-thumbnail');
remove_image_size('another-size');
}
add_action('init', 'shapeSpace_disable_other_image_sizes');
function bzg_filter_image_sizes( $sizes ) {
unset( $sizes['medium_large']);
unset( $sizes['large']);
return $sizes;
}
add_filter( 'intermediate_image_sizes_advanced', 'bzg_filter_image_sizes' );
// /* ------------------------ // 禁用自动生成的图片尺寸 ------------------------------ */
// /* ------------------------ // 添加 删除仪表盘模块小工具 ------------------------------ */
function example_remove_dashboard_widgets() {
global $wp_meta_boxes;
// 以下这一行代码将删除 "快速发布" 模块
unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_quick_press']);
// 以下这一行代码将删除 "引入链接" 模块
unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_incoming_links']);
// 以下这一行代码将删除 "插件" 模块
unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_plugins']);
// 以下这一行代码将删除 "近期评论" 模块
unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_recent_comments']);
// 以下这一行代码将删除 "近期草稿" 模块
unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_recent_drafts']);
// 以下这一行代码将删除 "WordPress 开发日志" 模块
unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_primary']);
// 以下这一行代码将删除 "其它 WordPress 新闻" 模块
unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_secondary']);
// 以下这一行代码将删除 "站点健康状态" 模块
unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_site_health']);
// 以下这一行代码将删除 "概况" 模块
//unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_right_now']);
// 以下这一行代码将删除 "搜索引擎新闻" 模块
unset($wp_meta_boxes['dashboard']['normal']['core']['semperplugins-rss-feed']);
}
add_action('wp_dashboard_setup', 'example_remove_dashboard_widgets' );
// 以下这一行代码将删除 "welcome" 模块
remove_action('welcome_panel', 'wp_welcome_panel');
//添加 仪表盘自定义小工具
if ( ! function_exists( 'add_dashboard_widgets' ) ) :
function welcome_dashboard_widget_function() {
echo "<ul><li><a href='post-new.php'>发布内容</a></li><li><a href='edit.php'>修改内容</a></li></ul>";
}
function add_dashboard_widgets() {
wp_add_dashboard_widget('dashboard_php_nag', '常规任务', 'welcome_dashboard_widget_function');
}
add_action('wp_dashboard_setup', 'add_dashboard_widgets' );
endif;
// /* ------------------------ // 添加 仪表盘自定义 小工具 ------------------------------ */
// /* ------------------------ 定义、注册列表 ------------------------------ */
register_nav_menu( 'header-menu', '顶部导航菜单列表' );
register_nav_menu( 'sidebar-product', '侧边产品分类列表' );
register_nav_menu( 'sidebar-appproduct', '侧边产品应用列表' );
// /* ------------------------ 定义、注册列表 ------------------------------ */
// /* ------------------------WordPress 中文名、数字名图片上传自动重命名 ------------------------------ */
function fanly_custom_upload_name($file){
$info = pathinfo($file);
$ext = empty($info['extension']) ? '' : '.' . $info['extension'];
$name = basename($file, $ext);
if(preg_match("/[一-龥]/u",$file)){//中文换名
$file = substr(md5($name), 0, 20) . rand(00,99) . $ext;//截取前 20 位 MD5 长度,加上两位随机
}elseif(is_numeric($name)){//数字换名
$file = substr(md5($name), 0, 20) . rand(00,99) . $ext;//截取前 20 位 MD5 长度,加上两位随机
}
return $file;
}
add_filter('sanitize_file_name','fanly_custom_upload_name', 5, 1 );
// /* ------------------------WordPress 中文名、数字名图片上传自动重命名------------------------------ */
// /* ------------------------ 分割 导航路径 路径生成函数 ------------------------------ */
function heku_breadcrumbs() {
$delimiter = '<span class="icon iconfont icon-right"></span>'; // 分隔符
$before = '<a href="#">'; // 在当前链接前插入
$after = '</a>'; // 在当前链接后插入
if ( !is_home() && !is_front_page() || is_paged() ) {
$breadcrumb = '';
global $post;
$homeLink = home_url();
$breadcrumb .= ' <a class="home-bar" href="' . $homeLink . '"><span class="icon iconfont icon-chunbo-shouye"></span>' . __( 'Home', 'heku' ) . '</a>' . $delimiter . ' ';
if ( is_category() ) { // 分类 存档
global $wp_query;
$cat_obj = $wp_query->get_queried_object();
$thisCat = $cat_obj->term_id;
$thisCat = get_category( $thisCat );
$parentCat = get_category( $thisCat->parent );
if ( $thisCat->parent != 0 ) {
$cat_code = get_category_parents( $parentCat, TRUE, ' ' . $delimiter . ' ' );
$cat_code = str_replace( '<a', '<a ', $cat_code );
$breadcrumb .= $cat_code;
}
$breadcrumb .= $before . '' . single_cat_title( '', false ) . '' . $after;
} elseif(is_tax()){
global $wp_query;
$cat_obj = $wp_query->get_queried_object();
$breadcrumb .= '<a href="' . $homeLink . '/' . $cat_obj->taxonomy .'/' . $cat_obj->slug . '/">' . $cat_obj->name . '</a> ';
} elseif ( is_single() && !is_attachment() ) { // 文章
if ( get_post_type() != 'post' ) { // 自定义文章类型
$post_type = get_post_type_object( get_post_type() );
$slug = $post_type->rewrite;
$breadcrumb .= '<a href="' . $homeLink . '/' . $slug[ 'slug' ] . '/">' . $post_type->labels->singular_name . '</a> ' ;
/* echo $before . get_the_title() . $after; */
} else { // 文章 post
$cat = get_the_category();
$cat = $cat[ 0 ];
$cat_code = get_category_parents( $cat, TRUE, ' ' . $delimiter . ' ' );
$breadcrumb .= $cat_code = str_replace( '<a', '<a ', str_replace( $delimiter . ' ' . 'end', ' ', $cat_code .'end' ) );
/* echo $before . get_the_title() . $after; */
}
} elseif ( !is_single() && !is_page() && get_post_type() != 'post' && !is_tag() && !is_search() ) {
$post_type = get_post_type_object( get_post_type() );
$breadcrumb .= $before . $post_type->labels->singular_name . $after;
} elseif ( is_attachment() ) { // 附件
$parent = get_post( $post->post_parent );
$cat = get_the_category( $parent->ID );
$cat = $cat[ 0 ];
$breadcrumb .= '<a href="' . get_permalink( $parent ) . '">' . $parent->post_title . '</a> ' . $delimiter . ' ';
$breadcrumb .= $before . get_the_title() . $after;
} elseif ( is_page() && !$post->post_parent ) { // 页面
$breadcrumb .= $before . get_the_title() . $after;
} elseif ( is_page() && $post->post_parent ) { // 父级页面
$parent_id = $post->post_parent;
$breadcrumbs = array();
while ( $parent_id ) {
$page = get_page( $parent_id );
$breadcrumbs[] = '<a href="' . get_permalink( $page->ID ) . '">' . get_the_title( $page->ID ) . '</a>';
$parent_id = $page->post_parent;
}
$breadcrumbs = array_reverse( $breadcrumbs );
foreach ( $breadcrumbs as $crumb )echo $crumb . ' ' . $delimiter . ' ';
$breadcrumb .= $before . get_the_title() . $after;
} elseif ( is_search() ) { // 搜索结果
$breadcrumb .= $before;
$breadcrumb .= 'Tag : ' . '<font color=#ff0505>'.get_search_query().'</font>';
//printf( __( '搜索词为: %s 的搜索结果:', 'cmp' ), get_search_query() );
$breadcrumb .= $after;
} elseif ( is_tag() ) { //标签 存档
$breadcrumb .= $before ;
$breadcrumb .= single_tag_title('TAG : ', false);
//$breadcrumb .= single_tag_title('TAG : ');
$breadcrumb .= $after;
} elseif ( is_author() ) { // 作者存档
global $author;
$userdata = get_userdata( $author );
$breadcrumb .= $before;
//printf( __( 'Author Archives: %s', 'cmp' ), $userdata->display_name );
$breadcrumb .= $after;
} elseif ( is_404() ) { // 404 页面
$breadcrumb .= $before;
_e( '404页面:', 'cmp' );
$breadcrumb .= $after;
}
/* if ( get_query_var( 'paged' ) ) { // 分页
if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() )
$breadcrumb .= sprintf( __( ' Page %s ', 'cmp' ), get_query_var( 'paged' ) );
} */
return $breadcrumb ;
}
}
// /* ------------------------ 分割 导航路径 路径生成函数 ------------------------------ */
// /* ------------------------ 自己仿照分页函数设涉及的 非主分页的正确分页函数 ------------------------------ */
function heku_mypage( $range = 5 ) {
global $paged, $wp_query;
$max_page = $wp_query->max_num_pages;
if ( $max_page > 1 ) {
echo '';
if ( !$paged ) {
$paged = 1;
}
echo '<div class="col-sm-3 col-md-4 col-lg-3 col-xl-3 page_arr_left">';
if ( $paged != 1 ) {
echo "<a href='" . get_pagenum_link( 1 ) . "' class='extend icon iconfont icon-start_left' title='Home Page'></a>";
}else{
echo "<a href='" . get_pagenum_link( 1 ) . "' class='pointernone extend icon iconfont icon-start_left' title='Home Page'></a>";
echo "<a href='" . get_pagenum_link( 1 ) . "' class='pointernone' ><i class='pointernone icon iconfont icon-left'></i><span> Previous </span> <b class='transition'></b></a>";
}
previous_posts_link( '<i class="icon iconfont icon-left"></i><span> Previous </span> <b class="transition"></b>' );
echo '</div>';
echo '<div class="col-sm-6 col-md-4 col-lg-6 col-xl-6 page_arr_center">';
if ( $max_page > $range ) {
if ( $paged <= ceil( ( $range / 2 ) ) ) {
for ( $i = 1; $i <= ( $range ); $i++ ) {
echo "<a href='" . get_pagenum_link( $i ) . "'";
if ( $i == $paged )echo " class='current'";
echo ">$i</a>";
}
echo "<a href='" . get_pagenum_link( $max_page ) . "' class='extend ' title='Last Page'>...</a>";
} elseif ( $paged > ceil( ( $range / 2 ) ) && $paged <= ( $max_page - ceil( ( $range / 2 ) ) ) ) {
for ( $i = ( $paged - ceil( $range / 2 )); $i < ( $paged + ceil( ( $range / 2 ) ) ); $i++ ) {
echo "<a href='" . get_pagenum_link( $i ) . "'";
if ( $i == $paged )echo " class='current'";
echo ">$i</a>";
}
echo "<a href='" . get_pagenum_link( $max_page ) . "' class='extend ' title='Last Page'>...</a>";
} elseif ( $paged > ( $max_page - ceil( ( $range / 2 ) ) ) ) {
for ( $i = $max_page - $range; $i <= $max_page; $i++ ) {
echo "<a href='" . get_pagenum_link( $i ) . "'";
if ( $i == $paged )echo " class='current'";
echo ">$i</a>";
}
}
} else {
for ( $i = 1; $i <= $max_page; $i++ ) {
echo "<a href='" . get_pagenum_link( $i ) . "'";
if ( $i == $paged )echo " class='current'";
echo ">$i</a>";
}
}
echo '</div>';
echo '<div class="col-sm-3 col-md-4 col-lg-3 col-xl-3 page_arr_right">';
next_posts_link( '<b class="transition"></b> <span> Next </span> <i class="icon iconfont icon-right"></i>' );
if ( $paged != $max_page ) {
echo "<a href='" . get_pagenum_link( $max_page ) . "' class='extend icon iconfont icon-end_right' title='Last Page'></a>";
}else{
echo "<a href='" . get_pagenum_link( $max_page ) . "' class='pointernone' > <b class='transition'></b><span> Next </span><i class='pointernone icon iconfont icon-right'></i></a>";
echo "<a href='" . get_pagenum_link( $max_page ) . "' class='pointernone extend icon iconfont icon-end_right' title='Last Page'></a>";
}
echo '</div>';
}
}
// /* ------------------------ 自己仿照分页函数设涉及的 非主分页的正确分页函数 ------------------------------ */
// /* ------------------------ 排序字段 自定义添加自定义排序 (现在 添加到每个分类列表 --考虑只添加部分)------------------------------ */
function heku_listorderColumn( $cols ) {
$posts = get_posts( [
'fields' => 'ids',
'posts_per_page' => -1,
'post_type' => $this->get_post_type_setting() ?: 'any',
'tax_query' => [
[
'taxonomy' => $this->get_taxonomy(),
'field' => 'id',
'terms' => $id,
],
],
] );
write_to_console( $cols );
var_dump( $cols );
// $new = array();
// foreach ( $cols as $key => $title ) {
// if ( $key == 'title' ) {
// $new[ 'listorder' ] = __( '排序' );
// }
// $new[ $key ] = $title;
//}
return $cols;
}
function write_to_console($data) {
$console = $data;
if (is_array($console))
$console = implode(',', $console);
echo "<script>console.log('Console: " . $console . "' );</script>";
}
function heku_listorderValue( $column_name, $post_id ,$sss) {
write_to_console( "sa" );
var_dump( $column_name );
/* if ( 'listorder' == $column_name ) {
$listorder = get_post_meta( $post_id, '_listorder', true );
if ( !$listorder ) {
update_post_meta( $post_id, "_listorder", $post_id );
$listorder = $post_id;
}
echo '<input type="text" size="4" id="listorder_' . $post_id . '_val" name="listorder_post[' . $post_id . '][listorder]" value="' . $listorder . '"><a class="listorder_edit" href="javascript:void(0);" id="listorder_' . $post_id . '" data-pid="' . $post_id . '"><img src="/wp-admin/images/generic.png" border="0" alt="" title="修改"></a>';
} */
}
function my_post_sortable_columns( $columns ) {
$columns[ 'listorder' ] = '_listorder';
return $columns;
}
function registerdate_column_orderby( $vars ) {
if ( isset( $vars[ 'orderby' ] ) && '_listorder' == $vars[ 'orderby' ] ) {
$vars = array_merge( $vars, array(
'meta_key' => '_listorder',
'orderby' => 'meta_value_num'
) );
}
return $vars;
}
add_filter( 'manage_category_custom_column', 'heku_listorderColumn');
add_action( 'manage_category_custom_column', 'heku_listorderValue', 9, 3 );
// add_filter( 'manage_edit-post_sortable_columns', 'my_post_sortable_columns' );
// add_filter( 'request', 'registerdate_column_orderby' );
/* ------------------------ 排序字段 自定义添加自定义排序和jx修改 (现在 添加到每个分类列表 --考虑只添加部分) ------------------------------ */
function edit_listorder() {
global $current_user;
$post_id = $_POST[ 'post_id' ] ? intval( $_POST[ 'post_id' ] ) : 327;
$listorder = $_POST[ 'listorder' ] ? intval( $_POST[ 'listorder' ] ) : 0;
update_post_meta( $post_id, "_listorder", $listorder );
wp_die();
// exit();
}
// add_action( 'wp_ajax_edit_listorder', "edit_listorder" );
if ( !function_exists( 'listorder_admin_head' ) ) {
function listorder_admin_head() {
?>
<!--<script type='text/javascript' src='http://127.0.0.190/heku-admin/js/jquery.min.js'></script>-->
<script type="text/javascript">
jQuery('.listorder_edit').click( function () {
var post_id = jQuery( this ).attr( "data-pid" );
var listorder = jQuery( '#listorder_' + post_id + '_val' ).val();
jQuery.post( "/wp-admin/admin-ajax.php", {
action: 'edit_listorder',
post_id: post_id,
listorder: listorder
}, function ( response ) {
alert( "修改成功! " );
} );
} );
</script>
<?php
}
}
// add_action( 'admin_print_footer_scripts', 'listorder_admin_head', 11029 );
function category_jump_page() { //定义函数
if(is_category()) { //判断是否是分类列表页面
global $wp_query; //定义全局变量
if ($wp_query->post_count >= 1) { //判断文章数量是否是1
wp_redirect( get_permalink( $wp_query->posts['0']->ID ) ); //如果是1则使用wordpress内置函数wp_redirect跳转
}
}
}
add_action('template_redirect', 'category_jump_page');
//把定义的函数使用上
// /* ------- 排序字段 自定义添加自定义排序 (现在 添加到每个分类列表 --考虑只添加部分)------- */