Skip to content

Commit

Permalink
Merge pull request #43 from av3nger/release/1.8.1
Browse files Browse the repository at this point in the history
Release 1.9.0
  • Loading branch information
av3nger authored Mar 22, 2024
2 parents cb65a6d + 228b4b9 commit 271f3ff
Show file tree
Hide file tree
Showing 28 changed files with 706 additions and 114 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.0 - 22.03.2024 =

Added:
* Set browser TTL for images
* Option to serve originals for logged-in users
* Option to apply settings network wide in multisite

Changed:
* Disable logging in wp-admin
* Improve detection of cropped images
* Fallback to scaled images if original image is larger than 20 Mb

Fixed:
* Image size can now be changed in the Gutenberg image block for fully offloaded images
* Full size images not replaced in the gallery block on expand
* Multiple fixes and improvements with the WPML integration

= 1.8.0 - 16.02.2024 =

Added:
Expand Down
47 changes: 19 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Donate link: https://www.paypal.com/donate/?business=JRR6QPRGTZ46N&no_recurring=
Requires at least: 5.6
Requires PHP: 7.0
Tested up to: 6.5
Stable tag: 1.8.0
Stable tag: 1.9.0
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html

Expand Down Expand Up @@ -51,9 +51,7 @@ Cloudflare, the Cloudflare logo, and Cloudflare Workers are trademarks and/or re

Special thanks to the plugin sponsors:

<a href="https://thismatters.agency" target="_blank">
<img src="https://thismatters.agency/wp-content/uploads/2023/03/thismatters-logo.svg" alt="this:matters" width="200" />
</a>
<a href="https://thismatters.agency">WordPress Agency this:matters</a>

== Installation ==

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

== Changelog ==

= 1.9.0 - 22.03.2024 =

Added:
* Set browser TTL for images
* Option to serve originals for logged-in users
* Option to apply settings network wide in multisite

Changed:
* Disable logging in wp-admin
* Improve detection of cropped images
* Fallback to scaled images if original image is larger than 20 Mb

Fixed:
* Image size can now be changed in the Gutenberg image block for fully offloaded images
* Full size images not replaced in the gallery block on expand
* Multiple fixes and improvements with the WPML integration

= 1.8.0 - 16.02.2024 =

Added:
Expand Down Expand Up @@ -161,30 +176,6 @@ Fixed:
* Bulk processing stops if an image triggers an error during upload
* Settings resetting on update after using a beta version

= 1.5.1 - 28.10.2023 =

Fixed:
* Do not replace images on wp-admin if full offload module is disabled

= 1.5.0 - 27.10.2023 =

Added:
* New and improved React-based UI
* Image compression module: optimize the size of your media library images
* WP CLI support via the "wp cf-images" commands (bulk & individual offload)
* Compatibility with the "Enable Media Replace" plugin
* Option to bulk add image captions
* Allow viewing a page with original images, using a "?cf-images-disable=true" URL query

Changed:
* The "Auto resize images on front-end" module has been refactored to prevent double loading of images

Fixed:
* Cropped image detection
* Compatibility with latest WordPress coding standards
* PHP warnings with page parser module on pages with no images
* Link for adding API key for AI module was not working

