Skip to content

Commit

Permalink
Merge pull request #52 from av3nger/release/1.9.3
Browse files Browse the repository at this point in the history
Release/1.9.3
  • Loading branch information
av3nger authored Oct 7, 2024
2 parents dabb46c + 2734682 commit 46c206b
Show file tree
Hide file tree
Showing 22 changed files with 629 additions and 82 deletions.
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
= 1.9.3 - 07.10.2024 =

Added:
* Integration with Smart Slider 3
* Integration with All in One SEO: allow controlling application/ld+json schema image URLs
* Integration with Rank Math: allow controlling application/ld+json schema image URLs
* Filter cf_images_disable_crop to disable auto cropping for registered crop images
* Background image support in Spectra plugin when styles are inlined (props @josephdsouza86)

Changed:
* Improve performance processing external images
* Rename 'cf_images_can_run' filter to 'cf_images_skip_image' so it's more clear what it does

Fixed:
* Images being replaced in RSS feeds, regardless of the settings
* Fatal error when a registered image size does not have height or width defined

= 1.9.2 - 17.07.2024 =

Added:
Expand Down
21 changes: 19 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ 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.6
Stable tag: 1.9.2
Tested up to: 6.7
Stable tag: 1.9.3
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.3 - 07.10.2024 =

Added:
* Integration with Smart Slider 3
* Integration with All in One SEO: allow controlling application/ld+json schema image URLs
* Integration with Rank Math: allow controlling application/ld+json schema image URLs
* Filter cf_images_disable_crop to disable auto cropping for registered crop images
* Background image support in Spectra plugin when styles are inlined (props @josephdsouza86)

Changed:
* Improve performance processing external images
* Rename 'cf_images_can_run' filter to 'cf_images_skip_image' so it's more clear what it does

Fixed:
* Images being replaced in RSS feeds, regardless of the settings
* Fatal error when a registered image size does not have height or width defined

= 1.9.2 - 17.07.2024 =

Added:
Expand Down
28 changes: 15 additions & 13 deletions app/class-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,22 +123,24 @@ public function enqueue_scripts( string $hook ) {
true
);

$i10n = array(
'nonce' => wp_create_nonce( 'cf-images-nonce' ),
'dirURL' => CF_IMAGES_DIR_URL,
'settings' => apply_filters( 'cf_images_settings', get_option( 'cf-images-settings', Settings::get_defaults() ) ),
'cfStatus' => $this->is_set_up(),
'domain' => get_option( 'cf-images-custom-domain', '' ),
'hideSidebar' => get_site_option( 'cf-images-hide-sidebar' ),
'fuzion' => $this->is_fuzion_api_connected(),
'stats' => $this->get_stats(),
'cdnEnabled' => (bool) get_option( 'cf-images-cdn-enabled', false ),
'isNetworkAdmin' => is_multisite() && is_main_site(),
'browserTTL' => get_site_option( 'cf-images-browser-ttl', 172800 ),
);

wp_localize_script(
$this->get_slug(),
'CFImages',
array(
'nonce' => wp_create_nonce( 'cf-images-nonce' ),
'dirURL' => CF_IMAGES_DIR_URL,
'settings' => apply_filters( 'cf_images_settings', get_option( 'cf-images-settings', Settings::get_defaults() ) ),
'cfStatus' => $this->is_set_up(),
'domain' => get_option( 'cf-images-custom-domain', '' ),
'hideSidebar' => get_site_option( 'cf-images-hide-sidebar' ),
'fuzion' => $this->is_fuzion_api_connected(),
'stats' => $this->get_stats(),
'cdnEnabled' => (bool) get_option( 'cf-images-cdn-enabled', false ),
'isNetworkAdmin' => is_multisite() && is_main_site(),
'browserTTL' => get_site_option( 'cf-images-browser-ttl', 172800 ),
)
apply_filters( 'cf_images_i10n', $i10n )
);
}

