← All changes
|
jetpack_vendor/automattic/jetpack-videopress/src/class-utils.php
+101
-0
16.2-beta
→
16.3-beta
View file →
| @@ -1,0 +1,101 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * The Utils class. | |
| 4 | + * | |
| 5 | + * @package automattic/jetpack-videopress | |
| 6 | + */ | |
| 7 | + | |
| 8 | +namespace Automattic\Jetpack\VideoPress; | |
| 9 | + | |
| 10 | +/** | |
| 11 | + * The Utils class. | |
| 12 | + */ | |
| 13 | +class Utils { | |
| 14 | + /** | |
| 15 | + * Build a VideoPress video URL based on the guid and block attributes. | |
| 16 | + * | |
| 17 | + * @param string|null $guid - Video GUID; null returns null. | |
| 18 | + * @param array $attributes - Video block attributes. Default is an empty array. | |
| 19 | + * | |
| 20 | + * @return string|null VideoPress video URL with the specified attributes, or null without a guid. | |
| 21 | + */ | |
| 22 | + public static function get_video_press_url( $guid, $attributes = array() ) { | |
| 23 | + if ( ! $guid ) { | |
| 24 | + return null; | |
| 25 | + } | |
| 26 | + | |
| 27 | + $video_press_url_options = wp_parse_args( | |
| 28 | + $attributes, | |
| 29 | + array( | |
| 30 | + 'autoplay' => false, | |
| 31 | + 'controls' => true, | |
| 32 | + 'loop' => false, | |
| 33 | + 'muted' => false, | |
| 34 | + 'playsinline' => false, | |
| 35 | + 'poster' => '', | |
| 36 | + 'preload' => 'metadata', | |
| 37 | + 'seekbarColor' => '', | |
| 38 | + 'seekbarPlayedColor' => '', | |
| 39 | + 'seekbarLoadingColor' => '', | |
| 40 | + 'useAverageColor' => true, | |
| 41 | + ) | |
| 42 | + ); | |
| 43 | + | |
| 44 | + // The site-wide opt-out wins over the block's own preload attribute. | |
| 45 | + if ( Data::get_videopress_player_preload_disabled() ) { | |
| 46 | + $video_press_url_options['preload'] = 'none'; | |
| 47 | + } | |
| 48 | + | |
| 49 | + $query_args = array( | |
| 50 | + 'resizeToParent' => 1, | |
| 51 | + 'cover' => 1, | |
| 52 | + 'autoPlay' => (int) $video_press_url_options['autoplay'], | |
| 53 | + 'controls' => (int) $video_press_url_options['controls'], | |
| 54 | + 'loop' => (int) $video_press_url_options['loop'], | |
| 55 | + 'muted' => (int) $video_press_url_options['muted'], | |
| 56 | + 'persistVolume' => $video_press_url_options['muted'] ? 0 : 1, | |
| 57 | + 'playsinline' => (int) $video_press_url_options['playsinline'], | |
| 58 | + 'preloadContent' => $video_press_url_options['preload'], | |
| 59 | + 'sbc' => $video_press_url_options['seekbarColor'], | |
| 60 | + 'sbpc' => $video_press_url_options['seekbarPlayedColor'], | |
| 61 | + 'sblc' => $video_press_url_options['seekbarLoadingColor'], | |
| 62 | + 'useAverageColor' => (int) $video_press_url_options['useAverageColor'], | |
| 63 | + ); | |
| 64 | + | |
| 65 | + if ( ! empty( $video_press_url_options['poster'] ) ) { | |
| 66 | + $query_args['posterUrl'] = rawurlencode( $video_press_url_options['poster'] ); | |
| 67 | + } | |
| 68 | + | |
| 69 | + $url = 'https://videopress.com/v/' . $guid; | |
| 70 | + return add_query_arg( $query_args, $url ); | |
| 71 | + } | |
| 72 | + | |
| 73 | + /** | |
| 74 | + * Determines if a given URL is a VideoPress URL. | |
| 75 | + * | |
| 76 | + * @since x.x.x | |
| 77 | + * | |
| 78 | + * @param mixed $url The URL to check. Non-strings return false. | |
| 79 | + * @return bool True if the URL is a VideoPress URL, false otherwise. | |
| 80 | + */ | |
| 81 | + public static function is_videopress_url( $url ) { | |
| 82 | + return null !== self::extract_videopress_guid_from_url( $url ); | |
| 83 | + } | |
| 84 | + | |
| 85 | + /** | |
| 86 | + * Extract the VideoPress guid from a recognised VideoPress URL. | |
| 87 | + * | |
| 88 | + * @param mixed $url The URL to inspect. Non-strings return null. | |
| 89 | + * @return string|null The 8-character guid, or null if the URL is not a recognised VideoPress URL. | |
| 90 | + */ | |
| 91 | + public static function extract_videopress_guid_from_url( $url ) { | |
| 92 | + if ( ! is_string( $url ) ) { | |
| 93 | + return null; | |
| 94 | + } | |
| 95 | + $pattern = '/^https?:\/\/(?:(?:v(?:ideo)?\.wordpress\.com|videopress\.com)\/(?:v|embed)|v\.wordpress\.com|videos\.(?:videopress\.com|files\.wordpress\.com))\/([a-z\d]{8})(\/|\b)/i'; // phpcs:ignore WordPress.WP.CapitalPDangit.MisspelledInText | |
| 96 | + if ( preg_match( $pattern, $url, $matches ) ) { | |
| 97 | + return $matches[1]; | |
| 98 | + } | |
| 99 | + return null; | |
| 100 | + } | |
| 101 | +} | |