WordPress.org

Make WordPress Core

Changeset 34951


Ignore:
Timestamp:
10/08/15 19:06:32 (2 years ago)
Author:
DrewAPicture
Message:

Administration: Add the ability to pass an array of screen IDs to add_meta_box() and remove_meta_box().

The $screen parameter in both functions can now accept a single screen ID, WP_Screen object, or an array of screen IDs.

Adds tests.

Props coffee2code, iamfriendly, madalinungureanu, mordauk, igmoweb, meloniq, DrewAPicture.
See #15000.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/template-functions.php

    r34893 r34951  
    848848 
    849849/** 
    850  * Add a meta box to an edit form. 
     850 * Adds a meta box to one or more screens. 
    851851 * 
    852852 * @since 2.5.0 
     853 * @since 4.4.0 The `$screen` parameter now accepts an array of screen IDs. 
    853854 * 
    854855 * @global array $wp_meta_boxes 
     
    858859 * @param callable         $callback      Function that fills the box with the desired content. 
    859860 *                                        The function should echo its output. 
    860  * @param string|WP_Screen $screen        Optional. The screen on which to show the box (like a post 
    861  *                                        type, 'link', or 'comment'). Default is the current screen. 
     861 * @param string|array|WP_Screen $screen  Optional. The screen or screens on which to show the box 
     862 *                                        (such as a post type, 'link', or 'comment'). Accepts a single 
     863 *                                        screen ID, WP_Screen object, or array of screen IDs. Default 
     864 *                                        is the current screen. 
    862865 * @param string           $context       Optional. The context within the screen where the boxes 
    863866 *                                        should display. Available contexts vary from screen to 
     
    875878    global $wp_meta_boxes; 
    876879 
    877     if ( empty( $screen ) ) 
     880    if ( empty( $screen ) ) { 
    878881        $screen = get_current_screen(); 
    879     elseif ( is_string( $screen ) ) 
     882    } elseif ( is_string( $screen ) ) { 
    880883        $screen = convert_to_screen( $screen ); 
     884    } elseif ( is_array( $screen ) ) { 
     885        foreach ( $screen as $single_screen ) { 
     886            add_meta_box( $id, $title, $callback, $single_screen, $context, $priority, $callback_args ); 
     887        } 
     888    } 
     889 
     890    if ( ! isset( $screen->id ) ) { 
     891        return; 
     892    } 
    881893 
    882894    $page = $screen->id; 
     
    10121024 
    10131025/** 
    1014  * Remove a meta box from an edit form. 
     1026 * Removes a meta box from one or more screens. 
    10151027 * 
    10161028 * @since 2.6.0 
     1029 * @since 4.4.0 The `$screen` parameter now accepts an array of screen IDs. 
    10171030 * 
    10181031 * @global array $wp_meta_boxes 
    10191032 * 
    10201033 * @param string        $id      String for use in the 'id' attribute of tags. 
    1021  * @param string|object $screen  The screen on which to show the box (post, page, link). 
     1034 * @param string|array|WP_Screen $screen The screen or screens on which the meta box is shown (such as a 
     1035 *                                       post type, 'link', or 'comment'). Accepts a single screen ID, 
     1036 *                                       WP_Screen object, or array of screen IDs. 
    10221037 * @param string        $context The context within the page where the boxes should show ('normal', 'advanced'). 
    10231038 */ 
    1024 function remove_meta_box($id, $screen, $context) { 
     1039function remove_meta_box( $id, $screen, $context ) { 
    10251040    global $wp_meta_boxes; 
    10261041 
    1027     if ( empty( $screen ) ) 
     1042    if ( empty( $screen ) ) { 
    10281043        $screen = get_current_screen(); 
    1029     elseif ( is_string( $screen ) ) 
     1044    } elseif ( is_string( $screen ) ) { 
    10301045        $screen = convert_to_screen( $screen ); 
     1046    } elseif ( is_array( $screen ) ) { 
     1047        foreach ( $screen as $single_screen ) { 
     1048            remove_meta_box( $id, $single_screen, $context ); 
     1049        } 
     1050    } 
     1051 
     1052    if ( ! isset( $screen->id ) ) { 
     1053        return; 
     1054    } 
    10311055 
    10321056    $page = $screen->id; 
  • trunk/tests/phpunit/tests/admin/includesTemplate.php

    r25002 r34951  
    4646        $this->assertEquals('', checked(0,false,false)); 
    4747    } 
     48 
     49    public function test_add_meta_box() { 
     50        global $wp_meta_boxes; 
     51 
     52        add_meta_box( 'testbox1', 'Test Metabox', '__return_false', 'post' ); 
     53         
     54        $this->assertArrayHasKey( 'testbox1', $wp_meta_boxes['post']['advanced']['default'] ); 
     55    } 
     56 
     57    public function test_remove_meta_box() { 
     58        global $wp_meta_boxes; 
     59 
     60        // Add a meta boxes to remove. 
     61        add_meta_box( 'testbox1', 'Test Metabox', '__return_false', $current_screen = 'post' ); 
     62 
     63        // Confirm it's there. 
     64        $this->assertArrayHasKey( 'testbox1', $wp_meta_boxes[ $current_screen ]['advanced']['default'] ); 
     65 
     66        // Remove the meta box. 
     67        remove_meta_box( 'testbox1', $current_screen, 'advanced' ); 
     68 
     69        // Check that it was removed properly (The meta box should be set to false once that it has been removed) 
     70        $this->assertFalse( $wp_meta_boxes[ $current_screen ]['advanced']['default']['testbox1'] ); 
     71    } 
     72 
     73    /** 
     74     * @ticket 15000 
     75     */ 
     76    public function test_add_meta_box_on_multiple_screens() { 
     77        global $wp_meta_boxes; 
     78 
     79        // Add a meta box to three different post types 
     80        add_meta_box( 'testbox1', 'Test Metabox', '__return_false', array( 'post', 'comment', 'attachment' ) ); 
     81 
     82        $this->assertArrayHasKey( 'testbox1', $wp_meta_boxes['post']['advanced']['default'] );  
     83        $this->assertArrayHasKey( 'testbox1', $wp_meta_boxes['comment']['advanced']['default'] ); 
     84        $this->assertArrayHasKey( 'testbox1', $wp_meta_boxes['attachment']['advanced']['default'] ); 
     85    } 
     86 
     87    /** 
     88     * @ticket 15000 
     89     */ 
     90    public function test_remove_meta_box_from_multiple_screens() { 
     91        global $wp_meta_boxes; 
     92 
     93        // Add a meta box to three different screens. 
     94        add_meta_box( 'testbox1', 'Test Metabox', '__return_false', array( 'post', 'comment', 'attachment' ) ); 
     95 
     96        // Remove meta box from posts. 
     97        remove_meta_box( 'testbox1', 'post', 'advanced' ); 
     98 
     99        // Check that we have removed the meta boxes only from posts 
     100        $this->assertFalse( $wp_meta_boxes['post']['advanced']['default']['testbox1'] ); 
     101        $this->assertArrayHasKey( 'testbox1', $wp_meta_boxes['comment']['advanced']['default'] ); 
     102        $this->assertArrayHasKey( 'testbox1', $wp_meta_boxes['attachment']['advanced']['default'] ); 
     103 
     104        // Remove the meta box from the other screens. 
     105        remove_meta_box( 'testbox1', array( 'comment', 'attachment' ), 'advanced' ); 
     106 
     107        $this->assertFalse( $wp_meta_boxes['comment']['advanced']['default']['testbox1'] ); 
     108        $this->assertFalse( $wp_meta_boxes['attachment']['advanced']['default']['testbox1'] ); 
     109    } 
     110 
    48111} 
Note: See TracChangeset for help on using the changeset viewer.