Expand Down
2 changes: 2 additions & 0 deletions app/class-core.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ private function set_cdn_domain() {
* @see Integrations\Shortpixel
* @see Integrations\JS_Composer
* @see Integrations\Flatsome
* @see Integrations\AIO_SEO
*/
private function init_integrations() {
$loader = Loader::get_instance();
Expand All @@ -193,6 +194,7 @@ private function init_integrations() {
$loader->integration( 'elementor' );
$loader->integration( 'js-composer' );
$loader->integration( 'flatsome' );
$loader->integration( 'aio-seo' );
}

/**
Expand Down
42 changes: 40 additions & 2 deletions app/class-image.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,13 @@ private function process_image() {
* @param bool $is_src Is this the src attribute.
*/
private function process( string $content, bool $is_src = false ) {
preg_match_all( '/https?[^\s\'"]*/i', $content, $urls );
/**
* Match URLs that start with:
* - http:
* - https:
* - // (but only if this string is at the beginning of a word or after whitespace)
*/
preg_match_all( '/https?:\S+|(?<!\S)\/\/\S+/i', $content, $urls );
if ( ! is_array( $urls ) || empty( $urls[0] ) ) {
return;
}
Expand Down Expand Up @@ -352,7 +358,7 @@ private function generate_url( string $image_url, bool $is_src = false ) {
return false;
}

if ( 0 === $this->id ) {
if ( 0 === $this->id && $this->is_local_image( $original ) ) {
// Could not get image ID from class name, try to get it from URL.
$this->id = $this->attachment_url_to_post_id( $original );
}
Expand Down Expand Up @@ -442,6 +448,11 @@ private function attachment_url_to_post_id( string $url ): int {
if ( false === $post_id ) {
global $wpdb;

// Normalize the URL.
if ( str_starts_with( $url, '//' ) ) {
$url = set_url_scheme( $url );
}

$sql = $wpdb->prepare(
"SELECT ID FROM $wpdb->posts WHERE guid = %s",
$url
Expand Down Expand Up @@ -548,4 +559,31 @@ public function get_width(): int {
public function get_id(): int {
return $this->id;
}

/**
* Check if this is a local image.
*
* @since 1.9.3
*
* @param string $url Image URL.
*
* @return bool
*/
private function is_local_image( string $url ): bool {
// Normalize the URL.
if ( str_starts_with( $url, '//' ) ) {
$url = set_url_scheme( $url );
}

$uploads = wp_get_upload_dir();

// Check if the URL is within the wp-content/uploads directory.
$uploads = ! isset( $uploads['baseurl'] ) || false !== strpos( $url, $uploads['baseurl'] );

if ( false === strpos( $url, content_url() ) && ! $uploads ) {
return false;
}

return true;
}
}
1 change: 1 addition & 0 deletions app/class-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public static function get_instance(): Loader {
*/
private function __construct() {
require_once __DIR__ . '/modules/class-module.php';
require_once __DIR__ . '/integrations/class-integration.php';
}

/**
Expand Down
100 changes: 100 additions & 0 deletions app/integrations/class-aio-seo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php
/**
* Integration class for the "All in One SEO" plugin
*
* This class adds compatibility with the "All in One SEO" plugin.
*
* @link https://vcore.au
*
* @package CF_Images
* @subpackage CF_Images/App/Integrations
* @author Anton Vanyukov <[email protected]>
* @since 1.9.3
*/

namespace CF_Images\App\Integrations;

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

/**
* AIO_SEO class.
*
* @since 1.9.3
*/
class AIO_SEO extends Integration {
/**
* Check if the integration should run.
*
* @since 1.9.3
*
* @return bool
*/
protected function should_run(): bool {
return defined( 'AIOSEO_PLUGIN_NAME' );
}

/**
* Define the variables for the integration.
*
* @since 1.9.3
*/
protected function init() {
$this->name = esc_html__( 'All in One SEO', 'cf-images' );
$this->slug = 'aio_seo';

/**
* Do not replace the ImageObject in application/ld+json schema.
*
* @see https://github.com/av3nger/cf-images/issues/49
*/
if ( ! $this->integration_option_value( false, 'image_object' ) ) {
add_action( 'wp_head', array( $this, 'halt_offload' ), 0 );
add_action( 'wp_head', array( $this, 'resume_offload' ), 2 );
}
}

/**
* Define the integration options.
*
* @since 1.9.3
*
* @param array $options Integration options.
* @param string $slug Integration slug.
*
* @return array
*/
public function integration_options( array $options, string $slug ): array {
if ( $this->slug !== $slug ) {
return $options;
}

return array(
array(
'name' => 'image_object',
'label' => esc_html__( 'Replace ImageObject', 'cf-images' ),
'description' => esc_html__( 'Use Cloudflare image for the ImageObject in the application/ld+json schema.', 'cf-images' ),
'value' => apply_filters( 'cf_images_integration_option_value', false, 'image_object' ),
),
);
}

/**
* Use a local version of the image in the head section.
*
* @since 1.9.3
*/
public function halt_offload() {
add_filter( 'cf_images_skip_image', '__return_true' );
}

/**
* Resume normal operations.
*
* @since 1.9.3
*/
public function resume_offload() {
remove_filter( 'cf_images_skip_image', '__return_false' );
}
}
Loading

0 comments on commit 46c206b

Please sign in to comment.