我从wordpress部分交叉发布这个,b / c我认为问题更多在于我对OOP缺乏了解,所以也许它应该首先发布在这里。
在编写 wordpress 插件时,我想创建一个类,我可以在每个插件中使用它来处理插件选项页面的平凡创建:所以添加菜单页面、白名单设置、吐出和验证选项等。我认为基本上,我想重用一些我必须写一个类的东西。
在最基本的情况下,我希望我可以在插件一和插件二(使用不同的 $args)中说 new KIA_Plugin_Options($args),并且每个插件都会为各自的插件创建选项页面。但是,我在 2 个插件中添加了以下类,而不是为每个插件创建菜单,它只处理 1 个然后死掉。所以它的一部分工作,但不是我想要的可重复使用的灵丹妙药,所以我觉得我错过了 OOP 中的一些重要概念。
这是我的选项类的骨架:
<?php
class KIA_Plugin_Options {
private $page_title;
private $menu_title;
private $capability;
private $menu_slug;
private $setting_name;
private $option_name;
function KIA_Plugin_Options( $args ){
//convert $args to array
if(!is_array($args)) $args['page_title'] = $args;
if(!isset($args['page_title'])) die (__('Plugin Options needs at least the plugin\'s page title parameter.','kia_plugin_options'));
// Define the class variables
$this->page_title = $args['page_title'];
$this->menu_title = isset($args['menu_title']) ? $args['menu_title'] : $this->page_title;
$this->capability = isset($args['capability']) ? $args['capability'] : 'manage_options';
$this->menu_slug = isset($args['menu_slug']) ? $args['menu_slug'] : sanitize_title_with_dashes($this->menu_title);
$this->setting_name = isset($args['setting_name']) ? $args['setting_name'] : $this->menu_slug;
$this->option_name = isset($args['option_name']) ? $args['option_name'] : $this->menu_slug;
// Set-up Action and Filter Hooks
register_activation_hook(__FILE__, array(&$this,'add_defaults_options'));
register_uninstall_hook(__FILE__, array(&$this,'delete_plugin_options'));
//add settings page
add_action('admin_init', array(&$this,'register_settings' ));
add_action('admin_menu', array(&$this,'add_options_page'));
//add settings link to plugins page
add_filter( 'plugin_action_links', array(&$this,'add_action_links'), 10, 2 );
}
// ------------------------------------------------------------------------------
// CALLBACK FUNCTION FOR: register_activation_hook(__FILE__, 'add_defaults_options')
// ------------------------------------------------------------------------------
// Define default option settings
function add_defaults_options() {
if(get_option('kia_tell_a_friend_options')) return false;
$defaults = array(
"emailsubject" => __( 'I saw this and thought of you!', 'kia_tell_a_friend'),
"emailmsg" => __( 'I just saw this!', 'kia_tell_a_friend'),
"twittermsg" => __( 'Check this out!', 'kia_tell_a_friend')
);
update_option('kia_tell_a_friend_options', $defaults);
}
// --------------------------------------------------------------------------------------
// CALLBACK FUNCTION FOR: register_uninstall_hook(__FILE__, 'delete_plugin_options')
// --------------------------------------------------------------------------------------
// Delete options table entries ONLY when plugin deactivated AND deleted
function delete_plugin_options() {
$options = get_option('kia_tell_a_friend_options', true);
if(isset($options['delete'])) delete_option('kia_tell_a_friend_options');
}
// ------------------------------------------------------------------------------
// CALLBACK FUNCTION FOR: add_action('admin_init', array(&$this,'register_settings' ));
// ------------------------------------------------------------------------------
// Init plugin options to white list our options
function register_settings(){
register_setting( $this->setting_name, $this->option_name, array(&$this,'validate_options') );
}
// ------------------------------------------------------------------------------
// CALLBACK FUNCTION FOR: add_action('admin_menu', array(&$this,'add_options_page'));
// ------------------------------------------------------------------------------
// Add menu page
function add_options_page() {
$page = add_options_page( $this->page_title , $this->menu_title, $this->capability, $this->menu_slug, array(&$this,'render_form'));
/* Using registered $page handle to hook stylesheet loading */
add_action( 'admin_print_styles-' . $page, array(&$this,'admin_style' ));
}
// ------------------------------------------------------------------------------
// CALLBACK FUNCTION FOR: add_action( 'admin_print_styles-' . $page, array(&$this,'admin_styles' ));
// ------------------------------------------------------------------------------
// Add menu page styles
function admin_style() {
wp_enqueue_style('plugin-options',plugins_url('css/options-framework.css', __FILE__));
}
// ------------------------------------------------------------------------------
// CALLBACK FUNCTION SPECIFIED IN: add_options_page()
// ------------------------------------------------------------------------------
// Render the Plugin options form
function render_form() {
echo "Bacon!!!";
}
// Sanitize and validate input. Accepts an array, return a sanitized array.
function validate_options($input) {
$clean = array();
// strip html from textboxes
$clean['pubid'] = wp_filter_nohtml_kses($input['pubid']); // Sanitize text input (strip html tags, and escape characters)
$clean['delete'] = isset( $input['delete'] ) ? 'true' : 'false' ; //checkbox
$clean['emailsubject'] = wp_filter_nohtml_kses($input['emailsubject']); // Sanitize text input (strip html tags, and escape characters)
$clean['emailmsg'] = wp_filter_post_kses($input['emailmsg']); // Sanitize textbox input (allow tags that area allowed in posts)
$clean['twittermsg'] = wp_filter_nohtml_kses($input['twittermsg']); // Sanitize text input (strip html tags, and escape characters)
return $clean;
}
// Display a Settings link on the main Plugins page
function add_action_links( $links, $file ) {
if ( $file == plugin_basename( __FILE__ ) ) {
$posk_links = '<a href="'.admin_url('options-general.php?page=tell-a-friend').'">'.__('Settings', 'kia_tell_a_friend').'</a>';
// make the 'Settings' link appear first
array_unshift( $links, $posk_links );
}
return $links;
}
}
然后在我的第一个插件中调用(在 init 钩子上)
$args = array ( 'page_title' => 'apple options page',
'menu_title' => 'apple options',
'capability' => 'manage_options',
'menu_slug' => 'apple_options',
'setting_name' => 'apple_settings',
'option_name' => 'apple_db_options' );
$this->options = new KIA_Plugin_Options( $args );
在第二个插件上
$args = array ( 'page_title' => 'bacon options page',
'menu_title' => 'bacon options',
'capability' => 'manage_options',
'menu_slug' => 'bacon_options',
'setting_name' => 'bacon_settings',
'option_name' => 'bacon_db_options' );
$this->options = new KIA_Plugin_Options( $args );
这两个插件都包含在各自的类中。如果有帮助的话,我可以发布其中一个假人,但它会变成很多代码。