From 892a81e03b2f026c167458ad9307be966a0e3030 Mon Sep 17 00:00:00 2001 From: David Cramer Date: Wed, 3 Feb 2021 12:05:58 +0200 Subject: [PATCH 001/141] restructure image data to use correct methods --- php/media/class-gallery.php | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/php/media/class-gallery.php b/php/media/class-gallery.php index 8a3ee3b92..32c04424d 100644 --- a/php/media/class-gallery.php +++ b/php/media/class-gallery.php @@ -256,30 +256,30 @@ public function get_image_data( array $images ) { foreach ( $images as $index => $image ) { $image_id = is_int( $image ) ? $image : $image['id']; - $transformations = null; - $image_data[ $index ] = array(); + $transformations = null; + $data = array(); // Send back the attachment id. - $image_data[ $index ]['attachmentId'] = $image_id; + $data['attachmentId'] = $image_id; // Fetch the public id by either syncing NOW or getting the current public id. - if ( ! $this->media->sync->is_synced( $image_id ) ) { + if ( ! $this->media->has_public_id( $image_id ) ) { $res = $this->media->sync->managers['upload']->upload_asset( $image_id ); - if ( ! is_wp_error( $res ) ) { - $image_data[ $index ]['publicId'] = $this->media->get_public_id_from_url( $res['url'] ); - $transformations = $this->media->get_transformations_from_string( $res['url'] ); + if ( is_wp_error( $res ) ) { + // Skip and move on to the next image as this is unlikely to get synced. + continue; } - } else { - $image_data[ $index ]['publicId'] = $this->media->get_public_id( $image_id, true ); - - $image_url = is_int( $image ) ? $this->media->cloudinary_url( $image_id ) : $image['url']; - $transformations = $this->media->get_transformations_from_string( $image_url ); } - + // If synced now, the ID will be available in the meta. + $data['publicId'] = $this->media->get_public_id( $image_id, true ); + $transformations = $this->media->get_transformation_from_meta( $image_id ); if ( $transformations ) { - $image_data[ $index ]['transformation'] = array( 'transformation' => $transformations ); + $data['transformation'] = array( 'transformation' => $transformations ); } + + // Add to output array. + $image_data[] = $data; } return $image_data; From 8136eb24ec4c86800c0925c55cd2d1286bfb9748 Mon Sep 17 00:00:00 2001 From: David Cramer Date: Wed, 10 Feb 2021 09:25:47 +0200 Subject: [PATCH 002/141] WIP --- php/class-plugin.php | 4 +- php/class-support.php | 127 ++++++++++++++++++++++++++++++ php/sync/class-delete-sync.php | 2 +- php/sync/class-upload-sync.php | 26 ++++++ php/ui/component/class-system.php | 101 ++++++++++++++++++++++++ 5 files changed, 257 insertions(+), 3 deletions(-) create mode 100644 php/class-support.php create mode 100644 php/ui/component/class-system.php diff --git a/php/class-plugin.php b/php/class-plugin.php index ec57135b8..934f66219 100644 --- a/php/class-plugin.php +++ b/php/class-plugin.php @@ -127,13 +127,13 @@ public function __construct() { * that extend the Customizer to ensure resources are available in time. */ public function init() { - $this->components['connect'] = new Connect( $this ); $this->components['deactivation'] = new Deactivation( $this ); $this->components['sync'] = new Sync( $this ); $this->components['media'] = new Media( $this ); $this->components['api'] = new REST_API( $this ); $this->components['storage'] = new Storage( $this ); + $this->components['support'] = new Support( $this ); } /** @@ -141,7 +141,7 @@ public function init() { * * @param mixed $component The component. * - * @return Connect|Media|REST_API|Settings_Page|Sync|null + * @return Logger|Connect|Media|REST_API|Settings_Page|Sync|null */ public function get_component( $component ) { $return = null; diff --git a/php/class-support.php b/php/class-support.php new file mode 100644 index 000000000..2349e6630 --- /dev/null +++ b/php/class-support.php @@ -0,0 +1,127 @@ +plugin = $plugin; + } + + public function setup() { + if ( 'on' === $this->settings->get_value( 'enable_support' ) ) { + add_action( 'add_meta_boxes', array( $this, 'image_meta_viewer' ) ); + } + } + + public function image_meta_viewer() { + $screen = get_current_screen(); + if ( ! $screen instanceof \WP_Screen || 'attachment' !== $screen->id ) { + return; + } + + add_meta_box( + 'meta-viewer', + 'Cloudinary Metadata viewer', + function ( $post ) { + + if ( 'attachment' === $post->post_type ) { + $meta = wp_get_attachment_metadata( $post->ID ); + echo '
';
+					echo wp_json_encode( $meta, JSON_PRETTY_PRINT );
+					echo '

All Meta

'; + echo wp_json_encode( get_post_meta( $post->ID ), JSON_PRETTY_PRINT ); + echo '
'; + } + + } + ); + + add_meta_box( + 'sizes-viewer', + 'Image Sizes viewer', + function ( $post ) { + + if ( 'attachment' === $post->post_type ) { + $meta = wp_get_attachment_metadata( $post->ID ); + if ( ! empty( $meta['sizes'] ) ) { + echo '
'; + $keys = array_keys( $meta['sizes'] ); + foreach ( $keys as $size ) { + $image = wp_get_attachment_image( $post->ID, $size ); + echo $size; + echo '
' . $image . '
'; + } + echo '
'; + echo ''; + } + } + + } + ); + } + + public function settings() { + $args = array( + 'type' => 'page', + 'menu_title' => __( 'Support', 'cloudinary' ), + 'tabs' => array( + 'setup' => array( + 'page_title' => __( 'Support', 'cloudinary' ), + array( + 'type' => 'panel', + 'title' => __( 'Support and Debug', 'cloudinary' ), + array( + 'title' => __( 'Enable debug reporting', 'cloudinary' ), + 'type' => 'on_off', + 'slug' => 'enable_support', + ), + ), + array( + 'type' => 'submit', + ), + ), + 'report' => array( + 'page_title' => __( 'Report', 'cloudinary' ), + 'enabled' => function () { + $enabled = get_plugin_instance()->settings->get_value( 'enable_support' ); + + return 'on' === $enabled; + }, + array( + 'type' => 'panel', + 'title' => __( 'Report', 'cloudinary' ), + array( + 'type' => 'system', + ), + ), + + ), + ), + ); + + return $args; + } +} diff --git a/php/sync/class-delete-sync.php b/php/sync/class-delete-sync.php index d18b9f5f0..e5f6c5ccc 100644 --- a/php/sync/class-delete-sync.php +++ b/php/sync/class-delete-sync.php @@ -66,7 +66,7 @@ public function can_delete_asset( $all_caps, $caps, $args ) { if ( empty( $has_error ) ) { $all_caps['delete_posts'] = false; $action = filter_input( INPUT_GET, 'action', FILTER_SANITIZE_STRING ); - if ( ! empty( $action ) && '-1' !== $action ) { + if ( ! empty( $action ) && 'delete' === $action ) { wp_die( esc_html__( 'Sorry, you can’t delete an asset until it has fully synced with Cloudinary. Try again once syncing is complete.', 'cloudinary' ) ); } } diff --git a/php/sync/class-upload-sync.php b/php/sync/class-upload-sync.php index 882dc8845..fd6f68357 100644 --- a/php/sync/class-upload-sync.php +++ b/php/sync/class-upload-sync.php @@ -83,6 +83,7 @@ private function register_hooks() { add_filter( 'handle_bulk_actions-upload', array( $this, 'handle_bulk_actions' ), 10, 3 ); // Add inline action. add_filter( 'media_row_actions', array( $this, 'add_inline_action' ), 10, 2 ); + add_filter( 'post_row_actions', array( $this, 'add_inline_action' ), 10, 2 ); // Add Bulk actions. add_filter( @@ -132,6 +133,24 @@ public function add_inline_action( $actions, $post ) { ); } } + + } + + if ('on' === $this->plugin->settings->get_value( 'enable_support' ) ) { + $action_url = add_query_arg( + array( + 'action' => 'cloudinary-report', + 'media[]' => $post->ID, + '_wpnonce' => wp_create_nonce( 'bulk-media' ), + ), + 'upload.php' + ); + $actions['cloudinary-support'] = sprintf( + '%s', + $action_url, + esc_attr__( 'Add to Cloudinary Report', 'cloudinary' ), + esc_html__( 'Add to Cloudinary Report', 'cloudinary' ) + ); } return $actions; @@ -156,6 +175,13 @@ public function handle_bulk_actions( $location, $action, $post_ids ) { $this->sync->add_to_sync( $post_id ); } break; + case 'cloudinary-report': + $report_items = get_option('_cloudinary_report', array() ); + foreach ( $post_ids as $post_id ) { + $report_items[] = $post_id; + } + update_option( '_cloudinary_report', $report_items ); + break; } return $location; diff --git a/php/ui/component/class-system.php b/php/ui/component/class-system.php new file mode 100644 index 000000000..4fe3fab14 --- /dev/null +++ b/php/ui/component/class-system.php @@ -0,0 +1,101 @@ + $active_theme->Name, + 'version' => $active_theme->Version, + 'author' => $active_theme->get( 'Author' ), + 'author_url' => $active_theme->get( 'AuthorURI' ), + 'child_theme' => is_child_theme(), + ); + $struct['content'] = __( '#Theme Data', 'cloudinary' ); + $struct['content'] .= "\r\n"; + $struct['content'] .= strip_tags( wp_json_encode( $theme_data, JSON_PRETTY_PRINT ) ); + $struct['content'] .= "\r\n"; + $struct['content'] .= "\r\n"; + + return $struct; + } + + protected function config( $struct ) { + $struct['element'] = null; + + $data = $this->setting->get_root_setting()->get_value(); + unset( $data['connect'] ); + $struct['content'] = __( '#Config Data', 'cloudinary' ); + $struct['content'] .= "\r\n"; + $struct['content'] .= strip_tags( wp_json_encode( $data, JSON_PRETTY_PRINT ) ); + $struct['content'] .= "\r\n"; + $struct['content'] .= "\r\n"; + + return $struct; + } + + protected function plugins( $struct ) { + $struct['element'] = null; + $plugin_data = array( + 'must_use' => wp_get_mu_plugins(), + 'plugins' => array(), + ); + $plugins = get_plugins(); + $active = wp_get_active_and_valid_plugins(); + foreach ( $active as $plugin ) { + $plugin_data[] = get_plugin_data( $plugin ); + } + $struct['content'] = __( '#Plugins Data', 'cloudinary' ); + $struct['content'] .= "\r\n"; + $struct['content'] .= strip_tags( wp_json_encode( $plugin_data, JSON_PRETTY_PRINT ) ); + $struct['content'] .= "\r\n"; + + return $struct; + } +} From 398aca259d7ff5f9236865dde26518f4bfa40082 Mon Sep 17 00:00:00 2001 From: David Cramer Date: Thu, 11 Feb 2021 10:19:58 +0200 Subject: [PATCH 003/141] remove self hosted video player and use embeded for better handling. --- php/class-media.php | 75 +++++--- php/class-utils.php | 24 +++ php/media/class-video.php | 358 +++++++++++--------------------------- 3 files changed, 178 insertions(+), 279 deletions(-) diff --git a/php/class-media.php b/php/class-media.php index 0a3505aba..29054af39 100644 --- a/php/class-media.php +++ b/php/class-media.php @@ -589,6 +589,41 @@ public function get_transformation( $transformations, $type ) { return false; } + /** + * Get transformations for an attachment to use in a final URL. + * + * @param int $attachment_id The attachment ID. + * @param array $transformations Base/starter set of transformations. + * @param bool $overwrite_transformations Flag to indicate if default transformations should not be applied. + * + * @return array + */ + public function get_transformations( $attachment_id, $transformations = array(), $overwrite_transformations = false ) { + // If not provided, get transformations from the attachment meta. + if ( empty( $transformations ) ) { + $transformations = $this->get_transformation_from_meta( $attachment_id ); + } + if ( false === $overwrite_transformations ) { + $overwrite_transformations = $this->maybe_overwrite_featured_image( $attachment_id ); + } + /** + * Filter the Cloudinary transformations. + * + * @param array $transformations Array of transformation options. + * @param int $attachment_id The id of the asset. + * + * @return array + */ + $transformations = apply_filters( 'cloudinary_transformations', $transformations, $attachment_id ); + + // Defaults are only to be added on front, main images ( not breakpoints, since these are adapted down), and videos. + if ( false === $overwrite_transformations && ! is_admin() ) { + $transformations = $this->apply_default_transformations( $transformations, $this->get_media_type( $attachment_id ) ); + } + + return $transformations; + } + /** * Extract the crop size part of a transformation that was done in the DAM widget. * @@ -704,7 +739,10 @@ public function attachment_url( $url, $attachment_id ) { * @return array */ public function apply_default_transformations( array $transformations, $type = 'image' ) { - + // Allow filter to bypass defaults. + if ( false === apply_filters( 'cloudinary_apply_default_transformations', true ) ) { + return $transformations; + } // Base image level. $new_transformations = array( 'image' => Api::generate_transformation_string( $transformations, $type ), @@ -806,10 +844,10 @@ public function default_image_freeform_transformations( $default ) { /** * Generate a Cloudinary URL based on attachment ID and required size. * - * @param int $attachment_id The id of the attachment. - * @param array|string $size The wp size to set for the URL. - * @param array $transformations Set of transformations to apply to this url. - * @param string $cloudinary_id Optional forced cloudinary ID. + * @param int $attachment_id The id of the attachment. + * @param array|string $size The wp size to set for the URL. + * @param array $transformations Set of transformations to apply to this url. + * @param string $cloudinary_id Optional forced cloudinary ID. * @param bool $overwrite_transformations Flag url is a breakpoint URL to stop re-applying default transformations. * * @return string The converted URL. @@ -821,9 +859,7 @@ public function cloudinary_url( $attachment_id, $size = array(), $transformation return null; } } - if ( empty( $transformations ) ) { - $transformations = $this->get_transformation_from_meta( $attachment_id ); - } + // Get the attachment resource type. $resource_type = $this->get_media_type( $attachment_id ); // Setup initial args for cloudinary_url. @@ -834,24 +870,9 @@ public function cloudinary_url( $attachment_id, $size = array(), $transformation ); $size = $this->prepare_size( $attachment_id, $size ); - if ( false === $overwrite_transformations ) { - $overwrite_transformations = $this->maybe_overwrite_featured_image( $attachment_id ); - } - /** - * Filter the Cloudinary transformations. - * - * @param array $transformations Array of transformation options. - * @param int $attachment_id The id of the asset. - * - * @return array - */ - $pre_args['transformation'] = apply_filters( 'cloudinary_transformations', $transformations, $attachment_id ); - $apply_default_transformations = apply_filters( 'cloudinary_apply_default_transformations', true ); - // Defaults are only to be added on front, main images ( not breakpoints, since these are adapted down), and videos. - if ( true === $apply_default_transformations && false === $overwrite_transformations && ! is_admin() ) { - $pre_args['transformation'] = $this->apply_default_transformations( $pre_args['transformation'], $resource_type ); - } + // Prepare transformations. + $pre_args['transformation'] = $this->get_transformations( $attachment_id, $transformations, $overwrite_transformations ); // Make a copy as not to destroy the options in \Cloudinary::cloudinary_url(). $args = $pre_args; @@ -1399,14 +1420,14 @@ function ( $value, $key ) use ( &$asset ) { // Check for transformations. $transformations = $this->get_transformations_from_string( $asset['url'] ); if ( ! empty( $transformations ) ) { - $asset['sync_key'] .= wp_json_encode( $transformations ); + $asset['sync_key'] .= wp_json_encode( $transformations ); $asset['transformations'] = $transformations; } // Check Format. $url_format = pathinfo( $asset['url'], PATHINFO_EXTENSION ); if ( strtolower( $url_format ) !== strtolower( $asset['format'] ) ) { - $asset['format'] = $url_format; + $asset['format'] = $url_format; $asset['sync_key'] .= $url_format; } diff --git a/php/class-utils.php b/php/class-utils.php index 7522e1248..4be4688d9 100644 --- a/php/class-utils.php +++ b/php/class-utils.php @@ -62,6 +62,7 @@ public static function get_active_setting() { * * @param array $input The array that will be processed. * @param string $separator Separator string. + * * @return array */ public static function expand_dot_notation( array $input, $separator = '.' ) { @@ -105,4 +106,27 @@ public static function is_amp( $html_string ) { public static function is_webstory_post_type( $post_type ) { return class_exists( Story_Post_Type::class ) && Story_Post_Type::POST_TYPE_SLUG === $post_type; } + + /** + * Get all the attributes from an HTML tag. + * + * @param string $tag HTML tag to get attributes from. + * + * @return array + */ + public static function get_tag_attributes( $tag ) { + $tag = strstr( $tag, ' ', false ); + $tag = trim( $tag, '> ' ); + $args = shortcode_parse_atts( $tag ); + $return = array(); + foreach ( $args as $key => $value ) { + if ( is_int( $key ) ) { + $return[ $value ] = 'true'; + continue; + } + $return[ $key ] = $value; + } + + return $return; + } } diff --git a/php/media/class-video.php b/php/media/class-video.php index ebb5a9e64..e70517712 100644 --- a/php/media/class-video.php +++ b/php/media/class-video.php @@ -8,6 +8,7 @@ namespace Cloudinary\Media; use Cloudinary\Media; +use Cloudinary\Utils; /** * Class Video. @@ -34,13 +35,6 @@ class Video { */ private $config; - /** - * Determines if the video player is active. - * - * @var bool - */ - private $player_enabled = false; - /** * List of attachment ID's to enable. * @@ -53,7 +47,7 @@ class Video { * * @var string */ - const PLAYER_VER = '1.4.0'; + const PLAYER_VER = '1.5.1'; /** * Cloudinary Core Version. @@ -87,44 +81,7 @@ public function __construct( Media $media ) { * @return bool */ public function player_enabled() { - return $this->player_enabled; - } - - /** - * $this->>register_scripts(); - * Initialises the Cloudinary player if it's enabled and if video content is found. - */ - public function init_player() { - if ( isset( $this->config['video_player'] ) && 'cld' === $this->config['video_player'] && ! is_admin() ) { - global $wp_query; - $posts = $wp_query->get_posts(); - // Check content has a video to enqueue assets in correct location. - foreach ( $posts as $post ) { - $has_video = $this->media->filter->get_video_shortcodes( get_the_content() ); - $video_tags = $this->media->filter->get_media_tags( get_the_content(), 'video' ); - if ( ! empty( $has_video ) || ! empty( $video_tags ) ) { - // Setup initial scripts. - wp_enqueue_style( 'cld-player' ); - wp_enqueue_style( 'cld-player-local', $this->media->plugin->dir_url . 'css/video.css', null, self::PLAYER_VER ); - wp_enqueue_script( 'cld-player' ); - - // Init cld script object. - $cld = array( - 'cloud_name' => $this->media->credentials['cloud_name'], - ); - if ( ! empty( $this->media->credentials['cname'] ) ) { - $cld['cname'] = $this->media->credentials['cname']; - $cld['private_cdn'] = true; - } - $code = 'var cld = cloudinary.Cloudinary.new(' . wp_json_encode( $cld ) . ');'; - wp_add_inline_script( 'cld-player', $code ); - - // Enable video for output. - $this->player_enabled = true; - break; // Exit since we determined that a video is present. - } - } - } + return isset( $this->config['video_player'] ) && 'cld' === $this->config['video_player'] && ! is_admin(); } /** @@ -196,187 +153,25 @@ public function validate_usable_transformations( $attachment_id, $transformation public function filter_video_shortcode( $html, $attr ) { // If not CLD video init, return default. - if ( false === $this->player_enabled ) { - return $html; - } - // Check for override flag. - $overwrite_transformations = false; - if ( ! empty( $attr['cldoverwrite'] ) ) { - $overwrite_transformations = true; - } - // Check for a cloudinary url, or prep sync if not found. - $cloudinary_url = $this->media->cloudinary_url( $attr['id'], false, false, null, $overwrite_transformations ); - if ( ! $this->media->plugin->components['sync']->is_synced( $attr['id'] ) ) { - // If the asset is not synced, then the metadata will not be complete since v1 didn't save any. - // Return html for now since cloudinary_url will queue it up for syncing in the background. - return $html; - } - - // Queue video. - $video = wp_get_attachment_metadata( $attr['id'] ); - $transformations = $this->media->get_transformations_from_string( $cloudinary_url, 'video' ); - $args = array(); - - if ( isset( $attr['autoplay'] ) ) { - $args['autoplay'] = 'true' === $attr['autoplay']; - $args['muted'] = 'true' === $attr['autoplay']; - } - if ( isset( $attr['loop'] ) ) { - $args['loop'] = 'true' === $attr['loop']; - } - // Transformations. - if ( ! empty( $transformations ) ) { - $args['transformation'] = $transformations; - } - $args['overwrite_transformations'] = $overwrite_transformations; - // Size settings. - $size = ''; - if ( ! empty( $attr['width'] ) ) { - $size .= ' width="' . esc_attr( $attr['width'] ) . '"'; - $args['size'] = true; - } - if ( ! empty( $attr['height'] ) ) { - $size .= ' height="' . esc_attr( $attr['height'] ) . '"'; - $args['size'] = true; - } - $instance = $this->queue_video_config( $attr['id'], $attr[ $video['fileformat'] ], $video['fileformat'], $args ); - - // Replace with video tag. - return ''; - } - - /** - * Filter video tags and queue them for the player. - * - * @param string $content HTML content of the post. - * - * @return mixed - */ - public function filter_video_tags( $content ) { - - $video_tags = $this->media->filter->get_media_tags( $content, 'video' ); - foreach ( $video_tags as $tag ) { - $args = array(); - - // Catch poster. - $poster_url = $this->media->filter->get_poster_from_tag( $tag ); - if ( false !== $poster_url ) { - - $poster_id = $this->media->get_id_from_url( $poster_url ); - $cloudinary_id = $this->media->cloudinary_id( $poster_id ); - $cloudinary_url = $this->media->cloudinary_url( $poster_id ); - $transformations = $this->media->get_transformations_from_string( $cloudinary_url ); - $args['posterOptions'] = array( - 'publicId' => $cloudinary_id, - ); - if ( ! empty( $transformations ) ) { - $args['posterOptions']['transformation'] = $transformations; - } + if ( ! $this->player_enabled() ) { + if ( empty( $attr['cloudinary'] ) ) { + $video = wp_get_attachment_metadata( $attr['id'] ); + $url = $this->media->cloudinary_url( $attr['id'] ); + $attr[ $video['fileformat'] ] = $url; + $attr['cloudinary'] = true; // Flag Cloudinary to ensure we don't call it again. + $html = wp_video_shortcode( $attr, $html ); } - $url = $this->media->filter->get_url_from_tag( $tag ); - if ( false === $url ) { - continue; - } - $attachment_id = $this->media->filter->get_id_from_tag( $tag ); - if ( empty( $attachment_id ) ) { - continue; // Missing or no attachment ID found. - } - // Enable Autoplay for this video. - if ( false !== strpos( $tag, 'autoplay' ) ) { - $args['autoplayMode'] = $this->config['video_autoplay_mode']; // if on, use defined mode. - $args['muted'] = 'always' === $this->config['video_autoplay_mode']; - } - // Enable Loop. - if ( false !== strpos( $tag, 'loop' ) ) { - $args['loop'] = true; - } - // If there is no controls, it has been turned off. - if ( false !== strpos( $tag, 'controls' ) ) { - $args['controls'] = true; - } - // If there is a muted, it has been turned on. - if ( false !== strpos( $tag, 'muted' ) ) { - $args['muted'] = true; - } - // If preload. - if ( preg_match( '/preload=\"([^\"]*)\"/i', $tag, $found ) ) { - $args['preload'] = $found[1]; - } - // Add transformations if found. - $classes = $this->media->filter->get_classes( $tag ); // check if this is a transformation overwrite. - $overwrite_transformations = false; - if ( false !== strpos( $classes, 'cld-overwrite' ) ) { - $overwrite_transformations = true; - } - $args['overwrite_transformations'] = $overwrite_transformations; - - $cloudinary_url = $this->media->cloudinary_url( $attachment_id, false, false, null, $overwrite_transformations ); - // Bail replacing the video URL for cases where it doesn't exist. - // Cases are, for instance, when the file size is larger than the API limits — free accounts. - if ( ! empty( $cloudinary_url ) ) { - $transformations = $this->media->get_transformations_from_string( $cloudinary_url, 'video' ); - if ( ! empty( $transformations ) ) { - $args['transformation'] = $transformations; - } - $video = wp_get_attachment_metadata( $attachment_id ); - if ( $this->player_enabled() ) { - $instance = $this->queue_video_config( $attachment_id, $url, $video['fileformat'], $args ); - // Remove src and replace with an ID. - $new_tag = str_replace( 'src="' . $url . '"', 'id="cloudinary-video-' . esc_attr( $instance ) . '"', $tag ); - $content = str_replace( $tag, $new_tag, $content ); - } else { - // Just replace URL. - $content = str_replace( $url, $cloudinary_url, $content ); - } - } + return $html; } + $attachment_id = $attr['id']; + unset( $attr['id'] ); + unset( $attr['width'] ); + unset( $attr['height'] ); - return $content; - } + $overwrite_transformations = ! empty( $attr['cldoverwrite'] ); - /** - * Output init scripts in footer for videos. - */ - public function print_video_scripts() { - - if ( $this->player_enabled() && ! empty( $this->attachments ) ) { - - $cld_videos = array(); - foreach ( $this->attachments as $instance => $video ) { - // @todo - ping the URL to ensure it has transformation available, else update an eager. - $cloudinary_id = $this->media->get_public_id( $video['id'] ); - $default = array( - 'publicId' => $cloudinary_id, - 'sourceTypes' => array( $video['format'] ), // @todo Make this based on eager items as mentioned above. - 'autoplay' => 'off' !== $this->config['video_autoplay_mode'], - 'loop' => $this->config['video_loop'], - ); - - $valid_autoplay_modes = array( 'never', 'always', 'on-scroll' ); - if ( $default['autoplay'] && in_array( $this->config['video_autoplay_mode'], $valid_autoplay_modes, true ) ) { - $default['autoplayMode'] = $this->config['video_autoplay_mode']; - } - - $config = wp_parse_args( $video['args'], $default ); - - if ( empty( $config['size'] ) && ! empty( $config['transformation'] ) && ! $this->media->get_crop_from_transformation( $config['transformation'] ) ) { - $config['fluid'] = true; - } - - $config['controls'] = $this->config['video_controls']; - $cld_videos[ $instance ] = $config; - } - - if ( empty( $cld_videos ) ) { - return; - } - - $json_cld_videos = wp_json_encode( $cld_videos ); - $video_freeform = esc_js( $this->config['video_freeform'] ); - - wp_add_inline_script( 'cld-player', "var cldVideos = '{$json_cld_videos}'; var videoFreeForm = '{$video_freeform}';" ); - } + return $this->build_video_embed( $attachment_id, $attr, $overwrite_transformations ); } /** @@ -387,16 +182,6 @@ public function enqueue_block_assets() { wp_add_inline_script( 'cloudinary-block', 'var CLD_VIDEO_PLAYER = ' . wp_json_encode( $this->config ), 'before' ); } - /** - * Register assets for the player. - */ - public function register_scripts_styles() { - wp_register_style( 'cld-player', 'https://unpkg.com/cloudinary-video-player@' . self::PLAYER_VER . '/dist/cld-video-player.min.css', null, self::PLAYER_VER ); - wp_register_script( 'cld-core', 'https://unpkg.com/cloudinary-core@' . self::CORE_VER . '/cloudinary-core-shrinkwrap.min.js', null, self::CORE_VER, true ); - wp_register_script( 'cld-player', 'https://unpkg.com/cloudinary-video-player@' . self::PLAYER_VER . '/dist/cld-video-player.min.js', array( 'cld-core' ), self::PLAYER_VER, true ); - wp_enqueue_script( 'cld-video-init', CLDN_URL . 'js/video-init.js', array( 'cld-player' ), self::CORE_VER, true ); - } - /** * Filter a video block to add the class for cld-overriding. * @@ -407,23 +192,19 @@ public function register_scripts_styles() { */ public function filter_video_block_pre_render( $block, $source_block ) { - if ( 'core/video' === $source_block['blockName'] ) { - $classes = 'cld-fluid'; - if ( ! empty( $source_block['attrs']['overwrite_transformations'] ) ) { - $classes .= ' cld-overwrite'; - } - if ( ! empty( $source_block['attrs']['id'] ) ) { - $classes .= ' wp-video-' . $source_block['attrs']['id']; - } + if ( 'core/video' === $source_block['blockName'] && ! empty( $source_block['attrs']['id'] ) && $this->media->has_public_id( $source_block['attrs']['id'] ) ) { + $attachment_id = $source_block['attrs']['id']; + $overwrite_transformations = ! empty( $source_block['attrs']['overwrite_transformations'] ); foreach ( $block['innerContent'] as &$content ) { - $video_tags = $this->media->filter->get_media_tags( $content ); - foreach ( $video_tags as $tag ) { - if ( false !== strpos( $tag, 'class="' ) ) { - $content = str_replace( 'class="', 'class="' . $classes . ' ', $content ); - } else { - $content = str_replace( '