Make WordPress Core


Ignore:
Timestamp:
06/28/2020 11:08:57 PM (5 years 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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.