Make WordPress Core

Ticket #37526: 37526.3.diff

File 37526.3.diff, 20.4 KB (added by flixos90, 9 years ago)
  • src/wp-admin/admin-header.php

     
    3232get_admin_page_title();
    3333$title = esc_html( strip_tags( $title ) );
    3434
    35 if ( is_network_admin() )
    36         $admin_title = sprintf( __( 'Network Admin: %s' ), esc_html( get_current_site()->site_name ) );
    37 elseif ( is_user_admin() )
    38         $admin_title = sprintf( __( 'User Dashboard: %s' ), esc_html( get_current_site()->site_name ) );
    39 else
    40         $admin_title = get_bloginfo( 'name' );
     35$admin_title = get_current_administration_panel( '', true )->admin_title;
    4136
    4237if ( $admin_title == $title )
    4338        $admin_title = sprintf( __( '%1$s — WordPress' ), $title );
     
    226221
    227222$current_screen->render_screen_meta();
    228223
    229 if ( is_network_admin() ) {
     224$_panel = get_current_administration_panel( '', true );
     225
     226if ( 'site' !== $_panel->name ) {
    230227        /**
    231          * Prints network admin screen notices.
     228         * Prints admin screen notices for a specific Admin.
    232229         *
    233          * @since 3.1.0
    234          */
    235         do_action( 'network_admin_notices' );
    236 } elseif ( is_user_admin() ) {
    237         /**
    238          * Prints user admin screen notices.
     230         * The dynamic portion of the hook name, `$panel_name`, refers to the Administration Panel name.
    239231         *
    240232         * @since 3.1.0
     233         * @since 4.7.0 Hook is now dynamic.
    241234         */
    242         do_action( 'user_admin_notices' );
     235        do_action( $_panel->name . '_admin_notices' );
    243236} else {
    244237        /**
    245238         * Prints admin screen notices.
     
    249242        do_action( 'admin_notices' );
    250243}
    251244
     245unset( $_panel );
     246
    252247/**
    253248 * Prints generic admin screen notices.
    254249 *
  • src/wp-admin/admin.php

     
    8383
    8484auth_redirect();
    8585
     86// Register the additional administration panels.
     87$site_name = function_exists( 'get_network' ) ? get_network()->site_name : get_bloginfo( 'name' );
     88
     89register_administration_panel( 'network', array(
     90        'constant_name'  => 'WP_NETWORK_ADMIN',
     91        'check_callback' => 'is_network_admin',
     92        'url_callback'   => 'network_admin_url',
     93        'menu_file'      => ABSPATH . 'wp-admin/network/menu.php',
     94        'admin_title'    => sprintf( __( 'Network Admin: %s' ), esc_html( $site_name ) ),
     95) );
     96
     97register_administration_panel( 'user', array(
     98        'constant_name'  => 'WP_USER_ADMIN',
     99        'check_callback' => 'is_user_admin',
     100        'url_callback'   => 'user_admin_url',
     101        'menu_file'      => ABSPATH . 'wp-admin/user/menu.php',
     102        'admin_title'    => sprintf( __( 'User Dashboard: %s' ), esc_html( $site_name ) ),
     103) );
     104
     105unset( $site_name );
     106
     107/**
     108 * Fires before an admin screen or script is being initialized.
     109 *
     110 * Note, this does not just run on user-facing admin screens.
     111 * It runs on admin-ajax.php and admin-post.php as well.
     112 *
     113 * @since 4.7.0
     114 */
     115do_action( 'admin_setup' );
     116
    86117// Schedule trash collection
    87118if ( ! wp_next_scheduled( 'wp_scheduled_delete' ) && ! wp_installing() )
    88119        wp_schedule_event(time(), 'daily', 'wp_scheduled_delete');
     
    130161else
    131162        $taxnow = '';
    132163
    133 if ( WP_NETWORK_ADMIN )
    134         require(ABSPATH . 'wp-admin/network/menu.php');
    135 elseif ( WP_USER_ADMIN )
    136         require(ABSPATH . 'wp-admin/user/menu.php');
    137 else
    138         require(ABSPATH . 'wp-admin/menu.php');
     164require( get_current_administration_panel()->menu_file );
    139165
    140166if ( current_user_can( 'manage_options' ) ) {
    141167        wp_raise_memory_limit( 'admin' );
  • src/wp-admin/includes/admin.php

     
    4949/** WordPress Post Administration API */
    5050require_once(ABSPATH . 'wp-admin/includes/post.php');
    5151
     52/** WordPress Administration Panel API */
     53require_once(ABSPATH . 'wp-admin/includes/class-wp-administration-panel.php');
     54require_once(ABSPATH . 'wp-admin/includes/administration-panel.php');
     55
    5256/** WordPress Administration Screen API */
    5357require_once(ABSPATH . 'wp-admin/includes/class-wp-screen.php');
    5458require_once(ABSPATH . 'wp-admin/includes/screen.php');
  • src/wp-admin/includes/administration-panel.php

     
     1<?php
     2/**
     3 * WordPress Administration Panel API.
     4 *
     5 * @package WordPress
     6 * @subpackage Administration
     7 */
     8
     9/**
     10 * Registers an administration panel.
     11 *
     12 * This function should be used on the 'admin_setup' hook.
     13 *
     14 * A function to register a custom administration panel from a plugin. WordPress also
     15 * uses it to register the 'network' and 'user' administration panels.
     16 *
     17 * @since 4.7.0
     18 *
     19 * @param string $name Name of the administration panel. Must not contain dashes.
     20 * @param array|string $args {
     21 *     Array or string of administration panel arguments.
     22 *
     23 *     @type string   $constant_name  Required. Name of the constant to check for whether we're currently
     24 *                                    in that administration panel.
     25 *     @type callable $check_callback Required. A callback function that can be used to check whether we're
     26 *                                    currently in that administration panel.
     27 *     @type callable $url_callback   Required. A callback function to return the URL to the administration
     28 *                                    panel. The function must accept two parameters, `$path` and `$scheme`
     29 *                                    and return the full URL to the administration panel for that path.
     30 *     @type string   $menu_file      Required. The filename of the PHP file to include to setup the menu
     31 *                                    for the administration panel.
     32 *     @type string   $admin_title    Optional. Title to use in the title tag.
     33 * }
     34 * @return WP_Administration_Panel|WP_Error The administration panel object on success, an error object otherwise.
     35 */
     36function register_administration_panel( $name, $args ) {
     37        return WP_Administration_Panel::register( $name, $args );
     38}
     39
     40/**
     41 * Returns the administration panel object for a given name.
     42 *
     43 * @since 4.7.0
     44 *
     45 * @param string $name Name of the administration panel.
     46 * @return WP_Administration_Panel|null Administration panel object or null if not found.
     47 */
     48function get_administration_panel( $name ) {
     49        return WP_Administration_Panel::get( $name );
     50}
     51
     52/**
     53 * Returns the current administration panel object.
     54 *
     55 * @since 4.7.0
     56 *
     57 * @param bool $detect_via_callback Optional. Whether to detect the current panel via the callback function
     58 *                                  instead of checking for the constant. Only used if $name is not provided.
     59 *                                  Default false.
     60 * @return WP_Administration_Panel Administration panel object.
     61 */
     62function get_current_administration_panel( $detect_via_callback = false ) {
     63        return WP_Administration_Panel::get_current( $detect_via_callback );
     64}
     65
     66/**
     67 * Returns all administration panels.
     68 *
     69 * @since 4.7.0
     70 *
     71 * @param bool $include_default Optional. Whether to include the default administration panel. Default false.
     72 * @return array Array of administration panel objects.
     73 */
     74function get_administration_panels( $include_default = false ) {
     75        return WP_Administration_Panel::get_all( $include_default );
     76}
  • src/wp-admin/includes/class-wp-administration-panel.php

    Property changes on: src/wp-admin/includes/administration-panel.php
    ___________________________________________________________________
    Added: svn:executable
    ## -0,0 +1 ##
    +*
    \ No newline at end of property
     
     1<?php
     2/**
     3 * Administration Panel API: WP_Administration_Panel class
     4 *
     5 * @package WordPress
     6 * @subpackage Administration
     7 * @since 4.7.0
     8 */
     9
     10/**
     11 * Core class used to implement an administration panel API.
     12 *
     13 * @since 4.7.0
     14 */
     15final class WP_Administration_Panel {
     16        /**
     17         * The panel identifier.
     18         *
     19         * @since 4.7.0
     20         * @access public
     21         * @var string
     22         */
     23        public $name;
     24
     25        /**
     26         * The panel's constant name.
     27         *
     28         * @since 4.7.0
     29         * @access public
     30         * @var string
     31         */
     32        public $constant_name;
     33
     34        /**
     35         * The panel's check function.
     36         *
     37         * @since 4.7.0
     38         * @access public
     39         * @var callable
     40         */
     41        public $check_callback;
     42
     43        /**
     44         * The panel's URL function.
     45         *
     46         * @since 4.7.0
     47         * @access public
     48         * @var callable
     49         */
     50        public $url_callback;
     51
     52        /**
     53         * The panel's menu file path.
     54         *
     55         * @since 4.7.0
     56         * @access public
     57         * @var string
     58         */
     59        public $menu_file;
     60
     61        /**
     62         * The panel admin title.
     63         *
     64         * @since 4.7.0
     65         * @access public
     66         * @var string
     67         */
     68        public $admin_title;
     69
     70        /**
     71         * The registered administration panels.
     72         *
     73         * @since 4.7.0
     74         * @access private
     75         * @static
     76         * @var array
     77         */
     78        private static $panels = array();
     79
     80        /**
     81         * The default administration panel.
     82         *
     83         * @since 4.7.0
     84         * @access private
     85         * @static
     86         * @var WP_Administration_Panel
     87         */
     88        private static $default_panel;
     89
     90        /**
     91         * Registers an administration panel.
     92         *
     93         * This function should be used on the 'admin_setup' hook.
     94         * It is by default used to register the 'network' and 'user' administration panels.
     95         *
     96         * @since 4.7.0
     97         * @access public
     98         * @static
     99         *
     100         * @see register_administration_panel()
     101         *
     102         * @param string $name Name of the administration panel. Must not contain dashes.
     103         * @param array|string $args {
     104         *     Array or string of administration panel arguments.
     105         *
     106         *     @type string   $constant_name  Required. Name of the constant to check for whether we're currently
     107         *                                    in that administration panel.
     108         *     @type callable $check_callback Required. A callback function that can be used to check whether we're
     109         *                                    currently in that administration panel.
     110         *     @type callable $url_callback   Required. A callback function to return the URL to the administration
     111         *                                    panel. The function must accept two parameters, `$path` and `$scheme`
     112         *                                    and return the full URL to the administration panel for that path.
     113         *     @type string   $menu_file      Required. The filename of the PHP file to include to setup the menu
     114         *                                    for the administration panel.
     115         *     @type string   $admin_title    Optional. Title to use in the title tag.
     116         * }
     117         * @return WP_Administration_Panel|WP_Error The administration panel object on success, an error object otherwise.
     118         */
     119        public static function register( $name, $args ) {
     120                // 'site' represents the default administration panel, so it is not explicitly registered, but still forbidden.
     121                if ( self::get_default()->name === $name ) {
     122                        return new WP_Error( 'admin_panel_already_exist', sprintf( __( 'The administration panel %s already exists.' ), 'site' ) );
     123                }
     124
     125                // It is not allowed to override an existing administration panel.
     126                if ( isset( self::$panels[ $name ] ) ) {
     127                        return new WP_Error( 'admin_panel_already_exist', sprintf( __( 'The administration panel %s already exists.' ), $name ) );
     128                }
     129
     130                $args = wp_parse_args( $args );
     131
     132                $required = array( 'constant_name', 'check_callback', 'url_callback', 'menu_file' );
     133
     134                foreach ( $required as $key ) {
     135                        if ( empty( $args[ $key ] ) ) {
     136                                return new WP_Error( 'empty_panel_argument', sprintf( __( 'The administration panel argument %s must not be empty.' ), $key ) );
     137                        }
     138                }
     139
     140                $panel = new self( $name, $args );
     141
     142                self::$panels[ $name ] = $panel;
     143
     144                return $panel;
     145        }
     146
     147        /**
     148         * Returns the administration panel object for a given name.
     149         *
     150         * @since 4.7.0
     151         * @access public
     152         * @static
     153         *
     154         * @see get_administration_panel()
     155         *
     156         * @param string $name Name of the administration panel.
     157         * @return WP_Administration_Panel|null Administration panel object or null if not found.
     158         */
     159        public static function get( $name ) {
     160                if ( self::get_default()->name === $name ) {
     161                        return self::get_default();
     162                }
     163
     164                if ( ! isset( self::$panels[ $name ] ) ) {
     165                        return null;
     166                }
     167
     168                return self::$panels[ $name ];
     169        }
     170
     171        /**
     172         * Returns the current administration panel object.
     173         *
     174         * @since 4.7.0
     175         * @access public
     176         * @static
     177         *
     178         * @see get_current_administration_panel()
     179         *
     180         * @param bool $detect_via_callback Optional. Whether to detect the current panel via the callback function
     181         *                                  instead of checking for the constant. Only used if $name is not provided.
     182         *                                  Default false.
     183         * @return WP_Administration_Panel Administration panel object.
     184         */
     185        public static function get_current( $detect_via_callback = false ) {
     186                foreach ( self::$panels as $panel_name => $panel ) {
     187                        if ( $panel->is_active( $detect_via_callback ) ) {
     188                                return $panel;
     189                        }
     190                }
     191
     192                return self::get_default();
     193        }
     194
     195        /**
     196         * Returns all administration panels.
     197         *
     198         * @since 4.7.0
     199         * @access public
     200         * @static
     201         *
     202         * @see get_administration_panels()
     203         *
     204         * @param bool $include_default Optional. Whether to include the default administration panel. Default false.
     205         * @return array Array of administration panel objects.
     206         */
     207        public static function get_all( $include_default = false ) {
     208                $panels = self::$panels;
     209
     210                if ( $include_default ) {
     211                        $panels[ self::$default_panel->name ] = self::$default_panel;
     212                }
     213
     214                return $panels;
     215        }
     216
     217        /**
     218         * Returns the default administration panel.
     219         *
     220         * @since 4.7.0
     221         * @access public
     222         * @static
     223         *
     224         * @return WP_Administration_Panel The default administration panel.
     225         */
     226        public static function get_default() {
     227                if ( ! isset( self::$default_panel ) ) {
     228                        self::$default_panel = new self( 'site', array(
     229                                'constant_name'  => 'WP_BLOG_ADMIN',
     230                                'check_callback' => 'is_blog_admin',
     231                                'url_callback'   => 'admin_url',
     232                                'menu_file'      => ABSPATH . 'wp-admin/menu.php',
     233                        ) );
     234                }
     235
     236                return self::$default_panel;
     237        }
     238
     239        /**
     240         * Constructor.
     241         *
     242         * @since 4.7.0
     243         * @access private
     244         *
     245         * @param string       $name Name of the administration panel. Must not contain dashes.
     246         * @param array|string $args Array or string of arguments for registering an administration panel.
     247         */
     248        private function __construct( $name, $args ) {
     249                $defaults = array(
     250                        'constant_name'  => '',
     251                        'check_callback' => null,
     252                        'url_callback'   => null,
     253                        'menu_file'      => '',
     254                        'admin_title'    => '',
     255                );
     256
     257                $args = wp_parse_args( $args, $defaults );
     258
     259                if ( empty( $args['admin_title'] ) ) {
     260                        $args['admin_title'] = get_bloginfo( 'name' );
     261                }
     262
     263                $this->name = $name;
     264
     265                foreach ( $args as $key => $value ) {
     266                        $this->$key = $value;
     267                }
     268        }
     269
     270        /**
     271         * Checks whether this is the currently active administration panel.
     272         *
     273         * @since 4.7.0
     274         * @access public
     275         *
     276         * @param bool $detect_via_callback Optional. Whether to detect the current panel via the callback function
     277         *                                  instead of checking for the constant. Only used if $name is not provided.
     278         *                                  Default false.
     279         * @return bool True if active, false otherwise.
     280         */
     281        public function is_active( $detect_via_callback = false ) {
     282                if ( $detect_via_callback && call_user_func( $this->check_callback ) ) {
     283                        return true;
     284                }
     285
     286                if ( ! $detect_via_callback && defined( $this->constant_name ) && constant( $this->constant_name ) ) {
     287                        return true;
     288                }
     289
     290                return false;
     291        }
     292}
  • src/wp-admin/includes/class-wp-screen.php

    Property changes on: src/wp-admin/includes/class-wp-administration-panel.php
    ___________________________________________________________________
    Added: svn:executable
    ## -0,0 +1 ##
    +*
    \ No newline at end of property
     
    243243                }
    244244
    245245                if ( ! $post_type && $hook_name ) {
    246                         if ( '-network' == substr( $id, -8 ) ) {
    247                                 $id = substr( $id, 0, -8 );
    248                                 $in_admin = 'network';
    249                         } elseif ( '-user' == substr( $id, -5 ) ) {
    250                                 $id = substr( $id, 0, -5 );
    251                                 $in_admin = 'user';
     246                        foreach ( get_administration_panels() as $panel ) {
     247                                $length = -1 - strlen( $panel->name );
     248                                if ( '-' . $panel->name == substr( $id, $length ) ) {
     249                                        $id = substr( $id, 0, $length );
     250                                        $in_admin = $panel->name;
     251                                        break;
     252                                }
    252253                        }
    253254
    254255                        $id = sanitize_key( $id );
     
    266267                        if ( ! $in_admin )
    267268                                $in_admin = 'site';
    268269                } else {
    269                         if ( defined( 'WP_NETWORK_ADMIN' ) && WP_NETWORK_ADMIN )
    270                                 $in_admin = 'network';
    271                         elseif ( defined( 'WP_USER_ADMIN' ) && WP_USER_ADMIN )
    272                                 $in_admin = 'user';
    273                         else
    274                                 $in_admin = 'site';
     270                        $in_admin = get_current_administration_panel()->name;
    275271                }
    276272
    277273                if ( 'index' == $id )
     
    337333                                break;
    338334                }
    339335
    340                 if ( 'network' == $in_admin ) {
    341                         $id   .= '-network';
    342                         $base .= '-network';
    343                 } elseif ( 'user' == $in_admin ) {
    344                         $id   .= '-user';
    345                         $base .= '-user';
     336                foreach ( get_administration_panels() as $panel ) {
     337                        if ( $panel->name == $in_admin ) {
     338                                $id   .= '-' . $panel->name;
     339                                $base .= '-' . $panel->name;
     340                                break;
     341                        }
    346342                }
    347343
    348344                if ( isset( self::$_registry[ $id ] ) ) {
  • src/wp-admin/includes/menu.php

     
    66 * @subpackage Administration
    77 */
    88
    9 if ( is_network_admin() ) {
     9$_panel_name = get_current_administration_panel()->name;
     10if ( 'site' !== $_panel_name ) {
    1011
    1112        /**
    12          * Fires before the administration menu loads in the Network Admin.
     13         * Fires before the administration menu loads in a specific Admin.
    1314         *
    1415         * The hook fires before menus and sub-menus are removed based on user privileges.
    1516         *
    16          * @private
    17          * @since 3.1.0
    18          */
    19         do_action( '_network_admin_menu' );
    20 } elseif ( is_user_admin() ) {
    21 
    22         /**
    23          * Fires before the administration menu loads in the User Admin.
     17         * The dynamic portion of the hook name, `$panel_name`, refers to the Administration Panel name.
    2418         *
    25          * The hook fires before menus and sub-menus are removed based on user privileges.
    26          *
    2719         * @private
    2820         * @since 3.1.0
     21         * @since 4.7.0 Hook is now dynamic.
    2922         */
    30         do_action( '_user_admin_menu' );
     23        do_action( '_' . $_panel_name . '_admin_menu' );
    3124} else {
    3225
    3326        /**
     
    117110}
    118111unset($id, $data, $subs, $first_sub, $old_parent, $new_parent);
    119112
    120 if ( is_network_admin() ) {
     113if ( 'site' !== $_panel_name ) {
    121114
    122115        /**
    123          * Fires before the administration menu loads in the Network Admin.
     116         * Fires before the administration menu loads in a specific Admin.
    124117         *
    125          * @since 3.1.0
     118         * The dynamic portion of the hook name, `$panel_name`, refers to the Administration Panel name.
    126119         *
    127          * @param string $context Empty context.
    128          */
    129         do_action( 'network_admin_menu', '' );
    130 } elseif ( is_user_admin() ) {
    131 
    132         /**
    133          * Fires before the administration menu loads in the User Admin.
    134          *
     120         * @private
    135121         * @since 3.1.0
     122         * @since 4.7.0 Hook is now dynamic.
    136123         *
    137124         * @param string $context Empty context.
    138125         */
    139         do_action( 'user_admin_menu', '' );
     126        do_action( $_panel_name . '_admin_menu', '' );
    140127} else {
    141128
    142129        /**
     
    149136        do_action( 'admin_menu', '' );
    150137}
    151138
     139unset( $_panel_name );
     140
    152141/*
    153142 * Remove menus that have no accessible submenus and require privileges
    154143 * that the user does not have. Run re-parent loop again.
  • src/wp-includes/link-template.php

     
    33983398 * @return string Admin URL link with optional path appended.
    33993399 */
    34003400function self_admin_url( $path = '', $scheme = 'admin' ) {
    3401         if ( is_network_admin() )
    3402                 return network_admin_url($path, $scheme);
    3403         elseif ( is_user_admin() )
    3404                 return user_admin_url($path, $scheme);
    3405         else
    3406                 return admin_url($path, $scheme);
     3401        if ( function_exists( 'get_current_administration_panel' ) ) {
     3402                $panel = get_current_administration_panel( true );
     3403                return call_user_func( $panel->url_callback, $path, $scheme );
     3404        }
     3405
     3406        return admin_url( $path, $scheme );
    34073407}
    34083408
    34093409/**