diff --git a/src/js/_enqueues/admin/postbox.js b/src/js/_enqueues/admin/postbox.js index b6d1b6569dc84..c7b159fbec918 100644 --- a/src/js/_enqueues/admin/postbox.js +++ b/src/js/_enqueues/admin/postbox.js @@ -657,4 +657,14 @@ pbhide : false }; + /* + * @since 7.0.0 + * @see https://core.trac.wordpress.org/ticket/17704 + */ + $( function() { + if ( typeof window.pagenow !== 'undefined' && ! postboxes.page && $( '.postbox' ).length ) { + postboxes.add_postbox_toggles( window.pagenow ); + } + } ); + }(jQuery)); diff --git a/src/wp-admin/includes/class-wp-screen.php b/src/wp-admin/includes/class-wp-screen.php index 227fda90c6273..0d54552c5e1ba 100644 --- a/src/wp-admin/includes/class-wp-screen.php +++ b/src/wp-admin/includes/class-wp-screen.php @@ -1001,6 +1001,14 @@ public function show_screen_options() { $show_screen = ! empty( $wp_meta_boxes[ $this->id ] ) || $columns || $this->get_option( 'per_page' ); + /* + * @since 7.0.0 + * @see https://core.trac.wordpress.org/ticket/17704 + */ + if ( ! empty( $wp_meta_boxes[ $this->id ] ) ) { + wp_enqueue_script( 'postbox' ); + } + $this->_screen_settings = ''; if ( 'post' === $this->base ) { diff --git a/tests/phpunit/tests/admin/includesScreen.php b/tests/phpunit/tests/admin/includesScreen.php index 26186391f261d..d73e25b0a9a4d 100644 --- a/tests/phpunit/tests/admin/includesScreen.php +++ b/tests/phpunit/tests/admin/includesScreen.php @@ -630,4 +630,45 @@ public function test_is_block_editor( $hook, $filter, $expected ) { $this->assertSame( $expected, $screen->is_block_editor ); } + + /** + * @ticket 17704 + */ + public function test_show_screen_options_enqueues_postbox_script_when_meta_boxes_exist() { + global $wp_meta_boxes; + + set_current_screen( 'tools.php' ); + $screen = get_current_screen(); + + add_meta_box( 'testbox-17704', 'Test Meta Box', '__return_false', $screen ); + + $screen->show_screen_options(); + + $this->assertTrue( + wp_script_is( 'postbox', 'enqueued' ), + 'The postbox script should be enqueued when meta boxes are registered for the screen.' + ); + + remove_meta_box( 'testbox-17704', $screen, 'advanced' ); + wp_dequeue_script( 'postbox' ); + } + + /** + * @ticket 17704 + */ + public function test_show_screen_options_does_not_enqueue_postbox_script_when_no_meta_boxes() { + global $wp_meta_boxes; + + set_current_screen( 'options-general.php' ); + $screen = get_current_screen(); + + unset( $wp_meta_boxes[ $screen->id ] ); + + $screen->show_screen_options(); + + $this->assertFalse( + wp_script_is( 'postbox', 'enqueued' ), + 'The postbox script should not be enqueued when no meta boxes are registered for the screen.' + ); + } }