WordPress.org

Make WordPress Core

Ticket #785: plugin_page_hooks.diff

File plugin_page_hooks.diff, 6.1 KB (added by morganiq, 13 years ago)
  • admin-functions.php

    RCS file: /cvsroot/cafelog/wordpress/wp-admin/admin-functions.php,v
    retrieving revision 1.78
    diff -u -r1.78 admin-functions.php
     
    773773}
    774774
    775775function add_menu_page($page_title, $menu_title, $access_level, $file) {
    776         global $menu;
     776        global $menu, $admin_page_hooks;
    777777
    778778        $file = plugin_basename($file);
    779779
    780780        $menu[] = array($menu_title, $access_level, $file, $page_title);
     781
     782        $admin_page_hooks[$file] = sanitize_title($menu_title);
    781783}
    782784
    783 function add_submenu_page($parent, $page_title, $menu_title, $access_level, $file) {
     785function add_submenu_page($parent, $page_title, $menu_title, $access_level, $file, $function = '') {
    784786        global $submenu;
    785787        global $menu;
    786788
     
    800802        }
    801803       
    802804        $submenu[$parent][] = array($menu_title, $access_level, $file, $page_title);
     805
     806        $hookname = get_plugin_page_hookname($file, $parent);
     807        if ( !empty($function) && !empty($hookname) )
     808                add_action($hookname, $function);
     809
     810        return $hookname;
    803811}
    804812
    805 function add_options_page($page_title, $menu_title, $access_level, $file) {
    806         add_submenu_page('options-general.php', $page_title, $menu_title, $access_level, $file);
     813function add_options_page($page_title, $menu_title, $access_level, $file, $function = '') {
     814        return add_submenu_page('options-general.php', $page_title, $menu_title, $access_level, $file, $function);
    807815}
    808816
    809 function add_management_page($page_title, $menu_title, $access_level, $file) {
    810         add_submenu_page('edit.php', $page_title, $menu_title, $access_level, $file);
     817function add_management_page($page_title, $menu_title, $access_level, $file, $function = '') {
     818        return add_submenu_page('edit.php', $page_title, $menu_title, $access_level, $file, $function);
    811819}
    812820
    813821function validate_file($file, $allowed_files = '') {
     
    9991007        return $wp_plugins;
    10001008}
    10011009
     1010function get_plugin_page_hookname($plugin_page, $parent_page) {
     1011        global $admin_page_hooks;
     1012
     1013        if ( isset($admin_page_hooks[$parent_page]) )
     1014                $page_type = $admin_page_hooks[$parent_page];
     1015        else
     1016                $page_type = 'admin';
     1017
     1018        $plugin_name = preg_replace('!\.php!', '', $plugin_page);
     1019
     1020        return $page_type . '_page_' . $plugin_name;
     1021}
     1022
     1023function get_plugin_page_hook($plugin_page, $parent_page) {
     1024        global $wp_filter;
     1025
     1026        $hook = get_plugin_page_hookname($plugin_page, $parent_page);
     1027
     1028        if ( isset($wp_filter[$hook]) )
     1029                return $hook;
     1030        else
     1031                return '';
     1032}
     1033
    10021034?>
  • admin.php

    RCS file: /cvsroot/cafelog/wordpress/wp-admin/admin.php,v
    retrieving revision 1.7
    diff -u -r1.7 admin.php
     
    4141// Handle plugin admin pages.
    4242if (isset($_GET['page'])) {
    4343        $plugin_page = plugin_basename($_GET['page']);
    44         if ( validate_file($plugin_page) ) {
    45                 die(__('Invalid plugin page'));
    46         }
    47 
    48         if (! file_exists(ABSPATH . "wp-content/plugins/$plugin_page"))
    49                 die(sprintf(__('Cannot load %s.'), $plugin_page));
     44        $page_hook = get_plugin_page_hook($plugin_page, $pagenow);
    5045
    51         if (! isset($_GET['noheader']))
    52                 require_once(ABSPATH . '/wp-admin/admin-header.php');
     46        if ( $page_hook ) {
     47                if (! isset($_GET['noheader']))
     48                        require_once(ABSPATH . '/wp-admin/admin-header.php');
     49               
     50                do_action($page_hook);
     51        } else {
     52                if ( validate_file($plugin_page) ) {
     53                        die(__('Invalid plugin page'));
     54                }
     55               
     56                if (! file_exists(ABSPATH . "wp-content/plugins/$plugin_page"))
     57                        die(sprintf(__('Cannot load %s.'), $plugin_page));
    5358
    54         include(ABSPATH . "wp-content/plugins/$plugin_page");
     59                if (! isset($_GET['noheader']))
     60                        require_once(ABSPATH . '/wp-admin/admin-header.php');
     61               
     62                include(ABSPATH . "wp-content/plugins/$plugin_page");
     63        }
     64       
     65        include(ABSPATH . 'wp-admin/admin-footer.php');
    5566
    56         include(ABSPATH . 'wp-admin/admin-footer.php');
     67        exit();
    5768}
    5869
    5970?>
     71 No newline at end of file
  • menu-header.php

    RCS file: /cvsroot/cafelog/wordpress/wp-admin/menu-header.php,v
    retrieving revision 1.4
    diff -u -r1.4 menu-header.php
     
    3535                 continue;
    3636         }
    3737
    38 if ( (substr($self, -10) == substr($item[2], -10)) || (isset($plugin_page) && $plugin_page == $item[2]) ) $class = ' class="current"';
     38if ( (isset($plugin_page) && $plugin_page == $item[2]) || (!isset($plugin_page) && substr($self, -10) == substr($item[2], -10)) ) $class = ' class="current"';
    3939else if (isset($submenu_file) && $submenu_file == substr($item[2], -10)) $class = ' class="current"';   
    4040else $class = '';
    4141
    42 if (file_exists(ABSPATH . "wp-content/plugins/{$item[2]}"))
    43         echo "\n\t<li><a href='" . get_settings('siteurl') . "/wp-admin/admin.php?page={$item[2]}'$class>{$item[0]}</a></li>";                 
    44  else
     42if (file_exists(ABSPATH . "wp-content/plugins/{$item[2]}")) {
     43        $page_hook = get_plugin_page_hook($item[2], $parent_file);
     44        if ( $page_hook )
     45                echo "\n\t<li><a href='" . get_settings('siteurl') . "/wp-admin/{$parent_file}?page={$item[2]}'$class>{$item[0]}</a></li>";             
     46        else
     47                echo "\n\t<li><a href='" . get_settings('siteurl') . "/wp-admin/admin.php?page={$item[2]}'$class>{$item[0]}</a></li>";
     48 } else {
    4549        echo "\n\t<li><a href='" . get_settings('siteurl') . "/wp-admin/{$item[2]}'$class>{$item[0]}</a></li>";
     50 }
    4651endforeach;
    4752?>
    4853
  • menu.php

    RCS file: /cvsroot/cafelog/wordpress/wp-admin/menu.php,v
    retrieving revision 1.39
    diff -u -r1.39 menu.php
     
    4848$submenu['themes.php'][5] = array(__('Themes'), 8, 'themes.php');
    4949$submenu['themes.php'][10] = array(__('Theme Editor'), 8, 'theme-editor.php');
    5050
     51// Create list of page plugin hook names.
     52foreach ($menu as $menu_page) {
     53        $admin_page_hooks[$menu_page[2]] = sanitize_title($menu_page[0]);
     54}
     55
    5156do_action('admin_menu', '');
    5257ksort($menu); // make it all pretty
    5358