diff --git a/src/wp-admin/includes/template-functions.php b/src/wp-admin/includes/template-functions.php index e3abbf93b0..88b07df0f3 100644 --- a/src/wp-admin/includes/template-functions.php +++ b/src/wp-admin/includes/template-functions.php @@ -847,9 +847,10 @@ function wp_import_upload_form( $action ) { } /** - * Add a meta box to an edit form. + * Adds a meta box to one or more screens. * * @since 2.5.0 + * @since 4.4.0 The `$screen` parameter now accepts an array of screen IDs. * * @global array $wp_meta_boxes * @@ -857,8 +858,10 @@ function wp_import_upload_form( $action ) { * @param string $title Title of the meta box. * @param callable $callback Function that fills the box with the desired content. * The function should echo its output. - * @param string|WP_Screen $screen Optional. The screen on which to show the box (like a post - * type, 'link', or 'comment'). Default is the current screen. + * @param string|array|WP_Screen $screen Optional. The screen or screens on which to show the box + * (such as a post type, 'link', or 'comment'). Accepts a single + * screen ID, WP_Screen object, or array of screen IDs. Default + * is the current screen. * @param string $context Optional. The context within the screen where the boxes * should display. Available contexts vary from screen to * screen. Post edit screen contexts include 'normal', 'side', @@ -874,10 +877,19 @@ function wp_import_upload_form( $action ) { function add_meta_box( $id, $title, $callback, $screen = null, $context = 'advanced', $priority = 'default', $callback_args = null ) { global $wp_meta_boxes; - if ( empty( $screen ) ) + if ( empty( $screen ) ) { $screen = get_current_screen(); - elseif ( is_string( $screen ) ) + } elseif ( is_string( $screen ) ) { $screen = convert_to_screen( $screen ); + } elseif ( is_array( $screen ) ) { + foreach ( $screen as $single_screen ) { + add_meta_box( $id, $title, $callback, $single_screen, $context, $priority, $callback_args ); + } + } + + if ( ! isset( $screen->id ) ) { + return; + } $page = $screen->id; @@ -1011,23 +1023,35 @@ function do_meta_boxes( $screen, $context, $object ) { } /** - * Remove a meta box from an edit form. + * Removes a meta box from one or more screens. * * @since 2.6.0 + * @since 4.4.0 The `$screen` parameter now accepts an array of screen IDs. * * @global array $wp_meta_boxes * * @param string $id String for use in the 'id' attribute of tags. - * @param string|object $screen The screen on which to show the box (post, page, link). + * @param string|array|WP_Screen $screen The screen or screens on which the meta box is shown (such as a + * post type, 'link', or 'comment'). Accepts a single screen ID, + * WP_Screen object, or array of screen IDs. * @param string $context The context within the page where the boxes should show ('normal', 'advanced'). */ -function remove_meta_box($id, $screen, $context) { +function remove_meta_box( $id, $screen, $context ) { global $wp_meta_boxes; - if ( empty( $screen ) ) + if ( empty( $screen ) ) { $screen = get_current_screen(); - elseif ( is_string( $screen ) ) + } elseif ( is_string( $screen ) ) { $screen = convert_to_screen( $screen ); + } elseif ( is_array( $screen ) ) { + foreach ( $screen as $single_screen ) { + remove_meta_box( $id, $single_screen, $context ); + } + } + + if ( ! isset( $screen->id ) ) { + return; + } $page = $screen->id; diff --git a/tests/phpunit/tests/admin/includesTemplate.php b/tests/phpunit/tests/admin/includesTemplate.php index b3bd6dccb6..736ffa7b7f 100644 --- a/tests/phpunit/tests/admin/includesTemplate.php +++ b/tests/phpunit/tests/admin/includesTemplate.php @@ -45,4 +45,67 @@ class Tests_Admin_includesTemplate extends WP_UnitTestCase { $this->assertEquals('', selected(0,false,false)); $this->assertEquals('', checked(0,false,false)); } -} + + public function test_add_meta_box() { + global $wp_meta_boxes; + + add_meta_box( 'testbox1', 'Test Metabox', '__return_false', 'post' ); + + $this->assertArrayHasKey( 'testbox1', $wp_meta_boxes['post']['advanced']['default'] ); + } + + public function test_remove_meta_box() { + global $wp_meta_boxes; + + // Add a meta boxes to remove. + add_meta_box( 'testbox1', 'Test Metabox', '__return_false', $current_screen = 'post' ); + + // Confirm it's there. + $this->assertArrayHasKey( 'testbox1', $wp_meta_boxes[ $current_screen ]['advanced']['default'] ); + + // Remove the meta box. + remove_meta_box( 'testbox1', $current_screen, 'advanced' ); + + // Check that it was removed properly (The meta box should be set to false once that it has been removed) + $this->assertFalse( $wp_meta_boxes[ $current_screen ]['advanced']['default']['testbox1'] ); + } + + /** + * @ticket 15000 + */ + public function test_add_meta_box_on_multiple_screens() { + global $wp_meta_boxes; + + // Add a meta box to three different post types + add_meta_box( 'testbox1', 'Test Metabox', '__return_false', array( 'post', 'comment', 'attachment' ) ); + + $this->assertArrayHasKey( 'testbox1', $wp_meta_boxes['post']['advanced']['default'] ); + $this->assertArrayHasKey( 'testbox1', $wp_meta_boxes['comment']['advanced']['default'] ); + $this->assertArrayHasKey( 'testbox1', $wp_meta_boxes['attachment']['advanced']['default'] ); + } + + /** + * @ticket 15000 + */ + public function test_remove_meta_box_from_multiple_screens() { + global $wp_meta_boxes; + + // Add a meta box to three different screens. + add_meta_box( 'testbox1', 'Test Metabox', '__return_false', array( 'post', 'comment', 'attachment' ) ); + + // Remove meta box from posts. + remove_meta_box( 'testbox1', 'post', 'advanced' ); + + // Check that we have removed the meta boxes only from posts + $this->assertFalse( $wp_meta_boxes['post']['advanced']['default']['testbox1'] ); + $this->assertArrayHasKey( 'testbox1', $wp_meta_boxes['comment']['advanced']['default'] ); + $this->assertArrayHasKey( 'testbox1', $wp_meta_boxes['attachment']['advanced']['default'] ); + + // Remove the meta box from the other screens. + remove_meta_box( 'testbox1', array( 'comment', 'attachment' ), 'advanced' ); + + $this->assertFalse( $wp_meta_boxes['comment']['advanced']['default']['testbox1'] ); + $this->assertFalse( $wp_meta_boxes['attachment']['advanced']['default']['testbox1'] ); + } + +} \ No newline at end of file