wordpress后台制作教程-设置选项类文件
上一篇教程中我们介绍了一个配置wordpress后台选项的类文件构架,这里就将由wp自学笔记整理的类文件源码贴出示众。
版本控制
2013.07.08,版本1.0
- 增加数组类型
- 修改复选框功能
- 添加编辑器功能
懒人下载
- <?php
- /*wordpress后台设置类文件
- Version: 1.0
- Author: 树是我的朋友
- Author URI: http://www.ashuwp.com
- License:http://www.ashuwp.com/courses/optionpage/214.html
- */
- ashu_option_class{
- $options;
- $pageinfo;
- $database_options;
- $saved_optionname;
- //类的构建函数
- ashu_option_class($options, $pageinfo) {
- $this->options = $options;
- $this->pageinfo = $pageinfo;
- $this->make_data_available(); //准备设置选项数据
- add_action( ‘admin_menu’, (&$this, ‘add_admin_menu’) );
- ( isset($_GET[‘page’]) && ($_GET[‘page’] == $this->pageinfo[‘filename’]) ) {
- //加载css js
- add_action(‘admin_init’, (&$this, ‘enqueue_head’));
- }
- }
- enqueue_head() {
- //加载的js路径
- wp_enqueue_script(‘ashu_options_js’, TEMJS_URI.’ashu_options.js’);
- wp_enqueue_style(‘ashu_options_css’, TEMJS_URI.’ashu_options.css’);
- wp_enqueue_script(‘thickbox’);
- wp_enqueue_style(‘thickbox’);
- }
- //创建菜单项函数
- add_admin_menu() {
- //添加顶级菜单项
- $top_level = “主题设置”;
- (!$this->pageinfo[‘child’]) {
- add_menu_page($top_level, $top_level, ‘edit_themes’, $this->pageinfo[‘filename’], (&$this, ‘initialize’));
- define(‘TOP_LEVEL_BASEAME’, $this->pageinfo[‘filename’]);
- }{
- add_submenu_page(TOP_LEVEL_BASEAME, $this->pageinfo[‘full_name’], $this->pageinfo[‘full_name’], ‘edit_themes’, $this->pageinfo[‘filename’], (&$this, ‘initialize’));
- }
- }
- make_data_available() {
- $ashu_option; //申明全局变量
- ($this->options $option) {
- ( isset($option[‘std’]) ) {
- $ashu_option_std[$this->pageinfo[‘optionname’]][$option[‘id’]] = $option[‘std’];
- }
- }
- //选项组名称
- $this->saved_optionname = ‘ashu_’.$this->pageinfo[‘optionname’];
- $ashu_option[$this->pageinfo[‘optionname’]] = get_option($this->saved_optionname);
- //合并数组
- $ashu_option[$this->pageinfo[‘optionname’]] = array_merge(()$ashu_option_std[$this->pageinfo[‘optionname’]], ()$ashu_option[$this->pageinfo[‘optionname’]]);
- //html实体转换
- $ashu_option[$this->pageinfo[‘optionname’]] = $this->htmlspecialchars_deep($ashu_option[$this->pageinfo[‘optionname’]]);
- }
- //使用递归将预定义html实体转换为字符
- htmlspecialchars_deep($mixed, $quote_style = ENT_QUOTES, $charset = ‘UTF-8’) {
- (is_array($mixed) || is_object($mixed)) {
- ($mixed $key => $value) {
- $mixed[$key] = $this->htmlspecialchars_deep($value, $quote_style, $charset);
- }
- }
- (is_string($mixed)) {
- $mixed = htmlspecialchars_decode($mixed, $quote_style);
- }
- $mixed;
- }
- initialize() {
- $this->get_save_options();
- $this->display();
- }
- //显示表单项函数
- display() {
- $saveoption = false;
- echo ‘<div =“wrap”>’;
- echo ‘<div =“icon32” id=“icon-options-general”><br/></div>’;
- echo ‘<h2>’.$this->pageinfo[‘full_name’].'</h2>’;
- echo ‘<form method=“post” action=“”>’;
- //根据选项类型执行对应函数
- ($this->options $option) {
- (method_exists($this, $option[‘type’])) {
- $this->$option[‘type’]($option);
- $saveoption = true;
- }
- }
- ($saveoption) {
- echo ‘<p =“submit”>’;
- echo ‘<input type=“hidden” value=“1” name=“save_my_options”/>’;
- echo ‘<input type=“submit” name=“Submit” =“button-primary autowidth” value=“Save Changes” /></p>’;
- }
- echo ‘</form></div>’;
- }
- //更新数据
- get_save_options() {
- $options = $newoptions = get_option($this->saved_optionname);
- ( isset( $_POST[‘save_my_options’] ) ) {
- echo ‘<div =“updated fade” id=“message” style=“”><p><strong>Settings saved.</strong></p></div>’;
- $opion_count = 0;
- ($_POST $key => $value) {
- ( preg_match(“/^(numbers_)/”, $key, $result) ){
- $numbers = explode( ‘,’, $value );
- $newoptions[$key] = $numbers;
- }( preg_match(“/^(tinymce_)/”, $key, $result) ){
- $value = stripslashes($value);
- $newoptions[$key] = $value;
- }( preg_match(“/^(checkbox_)/”, $key, $result) ){
- $newoptions[$key] = $value;
- }{
- $value = stripslashes($value);
- $newoptions[$key] = htmlspecialchars($value, ENT_QUOTES,“UTF-8”);
- }
- }
- }
- ( $options != $newoptions ) {
- $options = $newoptions;
- update_option($this->saved_optionname, $options);
- }
- ($options) {
- ($options $key => $value) {
- $options[$key] = empty($options[$key]) ? false : $options[$key];
- }
- }
- $this->database_options = $options;
- }
- /************开头***************/
- open($values) {
- (!isset($values[‘desc’])) $values[‘desc’] = “”;
- echo ‘<table =“widefat”>’;
- echo ‘<thead><tr><th colspan=“2”>’.$values[‘desc’].’ </th></tr></thead>’;
- }
- /***************结尾**************/
- close($values) {
- echo ‘<tfoot><tr><th> </th><th> </th></tr></tfoot></table>’;
- }
- /**********标题***********************/
- title($values) {
- echo ‘<h3>’.$values[‘name’].'</h3>’;
- (isset($values[‘desc’])) echo ‘<p>’.$values[‘desc’].'</p>’;
- }
- /*****************************文本域**********************************/
- textarea($values) {
- (isset($this->database_options[$values[‘id’]]))
- $values[‘std’] = $this->database_options[$values[‘id’]];
- echo ‘<tr valign=“top” >’;
- echo ‘<th scope=“row” width=“200px”>’.$values[‘name’].'</th>’;
- echo ‘<td>’.$values[‘desc’].'<br/>’;
- echo ‘<textarea name=“‘.$values[‘id’].'” cols=“60” rows=“7” id=“‘.$values[‘id’].'” style=“width: 80%; font-size: 12px;” =“code”>’;
- echo $values[‘std’].'</textarea><br/>’;
- echo ‘<br/></td>’;
- echo ‘</tr>’;
- }
- /*********************文本框**************************/
- text($values) {
- (isset($this->database_options[$values[‘id’]])) $values[‘std’] = $this->database_options[$values[‘id’]];
- echo ‘<tr valign=“top” >’;
- echo ‘<th scope=“row” width=“200px”>’.$values[‘name’].'</th>’;
- echo ‘<td>’.$values[‘desc’].'<br/>’;
- echo ‘<input type=“text” size=“‘.$values[‘size’].'” value=“‘.$values[‘std’].'” id=“‘.$values[‘id’].'” name=“‘.$values[‘id’].'”/>’;
- echo ‘<br/><br/></td>’;
- echo ‘</tr>’;
- }
- /**************复选框*******************/
- checkbox($values) {
- (isset($this->database_options[$values[‘id’]])) $values[‘std’] = $this->database_options[$values[‘id’]];
- echo ‘<tr valign=“top”>’;
- echo ‘<th scope=“row” width=“200px”>’.$values[‘name’].'</th>’;
- echo ‘<td>’.$values[‘desc’].'<br/>’;
- ( $values[‘buttons’] $key=>$value ) {
- $checked =“”;
- ( is_array($values[‘std’]) && in_array($key,$values[‘std’])) {
- $checked = ‘checked = “checked”‘;
- }
- echo ‘<input ‘.$checked.’ type=“checkbox” =“kcheck” value=“‘.$key.'” name=“‘.$values[‘id’].'[]”/>’.$value;
- }
- echo ‘<label =“‘.$values[‘id’].'”>’.$values[‘desc’].'</label><br/>’;
- echo ‘<br/></td>’;
- echo ‘</tr>’;
- }
- /**********************单选框******************************/
- radio($values) {
- (isset($this->database_options[$values[‘id’]])) $values[‘std’] = $this->database_options[$values[‘id’]];
- echo ‘<tr valign=“top” >’;
- echo ‘<th scope=“row” width=“200px”>’.$values[‘name’].'</th>’;
- echo ‘<td>’.$values[‘desc’].'<br/>’;
- ($values[‘buttons’] $key=>$value) {
- $checked =“”;
- (isset($values[‘std’]) && ($values[‘std’] == $key)) {
- $checked = ‘checked = “checked”‘;
- }
- echo ‘<p><input ‘.$checked.’ type=“radio” =“kcheck” value=“‘.$key.'” name=“‘.$values[‘id’].'”/>’;
- echo ‘<label =“‘.$values[‘id’].'”>’.$value.'</label></p>’;
- }
- echo ‘<br/></td>’;
- echo ‘</tr>’;
- }
- /*****************数组***********************/
- numbers_array($values){
- (isset($this->database_options[$values[‘id’]]))
- $values[‘std’] = $this->database_options[$values[‘id’]];
- $values[‘std’]=();
- $nums = implode( ‘,’, $values[‘std’] );
- echo ‘<tr valign=“top” >’;
- echo ‘<th scope=“row” width=“200px”>’.$values[‘name’].'</th>’;
- echo ‘<td>’.$values[‘desc’].'<br/>’;
- echo ‘<input type=“text” size=“‘.$values[‘size’].'” value=“‘.$nums.'” id=“‘.$values[‘id’].'” name=“‘.$values[‘id’].'”/>’;
- echo ‘<br/><br/></td>’;
- echo ‘</tr>’;
- }
- /********************下拉框*********************/
- dropdown($values) {
- (!isset($this->database_options[$values[‘id’]]) && isset($values[‘std’])) $this->database_options[$values[‘id’]] = $values[‘std’];
- echo ‘<tr valign=“top” >’;
- echo ‘<th scope=“row” width=“200px”>’.$values[‘name’].'</th>’;
- echo ‘<td>’.$values[‘desc’].'<br/>’;
- ($values[‘subtype’] == ‘page’) {
- $select = ‘Select page’;
- $entries = get_pages(‘title_li=&orderby=name’);
- } ($values[‘subtype’] == ‘sidebar’){
- $wp_registered_sidebars;
- $select = ‘Select a special sidebar’;
- $entries = $wp_registered_sidebars;
- } ($values[‘subtype’] == ‘cat’){
- $select = ‘Select category’;
- $entries = get_categories(‘title_li=&orderby=name&hide_empty=0’);
- }
- {
- $select = ‘Select…’;
- $entries = $values[‘subtype’];
- }
- echo ‘<select =“postform” id=“‘. $values[‘id’] .'” name=“‘. $values[‘id’] .'”> ‘;
- echo ‘<option value=“”>’.$select .'</option> ‘;
- ($entries $key => $entry) {
- ($values[‘subtype’] == ‘page’)
- {
- $id = $entry->ID;
- $title = $entry->post_title;
- } ($values[‘subtype’] == ‘cat’){
- $id = $entry->term_id;
- $title = $entry->name;
- } ($values[‘subtype’] == ‘sidebar’){
- $id = $entry[‘id’];
- $title = $entry[‘name’];
- }
- {
- $id = $key;
- $title = $entry;
- }
- ($this->database_options[$values[‘id’]] == $id )
- {
- $selected = “selected=’selected'”;
- }
- {
- $selected = “”;
- }
- echo“<option $selected value='”. $id.“‘>”. $title.“</option>”;
- }
- echo ‘</select>’;
- echo ‘<br/><br/></td>’;
- echo ‘</tr>’;
- }
- /*******************上传*****************************/
- upload($values) {
- $prevImg = ”;
- (isset($this->database_options[$values[‘id’]])) $values[‘std’] = $this->database_options[$values[‘id’]];
- ($values[‘std’] != ”){$prevImg = ‘<img src=’.$values[‘std’].’ alt=“” />’;}
- echo ‘<tr valign=“top” >’;
- echo ‘<th scope=“row” width=“200px”>’.$values[‘name’].'</th>’;
- echo ‘<td>’;
- echo ‘<div =“preview_pic_optionspage” id=“‘.$values[‘id’].’_div”>’.$prevImg.'</div>’;
- echo $values[‘desc’].'<br/>’;
- echo ‘<input type=“text” size=“60” value=“‘.$values[‘std’].'” name=“‘.$values[‘id’].'” =“upload_pic_input” />’;
- echo ‘ <a onclick=“return false;” title=“” =“k_hijack button thickbox” id=“‘.$values[‘id’].'” href=“media-upload.php?type=image&hijack_target=’.$values[‘id’].’&TB_iframe=true”>Insert Image</a>’;
- echo ‘<br/><br/></td>’;
- echo ‘</tr>’;
- }
- //编辑器
- tinymce($values){
- (isset($this->database_options[$values[‘id’]]))
- $values[‘std’] = $this->database_options[$values[‘id’]];
- echo ‘<tr valign=“top” >’;
- echo ‘<th scope=“row” width=“200px”>’.$values[‘name’].'</th>’;
- echo ‘<td>’.$values[‘desc’].'<br/>’;
- wp_editor( $values[‘std’], $values[‘id’],$settings=(‘tinymce’=>1,’media_buttons’=>0,) );
- echo ‘<br/></td>’;
- echo ‘</tr>’;
- }
- }
- ?>
注意类中还加载了一个自定义的js文件:ashu_options.js 这个js文件中的代码为:
- jQuery.noConflict();
- jQuery(document).ready(function()
- {
- ashu_media_uploader();
- ashu_preview_pic();
- });
- function ashu_preview_pic()
- {
- jQuery(‘.upload_pic_input’).each(function()
- {
- jQuery().bind(‘change focus blur ktrigger’, function()
- {
- $select = ‘#’ + jQuery().attr(‘name’) + ‘_div’;
- $value = jQuery().val();
- $image = ‘<img src =“‘+$value+'” />’;
- var $image = jQuery($select).html(”).append($image).find(‘img’);
- //set timeout because of safari
- window.setTimeout(function()
- {
- (parseInt($image.attr(‘width’)) < 20)
- {
- jQuery($select).html(”);
- }
- },500);
- });
- });
- }
- function ashu_media_uploader()
- {
- $buttons = jQuery(‘.k_hijack’);
- $realmediabuttons = jQuery(‘.media-buttons a’);
- window.custom_editor = ;
- // set a variable depending on what has been clicked, normal media uploader or kriesi hijacked uploader
- $buttons.click(function()
- {
- window.custom_editor = jQuery().attr(‘id’);
- });
- $realmediabuttons.click(function()
- {
- window.custom_editor = ;
- });
- window.original_send_to_editor = window.send_to_editor;
- window.send_to_editor = function(html)
- {
- (custom_editor)
- {
- $img = jQuery(html).attr(‘src’) || jQuery(html).find(‘img’).attr(‘src’) || jQuery(html).attr(‘href’);
- jQuery(‘input[name=’+custom_editor+’]’).val($img).trigger(‘ktrigger’);
- custom_editor = ;
- window.tb_remove();
- }
- {
- window.original_send_to_editor(html);
- }
- };
- }
限于篇幅,使用方法请参考下一篇文章:wp自学笔记设置类文件的使用方法
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END