File: /www/wwwroot/www.scdc-marine.com/wp-content/themes/scdc-marine/HekuClass/Admin/Cmb2SEO.php
<?php
namespace Heku\HekuClass\Admin;
/**
* @package Heku\HekuClass\Admin
* @subpackage Classes/Cmb2SEO
* @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 Cmb2SEO {
/**
* 类实现单例模式,类的独生子女
*/
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() {
// 给 pages 页面添加 标签(关键词)
add_action( 'init', array($this,'taxonomies_for_pages'));
add_filter( 'register_taxonomy_args', array($this,'my_edit_heku_taxonomy_args'), 10, 3);
add_action( 'cmb2_admin_init', array($this,'cmb2_post_metabox'));
add_action( 'cmb2_admin_init', array($this,'menu_seo_options'), 101, 5);
}
public function taxonomies_for_pages() {
register_taxonomy_for_object_type('post_tag','page');
}
function my_edit_heku_taxonomy_args( $args, $taxonomy, $object_type ) {
$taxonomies = array( 'post_tag');
if ( in_array( $taxonomy, $taxonomies ) ) {
/* alter the rewrite with front arg */
$args['labels'] = array(
'name' => _x( '关键词(标签)', 'post type singular name' ),
);
$args[ 'meta_box_cb' ] = array($this,'post_tags_meta_box2');
}
return $args;
}
public function post_tags_meta_box2( $post, $box ) {
$defaults = array( 'taxonomy' => 'post_tag' );
if ( ! isset( $box['args'] ) || ! is_array( $box['args'] ) ) {
$args = array();
} else {
$args = $box['args'];
}
$parsed_args = wp_parse_args( $args, $defaults );
$tax_name = esc_attr( $parsed_args['taxonomy'] );
$taxonomy = get_taxonomy( $parsed_args['taxonomy'] );
$user_can_assign_terms = current_user_can( $taxonomy->cap->assign_terms );
$comma = _x( ',', 'tag delimiter' );
$terms_to_edit = get_terms_to_edit( $post->ID, $tax_name );
if ( ! is_string( $terms_to_edit ) ) {
$terms_to_edit = '';
}
?>
<div class="tagsdiv" id="<?php echo $tax_name; ?>">
<ul class="tagchecklist" role="list"></ul>
<div class="jaxtag">
<div class="nojs-tags hide-if-js">
<label for="tax-input-<?php echo $tax_name; ?>"><?php echo $taxonomy->labels->add_or_remove_items; ?></label>
<p><textarea name="<?php echo "tax_input[$tax_name]"; ?>" rows="13" cols="20" class="the-tags" id="tax-input-<?php echo $tax_name; ?>" <?php disabled( ! $user_can_assign_terms ); ?> aria-describedby="new-tag-<?php echo $tax_name; ?>-desc"><?php echo str_replace( ',', $comma . ' ', $terms_to_edit ); // textarea_escaped by esc_attr() ?></textarea></p>
</div>
<?php if ( $user_can_assign_terms ) : ?>
<div class="ajaxtag hide-if-no-js">
<label class="screen-reader-text" for="new-tag-<?php echo $tax_name; ?>"><?php echo $taxonomy->labels->add_new_item; ?></label>
<input data-wp-taxonomy="<?php echo $tax_name; ?>" type="text" id="new-tag-<?php echo $tax_name; ?>" name="newtag[<?php echo $tax_name; ?>]" class="newtag form-input-tip" size="16" autocomplete="off" aria-describedby="new-tag-<?php echo $tax_name; ?>-desc" value="" />
<input type="button" class="button tagadd" value="<?php esc_attr_e( '添加标签' ); ?>" />
</div>
<p class="howto" id="new-tag-<?php echo $tax_name; ?>-desc"><?php echo $taxonomy->labels->separate_items_with_commas; ?></p>
<?php elseif ( empty( $terms_to_edit ) ) : ?>
<p><?php echo $taxonomy->labels->no_terms; ?></p>
<?php endif; ?>
</div>
</div>
<?php if ( $user_can_assign_terms ) : ?>
<p class="hide-if-no-js"><button type="button" class="button-link tagcloud-link" id="link-<?php echo $tax_name; ?>" aria-expanded="false"><?php echo $taxonomy->labels->choose_from_most_used; ?></button></p>
<?php endif; ?>
<?php
}
/*** 单页 SEO参数 设置 ***/
public function cmb2_post_metabox() {
// SEO 参数设置
$seo_metabox = new_cmb2_box(array(
'id' => 'heku_seo_metabox',
'object_types' => array( 'post','news','page','term'),
'context' => 'after_editor',
'taxonomies' => array( 'category', 'post_tag' ),
'remove_box_wrap' => true,
));
$seo_metabox->add_field(array(
'id' => 'heku_seo_title',
'before_row'=>'<div id="tag_seo_preview" class="cmb-row"></div><div id="tag_seo_title" class="cmb-row">',
'name' => __('SEO标题','b2'),
'type' => 'text',
'render_row_cb' => array($this,'cmb_seo_title_render_row_cb'),
'default'=>'',
'desc'=>__('搜索引擎的标题部分,一般不多于54个字符为最佳。调用字段:heku_seo_title','heku'),
'after_row'=>'</div>'
));
$seo_metabox->add_field(array(
'id' => 'heku_seo_description',
'before_row'=>'<div id="tag_seo_description" class="cmb-row">',
'name' => __('SEO描述','heku'),
'desc'=>__('搜索引擎的描述部分,一般不少于70个字符,不多与120个字符为最佳。调用字段:heku_seo_description','heku'),
'type' => 'textarea_small',
'render_row_cb' => array($this,'cmb_seo_description_render_row_cb'),
'after_row'=>'<div id="tag_seo_keywords" class="cmb-row"></div>'
));
$seo_metabox->add_field( array(
'name' => __( 'OpenGraph 标签图片', 'heku' ),
'id' => 'heku_seo_img',
'type' => 'file',
'options' => array(
'url' => true,
),
'text' => array(
'add_upload_file_text' => __( '选择 图片', 'heku' ),
),
'default' => '/wp-content/themes/scdc-marine/assets/img/scdc-logo.png',
'query_args' => array(
'type' => array(
'image/svg+xml',
'image/gif',
'image/jpeg',
'image/png',
),
),
'desc' => sprintf(__( ' 最小尺寸为 300 x 200 的图像 ,建议使用 %s 格式的 logo 图片。建议使用 https:// 开通的链接, %s ', 'heku' ),'<code> (JPG 或 PNG) </code>','<span class="heku-debug"> 字段ID:<code> global_seo_img </code> </span> '),
'after_row'=>'</div>',
));
// SEO 参数设置
}
public function cmb_seo_title_render_row_cb( $field_args, $field ) {
$id = $field->args( 'id' );
$label = $field->args( 'name' );
$name = $field->args( '_name' );
$value = $field->escaped_value();
$description = $field->args( 'description' );
$before_row = $field->args( 'before_row' );
$after_row = $field->args( 'after_row' );
$desc = '';
if(get_post($field->object_id)){
$desc = mb_strimwidth(preg_replace("/(\s|\ \;| |\xc2\xa0)/", "", strip_tags(apply_filters('the_content', get_post($field->object_id)->post_title))), 0, 60,"");;
if($value==""){
$value = $desc;
}
}
?>
<?php echo $before_row; ?>
<div class="cmb-row cmb-type-text table-layout">
<div class="cmb-th">
<label for="<?php echo $id; ?>"><?php echo $label; ?></label>
</div>
<div class="cmb-td">
<input type="text" id="<?php echo $id; ?>" name="<?php echo $name; ?>" value="<?php echo $value; ?>" placeholder="<?php echo $desc ; ?>">
<p class="description"><?php echo $description; ?></p>
</div>
</div>
<?php echo $after_row; ?>
<?php
}
public function cmb_seo_description_render_row_cb( $field_args, $field ) {
$id = $field->args( 'id' );
$label = $field->args( 'name' );
$name = $field->args( '_name' );
$value = $field->escaped_value();
$description = $field->args( 'description' );
$before_row = $field->args( 'before_row' );
$after_row = $field->args( 'after_row' );
$desc = '';
if(get_post($field->object_id)){
$desc = mb_strimwidth(preg_replace("/(\s|\ \;| |\xc2\xa0)/", "", strip_tags(apply_filters('the_content', get_post($field->object_id)->post_content))), 0, 90,"");
if($value==""){
$value = $desc;
}
}
?>
<?php echo $before_row; ?>
<div class="cmb-row cmb-type-text table-layout">
<div class="cmb-th">
<label for="<?php echo $id; ?>"><?php echo $label; ?></label>
</div>
<div class="cmb-td">
<textarea id="<?php echo $id; ?>" name="<?php echo $name; ?>" value="<?php echo $value; ?>" placeholder="<?php echo $desc ; ?>" rows="2" cols="80" ><?php echo $value; ?></textarea>
<p class="description"><?php echo $description; ?></p>
</div>
</div>
<?php echo $after_row; ?>
<?php
}
/*** 首页 SEO参数 设置 ***/
public function menu_seo_options(){
// 首页 SEO参数 模块 '<div class="links-module cmb-row"> <h2 style="padding-left: 10px;">SEO参数</h2>',
$heku_seo = new_cmb2_box(array(
'id' => 'heku_seo_options', // 列表的ID
'icon_url' => 'dashicons-buddicons-groups',
'option_key' => 'heku_seo_options',
'position' => 28,
'show_on' => array(
'options-page' => 'heku_seo_options',
),
'object_types' => array( 'options-page' ),
'menu_title' => __('SEO 设 置','heku'),
'title' => __('首页 SEO 设置','heku'),
));
$heku_seo->add_field(array(
'id' => 'global_seo_title',
'before_row'=>'<div class="links-module cmb-row"> <h2 style="padding-left: 10px;">首页 SEO 参数设置</h2> <div id="tag_seo_preview" class="cmb-row"></div><div id="tag_seo_title" class="cmb-row">',
'name' => __('SEO标题','b2'),
'type' => 'text',
'default'=>'',
'desc'=>__('网站首页的标题,使用与meta标签的"title"部分,一般多于54个字符为最佳。调用字段:global_seo_title','heku'),
'after_row'=>'</div>'
));
$heku_seo->add_field(array(
'id' => 'global_seo_description',
'before_row'=>'<div id="tag_seo_description" class="cmb-row">',
'name' => __('SEO描述','heku'),
'desc'=>__('网站首页的描述,使用与meta标签的"description"部分,一般不少于70个字符,不多与120个字符为最佳。调用字段:global_seo_description','heku'),
'type' => 'textarea_small',
'default'=>'',
'after_row'=>'</div><div id="tag_seo_keywords" class="cmb-row"></div>'
));
$heku_seo->add_field(array(
'id' => 'global_seo_keywords',
'before_row'=>'<div id="tag_seo_preview" class="cmb-row"></div><div id="tag_seo_title" class="cmb-row">',
'name' => __('SEO 关键词','b2'),
'type' => 'text',
'default'=>'',
'desc'=>__('网站首页的优化关键词,多个关键词请用英文逗号(,)分开,一般不多余8个关键词。调用字段:global_seo_keywords','heku'),
'after_row'=>'</div>',
));
$heku_seo->add_field( array(
'name' => __( 'OpenGraph 标签图片', 'heku' ),
'id' => 'global_seo_img',
'type' => 'file',
'options' => array(
'url' => true,
),
'text' => array(
'add_upload_file_text' => __( '选择 图片', 'heku' ),
),
'default' => '/wp-content/themes/scdc-marine/assets/img/scdc-logo.png',
'query_args' => array(
'type' => array(
'image/svg+xml',
'image/gif',
'image/jpeg',
'image/png',
),
),
'desc' => sprintf(__( ' 最小尺寸为 300 x 200 的图像 ,建议使用 %s 格式的 logo 图片。建议使用 https:// 开通的链接, %s ', 'heku' ),'<code> (JPG 或 PNG) </code>','<span class="heku-debug"> 字段ID:<code> global_seo_img </code> </span> '),
'after_row'=>'</div>',
));
// 首页 SEO参数 模块 结束 'after_row'=>'</div>'
}
}