Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
71.43% |
15 / 21 |
|
33.33% |
1 / 3 |
CRAP | |
0.00% |
0 / 1 |
| AJAX | |
71.43% |
15 / 21 |
|
33.33% |
1 / 3 |
13.82 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| delete | |
70.00% |
7 / 10 |
|
0.00% |
0 / 1 |
5.68 | |||
| delete_all | |
66.67% |
6 / 9 |
|
0.00% |
0 / 1 |
5.93 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Handle AJAX requests from the log table page (or custom settings page). |
| 4 | * |
| 5 | * @package brianhenryie/bh-wp-logger |
| 6 | */ |
| 7 | |
| 8 | namespace BrianHenryIE\WP_Logger\Admin; |
| 9 | |
| 10 | use BrianHenryIE\WP_Logger\API_Interface; |
| 11 | use BrianHenryIE\WP_Logger\Logger_Settings_Interface; |
| 12 | |
| 13 | /** |
| 14 | * Handle delete and delete-all actions. |
| 15 | */ |
| 16 | class AJAX { |
| 17 | |
| 18 | /** |
| 19 | * Settings describing the plugin this logger is for. |
| 20 | * |
| 21 | * @uses Logger_Settings_Interface::get_plugin_slug() |
| 22 | * |
| 23 | * @var Logger_Settings_Interface |
| 24 | */ |
| 25 | protected Logger_Settings_Interface $settings; |
| 26 | |
| 27 | /** |
| 28 | * The plugin's main functions. |
| 29 | * |
| 30 | * @uses \BrianHenryIE\WP_Logger\API_Interface::delete_log() |
| 31 | * @uses \BrianHenryIE\WP_Logger\API_Interface::delete_all_logs() |
| 32 | * |
| 33 | * @var API_Interface |
| 34 | */ |
| 35 | protected API_Interface $api; |
| 36 | |
| 37 | /** |
| 38 | * AJAX constructor. |
| 39 | * |
| 40 | * @param API_Interface $api Implementation of the plugin's main functions. |
| 41 | * @param Logger_Settings_Interface $settings The current settings for the logger. |
| 42 | */ |
| 43 | public function __construct( API_Interface $api, Logger_Settings_Interface $settings ) { |
| 44 | $this->api = $api; |
| 45 | $this->settings = $settings; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Delete a single log file. |
| 50 | * |
| 51 | * Request body should contain: |
| 52 | * * `action` "bh_wp_logger_logs_delete_all". |
| 53 | * * `date_to_delete` in `Y-m-d` format, e.g. 2022-03-02. |
| 54 | * * `plugin_slug` containing the slug used in settings. |
| 55 | * * `_wpnonce` with action `bh-wp-logger-delete`. |
| 56 | * |
| 57 | * Response format will be: |
| 58 | * array{success: bool, message: ?string}. |
| 59 | * |
| 60 | * @hooked wp_ajax_bh_wp_logger_logs_delete |
| 61 | * |
| 62 | * @uses API_Interface::delete_log() |
| 63 | */ |
| 64 | public function delete(): void { |
| 65 | |
| 66 | if ( ! isset( $_POST['_wpnonce'], $_POST['plugin_slug'], $_POST['date_to_delete'] ) |
| 67 | || ! wp_verify_nonce( sanitize_key( $_POST['_wpnonce'] ), 'bh-wp-logger-delete' ) ) { |
| 68 | return; |
| 69 | } |
| 70 | |
| 71 | // bh-wp-logger could be hooked for many plugins. |
| 72 | if ( $this->settings->get_plugin_slug() !== sanitize_key( $_POST['plugin_slug'] ) ) { |
| 73 | return; |
| 74 | } |
| 75 | |
| 76 | $ymd_date = sanitize_key( $_POST['date_to_delete'] ); |
| 77 | |
| 78 | $result = $this->api->delete_log( $ymd_date ); |
| 79 | |
| 80 | if ( $result['success'] ) { |
| 81 | wp_send_json_success( $result ); |
| 82 | } else { |
| 83 | wp_send_json_error( $result ); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Delete all log files for this plugin. |
| 89 | * |
| 90 | * Request body should contain: |
| 91 | * * `action` "bh_wp_logger_logs_delete". |
| 92 | * * `plugin_slug` containing the slug used in settings. |
| 93 | * * `_wpnonce` with action "bh-wp-logger-delete". |
| 94 | * |
| 95 | * Response format will be: |
| 96 | * array{success: bool, message: ?string}. |
| 97 | * |
| 98 | * @hooked wp_ajax_bh_wp_logger_logs_delete_all |
| 99 | * |
| 100 | * @uses \BrianHenryIE\WP_Logger\API_Interface::delete_all_logs() |
| 101 | */ |
| 102 | public function delete_all(): void { |
| 103 | |
| 104 | if ( ! isset( $_POST['_wpnonce'], $_POST['plugin_slug'] ) |
| 105 | || ! wp_verify_nonce( sanitize_key( $_POST['_wpnonce'] ), 'bh-wp-logger-delete' ) ) { |
| 106 | return; |
| 107 | } |
| 108 | |
| 109 | // bh-wp-logger could be hooked for many plugins. |
| 110 | if ( $this->settings->get_plugin_slug() !== sanitize_key( $_POST['plugin_slug'] ) ) { |
| 111 | return; |
| 112 | } |
| 113 | |
| 114 | $result = $this->api->delete_all_logs(); |
| 115 | |
| 116 | if ( $result['success'] ) { |
| 117 | wp_send_json_success( $result ); |
| 118 | } else { |
| 119 | wp_send_json_error( $result ); |
| 120 | } |
| 121 | } |
| 122 | } |