Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
89.82% |
503 / 560 |
|
65.38% |
17 / 26 |
CRAP | |
0.00% |
0 / 1 |
| Initializer | |
89.82% |
503 / 560 |
|
65.38% |
17 / 26 |
216.93 | |
0.00% |
0 / 1 |
| init | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
4 | |||
| update_init_options | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
3.33 | |||
| should_initialize_admin_ui | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| unconditional_initialization | |
91.30% |
21 / 23 |
|
0.00% |
0 / 1 |
4.01 | |||
| should_include_utilities | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
3.33 | |||
| prepare_poster_url_for_inline_style | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
4 | |||
| active_initialization | |
95.83% |
23 / 24 |
|
0.00% |
0 / 1 |
3 | |||
| register_oembed_providers | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| video_enqueue_bridge_when_oembed_present | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| register_videopress_blocks | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| register_videopress_all_playlists_block | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| render_videopress_video_block | |
93.28% |
125 / 134 |
|
0.00% |
0 / 1 |
41.51 | |||
| register_videopress_video_block | |
0.00% |
0 / 29 |
|
0.00% |
0 / 1 |
90 | |||
| register_videopress_playlist_block | |
100.00% |
19 / 19 |
|
100.00% |
1 / 1 |
7 | |||
| register_videopress_latest_videos_playlist_block | |
94.74% |
18 / 19 |
|
0.00% |
0 / 1 |
6.01 | |||
| render_videopress_latest_videos_playlist_block | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
3 | |||
| sanitize_playlist_entries | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
11 | |||
| playlist_embed_url | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
4 | |||
| playlist_timecode | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
3 | |||
| playlist_runtime_label | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
5 | |||
| playlist_resolution_label | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
3 | |||
| render_videopress_playlist_block | |
100.00% |
199 / 199 |
|
100.00% |
1 / 1 |
50 | |||
| enqueue_videopress_iframe_api_script | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
12 | |||
| maybe_pre_oembed_inline_player | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| maybe_render_oembed_inline_player | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
4 | |||
| render_inline_player_for_url | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * The initializer class for the videopress package |
| 4 | * |
| 5 | * @package automattic/jetpack-videopress |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\Jetpack\VideoPress; |
| 9 | |
| 10 | /** |
| 11 | * Initialized the VideoPress package |
| 12 | */ |
| 13 | class Initializer { |
| 14 | |
| 15 | /** |
| 16 | * Bounds of the Latest Videos Playlist block's "Number of videos" setting; |
| 17 | * the editor control uses the same range. |
| 18 | */ |
| 19 | const LATEST_VIDEOS_PLAYLIST_MIN_COUNT = 1; |
| 20 | const LATEST_VIDEOS_PLAYLIST_MAX_COUNT = 20; |
| 21 | const LATEST_VIDEOS_PLAYLIST_DEFAULT_COUNT = 5; |
| 22 | |
| 23 | const JETPACK_VIDEOPRESS_IFRAME_API_HANDLER = 'jetpack-videopress-iframe-api'; |
| 24 | |
| 25 | /** |
| 26 | * Initialization optinos |
| 27 | * |
| 28 | * @var array |
| 29 | */ |
| 30 | protected static $init_options = array(); |
| 31 | |
| 32 | /** |
| 33 | * Initializes the VideoPress package |
| 34 | * |
| 35 | * This method is called by Config::ensure. |
| 36 | * |
| 37 | * @return void |
| 38 | */ |
| 39 | public static function init() { |
| 40 | if ( ! did_action( 'videopress_init' ) ) { |
| 41 | |
| 42 | self::unconditional_initialization(); |
| 43 | |
| 44 | if ( Status::is_active() ) { |
| 45 | self::active_initialization(); |
| 46 | } elseif ( self::should_initialize_admin_ui() ) { |
| 47 | // Keep "Jetpack > VideoPress" in the menu when the module is not |
| 48 | // active, linking to the My Jetpack interstitial to activate it. |
| 49 | Admin_UI::init_inactive_menu(); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Fires after the VideoPress package is initialized |
| 55 | * |
| 56 | * @since 0.1.1 |
| 57 | */ |
| 58 | do_action( 'videopress_init' ); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Update the initialization options |
| 63 | * |
| 64 | * This method is called by the Config class |
| 65 | * |
| 66 | * @param array $options The initialization options. |
| 67 | * @return void |
| 68 | */ |
| 69 | public static function update_init_options( array $options ) { |
| 70 | if ( empty( $options['admin_ui'] ) || self::should_initialize_admin_ui() ) { // do not overwrite if already set to true. |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | self::$init_options['admin_ui'] = $options['admin_ui']; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Checks the initialization options and returns whether the admin_ui should be initialized or not |
| 79 | * |
| 80 | * @return boolean |
| 81 | */ |
| 82 | public static function should_initialize_admin_ui() { |
| 83 | return isset( self::$init_options['admin_ui'] ) && true === self::$init_options['admin_ui']; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Initialize VideoPress features that should be initialized whenever VideoPress is present, even if the module is not active |
| 88 | * |
| 89 | * @return void |
| 90 | */ |
| 91 | private static function unconditional_initialization() { |
| 92 | if ( self::should_include_utilities() ) { |
| 93 | require_once __DIR__ . '/utility-functions.php'; |
| 94 | } |
| 95 | |
| 96 | // Set up package version hook. |
| 97 | add_filter( 'jetpack_package_versions', __NAMESPACE__ . '\Package_Version::send_package_version_to_tracker' ); |
| 98 | |
| 99 | /* |
| 100 | * Keep the videopress_guid attachment meta out of reach of the |
| 101 | * user-facing meta write APIs (Custom Fields, XML-RPC set_custom_fields, |
| 102 | * the WordPress.com JSON API metadata op, REST). The post <-> guid |
| 103 | * mapping is what the VideoPress meta and poster endpoints authorize |
| 104 | * against, so a writable guid would let a caller point an object they |
| 105 | * can edit at somebody else's video and still pass the edit_post check. |
| 106 | * |
| 107 | * These auth_* filters are consulted by map_meta_cap() for the |
| 108 | * add/edit/delete_post_meta capabilities only, so unlike marking the key |
| 109 | * protected they leave is_protected_meta() false and meta_key queries |
| 110 | * against the WordPress.com JSON API keep working. VideoPress writes the |
| 111 | * mapping itself with update_post_meta(), which does not consult |
| 112 | * capabilities, so uploads and transcoding are unaffected. |
| 113 | * |
| 114 | * The subtype-specific filter covers attachments; the generic one covers |
| 115 | * calls made before a subtype can be resolved. |
| 116 | */ |
| 117 | add_filter( 'auth_post_meta_videopress_guid_for_attachment', '__return_false' ); |
| 118 | add_filter( 'auth_post_meta_videopress_guid', '__return_false' ); |
| 119 | |
| 120 | Module_Control::init(); |
| 121 | |
| 122 | /* |
| 123 | * The WPCOM REST API v2 endpoints only register routes/fields on REST |
| 124 | * init, so defer constructing them (and autoloading their classes) until |
| 125 | * a REST request is actually served. Registered on both REST init hooks |
| 126 | * so the routes remain available in every context they were before, and |
| 127 | * guarded so the endpoints are instantiated only once per request. |
| 128 | */ |
| 129 | $register_rest_api_v2_endpoints = static function () { |
| 130 | static $registered = false; |
| 131 | if ( $registered ) { |
| 132 | return; |
| 133 | } |
| 134 | $registered = true; |
| 135 | new WPCOM_REST_API_V2_Endpoint_VideoPress(); |
| 136 | new WPCOM_REST_API_V2_Endpoint_VideoPress_Caption_Tracks(); |
| 137 | new WPCOM_REST_API_V2_Attachment_VideoPress_Field(); |
| 138 | new WPCOM_REST_API_V2_Attachment_VideoPress_Data(); |
| 139 | new WPCOM_REST_API_V2_Endpoint_VideoPress_Edits(); |
| 140 | }; |
| 141 | add_action( 'rest_api_init', $register_rest_api_v2_endpoints, 0 ); |
| 142 | add_action( 'restapi_theme_init', $register_rest_api_v2_endpoints, 0 ); |
| 143 | |
| 144 | if ( is_admin() ) { |
| 145 | AJAX::init(); |
| 146 | } else { |
| 147 | require_once __DIR__ . '/class-block-replacement.php'; |
| 148 | Block_Replacement::init(); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * This avoids conflicts when running VideoPress plugin with older versions of the Jetpack plugin |
| 154 | * |
| 155 | * On version 11.3-a.7 utility functions include were removed from the plugin and it is safe to include it from the package |
| 156 | * |
| 157 | * @return boolean |
| 158 | */ |
| 159 | private static function should_include_utilities() { |
| 160 | if ( ! class_exists( 'Jetpack' ) || ! defined( 'JETPACK__VERSION' ) ) { |
| 161 | return true; |
| 162 | } |
| 163 | |
| 164 | return version_compare( JETPACK__VERSION, '11.3-a.7', '>=' ); |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Prepare a poster URL for a quoted CSS url() inside an HTML attribute. |
| 169 | * |
| 170 | * @param mixed $poster Poster URL. |
| 171 | * @return string Sanitized and HTML-encoded poster URL, or an empty string. |
| 172 | */ |
| 173 | private static function prepare_poster_url_for_inline_style( $poster ) { |
| 174 | if ( ! is_string( $poster ) || '' === $poster ) { |
| 175 | return ''; |
| 176 | } |
| 177 | |
| 178 | /* |
| 179 | * Decode one layer so ordinarily encoded URLs retain their semantics. Any |
| 180 | * remaining entities are encoded again below and stay inert after the HTML |
| 181 | * parser performs its single decoding pass. |
| 182 | */ |
| 183 | $poster_url = esc_url_raw( html_entity_decode( $poster, ENT_QUOTES | ENT_HTML5, 'UTF-8' ) ); |
| 184 | if ( '' === $poster_url ) { |
| 185 | return ''; |
| 186 | } |
| 187 | |
| 188 | /* |
| 189 | * Force existing character references to be encoded. esc_attr() preserves |
| 190 | * them, but this value crosses from an HTML attribute into a CSS string. |
| 191 | */ |
| 192 | return htmlspecialchars( $poster_url, ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML5, 'UTF-8', true ); |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Initialize VideoPress features that should be initialized only when the module is active |
| 197 | * |
| 198 | * @return void |
| 199 | */ |
| 200 | private static function active_initialization() { |
| 201 | Attachment_Handler::init(); |
| 202 | Jwt_Token_Bridge::init(); |
| 203 | Caption_Tracks::init(); |
| 204 | Initial_State::init(); |
| 205 | XMLRPC::init(); |
| 206 | Block_Editor_Content::init(); |
| 207 | Channel::init(); |
| 208 | Playlist_Index::init(); |
| 209 | |
| 210 | /* |
| 211 | * These endpoints only add their routes on REST init, so defer calling |
| 212 | * init() (and autoloading the endpoint classes) until a REST request is |
| 213 | * served. Priority 0 ensures the routes still register before the |
| 214 | * default-priority rest_api_init handlers run. Class-name strings are |
| 215 | * used so the classes are not autoloaded on non-REST requests. |
| 216 | */ |
| 217 | foreach ( |
| 218 | array( |
| 219 | Uploader_Rest_Endpoints::class, |
| 220 | Rest_Controller::class, |
| 221 | VideoPress_Rest_Api_V1_Stats::class, |
| 222 | VideoPress_Rest_Api_V1_Site::class, |
| 223 | VideoPress_Rest_Api_V1_Settings::class, |
| 224 | VideoPress_Rest_Api_V1_Features::class, |
| 225 | ) as $rest_endpoint |
| 226 | ) { |
| 227 | add_action( 'rest_api_init', array( $rest_endpoint, 'init' ), 0 ); |
| 228 | } |
| 229 | self::register_oembed_providers(); |
| 230 | |
| 231 | // In inline mode a VideoPress URL never needs the oEmbed round trip: skip |
| 232 | // the fetch, and swap iframes already cached in post meta for a placeholder. |
| 233 | add_filter( 'pre_oembed_result', array( __CLASS__, 'maybe_pre_oembed_inline_player' ), 10, 2 ); |
| 234 | add_filter( 'embed_oembed_html', array( __CLASS__, 'maybe_render_oembed_inline_player' ), 5, 4 ); |
| 235 | |
| 236 | // Enqueuethe VideoPress Iframe API script in the front-end. |
| 237 | add_filter( 'embed_oembed_html', array( __CLASS__, 'enqueue_videopress_iframe_api_script' ), 10, 4 ); |
| 238 | |
| 239 | if ( self::should_initialize_admin_ui() ) { |
| 240 | Admin_UI::init(); |
| 241 | } |
| 242 | |
| 243 | Divi::init(); |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * Explicitly register VideoPress oembed provider for patterns not supported by core |
| 248 | * |
| 249 | * @return void |
| 250 | */ |
| 251 | public static function register_oembed_providers() { |
| 252 | $host = rawurlencode( home_url() ); |
| 253 | // videopress.com/v is already registered in core. |
| 254 | // By explicitly declaring the provider here, we can speed things up by not relying on oEmbed discovery. |
| 255 | wp_oembed_add_provider( '#^https?://video.wordpress.com/v/.*#', 'https://public-api.wordpress.com/oembed/?for=' . $host, true ); |
| 256 | // This is needed as it's not supported in oEmbed discovery. |
| 257 | wp_oembed_add_provider( '|^https?://v\.wordpress\.com/([a-zA-Z\d]{8})(.+)?$|i', 'https://public-api.wordpress.com/oembed/?for=' . $host, true ); // phpcs:ignore WordPress.WP.CapitalPDangit.MisspelledInText |
| 258 | |
| 259 | add_filter( 'embed_oembed_html', array( __CLASS__, 'video_enqueue_bridge_when_oembed_present' ), 10, 4 ); |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * Enqueues VideoPress token bridge when a VideoPress oembed is present on the current page. |
| 264 | * |
| 265 | * @param string|false $cache The cached HTML result, stored in post meta. |
| 266 | * @param string $url The attempted embed URL. |
| 267 | * @param array $attr An array of shortcode attributes. |
| 268 | * @param int $post_ID Post ID. |
| 269 | * |
| 270 | * @return string|false |
| 271 | */ |
| 272 | public static function video_enqueue_bridge_when_oembed_present( $cache, $url, $attr, $post_ID = null ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
| 273 | if ( Utils::is_videopress_url( $url ) ) { |
| 274 | Jwt_Token_Bridge::enqueue_jwt_token_bridge(); |
| 275 | } |
| 276 | |
| 277 | return $cache; |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * Register all VideoPress blocks |
| 282 | * |
| 283 | * @return void |
| 284 | */ |
| 285 | public static function register_videopress_blocks() { |
| 286 | // Register VideoPress Video block. |
| 287 | self::register_videopress_video_block(); |
| 288 | |
| 289 | // Register Video Playlist block. |
| 290 | self::register_videopress_playlist_block(); |
| 291 | |
| 292 | // Register Latest Videos Playlist block. |
| 293 | self::register_videopress_latest_videos_playlist_block(); |
| 294 | |
| 295 | // Register All Playlists block. |
| 296 | self::register_videopress_all_playlists_block(); |
| 297 | } |
| 298 | |
| 299 | /** |
| 300 | * Register the All Playlists block, which lists the site's Video Playlist |
| 301 | * blocks from the playlist index. |
| 302 | * |
| 303 | * @param string|null $metadata_file Path to the block.json metadata file. Defaults to the |
| 304 | * package build output; tests can point it at a fixture. |
| 305 | * |
| 306 | * @return void |
| 307 | */ |
| 308 | public static function register_videopress_all_playlists_block( $metadata_file = null ) { |
| 309 | All_Playlists_Block::register( $metadata_file ); |
| 310 | } |
| 311 | |
| 312 | /** |
| 313 | * VideoPress video block render method |
| 314 | * |
| 315 | * @global \WP_Embed $wp_embed WordPress embed handler. |
| 316 | * |
| 317 | * @param array $block_attributes Block attributes. |
| 318 | * @param string $content Current block markup. |
| 319 | * @param \WP_Block $block Current block. |
| 320 | * |
| 321 | * @return string Block markup. |
| 322 | */ |
| 323 | public static function render_videopress_video_block( $block_attributes, $content, $block ) { |
| 324 | global $wp_embed; |
| 325 | |
| 326 | // Pre-build and cache the GUID list for this post to optimize authorization checks, |
| 327 | // and record the GUID actually being rendered: by render time WordPress has expanded |
| 328 | // synced patterns, templates, and template parts, so this covers embedding contexts |
| 329 | // the static content scan cannot see. |
| 330 | $post_id = $block->context['postId'] ?? get_the_ID(); |
| 331 | if ( ! empty( $post_id ) && isset( $block_attributes['guid'] ) && is_string( $block_attributes['guid'] ) ) { |
| 332 | Access_Control::ensure_post_guids_cached( absint( $post_id ), $block_attributes['guid'] ); |
| 333 | } elseif ( ! empty( $post_id ) ) { |
| 334 | Access_Control::build_and_cache_post_guids( absint( $post_id ) ); |
| 335 | } |
| 336 | |
| 337 | // CSS classes. |
| 338 | $align = $block_attributes['align'] ?? null; |
| 339 | $align_class = $align ? ' align' . $align : ''; |
| 340 | $custom_class = isset( $block_attributes['className'] ) ? ' ' . $block_attributes['className'] : ''; |
| 341 | $classes = 'wp-block-jetpack-videopress jetpack-videopress-player' . $custom_class . $align_class; |
| 342 | |
| 343 | // Inline style. |
| 344 | $style = ''; |
| 345 | $max_width = isset( $block_attributes['maxWidth'] ) && is_string( $block_attributes['maxWidth'] ) |
| 346 | ? trim( $block_attributes['maxWidth'] ) |
| 347 | : ''; |
| 348 | |
| 349 | // maxWidth is rendered into an inline style. Accept only a plain CSS length |
| 350 | // or percentage so the value stays a single, well-formed declaration; |
| 351 | // anything else is dropped and the block renders at full width (as with the |
| 352 | // "100%" default). |
| 353 | if ( '' !== $max_width && '100%' !== $max_width |
| 354 | && preg_match( '/^\d+(\.\d+)?(px|%|em|rem|vw|vh|vmin|vmax|ch|ex|cm|mm|in|pt|pc|q)$/i', $max_width ) |
| 355 | ) { |
| 356 | $style = sprintf( 'max-width: %s;', $max_width ); |
| 357 | $classes .= ' wp-block-jetpack-videopress--has-max-width'; |
| 358 | } |
| 359 | |
| 360 | /* |
| 361 | * <figcaption /> element |
| 362 | * Caption is stored into the block attributes, |
| 363 | * but also it was stored into the <figcaption /> element, |
| 364 | * meaning that it could be stored in two different places. |
| 365 | */ |
| 366 | $figcaption = ''; |
| 367 | |
| 368 | // Caption from block attributes. |
| 369 | $caption = $block_attributes['caption'] ?? null; |
| 370 | |
| 371 | /* |
| 372 | * If the caption is not stored into the block attributes, |
| 373 | * try to get it from the <figcaption /> element. |
| 374 | */ |
| 375 | if ( null === $caption ) { |
| 376 | preg_match( '/<figcaption>(.*?)<\/figcaption>/', $content, $matches ); |
| 377 | $caption = $matches[1] ?? null; |
| 378 | } |
| 379 | |
| 380 | // If we have a caption, create the <figcaption /> element. |
| 381 | if ( null !== $caption ) { |
| 382 | $figcaption = sprintf( '<figcaption>%s</figcaption>', wp_kses_post( $caption ) ); |
| 383 | } |
| 384 | |
| 385 | // Custom anchor from block content. |
| 386 | $id_attribute = ''; |
| 387 | |
| 388 | // Try to get the custom anchor from the block attributes. |
| 389 | if ( isset( $block_attributes['anchor'] ) && $block_attributes['anchor'] ) { |
| 390 | $id_attribute = sprintf( 'id="%s"', esc_attr( $block_attributes['anchor'] ) ); |
| 391 | } elseif ( preg_match( '/<figure[^>]*id="([^"]+)"/', $content, $matches ) ) { |
| 392 | // Otherwise, try to get the custom anchor from the <figure /> element. |
| 393 | $id_attribute = sprintf( 'id="%s"', esc_attr( $matches[1] ) ); |
| 394 | } |
| 395 | |
| 396 | // Preview On Hover data. |
| 397 | $is_poh_enabled = |
| 398 | isset( $block_attributes['posterData']['previewOnHover'] ) && |
| 399 | $block_attributes['posterData']['previewOnHover']; |
| 400 | |
| 401 | $autoplay = $block_attributes['autoplay'] ?? false; |
| 402 | $controls = $block_attributes['controls'] ?? false; |
| 403 | $poster = $block_attributes['posterData']['url'] ?? null; |
| 404 | |
| 405 | $preview_on_hover = ''; |
| 406 | |
| 407 | if ( $is_poh_enabled ) { |
| 408 | $preview_on_hover = array( |
| 409 | 'previewAtTime' => $block_attributes['posterData']['previewAtTime'], |
| 410 | 'previewLoopDuration' => $block_attributes['posterData']['previewLoopDuration'], |
| 411 | 'autoplay' => $autoplay, |
| 412 | 'showControls' => $controls, |
| 413 | ); |
| 414 | |
| 415 | // Create inline style in case video has a custom poster. |
| 416 | $inline_style = ''; |
| 417 | $poster_url = self::prepare_poster_url_for_inline_style( $poster ); |
| 418 | if ( $poster_url ) { |
| 419 | // Emit the poster URL as a double-quoted CSS string so it stays |
| 420 | // contained within url() and cannot affect the surrounding style. |
| 421 | $inline_style = sprintf( |
| 422 | 'style="background-image: url("%s"); background-size: cover; background-position: center center;"', |
| 423 | $poster_url |
| 424 | ); |
| 425 | } |
| 426 | |
| 427 | // Expose the preview on hover data to the client. |
| 428 | $preview_on_hover = sprintf( |
| 429 | '<div class="jetpack-videopress-player__overlay" %s></div><script type="application/json">%s</script>', |
| 430 | $inline_style, |
| 431 | wp_json_encode( $preview_on_hover, JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP ) |
| 432 | ); |
| 433 | |
| 434 | // Set `autoplay` and `muted` attributes to the video element. |
| 435 | $block_attributes['autoplay'] = true; |
| 436 | $block_attributes['muted'] = true; |
| 437 | } |
| 438 | |
| 439 | $figure_template = ' |
| 440 | <figure class="%1$s" style="%2$s" %3$s> |
| 441 | %4$s |
| 442 | %5$s |
| 443 | %6$s |
| 444 | </figure> |
| 445 | '; |
| 446 | |
| 447 | // VideoPress URL. |
| 448 | $guid = $block_attributes['guid'] ?? null; |
| 449 | $videopress_url = Utils::get_video_press_url( $guid, $block_attributes ); |
| 450 | |
| 451 | $video_wrapper = ''; |
| 452 | $video_wrapper_classes = 'jetpack-videopress-player__wrapper'; |
| 453 | $video_ratio = $block_attributes['videoRatio'] ?? null; |
| 454 | $video_ratio = is_numeric( $video_ratio ) && is_finite( (float) $video_ratio ) && $video_ratio > 0 ? (float) $video_ratio : null; |
| 455 | |
| 456 | // Preview on hover drives the player through the iframe API, so it keeps the iframe. |
| 457 | if ( $guid && ! $is_poh_enabled && Inline_Player::is_enabled() ) { |
| 458 | $video_wrapper = sprintf( |
| 459 | '<div class="%s">%s</div>', |
| 460 | $video_wrapper_classes, |
| 461 | Inline_Player::render( |
| 462 | $guid, |
| 463 | Inline_Player::get_player_options( $block_attributes ), |
| 464 | $video_ratio, |
| 465 | array( |
| 466 | 'poster' => Inline_Player::get_poster_url( $guid, $block_attributes ), |
| 467 | 'title' => $block_attributes['title'] ?? '', |
| 468 | ) |
| 469 | ) |
| 470 | ); |
| 471 | } elseif ( $videopress_url ) { |
| 472 | $videopress_url = wp_kses_post( $videopress_url ); |
| 473 | |
| 474 | /* |
| 475 | * Provide a fallback iframe for when the oEmbed endpoint fails, e.g. |
| 476 | * when the VideoPress backend isn't ready for a freshly uploaded video. |
| 477 | * This prevents the published page from showing a bare link. |
| 478 | */ |
| 479 | $fallback = function ( $output, $url ) use ( $videopress_url, $video_ratio ) { |
| 480 | $decoded_url = html_entity_decode( $videopress_url, ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401 ); |
| 481 | if ( $decoded_url !== $url ) { |
| 482 | return $output; |
| 483 | } |
| 484 | |
| 485 | return sprintf( |
| 486 | '<iframe title="%1$s" aria-label="%1$s" src="%2$s" width="640" height="%3$s" allowfullscreen data-resize-to-parent="true" allow="clipboard-write; presentation"></iframe>', |
| 487 | esc_attr__( 'VideoPress Video Player', 'jetpack-videopress-pkg' ), |
| 488 | esc_url( preg_replace( '#/v/#', '/embed/', $url, 1 ) ), |
| 489 | esc_attr( (string) ( 640 * ( $video_ratio ?? 56.25 ) / 100 ) ) |
| 490 | ); |
| 491 | }; |
| 492 | |
| 493 | add_filter( 'embed_maybe_make_link', $fallback, 10, 2 ); |
| 494 | $oembed_html = apply_filters( 'video_embed_html', $wp_embed->shortcode( array(), $videopress_url ) ); |
| 495 | remove_filter( 'embed_maybe_make_link', $fallback ); |
| 496 | |
| 497 | // Use the saved shape immediately, including when oEmbed still has old dimensions. |
| 498 | if ( null !== $video_ratio ) { |
| 499 | $processor = new \WP_HTML_Tag_Processor( $oembed_html ); |
| 500 | if ( $processor->next_tag( 'IFRAME' ) ) { |
| 501 | $width = $processor->get_attribute( 'width' ); |
| 502 | if ( is_numeric( $width ) && is_finite( (float) $width ) && $width > 0 ) { |
| 503 | $processor->set_attribute( 'height', (string) ( (float) $width * $video_ratio / 100 ) ); |
| 504 | $oembed_html = $processor->get_updated_html(); |
| 505 | } |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | $video_wrapper = sprintf( |
| 510 | '<div class="%s">%s %s</div>', |
| 511 | $video_wrapper_classes, |
| 512 | $preview_on_hover, |
| 513 | $oembed_html |
| 514 | ); |
| 515 | |
| 516 | /* |
| 517 | * Self-heal failed oEmbed cache for VideoPress URLs. |
| 518 | * |
| 519 | * When the VideoPress backend isn't ready for a freshly uploaded video, |
| 520 | * WordPress caches '{{unknown}}' in post meta with a TTL that is too long |
| 521 | * for this use case. Clear recent failures so the next page render retries |
| 522 | * oEmbed discovery, keeping the fallback iframe above temporary. |
| 523 | */ |
| 524 | $post_id = $block->context['postId'] ?? get_the_ID(); |
| 525 | |
| 526 | if ( $post_id ) { |
| 527 | $key_suffix = md5( $videopress_url . serialize( wp_embed_defaults( $videopress_url ) ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize -- Matching WP_Embed cache key format. |
| 528 | $oembed_value = get_post_meta( $post_id, '_oembed_' . $key_suffix, true ); |
| 529 | $oembed_time = (int) get_post_meta( $post_id, '_oembed_time_' . $key_suffix, true ); |
| 530 | |
| 531 | /* |
| 532 | * Only clear the '{{unknown}}' cache entry when it is recent, to avoid |
| 533 | * disabling WordPress's oEmbed backoff for persistent provider failures. |
| 534 | */ |
| 535 | if ( |
| 536 | '{{unknown}}' === $oembed_value |
| 537 | && ( ! $oembed_time || ( time() - $oembed_time ) < MINUTE_IN_SECONDS ) |
| 538 | ) { |
| 539 | delete_post_meta( $post_id, '_oembed_' . $key_suffix ); |
| 540 | delete_post_meta( $post_id, '_oembed_time_' . $key_suffix ); |
| 541 | } |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | // Get premium content from block context. |
| 546 | $premium_block_plan_id = isset( $block->context['premium-content/planId'] ) ? intval( $block->context['premium-content/planId'] ) : 0; |
| 547 | $is_premium_content_child = isset( $block->context['isPremiumContentChild'] ) ? (bool) $block->context['isPremiumContentChild'] : false; |
| 548 | $maybe_premium_script = ''; |
| 549 | if ( $is_premium_content_child && is_string( $guid ) ) { |
| 550 | Access_Control::instance()->set_guid_subscription( $guid, $premium_block_plan_id ); |
| 551 | $escaped_guid = wp_json_encode( $guid, JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP ); |
| 552 | $script_content = "if ( ! window.__guidsToPlanIds ) { window.__guidsToPlanIds = {}; }; window.__guidsToPlanIds[$escaped_guid] = $premium_block_plan_id;"; |
| 553 | $maybe_premium_script = '<script>' . $script_content . '</script>'; |
| 554 | } |
| 555 | |
| 556 | // $id_attribute, $video_wrapper, $figcaption properly escaped earlier in the code. |
| 557 | return sprintf( |
| 558 | $figure_template, |
| 559 | esc_attr( $classes ), |
| 560 | esc_attr( $style ), |
| 561 | $id_attribute, |
| 562 | $video_wrapper, |
| 563 | $figcaption, |
| 564 | $maybe_premium_script |
| 565 | ); |
| 566 | } |
| 567 | |
| 568 | /** |
| 569 | * Register the VideoPress block editor block, |
| 570 | * AKA "VideoPress Block v6". |
| 571 | * |
| 572 | * @return void |
| 573 | */ |
| 574 | public static function register_videopress_video_block() { |
| 575 | /* |
| 576 | * If only Jetpack is active, and if the VideoPress module is not active, |
| 577 | * we can register the block just to display a placeholder to turn on the module. |
| 578 | * That invitation is only useful for admins though. |
| 579 | */ |
| 580 | if ( |
| 581 | Status::is_jetpack_plugin_without_videopress_module_active() |
| 582 | && ! Status::is_standalone_plugin_active() |
| 583 | && ! current_user_can( 'jetpack_activate_modules' ) |
| 584 | ) { |
| 585 | return; |
| 586 | } |
| 587 | |
| 588 | $videopress_video_metadata_file = __DIR__ . '/../build/block-editor/blocks/video/block.json'; |
| 589 | $videopress_video_metadata_file_exists = file_exists( $videopress_video_metadata_file ); |
| 590 | if ( ! $videopress_video_metadata_file_exists ) { |
| 591 | return; |
| 592 | } |
| 593 | |
| 594 | $videopress_video_metadata = json_decode( |
| 595 | // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents |
| 596 | file_get_contents( $videopress_video_metadata_file ) |
| 597 | ); |
| 598 | |
| 599 | // Pick the block name straight from the block metadata .json file. |
| 600 | $videopress_video_block_name = $videopress_video_metadata->name; |
| 601 | |
| 602 | // Is the block already registered? |
| 603 | $is_block_registered = \WP_Block_Type_Registry::get_instance()->is_registered( $videopress_video_block_name ); |
| 604 | |
| 605 | // Do not register if the block is already registered. |
| 606 | if ( $is_block_registered ) { |
| 607 | return; |
| 608 | } |
| 609 | |
| 610 | $registration = register_block_type( |
| 611 | $videopress_video_metadata_file, |
| 612 | array( |
| 613 | 'render_callback' => array( __CLASS__, 'render_videopress_video_block' ), |
| 614 | 'render_email_callback' => array( Video_Block_Email_Renderer::class, 'render' ), |
| 615 | 'uses_context' => array( 'premium-content/planId', 'isPremiumContentChild', 'selectedPlanId' ), |
| 616 | ) |
| 617 | ); |
| 618 | |
| 619 | // Do not enqueue scripts if the block could not be registered. |
| 620 | if ( empty( $registration ) || empty( $registration->editor_script_handles ) ) { |
| 621 | return; |
| 622 | } |
| 623 | |
| 624 | // Extensions use Connection_Initial_State::render_script with script handle as parameter. |
| 625 | if ( is_array( $registration->editor_script_handles ) ) { |
| 626 | $script_handle = $registration->editor_script_handles[0]; |
| 627 | } else { |
| 628 | $script_handle = $registration->editor_script_handles; |
| 629 | } |
| 630 | |
| 631 | // Register and enqueue scripts used by the VideoPress video block. |
| 632 | Block_Editor_Extensions::init( $script_handle ); |
| 633 | } |
| 634 | |
| 635 | /** |
| 636 | * Register the Video Playlist block. |
| 637 | * |
| 638 | * @param string|null $metadata_file Path to the block.json metadata file. Defaults to the |
| 639 | * package build output; tests can point it at a fixture. |
| 640 | * |
| 641 | * @return void |
| 642 | */ |
| 643 | public static function register_videopress_playlist_block( $metadata_file = null ) { |
| 644 | /* |
| 645 | * Unlike the video block, the playlist block has no "activate the module" |
| 646 | * placeholder, so it is only registered where VideoPress can play videos. |
| 647 | */ |
| 648 | if ( |
| 649 | Status::is_jetpack_plugin_without_videopress_module_active() |
| 650 | && ! Status::is_standalone_plugin_active() |
| 651 | ) { |
| 652 | return; |
| 653 | } |
| 654 | |
| 655 | if ( null === $metadata_file ) { |
| 656 | $metadata_file = __DIR__ . '/../build/block-editor/blocks/playlist/block.json'; |
| 657 | } |
| 658 | |
| 659 | if ( ! file_exists( $metadata_file ) ) { |
| 660 | return; |
| 661 | } |
| 662 | |
| 663 | $metadata = json_decode( |
| 664 | // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents |
| 665 | file_get_contents( $metadata_file ) |
| 666 | ); |
| 667 | |
| 668 | if ( empty( $metadata->name ) |
| 669 | || \WP_Block_Type_Registry::get_instance()->is_registered( $metadata->name ) |
| 670 | ) { |
| 671 | return; |
| 672 | } |
| 673 | |
| 674 | register_block_type( |
| 675 | $metadata_file, |
| 676 | array( |
| 677 | 'render_callback' => array( __CLASS__, 'render_videopress_playlist_block' ), |
| 678 | ) |
| 679 | ); |
| 680 | } |
| 681 | |
| 682 | /** |
| 683 | * Register the Latest Videos Playlist block. |
| 684 | * |
| 685 | * It reuses the Video Playlist block's registered view script and styles, so |
| 686 | * it is only registered once that block is. Its inner Video Playlist block is |
| 687 | * the editor canvas only: the front end renders the newest videos fresh. |
| 688 | * |
| 689 | * @param string|null $metadata_file Path to the block.json metadata file. Defaults to the |
| 690 | * package build output; tests can point it at a fixture. |
| 691 | * |
| 692 | * @return void |
| 693 | */ |
| 694 | public static function register_videopress_latest_videos_playlist_block( $metadata_file = null ) { |
| 695 | if ( ! \WP_Block_Type_Registry::get_instance()->is_registered( 'videopress/playlist' ) ) { |
| 696 | return; |
| 697 | } |
| 698 | |
| 699 | if ( null === $metadata_file ) { |
| 700 | $metadata_file = __DIR__ . '/../build/block-editor/blocks/latest-videos-playlist/block.json'; |
| 701 | } |
| 702 | |
| 703 | if ( ! file_exists( $metadata_file ) ) { |
| 704 | return; |
| 705 | } |
| 706 | |
| 707 | $metadata = json_decode( |
| 708 | // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents |
| 709 | file_get_contents( $metadata_file ) |
| 710 | ); |
| 711 | |
| 712 | if ( empty( $metadata->name ) |
| 713 | || \WP_Block_Type_Registry::get_instance()->is_registered( $metadata->name ) |
| 714 | ) { |
| 715 | return; |
| 716 | } |
| 717 | |
| 718 | register_block_type( |
| 719 | $metadata_file, |
| 720 | array( |
| 721 | 'render_callback' => array( __CLASS__, 'render_videopress_latest_videos_playlist_block' ), |
| 722 | 'skip_inner_blocks' => true, |
| 723 | ) |
| 724 | ); |
| 725 | } |
| 726 | |
| 727 | /** |
| 728 | * Latest Videos Playlist block render callback: the newest VideoPress videos |
| 729 | * on the site, rendered by the Video Playlist block's callback. |
| 730 | * |
| 731 | * @param array $block_attributes Block attributes. |
| 732 | * @param string $content Current block markup, unused: the inner block is never rendered. |
| 733 | * @param \WP_Block|null $block Current block. |
| 734 | * |
| 735 | * @return string Block markup, or an empty string when the site has no VideoPress videos. |
| 736 | */ |
| 737 | public static function render_videopress_latest_videos_playlist_block( $block_attributes, $content = '', $block = null ) { |
| 738 | $count = isset( $block_attributes['count'] ) && is_numeric( $block_attributes['count'] ) |
| 739 | ? (int) $block_attributes['count'] |
| 740 | : self::LATEST_VIDEOS_PLAYLIST_DEFAULT_COUNT; |
| 741 | $count = max( self::LATEST_VIDEOS_PLAYLIST_MIN_COUNT, min( self::LATEST_VIDEOS_PLAYLIST_MAX_COUNT, $count ) ); |
| 742 | |
| 743 | $block_attributes['videos'] = Data::get_latest_videopress_playlist_entries( $count ); |
| 744 | |
| 745 | // Dynamic playlists have no title of their own, so no heading either. |
| 746 | unset( $block_attributes['playlistTitle'], $block_attributes['showPlaylistTitle'] ); |
| 747 | |
| 748 | return self::render_videopress_playlist_block( $block_attributes, '', $block ); |
| 749 | } |
| 750 | |
| 751 | /** |
| 752 | * Sanitize the playlist block's videos attribute into rendering-ready entries. |
| 753 | * |
| 754 | * @param mixed $videos Raw attribute value. |
| 755 | * |
| 756 | * @return array Entries with guid, title, durationMs, height and poster keys. |
| 757 | */ |
| 758 | public static function sanitize_playlist_entries( $videos ) { |
| 759 | if ( ! is_array( $videos ) ) { |
| 760 | return array(); |
| 761 | } |
| 762 | |
| 763 | $entries = array(); |
| 764 | foreach ( $videos as $video ) { |
| 765 | if ( ! is_array( $video ) || empty( $video['guid'] ) || ! is_string( $video['guid'] ) ) { |
| 766 | continue; |
| 767 | } |
| 768 | |
| 769 | // VideoPress GUIDs are 8 alphanumeric characters; drop anything else. |
| 770 | if ( ! preg_match( '/^[a-zA-Z0-9]{8}$/', $video['guid'] ) ) { |
| 771 | continue; |
| 772 | } |
| 773 | |
| 774 | /* |
| 775 | * Only the video reference and numeric metadata are stored. Display |
| 776 | * metadata (title, poster) is always live: the view script reads it |
| 777 | * from the video data after the page loads. |
| 778 | */ |
| 779 | $entries[] = array( |
| 780 | 'guid' => $video['guid'], |
| 781 | 'durationMs' => isset( $video['durationMs'] ) && is_numeric( $video['durationMs'] ) ? max( 0, (int) $video['durationMs'] ) : 0, |
| 782 | 'height' => isset( $video['height'] ) && is_numeric( $video['height'] ) ? max( 0, (int) $video['height'] ) : 0, |
| 783 | ); |
| 784 | } |
| 785 | |
| 786 | return $entries; |
| 787 | } |
| 788 | |
| 789 | /** |
| 790 | * Build the VideoPress embed URL for a playlist entry. |
| 791 | * |
| 792 | * @param string $guid Video GUID. |
| 793 | * @param bool $autoplay Whether the video should start playing once loaded. |
| 794 | * @param bool $muted Whether playback should start muted. |
| 795 | * |
| 796 | * @return string Embed URL. |
| 797 | */ |
| 798 | private static function playlist_embed_url( $guid, $autoplay, $muted = false ) { |
| 799 | $args = array( |
| 800 | 'cover' => 1, |
| 801 | 'preloadContent' => Data::get_videopress_player_preload_disabled() ? 'none' : 'metadata', |
| 802 | 'autoPlay' => $autoplay ? 1 : 0, |
| 803 | ); |
| 804 | if ( $muted ) { |
| 805 | $args['muted'] = 1; |
| 806 | } |
| 807 | |
| 808 | return add_query_arg( |
| 809 | $args, |
| 810 | 'https://videopress.com/embed/' . rawurlencode( $guid ) |
| 811 | ); |
| 812 | } |
| 813 | |
| 814 | /** |
| 815 | * Format a duration in milliseconds as a timecode, m:ss or h:mm:ss. |
| 816 | * |
| 817 | * @param int $duration_ms Duration in milliseconds. |
| 818 | * |
| 819 | * @return string Timecode, or an empty string when the duration is unknown. |
| 820 | */ |
| 821 | private static function playlist_timecode( $duration_ms ) { |
| 822 | if ( $duration_ms <= 0 ) { |
| 823 | return ''; |
| 824 | } |
| 825 | |
| 826 | $total_seconds = (int) round( $duration_ms / 1000 ); |
| 827 | $hours = intdiv( $total_seconds, 3600 ); |
| 828 | $minutes = intdiv( $total_seconds % 3600, 60 ); |
| 829 | $seconds = $total_seconds % 60; |
| 830 | |
| 831 | if ( $hours > 0 ) { |
| 832 | return sprintf( '%d:%02d:%02d', $hours, $minutes, $seconds ); |
| 833 | } |
| 834 | |
| 835 | return sprintf( '%d:%02d', $minutes, $seconds ); |
| 836 | } |
| 837 | |
| 838 | /** |
| 839 | * Format a duration in milliseconds as a long runtime, e.g. "1 hr 13 min". |
| 840 | * |
| 841 | * @param int $duration_ms Duration in milliseconds. |
| 842 | * |
| 843 | * @return string Runtime label, or an empty string when the duration is unknown. |
| 844 | */ |
| 845 | private static function playlist_runtime_label( $duration_ms ) { |
| 846 | if ( $duration_ms <= 0 ) { |
| 847 | return ''; |
| 848 | } |
| 849 | |
| 850 | $total_minutes = max( 1, (int) round( $duration_ms / 60000 ) ); |
| 851 | $hours = intdiv( $total_minutes, 60 ); |
| 852 | $minutes = $total_minutes % 60; |
| 853 | |
| 854 | if ( $hours > 0 && $minutes > 0 ) { |
| 855 | /* translators: 1: number of hours. 2: number of minutes. */ |
| 856 | return sprintf( __( '%1$d hr %2$d min', 'jetpack-videopress-pkg' ), $hours, $minutes ); |
| 857 | } |
| 858 | |
| 859 | if ( $hours > 0 ) { |
| 860 | /* translators: %d: number of hours. */ |
| 861 | return sprintf( __( '%d hr', 'jetpack-videopress-pkg' ), $hours ); |
| 862 | } |
| 863 | |
| 864 | /* translators: %d: number of minutes. */ |
| 865 | return sprintf( __( '%d min', 'jetpack-videopress-pkg' ), $minutes ); |
| 866 | } |
| 867 | |
| 868 | /** |
| 869 | * Map a video's pixel height to a resolution label, e.g. "1080p" or "4K". |
| 870 | * |
| 871 | * @param int $height Video height in pixels. |
| 872 | * |
| 873 | * @return string Resolution label, or an empty string when the height is unknown. |
| 874 | */ |
| 875 | private static function playlist_resolution_label( $height ) { |
| 876 | if ( $height <= 0 ) { |
| 877 | return ''; |
| 878 | } |
| 879 | |
| 880 | return $height >= 2160 ? '4K' : $height . 'p'; |
| 881 | } |
| 882 | |
| 883 | /** |
| 884 | * Video Playlist block render callback. |
| 885 | * |
| 886 | * @param array $block_attributes Block attributes. |
| 887 | * @param string $content Rendered inner blocks: the title heading, when there is one. |
| 888 | * @param \WP_Block|null $block Current block. |
| 889 | * |
| 890 | * @return string Block markup, or an empty string when the playlist has no playable entries. |
| 891 | */ |
| 892 | public static function render_videopress_playlist_block( $block_attributes, $content = '', $block = null ) { |
| 893 | $entries = self::sanitize_playlist_entries( $block_attributes['videos'] ?? null ); |
| 894 | |
| 895 | if ( ! $entries ) { |
| 896 | return ''; |
| 897 | } |
| 898 | |
| 899 | // Record the rendered GUIDs in the post's cached GUID list so private playlist |
| 900 | // entries pass the playback authorization check, including when the playlist |
| 901 | // sits inside a synced pattern, template, or template part. |
| 902 | $post_id = $block->context['postId'] ?? get_the_ID(); |
| 903 | if ( ! empty( $post_id ) ) { |
| 904 | Access_Control::ensure_post_guids_cached( absint( $post_id ), array_column( $entries, 'guid' ) ); |
| 905 | } |
| 906 | |
| 907 | $enabled = function ( $key, $default_value = true ) use ( $block_attributes ) { |
| 908 | return isset( $block_attributes[ $key ] ) ? (bool) $block_attributes[ $key ] : $default_value; |
| 909 | }; |
| 910 | |
| 911 | $layout = isset( $block_attributes['layout'] ) && in_array( $block_attributes['layout'], array( 'side-rail', 'grid', 'strip' ), true ) |
| 912 | ? $block_attributes['layout'] |
| 913 | : 'side-rail'; |
| 914 | |
| 915 | $show_player = $enabled( 'showPlayer' ); |
| 916 | // A hidden player can still be revealed by the first click on an entry. |
| 917 | $reveal_player = ! $show_player |
| 918 | && isset( $block_attributes['entryClickAction'] ) |
| 919 | && 'show-player' === $block_attributes['entryClickAction']; |
| 920 | $has_player = $show_player || $reveal_player; |
| 921 | $show_thumbnail = $enabled( 'showThumbnail' ); |
| 922 | $show_title = $enabled( 'showTitle' ); |
| 923 | $show_res = $enabled( 'showResolution' ); |
| 924 | $show_duration = $enabled( 'showDuration' ); |
| 925 | $show_number = $enabled( 'showPositionNumber', false ); |
| 926 | $show_runtime = $enabled( 'showTotalRuntime' ); |
| 927 | $muted = $enabled( 'muteByDefault', false ); |
| 928 | |
| 929 | $classes = array( 'videopress-playlist', 'is-layout-' . $layout ); |
| 930 | if ( $enabled( 'darkPlayer', false ) ) { |
| 931 | $classes[] = 'is-dark'; |
| 932 | } |
| 933 | if ( ! $show_player ) { |
| 934 | $classes[] = 'hide-player'; |
| 935 | } |
| 936 | if ( ! $show_thumbnail ) { |
| 937 | $classes[] = 'hide-thumbnails'; |
| 938 | } |
| 939 | if ( ! $show_title ) { |
| 940 | $classes[] = 'hide-titles'; |
| 941 | } |
| 942 | if ( ! $show_res ) { |
| 943 | $classes[] = 'hide-resolutions'; |
| 944 | } |
| 945 | if ( ! $show_duration ) { |
| 946 | $classes[] = 'hide-durations'; |
| 947 | } |
| 948 | if ( ! $show_runtime ) { |
| 949 | $classes[] = 'hide-runtime'; |
| 950 | } |
| 951 | |
| 952 | // Lets the embed play private videos for authorized viewers. |
| 953 | Jwt_Token_Bridge::enqueue_jwt_token_bridge(); |
| 954 | |
| 955 | $count = count( $entries ); |
| 956 | $total_ms = 0; |
| 957 | foreach ( $entries as $entry ) { |
| 958 | $total_ms += $entry['durationMs']; |
| 959 | } |
| 960 | |
| 961 | $total_timecode = self::playlist_timecode( $total_ms ); |
| 962 | /* translators: %d: number of videos in the playlist. */ |
| 963 | $count_label = sprintf( _n( '%d video', '%d videos', $count, 'jetpack-videopress-pkg' ), $count ); |
| 964 | |
| 965 | /* |
| 966 | * Hidden placeholder shown (via the button's is-locked class) when the view |
| 967 | * script cannot authorize a private video's thumbnail for the viewer. |
| 968 | */ |
| 969 | $lock_markup = '<span class="videopress-playlist__entry-lock">' |
| 970 | . '<svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false"><path d="M17 10h-1.2V7.3c0-2.1-1.7-3.8-3.8-3.8-2.1 0-3.8 1.7-3.8 3.8V10H7c-.6 0-1 .4-1 1v8c0 .6.4 1 1 1h10c.6 0 1-.4 1-1v-8c0-.6-.4-1-1-1Zm-2.7 0H9.7V7.3c0-1.3 1-2.3 2.3-2.3 1.3 0 2.3 1 2.3 2.3V10Z"/></svg>' |
| 971 | . '<span class="videopress-playlist__entry-lock-label">' . esc_html__( 'Private video', 'jetpack-videopress-pkg' ) . '</span>' |
| 972 | . '</span>'; |
| 973 | |
| 974 | $items = ''; |
| 975 | foreach ( $entries as $index => $entry ) { |
| 976 | /* |
| 977 | * Positional fallback only: the view script replaces titles (and |
| 978 | * adds posters) with live video data once the page loads. |
| 979 | */ |
| 980 | /* translators: %d: position of the video in the playlist. */ |
| 981 | $title = sprintf( __( 'Video %d', 'jetpack-videopress-pkg' ), $index + 1 ); |
| 982 | |
| 983 | $timecode = self::playlist_timecode( $entry['durationMs'] ); |
| 984 | $resolution = self::playlist_resolution_label( $entry['height'] ); |
| 985 | /* translators: 1: position of the video in the playlist. 2: number of videos in the playlist. */ |
| 986 | $position = sprintf( __( '%1$d of %2$d', 'jetpack-videopress-pkg' ), $index + 1, $count ); |
| 987 | $details = implode( ' · ', array_filter( array( $resolution, $timecode ) ) ); |
| 988 | $progress = '' !== $total_timecode |
| 989 | /* translators: 1: position of the video in the playlist. 2: number of videos. 3: total playlist timecode. */ |
| 990 | ? sprintf( __( '%1$d / %2$d · %3$s total', 'jetpack-videopress-pkg' ), $index + 1, $count, $total_timecode ) |
| 991 | /* translators: 1: position of the video in the playlist. 2: number of videos. */ |
| 992 | : sprintf( __( '%1$d / %2$d', 'jetpack-videopress-pkg' ), $index + 1, $count ); |
| 993 | |
| 994 | $number_markup = $show_number |
| 995 | ? sprintf( |
| 996 | '<span class="videopress-playlist__entry-number">%s</span>', |
| 997 | esc_html( str_pad( (string) ( $index + 1 ), 2, '0', STR_PAD_LEFT ) ) |
| 998 | ) |
| 999 | : ''; |
| 1000 | |
| 1001 | $time_markup = '' !== $timecode |
| 1002 | ? sprintf( '<span class="videopress-playlist__entry-time">%s</span>', esc_html( $timecode ) ) |
| 1003 | : ''; |
| 1004 | |
| 1005 | $resolution_markup = '' !== $resolution |
| 1006 | ? sprintf( '<span class="videopress-playlist__entry-resolution">%s</span>', esc_html( $resolution ) ) |
| 1007 | : ''; |
| 1008 | |
| 1009 | $duration_markup = '' !== $timecode |
| 1010 | ? sprintf( '<span class="videopress-playlist__entry-duration">%s</span>', esc_html( $timecode ) ) |
| 1011 | : ''; |
| 1012 | |
| 1013 | if ( $has_player ) { |
| 1014 | $is_current = $show_player && 0 === $index; |
| 1015 | $entry_open = sprintf( |
| 1016 | '<button type="button" class="videopress-playlist__select%1$s"%2$s data-guid="%3$s" data-embed-url="%4$s" data-title="%5$s" data-position="%6$s" data-details="%7$s" data-progress="%8$s">', |
| 1017 | $is_current ? ' is-current' : '', |
| 1018 | $is_current ? ' aria-current="true"' : '', |
| 1019 | esc_attr( $entry['guid'] ), |
| 1020 | esc_url( self::playlist_embed_url( $entry['guid'], true, $muted ) ), |
| 1021 | esc_attr( $title ), |
| 1022 | esc_attr( $position ), |
| 1023 | esc_attr( $details ), |
| 1024 | esc_attr( $progress ) |
| 1025 | ); |
| 1026 | $entry_close = '</button>'; |
| 1027 | } else { |
| 1028 | // No player to load the video into: the entry opens its VideoPress page instead. |
| 1029 | $entry_open = sprintf( |
| 1030 | '<a class="videopress-playlist__select" href="%1$s" target="_blank" rel="noopener noreferrer" data-guid="%2$s" data-title="%3$s" data-position="%4$s" data-details="%5$s">', |
| 1031 | /** |
| 1032 | * Filters where a playlist entry opens when the block has no player. |
| 1033 | * |
| 1034 | * @since $$next-version$$ |
| 1035 | * |
| 1036 | * @param string $url The video's page on videopress.com. |
| 1037 | * @param string $guid The video GUID. |
| 1038 | */ |
| 1039 | esc_url( apply_filters( 'videopress_playlist_entry_url', 'https://videopress.com/v/' . $entry['guid'], $entry['guid'] ) ), |
| 1040 | esc_attr( $entry['guid'] ), |
| 1041 | esc_attr( $title ), |
| 1042 | esc_attr( $position ), |
| 1043 | esc_attr( $details ) |
| 1044 | ); |
| 1045 | $entry_close = '</a>'; |
| 1046 | } |
| 1047 | |
| 1048 | $items .= sprintf( |
| 1049 | '<li class="videopress-playlist__entry">%1$s' . |
| 1050 | '%2$s<span class="videopress-playlist__entry-thumb"><span class="videopress-playlist__entry-flag">%3$s</span>%4$s%5$s</span>' . |
| 1051 | '<span class="videopress-playlist__entry-body"><span class="videopress-playlist__entry-title">%6$s</span><span class="videopress-playlist__entry-meta">%7$s%8$s</span></span>' . |
| 1052 | '%9$s</li>', |
| 1053 | $entry_open, |
| 1054 | $number_markup, |
| 1055 | esc_html__( 'Playing', 'jetpack-videopress-pkg' ), |
| 1056 | $lock_markup, |
| 1057 | $time_markup, |
| 1058 | esc_html( $title ), |
| 1059 | $resolution_markup, |
| 1060 | $duration_markup, |
| 1061 | $entry_close |
| 1062 | ); |
| 1063 | } |
| 1064 | |
| 1065 | $first = $entries[0]; |
| 1066 | $first_title = __( 'Video 1', 'jetpack-videopress-pkg' ); |
| 1067 | $runtime_label = self::playlist_runtime_label( $total_ms ); |
| 1068 | $runtime_markup = $show_runtime && '' !== $runtime_label |
| 1069 | ? sprintf( '<span class="videopress-playlist__runtime">%s</span>', esc_html( $runtime_label ) ) |
| 1070 | : ''; |
| 1071 | |
| 1072 | // Count · runtime meta line, shown by the side-rail layout in the list header. |
| 1073 | $list_meta_markup = sprintf( |
| 1074 | '<span class="videopress-playlist__list-meta"><span class="videopress-playlist__count">%s</span>%s</span>', |
| 1075 | esc_html( $count_label ), |
| 1076 | $runtime_markup |
| 1077 | ); |
| 1078 | |
| 1079 | /* |
| 1080 | * The player renders its own title overlay, so the stage carries no |
| 1081 | * duplicate now-playing text — only the grid layout's runtime line. |
| 1082 | */ |
| 1083 | $now_markup = $show_runtime && '' !== $runtime_label |
| 1084 | ? sprintf( |
| 1085 | '<div class="videopress-playlist__now"><span class="videopress-playlist__now-runtime">%s</span></div>', |
| 1086 | esc_html( $count_label . ' · ' . $runtime_label ) |
| 1087 | ) |
| 1088 | : ''; |
| 1089 | |
| 1090 | if ( $show_player ) { |
| 1091 | $stage_markup = sprintf( |
| 1092 | '<div class="videopress-playlist__stage">' . |
| 1093 | '<div class="videopress-playlist__player"><iframe class="videopress-playlist__iframe" title="%1$s" src="%2$s" allowfullscreen allow="clipboard-write"></iframe></div>%3$s</div>', |
| 1094 | esc_attr( $first_title ), |
| 1095 | esc_url( self::playlist_embed_url( $first['guid'], false, $muted ) ), |
| 1096 | $now_markup |
| 1097 | ); |
| 1098 | } elseif ( $reveal_player ) { |
| 1099 | // No src: nothing loads until the view script reveals the stage on a click. |
| 1100 | $stage_markup = sprintf( |
| 1101 | '<div class="videopress-playlist__stage" hidden>' . |
| 1102 | '<div class="videopress-playlist__player"><iframe class="videopress-playlist__iframe" title="%1$s" allowfullscreen allow="clipboard-write"></iframe></div></div>%2$s', |
| 1103 | esc_attr( $first_title ), |
| 1104 | $now_markup |
| 1105 | ); |
| 1106 | } else { |
| 1107 | // Without a player only the grid layout's runtime line remains of the stage. |
| 1108 | $stage_markup = $now_markup; |
| 1109 | } |
| 1110 | |
| 1111 | $progress_markup = $has_player && '' !== $total_timecode |
| 1112 | ? sprintf( |
| 1113 | '<span class="videopress-playlist__list-progress">%s</span>', |
| 1114 | /* translators: 1: position of the current video. 2: number of videos. 3: total playlist timecode. */ |
| 1115 | esc_html( sprintf( __( '%1$d / %2$d · %3$s total', 'jetpack-videopress-pkg' ), 1, $count, $total_timecode ) ) |
| 1116 | ) |
| 1117 | : ''; |
| 1118 | |
| 1119 | $list_markup = sprintf( |
| 1120 | '<div class="videopress-playlist__list">' . |
| 1121 | '<div class="videopress-playlist__list-header">' . |
| 1122 | '<span class="videopress-playlist__list-label videopress-playlist__list-label--rail">%1$s</span>' . |
| 1123 | '<span class="videopress-playlist__list-label videopress-playlist__list-label--strip">%2$s</span>%3$s%4$s</div>' . |
| 1124 | '<ol class="videopress-playlist__entries">%5$s</ol></div>', |
| 1125 | $show_player |
| 1126 | ? esc_html__( 'Up next', 'jetpack-videopress-pkg' ) |
| 1127 | : esc_html__( 'Playlist', 'jetpack-videopress-pkg' ), |
| 1128 | /* translators: %s: number of videos in the playlist, e.g. "5 videos". */ |
| 1129 | esc_html( sprintf( __( 'Playlist — %s', 'jetpack-videopress-pkg' ), $count_label ) ), |
| 1130 | $list_meta_markup, |
| 1131 | $progress_markup, |
| 1132 | $items |
| 1133 | ); |
| 1134 | |
| 1135 | /* |
| 1136 | * User-selected theme font presets (theme.json slugs) for the |
| 1137 | * customizable titles, exposed as CSS custom properties the |
| 1138 | * stylesheet reads. Anything but a plain preset slug is dropped. |
| 1139 | */ |
| 1140 | $font_style = ''; |
| 1141 | foreach ( array( |
| 1142 | 'entryTitleFontFamily' => '--vpp-entry-title-font', |
| 1143 | ) as $font_attribute => $css_variable ) { |
| 1144 | if ( ! empty( $block_attributes[ $font_attribute ] ) |
| 1145 | && is_string( $block_attributes[ $font_attribute ] ) |
| 1146 | && preg_match( '/^[a-zA-Z0-9-]+$/', $block_attributes[ $font_attribute ] ) |
| 1147 | ) { |
| 1148 | $font_style .= $css_variable . ':var(--wp--preset--font-family--' . $block_attributes[ $font_attribute ] . ');'; |
| 1149 | } |
| 1150 | } |
| 1151 | |
| 1152 | // Looping implies auto-advancing, so it forces the autoplay-next flag on. |
| 1153 | $loop_playlist = $enabled( 'loopPlaylist', false ); |
| 1154 | |
| 1155 | $wrapper_extra_attributes = array( |
| 1156 | 'class' => implode( ' ', $classes ), |
| 1157 | 'data-autoplay-next' => $enabled( 'autoplayNext', false ) || $loop_playlist ? '1' : '0', |
| 1158 | 'data-loop' => $loop_playlist ? '1' : '0', |
| 1159 | ); |
| 1160 | if ( '' !== $font_style ) { |
| 1161 | $wrapper_extra_attributes['style'] = $font_style; |
| 1162 | } |
| 1163 | |
| 1164 | $wrapper_attributes = get_block_wrapper_attributes( $wrapper_extra_attributes ); |
| 1165 | |
| 1166 | // The Heading inner block saved with the post; an empty title renders none. |
| 1167 | $playlist_title = isset( $block_attributes['playlistTitle'] ) && is_string( $block_attributes['playlistTitle'] ) |
| 1168 | ? trim( $block_attributes['playlistTitle'] ) |
| 1169 | : ''; |
| 1170 | $content = is_string( $content ) ? trim( $content ) : ''; |
| 1171 | $heading_markup = $enabled( 'showPlaylistTitle' ) && '' !== $playlist_title && '' !== $content |
| 1172 | ? '<div class="videopress-playlist__heading">' . $content . '</div>' |
| 1173 | : ''; |
| 1174 | |
| 1175 | return sprintf( |
| 1176 | '<figure %1$s>%2$s<div class="videopress-playlist__body">%3$s%4$s</div></figure>', |
| 1177 | $wrapper_attributes, |
| 1178 | $heading_markup, |
| 1179 | $stage_markup, |
| 1180 | $list_markup |
| 1181 | ); |
| 1182 | } |
| 1183 | |
| 1184 | /** |
| 1185 | * Enqueue the VideoPress Iframe API script |
| 1186 | * when the URL of oEmbed HTML is a VideoPress URL. |
| 1187 | * |
| 1188 | * @param string|false $cache The cached HTML result, stored in post meta. |
| 1189 | * @param string $url The attempted embed URL. |
| 1190 | * @param array $attr An array of shortcode attributes. |
| 1191 | * @param int $post_ID Post ID. |
| 1192 | * |
| 1193 | * @return string|false |
| 1194 | */ |
| 1195 | public static function enqueue_videopress_iframe_api_script( $cache, $url, $attr, $post_ID ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
| 1196 | if ( Utils::is_videopress_url( $url ) && ! Inline_Player::is_enabled() ) { |
| 1197 | // Enqueue the VideoPress IFrame API in the front-end. |
| 1198 | wp_enqueue_script( |
| 1199 | self::JETPACK_VIDEOPRESS_IFRAME_API_HANDLER, |
| 1200 | 'https://s0.wp.com/wp-content/plugins/video/assets/js/videojs/videopress-iframe-api.js', |
| 1201 | array(), |
| 1202 | gmdate( 'YW' ), |
| 1203 | false |
| 1204 | ); |
| 1205 | } |
| 1206 | |
| 1207 | return $cache; |
| 1208 | } |
| 1209 | |
| 1210 | /** |
| 1211 | * Short-circuit the oEmbed request for VideoPress URLs when the inline player is on. |
| 1212 | * |
| 1213 | * @param null|string $result The oEmbed result, null to let WordPress fetch it. |
| 1214 | * @param string $url The URL being embedded. |
| 1215 | * @return null|string Inline player markup, or the untouched result. |
| 1216 | */ |
| 1217 | public static function maybe_pre_oembed_inline_player( $result, $url ) { |
| 1218 | $inline = self::render_inline_player_for_url( $url ); |
| 1219 | |
| 1220 | return null === $inline ? $result : $inline; |
| 1221 | } |
| 1222 | |
| 1223 | /** |
| 1224 | * Replace a VideoPress oEmbed iframe (fresh or cached) with an inline player when the inline player is on. |
| 1225 | * |
| 1226 | * @param string|false $cache The oEmbed HTML. |
| 1227 | * @param string $url The URL being embedded. |
| 1228 | * @param array $attr Shortcode attributes. |
| 1229 | * @param int $post_ID Post ID. |
| 1230 | * @return string|false Inline player markup, or the untouched HTML. |
| 1231 | */ |
| 1232 | public static function maybe_render_oembed_inline_player( $cache, $url, $attr, $post_ID = null ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
| 1233 | if ( ! is_string( $cache ) || false === strpos( $cache, '<iframe' ) ) { |
| 1234 | return $cache; |
| 1235 | } |
| 1236 | |
| 1237 | $inline = self::render_inline_player_for_url( $url ); |
| 1238 | |
| 1239 | return null === $inline ? $cache : $inline; |
| 1240 | } |
| 1241 | |
| 1242 | /** |
| 1243 | * Build inline player markup for a videopress.com URL, honoring the player parameters in its query string. |
| 1244 | * |
| 1245 | * @param string $url The URL being embedded. |
| 1246 | * @return string|null Markup, or null when the URL is not a VideoPress video or the inline player is off. |
| 1247 | */ |
| 1248 | private static function render_inline_player_for_url( $url ) { |
| 1249 | if ( ! Inline_Player::is_enabled() ) { |
| 1250 | return null; |
| 1251 | } |
| 1252 | |
| 1253 | $guid = Utils::extract_videopress_guid_from_url( $url ); |
| 1254 | if ( null === $guid ) { |
| 1255 | return null; |
| 1256 | } |
| 1257 | |
| 1258 | $attributes = Inline_Player::get_attributes_from_embed_url( $url ); |
| 1259 | |
| 1260 | return Inline_Player::render( |
| 1261 | $guid, |
| 1262 | Inline_Player::get_player_options( $attributes ), |
| 1263 | null, |
| 1264 | array( 'poster' => Inline_Player::get_poster_url( $guid, $attributes ) ) |
| 1265 | ); |
| 1266 | } |
| 1267 | } |