From efb6e805dad360b4e45ebbd587bf6f70b7fc66e9 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Sat, 9 May 2020 12:24:31 +0000 Subject: [PATCH] Administration: Avoid a PHP 7.4 notice in `add_meta_box()` when attempting to re-add a previously removed box. The logic for skipping previously removed meta boxes with the `core` priority should also apply to the `sorted` priority that is used when the boxes were manually reordered. Add a unit test. Props coolmann, franzarmas, SergeyBiryukov. Fixes #50019. git-svn-id: https://develop.svn.wordpress.org/trunk@47777 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-admin/includes/template.php | 25 +++++++++++-------- .../phpunit/tests/admin/includesTemplate.php | 21 +++++++++++++++- 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/src/wp-admin/includes/template.php b/src/wp-admin/includes/template.php index 168dc3bc07..b0fb03cb18 100644 --- a/src/wp-admin/includes/template.php +++ b/src/wp-admin/includes/template.php @@ -1063,16 +1063,18 @@ function add_meta_box( $id, $title, $callback, $screen = null, $context = 'advan continue; } - // If a core box was previously added or removed by a plugin, don't add. - if ( 'core' === $priority ) { - // If core box previously deleted, don't add. - if ( false === $wp_meta_boxes[ $page ][ $a_context ][ $a_priority ][ $id ] ) { - return; - } + // If a core box was previously removed, don't add. + if ( ( 'core' === $priority || 'sorted' === $priority ) + && false === $wp_meta_boxes[ $page ][ $a_context ][ $a_priority ][ $id ] + ) { + return; + } + // If a core box was previously added by a plugin, don't add. + if ( 'core' === $priority ) { /* - * If box was added with default priority, give it core priority to - * maintain sort order. + * If the box was added with default priority, give it core priority + * to maintain sort order. */ if ( 'default' === $a_priority ) { $wp_meta_boxes[ $page ][ $a_context ]['core'][ $id ] = $wp_meta_boxes[ $page ][ $a_context ]['default'][ $id ]; @@ -1080,13 +1082,14 @@ function add_meta_box( $id, $title, $callback, $screen = null, $context = 'advan } return; } + // If no priority given and ID already present, use existing priority. if ( empty( $priority ) ) { $priority = $a_priority; /* - * Else, if we're adding to the sorted priority, we don't know the title - * or callback. Grab them from the previously added context/priority. - */ + * Else, if we're adding to the sorted priority, we don't know the title + * or callback. Grab them from the previously added context/priority. + */ } elseif ( 'sorted' === $priority ) { $title = $wp_meta_boxes[ $page ][ $a_context ][ $a_priority ][ $id ]['title']; $callback = $wp_meta_boxes[ $page ][ $a_context ][ $a_priority ][ $id ]['callback']; diff --git a/tests/phpunit/tests/admin/includesTemplate.php b/tests/phpunit/tests/admin/includesTemplate.php index ea656c81e8..a944615b8d 100644 --- a/tests/phpunit/tests/admin/includesTemplate.php +++ b/tests/phpunit/tests/admin/includesTemplate.php @@ -57,7 +57,7 @@ class Tests_Admin_includesTemplate extends WP_UnitTestCase { public function test_remove_meta_box() { global $wp_meta_boxes; - // Add a meta boxes to remove. + // Add a meta box to remove. add_meta_box( 'testbox1', 'Test Metabox', '__return_false', $current_screen = 'post' ); // Confirm it's there. @@ -108,6 +108,25 @@ class Tests_Admin_includesTemplate extends WP_UnitTestCase { $this->assertFalse( $wp_meta_boxes['attachment']['advanced']['default']['testbox1'] ); } + /** + * @ticket 50019 + */ + public function test_add_meta_box_with_previously_removed_box_and_sorted_priority() { + global $wp_meta_boxes; + + // Add a meta box to remove. + add_meta_box( 'testbox1', 'Test Metabox', '__return_false', $current_screen = 'post' ); + + // Remove the meta box. + remove_meta_box( 'testbox1', $current_screen, 'advanced' ); + + // Attempt to re-add the meta box with the 'sorted' priority. + add_meta_box( 'testbox1', null, null, $current_screen, 'advanced', 'sorted' ); + + // Check that the meta box was not re-added. + $this->assertFalse( $wp_meta_boxes[ $current_screen ]['advanced']['default']['testbox1'] ); + } + /** * Test calling get_settings_errors() with variations on where it gets errors from. *