Skip to content

Commit e8d4343

Browse files
committed
Add image optimizer cache purge
1 parent 2887421 commit e8d4343

File tree

2 files changed

+131
-1
lines changed

2 files changed

+131
-1
lines changed

includes/classes/Admin/Dashboard.php

Lines changed: 130 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
use const ImageOptimizerPro\Constants\LICENSE_ENDPOINT;
2020
use const ImageOptimizerPro\Constants\LICENSE_INFO_TRANSIENT;
2121
use const ImageOptimizerPro\Constants\LICENSE_KEY_OPTION;
22+
use const ImageOptimizerPro\Constants\MENU_SLUG;
23+
use const ImageOptimizerPro\Constants\PURGE_ENDPOINT;
2224
use const ImageOptimizerPro\Constants\SETTING_OPTION;
2325

2426
/**
@@ -69,9 +71,12 @@ public function setup() {
6971
add_action( 'admin_menu', [ $this, 'add_menu' ] );
7072
}
7173

74+
add_action( 'admin_notices', [ $this, 'maybe_display_message' ] );
75+
7276
add_action( 'admin_init', [ $this, 'save_settings' ] );
7377
add_action( 'admin_init', [ $this, 'maybe_redirect' ] );
7478
add_action( 'admin_init', [ $this, 'add_privacy_message' ] );
79+
add_action( 'admin_post_image_optimizer_pro_cache_purge', [ $this, 'handle_cache_purge' ] );
7580
}
7681

7782
/**
@@ -162,7 +167,16 @@ public function render_dashboard() {
162167
</tr>
163168
</tbody>
164169
</table>
165-
<?php submit_button( esc_html__( 'Save Changes', 'image-optimizer-pro' ), 'submit primary' ); ?>
170+
<p>
171+
<?php submit_button( esc_html__( 'Save Changes', 'image-optimizer-pro' ), 'submit primary', 'submit', false ); ?>
172+
<?php if ( ! is_multisite() && ! is_local_site() && false !== $license_info && 'valid' === $license_info['license_status'] ) : ?>
173+
<a
174+
href="<?php echo esc_url( wp_nonce_url( admin_url( 'admin-post.php?action=image_optimizer_pro_cache_purge' ), 'image_optimizer_pro_cache_purge' ) ); ?>"
175+
class="button-secondary" id="clear_image_optimizer_pro_cache">
176+
<?php esc_html_e( 'Clear Image Optimizer Cache', 'image-optimizer-pro' ); ?>
177+
</a>
178+
<?php endif; ?>
179+
</p>
166180
</form>
167181
</div>
168182

@@ -368,4 +382,119 @@ public function add_privacy_message() {
368382
}
369383
}
370384

385+
/**
386+
* Handle cache purge
387+
*
388+
* @return void
389+
*/
390+
public function handle_cache_purge() {
391+
if ( ! current_user_can( 'manage_options' ) ) {
392+
wp_die( esc_html__( 'You do not have sufficient permissions to perform this action.', 'image-optimizer-pro' ) );
393+
}
394+
395+
if ( ! isset( $_GET['_wpnonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ), 'image_optimizer_pro_cache_purge' ) ) {
396+
wp_die( esc_html__( 'Nonce verification failed.', 'image-optimizer-pro' ) );
397+
}
398+
399+
$response = $this->purge_image_optimizer_cache();
400+
401+
if ( ! is_wp_error( $response ) && ! empty( $response['success'] ) ) {
402+
$redirect_url = add_query_arg( 'iop_action', 'purge_image_optimizer_cache', wp_get_referer() );
403+
} else {
404+
$redirect_url = add_query_arg( 'iop_action', 'purge_image_optimizer_cache_failed', wp_get_referer() );
405+
}
406+
407+
wp_safe_redirect( esc_url_raw( $redirect_url ) );
408+
exit;
409+
}
410+
411+
/**
412+
* Purge Image Optimizer cache
413+
*
414+
* @return mixed|\WP_Error|null
415+
*/
416+
public function purge_image_optimizer_cache() {
417+
if ( is_multisite() ) {
418+
return new \WP_Error( 'multisite_not_supported', esc_html__( 'Multisite is not supported for Image Optimizer Purge.', 'image-optimizer-pro' ) );
419+
}
420+
421+
$body = wp_json_encode(
422+
[
423+
'license_key' => get_license_key(),
424+
'license_url' => home_url(),
425+
]
426+
);
427+
428+
$response = wp_remote_post(
429+
PURGE_ENDPOINT,
430+
[
431+
'headers' => [
432+
'Content-Type' => 'application/json',
433+
],
434+
'body' => $body,
435+
]
436+
);
437+
438+
if ( is_wp_error( $response ) ) {
439+
$error_message = $response->get_error_message();
440+
441+
return new \WP_Error( 'request_failed', esc_html__( 'Request failed: ', 'image-optimizer-pro' ) . $error_message );
442+
}
443+
444+
$response_body = wp_remote_retrieve_body( $response );
445+
446+
return json_decode( $response_body, true );
447+
}
448+
449+
450+
/**
451+
* Maybe display feedback messages when certain action is taken
452+
*
453+
* @since 1.1
454+
*/
455+
public function maybe_display_message() {
456+
// phpcs:disable WordPress.Security.NonceVerification.Recommended
457+
if ( ! isset( $_GET['iop_action'] ) ) {
458+
return;
459+
}
460+
461+
/**
462+
* Dont display multiple message when saving the options while having the query_params
463+
*/
464+
if ( ! empty( $_POST ) ) {
465+
return;
466+
}
467+
468+
$screen = get_current_screen();
469+
470+
$success_messages = [
471+
'purge_image_optimizer_cache' => esc_html__( 'Image optimizer cache purged successfully!', 'image-optimizer-pro' ),
472+
];
473+
474+
$err_messages = [
475+
'purge_image_optimizer_cache_failed' => esc_html__( 'Could not purge image optimizer cache. Please try again later and ensure your license key is activated!', 'image-optimizer-pro' ),
476+
];
477+
478+
if ( isset( $success_messages[ $_GET['iop_action'] ] ) ) {
479+
if ( MENU_SLUG === $screen->parent_base ) { // display with shared-ui on plugin page
480+
add_settings_error( $screen->parent_file, MENU_SLUG, $success_messages[ $_GET['iop_action'] ], 'success' ); // phpcs:ignore
481+
482+
return;
483+
}
484+
485+
printf( '<div class="notice notice-success is-dismissible"><p>%s</p></div>', $success_messages[ $_GET['iop_action'] ] ); // phpcs:ignore
486+
}
487+
488+
if ( isset( $err_messages[ $_GET['iop_action'] ] ) ) {
489+
if ( MENU_SLUG === $screen->parent_base ) { // display with shared-ui on plugin page
490+
add_settings_error( $screen->parent_file, MENU_SLUG, $err_messages[ $_GET['iop_action'] ], 'error' ); // phpcs:ignore
491+
492+
return;
493+
}
494+
495+
printf( '<div class="notice notice-error is-dismissible"><p>%s</p></div>', $err_messages[ $_GET['iop_action'] ] ); // phpcs:ignore
496+
}
497+
// phpcs:enable WordPress.Security.NonceVerification.Recommended
498+
}
499+
371500
}

includes/constants.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@
1616
const ACTIVATION_REDIRECT_TRANSIENT = 'image_optimizer_pro_activation_redirect';
1717

1818
const LICENSE_ENDPOINT = 'https://poweredcache.com/wp-json/paddlepress-api/v1/license';
19+
const PURGE_ENDPOINT = 'https://poweredcache.com/wp-json/image-optimizer/v1/purge';

0 commit comments

Comments
 (0)