Skip to content

Commit

Permalink
Merge pull request #46 from av3nger/release/1.9.2
Browse files Browse the repository at this point in the history
Release/1.9.2
  • Loading branch information
av3nger authored Jul 16, 2024
2 parents d2dc9eb + f2d45a6 commit dabb46c
Show file tree
Hide file tree
Showing 14 changed files with 320 additions and 37 deletions.
30 changes: 30 additions & 0 deletions .wordpress-org/blueprints/blueprint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"landingPage": "\/wp-admin\/plugins.php",
"preferredVersions": {
"php": "8.0",
"wp": "latest"
},
"phpExtensionBundles": [
"kitchen-sink"
],
"features": {
"networking": true
},
"steps": [
{
"step": "installPlugin",
"pluginZipFile": {
"resource": "url",
"url": "https:\/\/downloads.wordpress.org\/plugin\/cf-images.zip"
},
"options": {
"activate": true
}
},
{
"step": "login",
"username": "admin",
"password": "password"
}
]
}
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
= 1.9.2 - 17.07.2024 =

Added:
* Integration with WPBakery page builder image galleries
* Integration with Elementor Pro Gallery
* Integration with Flatsome theme gallery
* cf_images_upload_host filter to adjust the image host ID

Changed:
* Improve image AI modules
* Improve performance when Rank Math image SEO is active

Fixed:
* Only allow generating image alt text for supported formats (JPEG, PNG, GIF, BMP)
* Duplicate queries for images that are not part of the media library
* Rank Math image SEO module not working with custom domains

= 1.9.1 - 23.04.2024 =

Added:
Expand Down
23 changes: 20 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
=== Offload, AI & Optimize with Cloudflare Images ===
Plugin Name: Offload, AI & Optimize with Cloudflare Images
Contributors: vanyukov
Tags: cdn, cloudflare images, image ai, compress, optimize
Tags: cdn, cloudflare images, image AI, compress, optimize
Donate link: https://www.paypal.com/donate/?business=JRR6QPRGTZ46N&no_recurring=0&item_name=Help+support+the+development+of+the+Cloudflare+Images+plugin+for+WordPress&currency_code=AUD
Requires at least: 5.6
Requires PHP: 7.0
Tested up to: 6.5
Stable tag: 1.9.1
Tested up to: 6.6
Stable tag: 1.9.2
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html

Expand Down Expand Up @@ -102,6 +102,23 @@ If something is still not working for you, please let me know by creating a supp

== Changelog ==

= 1.9.2 - 17.07.2024 =

Added:
* Integration with WPBakery page builder image galleries
* Integration with Elementor Pro Gallery
* Integration with Flatsome theme gallery
* cf_images_upload_host filter to adjust the image host ID

Changed:
* Improve image AI modules
* Improve performance when Rank Math image SEO is active

Fixed:
* Only allow generating image alt text for supported formats (JPEG, PNG, GIF, BMP)
* Duplicate queries for images that are not part of the media library
* Rank Math image SEO module not working with custom domains

= 1.9.1 - 23.04.2024 =

