Skip to content

Commit

Permalink
Options, Meta APIs: Ensure after_section is printed for sections wi…
Browse files Browse the repository at this point in the history
…thout 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
  • Loading branch information
SergeyBiryukov committed Dec 28, 2024
1 parent 779ed48 commit 4a6b12b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
9 changes: 4 additions & 5 deletions src/wp-admin/includes/template.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 '<table class="form-table" role="presentation">';
do_settings_fields( $page, $section['id'] );
echo '</table>';
}
echo '<table class="form-table" role="presentation">';
do_settings_fields( $page, $section['id'] );
echo '</table>';

if ( '' !== $section['after_section'] ) {
echo wp_kses_post( $section['after_section'] );
Expand Down
24 changes: 24 additions & 0 deletions tests/phpunit/tests/admin/includesTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()`.
*
Expand Down

0 comments on commit 4a6b12b

Please sign in to comment.