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