Make WordPress Core

Ticket #42057: 42057.3.diff

File 42057.3.diff, 3.7 KB (added by williampatton, 6 years ago)

fixed issue with args array not being cast back after filters

  • wp-includes/general-template.php

    diff --git wp-includes/general-template.php wp-includes/general-template.php
    index 11d4b77ff4..52e1b662f7 100644
    function get_template_part( $slug, $name = null ) { 
    175175 * search. To give a few examples of what it can be used for.
    176176 *
    177177 * @since 2.7.0
     178 * @since 5.2.0 the $args array paramiter was added in place of an $echo boolean flag.
    178179 *
    179  * @param bool $echo Default to echo and not return the form.
    180  * @return string|void String when $echo is false.
     180 * @param array $args Default args are to echo and include no custom label.
    181181 */
    182 function get_search_form( $echo = true ) {
     182function get_search_form( $args = array() ) {
    183183        /**
    184184         * Fires before the search form is retrieved, at the start of get_search_form().
    185185         *
    function get_search_form( $echo = true ) { 
    192192
    193193        $format = current_theme_supports( 'html5', 'search-form' ) ? 'html5' : 'xhtml';
    194194
     195        /*
     196         * Back compat: to ensure previous uses of get_search_form continue to
     197         * function as expected we handle a value for $echo as though nothing has
     198         * changed. Then we deal with the $args array and cast it's defaults.
     199         */
     200        $echo = true;
     201        if ( false === $args ) {
     202                $echo = false;
     203        }
     204        if ( ! is_array( $args ) ) {
     205                // we can set an empty array and allow default args to take over.
     206                $args = array();
     207        }
     208
     209        // defaults are to echo and to output no custom label on the landmark.
     210        $defaults = array(
     211                'echo'  => $echo,
     212                'label' => false,
     213        );
     214
     215        // merge any args with the defaults.
     216        $args = wp_parse_args( $args, $defaults );
     217
     218        /**
     219         * Filters the array of args used when generating the search form.
     220         *
     221         * @since 5.2.0
     222         *
     223         * @param array $args The array of args used when building the search form.
     224         */
     225        $args = apply_filters( 'search_form_args', $args );
     226
    195227        /**
    196228         * Filters the HTML format of the search form.
    197229         *
    function get_search_form( $echo = true ) { 
    208240                require( $search_form_template );
    209241                $form = ob_get_clean();
    210242        } else {
     243                // form a string containaing a label to use for the search role.
     244                if ( $args['label'] ) {
     245                        $label = 'aria-label="' . esc_attr( $args['label'] ) . '" ';
     246                } else {
     247                        // if there is no custom label we can set a default here. ATM it is
     248                        // empty as there's uncertainty about what the default should be.
     249                        $label = '';
     250                }
    211251                if ( 'html5' == $format ) {
    212                         $form = '<form role="search" method="get" class="search-form" action="' . esc_url( home_url( '/' ) ) . '">
     252                        $form = '<form role="search" ' . $label . 'method="get" class="search-form" action="' . esc_url( home_url( '/' ) ) . '">
    213253                                <label>
    214254                                        <span class="screen-reader-text">' . _x( 'Search for:', 'label' ) . '</span>
    215255                                        <input type="search" class="search-field" placeholder="' . esc_attr_x( 'Search &hellip;', 'placeholder' ) . '" value="' . get_search_query() . '" name="s" />
    function get_search_form( $echo = true ) { 
    217257                                <input type="submit" class="search-submit" value="' . esc_attr_x( 'Search', 'submit button' ) . '" />
    218258                        </form>';
    219259                } else {
    220                         $form = '<form role="search" method="get" id="searchform" class="searchform" action="' . esc_url( home_url( '/' ) ) . '">
     260                        $form = '<form role="search" ' . $label . 'method="get" id="searchform" class="searchform" action="' . esc_url( home_url( '/' ) ) . '">
    221261                                <div>
    222262                                        <label class="screen-reader-text" for="s">' . _x( 'Search for:', 'label' ) . '</label>
    223263                                        <input type="text" value="' . get_search_query() . '" name="s" id="s" />
    function get_search_form( $echo = true ) { 
    240280                $result = $form;
    241281        }
    242282
    243         if ( $echo ) {
     283        if ( array_key_exists( 'echo', $args ) && $args['echo'] ) {
    244284                echo $result;
    245285        } else {
    246286                return $result;