| 1 |
<?php |
| 2 |
declare(strict_types=1); |
| 3 |
|
| 4 |
namespace Imagify\Tools; |
| 5 |
|
| 6 |
use Imagify\EventManagement\SubscriberInterface; |
| 7 |
|
| 8 |
/** |
| 9 |
* Registers the AJAX action for the one-click internal-state reset. |
| 10 |
*/ |
| 11 |
class Subscriber implements SubscriberInterface { |
| 12 |
|
| 13 |
/** |
| 14 |
* ResetInternalState service. |
| 15 |
* |
| 16 |
* @var ResetInternalState |
| 17 |
*/ |
| 18 |
private $reset; |
| 19 |
|
| 20 |
/** |
| 21 |
* Constructor. |
| 22 |
* |
| 23 |
* @param ResetInternalState $reset ResetInternalState service. |
| 24 |
*/ |
| 25 |
public function __construct( ResetInternalState $reset ) { |
| 26 |
$this->reset = $reset; |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Returns the events this subscriber listens to. |
| 31 |
* |
| 32 |
* Note: the settings-section template is rendered directly via print_template() |
| 33 |
* in page-settings.php — no imagify_settings_tools hook is registered here. |
| 34 |
* |
| 35 |
* @return array |
| 36 |
*/ |
| 37 |
public static function get_subscribed_events(): array { |
| 38 |
return [ |
| 39 |
// @action |
| 40 |
'wp_ajax_imagify_reset_internal_state' => 'reset_internal_state', |
| 41 |
]; |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Handles the AJAX request to reset Imagify internal state. |
| 46 |
* |
| 47 |
* Verifies the nonce and capability before delegating to ResetInternalState. |
| 48 |
* |
| 49 |
* @return void |
| 50 |
*/ |
| 51 |
public function reset_internal_state(): void { |
| 52 |
imagify_check_nonce( 'imagify_reset_internal_state' ); |
| 53 |
|
| 54 |
if ( ! imagify_get_context( 'wp' )->current_user_can( 'manage' ) ) { |
| 55 |
imagify_die(); |
| 56 |
return; |
| 57 |
} |
| 58 |
|
| 59 |
$this->reset->reset(); |
| 60 |
|
| 61 |
wp_send_json_success( [ 'message' => __( 'Imagify internal state has been reset successfully.', 'imagify' ) ] ); |
| 62 |
} |
| 63 |
} |
| 64 |
|