Ticket #38738: 38738.7.diff
File 38738.7.diff, 5.9 KB (added by , 4 years ago) |
---|
-
src/wp-includes/theme.php
1400 1400 * @return bool True if a custom header is set. False if not. 1401 1401 */ 1402 1402 function 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() ) ) { 1404 1404 return true; 1405 1405 } 1406 1406 … … 1408 1408 } 1409 1409 1410 1410 /** 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 */ 1417 function 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 /** 1411 1441 * Retrieve the markup for a custom header. 1412 1442 * 1413 1443 * The container div will always be returned in the Customizer preview. … … 1442 1472 1443 1473 echo $custom_header; 1444 1474 1445 if ( is_ front_page() && ( has_header_video() || is_customize_preview() ) ) {1475 if ( is_header_video_active() && ( has_header_video() || is_customize_preview() ) ) { 1446 1476 wp_enqueue_script( 'wp-custom-header' ); 1447 1477 wp_localize_script( 'wp-custom-header', '_wpCustomHeaderSettings', get_header_video_settings() ); 1448 1478 } … … 2058 2088 'admin-head-callback' => '', 2059 2089 'admin-preview-callback' => '', 2060 2090 'video' => false, 2091 'video-active-callback' => 'is_front_page', 2061 2092 ); 2062 2093 2063 2094 $jit = isset( $args[0]['__jit'] ); … … 2319 2350 if ( ! did_action( 'wp_loaded' ) ) 2320 2351 break; 2321 2352 $support = get_theme_support( 'custom-header' ); 2322 if ( $support[0]['wp-head-callback'] )2353 if ( isset( $support[0]['wp-head-callback'] ) ) { 2323 2354 remove_action( 'wp_head', $support[0]['wp-head-callback'] ); 2324 remove_action( 'admin_menu', array( $GLOBALS['custom_image_header'], 'init' ) ); 2325 unset( $GLOBALS['custom_image_header'] ); 2355 } 2356 if ( isset( $GLOBALS['custom_image_header'] ) ) { 2357 remove_action( 'admin_menu', array( $GLOBALS['custom_image_header'], 'init' ) ); 2358 unset( $GLOBALS['custom_image_header'] ); 2359 } 2326 2360 break; 2327 2361 2328 2362 case 'custom-background' : -
tests/phpunit/tests/theme/customHeader.php
1 <?php 2 /** 3 * @group themes 4 */ 5 class Tests_Theme_Custom_Header extends WP_UnitTestCase { 6 function tearDown() { 7 remove_theme_support( 'custom-header' ); 8 parent::tearDown(); 9 } 10 11 /** 12 * @ticket 38738 13 */ 14 function test_video_header_callback_front_page_from_front_page() { 15 $this->_add_theme_support( array( 16 'video' => true, 17 ) ); 18 19 $this->go_to( home_url() ); 20 21 $result = is_header_video_active(); 22 23 $this->assertTrue( $result ); 24 } 25 26 /** 27 * @ticket 38738 28 */ 29 function test_video_header_callback_front_page_from_elsewhere() { 30 $this->_add_theme_support( array( 31 'video' => true, 32 ) ); 33 34 $p = self::factory()->post->create( array( 35 'post_status' => 'publish', 36 ) ); 37 38 $this->go_to( get_permalink( $p ) ); 39 40 $result = is_header_video_active(); 41 42 $this->assertFalse( $result ); 43 } 44 45 /** 46 * @ticket 38738 47 */ 48 function test_video_header_callback_globally_from_front_page() { 49 $this->_add_theme_support( array( 50 'video' => true, 51 'video-active-callback' => '__return_true', 52 ) ); 53 54 $this->go_to( home_url() ); 55 56 $result = is_header_video_active(); 57 58 $this->assertTrue( $result ); 59 } 60 61 /** 62 * @ticket 38738 63 */ 64 function test_video_header_callback_globally_from_elsewhere() { 65 $this->_add_theme_support( array( 66 'video' => true, 67 'video-active-callback' => '__return_true', 68 ) ); 69 70 $p = self::factory()->post->create( array( 71 'post_status' => 'publish', 72 ) ); 73 74 $this->go_to( get_permalink( $p ) ); 75 76 $result = is_header_video_active(); 77 78 $this->assertTrue( $result ); 79 } 80 81 /** 82 * @ticket 38738 83 */ 84 function test_video_header_callback_globally_with_negative_filter() { 85 $this->_add_theme_support( array( 86 'video' => true, 87 'video-active-callback' => '__return_true', 88 ) ); 89 90 $p = self::factory()->post->create( array( 91 'post_status' => 'publish', 92 ) ); 93 94 $this->go_to( get_permalink( $p ) ); 95 96 add_filter( 'is_header_video_active', '__return_false' ); 97 $result = is_header_video_active(); 98 remove_filter( 'is_header_video_active', '__return_false' ); 99 100 $this->assertFalse( $result ); 101 } 102 103 /** 104 * Adds arguments directly to the $_wp_theme_features global. Calling 105 * add_theme_support( 'custom-header' ) will poison subsequent tests since 106 * it defines constants. 107 */ 108 function _add_theme_support( $args = array() ) { 109 global $_wp_theme_features; 110 111 $_wp_theme_features['custom-header'][0] = wp_parse_args( $args, array( 112 'default-image' => '', 113 'random-default' => false, 114 'width' => 0, 115 'height' => 0, 116 'flex-height' => false, 117 'flex-width' => false, 118 'default-text-color' => '', 119 'header-text' => true, 120 'uploads' => true, 121 'wp-head-callback' => '', 122 'admin-head-callback' => '', 123 'admin-preview-callback' => '', 124 'video' => false, 125 'video-active-callback' => 'is_front_page', 126 ) ); 127 } 128 }