Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
70.00% covered (warning)
70.00%
14 / 20
33.33% covered (danger)
33.33%
1 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
AJAX
70.00% covered (warning)
70.00%
14 / 20
33.33% covered (danger)
33.33%
1 / 3
14.27
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
70.00% covered (warning)
70.00%
7 / 10
0.00% covered (danger)
0.00%
0 / 1
5.68
 delete_all
66.67% covered (warning)
66.67%
6 / 9
0.00% covered (danger)
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
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 \BrianHenryIE\WP_Logger\API_Interface::delete_log()
17 * @uses \BrianHenryIE\WP_Logger\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            || ! wp_verify_nonce( sanitize_key( $_POST['_wpnonce'] ), 'bh-wp-logger-delete' ) ) {
54            return;
55        }
56
57        // bh-wp-logger could be hooked for many plugins.
58        if ( $this->settings->get_plugin_slug() !== sanitize_key( $_POST['plugin_slug'] ) ) {
59            return;
60        }
61
62        $ymd_date = sanitize_key( $_POST['date_to_delete'] );
63
64        $result = $this->api->delete_log( $ymd_date );
65
66        if ( $result['success'] ) {
67            wp_send_json_success( $result );
68        } else {
69            wp_send_json_error( $result );
70        }
71    }
72
73    /**
74     * Delete all log files for this plugin.
75     *
76     * Request body should contain:
77     * * `action` "bh_wp_logger_logs_delete".
78     * * `plugin_slug` containing the slug used in settings.
79     * * `_wpnonce` with action "bh-wp-logger-delete".
80     *
81     * Response format will be:
82     * array{success: bool, message: ?string}.
83     *
84     * @hooked wp_ajax_bh_wp_logger_logs_delete_all
85     *
86     * @uses \BrianHenryIE\WP_Logger\API_Interface::delete_all_logs()
87     */
88    public function delete_all(): void {
89
90        if ( ! isset( $_POST['_wpnonce'], $_POST['plugin_slug'] )
91            || ! wp_verify_nonce( sanitize_key( $_POST['_wpnonce'] ), 'bh-wp-logger-delete' ) ) {
92            return;
93        }
94
95        // bh-wp-logger could be hooked for many plugins.
96        if ( $this->settings->get_plugin_slug() !== sanitize_key( $_POST['plugin_slug'] ) ) {
97            return;
98        }
99
100        $result = $this->api->delete_all_logs();
101
102        if ( $result['success'] ) {
103            wp_send_json_success( $result );
104        } else {
105            wp_send_json_error( $result );
106        }
107    }
108}