Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
97.92% covered (success)
97.92%
47 / 48
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
Utils
97.92% covered (success)
97.92%
47 / 48
66.67% covered (warning)
66.67%
2 / 3
11
0.00% covered (danger)
0.00%
0 / 1
 get_video_press_url
97.56% covered (success)
97.56%
40 / 41
0.00% covered (danger)
0.00%
0 / 1
7
 is_videopress_url
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 extract_videopress_guid_from_url
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2/**
3 * The Utils class.
4 *
5 * @package automattic/jetpack-videopress
6 */
7
8namespace Automattic\Jetpack\VideoPress;
9
10/**
11 * The Utils class.
12 */
13class 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                // On by default: an iPhone plays a video in the page only when
35                // asked to, and opens the system's fullscreen player otherwise,
36                // without the VideoPress controls.
37                'playsinline'         => true,
38                'poster'              => '',
39                'preload'             => 'metadata',
40                'seekbarColor'        => '',
41                'seekbarPlayedColor'  => '',
42                'seekbarLoadingColor' => '',
43                'useAverageColor'     => true,
44            )
45        );
46
47        // The site-wide opt-out wins over the block's own preload attribute.
48        if ( Data::get_videopress_player_preload_disabled() ) {
49            $video_press_url_options['preload'] = 'none';
50        }
51
52        $query_args = array(
53            'resizeToParent'  => 1,
54            'cover'           => 1,
55            'autoPlay'        => (int) $video_press_url_options['autoplay'],
56            'controls'        => (int) $video_press_url_options['controls'],
57            'loop'            => (int) $video_press_url_options['loop'],
58            'muted'           => (int) $video_press_url_options['muted'],
59            'persistVolume'   => $video_press_url_options['muted'] ? 0 : 1,
60            'playsinline'     => (int) $video_press_url_options['playsinline'],
61            'preloadContent'  => $video_press_url_options['preload'],
62            'sbc'             => $video_press_url_options['seekbarColor'],
63            'sbpc'            => $video_press_url_options['seekbarPlayedColor'],
64            'sblc'            => $video_press_url_options['seekbarLoadingColor'],
65            'useAverageColor' => (int) $video_press_url_options['useAverageColor'],
66        );
67
68        if ( ! empty( $video_press_url_options['poster'] ) ) {
69            $query_args['posterUrl'] = rawurlencode( $video_press_url_options['poster'] );
70        }
71
72        if ( isset( $video_press_url_options['at'] ) && (int) $video_press_url_options['at'] > 0 ) {
73            $query_args['at'] = (int) $video_press_url_options['at'];
74        }
75
76        $url = 'https://videopress.com/v/' . $guid;
77        return add_query_arg( $query_args, $url );
78    }
79
80    /**
81     * Determines if a given URL is a VideoPress URL.
82     *
83     * @since x.x.x
84     *
85     * @param mixed $url The URL to check. Non-strings return false.
86     * @return bool True if the URL is a VideoPress URL, false otherwise.
87     */
88    public static function is_videopress_url( $url ) {
89        return null !== self::extract_videopress_guid_from_url( $url );
90    }
91
92    /**
93     * Extract the VideoPress guid from a recognised VideoPress URL.
94     *
95     * @param mixed $url The URL to inspect. Non-strings return null.
96     * @return string|null The 8-character guid, or null if the URL is not a recognised VideoPress URL.
97     */
98    public static function extract_videopress_guid_from_url( $url ) {
99        if ( ! is_string( $url ) ) {
100            return null;
101        }
102        $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
103        if ( preg_match( $pattern, $url, $matches ) ) {
104            return $matches[1];
105        }
106        return null;
107    }
108}