PluginProbe
SureCookie – GDPR Cookie Consent Banner, Cookie Scanner & Script Blocking / trunk
SureCookie – GDPR Cookie Consent Banner, Cookie Scanner & Script Blocking vtrunk
1.5.0 1.4.0 1.3.0 1.3.1 trunk 0.0.0-alpha.1 0.0.0-alpha.2 0.0.0-alpha.3 0.0.1-beta.1 0.0.1-beta.2 0.0.1-beta.3 0.0.1-beta.4 1.0.0 1.1.0 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4
surecookie / inc / modules / automatic-scanning / api.php

api.php in SureCookie – GDPR Cookie Consent Banner, Cookie Scanner & Script Blocking trunk, at inc/modules/automatic-scanning/api.php

134 lines 3.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Automatic Scanning REST API.
4 *
5 * Exposes the single latest scan record (with its change set), scheduling
6 * status, and a manual "Scan now" trigger. Registered via the
7 * surecookie_api_controllers filter and gated by the shared nonce + capability.
8 *
9 * @package SureCookie\Inc\Modules\AutomaticScanning
10 * @since 1.2.0
11 */
12
13 namespace SureCookie\Inc\Modules\AutomaticScanning;
14
15 use SureCookie\Inc\API\Base;
16 use SureCookie\Inc\Functions\Helper;
17 use SureCookie\Inc\Functions\SendJson;
18 use SureCookie\Inc\Functions\Settings;
19 use SureCookie\Inc\Modules\SiteScanner\SaasClient;
20 use SureCookie\Inc\Traits\GetInstance;
21 use WP_REST_Server;
22
23 if ( ! defined( 'ABSPATH' ) ) {
24 exit; // Exit if accessed directly.
25 }
26
27 /**
28 * Api
29 *
30 * @since 1.2.0
31 */
32 class Api extends Base {
33 use GetInstance;
34
35 /**
36 * Register API routes.
37 *
38 * @since 1.2.0
39 * @return void
40 */
41 public function register_routes(): void {
42 register_rest_route(
43 $this->get_api_namespace(),
44 '/auto-scan/history',
45 [
46 'methods' => WP_REST_Server::READABLE,
47 'callback' => [ $this, 'get_history' ],
48 'permission_callback' => [ $this, 'validate_permission' ],
49 ]
50 );
51
52 register_rest_route(
53 $this->get_api_namespace(),
54 '/auto-scan/next-run',
55 [
56 'methods' => WP_REST_Server::READABLE,
57 'callback' => [ $this, 'get_next_run' ],
58 'permission_callback' => [ $this, 'validate_permission' ],
59 ]
60 );
61
62 register_rest_route(
63 $this->get_api_namespace(),
64 '/auto-scan/run-now',
65 [
66 'methods' => WP_REST_Server::CREATABLE,
67 'callback' => [ $this, 'run_now' ],
68 'permission_callback' => [ $this, 'validate_permission' ],
69 ]
70 );
71 }
72
73 /**
74 * Get the single latest scan record with its change set.
75 *
76 * @param \WP_REST_Request<array<string, mixed>> $request Request.
77 * @since 1.2.0
78 * @return void
79 */
80 public function get_history( $request ): void {
81 unset( $request );
82
83 SendJson::success( [ 'history' => History::get_latest() ] );
84 }
85
86 /**
87 * Get scheduling status (next run, enablement, cron availability).
88 *
89 * @param \WP_REST_Request<array<string, mixed>> $request Request.
90 * @since 1.2.0
91 * @return void
92 */
93 public function get_next_run( $request ): void {
94 unset( $request );
95
96 SendJson::success(
97 [
98 'enabled' => (bool) Settings::get( 'auto_scan_enabled' ),
99 'frequency' => Scheduler::effective_frequency(),
100 'allowed_frequencies' => Scheduler::allowed_frequencies(),
101 'next_run' => Scheduler::next_run(),
102 'cron_status' => Helper::are_crons_available(),
103 ]
104 );
105 }
106
107 /**
108 * Trigger the scheduled scan flow on demand.
109 *
110 * @param \WP_REST_Request<array<string, mixed>> $request Request.
111 * @since 1.2.0
112 * @return void
113 */
114 public function run_now( $request ): void {
115 unset( $request );
116
117 if ( ! (bool) Settings::get( 'auto_scan_enabled' ) ) {
118 SendJson::error( [ 'message' => __( 'Enable Automatic Scanning before running a scan.', 'surecookie' ) ] );
119 }
120
121 if ( SaasClient::get_instance()->is_scan_in_progress() ) {
122 SendJson::error( [ 'message' => __( 'A scan is already in progress.', 'surecookie' ) ] );
123 }
124
125 try {
126 Runner::get_instance()->run_scheduled_scan();
127 } catch ( \Throwable $e ) {
128 SendJson::error( [ 'message' => __( 'Could not start the scan. Please try again.', 'surecookie' ) ] );
129 }
130
131 SendJson::success( [ 'message' => __( 'Scan triggered.', 'surecookie' ) ] );
132 }
133 }
134