Make WordPress Core

Ticket #42791: 42791.5.diff

File 42791.5.diff, 6.0 KB (added by johnbillion, 4 years ago)
  • src/wp-admin/includes/dashboard.php

     
    157157 * Adds a new dashboard widget.
    158158 *
    159159 * @since 2.7.0
     160 * @since 5.6.0 The `$context` and `$priority` parameters were added.
    160161 *
    161162 * @global array $wp_dashboard_control_callbacks
    162163 *
     
    167168 * @param callable $control_callback Optional. Function that outputs controls for the widget. Default null.
    168169 * @param array    $callback_args    Optional. Data that should be set as the $args property of the widget array
    169170 *                                   (which is the second parameter passed to your callback). Default null.
     171 * @param string   $context          Optional. The context within the screen where the box should display.
     172 *                                   Accepts 'normal', 'side', 'column3', or 'column4'. Default 'normal'.
     173 * @param string   $priority         Optional. The priority within the context where the box should show.
     174 *                                   Accepts 'high', 'core', 'default', or 'low'. Default 'core'.
    170175 */
    171 function wp_add_dashboard_widget( $widget_id, $widget_name, $callback, $control_callback = null, $callback_args = null ) {
     176function wp_add_dashboard_widget( $widget_id, $widget_name, $callback, $control_callback = null, $callback_args = null, $context = 'normal', $priority = 'core' ) {
    172177        $screen = get_current_screen();
    173178        global $wp_dashboard_control_callbacks;
    174179
     
    194199
    195200        $side_widgets = array( 'dashboard_quick_press', 'dashboard_primary' );
    196201
    197         $location = 'normal';
    198202        if ( in_array( $widget_id, $side_widgets, true ) ) {
    199                 $location = 'side';
     203                $context = 'side';
    200204        }
    201205
    202206        $high_priority_widgets = array( 'dashboard_browser_nag', 'dashboard_php_nag' );
    203207
    204         $priority = 'core';
    205208        if ( in_array( $widget_id, $high_priority_widgets, true ) ) {
    206209                $priority = 'high';
    207210        }
    208211
    209         add_meta_box( $widget_id, $widget_name, $callback, $screen, $location, $priority, $callback_args );
     212        if ( empty( $context ) ) {
     213                $context = 'normal';
     214        }
     215        if ( empty( $priority ) ) {
     216                $priority = 'core';
     217        }
     218
     219        add_meta_box( $widget_id, $widget_name, $callback, $screen, $context, $priority, $callback_args );
    210220}
    211221
    212222/**
  • src/wp-admin/includes/template.php

     
    10141014 *                                              add_submenu_page() to create a new screen (and hence screen_id),
    10151015 *                                              make sure your menu slug conforms to the limits of sanitize_key()
    10161016 *                                              otherwise the 'screen' menu may not correctly render on your page.
    1017  * @param string                 $context       Optional. The context within the screen where the boxes
     1017 * @param string                 $context       Optional. The context within the screen where the box
    10181018 *                                              should display. Available contexts vary from screen to
    10191019 *                                              screen. Post edit screen contexts include 'normal', 'side',
    10201020 *                                              and 'advanced'. Comments screen contexts include 'normal'
    10211021 *                                              and 'side'. Menus meta boxes (accordion sections) all use
    10221022 *                                              the 'side' context. Global default is 'advanced'.
    1023  * @param string                 $priority      Optional. The priority within the context where the boxes
    1024  *                                              should show ('high', 'low'). Default 'default'.
     1023 * @param string                 $priority      Optional. The priority within the context where the box should show.
     1024 *                                              Accepts 'high', 'core', 'default', or 'low'. Default 'default'.
    10251025 * @param array                  $callback_args Optional. Data that should be set as the $args property
    10261026 *                                              of the box array (which is the second parameter passed
    10271027 *                                              to your callback). Default null.
  • tests/phpunit/tests/admin/includesTemplate.php

     
    201201                );
    202202        }
    203203
     204        /**
     205         * @ticket 42791
     206         */
     207        public function test_wp_add_dashboard_widget() {
     208                global $wp_meta_boxes;
     209
     210                set_current_screen( 'dashboard' );
     211
     212                if ( ! function_exists( 'wp_add_dashboard_widget' ) ) {
     213                        require_once ABSPATH . 'wp-admin/includes/dashboard.php';
     214                }
     215
     216                // Some hardcoded defaults for core widgets
     217                wp_add_dashboard_widget( 'dashboard_quick_press', 'Quick', '__return_false' );
     218                wp_add_dashboard_widget( 'dashboard_browser_nag', 'Nag', '__return_false' );
     219
     220                $this->assertArrayHasKey( 'dashboard_quick_press', $wp_meta_boxes['dashboard']['side']['core'] );
     221                $this->assertArrayHasKey( 'dashboard_browser_nag', $wp_meta_boxes['dashboard']['normal']['high'] );
     222
     223                // Location and priority defaults
     224                wp_add_dashboard_widget( 'dashboard1', 'Widget 1', '__return_false', null, null, 'foo' );
     225                wp_add_dashboard_widget( 'dashboard2', 'Widget 2', '__return_false', null, null, null, 'bar' );
     226
     227                $this->assertArrayHasKey( 'dashboard1', $wp_meta_boxes['dashboard']['foo']['core'] );
     228                $this->assertArrayHasKey( 'dashboard2', $wp_meta_boxes['dashboard']['normal']['bar'] );
     229
     230                // Cleanup
     231                remove_meta_box( 'dashboard_quick_press', 'dashboard', 'side' );
     232                remove_meta_box( 'dashboard_browser_nag', 'dashboard', 'normal' );
     233                remove_meta_box( 'dashboard1', 'dashboard', 'foo' );
     234
     235                // This doesn't actually get removed due to the invalid priority
     236                remove_meta_box( 'dashboard2', 'dashboard', 'normal' );
     237
     238                set_current_screen( 'front' );
     239        }
     240
    204241}