← All changes
|
jetpack_vendor/automattic/jetpack-videopress/src/class-utils.php
+27
-6
12.9.5
→
16.3
View file →
| @@ -13,12 +13,12 @@ | ||
| 13 | 13 | class Utils { |
| 14 | 14 | /** |
| 15 | 15 | * Build a VideoPress video URL based on the guid and block attributes. |
| 16 | 16 | * |
| 17 | - * @param {string} $guid - Video GUID. | |
| 18 | - * @param {array} $attributes - Video block attributes. Default is an empty array. | |
| 17 | + * @param string|null $guid - Video GUID; null returns null. | |
| 18 | + * @param array $attributes - Video block attributes. Default is an empty array. | |
| 19 | 19 | * |
| 20 | - * @return {string} VideoPress video URL with the specified attributes. | |
| 20 | + * @return string|null VideoPress video URL with the specified attributes, or null without a guid. | |
| 21 | 21 | */ |
| 22 | 22 | public static function get_video_press_url( $guid, $attributes = array() ) { |
| 23 | 23 | if ( ! $guid ) { |
| 24 | 24 | return null; |
| @@ -40,8 +40,13 @@ | ||
| 40 | 40 | 'useAverageColor' => true, |
| 41 | 41 | ) |
| 42 | 42 | ); |
| 43 | 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 | + | |
| 44 | 49 | $query_args = array( |
| 45 | 50 | 'resizeToParent' => 1, |
| 46 | 51 | 'cover' => 1, |
| 47 | 52 | 'autoPlay' => (int) $video_press_url_options['autoplay'], |
| @@ -69,12 +74,28 @@ | ||
| 69 | 74 | * Determines if a given URL is a VideoPress URL. |
| 70 | 75 | * |
| 71 | 76 | * @since x.x.x |
| 72 | 77 | * |
| 73 | - * @param string $url The URL to check. | |
| 78 | + * @param mixed $url The URL to check. Non-strings return false. | |
| 74 | 79 | * @return bool True if the URL is a VideoPress URL, false otherwise. |
| 75 | 80 | */ |
| 76 | 81 | public static function is_videopress_url( $url ) { |
| 77 | - $pattern = '/^https?:\/\/(?:(?:v(?:ideo)?\.wordpress\.com|videopress\.com)\/(?:v|embed)|v\.wordpress\.com)\/([a-z\d]{8})(\/|\b)/i'; // phpcs:ignore WordPress.WP.CapitalPDangit.MisspelledInText | |
| 78 | - return (bool) preg_match( $pattern, $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; | |
| 79 | 100 | } |
| 80 | 101 | } |