我在wordpress部分交叉发布这个,b / c我认为问题更多的是我对OOP缺乏了解,所以也许它应该首先发布在这里。
在编写wordpress插件时,我想创建一个类,我可以在每个插件中使用它来处理插件选项页面的平凡创建:所以添加菜单页面,白名单设置,吐出和验证选项等。基本上我想,我想重复使用我必须写的课程。
在最基础上,我期待我可以在插件一和二(使用不同的$ args)中说新的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 );
这两个插件都包含在各自的类中。我可以发布其中一个假人,如果这会有所帮助,但它会变成很多代码。
答案 0 :(得分:1)
根据自己的经验,我可以说可以用PHP共享基类本身,但请记住,Wordpress本身不是面向对象的。
因此,当您注册钩子,页面,回调等时,请注意您和wordpress本身可以轻松区分。
例如,在插件的基类中,您使用__FILE__
来注册挂钩。在所有情况下,该值都相同,因此无用。对于某些钩子,您还需要一些其他值。所以也许先从一些更轻量级的东西开始,例如没有激活/卸载例程并使其工作。管理员只需两页,他们的菜单条目等。一步一步。
然后看看Breadcrumb NavXT插件。它做了类似的事情,它有一个AdminKit基类,具体选项页面扩展自。我个人不会说它很完美,但这对你来说可能很有趣:
此外,我建议您不仅要将所有东西都拧成基类,而且还要在需要时更多地创建对象,以使事物更加分散和灵活:
希望这有助于让事情恢复正常。