<?php
error_reporting(E_ALL ^ E_NOTICE)if ( !class_exists('myCustomFields') ) {
class myCustomFields {
var $prefix = 'git_';
var $postTypes = array( "page", "post", "product" );
var $customFields = array(
array(
"name" => "wx_submit",
"title" => "推送到订阅用户微信",
"description" => "请慎重选择是否推送,优质的文章才值得推送哦,滥用此功能的可能会被禁用该功能哦~",
"type" => "checkbox",
"scope" => array( "post" ),
"capability" => "manage_options"
),
array(
"name" => "thumb",
"title" => "自定义缩略图/主图",
"description" => "这里可以输入您的自定义缩略图链接、也是产品页面的产品主图",
"type" => "text",
"scope" => array( "post", "product" ),
"capability" => "edit_posts"
),
array(
"name" => "product_cpjianjie",
"title" => "产品的简介",
"description" => "这里可以输入您的产品的简介,同时作为产品页面的摘要,字数不要多",
"type" => "text",
"scope" => array( "product" ),
"capability" => "edit_posts"
),
array(
"name" => "product_jiage",
"title" => "产品的价格",
"description" => "这里可以输入您的产品的销售价格,显示在产品页面和产品详情页",
"type" => "text",
"scope" => array( "product" ),
"capability" => "edit_posts"
),
array(
"name" => "product_fahuodi",
"title" => "产品的发货地址",
"description" => "这里可以输入您的产品的发货地址",
"type" => "text",
"scope" => array( "product" ),
"capability" => "edit_posts"
),
array(
"name" => "product_tblink",
"title" => "产品的购买链接",
"description" => "这里可以输入您的产品的购买外链,已做nofollow",
"type" => "text",
"scope" => array( "product" ),
"capability" => "edit_posts"
),
array(
"name" => "zhuanzai_name",
"title" => "转载来源名字",
"description" => "这里可以输入文章转载名字,不填写的话,不显示转载标签",
"type" => "text",
"scope" => array( "post" ),
"capability" => "edit_posts"
),
array(
"name" => "zhuanzai_link",
"title" => "转载来源链接",
"description" => "这里可以输入您的转载来源链接",
"type" => "text",
"scope" => array( "post" ),
"capability" => "edit_posts"
),
array(
"name" => "download_name",
"title" => "单页下载文件名字",
"description" => "这里可以输入您的下载文件的名字",
"type" => "text",
"scope" => array( "post" ),
"capability" => "edit_posts"
),
array(
"name" => "download_size",
"title" => "单页下载文件大小",
"description" => "这里可以输入您的下载文件的大小,可以加上单位,比如:233KB或者233MB",
"type" => "text",
"scope" => array( "post" ),
"capability" => "edit_posts"
),
array(
"name" => "download_link",
"title" => "单页下载下载链接【新版】",
"description" => "按照链接,名字,备注的格式,注意中间是用英文逗号,换行可添加多个,举个栗子:<code>https://www.baidu.com,百度官网,中国最大的搜索引擎网站</code>",
"type" => "textarea",
"scope" => array( "post" ),
"capability" => "edit_posts"
),
array(
"name" => "demo",
"title" => "代码演示",
"description" => "请在这里输入您的演示代码",
"type" => "textarea",
"scope" => array( "post" ),
"capability" => "edit_pages"
)
);
function __construct() {
add_action( 'admin_menu', array( $this, 'createCustomFields' ) );
add_action( 'save_post', array( $this, 'saveCustomFields' ), 1, 2 );
}
function createCustomFields() {
if ( function_exists( 'add_meta_box' ) ) {
foreach ( $this->postTypes as $postType ) {
add_meta_box( 'my-custom-fields', 'Git 主题文章发布选项', array( $this, 'displayCustomFields' ), $postType, 'normal', 'high' );
}
}
}
function displayCustomFields() {
global $post;
?>
<div class="form-wrap">
<?php
wp_nonce_field( 'my-custom-fields', 'my-custom-fields_wpnonce', false, true );
foreach ( $this->customFields as $customField ) {
$scope = $customField[ 'scope' ];
$output = false;
foreach ( $scope as $scopeItem ) {
switch ( $scopeItem ) {
default: {
if ( $post->post_type == $scopeItem )
$output = true;
break;
}
}
if ( $output ) break;
}
if ( !current_user_can( $customField['capability'], $post->ID ) )
$output = false;
if ( $output ) { ?>
<div class="form-field form-required">
<?php
switch ( $customField[ 'type' ] ) {
case "checkbox": {
echo '<label for="' . $this->prefix . $customField[ 'name' ] .'" style="display:inline;"><b>' . $customField[ 'title' ] . '</b></label> ';
echo '<input type="checkbox" name="' . $this->prefix . $customField['name'] . '" id="' . $this->prefix . $customField['name'] . '" value="1"';
if ( get_post_meta( $post->ID, $this->prefix . $customField['name'], true ) == "1" )
echo ' checked="checked"';
echo '" style="width: auto;" />';
break;
}
case "textarea":
case "wysiwyg": {
echo '<label for="' . $this->prefix . $customField[ 'name' ] .'"><b>' . $customField[ 'title' ] . '</b></label>';
echo '<textarea name="' . $this->prefix . $customField[ 'name' ] . '" id="' . $this->prefix . $customField[ 'name' ] . '" columns="30" rows="5">' . htmlspecialchars( get_post_meta( $post->ID, $this->prefix . $customField[ 'name' ], true ) ) . '</textarea>';
if ( $customField[ 'type' ] == "wysiwyg" ) { ?>
<script type="text/javascript">
jQuery( document ).ready( function() {
jQuery( "<?php echo $this->prefix . $customField[ 'name' ]; ?>" ).addClass( "mceEditor" );
if ( typeof( tinyMCE ) == "object" && typeof( tinyMCE.execCommand ) == "function" ) {
tinyMCE.execCommand( "mceAddControl", false, "<?php echo $this->prefix . $customField[ 'name' ]; ?>" );
}
});
</script>
<?php }
break;
}
default: {
echo '<label for="' . $this->prefix . $customField[ 'name' ] .'"><b>' . $customField[ 'title' ] . '</b></label>';
echo '<input type="text" name="' . $this->prefix . $customField[ 'name' ] . '" id="' . $this->prefix . $customField[ 'name' ] . '" value="' . htmlspecialchars( get_post_meta( $post->ID, $this->prefix . $customField[ 'name' ], true ) ) . '" />';
break;
}
}
?>
<?php if ( $customField[ 'description' ] ) echo '<p>' . $customField[ 'description' ] . '</p>'; ?>
</div>
<?php
}
} ?>
</div>
<?php
}
function saveCustomFields( $post_id, $post ) {
if ( !isset( $_POST[ 'my-custom-fields_wpnonce' ] ) || !wp_verify_nonce( $_POST[ 'my-custom-fields_wpnonce' ], 'my-custom-fields' ) )
return;
if ( !current_user_can( 'edit_post', $post_id ) )
return;
if ( ! in_array( $post->post_type, $this->postTypes ) )
return;
foreach ( $this->customFields as $customField ) {
if ( current_user_can( $customField['capability'], $post_id ) ) {
if ( isset( $_POST[ $this->prefix . $customField['name'] ] ) && trim( $_POST[ $this->prefix . $customField['name'] ] ) ) {
$value = $_POST[ $this->prefix . $customField['name'] ];
if ( $customField['type'] == "wysiwyg" ) $value = wpautop( $value );
update_post_meta( $post_id, $this->prefix . $customField[ 'name' ], $value );
} else {
delete_post_meta( $post_id, $this->prefix . $customField[ 'name' ] );
}
}
}
}
}
} if ( class_exists('myCustomFields') ) {
$myCustomFields_var = new myCustomFields();
}
?>