Make WordPress Core

Ticket #17851: 17851.5.patch

File 17851.5.patch, 6.7 KB (added by martin.krcho, 3 years ago)

This is updated patch 17851.4.diff with added unit tests.

  • tests/phpunit/tests/template.php

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
    diff --git a/tests/phpunit/tests/template.php b/tests/phpunit/tests/template.php
    a b  
    456456                );
    457457        }
    458458
     459        /**
     460         * @ticket 17851
     461         * @covers ::add_settings_section
     462         */
     463        public function test_add_settings_section() {
     464                add_settings_section( 'test-section', 'Section title', '__return_false', 'test-page' );
     465
     466                global $wp_settings_sections;
     467                $this->assertIsArray( $wp_settings_sections );
     468                $this->assertArrayHasKey( 'test-page', $wp_settings_sections );
     469                $this->assertIsArray( $wp_settings_sections['test-page'] );
     470                $this->assertArrayHasKey( 'test-section', $wp_settings_sections['test-page'] );
     471
     472                $this->assertEqualSetsWithIndex(
     473                        array(
     474                                'id'             => 'test-section',
     475                                'title'          => 'Section title',
     476                                'callback'       => '__return_false',
     477                                'before_section' => '',
     478                                'after_section'  => '',
     479                                'section_class'  => '',
     480                        ),
     481                        $wp_settings_sections['test-page']['test-section']
     482                );
     483        }
     484
     485        /**
     486         * @ticket 17851
     487         * @covers ::add_settings_section
     488         * @covers ::do_settings_sections
     489         */
     490        public function test_add_settings_section_with_extra_args() {
     491                $args = array(
     492                        'before_section' => '<div class="%s">',
     493                        'after_section'  => '</div><!-- end of the test section -->',
     494                        'section_class'  => 'test-section-wrap',
     495                );
     496
     497                add_settings_section( 'test-section', 'Section title', '__return_false', 'test-page', $args );
     498                add_settings_field( 'test-field', 'Field title', '__return_false', 'test-page', 'test-section' );
     499
     500                global $wp_settings_sections;
     501                $this->assertIsArray( $wp_settings_sections );
     502                $this->assertArrayHasKey( 'test-page', $wp_settings_sections );
     503                $this->assertIsArray( $wp_settings_sections['test-page'] );
     504                $this->assertArrayHasKey( 'test-section', $wp_settings_sections['test-page'] );
     505
     506                $this->assertEqualSetsWithIndex(
     507                        array(
     508                                'id'             => 'test-section',
     509                                'title'          => 'Section title',
     510                                'callback'       => '__return_false',
     511                                'before_section' => '<div class="%s">',
     512                                'after_section'  => '</div><!-- end of the test section -->',
     513                                'section_class'  => 'test-section-wrap',
     514                        ),
     515                        $wp_settings_sections['test-page']['test-section']
     516                );
     517
     518                ob_start();
     519                do_settings_sections( 'test-page' );
     520                $output = ob_get_clean();
     521
     522                $this->assertStringContainsString( '<div class="test-section-wrap">', $output );
     523                $this->assertStringContainsString( '</div><!-- end of the test section -->', $output );
     524
     525        }
     526
     527        /**
     528         * @ticket 17851
     529         * @covers ::add_settings_section
     530         *
     531         * @expectedIncorrectUsage add_settings_section
     532         */
     533        public function test_add_settings_section_missing_section_class_placeholder() {
     534                $args = array(
     535                        'before_section' => '<div class="test-section-wrapper">',
     536                        'after_section'  => '</div><!-- end of the test section -->',
     537                        'section_class'  => 'test-section-wrap',
     538                );
     539
     540                add_settings_section( 'test-section', 'Section title', '__return_false', 'test-page', $args );
     541
     542                //https://make.wordpress.org/core/handbook/testing/automated-testing/writing-phpunit-tests/
     543
     544        }
    459545
    460546        public function assertTemplateHierarchy( $url, array $expected, $message = '' ) {
    461547                $this->go_to( $url );
  • src/wp-admin/includes/template.php

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
    diff --git a/src/wp-admin/includes/template.php b/src/wp-admin/includes/template.php
    a b  
    15611561 * fields. It can output nothing if you want.
    15621562 *
    15631563 * @since 2.7.0
     1564 * @since 6.1.0 Added an `$args` parameter for the section's wrapper HTML and class name.
    15641565 *
    15651566 * @global array $wp_settings_sections Storage array of all settings sections added to admin pages.
    15661567 *
     
    15701571 * @param string   $page     The slug-name of the settings page on which to show the section. Built-in pages include
    15711572 *                           'general', 'reading', 'writing', 'discussion', 'media', etc. Create your own using
    15721573 *                           add_options_page();
     1574 * @param array    $args     {
     1575 *     Arguments used to create the settings section.
     1576 *
     1577 *     @type string $before_section HTML content to prepend to the section's HTML output.
     1578 *                                  Receives the section's class name as `%s`. Default empty.
     1579 *     @type string $after_section  HTML content to append to the section's HTML output. Default empty.
     1580 *     @type string $section_class  The class name to use for the section. Default empty.
     1581 * }
    15731582 */
    1574 function add_settings_section( $id, $title, $callback, $page ) {
     1583function add_settings_section( $id, $title, $callback, $page, $args = array() ) {
    15751584        global $wp_settings_sections;
    15761585
     1586        $defaults = array(
     1587                'id'             => $id,
     1588                'title'          => $title,
     1589                'callback'       => $callback,
     1590                'before_section' => '',
     1591                'after_section'  => '',
     1592                'section_class'  => '',
     1593        );
     1594
     1595        $section = wp_parse_args( $args, $defaults );
     1596
     1597        if ( array_key_exists( 'before_section', $section )
     1598                && is_string( $section['before_section'] )
     1599                && strlen( $section['before_section'] ) > 0
     1600                && 1 !== preg_match( '/%s/', $section['before_section'] )
     1601        ) {
     1602                _doing_it_wrong(
     1603                        'add_settings_section',
     1604                        sprintf(
     1605                                /* translators: before_section */
     1606                                __( '%s argument must contain a placeholder for the section CSS class.' ),
     1607                                '<code>before_section</code>'
     1608                        ),
     1609                        '6.1.0'
     1610                );
     1611                return;
     1612        }
     1613
    15771614        if ( 'misc' === $page ) {
    15781615                _deprecated_argument(
    15791616                        __FUNCTION__,
     
    16001637                $page = 'reading';
    16011638        }
    16021639
    1603         $wp_settings_sections[ $page ][ $id ] = array(
    1604                 'id'       => $id,
    1605                 'title'    => $title,
    1606                 'callback' => $callback,
    1607         );
     1640        $wp_settings_sections[ $page ][ $id ] = $section;
    16081641}
    16091642
    16101643/**
     
    17001733        }
    17011734
    17021735        foreach ( (array) $wp_settings_sections[ $page ] as $section ) {
     1736                if ( '' !== $section['before_section'] ) {
     1737                        echo sprintf( $section['before_section'], $section['section_class'] );
     1738                }
     1739
    17031740                if ( $section['title'] ) {
    17041741                        echo "<h2>{$section['title']}</h2>\n";
    17051742                }
     
    17141751                echo '<table class="form-table" role="presentation">';
    17151752                do_settings_fields( $page, $section['id'] );
    17161753                echo '</table>';
     1754
     1755                if ( '' !== $section['after_section'] ) {
     1756                        echo $section['after_section'];
     1757                }
    17171758        }
    17181759}
    17191760