WordPress.org

Make WordPress Core

Changeset 48209


Ignore:
Timestamp:
06/28/2020 11:08:57 PM (7 days ago)
Author:
SergeyBiryukov
Message:

Themes: Add a return value to theme functions calling locate_template():

  • get_header()
  • get_footer()
  • get_sidebar()
  • get_template_part()

These functions now return false if the template file could not be found, to allow for easier debugging.

Props tferry, sphakka, johnbillion, pento, davidbinda, desrosj, birgire, garrett-eclipse, williampatton, davidbaumwald, SergeyBiryukov.
Fixes #40969.

Location:
trunk
Files:
3 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/general-template.php

    r48199 r48209  
    1717 *
    1818 * @since 1.5.0
     19 * @since 5.5.0 A return value was added.
    1920 *
    2021 * @param string $name The name of the specialised header.
     22 * @return void|false Void on success, false if the template does not exist.
    2123 */
    2224function get_header( $name = null ) {
     
    3941    $templates[] = 'header.php';
    4042
    41     locate_template( $templates, true );
     43    if ( ! locate_template( $templates, true ) ) {
     44        return false;
     45    }
    4246}
    4347
     
    5256 *
    5357 * @since 1.5.0
     58 * @since 5.5.0 A return value was added.
    5459 *
    5560 * @param string $name The name of the specialised footer.
     61 * @return void|false Void on success, false if the template does not exist.
    5662 */
    5763function get_footer( $name = null ) {
     
    7480    $templates[] = 'footer.php';
    7581
    76     locate_template( $templates, true );
     82    if ( ! locate_template( $templates, true ) ) {
     83        return false;
     84    }
    7785}
    7886
     
    8795 *
    8896 * @since 1.5.0
     97 * @since 5.5.0 A return value was added.
    8998 *
    9099 * @param string $name The name of the specialised sidebar.
     100 * @return void|false Void on success, false if the template does not exist.
    91101 */
    92102function get_sidebar( $name = null ) {
     
    109119    $templates[] = 'sidebar.php';
    110120
    111     locate_template( $templates, true );
     121    if ( ! locate_template( $templates, true ) ) {
     122        return false;
     123    }
    112124}
    113125
     
    129141 *
    130142 * @since 3.0.0
     143 * @since 5.5.0 A return value was added.
    131144 *
    132145 * @param string $slug The slug name for the generic template.
    133146 * @param string $name The name of the specialised template.
     147 * @return void|false Void on success, false if the template does not exist.
    134148 */
    135149function get_template_part( $slug, $name = null ) {
     
    166180    do_action( 'get_template_part', $slug, $name, $templates );
    167181
    168     locate_template( $templates, true, false );
     182    if ( ! locate_template( $templates, true, false ) ) {
     183        return false;
     184    }
    169185}
    170186
  • trunk/tests/phpunit/tests/general/template.php

    r47288 r48209  
    632632     * @ticket 40969
    633633     */
    634     function test_get_template_part_returns_nothing() {
    635         ob_start();
     634    function test_get_header_returns_nothing_on_success() {
     635        $this->expectOutputRegex( '/Header/' );
     636
     637        // The `get_header()` function must not return anything
     638        // due to themes in the wild that may echo its return value.
     639        $this->assertNull( get_header() );
     640    }
     641
     642    /**
     643     * @ticket 40969
     644     */
     645    function test_get_footer_returns_nothing_on_success() {
     646        $this->expectOutputRegex( '/Footer/' );
     647
     648        // The `get_footer()` function must not return anything
     649        // due to themes in the wild that may echo its return value.
     650        $this->assertNull( get_footer() );
     651    }
     652
     653    /**
     654     * @ticket 40969
     655     */
     656    function test_get_sidebar_returns_nothing_on_success() {
     657        $this->expectOutputRegex( '/Sidebar/' );
     658
     659        // The `get_sidebar()` function must not return anything
     660        // due to themes in the wild that may echo its return value.
     661        $this->assertNull( get_sidebar() );
     662    }
     663
     664    /**
     665     * @ticket 40969
     666     */
     667    function test_get_template_part_returns_nothing_on_success() {
     668        $this->expectOutputRegex( '/Template Part/' );
    636669
    637670        // The `get_template_part()` function must not return anything
    638671        // due to themes in the wild that echo its return value.
    639         $part   = get_template_part( 'template', 'part' );
    640         $output = ob_get_clean();
    641 
    642         self::assertSame( 'Template Part', trim( $output ) );
    643         self::assertSame( null, $part );
     672        $this->assertNull( get_template_part( 'template', 'part' ) );
     673    }
     674
     675    /**
     676     * @ticket 40969
     677     */
     678    function test_get_template_part_returns_false_on_failure() {
     679        $this->assertFalse( get_template_part( 'non-existing-template' ) );
    644680    }
    645681}
Note: See TracChangeset for help on using the changeset viewer.