[Full changelog](https://github.com/av3nger/cf-images/blob/master/CHANGELOG.md).

== Upgrade Notice ==
Expand Down
23 changes: 23 additions & 0 deletions app/api/class-variant.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,27 @@ public function toggle_flexible( bool $value ): stdClass {

return $this->request();
}

/**
* Set images cache TTL.
*
* @since 1.9.0
*
* @param int $value Cache TTL in seconds.
*
* @return stdClass
* @throws Exception Exception during API call.
*/
public function set_cache_ttl( int $value ): stdClass {
$data = array(
'browser_ttl' => $value,
);

$this->set_method( 'PATCH' );
$this->set_timeout( 2 );
$this->set_endpoint( '/config' );
$this->set_request_body( wp_json_encode( $data ) );

return $this->request();
}
}
28 changes: 19 additions & 9 deletions app/class-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,17 @@ public function enqueue_scripts( string $hook ) {
$this->get_slug(),
'CFImages',
array(
'nonce' => wp_create_nonce( 'cf-images-nonce' ),
'dirURL' => CF_IMAGES_DIR_URL,
'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 ),
'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 ),
)
);
}
Expand All @@ -150,6 +152,10 @@ public function enqueue_scripts( string $hook ) {
* @return array
*/
public function settings_link( array $actions ): array {
if ( $this->is_network_wide() ) {
return $actions;
}

if ( ! current_user_can( 'manage_options' ) ) {
return $actions;
}
Expand All @@ -164,6 +170,10 @@ public function settings_link( array $actions ): array {
* @since 1.0.0
*/
public function register_menu() {
if ( $this->is_network_wide() ) {
return;
}

add_submenu_page(
'upload.php',
__( 'Offload Images to Cloudflare', 'cf-images' ),
Expand Down
4 changes: 4 additions & 0 deletions app/class-core.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,13 @@ private function init_integrations() {
* @see Modules\Service
* @see Modules\CDN
* @see Modules\Full_Offload
* @see Modules\Multisite
* @see Modules\Cache_TTL
*/
private function load_modules() {
$loader = Loader::get_instance();

$loader->module( 'multisite' ); // This should be loaded before other modules.
$loader->module( 'cdn' ); // This should be loaded before other modules.
$loader->module( 'auto-offload' );
$loader->module( 'auto-resize' );
Expand All @@ -229,6 +232,7 @@ private function load_modules() {
$loader->module( 'custom-path' );
$loader->module( 'service' );
$loader->module( 'full-offload' );
$loader->module( 'cache-ttl' );
}

/**
Expand Down
11 changes: 11 additions & 0 deletions app/class-media.php
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,9 @@ public function upload_image( $metadata, int $attachment_id, string $action = ''
return $metadata;
}

// This is used with WPML integration.
$attachment_id = apply_filters( 'cf_images_media_post_id', $attachment_id );

$mime = get_post_mime_type( $attachment_id );

if ( ! wp_attachment_is_image( $attachment_id ) || false !== strpos( $mime, 'image/svg' ) ) {
Expand All @@ -407,6 +410,10 @@ public function upload_image( $metadata, int $attachment_id, string $action = ''
$dir = wp_get_upload_dir();
$path = wp_get_original_image_path( $attachment_id );

if ( file_exists( $path ) && ( MB_IN_BYTES * 20 ) <= filesize( $path ) ) {
$path = get_attached_file( $attachment_id );
}

$url = wp_parse_url( get_site_url() );
if ( is_multisite() && ! is_subdomain_install() ) {
$host = $url['host'] . $url['path'];
Expand All @@ -429,6 +436,8 @@ public function upload_image( $metadata, int $attachment_id, string $action = ''
update_post_meta( $attachment_id, '_cloudflare_image_id', $results->id );
$this->maybe_save_hash( $results->variants );

do_action( 'cf_images_upload_success', $attachment_id, $results );

if ( doing_filter( 'wp_async_wp_generate_attachment_metadata' ) ) {
$this->fetch_stats( new Api\Image() );
}
Expand Down Expand Up @@ -461,6 +470,8 @@ public function remove_from_cloudflare( int $post_id ) {
delete_post_meta( $post_id, '_cloudflare_image_id' );
delete_post_meta( $post_id, '_cloudflare_image_skip' );

do_action( 'cf_images_remove_success', $post_id );

if ( doing_action( 'delete_attachment' ) ) {
$this->fetch_stats( new Api\Image() );
}
Expand Down
1 change: 1 addition & 0 deletions app/class-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class Settings {
'image-generate' => false,
'logging' => false,
'rss-feeds' => false,
'no-offload-user' => false, // Do not offload images for logged-in users.
);

/**
Expand Down
90 changes: 81 additions & 9 deletions app/integrations/class-wpml.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

namespace CF_Images\App\Integrations;

use stdClass;

if ( ! defined( 'WPINC' ) ) {
die;
}
Expand All @@ -32,8 +34,9 @@ class Wpml {
public function __construct() {
add_filter( 'cf_images_media_post_id', array( $this, 'get_original_image_id' ) );
add_action( 'cf_images_before_wp_query', array( $this, 'remove_wpml_filters' ) );
add_action( 'wpml_after_duplicate_attachment', array( $this, 'ignore_attachment' ), 10, 2 );
add_action( 'wpml_after_copy_attached_file_postmeta', array( $this, 'ignore_attachment' ), 10, 2 );
add_action( 'cf_images_upload_success', array( $this, 'update_image_meta' ), 10, 2 );
add_action( 'cf_images_remove_success', array( $this, 'image_removed_from_cf' ) );
add_filter( 'cf_images_wp_query_args', array( $this, 'add_wp_query_args' ), 10, 2 );
}

/**
Expand Down Expand Up @@ -75,16 +78,85 @@ public function remove_wpml_filters() {
}

/**
* Fires when an attachment is duplicated.
* Get translations for an image.
*
* Duplicated images do not need to be processed, otherwise this causes double uploads to Cloudflare.
* @since 1.9.0
*
* @param int $attachment_id The ID of the source/original attachment.
* @param int $duplicated_attachment_id The ID of the duplicated attachment.
* @param int $attachment_id Attachment ID.
*
* @since 1.4.0
* @return array
*/
private function get_translations( int $attachment_id ): array {
global $sitepress;

if ( ! $sitepress || ! method_exists( $sitepress, 'get_element_trid' ) ) {
return array();
}

$translation_id = $sitepress->get_element_trid( $attachment_id, 'post_attachment' );
return $sitepress->get_element_translations( $translation_id, 'post_attachment', true );
}

/**
* Update the meta for all images.
*
* @since 1.9.0
*
* @param int $attachment_id Original attachment ID.
* @param stdClass $results Upload results.
*/
public function update_image_meta( int $attachment_id, stdClass $results ) {
$translations = $this->get_translations( $attachment_id );

foreach ( $translations as $translation ) {
if ( $translation->original ) {
continue;
}

update_post_meta( $translation->element_id, '_cloudflare_image_id', $results->id );
}
}

/**
* Remove meta from all translatable images when the main image is removed from Cloudflare.
*
* @since 1.9.0
*
* @param int $attachment_id Attachment ID.
*/
public function image_removed_from_cf( int $attachment_id ) {
$translations = $this->get_translations( $attachment_id );

foreach ( $translations as $translation ) {
if ( $translation->original ) {
continue;
}

delete_post_meta( $translation->element_id, '_cloudflare_image_id' );
}
}

/**
* Adjust the WP_Query args for bulk offload action.
*
* @since 1.9.0
* @see Ajax::get_wp_query_args()
*
* @param array $args WP_Query args.
* @param string $action Executing action.
*
* @return array
*/
public function ignore_attachment( int $attachment_id, int $duplicated_attachment_id ) {
update_post_meta( $duplicated_attachment_id, '_cloudflare_image_skip', true );
public function add_wp_query_args( array $args, string $action ): array {
if ( 'upload' !== $action ) {
return $args;
}

$args['meta_query'][] = array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
'compare' => 'NOT EXISTS',
'key' => 'wpml_media_processed',
);

return $args;
}
}
Loading

0 comments on commit 271f3ff

Please sign in to comment.