Skip to content

Commit d0b7f21

Browse files
committed
add cache clearing on image update
1 parent b810d1c commit d0b7f21

File tree

4 files changed

+48
-11
lines changed

4 files changed

+48
-11
lines changed

inc/admin.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ public function __construct() {
7474
add_action( 'updated_post_meta', [ $this, 'detect_image_alt_change' ], 10, 4 );
7575
add_action( 'added_post_meta', [ $this, 'detect_image_alt_change' ], 10, 4 );
7676
add_action( 'init', [ $this, 'schedule_data_enhance_cron' ] );
77+
add_filter( 'update_attached_file', [ $this, 'listen_update_file' ], 999, 2 );
7778
}
7879
add_action( 'init', [ $this, 'update_default_settings' ] );
7980
add_action( 'init', [ $this, 'update_limit_dimensions' ] );
@@ -98,6 +99,21 @@ public function __construct() {
9899

99100
add_filter( 'themeisle-sdk/survey/' . OPTML_PRODUCT_SLUG, [ $this, 'get_survey_metadata' ], 10, 2 );
100101
}
102+
103+
/**
104+
* Listen when the file is updated and clear the cache for the file.
105+
*
106+
* @param string $file The file path.
107+
* @param int $post_id The post ID.
108+
*
109+
* @return string The file path.
110+
*/
111+
public function listen_update_file( $file, $post_id ) {
112+
$basename = wp_basename( $file );
113+
$settings = new Optml_Settings();
114+
$settings->clear_cache( $basename );
115+
return $file;
116+
}
101117
/**
102118
* Check if the file is an SVG, if so handle appropriately
103119
*

inc/api.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,13 @@ public function get_cache_token( $token = '', $type = '', $api_key = '' ) {
127127
if ( ! empty( $api_key ) ) {
128128
$this->api_key = $api_key;
129129
}
130-
$lock = get_transient( 'optml_cache_lock' );
131-
if ( ! empty( $type ) && $type === 'assets' ) {
130+
$lock = '';
131+
if ( empty( $type ) || $type === 'images' ) {
132+
$lock = get_transient( 'optml_cache_lock' );
133+
} elseif ( $type === 'assets' ) {
132134
$lock = get_transient( 'optml_cache_lock_assets' );
135+
} else {
136+
$type = '_file_' . crc32( $type );
133137
}
134138

135139
if ( $lock === 'yes' ) {

inc/settings.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,7 @@ public function register_settings() {
732732
);
733733
}
734734

735+
735736
/**
736737
* Clear cache.
737738
*
@@ -743,12 +744,15 @@ public function clear_cache( $type = '' ) {
743744
$token = $this->get( 'cache_buster' );
744745
$token_images = $this->get( 'cache_buster_images' );
745746

746-
if ( ! empty( $token_images ) ) {
747-
$token = $token_images;
748-
}
749-
750-
if ( ! empty( $type ) && $type === 'assets' ) {
747+
if ( ( empty( $type ) || $type === 'images' ) ) {
748+
if ( ! empty( $token_images ) ) {
749+
$token = $token_images;
750+
}
751+
} elseif ( $type === 'assets' ) {
751752
$token = $this->get( 'cache_buster_assets' );
753+
} else {
754+
// here is an individual clear cache based on filename.
755+
$token = get_transient( '_file_' . crc32( $type ) ) ?? '';
752756
}
753757

754758
$request = new Optml_Api();
@@ -769,12 +773,14 @@ public function clear_cache( $type = '' ) {
769773
return new WP_Error( 'optimole_cache_buster_error', __( 'Can not get new token from Optimole service', 'optimole-wp' ) . $extra );
770774
}
771775

772-
if ( ! empty( $type ) && $type === 'assets' ) {
776+
if ( empty( $type ) || $type === 'images' ) {
777+
set_transient( 'optml_cache_lock', 'yes', 5 * MINUTE_IN_SECONDS );
778+
$this->update( 'cache_buster_images', $data['token'] );
779+
} elseif ( $type === 'assets' ) {
773780
set_transient( 'optml_cache_lock_assets', 'yes', 5 * MINUTE_IN_SECONDS );
774781
$this->update( 'cache_buster_assets', $data['token'] );
775782
} else {
776-
set_transient( 'optml_cache_lock', 'yes', 5 * MINUTE_IN_SECONDS );
777-
$this->update( 'cache_buster_images', $data['token'] );
783+
set_transient( '_file_' . crc32( $type ), $data['token'], 6 * HOUR_IN_SECONDS );
778784
}
779785

780786
return $data['token'];

inc/url_replacer.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ private function normalize_image( $url, $original_url, $args, $is_uploaded = fal
228228
}
229229

230230
$args = apply_filters( 'optml_image_args', $args, $original_url );
231-
$image = Optimole::image( apply_filters( 'optml_processed_url', $url ), $this->active_cache_buster );
231+
$image = Optimole::image( apply_filters( 'optml_processed_url', $url ), self::get_active_cache_booster( $url, $this->active_cache_buster ) );
232232

233233
$image->width( ! empty( $args['width'] ) && is_int( $args['width'] ) ? $args['width'] : 'auto' );
234234
$image->height( ! empty( $args['height'] ) && is_int( $args['height'] ) ? $args['height'] : 'auto' );
@@ -275,6 +275,17 @@ private function normalize_image( $url, $original_url, $args, $is_uploaded = fal
275275
return $image->getUrl();
276276
}
277277

278+
/**
279+
* Get the active cache booster.
280+
*
281+
* @param string $url The URL.
282+
* @param string $main_cache_buster The default value.
283+
*
284+
* @return string
285+
*/
286+
public static function get_active_cache_booster( $url, $main_cache_buster ) {
287+
return get_transient( '_file_' . crc32( wp_basename( $url ) ) ) ?? $main_cache_buster;
288+
}
278289
/**
279290
* Throw error on object clone
280291
*

0 commit comments

Comments
 (0)