| | 1315 | * Check whether a header video is set or not. |
| | 1316 | * |
| | 1317 | * @since 4.7.0 |
| | 1318 | * |
| | 1319 | * @see get_header_video() |
| | 1320 | * |
| | 1321 | * @return bool Whether a header video is set or not. |
| | 1322 | */ |
| | 1323 | function has_header_video() { |
| | 1324 | return (bool) get_header_video(); |
| | 1325 | } |
| | 1326 | |
| | 1327 | /* Retrieve header video for custom header. |
| | 1328 | * |
| | 1329 | * @since 4.7.0 |
| | 1330 | * |
| | 1331 | * @return string|false |
| | 1332 | */ |
| | 1333 | function get_header_video() { |
| | 1334 | $url = get_theme_mod( 'header_video' ); |
| | 1335 | |
| | 1336 | if ( 'remove-header' == $url ) |
| | 1337 | return false; |
| | 1338 | |
| | 1339 | if ( $url ) { |
| | 1340 | // We have an attachment ID, need the full URL |
| | 1341 | $url = wp_get_attachment_url( $url ); |
| | 1342 | } |
| | 1343 | |
| | 1344 | return esc_url_raw( set_url_scheme( $url ) ); |
| | 1345 | } |
| | 1346 | |
| | 1347 | /** |
| | 1348 | * Create video tag markup for a custom header video. |
| | 1349 | * |
| | 1350 | * @since 4.7.0 |
| | 1351 | * |
| | 1352 | * @param array $attr Optional. Additional attributes for the image tag. Can be used |
| | 1353 | * to override the default attributes. Default empty. |
| | 1354 | * @return string HTML image element markup or empty string on failure. |
| | 1355 | */ |
| | 1356 | function get_header_video_tag( $attr = array() ) { |
| | 1357 | $header = get_custom_header(); |
| | 1358 | $video = get_header_video(); |
| | 1359 | $image = get_header_image(); |
| | 1360 | |
| | 1361 | if ( empty( $video ) ) { |
| | 1362 | return ''; |
| | 1363 | } |
| | 1364 | |
| | 1365 | $width = absint( $header->width ); |
| | 1366 | $height = absint( $header->height ); |
| | 1367 | |
| | 1368 | $attr = wp_parse_args( |
| | 1369 | $attr, |
| | 1370 | array( |
| | 1371 | 'src' => get_header_video(), |
| | 1372 | 'width' => $width, |
| | 1373 | 'height' => $height, |
| | 1374 | 'controls' => '', |
| | 1375 | 'autoplay' => '', |
| | 1376 | 'loop' => '', |
| | 1377 | 'muted' => '', |
| | 1378 | ) |
| | 1379 | ); |
| | 1380 | |
| | 1381 | // Use the header image as poster attribute. |
| | 1382 | if ( ! empty( $image ) ) { |
| | 1383 | $attr['poster'] = $header->url; |
| | 1384 | } |
| | 1385 | |
| | 1386 | $attr = array_map( 'esc_attr', $attr ); |
| | 1387 | $html = '<video'; |
| | 1388 | |
| | 1389 | foreach ( $attr as $name => $value ) { |
| | 1390 | $html .= ' ' . $name . '="' . $value . '"'; |
| | 1391 | } |
| | 1392 | |
| | 1393 | $html .= '></video>'; |
| | 1394 | |
| | 1395 | /** |
| | 1396 | * Filters the markup of header videos. |
| | 1397 | * |
| | 1398 | * @since 4.7.0 |
| | 1399 | * |
| | 1400 | * @param string $html The HTML image tag markup being filtered. |
| | 1401 | * @param object $header The custom header object returned by 'get_custom_header()'. |
| | 1402 | * @param array $attr Array of the attributes for the image tag. |
| | 1403 | */ |
| | 1404 | return apply_filters( 'get_header_video_tag', $html, $header, $attr ); |
| | 1405 | } |
| | 1406 | |
| | 1407 | /** |
| | 1408 | * Display the video markup for a custom header video. |
| | 1409 | * |
| | 1410 | * @since 4.7.0 |
| | 1411 | * |
| | 1412 | * @param array $attr Optional. Attributes for the image markup. Default empty. |
| | 1413 | */ |
| | 1414 | function the_header_video_tag( $attr = array() ) { |
| | 1415 | echo get_header_video_tag( $attr ); |
| | 1416 | } |
| | 1417 | |
| | 1418 | /** |
| | 1419 | * Display header video URL. |
| | 1420 | * |
| | 1421 | * @since 4.7.0 |
| | 1422 | */ |
| | 1423 | function the_header_video() { |
| | 1424 | $video = get_header_video(); |
| | 1425 | if ( $video ) { |
| | 1426 | echo esc_url( $video ); |
| | 1427 | } |
| | 1428 | } |
| | 1429 | |
| | 1430 | /** |
| | 1431 | * Display the video and image markup for a custom header. |
| | 1432 | * |
| | 1433 | * @since 4.7.0 |
| | 1434 | */ |
| | 1435 | function the_custom_header() { |
| | 1436 | $video = get_header_video(); |
| | 1437 | $image = get_header_image(); |
| | 1438 | |
| | 1439 | if ( ! empty( $video ) ) { |
| | 1440 | the_header_video_tag(); |
| | 1441 | } elseif ( ! empty( $image ) ) { |
| | 1442 | the_header_image_tag(); |
| | 1443 | } |
| | 1444 | } |
| | 1445 | |
| | 1446 | /** |