WordPress admin page system
<?php | |
/** | |
* A WordPress admin page rendered using a PHP template. | |
*/ | |
abstract class MyPlugin_AbstractRenderedAdminPage implements MyPlugin_AdminPageInterface | |
{ | |
/** | |
* Path to the admin page's templates. | |
* | |
* @var string | |
*/ | |
protected $template_path; | |
/** | |
* Constructor. | |
* | |
* @param string $template_path | |
*/ | |
public function __construct($template_path) | |
{ | |
$this->template_path = rtrim($template_path, '/'); | |
} | |
/** | |
* Render the admin page. | |
*/ | |
public function render_page() | |
{ | |
$this->render_template('page'); | |
} | |
/** | |
* Renders the given template if it's readable. | |
* | |
* @param string $template | |
*/ | |
protected function render_template($template) | |
{ | |
$template_path = $this->template_path . '/' . $template . '.php'; | |
if (!is_readable($template_path)) { | |
return; | |
} | |
include $template_path; | |
} | |
} |
<?php | |
/** | |
* A WordPress admin page. | |
*/ | |
interface MyPlugin_AdminPageInterface | |
{ | |
/** | |
* Get the capability required to view the admin page. | |
* | |
* @return string | |
*/ | |
public function get_capability(); | |
/** | |
* Get the title of the admin page in the WordPress admin menu. | |
* | |
* @return string | |
*/ | |
public function get_menu_title(); | |
/** | |
* Get the title of the admin page. | |
* | |
* @return string | |
*/ | |
public function get_page_title(); | |
/** | |
* Get the parent slug of the admin page. | |
* | |
* @return string | |
*/ | |
public function get_parent_slug(); | |
/** | |
* Get the slug used by the admin page. | |
* | |
* @return string | |
*/ | |
public function get_slug(); | |
/** | |
* Renders the admin page. | |
*/ | |
public function render_page(); | |
} |
<?php | |
/** | |
* Subscriber that registers the plugin's admin pages with WordPress. | |
*/ | |
class MyPlugin_AdminPagesSubscriber implements MyPlugin_SubscriberInterface | |
{ | |
/** | |
* The admin pages that the subscriber manages. | |
* | |
* @var MyPlugin_AdminPageInterface[] | |
*/ | |
private $admin_pages; | |
/** | |
* Constructor. | |
* | |
* @param MyPlugin_AdminPageInterface[] $admin_pages | |
*/ | |
public function __construct(array $admin_pages) | |
{ | |
$this->admin_pages = array(); | |
foreach ($admin_pages as $admin_page) { | |
$this->add_admin_page($admin_page); | |
} | |
} | |
/** | |
* Returns an array of events that this subscriber wants to listen to. | |
* | |
* The array key is the event name. The value can be: | |
* | |
* * The method name | |
* * An array with the method name and priority | |
* * An array with the method name, priority and number of accepted arguments | |
* | |
* For instance: | |
* | |
* * array('event_name' => 'method_name') | |
* * array('event_name' => array('method_name', $priority)) | |
* * array('event_name' => array('method_name', $priority, $accepted_args)) | |
* | |
* @return array | |
*/ | |
public static function get_subscribed_events() | |
{ | |
return array( | |
'admin_init' => 'configure_admin_pages', | |
'admin_menu' => 'add_admin_pages', | |
); | |
} | |
/** | |
* Adds the plugin's admin pages to the WordPress admin. | |
*/ | |
public function add_admin_pages() | |
{ | |
foreach ($this->admin_pages as $admin_page) { | |
add_submenu_page( | |
$admin_page->get_parent_slug(), | |
$admin_page->get_page_title(), | |
$admin_page->get_menu_title(), | |
$admin_page->get_capability(), | |
$admin_page->get_slug(), | |
array($admin_page, 'render_page') | |
); | |
} | |
} | |
/** | |
* Configure the plugin's admin pages using the Settings API. | |
*/ | |
public function configure_admin_pages() | |
{ | |
foreach ($this->admin_pages as $admin_page) { | |
if ($admin_page instanceof MyPlugin_ConfigurableAdminPageInterface) { | |
$admin_page->configure(); | |
} | |
} | |
} | |
/** | |
* Add a new admin page to the subscriber. | |
* | |
* @param MyPlugin_AdminPageInterface $admin_page | |
*/ | |
private function add_admin_page(MyPlugin_AdminPageInterface $admin_page) | |
{ | |
$this->admin_pages[] = $admin_page; | |
} | |
} |
<?php | |
/** | |
* A WordPress admin page configured using the settings API. | |
*/ | |
interface MyPlugin_ConfigurableAdminPageInterface extends MyPlugin_AdminPageInterface | |
{ | |
/** | |
* Configure the admin page using the Settings API. | |
*/ | |
public function configure(); | |
} |
<?php | |
function myplugin_get_admin_pages() { | |
return array( | |
// Put admin page objects here | |
); | |
} | |
function myplugin_add_admin_pages() { | |
$admin_pages = myplugin_get_admin_pages(); | |
foreach ($admin_pages as $admin_page) { | |
add_submenu_page( | |
$admin_page->get_parent_slug(), | |
$admin_page->get_page_title(), | |
$admin_page->get_menu_title(), | |
$admin_page->get_capability(), | |
$admin_page->get_slug(), | |
array($admin_page, 'render_page') | |
); | |
} | |
} | |
add_action('admin_menu', 'myplugin_add_admin_pages'); | |
function myplugin_configure_admin_pages() { | |
$admin_pages = myplugin_get_admin_pages(); | |
foreach ($admin_pages as $admin_page) { | |
if ($admin_page instanceof MyPlugin_ConfigurableAdminPageInterface) { | |
$admin_page->configure(); | |
} | |
} | |
} | |
add_action('admin_init', 'myplugin_configure_admin_pages'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment