WordPress.org

Make WordPress Core

Ticket #38738: 38738.4.diff

File 38738.4.diff, 4.0 KB (added by peterwilsoncc, 2 years ago)
  • src/wp-includes/theme.php

    diff --git src/wp-includes/theme.php src/wp-includes/theme.php
    index 865f851..9bf9bd0 100644
    function get_header_video_settings() { 
    14001400 * @return bool True if a custom header is set. False if not.
    14011401 */
    14021402function has_custom_header() {
    1403         if ( has_header_image() || ( is_front_page() && has_header_video() ) ) {
     1403        if ( has_header_image() || ( has_header_video() && is_header_video_active() ) ) {
    14041404                return true;
    14051405        }
    14061406
    function has_custom_header() { 
    14081408}
    14091409
    14101410/**
     1411 * Checks whether the custom header video is eligible to show on the current page.
     1412 *
     1413 * @since 4.7.0
     1414 *
     1415 * @return bool True if the custom header video should be shown. False if not.
     1416 */
     1417function is_header_video_active() {
     1418        if ( ! get_theme_support( 'custom-header', 'video' ) ) {
     1419                return false;
     1420        }
     1421
     1422        $video_active_cb = get_theme_support( 'custom-header', 'video-active-callback' );
     1423
     1424        if ( empty( $video_active_cb ) || ! is_callable( $video_active_cb ) ) {
     1425                $show_video = true;
     1426        } else {
     1427                $show_video = call_user_func( $video_active_cb );
     1428        }
     1429
     1430        /**
     1431         * Modify whether the custom header video is eligible to show on the current page.
     1432         *
     1433         * @since 4.7.0
     1434         *
     1435         * @param bool $show_video Whether the custom header video should be shown. Defaults to the value of the theme setting for the `custom-header`'s `video-active-callback`. If no callback is set, the default value is that of `is_front_page()`.
     1436         */
     1437        return apply_filters( 'is_header_video_active', $show_video );
     1438}
     1439
     1440/**
    14111441 * Retrieve the markup for a custom header.
    14121442 *
    14131443 * The container div will always be returned in the Customizer preview.
    function the_custom_header_markup() { 
    14421472
    14431473        echo $custom_header;
    14441474
    1445         if ( is_front_page() && ( has_header_video() || is_customize_preview() ) ) {
     1475        if ( is_header_video_active() && ( has_header_video() || is_customize_preview() ) ) {
    14461476                wp_enqueue_script( 'wp-custom-header' );
    14471477                wp_localize_script( 'wp-custom-header', '_wpCustomHeaderSettings', get_header_video_settings() );
    14481478        }
    function add_theme_support( $feature ) { 
    20582088                                'admin-head-callback' => '',
    20592089                                'admin-preview-callback' => '',
    20602090                                'video' => false,
     2091                                'video-active-callback' => 'is_front_page',
    20612092                        );
    20622093
    20632094                        $jit = isset( $args[0]['__jit'] );
  • tests/phpunit/tests/theme/support.php

    diff --git tests/phpunit/tests/theme/support.php tests/phpunit/tests/theme/support.php
    index 648f031..3b148f8 100644
    class Tests_Theme_Support extends WP_UnitTestCase { 
    191191                $this->assertEmpty( get_registered_nav_menus() );
    192192                $this->assertFalse( current_theme_supports( 'menus' ) );
    193193        }
     194
     195        /**
     196         * @ticket 38738
     197         */
     198        function test_video_header_callback() {
     199                // Back up theme features global to avoid poisoning later tests.
     200                global $_wp_theme_features;
     201                $_wp_theme_features_backup = $_wp_theme_features;
     202
     203                add_theme_support( 'custom-header', array(
     204                        'video' => true,
     205                        '__jit' => true, // Force refresh.
     206                ) );
     207
     208                $p = self::factory()->post->create( array(
     209                        'post_status' => 'publish',
     210                ) );
     211
     212                $this->go_to( home_url() );
     213                $this->assertTrue( is_header_video_active() );
     214
     215                $this->go_to( get_permalink( $p ) );
     216                $this->assertSame( false, is_header_video_active() );
     217
     218                /*
     219                 * Change callback to __return_true().
     220                 *
     221                 * Support must be removed first, otherwise the previously set
     222                 * values will override the new values.
     223                 */
     224                remove_theme_support( 'custom-header' );
     225                add_theme_support( 'custom-header', array(
     226                        'video' => true,
     227                        'video-active-callback' => '__return_true',
     228                        '__jit' => true, // Force refresh.
     229                ) );
     230
     231                $this->assertTrue( is_header_video_active() );
     232
     233                // Now check the filter works as expected.
     234                add_filter( 'is_header_video_active', '__return_false' );
     235                $this->assertSame( false, is_header_video_active() );
     236                remove_filter( 'is_header_video_active', '__return_false' );
     237
     238                // Restore theme features global to initial state.
     239                $_wp_theme_features = $_wp_theme_features_backup;
     240        }
    194241}