构建某种注册表,其他插件可以在其中注册新选项卡。假设插件的管理页面处理程序被调用Main_Controller
:
class Main_Controller {
protected $tabs;
public function __construct()
{
$this->tabs = new Tab_List;
do_action( \'register_tabs\', $this->tabs );
}
public function create_tabs() {
$all_tabs = $this->tabs->get_tabs();
if ( empty ( $all_tabs ) )
return;
foreach ( $all_tabs as $id => $tab ) {
// print the tab
}
}
}
Tab_List
是包含所有已注册选项卡的列表:
class Tab_List {
protected $tabs = array();
public function register( $id, Tab $tab ) {
$this->tabs[ $id ] = $tab;
}
public function get_tabs() {
return $this->tabs;
}
}
方法
register
需要一个定义良好的类实例
Tab
:
class Tab {
// Have to be replaced.
protected $properties = array(
\'tab_title\' => \'MISSING TAB TITLE\',
\'page_title\' => \'MISSING PAGE TITLE\',
\'content_callback\' => \'__return_false\',
\'save_callback\' => \'__return_false\'
);
public function __set( $name, $value ) {
if ( isset ( $this->properties[ $name ] ) )
$this->properties[ $name ] = $value;
}
public function __get( $name ) {
if ( isset ( $this->properties[ $name ] ) )
return $this->properties[ $name ];
}
}
现在,其他插件可以用一个简单的挂钩注册它们的标签:
add_action( \'register_tabs\', function( Tab_List $tab_list )
{
$data = new My_Color_Data;
$view = new My_Color_View( $data );
$my_tab = new Tab;
$my_tab->tab_title = \'Colors\';
$my_tab->page_title = \'Set custom colors\';
$my_tab->content_callback = array( $view, \'print_tab\' );
$my_tab->save_callback = array( $data, \'save_tab\' );
$tab_list->register( \'colors\', $my_tab );
});
课程
My_Color_Data
和
My_Color_View
是自定义的。这要看你的想象了。:)
另请参见: