Plugin page hooks. Props: morganiq. http://mosquito.wordpress.org/view.php?id=785 http://mosquito.wordpress.org/view.php?id=797
git-svn-id: http://svn.automattic.com/wordpress/trunk@2234 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
20ef36ab0f
commit
3ec8c50289
|
@ -773,14 +773,16 @@ function plugin_basename($file) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function add_menu_page($page_title, $menu_title, $access_level, $file) {
|
function add_menu_page($page_title, $menu_title, $access_level, $file) {
|
||||||
global $menu;
|
global $menu, $admin_page_hooks;
|
||||||
|
|
||||||
$file = plugin_basename($file);
|
$file = plugin_basename($file);
|
||||||
|
|
||||||
$menu[] = array($menu_title, $access_level, $file, $page_title);
|
$menu[] = array($menu_title, $access_level, $file, $page_title);
|
||||||
|
|
||||||
|
$admin_page_hooks[$file] = sanitize_title($menu_title);
|
||||||
}
|
}
|
||||||
|
|
||||||
function add_submenu_page($parent, $page_title, $menu_title, $access_level, $file) {
|
function add_submenu_page($parent, $page_title, $menu_title, $access_level, $file, $function = '') {
|
||||||
global $submenu;
|
global $submenu;
|
||||||
global $menu;
|
global $menu;
|
||||||
|
|
||||||
|
@ -800,14 +802,20 @@ function add_submenu_page($parent, $page_title, $menu_title, $access_level, $fil
|
||||||
}
|
}
|
||||||
|
|
||||||
$submenu[$parent][] = array($menu_title, $access_level, $file, $page_title);
|
$submenu[$parent][] = array($menu_title, $access_level, $file, $page_title);
|
||||||
|
|
||||||
|
$hookname = get_plugin_page_hookname($file, $parent);
|
||||||
|
if ( !empty($function) && !empty($hookname) )
|
||||||
|
add_action($hookname, $function);
|
||||||
|
|
||||||
|
return $hookname;
|
||||||
}
|
}
|
||||||
|
|
||||||
function add_options_page($page_title, $menu_title, $access_level, $file) {
|
function add_options_page($page_title, $menu_title, $access_level, $file, $function = '') {
|
||||||
add_submenu_page('options-general.php', $page_title, $menu_title, $access_level, $file);
|
return add_submenu_page('options-general.php', $page_title, $menu_title, $access_level, $file, $function);
|
||||||
}
|
}
|
||||||
|
|
||||||
function add_management_page($page_title, $menu_title, $access_level, $file) {
|
function add_management_page($page_title, $menu_title, $access_level, $file, $function = '') {
|
||||||
add_submenu_page('edit.php', $page_title, $menu_title, $access_level, $file);
|
return add_submenu_page('edit.php', $page_title, $menu_title, $access_level, $file, $function);
|
||||||
}
|
}
|
||||||
|
|
||||||
function validate_file($file, $allowed_files = '') {
|
function validate_file($file, $allowed_files = '') {
|
||||||
|
@ -999,4 +1007,28 @@ function get_plugins() {
|
||||||
return $wp_plugins;
|
return $wp_plugins;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function get_plugin_page_hookname($plugin_page, $parent_page) {
|
||||||
|
global $admin_page_hooks;
|
||||||
|
|
||||||
|
if ( isset($admin_page_hooks[$parent_page]) )
|
||||||
|
$page_type = $admin_page_hooks[$parent_page];
|
||||||
|
else
|
||||||
|
$page_type = 'admin';
|
||||||
|
|
||||||
|
$plugin_name = preg_replace('!\.php!', '', $plugin_page);
|
||||||
|
|
||||||
|
return $page_type . '_page_' . $plugin_name;
|
||||||
|
}
|
||||||
|
|
||||||
|
function get_plugin_page_hook($plugin_page, $parent_page) {
|
||||||
|
global $wp_filter;
|
||||||
|
|
||||||
|
$hook = get_plugin_page_hookname($plugin_page, $parent_page);
|
||||||
|
|
||||||
|
if ( isset($wp_filter[$hook]) )
|
||||||
|
return $hook;
|
||||||
|
else
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
?>
|
?>
|
||||||
|
|
|
@ -41,19 +41,30 @@ require(ABSPATH . '/wp-admin/menu.php');
|
||||||
// Handle plugin admin pages.
|
// Handle plugin admin pages.
|
||||||
if (isset($_GET['page'])) {
|
if (isset($_GET['page'])) {
|
||||||
$plugin_page = plugin_basename($_GET['page']);
|
$plugin_page = plugin_basename($_GET['page']);
|
||||||
if ( validate_file($plugin_page) ) {
|
$page_hook = get_plugin_page_hook($plugin_page, $pagenow);
|
||||||
die(__('Invalid plugin page'));
|
|
||||||
|
if ( $page_hook ) {
|
||||||
|
if (! isset($_GET['noheader']))
|
||||||
|
require_once(ABSPATH . '/wp-admin/admin-header.php');
|
||||||
|
|
||||||
|
do_action($page_hook);
|
||||||
|
} else {
|
||||||
|
if ( validate_file($plugin_page) ) {
|
||||||
|
die(__('Invalid plugin page'));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! file_exists(ABSPATH . "wp-content/plugins/$plugin_page"))
|
||||||
|
die(sprintf(__('Cannot load %s.'), $plugin_page));
|
||||||
|
|
||||||
|
if (! isset($_GET['noheader']))
|
||||||
|
require_once(ABSPATH . '/wp-admin/admin-header.php');
|
||||||
|
|
||||||
|
include(ABSPATH . "wp-content/plugins/$plugin_page");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
include(ABSPATH . 'wp-admin/admin-footer.php');
|
||||||
|
|
||||||
if (! file_exists(ABSPATH . "wp-content/plugins/$plugin_page"))
|
exit();
|
||||||
die(sprintf(__('Cannot load %s.'), $plugin_page));
|
|
||||||
|
|
||||||
if (! isset($_GET['noheader']))
|
|
||||||
require_once(ABSPATH . '/wp-admin/admin-header.php');
|
|
||||||
|
|
||||||
include(ABSPATH . "wp-content/plugins/$plugin_page");
|
|
||||||
|
|
||||||
include(ABSPATH . 'wp-admin/admin-footer.php');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
?>
|
?>
|
|
@ -35,14 +35,19 @@ foreach ($submenu["$parent_file"] as $item) :
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( (substr($self, -10) == substr($item[2], -10)) || (isset($plugin_page) && $plugin_page == $item[2]) ) $class = ' class="current"';
|
if ( (isset($plugin_page) && $plugin_page == $item[2]) || (!isset($plugin_page) && substr($self, -10) == substr($item[2], -10)) ) $class = ' class="current"';
|
||||||
else if (isset($submenu_file) && $submenu_file == substr($item[2], -10)) $class = ' class="current"';
|
else if (isset($submenu_file) && $submenu_file == substr($item[2], -10)) $class = ' class="current"';
|
||||||
else $class = '';
|
else $class = '';
|
||||||
|
|
||||||
if (file_exists(ABSPATH . "wp-content/plugins/{$item[2]}"))
|
if (file_exists(ABSPATH . "wp-content/plugins/{$item[2]}")) {
|
||||||
echo "\n\t<li><a href='" . get_settings('siteurl') . "/wp-admin/admin.php?page={$item[2]}'$class>{$item[0]}</a></li>";
|
$page_hook = get_plugin_page_hook($item[2], $parent_file);
|
||||||
else
|
if ( $page_hook )
|
||||||
|
echo "\n\t<li><a href='" . get_settings('siteurl') . "/wp-admin/{$parent_file}?page={$item[2]}'$class>{$item[0]}</a></li>";
|
||||||
|
else
|
||||||
|
echo "\n\t<li><a href='" . get_settings('siteurl') . "/wp-admin/admin.php?page={$item[2]}'$class>{$item[0]}</a></li>";
|
||||||
|
} else {
|
||||||
echo "\n\t<li><a href='" . get_settings('siteurl') . "/wp-admin/{$item[2]}'$class>{$item[0]}</a></li>";
|
echo "\n\t<li><a href='" . get_settings('siteurl') . "/wp-admin/{$item[2]}'$class>{$item[0]}</a></li>";
|
||||||
|
}
|
||||||
endforeach;
|
endforeach;
|
||||||
?>
|
?>
|
||||||
|
|
||||||
|
|
|
@ -48,6 +48,11 @@ $submenu['plugins.php'][10] = array(__('Plugin Editor'), 8, 'plugin-editor.php')
|
||||||
$submenu['themes.php'][5] = array(__('Themes'), 8, 'themes.php');
|
$submenu['themes.php'][5] = array(__('Themes'), 8, 'themes.php');
|
||||||
$submenu['themes.php'][10] = array(__('Theme Editor'), 8, 'theme-editor.php');
|
$submenu['themes.php'][10] = array(__('Theme Editor'), 8, 'theme-editor.php');
|
||||||
|
|
||||||
|
// Create list of page plugin hook names.
|
||||||
|
foreach ($menu as $menu_page) {
|
||||||
|
$admin_page_hooks[$menu_page[2]] = sanitize_title($menu_page[0]);
|
||||||
|
}
|
||||||
|
|
||||||
do_action('admin_menu', '');
|
do_action('admin_menu', '');
|
||||||
ksort($menu); // make it all pretty
|
ksort($menu); // make it all pretty
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue