|
19 | 19 | use const ImageOptimizerPro\Constants\LICENSE_ENDPOINT; |
20 | 20 | use const ImageOptimizerPro\Constants\LICENSE_INFO_TRANSIENT; |
21 | 21 | use const ImageOptimizerPro\Constants\LICENSE_KEY_OPTION; |
| 22 | +use const ImageOptimizerPro\Constants\MENU_SLUG; |
| 23 | +use const ImageOptimizerPro\Constants\PURGE_ENDPOINT; |
22 | 24 | use const ImageOptimizerPro\Constants\SETTING_OPTION; |
23 | 25 |
|
24 | 26 | /** |
@@ -69,9 +71,12 @@ public function setup() { |
69 | 71 | add_action( 'admin_menu', [ $this, 'add_menu' ] ); |
70 | 72 | } |
71 | 73 |
|
| 74 | + add_action( 'admin_notices', [ $this, 'maybe_display_message' ] ); |
| 75 | + |
72 | 76 | add_action( 'admin_init', [ $this, 'save_settings' ] ); |
73 | 77 | add_action( 'admin_init', [ $this, 'maybe_redirect' ] ); |
74 | 78 | add_action( 'admin_init', [ $this, 'add_privacy_message' ] ); |
| 79 | + add_action( 'admin_post_image_optimizer_pro_cache_purge', [ $this, 'handle_cache_purge' ] ); |
75 | 80 | } |
76 | 81 |
|
77 | 82 | /** |
@@ -162,7 +167,16 @@ public function render_dashboard() { |
162 | 167 | </tr> |
163 | 168 | </tbody> |
164 | 169 | </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> |
166 | 180 | </form> |
167 | 181 | </div> |
168 | 182 |
|
@@ -368,4 +382,119 @@ public function add_privacy_message() { |
368 | 382 | } |
369 | 383 | } |
370 | 384 |
|
| 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 | + |
371 | 500 | } |
0 commit comments