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
This commit is contained in:
Sergey Biryukov 2020-05-09 12:24:31 +00:00
parent e6547ba815
commit efb6e805da
2 changed files with 34 additions and 12 deletions

View File

@ -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'];

View File

@ -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.
*