← All changes
|
jetpack_vendor/automattic/jetpack-videopress/src/class-initializer.php
+1253
-0
16.2-beta
→
16.3
View file →
| @@ -1,0 +1,1253 @@ | ||
| 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 | + Analytics_Dashboard::init(); | |
| 210 | + | |
| 211 | + /* | |
| 212 | + * These endpoints only add their routes on REST init, so defer calling | |
| 213 | + * init() (and autoloading the endpoint classes) until a REST request is | |
| 214 | + * served. Priority 0 ensures the routes still register before the | |
| 215 | + * default-priority rest_api_init handlers run. Class-name strings are | |
| 216 | + * used so the classes are not autoloaded on non-REST requests. | |
| 217 | + */ | |
| 218 | + foreach ( | |
| 219 | + array( | |
| 220 | + Uploader_Rest_Endpoints::class, | |
| 221 | + Rest_Controller::class, | |
| 222 | + VideoPress_Rest_Api_V1_Stats::class, | |
| 223 | + VideoPress_Rest_Api_V1_Site::class, | |
| 224 | + VideoPress_Rest_Api_V1_Settings::class, | |
| 225 | + VideoPress_Rest_Api_V1_Features::class, | |
| 226 | + ) as $rest_endpoint | |
| 227 | + ) { | |
| 228 | + add_action( 'rest_api_init', array( $rest_endpoint, 'init' ), 0 ); | |
| 229 | + } | |
| 230 | + self::register_oembed_providers(); | |
| 231 | + | |
| 232 | + // In inline mode a VideoPress URL never needs the oEmbed round trip: skip | |
| 233 | + // the fetch, and swap iframes already cached in post meta for a placeholder. | |
| 234 | + add_filter( 'pre_oembed_result', array( __CLASS__, 'maybe_pre_oembed_inline_player' ), 10, 2 ); | |
| 235 | + add_filter( 'embed_oembed_html', array( __CLASS__, 'maybe_render_oembed_inline_player' ), 5, 4 ); | |
| 236 | + | |
| 237 | + // Enqueuethe VideoPress Iframe API script in the front-end. | |
| 238 | + add_filter( 'embed_oembed_html', array( __CLASS__, 'enqueue_videopress_iframe_api_script' ), 10, 4 ); | |
| 239 | + | |
| 240 | + if ( self::should_initialize_admin_ui() ) { | |
| 241 | + Admin_UI::init(); | |
| 242 | + } | |
| 243 | + | |
| 244 | + Divi::init(); | |
| 245 | + } | |
| 246 | + | |
| 247 | + /** | |
| 248 | + * Explicitly register VideoPress oembed provider for patterns not supported by core | |
| 249 | + * | |
| 250 | + * @return void | |
| 251 | + */ | |
| 252 | + public static function register_oembed_providers() { | |
| 253 | + $host = rawurlencode( home_url() ); | |
| 254 | + // videopress.com/v is already registered in core. | |
| 255 | + // By explicitly declaring the provider here, we can speed things up by not relying on oEmbed discovery. | |
| 256 | + wp_oembed_add_provider( '#^https?://video.wordpress.com/v/.*#', 'https://public-api.wordpress.com/oembed/?for=' . $host, true ); | |
| 257 | + // This is needed as it's not supported in oEmbed discovery. | |
| 258 | + 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 | |
| 259 | + | |
| 260 | + add_filter( 'embed_oembed_html', array( __CLASS__, 'video_enqueue_bridge_when_oembed_present' ), 10, 4 ); | |
| 261 | + } | |
| 262 | + | |
| 263 | + /** | |
| 264 | + * Enqueues VideoPress token bridge when a VideoPress oembed is present on the current page. | |
| 265 | + * | |
| 266 | + * @param string|false $cache The cached HTML result, stored in post meta. | |
| 267 | + * @param string $url The attempted embed URL. | |
| 268 | + * @param array $attr An array of shortcode attributes. | |
| 269 | + * @param int $post_ID Post ID. | |
| 270 | + * | |
| 271 | + * @return string|false | |
| 272 | + */ | |
| 273 | + public static function video_enqueue_bridge_when_oembed_present( $cache, $url, $attr, $post_ID = null ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable | |
| 274 | + if ( Utils::is_videopress_url( $url ) ) { | |
| 275 | + Jwt_Token_Bridge::enqueue_jwt_token_bridge(); | |
| 276 | + } | |
| 277 | + | |
| 278 | + return $cache; | |
| 279 | + } | |
| 280 | + | |
| 281 | + /** | |
| 282 | + * Register all VideoPress blocks | |
| 283 | + * | |
| 284 | + * @return void | |
| 285 | + */ | |
| 286 | + public static function register_videopress_blocks() { | |
| 287 | + // Register VideoPress Video block. | |
| 288 | + self::register_videopress_video_block(); | |
| 289 | + | |
| 290 | + // Register Video Playlist block. | |
| 291 | + self::register_videopress_playlist_block(); | |
| 292 | + | |
| 293 | + // Register Latest Videos Playlist block. | |
| 294 | + self::register_videopress_latest_videos_playlist_block(); | |
| 295 | + | |
| 296 | + // Register All Playlists block. | |
| 297 | + self::register_videopress_all_playlists_block(); | |
| 298 | + } | |
| 299 | + | |
| 300 | + /** | |
| 301 | + * Register the All Playlists block, which lists the site's Video Playlist | |
| 302 | + * blocks from the playlist index. | |
| 303 | + * | |
| 304 | + * @param string|null $metadata_file Path to the block.json metadata file. Defaults to the | |
| 305 | + * package build output; tests can point it at a fixture. | |
| 306 | + * | |
| 307 | + * @return void | |
| 308 | + */ | |
| 309 | + public static function register_videopress_all_playlists_block( $metadata_file = null ) { | |
| 310 | + All_Playlists_Block::register( $metadata_file ); | |
| 311 | + } | |
| 312 | + | |
| 313 | + /** | |
| 314 | + * VideoPress video block render method | |
| 315 | + * | |
| 316 | + * @global \WP_Embed $wp_embed WordPress embed handler. | |
| 317 | + * | |
| 318 | + * @param array $block_attributes Block attributes. | |
| 319 | + * @param string $content Current block markup. | |
| 320 | + * @param \WP_Block $block Current block. | |
| 321 | + * | |
| 322 | + * @return string Block markup. | |
| 323 | + */ | |
| 324 | + public static function render_videopress_video_block( $block_attributes, $content, $block ) { | |
| 325 | + global $wp_embed; | |
| 326 | + | |
| 327 | + // Pre-build and cache the GUID list for this post to optimize authorization checks, | |
| 328 | + // and record the GUID actually being rendered: by render time WordPress has expanded | |
| 329 | + // synced patterns, templates, and template parts, so this covers embedding contexts | |
| 330 | + // the static content scan cannot see. | |
| 331 | + $post_id = $block->context['postId'] ?? get_the_ID(); | |
| 332 | + if ( ! empty( $post_id ) && isset( $block_attributes['guid'] ) && is_string( $block_attributes['guid'] ) ) { | |
| 333 | + Access_Control::ensure_post_guids_cached( absint( $post_id ), $block_attributes['guid'] ); | |
| 334 | + } elseif ( ! empty( $post_id ) ) { | |
| 335 | + Access_Control::build_and_cache_post_guids( absint( $post_id ) ); | |
| 336 | + } | |
| 337 | + | |
| 338 | + // CSS classes. | |
| 339 | + $align = $block_attributes['align'] ?? null; | |
| 340 | + $align_class = $align ? ' align' . $align : ''; | |
| 341 | + $custom_class = isset( $block_attributes['className'] ) ? ' ' . $block_attributes['className'] : ''; | |
| 342 | + $classes = 'wp-block-jetpack-videopress jetpack-videopress-player' . $custom_class . $align_class; | |
| 343 | + | |
| 344 | + // Inline style. | |
| 345 | + $style = ''; | |
| 346 | + $max_width = isset( $block_attributes['maxWidth'] ) && is_string( $block_attributes['maxWidth'] ) | |
| 347 | + ? trim( $block_attributes['maxWidth'] ) | |
| 348 | + : ''; | |
| 349 | + | |
| 350 | + // maxWidth is rendered into an inline style. Accept only a plain CSS length | |
| 351 | + // or percentage so the value stays a single, well-formed declaration; | |
| 352 | + // anything else is dropped and the block renders at full width (as with the | |
| 353 | + // "100%" default). | |
| 354 | + if ( '' !== $max_width && '100%' !== $max_width | |
| 355 | + && preg_match( '/^\d+(\.\d+)?(px|%|em|rem|vw|vh|vmin|vmax|ch|ex|cm|mm|in|pt|pc|q)$/i', $max_width ) | |
| 356 | + ) { | |
| 357 | + $style = sprintf( 'max-width: %s;', $max_width ); | |
| 358 | + $classes .= ' wp-block-jetpack-videopress--has-max-width'; | |
| 359 | + } | |
| 360 | + | |
| 361 | + /* | |
| 362 | + * <figcaption /> element | |
| 363 | + * Caption is stored into the block attributes, | |
| 364 | + * but also it was stored into the <figcaption /> element, | |
| 365 | + * meaning that it could be stored in two different places. | |
| 366 | + */ | |
| 367 | + $figcaption = ''; | |
| 368 | + | |
| 369 | + // Caption from block attributes. | |
| 370 | + $caption = $block_attributes['caption'] ?? null; | |
| 371 | + | |
| 372 | + /* | |
| 373 | + * If the caption is not stored into the block attributes, | |
| 374 | + * try to get it from the <figcaption /> element. | |
| 375 | + */ | |
| 376 | + if ( null === $caption ) { | |
| 377 | + preg_match( '/<figcaption>(.*?)<\/figcaption>/', $content, $matches ); | |
| 378 | + $caption = $matches[1] ?? null; | |
| 379 | + } | |
| 380 | + | |
| 381 | + // If we have a caption, create the <figcaption /> element. | |
| 382 | + if ( null !== $caption ) { | |
| 383 | + $figcaption = sprintf( '<figcaption>%s</figcaption>', wp_kses_post( $caption ) ); | |
| 384 | + } | |
| 385 | + | |
| 386 | + // Custom anchor from block content. | |
| 387 | + $id_attribute = ''; | |
| 388 | + | |
| 389 | + // Try to get the custom anchor from the block attributes. | |
| 390 | + if ( isset( $block_attributes['anchor'] ) && $block_attributes['anchor'] ) { | |
| 391 | + $id_attribute = sprintf( 'id="%s"', esc_attr( $block_attributes['anchor'] ) ); | |
| 392 | + } elseif ( preg_match( '/<figure[^>]*id="([^"]+)"/', $content, $matches ) ) { | |
| 393 | + // Otherwise, try to get the custom anchor from the <figure /> element. | |
| 394 | + $id_attribute = sprintf( 'id="%s"', esc_attr( $matches[1] ) ); | |
| 395 | + } | |
| 396 | + | |
| 397 | + // Preview On Hover data. | |
| 398 | + $is_poh_enabled = | |
| 399 | + isset( $block_attributes['posterData']['previewOnHover'] ) && | |
| 400 | + $block_attributes['posterData']['previewOnHover']; | |
| 401 | + | |
| 402 | + $autoplay = $block_attributes['autoplay'] ?? false; | |
| 403 | + $controls = $block_attributes['controls'] ?? false; | |
| 404 | + $poster = $block_attributes['posterData']['url'] ?? null; | |
| 405 | + | |
| 406 | + $preview_on_hover = ''; | |
| 407 | + | |
| 408 | + if ( $is_poh_enabled ) { | |
| 409 | + $preview_on_hover = array( | |
| 410 | + 'previewAtTime' => $block_attributes['posterData']['previewAtTime'], | |
| 411 | + 'previewLoopDuration' => $block_attributes['posterData']['previewLoopDuration'], | |
| 412 | + 'autoplay' => $autoplay, | |
| 413 | + 'showControls' => $controls, | |
| 414 | + ); | |
| 415 | + | |
| 416 | + // Create inline style in case video has a custom poster. | |
| 417 | + $inline_style = ''; | |
| 418 | + $poster_url = self::prepare_poster_url_for_inline_style( $poster ); | |
| 419 | + if ( $poster_url ) { | |
| 420 | + // Emit the poster URL as a double-quoted CSS string so it stays | |
| 421 | + // contained within url() and cannot affect the surrounding style. | |
| 422 | + $inline_style = sprintf( | |
| 423 | + 'style="background-image: url("%s"); background-size: cover; background-position: center center;"', | |
| 424 | + $poster_url | |
| 425 | + ); | |
| 426 | + } | |
| 427 | + | |
| 428 | + // Expose the preview on hover data to the client. | |
| 429 | + $preview_on_hover = sprintf( | |
| 430 | + '<div class="jetpack-videopress-player__overlay" %s></div><script type="application/json">%s</script>', | |
| 431 | + $inline_style, | |
| 432 | + wp_json_encode( $preview_on_hover, JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP ) | |
| 433 | + ); | |
| 434 | + | |
| 435 | + // Set `autoplay` and `muted` attributes to the video element. | |
| 436 | + $block_attributes['autoplay'] = true; | |
| 437 | + $block_attributes['muted'] = true; | |
| 438 | + } | |
| 439 | + | |
| 440 | + $figure_template = ' | |
| 441 | + <figure class="%1$s" style="%2$s" %3$s> | |
| 442 | + %4$s | |
| 443 | + %5$s | |
| 444 | + %6$s | |
| 445 | + </figure> | |
| 446 | + '; | |
| 447 | + | |
| 448 | + // VideoPress URL. | |
| 449 | + $guid = $block_attributes['guid'] ?? null; | |
| 450 | + $videopress_url = Utils::get_video_press_url( $guid, $block_attributes ); | |
| 451 | + | |
| 452 | + $video_wrapper = ''; | |
| 453 | + $video_wrapper_classes = 'jetpack-videopress-player__wrapper'; | |
| 454 | + | |
| 455 | + // Preview on hover drives the player through the iframe API, so it keeps the iframe. | |
| 456 | + if ( $guid && ! $is_poh_enabled && Inline_Player::is_enabled() ) { | |
| 457 | + $video_wrapper = sprintf( | |
| 458 | + '<div class="%s">%s</div>', | |
| 459 | + $video_wrapper_classes, | |
| 460 | + Inline_Player::render( | |
| 461 | + $guid, | |
| 462 | + Inline_Player::get_player_options( $block_attributes ), | |
| 463 | + $block_attributes['videoRatio'] ?? null, | |
| 464 | + array( | |
| 465 | + 'poster' => Inline_Player::get_poster_url( $guid, $block_attributes ), | |
| 466 | + 'title' => $block_attributes['title'] ?? '', | |
| 467 | + ) | |
| 468 | + ) | |
| 469 | + ); | |
| 470 | + } elseif ( $videopress_url ) { | |
| 471 | + $videopress_url = wp_kses_post( $videopress_url ); | |
| 472 | + | |
| 473 | + /* | |
| 474 | + * Provide a fallback iframe for when the oEmbed endpoint fails, e.g. | |
| 475 | + * when the VideoPress backend isn't ready for a freshly uploaded video. | |
| 476 | + * This prevents the published page from showing a bare link. | |
| 477 | + */ | |
| 478 | + $fallback = function ( $output, $url ) use ( $videopress_url ) { | |
| 479 | + $decoded_url = html_entity_decode( $videopress_url, ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401 ); | |
| 480 | + if ( $decoded_url !== $url ) { | |
| 481 | + return $output; | |
| 482 | + } | |
| 483 | + | |
| 484 | + return sprintf( | |
| 485 | + '<iframe title="%1$s" aria-label="%1$s" src="%2$s" width="640" height="360" allowfullscreen data-resize-to-parent="true" allow="clipboard-write; presentation"></iframe>', | |
| 486 | + esc_attr__( 'VideoPress Video Player', 'jetpack-videopress-pkg' ), | |
| 487 | + esc_url( preg_replace( '#/v/#', '/embed/', $url, 1 ) ) | |
| 488 | + ); | |
| 489 | + }; | |
| 490 | + | |
| 491 | + add_filter( 'embed_maybe_make_link', $fallback, 10, 2 ); | |
| 492 | + $oembed_html = apply_filters( 'video_embed_html', $wp_embed->shortcode( array(), $videopress_url ) ); | |
| 493 | + remove_filter( 'embed_maybe_make_link', $fallback ); | |
| 494 | + | |
| 495 | + $video_wrapper = sprintf( | |
| 496 | + '<div class="%s">%s %s</div>', | |
| 497 | + $video_wrapper_classes, | |
| 498 | + $preview_on_hover, | |
| 499 | + $oembed_html | |
| 500 | + ); | |
| 501 | + | |
| 502 | + /* | |
| 503 | + * Self-heal failed oEmbed cache for VideoPress URLs. | |
| 504 | + * | |
| 505 | + * When the VideoPress backend isn't ready for a freshly uploaded video, | |
| 506 | + * WordPress caches '{{unknown}}' in post meta with a TTL that is too long | |
| 507 | + * for this use case. Clear recent failures so the next page render retries | |
| 508 | + * oEmbed discovery, keeping the fallback iframe above temporary. | |
| 509 | + */ | |
| 510 | + $post_id = $block->context['postId'] ?? get_the_ID(); | |
| 511 | + | |
| 512 | + if ( $post_id ) { | |
| 513 | + $key_suffix = md5( $videopress_url . serialize( wp_embed_defaults( $videopress_url ) ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize -- Matching WP_Embed cache key format. | |
| 514 | + $oembed_value = get_post_meta( $post_id, '_oembed_' . $key_suffix, true ); | |
| 515 | + $oembed_time = (int) get_post_meta( $post_id, '_oembed_time_' . $key_suffix, true ); | |
| 516 | + | |
| 517 | + /* | |
| 518 | + * Only clear the '{{unknown}}' cache entry when it is recent, to avoid | |
| 519 | + * disabling WordPress's oEmbed backoff for persistent provider failures. | |
| 520 | + */ | |
| 521 | + if ( | |
| 522 | + '{{unknown}}' === $oembed_value | |
| 523 | + && ( ! $oembed_time || ( time() - $oembed_time ) < MINUTE_IN_SECONDS ) | |
| 524 | + ) { | |
| 525 | + delete_post_meta( $post_id, '_oembed_' . $key_suffix ); | |
| 526 | + delete_post_meta( $post_id, '_oembed_time_' . $key_suffix ); | |
| 527 | + } | |
| 528 | + } | |
| 529 | + } | |
| 530 | + | |
| 531 | + // Get premium content from block context. | |
| 532 | + $premium_block_plan_id = isset( $block->context['premium-content/planId'] ) ? intval( $block->context['premium-content/planId'] ) : 0; | |
| 533 | + $is_premium_content_child = isset( $block->context['isPremiumContentChild'] ) ? (bool) $block->context['isPremiumContentChild'] : false; | |
| 534 | + $maybe_premium_script = ''; | |
| 535 | + if ( $is_premium_content_child && is_string( $guid ) ) { | |
| 536 | + Access_Control::instance()->set_guid_subscription( $guid, $premium_block_plan_id ); | |
| 537 | + $escaped_guid = wp_json_encode( $guid, JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP ); | |
| 538 | + $script_content = "if ( ! window.__guidsToPlanIds ) { window.__guidsToPlanIds = {}; }; window.__guidsToPlanIds[$escaped_guid] = $premium_block_plan_id;"; | |
| 539 | + $maybe_premium_script = '<script>' . $script_content . '</script>'; | |
| 540 | + } | |
| 541 | + | |
| 542 | + // $id_attribute, $video_wrapper, $figcaption properly escaped earlier in the code. | |
| 543 | + return sprintf( | |
| 544 | + $figure_template, | |
| 545 | + esc_attr( $classes ), | |
| 546 | + esc_attr( $style ), | |
| 547 | + $id_attribute, | |
| 548 | + $video_wrapper, | |
| 549 | + $figcaption, | |
| 550 | + $maybe_premium_script | |
| 551 | + ); | |
| 552 | + } | |
| 553 | + | |
| 554 | + /** | |
| 555 | + * Register the VideoPress block editor block, | |
| 556 | + * AKA "VideoPress Block v6". | |
| 557 | + * | |
| 558 | + * @return void | |
| 559 | + */ | |
| 560 | + public static function register_videopress_video_block() { | |
| 561 | + /* | |
| 562 | + * If only Jetpack is active, and if the VideoPress module is not active, | |
| 563 | + * we can register the block just to display a placeholder to turn on the module. | |
| 564 | + * That invitation is only useful for admins though. | |
| 565 | + */ | |
| 566 | + if ( | |
| 567 | + Status::is_jetpack_plugin_without_videopress_module_active() | |
| 568 | + && ! Status::is_standalone_plugin_active() | |
| 569 | + && ! current_user_can( 'jetpack_activate_modules' ) | |
| 570 | + ) { | |
| 571 | + return; | |
| 572 | + } | |
| 573 | + | |
| 574 | + $videopress_video_metadata_file = __DIR__ . '/../build/block-editor/blocks/video/block.json'; | |
| 575 | + $videopress_video_metadata_file_exists = file_exists( $videopress_video_metadata_file ); | |
| 576 | + if ( ! $videopress_video_metadata_file_exists ) { | |
| 577 | + return; | |
| 578 | + } | |
| 579 | + | |
| 580 | + $videopress_video_metadata = json_decode( | |
| 581 | + // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents | |
| 582 | + file_get_contents( $videopress_video_metadata_file ) | |
| 583 | + ); | |
| 584 | + | |
| 585 | + // Pick the block name straight from the block metadata .json file. | |
| 586 | + $videopress_video_block_name = $videopress_video_metadata->name; | |
| 587 | + | |
| 588 | + // Is the block already registered? | |
| 589 | + $is_block_registered = \WP_Block_Type_Registry::get_instance()->is_registered( $videopress_video_block_name ); | |
| 590 | + | |
| 591 | + // Do not register if the block is already registered. | |
| 592 | + if ( $is_block_registered ) { | |
| 593 | + return; | |
| 594 | + } | |
| 595 | + | |
| 596 | + $registration = register_block_type( | |
| 597 | + $videopress_video_metadata_file, | |
| 598 | + array( | |
| 599 | + 'render_callback' => array( __CLASS__, 'render_videopress_video_block' ), | |
| 600 | + 'render_email_callback' => array( Video_Block_Email_Renderer::class, 'render' ), | |
| 601 | + 'uses_context' => array( 'premium-content/planId', 'isPremiumContentChild', 'selectedPlanId' ), | |
| 602 | + ) | |
| 603 | + ); | |
| 604 | + | |
| 605 | + // Do not enqueue scripts if the block could not be registered. | |
| 606 | + if ( empty( $registration ) || empty( $registration->editor_script_handles ) ) { | |
| 607 | + return; | |
| 608 | + } | |
| 609 | + | |
| 610 | + // Extensions use Connection_Initial_State::render_script with script handle as parameter. | |
| 611 | + if ( is_array( $registration->editor_script_handles ) ) { | |
| 612 | + $script_handle = $registration->editor_script_handles[0]; | |
| 613 | + } else { | |
| 614 | + $script_handle = $registration->editor_script_handles; | |
| 615 | + } | |
| 616 | + | |
| 617 | + // Register and enqueue scripts used by the VideoPress video block. | |
| 618 | + Block_Editor_Extensions::init( $script_handle ); | |
| 619 | + } | |
| 620 | + | |
| 621 | + /** | |
| 622 | + * Register the Video Playlist block. | |
| 623 | + * | |
| 624 | + * @param string|null $metadata_file Path to the block.json metadata file. Defaults to the | |
| 625 | + * package build output; tests can point it at a fixture. | |
| 626 | + * | |
| 627 | + * @return void | |
| 628 | + */ | |
| 629 | + public static function register_videopress_playlist_block( $metadata_file = null ) { | |
| 630 | + /* | |
| 631 | + * Unlike the video block, the playlist block has no "activate the module" | |
| 632 | + * placeholder, so it is only registered where VideoPress can play videos. | |
| 633 | + */ | |
| 634 | + if ( | |
| 635 | + Status::is_jetpack_plugin_without_videopress_module_active() | |
| 636 | + && ! Status::is_standalone_plugin_active() | |
| 637 | + ) { | |
| 638 | + return; | |
| 639 | + } | |
| 640 | + | |
| 641 | + if ( null === $metadata_file ) { | |
| 642 | + $metadata_file = __DIR__ . '/../build/block-editor/blocks/playlist/block.json'; | |
| 643 | + } | |
| 644 | + | |
| 645 | + if ( ! file_exists( $metadata_file ) ) { | |
| 646 | + return; | |
| 647 | + } | |
| 648 | + | |
| 649 | + $metadata = json_decode( | |
| 650 | + // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents | |
| 651 | + file_get_contents( $metadata_file ) | |
| 652 | + ); | |
| 653 | + | |
| 654 | + if ( empty( $metadata->name ) | |
| 655 | + || \WP_Block_Type_Registry::get_instance()->is_registered( $metadata->name ) | |
| 656 | + ) { | |
| 657 | + return; | |
| 658 | + } | |
| 659 | + | |
| 660 | + register_block_type( | |
| 661 | + $metadata_file, | |
| 662 | + array( | |
| 663 | + 'render_callback' => array( __CLASS__, 'render_videopress_playlist_block' ), | |
| 664 | + ) | |
| 665 | + ); | |
| 666 | + } | |
| 667 | + | |
| 668 | + /** | |
| 669 | + * Register the Latest Videos Playlist block. | |
| 670 | + * | |
| 671 | + * It reuses the Video Playlist block's registered view script and styles, so | |
| 672 | + * it is only registered once that block is. Its inner Video Playlist block is | |
| 673 | + * the editor canvas only: the front end renders the newest videos fresh. | |
| 674 | + * | |
| 675 | + * @param string|null $metadata_file Path to the block.json metadata file. Defaults to the | |
| 676 | + * package build output; tests can point it at a fixture. | |
| 677 | + * | |
| 678 | + * @return void | |
| 679 | + */ | |
| 680 | + public static function register_videopress_latest_videos_playlist_block( $metadata_file = null ) { | |
| 681 | + if ( ! \WP_Block_Type_Registry::get_instance()->is_registered( 'videopress/playlist' ) ) { | |
| 682 | + return; | |
| 683 | + } | |
| 684 | + | |
| 685 | + if ( null === $metadata_file ) { | |
| 686 | + $metadata_file = __DIR__ . '/../build/block-editor/blocks/latest-videos-playlist/block.json'; | |
| 687 | + } | |
| 688 | + | |
| 689 | + if ( ! file_exists( $metadata_file ) ) { | |
| 690 | + return; | |
| 691 | + } | |
| 692 | + | |
| 693 | + $metadata = json_decode( | |
| 694 | + // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents | |
| 695 | + file_get_contents( $metadata_file ) | |
| 696 | + ); | |
| 697 | + | |
| 698 | + if ( empty( $metadata->name ) | |
| 699 | + || \WP_Block_Type_Registry::get_instance()->is_registered( $metadata->name ) | |
| 700 | + ) { | |
| 701 | + return; | |
| 702 | + } | |
| 703 | + | |
| 704 | + register_block_type( | |
| 705 | + $metadata_file, | |
| 706 | + array( | |
| 707 | + 'render_callback' => array( __CLASS__, 'render_videopress_latest_videos_playlist_block' ), | |
| 708 | + 'skip_inner_blocks' => true, | |
| 709 | + ) | |
| 710 | + ); | |
| 711 | + } | |
| 712 | + | |
| 713 | + /** | |
| 714 | + * Latest Videos Playlist block render callback: the newest VideoPress videos | |
| 715 | + * on the site, rendered by the Video Playlist block's callback. | |
| 716 | + * | |
| 717 | + * @param array $block_attributes Block attributes. | |
| 718 | + * @param string $content Current block markup, unused: the inner block is never rendered. | |
| 719 | + * @param \WP_Block|null $block Current block. | |
| 720 | + * | |
| 721 | + * @return string Block markup, or an empty string when the site has no VideoPress videos. | |
| 722 | + */ | |
| 723 | + public static function render_videopress_latest_videos_playlist_block( $block_attributes, $content = '', $block = null ) { | |
| 724 | + $count = isset( $block_attributes['count'] ) && is_numeric( $block_attributes['count'] ) | |
| 725 | + ? (int) $block_attributes['count'] | |
| 726 | + : self::LATEST_VIDEOS_PLAYLIST_DEFAULT_COUNT; | |
| 727 | + $count = max( self::LATEST_VIDEOS_PLAYLIST_MIN_COUNT, min( self::LATEST_VIDEOS_PLAYLIST_MAX_COUNT, $count ) ); | |
| 728 | + | |
| 729 | + $block_attributes['videos'] = Data::get_latest_videopress_playlist_entries( $count ); | |
| 730 | + | |
| 731 | + // Dynamic playlists have no title of their own, so no heading either. | |
| 732 | + unset( $block_attributes['playlistTitle'], $block_attributes['showPlaylistTitle'] ); | |
| 733 | + | |
| 734 | + return self::render_videopress_playlist_block( $block_attributes, '', $block ); | |
| 735 | + } | |
| 736 | + | |
| 737 | + /** | |
| 738 | + * Sanitize the playlist block's videos attribute into rendering-ready entries. | |
| 739 | + * | |
| 740 | + * @param mixed $videos Raw attribute value. | |
| 741 | + * | |
| 742 | + * @return array Entries with guid, title, durationMs, height and poster keys. | |
| 743 | + */ | |
| 744 | + public static function sanitize_playlist_entries( $videos ) { | |
| 745 | + if ( ! is_array( $videos ) ) { | |
| 746 | + return array(); | |
| 747 | + } | |
| 748 | + | |
| 749 | + $entries = array(); | |
| 750 | + foreach ( $videos as $video ) { | |
| 751 | + if ( ! is_array( $video ) || empty( $video['guid'] ) || ! is_string( $video['guid'] ) ) { | |
| 752 | + continue; | |
| 753 | + } | |
| 754 | + | |
| 755 | + // VideoPress GUIDs are 8 alphanumeric characters; drop anything else. | |
| 756 | + if ( ! preg_match( '/^[a-zA-Z0-9]{8}$/', $video['guid'] ) ) { | |
| 757 | + continue; | |
| 758 | + } | |
| 759 | + | |
| 760 | + /* | |
| 761 | + * Only the video reference and numeric metadata are stored. Display | |
| 762 | + * metadata (title, poster) is always live: the view script reads it | |
| 763 | + * from the video data after the page loads. | |
| 764 | + */ | |
| 765 | + $entries[] = array( | |
| 766 | + 'guid' => $video['guid'], | |
| 767 | + 'durationMs' => isset( $video['durationMs'] ) && is_numeric( $video['durationMs'] ) ? max( 0, (int) $video['durationMs'] ) : 0, | |
| 768 | + 'height' => isset( $video['height'] ) && is_numeric( $video['height'] ) ? max( 0, (int) $video['height'] ) : 0, | |
| 769 | + ); | |
| 770 | + } | |
| 771 | + | |
| 772 | + return $entries; | |
| 773 | + } | |
| 774 | + | |
| 775 | + /** | |
| 776 | + * Build the VideoPress embed URL for a playlist entry. | |
| 777 | + * | |
| 778 | + * @param string $guid Video GUID. | |
| 779 | + * @param bool $autoplay Whether the video should start playing once loaded. | |
| 780 | + * @param bool $muted Whether playback should start muted. | |
| 781 | + * | |
| 782 | + * @return string Embed URL. | |
| 783 | + */ | |
| 784 | + private static function playlist_embed_url( $guid, $autoplay, $muted = false ) { | |
| 785 | + $args = array( | |
| 786 | + 'cover' => 1, | |
| 787 | + 'preloadContent' => Data::get_videopress_player_preload_disabled() ? 'none' : 'metadata', | |
| 788 | + 'autoPlay' => $autoplay ? 1 : 0, | |
| 789 | + ); | |
| 790 | + if ( $muted ) { | |
| 791 | + $args['muted'] = 1; | |
| 792 | + } | |
| 793 | + | |
| 794 | + return add_query_arg( | |
| 795 | + $args, | |
| 796 | + 'https://videopress.com/embed/' . rawurlencode( $guid ) | |
| 797 | + ); | |
| 798 | + } | |
| 799 | + | |
| 800 | + /** | |
| 801 | + * Format a duration in milliseconds as a timecode, m:ss or h:mm:ss. | |
| 802 | + * | |
| 803 | + * @param int $duration_ms Duration in milliseconds. | |
| 804 | + * | |
| 805 | + * @return string Timecode, or an empty string when the duration is unknown. | |
| 806 | + */ | |
| 807 | + private static function playlist_timecode( $duration_ms ) { | |
| 808 | + if ( $duration_ms <= 0 ) { | |
| 809 | + return ''; | |
| 810 | + } | |
| 811 | + | |
| 812 | + $total_seconds = (int) round( $duration_ms / 1000 ); | |
| 813 | + $hours = intdiv( $total_seconds, 3600 ); | |
| 814 | + $minutes = intdiv( $total_seconds % 3600, 60 ); | |
| 815 | + $seconds = $total_seconds % 60; | |
| 816 | + | |
| 817 | + if ( $hours > 0 ) { | |
| 818 | + return sprintf( '%d:%02d:%02d', $hours, $minutes, $seconds ); | |
| 819 | + } | |
| 820 | + | |
| 821 | + return sprintf( '%d:%02d', $minutes, $seconds ); | |
| 822 | + } | |
| 823 | + | |
| 824 | + /** | |
| 825 | + * Format a duration in milliseconds as a long runtime, e.g. "1 hr 13 min". | |
| 826 | + * | |
| 827 | + * @param int $duration_ms Duration in milliseconds. | |
| 828 | + * | |
| 829 | + * @return string Runtime label, or an empty string when the duration is unknown. | |
| 830 | + */ | |
| 831 | + private static function playlist_runtime_label( $duration_ms ) { | |
| 832 | + if ( $duration_ms <= 0 ) { | |
| 833 | + return ''; | |
| 834 | + } | |
| 835 | + | |
| 836 | + $total_minutes = max( 1, (int) round( $duration_ms / 60000 ) ); | |
| 837 | + $hours = intdiv( $total_minutes, 60 ); | |
| 838 | + $minutes = $total_minutes % 60; | |
| 839 | + | |
| 840 | + if ( $hours > 0 && $minutes > 0 ) { | |
| 841 | + /* translators: 1: number of hours. 2: number of minutes. */ | |
| 842 | + return sprintf( __( '%1$d hr %2$d min', 'jetpack-videopress-pkg' ), $hours, $minutes ); | |
| 843 | + } | |
| 844 | + | |
| 845 | + if ( $hours > 0 ) { | |
| 846 | + /* translators: %d: number of hours. */ | |
| 847 | + return sprintf( __( '%d hr', 'jetpack-videopress-pkg' ), $hours ); | |
| 848 | + } | |
| 849 | + | |
| 850 | + /* translators: %d: number of minutes. */ | |
| 851 | + return sprintf( __( '%d min', 'jetpack-videopress-pkg' ), $minutes ); | |
| 852 | + } | |
| 853 | + | |
| 854 | + /** | |
| 855 | + * Map a video's pixel height to a resolution label, e.g. "1080p" or "4K". | |
| 856 | + * | |
| 857 | + * @param int $height Video height in pixels. | |
| 858 | + * | |
| 859 | + * @return string Resolution label, or an empty string when the height is unknown. | |
| 860 | + */ | |
| 861 | + private static function playlist_resolution_label( $height ) { | |
| 862 | + if ( $height <= 0 ) { | |
| 863 | + return ''; | |
| 864 | + } | |
| 865 | + | |
| 866 | + return $height >= 2160 ? '4K' : $height . 'p'; | |
| 867 | + } | |
| 868 | + | |
| 869 | + /** | |
| 870 | + * Video Playlist block render callback. | |
| 871 | + * | |
| 872 | + * @param array $block_attributes Block attributes. | |
| 873 | + * @param string $content Rendered inner blocks: the title heading, when there is one. | |
| 874 | + * @param \WP_Block|null $block Current block. | |
| 875 | + * | |
| 876 | + * @return string Block markup, or an empty string when the playlist has no playable entries. | |
| 877 | + */ | |
| 878 | + public static function render_videopress_playlist_block( $block_attributes, $content = '', $block = null ) { | |
| 879 | + $entries = self::sanitize_playlist_entries( $block_attributes['videos'] ?? null ); | |
| 880 | + | |
| 881 | + if ( ! $entries ) { | |
| 882 | + return ''; | |
| 883 | + } | |
| 884 | + | |
| 885 | + // Record the rendered GUIDs in the post's cached GUID list so private playlist | |
| 886 | + // entries pass the playback authorization check, including when the playlist | |
| 887 | + // sits inside a synced pattern, template, or template part. | |
| 888 | + $post_id = $block->context['postId'] ?? get_the_ID(); | |
| 889 | + if ( ! empty( $post_id ) ) { | |
| 890 | + Access_Control::ensure_post_guids_cached( absint( $post_id ), array_column( $entries, 'guid' ) ); | |
| 891 | + } | |
| 892 | + | |
| 893 | + $enabled = function ( $key, $default_value = true ) use ( $block_attributes ) { | |
| 894 | + return isset( $block_attributes[ $key ] ) ? (bool) $block_attributes[ $key ] : $default_value; | |
| 895 | + }; | |
| 896 | + | |
| 897 | + $layout = isset( $block_attributes['layout'] ) && in_array( $block_attributes['layout'], array( 'side-rail', 'grid', 'strip' ), true ) | |
| 898 | + ? $block_attributes['layout'] | |
| 899 | + : 'side-rail'; | |
| 900 | + | |
| 901 | + $show_player = $enabled( 'showPlayer' ); | |
| 902 | + // A hidden player can still be revealed by the first click on an entry. | |
| 903 | + $reveal_player = ! $show_player | |
| 904 | + && isset( $block_attributes['entryClickAction'] ) | |
| 905 | + && 'show-player' === $block_attributes['entryClickAction']; | |
| 906 | + $has_player = $show_player || $reveal_player; | |
| 907 | + $show_thumbnail = $enabled( 'showThumbnail' ); | |
| 908 | + $show_title = $enabled( 'showTitle' ); | |
| 909 | + $show_res = $enabled( 'showResolution' ); | |
| 910 | + $show_duration = $enabled( 'showDuration' ); | |
| 911 | + $show_number = $enabled( 'showPositionNumber', false ); | |
| 912 | + $show_runtime = $enabled( 'showTotalRuntime' ); | |
| 913 | + $muted = $enabled( 'muteByDefault', false ); | |
| 914 | + | |
| 915 | + $classes = array( 'videopress-playlist', 'is-layout-' . $layout ); | |
| 916 | + if ( $enabled( 'darkPlayer', false ) ) { | |
| 917 | + $classes[] = 'is-dark'; | |
| 918 | + } | |
| 919 | + if ( ! $show_player ) { | |
| 920 | + $classes[] = 'hide-player'; | |
| 921 | + } | |
| 922 | + if ( ! $show_thumbnail ) { | |
| 923 | + $classes[] = 'hide-thumbnails'; | |
| 924 | + } | |
| 925 | + if ( ! $show_title ) { | |
| 926 | + $classes[] = 'hide-titles'; | |
| 927 | + } | |
| 928 | + if ( ! $show_res ) { | |
| 929 | + $classes[] = 'hide-resolutions'; | |
| 930 | + } | |
| 931 | + if ( ! $show_duration ) { | |
| 932 | + $classes[] = 'hide-durations'; | |
| 933 | + } | |
| 934 | + if ( ! $show_runtime ) { | |
| 935 | + $classes[] = 'hide-runtime'; | |
| 936 | + } | |
| 937 | + | |
| 938 | + // Lets the embed play private videos for authorized viewers. | |
| 939 | + Jwt_Token_Bridge::enqueue_jwt_token_bridge(); | |
| 940 | + | |
| 941 | + $count = count( $entries ); | |
| 942 | + $total_ms = 0; | |
| 943 | + foreach ( $entries as $entry ) { | |
| 944 | + $total_ms += $entry['durationMs']; | |
| 945 | + } | |
| 946 | + | |
| 947 | + $total_timecode = self::playlist_timecode( $total_ms ); | |
| 948 | + /* translators: %d: number of videos in the playlist. */ | |
| 949 | + $count_label = sprintf( _n( '%d video', '%d videos', $count, 'jetpack-videopress-pkg' ), $count ); | |
| 950 | + | |
| 951 | + /* | |
| 952 | + * Hidden placeholder shown (via the button's is-locked class) when the view | |
| 953 | + * script cannot authorize a private video's thumbnail for the viewer. | |
| 954 | + */ | |
| 955 | + $lock_markup = '<span class="videopress-playlist__entry-lock">' | |
| 956 | + . '<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>' | |
| 957 | + . '<span class="videopress-playlist__entry-lock-label">' . esc_html__( 'Private video', 'jetpack-videopress-pkg' ) . '</span>' | |
| 958 | + . '</span>'; | |
| 959 | + | |
| 960 | + $items = ''; | |
| 961 | + foreach ( $entries as $index => $entry ) { | |
| 962 | + /* | |
| 963 | + * Positional fallback only: the view script replaces titles (and | |
| 964 | + * adds posters) with live video data once the page loads. | |
| 965 | + */ | |
| 966 | + /* translators: %d: position of the video in the playlist. */ | |
| 967 | + $title = sprintf( __( 'Video %d', 'jetpack-videopress-pkg' ), $index + 1 ); | |
| 968 | + | |
| 969 | + $timecode = self::playlist_timecode( $entry['durationMs'] ); | |
| 970 | + $resolution = self::playlist_resolution_label( $entry['height'] ); | |
| 971 | + /* translators: 1: position of the video in the playlist. 2: number of videos in the playlist. */ | |
| 972 | + $position = sprintf( __( '%1$d of %2$d', 'jetpack-videopress-pkg' ), $index + 1, $count ); | |
| 973 | + $details = implode( ' · ', array_filter( array( $resolution, $timecode ) ) ); | |
| 974 | + $progress = '' !== $total_timecode | |
| 975 | + /* translators: 1: position of the video in the playlist. 2: number of videos. 3: total playlist timecode. */ | |
| 976 | + ? sprintf( __( '%1$d / %2$d · %3$s total', 'jetpack-videopress-pkg' ), $index + 1, $count, $total_timecode ) | |
| 977 | + /* translators: 1: position of the video in the playlist. 2: number of videos. */ | |
| 978 | + : sprintf( __( '%1$d / %2$d', 'jetpack-videopress-pkg' ), $index + 1, $count ); | |
| 979 | + | |
| 980 | + $number_markup = $show_number | |
| 981 | + ? sprintf( | |
| 982 | + '<span class="videopress-playlist__entry-number">%s</span>', | |
| 983 | + esc_html( str_pad( (string) ( $index + 1 ), 2, '0', STR_PAD_LEFT ) ) | |
| 984 | + ) | |
| 985 | + : ''; | |
| 986 | + | |
| 987 | + $time_markup = '' !== $timecode | |
| 988 | + ? sprintf( '<span class="videopress-playlist__entry-time">%s</span>', esc_html( $timecode ) ) | |
| 989 | + : ''; | |
| 990 | + | |
| 991 | + $resolution_markup = '' !== $resolution | |
| 992 | + ? sprintf( '<span class="videopress-playlist__entry-resolution">%s</span>', esc_html( $resolution ) ) | |
| 993 | + : ''; | |
| 994 | + | |
| 995 | + $duration_markup = '' !== $timecode | |
| 996 | + ? sprintf( '<span class="videopress-playlist__entry-duration">%s</span>', esc_html( $timecode ) ) | |
| 997 | + : ''; | |
| 998 | + | |
| 999 | + if ( $has_player ) { | |
| 1000 | + $is_current = $show_player && 0 === $index; | |
| 1001 | + $entry_open = sprintf( | |
| 1002 | + '<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">', | |
| 1003 | + $is_current ? ' is-current' : '', | |
| 1004 | + $is_current ? ' aria-current="true"' : '', | |
| 1005 | + esc_attr( $entry['guid'] ), | |
| 1006 | + esc_url( self::playlist_embed_url( $entry['guid'], true, $muted ) ), | |
| 1007 | + esc_attr( $title ), | |
| 1008 | + esc_attr( $position ), | |
| 1009 | + esc_attr( $details ), | |
| 1010 | + esc_attr( $progress ) | |
| 1011 | + ); | |
| 1012 | + $entry_close = '</button>'; | |
| 1013 | + } else { | |
| 1014 | + // No player to load the video into: the entry opens its VideoPress page instead. | |
| 1015 | + $entry_open = sprintf( | |
| 1016 | + '<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">', | |
| 1017 | + /** | |
| 1018 | + * Filters where a playlist entry opens when the block has no player. | |
| 1019 | + * | |
| 1020 | + * @since 0.55.0 | |
| 1021 | + * | |
| 1022 | + * @param string $url The video's page on videopress.com. | |
| 1023 | + * @param string $guid The video GUID. | |
| 1024 | + */ | |
| 1025 | + esc_url( apply_filters( 'videopress_playlist_entry_url', 'https://videopress.com/v/' . $entry['guid'], $entry['guid'] ) ), | |
| 1026 | + esc_attr( $entry['guid'] ), | |
| 1027 | + esc_attr( $title ), | |
| 1028 | + esc_attr( $position ), | |
| 1029 | + esc_attr( $details ) | |
| 1030 | + ); | |
| 1031 | + $entry_close = '</a>'; | |
| 1032 | + } | |
| 1033 | + | |
| 1034 | + $items .= sprintf( | |
| 1035 | + '<li class="videopress-playlist__entry">%1$s' . | |
| 1036 | + '%2$s<span class="videopress-playlist__entry-thumb"><span class="videopress-playlist__entry-flag">%3$s</span>%4$s%5$s</span>' . | |
| 1037 | + '<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>' . | |
| 1038 | + '%9$s</li>', | |
| 1039 | + $entry_open, | |
| 1040 | + $number_markup, | |
| 1041 | + esc_html__( 'Playing', 'jetpack-videopress-pkg' ), | |
| 1042 | + $lock_markup, | |
| 1043 | + $time_markup, | |
| 1044 | + esc_html( $title ), | |
| 1045 | + $resolution_markup, | |
| 1046 | + $duration_markup, | |
| 1047 | + $entry_close | |
| 1048 | + ); | |
| 1049 | + } | |
| 1050 | + | |
| 1051 | + $first = $entries[0]; | |
| 1052 | + $first_title = __( 'Video 1', 'jetpack-videopress-pkg' ); | |
| 1053 | + $runtime_label = self::playlist_runtime_label( $total_ms ); | |
| 1054 | + $runtime_markup = $show_runtime && '' !== $runtime_label | |
| 1055 | + ? sprintf( '<span class="videopress-playlist__runtime">%s</span>', esc_html( $runtime_label ) ) | |
| 1056 | + : ''; | |
| 1057 | + | |
| 1058 | + // Count · runtime meta line, shown by the side-rail layout in the list header. | |
| 1059 | + $list_meta_markup = sprintf( | |
| 1060 | + '<span class="videopress-playlist__list-meta"><span class="videopress-playlist__count">%s</span>%s</span>', | |
| 1061 | + esc_html( $count_label ), | |
| 1062 | + $runtime_markup | |
| 1063 | + ); | |
| 1064 | + | |
| 1065 | + /* | |
| 1066 | + * The player renders its own title overlay, so the stage carries no | |
| 1067 | + * duplicate now-playing text — only the grid layout's runtime line. | |
| 1068 | + */ | |
| 1069 | + $now_markup = $show_runtime && '' !== $runtime_label | |
| 1070 | + ? sprintf( | |
| 1071 | + '<div class="videopress-playlist__now"><span class="videopress-playlist__now-runtime">%s</span></div>', | |
| 1072 | + esc_html( $count_label . ' · ' . $runtime_label ) | |
| 1073 | + ) | |
| 1074 | + : ''; | |
| 1075 | + | |
| 1076 | + if ( $show_player ) { | |
| 1077 | + $stage_markup = sprintf( | |
| 1078 | + '<div class="videopress-playlist__stage">' . | |
| 1079 | + '<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>', | |
| 1080 | + esc_attr( $first_title ), | |
| 1081 | + esc_url( self::playlist_embed_url( $first['guid'], false, $muted ) ), | |
| 1082 | + $now_markup | |
| 1083 | + ); | |
| 1084 | + } elseif ( $reveal_player ) { | |
| 1085 | + // No src: nothing loads until the view script reveals the stage on a click. | |
| 1086 | + $stage_markup = sprintf( | |
| 1087 | + '<div class="videopress-playlist__stage" hidden>' . | |
| 1088 | + '<div class="videopress-playlist__player"><iframe class="videopress-playlist__iframe" title="%1$s" allowfullscreen allow="clipboard-write"></iframe></div></div>%2$s', | |
| 1089 | + esc_attr( $first_title ), | |
| 1090 | + $now_markup | |
| 1091 | + ); | |
| 1092 | + } else { | |
| 1093 | + // Without a player only the grid layout's runtime line remains of the stage. | |
| 1094 | + $stage_markup = $now_markup; | |
| 1095 | + } | |
| 1096 | + | |
| 1097 | + $progress_markup = $has_player && '' !== $total_timecode | |
| 1098 | + ? sprintf( | |
| 1099 | + '<span class="videopress-playlist__list-progress">%s</span>', | |
| 1100 | + /* translators: 1: position of the current video. 2: number of videos. 3: total playlist timecode. */ | |
| 1101 | + esc_html( sprintf( __( '%1$d / %2$d · %3$s total', 'jetpack-videopress-pkg' ), 1, $count, $total_timecode ) ) | |
| 1102 | + ) | |
| 1103 | + : ''; | |
| 1104 | + | |
| 1105 | + $list_markup = sprintf( | |
| 1106 | + '<div class="videopress-playlist__list">' . | |
| 1107 | + '<div class="videopress-playlist__list-header">' . | |
| 1108 | + '<span class="videopress-playlist__list-label videopress-playlist__list-label--rail">%1$s</span>' . | |
| 1109 | + '<span class="videopress-playlist__list-label videopress-playlist__list-label--strip">%2$s</span>%3$s%4$s</div>' . | |
| 1110 | + '<ol class="videopress-playlist__entries">%5$s</ol></div>', | |
| 1111 | + $show_player | |
| 1112 | + ? esc_html__( 'Up next', 'jetpack-videopress-pkg' ) | |
| 1113 | + : esc_html__( 'Playlist', 'jetpack-videopress-pkg' ), | |
| 1114 | + /* translators: %s: number of videos in the playlist, e.g. "5 videos". */ | |
| 1115 | + esc_html( sprintf( __( 'Playlist — %s', 'jetpack-videopress-pkg' ), $count_label ) ), | |
| 1116 | + $list_meta_markup, | |
| 1117 | + $progress_markup, | |
| 1118 | + $items | |
| 1119 | + ); | |
| 1120 | + | |
| 1121 | + /* | |
| 1122 | + * User-selected theme font presets (theme.json slugs) for the | |
| 1123 | + * customizable titles, exposed as CSS custom properties the | |
| 1124 | + * stylesheet reads. Anything but a plain preset slug is dropped. | |
| 1125 | + */ | |
| 1126 | + $font_style = ''; | |
| 1127 | + foreach ( array( | |
| 1128 | + 'entryTitleFontFamily' => '--vpp-entry-title-font', | |
| 1129 | + ) as $font_attribute => $css_variable ) { | |
| 1130 | + if ( ! empty( $block_attributes[ $font_attribute ] ) | |
| 1131 | + && is_string( $block_attributes[ $font_attribute ] ) | |
| 1132 | + && preg_match( '/^[a-zA-Z0-9-]+$/', $block_attributes[ $font_attribute ] ) | |
| 1133 | + ) { | |
| 1134 | + $font_style .= $css_variable . ':var(--wp--preset--font-family--' . $block_attributes[ $font_attribute ] . ');'; | |
| 1135 | + } | |
| 1136 | + } | |
| 1137 | + | |
| 1138 | + // Looping implies auto-advancing, so it forces the autoplay-next flag on. | |
| 1139 | + $loop_playlist = $enabled( 'loopPlaylist', false ); | |
| 1140 | + | |
| 1141 | + $wrapper_extra_attributes = array( | |
| 1142 | + 'class' => implode( ' ', $classes ), | |
| 1143 | + 'data-autoplay-next' => $enabled( 'autoplayNext', false ) || $loop_playlist ? '1' : '0', | |
| 1144 | + 'data-loop' => $loop_playlist ? '1' : '0', | |
| 1145 | + ); | |
| 1146 | + if ( '' !== $font_style ) { | |
| 1147 | + $wrapper_extra_attributes['style'] = $font_style; | |
| 1148 | + } | |
| 1149 | + | |
| 1150 | + $wrapper_attributes = get_block_wrapper_attributes( $wrapper_extra_attributes ); | |
| 1151 | + | |
| 1152 | + // The Heading inner block saved with the post; an empty title renders none. | |
| 1153 | + $playlist_title = isset( $block_attributes['playlistTitle'] ) && is_string( $block_attributes['playlistTitle'] ) | |
| 1154 | + ? trim( $block_attributes['playlistTitle'] ) | |
| 1155 | + : ''; | |
| 1156 | + $content = is_string( $content ) ? trim( $content ) : ''; | |
| 1157 | + $heading_markup = $enabled( 'showPlaylistTitle' ) && '' !== $playlist_title && '' !== $content | |
| 1158 | + ? '<div class="videopress-playlist__heading">' . $content . '</div>' | |
| 1159 | + : ''; | |
| 1160 | + | |
| 1161 | + return sprintf( | |
| 1162 | + '<figure %1$s>%2$s<div class="videopress-playlist__body">%3$s%4$s</div></figure>', | |
| 1163 | + $wrapper_attributes, | |
| 1164 | + $heading_markup, | |
| 1165 | + $stage_markup, | |
| 1166 | + $list_markup | |
| 1167 | + ); | |
| 1168 | + } | |
| 1169 | + | |
| 1170 | + /** | |
| 1171 | + * Enqueue the VideoPress Iframe API script | |
| 1172 | + * when the URL of oEmbed HTML is a VideoPress URL. | |
| 1173 | + * | |
| 1174 | + * @param string|false $cache The cached HTML result, stored in post meta. | |
| 1175 | + * @param string $url The attempted embed URL. | |
| 1176 | + * @param array $attr An array of shortcode attributes. | |
| 1177 | + * @param int $post_ID Post ID. | |
| 1178 | + * | |
| 1179 | + * @return string|false | |
| 1180 | + */ | |
| 1181 | + public static function enqueue_videopress_iframe_api_script( $cache, $url, $attr, $post_ID ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable | |
| 1182 | + if ( Utils::is_videopress_url( $url ) && ! Inline_Player::is_enabled() ) { | |
| 1183 | + // Enqueue the VideoPress IFrame API in the front-end. | |
| 1184 | + wp_enqueue_script( | |
| 1185 | + self::JETPACK_VIDEOPRESS_IFRAME_API_HANDLER, | |
| 1186 | + 'https://s0.wp.com/wp-content/plugins/video/assets/js/videojs/videopress-iframe-api.js', | |
| 1187 | + array(), | |
| 1188 | + gmdate( 'YW' ), | |
| 1189 | + false | |
| 1190 | + ); | |
| 1191 | + } | |
| 1192 | + | |
| 1193 | + return $cache; | |
| 1194 | + } | |
| 1195 | + | |
| 1196 | + /** | |
| 1197 | + * Short-circuit the oEmbed request for VideoPress URLs when the inline player is on. | |
| 1198 | + * | |
| 1199 | + * @param null|string $result The oEmbed result, null to let WordPress fetch it. | |
| 1200 | + * @param string $url The URL being embedded. | |
| 1201 | + * @return null|string Inline player markup, or the untouched result. | |
| 1202 | + */ | |
| 1203 | + public static function maybe_pre_oembed_inline_player( $result, $url ) { | |
| 1204 | + $inline = self::render_inline_player_for_url( $url ); | |
| 1205 | + | |
| 1206 | + return null === $inline ? $result : $inline; | |
| 1207 | + } | |
| 1208 | + | |
| 1209 | + /** | |
| 1210 | + * Replace a VideoPress oEmbed iframe (fresh or cached) with an inline player when the inline player is on. | |
| 1211 | + * | |
| 1212 | + * @param string|false $cache The oEmbed HTML. | |
| 1213 | + * @param string $url The URL being embedded. | |
| 1214 | + * @param array $attr Shortcode attributes. | |
| 1215 | + * @param int $post_ID Post ID. | |
| 1216 | + * @return string|false Inline player markup, or the untouched HTML. | |
| 1217 | + */ | |
| 1218 | + public static function maybe_render_oembed_inline_player( $cache, $url, $attr, $post_ID = null ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable | |
| 1219 | + if ( ! is_string( $cache ) || false === strpos( $cache, '<iframe' ) ) { | |
| 1220 | + return $cache; | |
| 1221 | + } | |
| 1222 | + | |
| 1223 | + $inline = self::render_inline_player_for_url( $url ); | |
| 1224 | + | |
| 1225 | + return null === $inline ? $cache : $inline; | |
| 1226 | + } | |
| 1227 | + | |
| 1228 | + /** | |
| 1229 | + * Build inline player markup for a videopress.com URL, honoring the player parameters in its query string. | |
| 1230 | + * | |
| 1231 | + * @param string $url The URL being embedded. | |
| 1232 | + * @return string|null Markup, or null when the URL is not a VideoPress video or the inline player is off. | |
| 1233 | + */ | |
| 1234 | + private static function render_inline_player_for_url( $url ) { | |
| 1235 | + if ( ! Inline_Player::is_enabled() ) { | |
| 1236 | + return null; | |
| 1237 | + } | |
| 1238 | + | |
| 1239 | + $guid = Utils::extract_videopress_guid_from_url( $url ); | |
| 1240 | + if ( null === $guid ) { | |
| 1241 | + return null; | |
| 1242 | + } | |
| 1243 | + | |
| 1244 | + $attributes = Inline_Player::get_attributes_from_embed_url( $url ); | |
| 1245 | + | |
| 1246 | + return Inline_Player::render( | |
| 1247 | + $guid, | |
| 1248 | + Inline_Player::get_player_options( $attributes ), | |
| 1249 | + null, | |
| 1250 | + array( 'poster' => Inline_Player::get_poster_url( $guid, $attributes ) ) | |
| 1251 | + ); | |
| 1252 | + } | |
| 1253 | +} | |