WordPress.org

Make WordPress Core

Ticket #21676: 21676.13.diff

File 21676.13.diff, 11.1 KB (added by SergeyBiryukov, 15 months ago)
  • src/wp-includes/general-template.php

     
    1717 *
    1818 * @since 1.5.0
    1919 * @since 5.5.0 A return value was added.
     20 * @since 5.5.0 The `$args` parameter was added.
    2021 *
    2122 * @param string $name The name of the specialised header.
     23 * @param array  $args Optional. Additional arguments passed to the header template.
     24 *                     Default empty array.
    2225 * @return void|false Void on success, false if the template does not exist.
    2326 */
    24 function get_header( $name = null ) {
     27function get_header( $name = null, $args = array() ) {
    2528        /**
    2629         * Fires before the header template file is loaded.
    2730         *
    2831         * @since 2.1.0
    29          * @since 2.8.0 $name parameter added.
     32         * @since 2.8.0 The `$name` parameter was added.
     33         * @since 5.5.0 The `$args` parameter was added.
    3034         *
    31          * @param string|null $name Name of the specific header file to use. null for the default header.
     35         * @param string|null $name Name of the specific header file to use. Null for the default header.
     36         * @param array       $args Additional arguments passed to the header template.
    3237         */
    33         do_action( 'get_header', $name );
     38        do_action( 'get_header', $name, $args );
    3439
    3540        $templates = array();
    3641        $name      = (string) $name;
     
    4045
    4146        $templates[] = 'header.php';
    4247
    43         if ( ! locate_template( $templates, true ) ) {
     48        if ( ! locate_template( $templates, true, true, $args ) ) {
    4449                return false;
    4550        }
    4651}
     
    5661 *
    5762 * @since 1.5.0
    5863 * @since 5.5.0 A return value was added.
     64 * @since 5.5.0 The `$args` parameter was added.
    5965 *
    6066 * @param string $name The name of the specialised footer.
     67 * @param array  $args Optional. Additional arguments passed to the footer template.
     68 *                     Default empty array.
    6169 * @return void|false Void on success, false if the template does not exist.
    6270 */
    63 function get_footer( $name = null ) {
     71function get_footer( $name = null, $args = array() ) {
    6472        /**
    6573         * Fires before the footer template file is loaded.
    6674         *
    6775         * @since 2.1.0
    68          * @since 2.8.0 $name parameter added.
     76         * @since 2.8.0 The `$name` parameter was added.
     77         * @since 5.5.0 The `$args` parameter was added.
    6978         *
    70          * @param string|null $name Name of the specific footer file to use. null for the default footer.
     79         * @param string|null $name Name of the specific footer file to use. Null for the default footer.
     80         * @param array       $args Additional arguments passed to the footer template.
    7181         */
    72         do_action( 'get_footer', $name );
     82        do_action( 'get_footer', $name, $args );
    7383
    7484        $templates = array();
    7585        $name      = (string) $name;
     
    7989
    8090        $templates[] = 'footer.php';
    8191
    82         if ( ! locate_template( $templates, true ) ) {
     92        if ( ! locate_template( $templates, true, true, $args ) ) {
    8393                return false;
    8494        }
    8595}
     
    95105 *
    96106 * @since 1.5.0
    97107 * @since 5.5.0 A return value was added.
     108 * @since 5.5.0 The `$args` parameter was added.
    98109 *
    99110 * @param string $name The name of the specialised sidebar.
     111 * @param array  $args Optional. Additional arguments passed to the sidebar template.
     112 *                     Default empty array.
    100113 * @return void|false Void on success, false if the template does not exist.
    101114 */
    102 function get_sidebar( $name = null ) {
     115function get_sidebar( $name = null, $args = array() ) {
    103116        /**
    104117         * Fires before the sidebar template file is loaded.
    105118         *
    106119         * @since 2.2.0
    107          * @since 2.8.0 $name parameter added.
     120         * @since 2.8.0 The `$name` parameter was added.
     121         * @since 5.5.0 The `$args` parameter was added.
    108122         *
    109          * @param string|null $name Name of the specific sidebar file to use. null for the default sidebar.
     123         * @param string|null $name Name of the specific sidebar file to use. Null for the default sidebar.
     124         * @param array       $args Additional arguments passed to the sidebar template.
    110125         */
    111         do_action( 'get_sidebar', $name );
     126        do_action( 'get_sidebar', $name, $args );
    112127
    113128        $templates = array();
    114129        $name      = (string) $name;
     
    118133
    119134        $templates[] = 'sidebar.php';
    120135
    121         if ( ! locate_template( $templates, true ) ) {
     136        if ( ! locate_template( $templates, true, true, $args ) ) {
    122137                return false;
    123138        }
    124139}
     
    141156 *
    142157 * @since 3.0.0
    143158 * @since 5.5.0 A return value was added.
     159 * @since 5.5.0 The `$args` parameter was added.
    144160 *
    145161 * @param string $slug The slug name for the generic template.
    146162 * @param string $name The name of the specialised template.
     163 * @param array  $args Optional. Additional arguments passed to the template.
     164 *                     Default empty array.
    147165 * @return void|false Void on success, false if the template does not exist.
    148166 */
    149 function get_template_part( $slug, $name = null ) {
     167function get_template_part( $slug, $name = null, $args = array() ) {
    150168        /**
    151169         * Fires before the specified template part file is loaded.
    152170         *
     
    154172         * for the generic template part.
    155173         *
    156174         * @since 3.0.0
     175         * @since 5.5.0 The `$args` parameter was added.
    157176         *
    158177         * @param string      $slug The slug name for the generic template.
    159178         * @param string|null $name The name of the specialized template.
     179         * @param array       $args Additional arguments passed to the template.
    160180         */
    161         do_action( "get_template_part_{$slug}", $slug, $name );
     181        do_action( "get_template_part_{$slug}", $slug, $name, $args );
    162182
    163183        $templates = array();
    164184        $name      = (string) $name;
     
    172192         * Fires before a template part is loaded.
    173193         *
    174194         * @since 5.2.0
     195         * @since 5.5.0 The `$args` parameter was added.
    175196         *
    176197         * @param string   $slug      The slug name for the generic template.
    177198         * @param string   $name      The name of the specialized template.
    178199         * @param string[] $templates Array of template files to search for, in order.
     200         * @param array    $args      Additional arguments passed to the template.
    179201         */
    180         do_action( 'get_template_part', $slug, $name, $templates );
     202        do_action( 'get_template_part', $slug, $name, $templates, $args );
    181203
    182         if ( ! locate_template( $templates, true, false ) ) {
     204        if ( ! locate_template( $templates, true, false, $args ) ) {
    183205                return false;
    184206        }
    185207}
     
    202224 * search. To give a few examples of what it can be used for.
    203225 *
    204226 * @since 2.7.0
    205  * @since 5.2.0 The $args array parameter was added in place of an $echo boolean flag.
     227 * @since 5.2.0 The `$args` array parameter was added in place of an `$echo` boolean flag.
    206228 *
    207229 * @param array $args {
    208230 *     Optional. Array of display arguments.
     
    220242         *
    221243         * @since 2.7.0 as 'get_search_form' action.
    222244         * @since 3.6.0
     245         * @since 5.5.0 The `$args` parameter was added.
    223246         *
    224247         * @link https://core.trac.wordpress.org/ticket/19321
     248         *
     249         * @param array $args The array of arguments for building the search form.
    225250         */
    226         do_action( 'pre_get_search_form' );
     251        do_action( 'pre_get_search_form', $args );
    227252
    228253        $echo = true;
    229254
     
    262287         * Filters the HTML format of the search form.
    263288         *
    264289         * @since 3.6.0
     290         * @since 5.5.0 The `$args` parameter was added.
    265291         *
    266292         * @param string $format The type of markup to use in the search form.
    267293         *                       Accepts 'html5', 'xhtml'.
     294         * @param array  $args   The array of arguments for building the search form.
    268295         */
    269         $format = apply_filters( 'search_form_format', $format );
     296        $format = apply_filters( 'search_form_format', $format, $args );
    270297
    271298        $search_form_template = locate_template( 'searchform.php' );
    272299
     
    308335         * Filters the HTML output of the search form.
    309336         *
    310337         * @since 2.7.0
     338         * @since 5.5.0 The `$args` parameter was added.
    311339         *
    312340         * @param string $form The search form HTML output.
     341         * @param array  $args The array of arguments for building the search form.
    313342         */
    314         $result = apply_filters( 'get_search_form', $form );
     343        $result = apply_filters( 'get_search_form', $form, $args );
    315344
    316345        if ( null === $result ) {
    317346                $result = $form;
  • src/wp-includes/template.php

     
    644644 * so that themes which inherit from a parent theme can just overload one file.
    645645 *
    646646 * @since 2.7.0
     647 * @since 5.5.0 The `$args` parameter was added.
    647648 *
    648649 * @param string|array $template_names Template file(s) to search for, in order.
    649650 * @param bool         $load           If true the template file will be loaded if it is found.
    650  * @param bool         $require_once   Whether to require_once or require. Default true. Has no effect if $load is false.
     651 * @param bool         $require_once   Whether to require_once or require. Has no effect if `$load` is false.
     652 *                                     Default true.
     653 * @param array        $args           Optional. Additional arguments passed to the template.
     654 *                                     Default empty array.
    651655 * @return string The template filename if one is located.
    652656 */
    653 function locate_template( $template_names, $load = false, $require_once = true ) {
     657function locate_template( $template_names, $load = false, $require_once = true, $args = array() ) {
    654658        $located = '';
    655659        foreach ( (array) $template_names as $template_name ) {
    656660                if ( ! $template_name ) {
     
    669673        }
    670674
    671675        if ( $load && '' !== $located ) {
    672                 load_template( $located, $require_once );
     676                load_template( $located, $require_once, $args );
    673677        }
    674678
    675679        return $located;
     
    683687 * also available.
    684688 *
    685689 * @since 1.5.0
     690 * @since 5.5.0 The `$args` parameter was added.
    686691 *
    687692 * @global array      $posts
    688693 * @global WP_Post    $post          Global post object.
     
    698703 *
    699704 * @param string $_template_file Path to template file.
    700705 * @param bool   $require_once   Whether to require_once or require. Default true.
     706 * @param array  $args           Optional. Additional arguments passed to the template.
     707 *                               Default empty array.
    701708 */
    702 function load_template( $_template_file, $require_once = true ) {
     709function load_template( $_template_file, $require_once = true, $args = array() ) {
    703710        global $posts, $post, $wp_did_header, $wp_query, $wp_rewrite, $wpdb, $wp_version, $wp, $id, $comment, $user_ID;
    704711
    705712        if ( is_array( $wp_query->query_vars ) ) {
  • tests/phpunit/data/themedir1/default/template-part.php

     
    11Template Part
     2
     3<?php echo json_encode( $args ); ?>
  • tests/phpunit/tests/general/template.php

     
    678678        function test_get_template_part_returns_false_on_failure() {
    679679                $this->assertFalse( get_template_part( 'non-existing-template' ) );
    680680        }
     681
     682        /**
     683         * @ticket 21676
     684         */
     685        function test_get_template_part_passes_arguments_to_template() {
     686                $this->expectOutputRegex( '/{"foo":"baz"}/' );
     687
     688                get_template_part( 'template', 'part', array( 'foo' => 'baz' ) );
     689        }
    681690}