ributes['posterData']['previewAtTime'],
'previewLoopDuration' => $block_attributes['posterData']['previewLoopDuration'],
'autoplay' => $autoplay,
'showControls' => $controls,
);
// Create inlione style in case video has a custom poster.
$inline_style = '';
if ( $poster ) {
$inline_style = sprintf(
'style="background-image: url(%s); background-size: cover; background-position: center center;"',
esc_attr( $poster )
);
}
// Expose the preview on hover data to the client.
$preview_on_hover = sprintf(
'
',
$inline_style,
wp_json_encode( $preview_on_hover )
);
// Set `autoplay` and `muted` attributes to the video element.
$block_attributes['autoplay'] = true;
$block_attributes['muted'] = true;
}
$figure_template = '
%4$s
%5$s
%6$s
';
// VideoPress URL
$guid = isset( $block_attributes['guid'] ) ? $block_attributes['guid'] : null;
$videopress_url = Utils::get_video_press_url( $guid, $block_attributes );
$video_wrapper = '';
$video_wrapper_classes = 'jetpack-videopress-player__wrapper';
if ( $videopress_url ) {
$videopress_url = wp_kses_post( $videopress_url );
$oembed_html = apply_filters( 'video_embed_html', $wp_embed->shortcode( array(), $videopress_url ) );
$video_wrapper = sprintf(
'%s %s
',
$video_wrapper_classes,
$preview_on_hover,
$oembed_html
);
}
// Get premium content from block context
$premium_block_plan_id = isset( $block->context['premium-content/planId'] ) ? intval( $block->context['premium-content/planId'] ) : 0;
$is_premium_content_child = isset( $block->context['isPremiumContentChild'] ) ? (bool) $block->context['isPremiumContentChild'] : false;
$maybe_premium_script = '';
if ( $is_premium_content_child ) {
Access_Control::instance()->set_guid_subscription( $guid, $premium_block_plan_id );
$escaped_guid = esc_js( $guid );
$script_content = "if ( ! window.__guidsToPlanIds ) { window.__guidsToPlanIds = {}; }; window.__guidsToPlanIds['$escaped_guid'] = $premium_block_plan_id;";
$maybe_premium_script = '';
}
// $id_attribute, $video_wrapper, $figcaption properly escaped earlier on the code
return sprintf(
$figure_template,
esc_attr( $classes ),
esc_attr( $style ),
$id_attribute,
$video_wrapper,
$figcaption,
$maybe_premium_script
);
}
/**
* Register the VideoPress block editor block,
* AKA "VideoPress Block v6".
*
* @return void
*/
public static function register_videopress_video_block() {
$videopress_video_metadata_file = __DIR__ . '/../build/block-editor/blocks/video/block.json';
$videopress_video_metadata_file_exists = file_exists( $videopress_video_metadata_file );
if ( ! $videopress_video_metadata_file_exists ) {
return;
}
$videopress_video_metadata = json_decode(
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
file_get_contents( $videopress_video_metadata_file )
);
// Pick the block name straight from the block metadata .json file.
$videopress_video_block_name = $videopress_video_metadata->name;
// Is the block already registered?
$is_block_registered = \WP_Block_Type_Registry::get_instance()->is_registered( $videopress_video_block_name );
// Do not register if the block is already registered.
if ( $is_block_registered ) {
return;
}
// Is this a REST API request?
$is_rest = defined( 'REST_API_REQUEST' ) && REST_API_REQUEST;
if ( $is_rest ) {
register_block_type(
$videopress_video_metadata_file,
array(
'render_callback' => array( __CLASS__, 'render_videopress_video_block' ),
)
);
return;
}
$registration = register_block_type(
$videopress_video_metadata_file,
array(
'render_callback' => array( __CLASS__, 'render_videopress_video_block' ),
'uses_context' => array( 'premium-content/planId', 'isPremiumContentChild', 'selectedPlanId' ),
)
);
// Do not enqueue scripts if the block could not be registered.
if ( empty( $registration ) || empty( $registration->editor_script_handles ) ) {
return;
}
// Extensions use Connection_Initial_State::render_script with script handle as parameter.
if ( is_array( $registration->editor_script_handles ) ) {
$script_handle = $registration->editor_script_handles[0];
} else {
$script_handle = $registration->editor_script_handles;
}
// Register and enqueue scripts used by the VideoPress video block.
Block_Editor_Extensions::init( $script_handle );
}
/**
* Enqueue the VideoPress Iframe API script
* when the URL of oEmbed HTML is a VideoPress URL.
*
* @param string|false $cache The cached HTML result, stored in post meta.
* @param string $url The attempted embed URL.
* @param array $attr An array of shortcode attributes.
* @param int $post_ID Post ID.
*
* @return string|false
*/
public static function enqueue_videopress_iframe_api_script( $cache, $url, $attr, $post_ID ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
if ( Utils::is_videopress_url( $url ) ) {
// Enqueue the VideoPress IFrame API in the front-end.
wp_enqueue_script(
self::JETPACK_VIDEOPRESS_IFRAME_API_HANDLER,
'https://s0.wp.com/wp-content/plugins/video/assets/js/videojs/videopress-iframe-api.js',
array(),
gmdate( 'YW' ),
false
);
}
return $cache;
}
}