WordPress.org

Make WordPress Core

Changeset 47777


Ignore:
Timestamp:
05/09/2020 12:24:31 PM (3 months ago)
Author:
SergeyBiryukov
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

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

    r47775 r47777  
    10641064            }
    10651065
    1066             // If a core box was previously added or removed by a plugin, don't add.
     1066            // If a core box was previously removed, don't add.
     1067            if ( ( 'core' === $priority || 'sorted' === $priority )
     1068                && false === $wp_meta_boxes[ $page ][ $a_context ][ $a_priority ][ $id ]
     1069            ) {
     1070                return;
     1071            }
     1072
     1073            // If a core box was previously added by a plugin, don't add.
    10671074            if ( 'core' === $priority ) {
    1068                 // If core box previously deleted, don't add.
    1069                 if ( false === $wp_meta_boxes[ $page ][ $a_context ][ $a_priority ][ $id ] ) {
    1070                     return;
    1071                 }
    1072 
    10731075                /*
    1074                  * If box was added with default priority, give it core priority to
    1075                  * maintain sort order.
     1076                 * If the box was added with default priority, give it core priority
     1077                 * to maintain sort order.
    10761078                 */
    10771079                if ( 'default' === $a_priority ) {
     
    10811083                return;
    10821084            }
     1085
    10831086            // If no priority given and ID already present, use existing priority.
    10841087            if ( empty( $priority ) ) {
    10851088                $priority = $a_priority;
    10861089                /*
    1087                 * Else, if we're adding to the sorted priority, we don't know the title
    1088                 * or callback. Grab them from the previously added context/priority.
    1089                 */
     1090                 * Else, if we're adding to the sorted priority, we don't know the title
     1091                 * or callback. Grab them from the previously added context/priority.
     1092                 */
    10901093            } elseif ( 'sorted' === $priority ) {
    10911094                $title         = $wp_meta_boxes[ $page ][ $a_context ][ $a_priority ][ $id ]['title'];
  • trunk/tests/phpunit/tests/admin/includesTemplate.php

    r47122 r47777  
    5858        global $wp_meta_boxes;
    5959
    60         // Add a meta boxes to remove.
     60        // Add a meta box to remove.
    6161        add_meta_box( 'testbox1', 'Test Metabox', '__return_false', $current_screen = 'post' );
    6262
     
    107107        $this->assertFalse( $wp_meta_boxes['comment']['advanced']['default']['testbox1'] );
    108108        $this->assertFalse( $wp_meta_boxes['attachment']['advanced']['default']['testbox1'] );
     109    }
     110
     111    /**
     112     * @ticket 50019
     113     */
     114    public function test_add_meta_box_with_previously_removed_box_and_sorted_priority() {
     115        global $wp_meta_boxes;
     116
     117        // Add a meta box to remove.
     118        add_meta_box( 'testbox1', 'Test Metabox', '__return_false', $current_screen = 'post' );
     119
     120        // Remove the meta box.
     121        remove_meta_box( 'testbox1', $current_screen, 'advanced' );
     122
     123        // Attempt to re-add the meta box with the 'sorted' priority.
     124        add_meta_box( 'testbox1', null, null, $current_screen, 'advanced', 'sorted' );
     125
     126        // Check that the meta box was not re-added.
     127        $this->assertFalse( $wp_meta_boxes[ $current_screen ]['advanced']['default']['testbox1'] );
    109128    }
    110129
Note: See TracChangeset for help on using the changeset viewer.