Skip to content

Commit 5c4a906

Browse files
authored
Merge pull request #893 from Codeinwp/feat/clear-cache-on-update
Feat/clear cache on update
2 parents e04e30b + 758be11 commit 5c4a906

File tree

6 files changed

+66
-130
lines changed

6 files changed

+66
-130
lines changed

inc/admin.php

+28-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function __construct() {
6565
add_action( 'admin_notices', [ $this, 'add_notice_conflicts' ] );
6666
add_action( self::SYNC_CRON, [ $this, 'daily_sync' ] );
6767
add_action( 'admin_init', [ $this, 'redirect_old_dashboard' ] );
68-
68+
add_action( 'optml_purge_image_cache', [ $this, 'purge_image_cache' ] );
6969
if ( $this->settings->is_connected() ) {
7070
add_action( 'init', [ $this, 'check_domain_change' ] );
7171
add_action( self::ENRICH_CRON, [ $this, 'pull_image_data' ] );
@@ -80,6 +80,7 @@ public function __construct() {
8080

8181
add_action( 'updated_post_meta', [ $this, 'detect_image_alt_change' ], 10, 4 );
8282
add_action( 'added_post_meta', [ $this, 'detect_image_alt_change' ], 10, 4 );
83+
add_filter( 'update_attached_file', [ $this, 'listen_update_file' ], 999, 2 );
8384
if ( ! wp_next_scheduled( self::ENRICH_CRON ) ) {
8485
wp_schedule_event( time() + 10, 'hourly', self::ENRICH_CRON );
8586
}
@@ -107,6 +108,32 @@ public function __construct() {
107108

108109
add_filter( 'themeisle-sdk/survey/' . OPTML_PRODUCT_SLUG, [ $this, 'get_survey_metadata' ], 10, 2 );
109110
}
111+
112+
/**
113+
* Function that purges the image cache for a specific file.
114+
*
115+
* @param string $file Path or url of the file to clear.
116+
*
117+
* @return void
118+
*/
119+
public function purge_image_cache( $file ) {
120+
$basename = wp_basename( $file );
121+
$settings = new Optml_Settings();
122+
$settings->clear_cache( $basename );
123+
}
124+
/**
125+
* Listen when the file is updated and clear the cache for the file.
126+
*
127+
* @param string $file The file path.
128+
* @param int $post_id The post ID.
129+
*
130+
* @return string The file path.
131+
*/
132+
public function listen_update_file( $file, $post_id ) {
133+
// Purge the image cache.
134+
do_action( 'optml_purge_image_cache', $file );
135+
return $file;
136+
}
110137
/**
111138
* Check if the file is an SVG, if so handle appropriately
112139
*

inc/api.php

+6-55
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 = 'images';
133137
}
134138

135139
if ( $lock === 'yes' ) {
@@ -352,59 +356,6 @@ public function get_optimized_images( $api_key = '' ) {
352356
return $this->request( '/optml/v1/stats/images', 'GET', [], [ 'application' => $app_key ] );
353357
}
354358

355-
/**
356-
* Get the watermarks from API.
357-
*
358-
* @param string $api_key The API key.
359-
*
360-
* @return array|bool|WP_Error
361-
*/
362-
public function get_watermarks( $api_key = '' ) {
363-
if ( ! empty( $api_key ) ) {
364-
$this->api_key = $api_key;
365-
}
366-
367-
return $this->request( '/optml/v1/settings/watermark' );
368-
}
369-
370-
/**
371-
* Remove the watermark from the API.
372-
*
373-
* @param integer $post_id The watermark post ID.
374-
* @param string $api_key The API key.
375-
*
376-
* @return array|bool|WP_Error
377-
*/
378-
public function remove_watermark( $post_id, $api_key = '' ) {
379-
if ( ! empty( $api_key ) ) {
380-
$this->api_key = $api_key;
381-
}
382-
383-
return $this->request( '/optml/v1/settings/watermark', 'DELETE', [ 'watermark' => $post_id ] );
384-
}
385-
386-
/**
387-
* Add watermark.
388-
*
389-
* @param array $file The file to be uploaded.
390-
*
391-
* @return array|bool|mixed|object
392-
*/
393-
public function add_watermark( $file ) {
394-
395-
$headers = [
396-
'Content-Disposition' => 'attachment; filename=' . $file['file']['name'],
397-
];
398-
399-
$response = $this->request( 'wp/v2/media', 'POST', file_get_contents( $file['file']['tmp_name'] ), $headers );
400-
401-
if ( $response === false ) {
402-
return false;
403-
}
404-
405-
return $response;
406-
}
407-
408359
/**
409360
* Call the images endpoint.
410361
*

inc/rest.php

-65
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,6 @@ class Optml_Rest {
8181
'clear_offload_errors' => 'GET',
8282
'get_offload_conflicts' => 'GET',
8383
],
84-
'watermark_routes' => [
85-
'poll_watermarks' => 'GET',
86-
'add_watermark' => 'POST',
87-
'remove_watermark' => 'POST',
88-
],
8984
'conflict_routes' => [
9085
'poll_conflicts' => 'GET',
9186
'dismiss_conflict' => 'POST',
@@ -175,7 +170,6 @@ public function register() {
175170
$this->register_service_routes();
176171

177172
$this->register_image_routes();
178-
$this->register_watermark_routes();
179173
$this->register_conflict_routes();
180174
$this->register_cache_routes();
181175
$this->register_media_offload_routes();
@@ -217,14 +211,6 @@ public function register_media_offload_routes() {
217211
}
218212
}
219213

220-
/**
221-
* Method to register watermark specific routes.
222-
*/
223-
public function register_watermark_routes() {
224-
foreach ( self::$rest_routes['watermark_routes'] as $route => $details ) {
225-
$this->reqister_route( $route, $details );
226-
}
227-
}
228214

229215
/**
230216
* Method to register conflicts specific routes.
@@ -650,57 +636,6 @@ public function poll_optimized_images( WP_REST_Request $request ) {
650636
return $this->response( $final_images );
651637
}
652638

653-
/**
654-
* Get watermarks from API.
655-
*
656-
* @param WP_REST_Request $request rest request.
657-
*
658-
* @return WP_REST_Response
659-
*/
660-
public function poll_watermarks( WP_REST_Request $request ) {
661-
$api_key = $request->get_param( 'api_key' );
662-
$request = new Optml_Api();
663-
$watermarks = $request->get_watermarks( $api_key );
664-
if ( ! isset( $watermarks['watermarks'] ) || empty( $watermarks['watermarks'] ) ) {
665-
return $this->response( [] );
666-
}
667-
$final_images = array_splice( $watermarks['watermarks'], 0, 10 );
668-
669-
return $this->response( $final_images );
670-
}
671-
672-
/**
673-
* Add watermark.
674-
*
675-
* @param WP_REST_Request $request rest request.
676-
*
677-
* @return WP_REST_Response
678-
*/
679-
public function add_watermark( WP_REST_Request $request ) {
680-
$file = $request->get_file_params();
681-
$request = new Optml_Api();
682-
$response = $request->add_watermark( $file );
683-
if ( $response === false ) {
684-
return $this->response( __( 'Error uploading image. Please try again.', 'optimole-wp' ), 'error' );
685-
}
686-
687-
return $this->response( __( 'Watermark image uploaded succesfully !', 'optimole-wp' ) );
688-
}
689-
690-
/**
691-
* Remove watermark.
692-
*
693-
* @param WP_REST_Request $request rest request.
694-
*
695-
* @return WP_REST_Response
696-
*/
697-
public function remove_watermark( WP_REST_Request $request ) {
698-
$post_id = $request->get_param( 'postID' );
699-
$api_key = $request->get_param( 'api_key' );
700-
$request = new Optml_Api();
701-
702-
return $this->response( $request->remove_watermark( $post_id, $api_key ) );
703-
}
704639

705640
/**
706641
* Get conflicts from API.

inc/settings.php

+19-8
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class Optml_Settings {
1616
const FILTER_TYPE_LAZYLOAD = 'lazyload';
1717
const FILTER_TYPE_OPTIMIZE = 'optimize';
1818
const OPTML_USER_EMAIL = 'optml_user_email';
19+
const INDIVIDUAL_CACHE_TOKENS_KEY = '_optml_cache_tokens_individual';
1920
/**
2021
* Holds an array of possible settings to alter via wp cli or wp-config constants.
2122
*
@@ -741,6 +742,7 @@ public function register_settings() {
741742
);
742743
}
743744

745+
744746
/**
745747
* Clear cache.
746748
*
@@ -752,12 +754,16 @@ public function clear_cache( $type = '' ) {
752754
$token = $this->get( 'cache_buster' );
753755
$token_images = $this->get( 'cache_buster_images' );
754756

755-
if ( ! empty( $token_images ) ) {
756-
$token = $token_images;
757-
}
758-
759-
if ( ! empty( $type ) && $type === 'assets' ) {
757+
// here is an individual cache tokens
758+
$individual = get_transient( self::INDIVIDUAL_CACHE_TOKENS_KEY ) ?: [];
759+
if ( ( empty( $type ) || $type === 'images' ) ) {
760+
if ( ! empty( $token_images ) ) {
761+
$token = $token_images;
762+
}
763+
} elseif ( $type === 'assets' ) {
760764
$token = $this->get( 'cache_buster_assets' );
765+
} else {
766+
$token = $individual[ crc32( $type ) ] ?? $token_images ?: $token;
761767
}
762768

763769
$request = new Optml_Api();
@@ -778,12 +784,17 @@ public function clear_cache( $type = '' ) {
778784
return new WP_Error( 'optimole_cache_buster_error', __( 'Can not get new token from Optimole service', 'optimole-wp' ) . $extra );
779785
}
780786

781-
if ( ! empty( $type ) && $type === 'assets' ) {
787+
if ( empty( $type ) || $type === 'images' ) {
788+
set_transient( 'optml_cache_lock', 'yes', 5 * MINUTE_IN_SECONDS );
789+
$this->update( 'cache_buster_images', $data['token'] );
790+
// we delete individual cache tokens since this is a global cache clear.
791+
delete_transient( self::INDIVIDUAL_CACHE_TOKENS_KEY );
792+
} elseif ( $type === 'assets' ) {
782793
set_transient( 'optml_cache_lock_assets', 'yes', 5 * MINUTE_IN_SECONDS );
783794
$this->update( 'cache_buster_assets', $data['token'] );
784795
} else {
785-
set_transient( 'optml_cache_lock', 'yes', 5 * MINUTE_IN_SECONDS );
786-
$this->update( 'cache_buster_images', $data['token'] );
796+
$individual[ crc32( $type ) ] = $data['token'];
797+
set_transient( self::INDIVIDUAL_CACHE_TOKENS_KEY, $individual, 6 * HOUR_IN_SECONDS );
787798
}
788799

789800
return $data['token'];

inc/url_replacer.php

+12-1
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( Optml_Settings::INDIVIDUAL_CACHE_TOKENS_KEY ) ?: [] )[ crc32( wp_basename( $url ) ) ] ?? $main_cache_buster;
288+
}
278289
/**
279290
* Throw error on object clone
280291
*

phpcs.xml

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
<exclude name="Squiz.Commenting.FileComment"/>
3131
<exclude name="WordPress.PHP.YodaConditions.NotYoda"/>
3232
<exclude name="Universal.Arrays.DisallowShortArraySyntax.Found"/>
33+
<exclude name="Universal.Operators.DisallowShortTernary.Found"/>
3334
<exclude name="Squiz.PHP.DisallowMultipleAssignments"/>
3435
<exclude name="WordPress.NamingConvention.ValidVariableName.StringNotSnakeCase"/>
3536
<exclude name="WordPress.NamingConventions.ValidVariableName.NotSnakeCase"/>

0 commit comments

Comments
 (0)