Make WordPress Core

Ticket #23253: 23253.diff

File 23253.diff, 3.6 KB (added by gazchap, 4 years ago)

Suggested patch incorporating a do_settings_section() function

  • wp-admin/includes/template.php

     
    16671667 * add_settings_section() and add_settings_field()
    16681668 *
    16691669 * @global array $wp_settings_sections Storage array of all settings sections added to admin pages.
    1670  * @global array $wp_settings_fields Storage array of settings fields and info about their pages/sections.
    16711670 * @since 2.7.0
    16721671 *
    16731672 * @param string $page The slug name of the page whose settings sections you want to output.
    16741673 */
    16751674function do_settings_sections( $page ) {
    1676         global $wp_settings_sections, $wp_settings_fields;
     1675        global $wp_settings_sections;
    16771676
    16781677        if ( ! isset( $wp_settings_sections[ $page ] ) ) {
    16791678                return;
     
    16801679        }
    16811680
    16821681        foreach ( (array) $wp_settings_sections[ $page ] as $section ) {
    1683                 if ( $section['title'] ) {
    1684                         echo "<h2>{$section['title']}</h2>\n";
    1685                 }
     1682                render_settings_section( $page, $section );
     1683        }
     1684}
    16861685
    1687                 if ( $section['callback'] ) {
    1688                         call_user_func( $section['callback'], $section );
    1689                 }
     1686/**
     1687 * Prints out a particular settings section added to a particular settings page
     1688 *
     1689 * Part of the Settings API. Use this in a settings page callback function
     1690 * to output all the fields that were added into the $section_to_find for that $page with
     1691 * add_settings_section() and add_settings_field()
     1692 *
     1693 * @global array $wp_settings_sections Storage array of all settings sections added to admin pages.
     1694 *
     1695 * @param string $page The slug name of the page whose settings sections you want to output.
     1696 * @param string $section_to_find The id of the section on $page you want to output.
     1697 */
     1698function do_settings_section( $page, $section_to_find ) {
     1699        global $wp_settings_sections;
    16901700
    1691                 if ( ! isset( $wp_settings_fields ) || ! isset( $wp_settings_fields[ $page ] ) || ! isset( $wp_settings_fields[ $page ][ $section['id'] ] ) ) {
    1692                         continue;
     1701        if ( ! isset( $wp_settings_sections[ $page ] ) ) {
     1702                return;
     1703        }
     1704
     1705        $found_section = false;
     1706        foreach ( (array) $wp_settings_sections[ $page ] as $section ) {
     1707                if ( $section['id'] == $section_to_find ) {
     1708                        $found_section = true;
     1709                        break;
    16931710                }
    1694                 echo '<table class="form-table" role="presentation">';
    1695                 do_settings_fields( $page, $section['id'] );
    1696                 echo '</table>';
    16971711        }
     1712
     1713        if ( ! $found_section ) {
     1714                return;
     1715        }
     1716
     1717        render_settings_section( $page, $section );
    16981718}
    16991719
    17001720/**
     1721 * Prints out a particular settings section added to a particular settings page
     1722 *
     1723 * Used by do_settings_sections() and do_settings_section() but not intended for public use
     1724 *
     1725 * @global array $wp_settings_fields Storage array of settings fields and info about their pages/sections.
     1726 *
     1727 * @param string $page The slug name of the page whose settings sections you want to output.
     1728 * @param array $section The section array from $wp_settings_sections to be output.
     1729 */
     1730function render_settings_section( $page, $section ) {
     1731        global $wp_settings_fields;
     1732
     1733        if ( ! is_array( $section ) || ! isset( $section['id'] ) ) {
     1734                return;
     1735        }
     1736
     1737        if ( $section['title'] ) {
     1738                echo "<h2>{$section['title']}</h2>\n";
     1739        }
     1740
     1741        if ( $section['callback'] ) {
     1742                call_user_func( $section['callback'], $section );
     1743        }
     1744
     1745        if ( ! isset( $wp_settings_fields ) || ! isset( $wp_settings_fields[ $page ] ) || ! isset( $wp_settings_fields[ $page ][ $section['id'] ] ) ) {
     1746                return;
     1747        }
     1748        echo '<table class="form-table" role="presentation">';
     1749        do_settings_fields( $page, $section['id'] );
     1750        echo '</table>';
     1751}
     1752
     1753/**
    17011754 * Print out the settings fields for a particular settings section.
    17021755 *
    17031756 * Part of the Settings API. Use this in a settings page to output