2 Star 9 Fork 3

背字根 / WordPress后台控制面板类

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
ThemeSetting.php 18.76 KB
一键复制 编辑 原始数据 按行查看 历史
背字根 提交于 2021-04-25 20:08 . Initial commit
<?phpclass ThemeSetting { private $options = array(); function __set($name, $value) { $this->$name = $value; } /** * Meta表单 */ private function metaBox($feild) { $feild['value'] = $feild['value'] ? $feild['value'] : $feild['default']; if($feild['type'] == 'input') { echo '<input type="text" id="' . $feild['name'] . '" class="' . $feild['class'] . '" name="' . $feild['name'] . '" value="' . $feild['value'] . '" placeholder="' . $feild['placeholder'] . '" size="' . $feild['size'] . '" />'; } if($feild['type'] == 'textarea') { echo '<textarea id="' . $feild['name'] . '" class="' . $feild['class'] . '" name="' . $feild['name'] . '" rows="' . $feild['rows'] . '" cols="' . $feild['cols'] . '">' . $feild['value'] . '</textarea>'; } if($feild['type'] == 'select') { if( ! is_array($feild['option']) ) { $feild['option'] = explode(',', $feild['option']); } echo '<select id="' . $feild['name'] . '" name="' . $feild['name'] . '">'; foreach($feild['option'] as $key=>$val) { $selected = ''; if( $feild['value'] == $key ) $selected = ' selected="selected"'; echo '<option value="' . $key . '"' . $selected . '>' . $val . '</option>'; } echo '</select>'; } if($feild['type'] == 'checkbox') { if( ! is_array($feild['option']) ) { $feild['option'] = explode(',', $feild['option']); } $feild['value'] = explode(',', $feild['value']); foreach($feild['option'] as $key=>$val) { $checked = ''; if( in_array($key, $feild['value']) ) $checked = ' checked="checked"'; echo '<label><input type="checkbox" name="' . $feild['name'] . '[]" value="' . $key . '"' . $checked . ' /> ' . $val . '</label><br />'; } } if($feild['type'] == 'radio') { if( ! is_array($feild['option']) ) { $feild['option'] = explode(',', $feild['option']); } foreach($feild['option'] as $key=>$val) { $checked = ''; if( $feild['value'] == $key ) $checked = ' checked="checked"'; echo '<label><input type="radio" name="' . $feild['name'] . '" value="' . $key . '"' . $checked . ' /> ' . $val . '</label><br />'; } } if($feild['type'] == 'file') { echo '<input type="hidden" name="' . $feild['name'] . '" id="' . $feild['name'] . '_input" value="' . $feild['value'] . '" />'; echo '<img id="' . $feild['name'] . '_preview" src="' . $feild['value'] . '" alt="" width="' . $feild['width'] . '" height="' . $feild['height'] . '" /><br />'; echo '<input class="button theme-add-media" type="button" value="上传图片" id="' . $feild['name'] . '_button" />'; } } /** * 支持上传的JS,WP 3.55之前版本样式 */ private function oldUploadJS() { wp_enqueue_script('thickbox'); wp_enqueue_style('thickbox'); echo '<script type="text/javascript"> jQuery(document).ready(function(){ var target_field, target_img; jQuery("body").on("click", ".theme-add-media", function(){ var feild_name = jQuery(this).attr("id").replace("_button", ""); target_field = jQuery("#" + feild_name + "_input"); target_img = jQuery("#" + feild_name + "_preview"); tb_show("上传图片", "media-upload.php?type=image&amp;TB_iframe=1"); return false; }); window.send_to_editor = function(html){ var img_url = jQuery( html ).attr("src"); target_field.val(img_url); target_img.attr("src", img_url); tb_remove(); } }); </script>'; } /** * 支持上传的JS,WP新版本样式 */ private function uploadJS() { wp_enqueue_media(); echo '<script type="text/javascript"> jQuery(document).ready(function(){ var theme_upload_frame, target_field, target_img; jQuery("body").on("click", ".theme-add-media", function(event) { event.preventDefault(); var feild_name = jQuery(this).attr("id").replace("_button", ""); target_field = jQuery("#" + feild_name + "_input"); target_img = jQuery("#" + feild_name + "_preview"); if( theme_upload_frame ) { theme_upload_frame.open(); return; } theme_upload_frame = wp.media({ title: "上传图片", button: { text: "确认", }, multiple: false }); theme_upload_frame.on("select", function() { var attachment = theme_upload_frame.state().get("selection").first().toJSON(); target_field.val(attachment.url); target_img.attr("src", attachment.url); }); theme_upload_frame.open(); }); }); </script>'; } /** * 文章Meta设置 * 参数为二维数组 */ public function postMeta() { $defaults = array( 'id' => '', 'heading' => '', 'screen' => 'post', 'context' => 'side', 'priority' => 'high', 'style' => 'v', 'upload' => false, 'feild' => array(), ); if( empty($this->options) || ! is_array($this->options) ) return false; $this->options = wp_parse_args( $this->options, $defaults ); add_action('admin_menu', array($this, 'addPostMetaBox') ); add_action('save_post', array($this, 'savePostMeta') ); } /** * 添加文章Meta */ function addPostMetaBox() { if ( function_exists('add_meta_box') ) { add_meta_box( $this->options['id'], $this->options['heading'], array($this, 'postMetaBox'), $this->options['screen'], $this->options['context'], $this->options['priority'] ); } } /** * 文章Meta表单 */ function postMetaBox($post) { if($this->options['upload']) { $this->uploadJS(); } if($this->options['style'] == 'h') { $this->hPostMetaBox($post); } else { $this->vPostMetaBox($post); } } /** * 文章Meta水平排列表单 */ private function hPostMetaBox($post) { $defaults = array( 'size' => 25, 'rows' => 3, 'cols' => 25, 'width' => 80, 'height' => 80, 'class' => 'large-text', ); echo '<table width="100%" cellpadding="5">'; foreach($this->options['feild'] as $feild) { if( ! $feild['name'] ) continue; $feild = wp_parse_args( $feild, $defaults ); $feild['value'] = get_post_meta($post->ID, $feild['name'], true); echo '<tr>'; echo $feild['label'] ? '<td style="white-space: nowrap; vertical-align: top;"><label for="' . $feild['name'] . '">' . $feild['label'] . '</label></td>' : ''; echo '<td>'; $this->metaBox($feild); echo $feild['dsc'] ? '<br /><span class="howto">' . $feild['dsc'] . '</span>' : ''; echo '</td></tr>'; } echo '</table>'; } /** * 文章Meta垂直排列表单 */ private function vPostMetaBox($post) { $defaults = array( 'size' => 25, 'rows' => 3, 'cols' => 25, 'width' => 80, 'height' => 80, 'class' => 'large-text', ); foreach($this->options['feild'] as $feild) { if( ! $feild['name'] ) continue; echo '<p>'; $feild = wp_parse_args( $feild, $defaults ); $feild['value'] = get_post_meta($post->ID, $feild['name'], true); echo $feild['label'] ? '<label style="display: block;" for="' . $feild['name'] . '">' . $feild['label'] . '</label>' : ''; $this->metaBox($feild); echo $feild['dsc'] ? '<br /><span class="howto">' . $feild['dsc'] . '</span>' : ''; echo '</p>'; } } /** * 保存文章Meta */ function savePostMeta($post_id) { if ( 'page' == $_POST['post_type'] ) { if ( ! current_user_can( 'edit_page', $post_id ) ) return $post_id; } else { if ( ! current_user_can( 'edit_post', $post_id )) return $post_id; } foreach($this->options['feild'] as $feild) { if( ! $feild['name'] ) continue; $data = $_POST[$feild['name']]; if( is_array($data) ) { $data = implode(',', $data); } else { $data = trim($data); } if($data && $data != get_post_meta($post_id, $feild['name']) ) { update_post_meta($post_id, $feild['name'], $data); } elseif( ! $data ) { delete_post_meta($post_id, $feild['name']); } } } /** * 页面Meta设置 * 参数为二维数组 */ public function pageMeta() { $defaults = array( 'id' => '', 'heading' => '', 'screen' => 'page', 'context' => 'side', 'priority' => 'high', 'upload' => false, 'feild' => array(), ); if( empty($this->options) || ! is_array($this->options) ) return false; $this->options = wp_parse_args( $this->options, $defaults ); add_action('admin_menu', array($this, 'addPostMetaBox') ); add_action('save_post', array($this, 'savePostMeta') ); } /** * 分类Meta设置 * 参数为二维数组 */ public function catMeta() { $defaults = array( 'upload' => false, ); if( empty($this->options) || ! is_array($this->options) ) return false; $this->options = wp_parse_args( $this->options, $defaults ); add_action( 'category_add_form_fields', array($this, 'addCatMetaBox'), 10, 2 ); add_action( 'post_tag_add_form_fields', array($this, 'addCatMetaBox'), 10, 2 ); add_action( 'category_edit_form_fields', array($this, 'editCatMetaBox'), 10, 2 ); add_action( 'post_tag_edit_form_fields', array($this, 'editCatMetaBox'), 10, 2 ); add_action( 'created_category', array($this, 'saveCatMeta'), 10, 1 ); add_action( 'edited_category', array($this, 'saveCatMeta'), 10, 1 ); add_action( 'created_post_tag', array($this, 'saveCatMeta'), 10, 1 ); add_action( 'edited_post_tag', array($this, 'saveCatMeta'), 10, 1 ); add_action( 'delete_category', array($this, 'deleteCatMeta'), 10, 1 ); add_action( 'delete_post_tag', array($this, 'deleteCatMeta'), 10, 1 ); } /** * 添加分类Meta表单 */ function addCatMetaBox($tag) { $defaults = array( 'size' => 40, 'rows' => 5, 'cols' => 40, 'width' => 80, 'height' => 80, ); if($this->options['upload']) { $this->uploadJS(); } foreach($this->options['feild'] as $feild) { if( ! $feild['name'] ) continue; $feild = wp_parse_args( $feild, $defaults ); $feild['value'] = get_term_meta( $tag->term_id , $feild['name'], true ); echo '<div class="form-field">'; echo '<label for="' . $feild['name'] . '">' . $feild['label'] . '</label>'; $this->metaBox($feild); echo $feild['dsc'] ? '<p>' . $feild['dsc'] . '</p>' : ''; echo '</div>'; } } /** * 编辑分类Meta表单 */ function editCatMetaBox($tag) { $defaults = array( 'size' => 40, 'rows' => 5, 'cols' => 50, 'width' => 80, 'height' => 80, 'class' => 'large-text', ); if($this->options['upload']) { $this->uploadJS(); } foreach($this->options['feild'] as $feild) { if( ! $feild['name'] ) continue; $feild = wp_parse_args( $feild, $defaults ); $feild['value'] = get_term_meta( $tag->term_id , $feild['name'], true ); echo '<tr class="form-field">'; echo '<th scope="row" valign="top"><label for="' . $feild['name'] . '">' . $feild['label'] . '</label></th><td>'; $this->metaBox($feild); echo $feild['dsc'] ? '<p class="description">' . $feild['dsc'] . '</p>' : ''; echo '</td></tr>'; } } /** * 保存分类Meta */ function saveCatMeta($term_id) { if( ! current_user_can( 'manage_categories' ) ) return $term_id ; foreach($this->options['feild'] as $feild) { if( ! $feild['name'] ) continue; $data = $_POST[$feild['name']]; if( is_array($data) ) { $data = implode(',', $data); } else { $data = trim($data); } update_term_meta( $term_id , $feild['name'], $data ); } } /** * 删除分类Meta */ function deleteCatMeta($term_id) { if( ! current_user_can( 'manage_categories' ) ) return $term_id ; foreach($this->options['feild'] as $feild) { if( ! $feild['name'] ) continue; delete_term_meta( $term_id , $feild['name'] ); } } /** * 主题设置 * 参数为三维数组,幻灯片与页面为二维数组 */ public function themeControl() { add_action('admin_menu', array($this, 'addThemeMenu') ); } /** * 添加主题菜单 */ function addThemeMenu() { add_menu_page( '主题设置', '主题设置', 'administrator', 'themeControl', array($this, 'themeControlBox'), 'dashicons-screenoptions'); } /** * 主题控制面板 */ function themeControlBox() { if( empty($this->options) || ! is_array($this->options) ) return false; $message = ''; if ( isset($_POST['action']) && 'save' == $_POST['action'] ) { foreach($this->options as $options) { if($options['feild']) { foreach($options['feild'] as $feild) { if( ! $feild['name'] ) continue; if( ! isset($_POST[$feild['name']]) ) continue; $data = $_POST[$feild['name']]; if( is_array($data) ) { $data = implode(',', $data); } else { $data = trim($data); } update_option( $feild['name'], stripslashes($data) ); } } if($options['type'] == 'slide' && $options['name']) { $slide_key = $_POST['slide_key']; $slide_data = array(); if( ! empty($slide_key) ) { foreach($slide_key as $key) { if( empty($_POST['img_' . $key]) ) continue; $slide_data[$key] = array( 'img' => $_POST['img_' . $key], 'alt' => trim($_POST['alt_' . $key]), 'link' => trim($_POST['link_' . $key]), ); } } $up_slide = serialize($slide_data); update_option( $options['name'], $up_slide ); } } $message = '设置已保存!'; } $defaults = array( 'size' => 40, 'rows' => 5, 'cols' => 50, 'width' => 80, 'height' => 80, 'class' => 'large-text', ); echo <<<EXCERPT <style type="text/css"> .wrap div.notice { margin: 20px 0; padding: .5em 1em; } .wrap .tips { margin: 20px 0; } .wrap .option { display: none; } .wrap .option-active { display: block; } .wrap .slide { background-color: #fafafa; } .wrap .slide tr:nth-of-type(even) { background-color: #fefefe; } .wrap .slide th, .wrap .slide td { padding: 15px 10px; border: 1px solid #ececec; } </style> <script type="text/javascript"> jQuery(document).ready(function(){ jQuery('.nav-tab-wrapper .nav-tab').click(function(){ jQuery(this).addClass('nav-tab-active').siblings().removeClass('nav-tab-active'); jQuery(jQuery(this).attr('href')).addClass('option-active').siblings().removeClass('option-active'); jQuery(this).blur(); return false; }); jQuery('.slide').on('click', '.del-img', function(){ jQuery(this).parent().parent().remove(); }); jQuery('.slide').on('click', '.add-img', function(){ var parenttr = jQuery(this).parent().parent(); var date = new Date(); var key = date.getTime(); var parent_key = jQuery(this).parent().find('input[type=hidden]').val(); var reg = new RegExp(parent_key, 'g'); parenttr.after(parenttr.clone().get(0).outerHTML.replace(reg, key)); jQuery('#img_' + key + '_preview').attr('src', ''); jQuery('#img_' + key + '_input').val(''); jQuery('#alt_' + key).val(''); jQuery('#link_' + key).val(''); }); jQuery('.slide').on('click', '.up-img', function(){ var parenttr = jQuery(this).parent().parent(); if( parenttr.prev().prev().length == 0 ) return false; parenttr.prev().before(parenttr); }); jQuery('.slide').on('click', '.down-img', function(){ var parenttr = jQuery(this).parent().parent(); if( parenttr.next().length == 0 ) return false; parenttr.next().after(parenttr); }); }); </script>EXCERPT; $this->uploadJS(); echo '<div class="wrap">'; echo '<h2 class="nav-tab-wrapper">'; $count = 1; // 输出选项卡 foreach($this->options as $options) { $class = $count == 1 ? ' nav-tab-active' : ''; echo '<a href="#' . $options['id'] . '" class="nav-tab' . $class . '">' . $options['heading'] . '</a>'; $count++; } echo '</h2>'; echo ! empty($message) ? '<div class="updated notice">' . $message . '</div>' : ''; echo '<form method="post" action="">'; $count = 1; foreach($this->options as $options) { $class = $count == 1 ? ' option-active' : ''; echo '<div id="' . $options['id'] . '" class="option' . $class . '">'; echo ! empty($options['dsc']) ? '<div class="tips">' . $options['dsc'] . '</div>' : ''; if($options['type'] == 'slide' && $options['name']) { // 幻灯片输出 echo '<table class="form-table slide">'; echo '<tr><th scope="col">图片</th><th scope="col">描述</th><th scope="col">链接</th><th scope="col"></th></tr>'; $slides = get_option($options['name']); $slides = unserialize($slides); $img_feild = array( 'type' => 'file', 'name' => 'img', ); $img_feild['width'] = $options['width'] ? $options['width'] : 320; $img_feild['height'] = $options['height'] ? $options['height'] : 80; $alt_feild = array( 'type' => 'textarea', 'name' => 'alt', 'rows' => 3, 'cols' => 50, 'class' => 'large-text', ); $link_feild = array( 'type' => 'input', 'name' => 'link', 'size' => 40, 'class' => 'large-text', ); if( ! empty($slides) ) { foreach($slides as $key=>$val) { $img_feild['value'] = $val['img']; $alt_feild['value'] = $val['alt']; $link_feild['value'] = $val['link']; $img_feild['name'] = 'img_' . $key; $alt_feild['name'] = 'alt_' . $key; $link_feild['name'] = 'link_' . $key; echo '<tr><td>'; $this->metaBox($img_feild); echo '</td><td>'; $this->metaBox($alt_feild); echo '</td><td>'; $this->metaBox($link_feild); echo '</td><td>'; echo '<input class="button add-img" type="button" value="添加" /> <input class="button del-img" type="button" value="删除" /> <input class="button up-img" type="button" value="↑" /> <input class="button down-img" type="button" value="↓" />'; echo '<input type="hidden" name="slide_key[]" value="' . $key . '" />'; echo '</td></tr>'; } } else { $slide_key = floor(microtime(true)*1000); $img_feild['name'] = 'img_' . $slide_key; $alt_feild['name'] = 'alt_' . $slide_key; $link_feild['name'] = 'link_' . $slide_key; echo '<tr><td>'; $this->metaBox($img_feild); echo '</td><td>'; $this->metaBox($alt_feild); echo '</td><td>'; $this->metaBox($link_feild); echo '</td><td>'; echo '<input class="button add-img" type="button" value="添加" /> <input class="button del-img" type="button" value="删除" />'; echo '<input type="hidden" name="slide_key[]" value="' . $slide_key . '" />'; echo '</td></tr>'; } echo '</table>'; } elseif($options['type'] == 'page') { // 页面输出,例如欢迎页面 echo $options['content']; } else { // 表单输出 echo '<table class="form-table">'; foreach($options['feild'] as $feild) { if( ! $feild['name'] ) continue; $feild = wp_parse_args( $feild, $defaults ); $feild['value'] = get_option( $feild['name'] ); echo '<tr>'; echo $feild['label'] ? '<th scope="row"><label for="' . $feild['name'] . '">' . $feild['label'] . '</label></th>' : ''; echo '<td>'; $this->metaBox($feild); echo $feild['dsc'] ? '<p class="description">' . $feild['dsc'] . '</p>' : ''; echo '</td></tr>'; } echo '</table>'; } echo '</div>'; $count++; } echo '<p><input type="submit" class="button-primary" value="保存设置" /><input type="hidden" name="action" value="save" /></p>'; echo '</form>'; echo '</div>'; }}?>
PHP
1
https://gitee.com/beizigen/wordpress-theme-control-panel.git
git@gitee.com:beizigen/wordpress-theme-control-panel.git
beizigen
wordpress-theme-control-panel
WordPress后台控制面板类
master

搜索帮助