Make WordPress Core

Ticket #37526: 37526.2.diff

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

     
    3333get_admin_page_title();
    3434$title = esc_html( strip_tags( $title ) );
    3535
    36 if ( is_network_admin() )
    37         $admin_title = sprintf( __( 'Network Admin: %s' ), esc_html( get_current_site()->site_name ) );
    38 elseif ( is_user_admin() )
    39         $admin_title = sprintf( __( 'User Dashboard: %s' ), esc_html( get_current_site()->site_name ) );
    40 else
     36$_panel = get_administration_panel( '', true );
     37if ( $_panel && ! empty( $_panel->admin_title ) ) {
     38        $admin_title = $_panel->admin_title;
     39} else {
    4140        $admin_title = get_bloginfo( 'name' );
     41}
    4242
    4343if ( $admin_title == $title )
    4444        $admin_title = sprintf( __( '%1$s — WordPress' ), $title );
     
    227227
    228228$current_screen->render_screen_meta();
    229229
    230 if ( is_network_admin() ) {
     230if ( $_panel ) {
    231231        /**
    232          * Prints network admin screen notices.
     232         * Prints admin screen notices for a specific Admin.
    233233         *
    234          * @since 3.1.0
    235          */
    236         do_action( 'network_admin_notices' );
    237 } elseif ( is_user_admin() ) {
    238         /**
    239          * Prints user admin screen notices.
     234         * The dynamic portion of the hook name, `$panel_name`, refers to the Administration Panel name.
    240235         *
    241236         * @since 3.1.0
     237         * @since 4.7.0 Hook is now dynamic.
    242238         */
    243         do_action( 'user_admin_notices' );
     239        do_action( $_panel->name . '_admin_notices' );
    244240} else {
    245241        /**
    246242         * Prints admin screen notices.
     
    250246        do_action( 'admin_notices' );
    251247}
    252248
     249unset( $_panel );
     250
    253251/**
    254252 * Prints generic admin screen notices.
    255253 *
  • src/wp-admin/admin.php

     
    8383
    8484auth_redirect();
    8585
     86// Register the additional administration panels.
     87$site_name = function_exists( 'get_current_site' ) ? get_current_site()->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
     164$_panel = get_administration_panel();
     165if ( $_panel ) {
     166        require( $_panel->menu_file );
     167} else {
    138168        require(ABSPATH . 'wp-admin/menu.php');
     169}
     170unset( $_panel );
    139171
    140172if ( current_user_can( 'manage_options' ) ) {
    141173        wp_raise_memory_limit( 'admin' );
  • src/wp-admin/includes/class-wp-screen.php

     
    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
     270                        $panel = get_administration_panel();
     271                        if ( $panel ) {
     272                                $in_admin = $panel->name;
     273                        } else {
    274274                                $in_admin = 'site';
     275                        }
    275276                }
    276277
    277278                if ( 'index' == $id )
     
    337338                                break;
    338339                }
    339340
    340                 if ( 'network' == $in_admin ) {
    341                         $id   .= '-network';
    342                         $base .= '-network';
    343                 } elseif ( 'user' == $in_admin ) {
    344                         $id   .= '-user';
    345                         $base .= '-user';
     341                foreach ( get_administration_panels() as $panel ) {
     342                        if ( $panel->name == $in_admin ) {
     343                                $id   .= '-' . $panel->name;
     344                                $base .= '-' . $panel->name;
     345                                break;
     346                        }
    346347                }
    347348
    348349                if ( isset( self::$_registry[ $id ] ) ) {
  • src/wp-admin/includes/menu.php

     
    66 * @subpackage Administration
    77 */
    88
    9 if ( is_network_admin() ) {
     9$_panel = get_administration_panel( '', true );
     10if ( $_panel ) {
    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 ( $_panel ) {
    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 );
     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-admin/includes/misc.php

     
    936936        </script>
    937937        <?php
    938938}
     939
     940/**
     941 * Register an administration panel. Use this function on admin_setup.
     942 *
     943 * A function to register a custom administration panel from a plugin. WordPress also
     944 * uses it to register the 'network' and 'user' administration panels.
     945 *
     946 * @since 4.7.0
     947 * @global array $_wp_administration_panels Contains the administration panels.
     948 *
     949 * @param string $post_status Name of the administration panel. Must not contain dashes.
     950 * @param array|string $args {
     951 *     Array or string of administration panel arguments.
     952 *
     953 *     @type string   $constant_name  Required. Name of the constant to check for whether we're currently
     954 *                                    in that administration panel.
     955 *     @type callable $check_callback Required. A callback function that can be used to check whether we're
     956 *                                    currently in that administration panel.
     957 *     @type callable $url_callback   Required. A callback function to return the URL to the administration
     958 *                                    panel. The function must accept two parameters, `$path` and `$scheme`
     959 *                                    and return the full URL to the administration panel for that path.
     960 *     @type string   $menu_file      Required. The filename of the PHP file to include to setup the menu
     961 *                                    for the administration panel.
     962 *     @type string   $admin_title    Optional. Title to use in the title tag.
     963 * }
     964 * @return object|WP_Error The administration panel object on success, an error object otherwise.
     965 */
     966function register_administration_panel( $name, $args ) {
     967        global $_wp_administration_panels;
     968
     969        if ( ! isset( $_wp_administration_panels ) ) {
     970                $_wp_administration_panels = array();
     971        }
     972
     973        // 'site' represents the default administration panel, so it is not explicitly registered, but still forbidden.
     974        if ( 'site' === $name ) {
     975                return new WP_Error( 'admin_panel_already_exist', sprintf( __( 'The administration panel %s already exists.' ), 'site' ) );
     976        }
     977
     978        // It is not allowed to override an existing administration panel.
     979        if ( isset( $_wp_administration_panels[ $name ] ) ) {
     980                return new WP_Error( 'admin_panel_already_exist', sprintf( __( 'The administration panel %s already exists.' ), $name ) );
     981        }
     982
     983        $defaults = array(
     984                'constant_name'  => '',
     985                'check_callback' => null,
     986                'url_callback'   => null,
     987                'menu_file'      => '',
     988                'admin_title'    => '',
     989        );
     990
     991        $args = wp_parse_args( $args, $defaults );
     992
     993        $required = array( 'constant_name', 'check_callback', 'url_callback', 'menu_file' );
     994
     995        foreach ( $required as $key ) {
     996                if ( empty( $args[ $key ] ) ) {
     997                        return new WP_Error( 'empty_panel_argument', sprintf( __( 'The administration panel argument %s must not be empty.' ), $key ) );
     998                }
     999        }
     1000
     1001        $args['name'] = $name;
     1002
     1003        $panel = (object) $args;
     1004
     1005        $_wp_administration_panels[ $name ] = $panel;
     1006
     1007        return $panel;
     1008}
     1009
     1010/**
     1011 * Returns the administration panels.
     1012 *
     1013 * @since 4.7.0
     1014 * @global array $_wp_administration_panels Contains the administration panels.
     1015 *
     1016 * @return array Array of administration panel objects.
     1017 */
     1018function get_administration_panels() {
     1019        global $_wp_administration_panels;
     1020
     1021        if ( ! isset( $_wp_administration_panels ) ) {
     1022                return array();
     1023        }
     1024
     1025        return $_wp_administration_panels;
     1026}
     1027
     1028/**
     1029 * Returns an administration panel object.
     1030 *
     1031 * If $name is not provided, it returns the current administration panel.
     1032 *
     1033 * @since 4.7.0
     1034 * @global array $_wp_administration_panels Contains the administration panels.
     1035 *
     1036 * @param string $name                Optional. Name of the administration panel. Defaults to the current
     1037 *                                    administration panel.
     1038 * @param bool   $detect_via_callback Optional. Whether to detect the current panel via the callback function
     1039 *                                    instead of checking for the constant. Only used if $name is not provided.
     1040 *                                    Default false.
     1041 * @return object|null Administration panel object or null if not found.
     1042 */
     1043function get_administration_panel( $name = '', $detect_via_callback = false ) {
     1044        global $_wp_administration_panels;
     1045
     1046        if ( ! isset( $_wp_administration_panels ) ) {
     1047                return null;
     1048        }
     1049
     1050        if ( empty( $name ) ) {
     1051                foreach ( $_wp_administration_panels as $panel_name => $panel ) {
     1052                        if ( $detect_via_callback && call_user_func( $panel->check_callback ) ) {
     1053                                return $panel;
     1054                        } elseif ( ! $detect_via_callback && defined( $panel->constant_name ) && constant( $panel->constant_name ) ) {
     1055                                return $panel;
     1056                        }
     1057                }
     1058                return null;
     1059        }
     1060
     1061        if ( ! isset( $_wp_administration_panels[ $name ] ) ) {
     1062                return null;
     1063        }
     1064
     1065        return $_wp_administration_panels[ $name ];
     1066}
  • src/wp-includes/link-template.php

     
    34013401 * @return string Admin URL link with optional path appended.
    34023402 */
    34033403function self_admin_url( $path = '', $scheme = 'admin' ) {
    3404         if ( is_network_admin() )
    3405                 return network_admin_url($path, $scheme);
    3406         elseif ( is_user_admin() )
    3407                 return user_admin_url($path, $scheme);
    3408         else
    3409                 return admin_url($path, $scheme);
     3404        if ( function_exists( 'get_administration_panel' ) ) {
     3405                $panel = get_administration_panel( '', true );
     3406                if ( $panel ) {
     3407                        return call_user_func( $panel->url_callback, $path, $scheme );
     3408                }
     3409        }
     3410
     3411        return admin_url( $path, $scheme );
    34103412}
    34113413
    34123414/**