From 4a6b12bea7761ec8f9037685aad3104285e15b21 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Sat, 28 Dec 2024 22:08:19 +0000 Subject: [PATCH] Options, Meta APIs: Ensure `after_section` is printed for sections without any fields. This brings consistency with the `before_section` HTML content, which did get printed in `do_settings_sections()` regardless of whether the settings section has any fields attached. Follow-up to [8855], [21742], [54247]. Props alpipego, SergeyBiryukov. Fixes #62746. git-svn-id: https://develop.svn.wordpress.org/trunk@59564 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-admin/includes/template.php | 9 ++++--- .../phpunit/tests/admin/includesTemplate.php | 24 +++++++++++++++++++ 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/wp-admin/includes/template.php b/src/wp-admin/includes/template.php index 3bff8bc279b5f..a0d07696d3019 100644 --- a/src/wp-admin/includes/template.php +++ b/src/wp-admin/includes/template.php @@ -1784,12 +1784,11 @@ function do_settings_sections( $page ) { call_user_func( $section['callback'], $section ); } - if ( ! isset( $wp_settings_fields ) || ! isset( $wp_settings_fields[ $page ] ) || ! isset( $wp_settings_fields[ $page ][ $section['id'] ] ) ) { - continue; + if ( isset( $wp_settings_fields[ $page ][ $section['id'] ] ) ) { + echo ''; + do_settings_fields( $page, $section['id'] ); + echo ''; } - echo ''; - do_settings_fields( $page, $section['id'] ); - echo ''; if ( '' !== $section['after_section'] ) { echo wp_kses_post( $section['after_section'] ); diff --git a/tests/phpunit/tests/admin/includesTemplate.php b/tests/phpunit/tests/admin/includesTemplate.php index f24a046933922..66e3befd5f07b 100644 --- a/tests/phpunit/tests/admin/includesTemplate.php +++ b/tests/phpunit/tests/admin/includesTemplate.php @@ -240,6 +240,30 @@ public function test_add_settings_section_with_extra_args( $extra_args, $expecte $this->assertStringContainsString( $expected_after_section_html, $output, 'Test page output does not contain the custom markup to be placed after the section.' ); } + /** + * @ticket 62746 + * + * @param array $extra_args Extra arguments to pass to function `add_settings_section()`. + * @param array $expected_section_data Expected set of section data. + * @param string $expected_before_section_html Expected HTML markup to be rendered before the settings section. + * @param string $expected_after_section_html Expected HTML markup to be rendered after the settings section. + * + * @covers ::add_settings_section + * @covers ::do_settings_sections + * + * @dataProvider data_extra_args_for_add_settings_section + */ + public function test_add_settings_section_without_any_fields( $extra_args, $expected_section_data, $expected_before_section_html, $expected_after_section_html ) { + add_settings_section( 'test-section', 'Section title', '__return_false', 'test-page', $extra_args ); + + ob_start(); + do_settings_sections( 'test-page' ); + $output = ob_get_clean(); + + $this->assertStringContainsString( $expected_before_section_html, $output, 'Test page output does not contain the custom markup to be placed before the section.' ); + $this->assertStringContainsString( $expected_after_section_html, $output, 'Test page output does not contain the custom markup to be placed after the section.' ); + } + /** * Data provider for `test_add_settings_section_with_extra_args()`. *