js
2 months ago
tus
1 month ago
videopress-divi
2 months ago
videopress-divi-5
2 months ago
class-access-control.php
5 days ago
class-admin-ui.php
2 weeks ago
class-ajax.php
1 month ago
class-attachment-handler.php
1 month ago
class-block-editor-content.php
1 month ago
class-block-editor-extensions.php
2 weeks ago
class-block-replacement.php
9 months ago
class-caption-tracks.php
1 month ago
class-data.php
1 month ago
class-divi.php
2 months ago
class-initial-state.php
2 weeks ago
class-initializer.php
5 days ago
class-jwt-token-bridge.php
2 years ago
class-module-control.php
3 years ago
class-options.php
2 years ago
class-package-version.php
5 days ago
class-plan.php
2 years ago
class-rest-controller.php
1 month ago
class-site.php
3 years ago
class-stats.php
1 year ago
class-status.php
9 months ago
class-upload-exception.php
3 years ago
class-uploader-rest-endpoints.php
2 weeks ago
class-uploader.php
1 month ago
class-utils.php
3 months ago
class-video-block-email-renderer.php
2 months ago
class-videopress-rest-api-v1-features.php
6 months ago
class-videopress-rest-api-v1-settings.php
1 month ago
class-videopress-rest-api-v1-site.php
2 weeks ago
class-videopress-rest-api-v1-stats.php
3 years ago
class-videopresstoken.php
1 month ago
class-wpcom-rest-api-v2-attachment-field-videopress.php
9 months ago
class-wpcom-rest-api-v2-attachment-videopress-data.php
1 month ago
class-wpcom-rest-api-v2-endpoint-videopress-caption-tracks.php
1 month ago
class-wpcom-rest-api-v2-endpoint-videopress.php
2 weeks ago
class-xmlrpc.php
1 month ago
utility-functions.php
4 weeks ago
class-utils.php
97 lines
| 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 $guid - Video GUID. |
| 18 | * @param array $attributes - Video block attributes. Default is an empty array. |
| 19 | * |
| 20 | * @return string VideoPress video URL with the specified attributes. |
| 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 | $query_args = array( |
| 45 | 'resizeToParent' => 1, |
| 46 | 'cover' => 1, |
| 47 | 'autoPlay' => (int) $video_press_url_options['autoplay'], |
| 48 | 'controls' => (int) $video_press_url_options['controls'], |
| 49 | 'loop' => (int) $video_press_url_options['loop'], |
| 50 | 'muted' => (int) $video_press_url_options['muted'], |
| 51 | 'persistVolume' => $video_press_url_options['muted'] ? 0 : 1, |
| 52 | 'playsinline' => (int) $video_press_url_options['playsinline'], |
| 53 | 'preloadContent' => $video_press_url_options['preload'], |
| 54 | 'sbc' => $video_press_url_options['seekbarColor'], |
| 55 | 'sbpc' => $video_press_url_options['seekbarPlayedColor'], |
| 56 | 'sblc' => $video_press_url_options['seekbarLoadingColor'], |
| 57 | 'useAverageColor' => (int) $video_press_url_options['useAverageColor'], |
| 58 | ); |
| 59 | |
| 60 | if ( ! empty( $video_press_url_options['poster'] ) ) { |
| 61 | $query_args['posterUrl'] = rawurlencode( $video_press_url_options['poster'] ); |
| 62 | } |
| 63 | |
| 64 | $url = 'https://videopress.com/v/' . $guid; |
| 65 | return add_query_arg( $query_args, $url ); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Determines if a given URL is a VideoPress URL. |
| 70 | * |
| 71 | * @since x.x.x |
| 72 | * |
| 73 | * @param mixed $url The URL to check. Non-strings return false. |
| 74 | * @return bool True if the URL is a VideoPress URL, false otherwise. |
| 75 | */ |
| 76 | public static function is_videopress_url( $url ) { |
| 77 | return null !== self::extract_videopress_guid_from_url( $url ); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Extract the VideoPress guid from a recognised VideoPress URL. |
| 82 | * |
| 83 | * @param mixed $url The URL to inspect. Non-strings return null. |
| 84 | * @return string|null The 8-character guid, or null if the URL is not a recognised VideoPress URL. |
| 85 | */ |
| 86 | public static function extract_videopress_guid_from_url( $url ) { |
| 87 | if ( ! is_string( $url ) ) { |
| 88 | return null; |
| 89 | } |
| 90 | $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 |
| 91 | if ( preg_match( $pattern, $url, $matches ) ) { |
| 92 | return $matches[1]; |
| 93 | } |
| 94 | return null; |
| 95 | } |
| 96 | } |
| 97 |