From bfd67513508ce4d83bea1ff5df7566cdda620959 Mon Sep 17 00:00:00 2001 From: Dugi Surdulli Date: Wed, 9 Sep 2020 18:30:51 -0400 Subject: [PATCH 001/119] Convert gif to video if f_mp4 is present --- .../php/class-utils.php | 58 ++++++++++++++++++ .../php/media/class-filter.php | 60 ++++++++++++++++--- 2 files changed, 111 insertions(+), 7 deletions(-) create mode 100644 cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-utils.php diff --git a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-utils.php b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-utils.php new file mode 100644 index 000000000..34cb6e0cc --- /dev/null +++ b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-utils.php @@ -0,0 +1,58 @@ +get_classes( $asset ); // check if this is a transformation overwrite. $overwrite_transformations = false; + if ( false !== strpos( $classes, 'cld-overwrite' ) ) { $overwrite_transformations = true; } + $cloudinary_url = $this->media->cloudinary_url( $attachment_id, $wp_size, $transformations, null, $overwrite_transformations, $clean ); if ( $url === $cloudinary_url ) { @@ -358,13 +361,10 @@ public function filter_out_local( $content ) { $new_tag = str_replace( '/>', ' class="wp-image-' . $attachment_id . '"/>', $new_tag ); } - // Apply lazy loading attribute - if ( - apply_filters( 'wp_lazy_loading_enabled', true ) && - false === strpos( $new_tag, 'loading="lazy"' ) && - $clean - ) { - $new_tag = str_replace( '/>', ' loading="lazy" />', $new_tag ); + $new_tag = $this->add_lazy_loading_attribute( $new_tag ); + + if ( $video_tag = $this->check_fmp4_presence( $cloudinary_url, $attachment_id ) ) { + $new_tag = $video_tag; } // If Cloudinary player is active, this is replaced there. @@ -383,6 +383,52 @@ public function filter_out_local( $content ) { return $this->filter_video_shortcodes( $content ); } + /** + * Apply loading=lazy to if necessaary. + * + * @param string $new_tag + * @param bool $is_admin + * + * @return string + */ + protected function add_lazy_loading_attribute( $new_tag, $is_admin = false ) { + if ( + apply_filters( 'wp_lazy_loading_enabled', true ) && + false === strpos( $new_tag, 'loading="lazy"' ) && + $is_admin + ) { + $new_tag = str_replace( '/>', ' loading="lazy" />', $new_tag ); + } + + return $new_tag; + } + + /** + * Checks whether $cloudinary_url has f_mp4 which converts gifs to mp4s. + * + * @param string $cloudinary_url + * @param int $attachment_id + * + * @return string|null + */ + protected function check_fmp4_presence( $cloudinary_url, $attachment_id ) { + $return = null; + + if ( Utils::ends_with( $cloudinary_url, '.gif' ) ) { + $all_transformations = Utils::flatten( $this->media->get_transformations_from_string( $cloudinary_url ) ); + + if ( in_array( 'mp4', $all_transformations ) ) { + $return = sprintf( + '