File: /www/wwwroot/www.scdc-marine.com/wp-content/themes/scdc-marine/HekuClass/Admin/Cmb2News.php
<?php
namespace Heku\HekuClass\Admin;
/**
* @package Heku\HekuClass\Admin
* @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 Cmb2News {
/**
* 类实现单例模式,类的独生子女
*/
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() {
add_action( 'init', array($this,'custom_post_news') );
add_action( 'cmb2_admin_init', array($this,'cmb2_news_metabox'));
add_action( 'edit_form_after_title', array( $this, 'edit_form_after_title' ) );
//add_action( 'edit_form_after_editor', array( $this, 'edit_form_after_editor' ) );
}
public function custom_post_news() {
$labels = array(
'name' => _x( '新闻中心', 'post type general name' ),
'singular_name' => _x( 'News', 'post type singular name' ),
'add_new' => _x( '添加新闻','add news' ),
'add_new_item' => __( '发布新闻' ),
'edit_item' => __( '编辑新闻' ),
'new_item' => __( '添加新闻' ),
'all_items' => __( '新闻列表' ),
'view_item' => __( '浏览新闻' ),
'search_items' => __( '查找新闻' ),
'not_found' => __( '没有找到新闻' ),
'not_found_in_trash' => __( '回收站没有新闻' ),
'parent_item_colon' => '',
'menu_name' => __( '新 闻 中 心' )
);
$args = array(
'labels' => $labels,
'description' => 'Holds our product and product specific data',
'public' => true,
'menu_position' => 7, //5是产品中心 10是已经隐藏的媒体页面 20是页面管理
'has_archive' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array( 'slug' => strtolower( 'news' ) ),
'capability_type' => 'post',
'hierarchical' => false,
'supports' => array( 'title', 'editor', 'thumbnail' ),
'taxonomies' => array( 'post_tag' )
);
register_post_type( 'news', $args );
//Custom Taxonomies (news Categories) 为新闻添加新闻分类
// function taxonomies_news() {
// $labels = array(
// 'name' => _x( '新闻分类', 'taxonomy general name' ),
// 'singular_name' => _x( 'News Categories', 'taxonomy singular name' ),
// 'search_items' => __( '搜索新闻分类' ),
// 'all_items' => __( '新闻分类' ),
// 'parent_item' => __( '新闻分类' ),
// 'parent_item_colon' => __( '新闻分类:' ),
// 'edit_item' => __( '新闻分类修改' ),
// 'update_item' => __( '新闻分类更新' ),
// 'add_new_item' => __( '新增新闻分类' ),
// 'new_item_name' => __( '新新闻分类' ),
// 'menu_name' => __( '新闻分类' ),
// );
// $args = array(
// 'labels' => $labels,
// 'hierarchical' => true,
// );
// register_taxonomy( 'news_catalog', 'news', $args );
// }
// add_action( 'init', 'taxonomies_news', 0 );
}
public function cmb2_news_metabox() {
$news_side_metabox = new_cmb2_box(array(
'id' => 'news_side_metabox',
'object_types' => array( 'news'), // Post type
'context' => 'side',
'title' => '缩略图'
//'remove_box_wrap' => true, // 省略“title”属性,改为添加“remove_box_wrap”=>true,这样就没有 Metabox
));
$news_side_metabox->add_field( array(
'before_row'=>'<div class="slider-1-module cmb-row ">',
'name' => __( '缩略图图片(深色)', 'heku' ),
'id' => 'heku_thumbnail',
'type' => 'file',
'options' => array(
'url' => true,
),
'text' => array(
'add_upload_file_text' => __( '选择 缩略图', 'heku' ),
),
'query_args' => array(
'type' => array(
'image/svg+xml',
'image/gif',
'image/jpeg',
'image/png',
),
),
'desc' => sprintf(__( ' 文章的缩略图,建议使用 %s 格式的图片,以适应高分辨率屏幕。%s ', 'heku' ),'<code>.jpg</code>','<span class="heku-debug"> 字段ID:<code> heku_thumbnail </code> </span> '),
'after_row'=>'</div>',
) );
// 产品额外参数 设置
$news_parameter_metabox = new_cmb2_box(array(
'id' => 'news_after_editor_metabox',
'object_types' => array( 'news'),
'context' => 'after_editor',
'remove_box_wrap' => true,
) );
$news_parameter_metabox->add_field( array(
'before_row'=>'<div class="clear cmb-row"> <h2 style="font-size: 18px;padding-left: 0px;margin-bottom: 0px;">相关新闻和案例</h2>',
'name' => __( '不展示', 'heku' ),
'desc' => __( '选择相关联的新闻和案例链接,最好有图片,调用ID:product_related_news ', 'heku' ),
'id' => 'product_related_news',
'type' => 'custom_attached_posts',
'show_names' => false,
'column' => true, // Output in the admin post-listing as a custom column. https://github.com/CMB2/CMB2/wiki/Field-Parameters#column
'options' => array(
'show_thumbnails' => true, // Show thumbnails on the left
'filter_boxes' => true, // Show a text box for filtering the results
//'query_users' => true, // Do users instead of posts/custom-post-types.
'query_args' => array(
'posts_per_page' => 10,
'post_type' => array('news'),
),
),
'after_row'=>'</div>'
) );
$news_parameter_metabox->add_field( array(
'before_row'=>'<div id="product_parameter_div"> <div class="clear cmb-row"> <h2 style="font-size: 18px;padding-left: 0px;margin-bottom: 0px;">相关产品展示</h2>',
'name' => __( '不展示', 'yourtextdomain' ),
'desc' => __( '选择相关联的产品链接,建议不要多余4个,ID: product_hot_post ', 'heku' ),
'id' => 'product_hot_post',
'type' => 'custom_attached_posts',
'show_names' => false,
//'render_row_cb' => array( $this , 'yourprefix_render_row_cb' ),
'column' => true, // Output in the admin post-listing as a custom column. https://github.com/CMB2/CMB2/wiki/Field-Parameters#column
'options' => array(
'show_thumbnails' => true, // Show thumbnails on the left
'filter_boxes' => true, // Show a text box for filtering the results
'query_args' => array(
'posts_per_page' => 10,
'post_type' => array('post'),
),
),
'after_row'=>'</div></div>'
) );
// 产品额外参数 设置
}
public function edit_form_after_title() {
global $post_type;
if('news' === $post_type ):?>
<ul class="edit-detail-tabs">
<li class="product_div current"><?php echo get_post_type_object($post_type)->labels->singular_name; ?>信息上传</li>
<li class="product_parameter">显示额外参数</li>
<li class="tag_seo">SEO参数设置</li>
</ul>
<style type="text/css">
#page_after_title_metabox{ margin-top: -10px;}
.edit-detail-tabs:after{content: "";display: block;clear: both;visibility: hidden;height: 0;}
.edit-detail-tabs{height: 36px;line-height: 35px;border-bottom: 1px solid #6f6f6f;font-size: 16px;margin: 10px auto 0px;}
.edit-detail-tabs li{float: left;padding: 0 15px;background: #6f6f6f;border-radius: 8px 8px 0 0;font-size: 12px;margin: 0 20px -2px 0;position: relative;z-index: 22;cursor: pointer;}
.edit-detail-tabs li,.edit-detail-tabs li a{color: #fff;}
.edit-detail-tabs li.current{background: #42586f;}
.edit-detail-tabs li.current,.edit-detail-tabs li.current a{color: #fff;}
#acf_15 .handlediv ,#acf_15 .hndle{ display: none;}
.acf-tab-group { padding: 20px 8px 0 0;}
.acf-tab-group li a{ border-top-left-radius: 10px; border-top-right-radius: 10px; padding: 6px 25px; }
.acf_postbox.default .acf-tab-group { padding-left: 25px;}
.acf_postbox.default .acf-tab-group li a{ background: #bed4ca;}
#acf_15 .acf-tab-group li { margin: 0 12px 0 0}
#acf_15{margin-bottom:7px}
#postdivrich { margin-top: 0px;}
#insert-media-button{margin:-6px 6px}
#aiosp .handlediv,#aiosp h2,#aio-pro-update{display: none;}
#aiosp .aioseop_wrapper{padding: 10px 13px 0;}
#aiosp .inside{padding: 0 30px 12px;}
#aiosp { margin-bottom:0px ;}
#aiosp .aioseop_option_label ,#aiosp .aioseop_label { line-height: 35px; }
#aiosp_noindex_wrapper,#aiosp_nofollow_wrapper,#aiosp_disable_wrapper{display: none;}
.postbox-container div#aiosp_upgrade_wrapper .aioseop_input .aioseop_option_input .aioseop_option_div{padding:0}
.cmb2-context-wrap .cmb-td{width:100%}
.mce-toolbar .mce-btn-group .mce-btn.mce-wp-dfw{margin: 10px 15px 0 0;}
#postbox-container-1 .postbox {border: 0px solid #c3c4c7; box-shadow: 2px 4px 4px rgba(0,0,0,0.1);}
#titlediv #title{border: 0px solid #8c8f94;height: 2em;padding: 3px 15px;box-shadow: 2px 4px 4px rgba(0,0,0,0.1);border-radius: 8px;}
#submitdiv.postbox, .postbox{ border-radius: 8px;}
.cmb2-context-wrap{margin-top:18px;}
</style>
<script>jQuery(document).ready(function($){
$('#product_parameter_show').hide()
$('#product_div_show,#product_parameter_show,#tag_seo_show').addClass('edit-detail-panel')
$('.edit-detail-tabs li').click(function() {
$(this).addClass('current').siblings().removeClass('current')
$('.edit-detail-panel').hide()
var activeClass = $(this).attr('class').split(" ")[0]
var activeCont = '#' + activeClass + '_show'
$(activeCont).show()
})
$('.product_div').click(function() {
$('#tag_seo_show').show()
})
$('#acf_15').appendTo('#product_div_show')
$('#postdivrich').appendTo('#product_div_show')
$('#acf_95').appendTo('#product_parameter_show')
$('#aiosp').appendTo('#tag_seo_show')
//$('#tagsdiv-post_tag').appendTo('#tag_seo_show')
})</script>
<?php endif;
}
public function edit_form_after_editor() {
global $post_type;
if('news' === $post_type ):?>
<ul class="edit-detail-tabs">
<li class="product_div current">产品信息上传<?php echo $post_type; ?></li>
<li class="product_parameter">显示额外参数</li>
<li class="tag_seo">SEO参数设置</li>
</ul>
<div id="product_div_show"></div>
<div id="product_parameter_show"></div>
<div id="tag_seo_show"></div>
<?php endif;
}
}