Ticket #17851: 17851.5.patch
| File 17851.5.patch, 6.7 KB (added by , 3 years ago) |
|---|
-
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 456 456 ); 457 457 } 458 458 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 } 459 545 460 546 public function assertTemplateHierarchy( $url, array $expected, $message = '' ) { 461 547 $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 1561 1561 * fields. It can output nothing if you want. 1562 1562 * 1563 1563 * @since 2.7.0 1564 * @since 6.1.0 Added an `$args` parameter for the section's wrapper HTML and class name. 1564 1565 * 1565 1566 * @global array $wp_settings_sections Storage array of all settings sections added to admin pages. 1566 1567 * … … 1570 1571 * @param string $page The slug-name of the settings page on which to show the section. Built-in pages include 1571 1572 * 'general', 'reading', 'writing', 'discussion', 'media', etc. Create your own using 1572 1573 * 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 * } 1573 1582 */ 1574 function add_settings_section( $id, $title, $callback, $page ) {1583 function add_settings_section( $id, $title, $callback, $page, $args = array() ) { 1575 1584 global $wp_settings_sections; 1576 1585 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 1577 1614 if ( 'misc' === $page ) { 1578 1615 _deprecated_argument( 1579 1616 __FUNCTION__, … … 1600 1637 $page = 'reading'; 1601 1638 } 1602 1639 1603 $wp_settings_sections[ $page ][ $id ] = array( 1604 'id' => $id, 1605 'title' => $title, 1606 'callback' => $callback, 1607 ); 1640 $wp_settings_sections[ $page ][ $id ] = $section; 1608 1641 } 1609 1642 1610 1643 /** … … 1700 1733 } 1701 1734 1702 1735 foreach ( (array) $wp_settings_sections[ $page ] as $section ) { 1736 if ( '' !== $section['before_section'] ) { 1737 echo sprintf( $section['before_section'], $section['section_class'] ); 1738 } 1739 1703 1740 if ( $section['title'] ) { 1704 1741 echo "<h2>{$section['title']}</h2>\n"; 1705 1742 } … … 1714 1751 echo '<table class="form-table" role="presentation">'; 1715 1752 do_settings_fields( $page, $section['id'] ); 1716 1753 echo '</table>'; 1754 1755 if ( '' !== $section['after_section'] ) { 1756 echo $section['after_section']; 1757 } 1717 1758 } 1718 1759 } 1719 1760