File: /www/wwwroot/www.scdc-marine.com/wp-content/themes/scdc-marine/HekuClass/Common/ActionHook.php
<?php
namespace Heku\HekuClass\Common;
/**
* @package Heku\HekuClass\Common
* @subpackage Classes/Cmb2News
* @author Heku Daleconan <http://www.daleconan.com>
* @copyright Copyright (c) 2022-2023, Heku Daleconan
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
*/
class ActionHook {
/**
* 类实现单例模式,类的独生子女
*/
private static $instance;
/**
* 依赖注入的接口 Instance 方法
* 目标是 保证 init() 初始化方法 只执行一次
*
* @return self 唯一实例化的本类
*/
public static function instance() {
if ( ! isset( self::$instance ) ) {
self::$instance = new self;
self::$instance->init();
}
return self::$instance;
}
private function init() {
// /* ------------------------ 清理wordpress头部文件,提高网页加载速度。 ------------------------------ */
add_filter('pre_site_transient_update_core', array( $this, 'remove_core_updates' ) ); //WordPress核心
add_filter('pre_site_transient_update_plugins', array( $this, 'remove_core_updates') ); //WordPress插件
add_filter('pre_site_transient_update_themes', array( $this, '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头部文件,提高网页加载速度。 ------------------------------ */
add_action( 'template_redirect', array( $this,'fanly_basic_redirects'));
// 跳转控制 //避免首页 page/1 报错
add_action('init', array( $this,'custom_rewrite_rules'), 999);
// 修改page的固定链接结构, .html
// /* ------------------------ // 禁用自动生成的图片尺寸 ------------------------------ */
add_action('intermediate_image_sizes_advanced', array( $this,'shapeSpace_disable_image_sizes'));
// 禁用缩放尺寸
add_filter('big_image_size_threshold', '__return_false');
// 禁用其他图片尺寸
add_action('init', array( $this,'shapeSpace_disable_other_image_sizes'));
add_filter( 'intermediate_image_sizes_advanced', array( $this,'bzg_filter_image_sizes'));
// /* ------------------------ // 禁用自动生成的图片尺寸 ------------------------------ */
add_filter('sanitize_file_name',array( $this, 'fanly_custom_upload_name'), 5, 1 );
// WordPress 中文名、数字名图片上传自动
add_action('wp_dashboard_setup', array( $this,'example_remove_dashboard_widgets' ) );
// 以下这一行代码将删除 "welcome" 模块
remove_action('welcome_panel', array( $this,'wp_welcome_panel') );
// 添加 仪表盘自定义小工具
add_action('wp_dashboard_setup', array( $this,'add_dashboard_widgets' ) );
// 添加 仪表盘自定义 小工具
// 自定义 各文章类型的分页 数量
add_action( 'pre_get_posts', array( $this,'custom_archive_posts_per_page' ) );
// 添加过滤器,在导航菜单生成之前调用自定义函数
add_filter('wp_nav_menu_objects', array( $this,'add_current_menu_item_for_parent_category') );
}
public function remove_core_updates(){
global $wp_version;
return(object) array('last_checked'=> time(),'version_checked'=> $wp_version,);
}
// /* ------------------------ /跳转控制 //避免首页 page/1 报错 ------------------------------ */
public function fanly_basic_redirects() {
if( is_home() && get_query_var('paged')===1 ) {
wp_redirect( home_url(), 301 );
die;
}
}
// /* ------------------------ /跳转控制 //避免首页 page/1 报错 ------------------------------ */
// /* ------------------------ 修改page的固定链接结构, .html ------------------------------ */
public 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';
// }
}
// /* ------------------------ 修改page的固定链接结构, .html ------------------------------ */
// /* ------------------------ // 禁用自动生成的图片尺寸 ------------------------------ */
public 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;
}
// 禁用其他图片尺寸
public function shapeSpace_disable_other_image_sizes() {
remove_image_size('post-thumbnail');
remove_image_size('another-size');
}
//
public function bzg_filter_image_sizes( $sizes ) {
unset( $sizes['medium_large']);
unset( $sizes['large']);
return $sizes;
}
// /* ------------------------ // 禁用自动生成的图片尺寸 ------------------------------ */
// /* ------------------------WordPress 中文名、数字名图片上传自动重命名 ------------------------------ */
public 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;
}
// /* ------------------------WordPress 中文名、数字名图片上传自动重命名------------------------------ */
// /* ------------------------ // 添加 删除仪表盘模块小工具 ------------------------------ */
public 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']);
}
public function welcome_dashboard_widget_function() {
echo "<ul><li><a href='post-new.php'>发布内容</a></li><li><a href='edit.php'>修改内容</a></li></ul>";
}
public function add_dashboard_widgets() {
wp_add_dashboard_widget('dashboard_php_nag', '常规任务', array( $this,'welcome_dashboard_widget_function') );
}
// /* ------------------------ // 添加 仪表盘自定义 小工具 ------------------------------ */
// /* ------------------------ // 自定义 各文章类型的分页 数量 ------------------------------ */
function custom_archive_posts_per_page( $query ) {
if ( $query->is_main_query() ) { // 检查是否是归档页面
//var_dump(get_post_type());
//var_dump(is_post_type_archive("post"));
if(is_post_type_archive("news")){
$query->set( 'posts_per_page', 9 ); // 设置每页显示10篇文章
}elseif(is_category()){
$query->set( 'posts_per_page', 9 ); // 设置每页显示10篇文章
}
// if ( is_category() ) { // 检查是否是分类归档
// $query->set( 'posts_per_page', 10 ); // 设置每页显示10篇文章
// } elseif ( is_tag() ) { // 检查是否是标签归档
// $query->set( 'posts_per_page', 15 ); // 设置每页显示15篇文章
// } elseif ( is_home() ) { // 检查是否是首页
// $query->set( 'posts_per_page', 5 ); // 设置每页显示5篇文章
// }
}
}
// /* ------------------------ // 自定义 各文章类型的分页 数量 ------------------------------ */
function add_current_menu_item_for_parent_category($items) {
// 获取当前页面的对象
global $post;
// 如果当前页面是分类页面
if (is_category()) {
// 获取当前分类的ID
$current_category_id = get_query_var('cat');
// 获取当前分类的父分类ID
$parent_category_id = wp_get_post_parent_id($current_category_id);
// 如果存在父分类,并且父分类的ID在导航菜单项中
if ($parent_category_id && array_key_exists($parent_category_id, $items)) {
// 给父分类添加current-menu-item类
$items[$parent_category_id]->current = true;
$items[$parent_category_id]->classes[] = 'current-menu-item';
}
}
return $items;
}
}