Added:
Expand Down
4 changes: 4 additions & 0 deletions app/class-core.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@ private function set_cdn_domain() {
* @see Integrations\Spectra
* @see Integrations\Wpml
* @see Integrations\Shortpixel
* @see Integrations\JS_Composer
* @see Integrations\Flatsome
*/
private function init_integrations() {
$loader = Loader::get_instance();
Expand All @@ -189,6 +191,8 @@ private function init_integrations() {
$loader->integration( 'wpml' );
$loader->integration( 'shortpixel' );
$loader->integration( 'elementor' );
$loader->integration( 'js-composer' );
$loader->integration( 'flatsome' );
}

/**
Expand Down
7 changes: 4 additions & 3 deletions app/class-image.php
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ private function get_crop_string( int $width, array $size ): string {
private function attachment_url_to_post_id( string $url ): int {
$post_id = wp_cache_get( $url, 'cf_images' );

if ( ! $post_id ) {
if ( false === $post_id ) {
global $wpdb;

$sql = $wpdb->prepare(
Expand All @@ -452,16 +452,17 @@ private function attachment_url_to_post_id( string $url ): int {

if ( $results ) {
$post_id = reset( $results )->ID;
wp_cache_add( $url, $post_id, 'cf_images' );
} else {
// This is a fallback, in case the above doesn't work for some reason.
$results = attachment_url_to_postid( $url );

if ( $results ) {
$post_id = $results;
wp_cache_add( $url, $post_id, 'cf_images' );
}
}

// Store this regardless if we have the post ID, prevents duplicate queries.
wp_cache_add( $url, $post_id, 'cf_images' );
}

return $post_id;
Expand Down
12 changes: 11 additions & 1 deletion app/class-media.php
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,17 @@ public function upload_image( $metadata, int $attachment_id, string $action = ''
$host = $url['host'];
}

$name = trailingslashit( $host ) . str_replace( trailingslashit( $dir['basedir'] ), '', $path );
/**
* This filters allows modifying the host slug in the image path that is used to identify the image on Cloudflare.
*
* @since 1.9.2
*
* @param string $host Site domain.
* @param int $attachment_id Attachment ID.
*/
$host = apply_filters( 'cf_images_upload_host', $host, $attachment_id );

$name = ( $host ? trailingslashit( $host ) : '' ) . str_replace( trailingslashit( $dir['basedir'] ), '', $path );

try {
// This allows us to replace the image on Cloudflare.
Expand Down
10 changes: 6 additions & 4 deletions app/integrations/class-elementor.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use CF_Images\App\Traits;
use Elementor\Widget_Base;
use Elementor\Widget_Image_Carousel;
use ElementorPro\Modules\Gallery\Widgets\Gallery;

if ( ! defined( 'WPINC' ) ) {
die;
Expand Down Expand Up @@ -53,17 +54,18 @@ public function __construct() {
* @param Widget_Base $widget The widget.
*/
public function add_lightbox_support( string $widget_content, Widget_Base $widget ): string {
if ( ! $widget instanceof Widget_Image_Carousel ) {
if ( ! $widget instanceof Widget_Image_Carousel && ! $widget instanceof Gallery ) {
return $widget_content;
}

// Regular expression to find <a> tags with data-elementor-open-lightbox="yes" and Cloudflare Images links.
$pattern = '/(<a\s[^>]*data-elementor-open-lightbox="yes"[^>]*href=")(' . preg_quote( $this->get_cdn_domain(), '/' ) . '[^"]+)(")/i';
$pattern = '/(<a\s[^>]*href="' . preg_quote( $this->get_cdn_domain(), '/' ) . '[^"#]*)(#[^"]*)?(".*?data-elementor-open-lightbox="yes".*?>)/i';

// Callback function to append '#.jpg' to the href attribute.
$callback = function ( $matches ) {
// Append '#.jpg' only if it's not already appended.
return $matches[1] . $matches[2] . ( substr( $matches[2], -5 ) !== '#.jpg' ? '#.jpg' : '' ) . $matches[3];
// Check if '#.jpg' is not already appended, and append if necessary.
$new_url = $matches[1] . ( isset( $matches[2] ) && strpos( $matches[2], '#.jpg' ) !== false ? $matches[2] : '#.jpg' );
return $new_url . $matches[3];
};

return preg_replace_callback( $pattern, $callback, $widget_content );
Expand Down
48 changes: 48 additions & 0 deletions app/integrations/class-flatsome.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
/**
* Flatsome theme integration class
*
* This class adds compatibility with the Flatsome theme.
*
* @link https://vcore.au
*
* @package CF_Images
* @subpackage CF_Images/App/Integrations
* @author Anton Vanyukov <[email protected]>
* @since 1.9.2
*/

namespace CF_Images\App\Integrations;

use CF_Images\App\Modules\Cloudflare_Images;

if ( ! defined( 'WPINC' ) ) {
die;
}

/**
* Flatsome class.
*
* @since 1.9.2
*/
class Flatsome {
/**
* Class constructor.
*
* @since 1.9.2
*/
public function __construct() {
add_action( 'wp_ajax_flatsome_additional_variation_images_load_images_ajax_frontend', array( $this, 'load_images_ajax' ) );
add_action( 'wp_ajax_nopriv_flatsome_additional_variation_images_load_images_ajax_frontend', array( $this, 'load_images_ajax' ) );
}

/**
* Add support for additional variation images (Flatsome gallery).
*
* @since 1.9.2
*/
public function load_images_ajax() {
$cf_images = new Cloudflare_Images( 'cloudflare-images' );
add_filter( 'wp_get_attachment_image_src', array( $cf_images, 'get_attachment_image_src' ), 10, 3 );
}
}
94 changes: 94 additions & 0 deletions app/integrations/class-js-composer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php
/**
* WPBakery page builder integration class
*
* This class adds compatibility with the WPBakery page builder plugin.
*
* @link https://vcore.au
*
* @package CF_Images
* @subpackage CF_Images/App/Integrations
* @author Anton Vanyukov <[email protected]>
* @since 1.9.2
*/

namespace CF_Images\App\Integrations;

use CF_Images\App\Modules\Cloudflare_Images;
use CF_Images\App\Traits\Helpers;

if ( ! defined( 'WPINC' ) ) {
die;
}

/**
* JS_Composer class.
*
* @since 1.9.2
*/
class JS_Composer {
use Helpers;

/**
* Class constructor.
*
* @since 1.9.2
*/
public function __construct() {
add_filter( 'vc_wpb_getimagesize', array( $this, 'fix_getimagesize_paths' ), 10, 3 );
}

/**
* When using custom image sizes on gallery images, WPBakery strips out Cloudflare Images parameters,
* breaking the images. This fixes the images, by appending the required image parameters.
*
* @since 1.9.2
*
* @param array|bool $image_data Array with image data.
* @param string|int $attachment_id Attachment ID.
* @param array $params Image parameters.
*
* @return array
*/
public function fix_getimagesize_paths( $image_data, $attachment_id, array $params ): array {
if ( ! isset( $image_data['thumbnail'] ) || ! isset( $image_data['p_img_large'] ) || ! is_array( $image_data['p_img_large'] ) ) {
return $image_data;
}

$pattern = '/<(?:img|source)\b(?>\s+(?:src=[\'"]([^\'"]*)[\'"]|srcset=[\'"]([^\'"]*)[\'"])|[^\s>]+|\s+)*>/i';
if ( ! preg_match_all( $pattern, $image_data['thumbnail'], $images ) ) {
do_action( 'cf_images_log', 'Running fix_getimagesize_paths(), `src` not found, returning image. Attachment ID: %s. Image: %s', $attachment_id, $image_data['thumbnail'] );
return $image_data;
}

// Check if the image has the 'w' or 'h' attribute set.
if ( preg_match( '/[?&]([wh])=\d+/', $images[1][0] ) ) {
return $image_data;
}

// The image is not on Cloudflare, exit.
if ( false === strpos( $image_data['p_img_large'][0], $this->get_cdn_domain() ) ) {
return $image_data;
}

// Now let's add the correct parameters to the original image.
if ( ! isset( $params['thumb_size'] ) || ! is_string( $params['thumb_size'] ) || ! preg_match( '/(\d+)x(\d+)/', $params['thumb_size'], $size ) ) {
return $image_data;
}

list( $hash, $cloudflare_image_id ) = Cloudflare_Images::get_hash_id_url_string( (int) $attachment_id );

if ( empty( $cloudflare_image_id ) || ( empty( $hash ) && ! apply_filters( 'cf_images_module_enabled', false, 'custom-path' ) ) ) {
return $image_data;
}

$image_url = trailingslashit( $this->get_cdn_domain() . "/$hash" ) . "$cloudflare_image_id/w=$size[1],h=$size[2]";
if ( $size[1] === $size[2] ) {
$image_url .= ',fit=crop';
}

$image_data['thumbnail'] = str_replace( $images[1][0], $image_url, $image_data['thumbnail'] );

return $image_data;
}
}
Loading

0 comments on commit dabb46c

Please sign in to comment.