| @@ -1,12 +1,8 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | namespace NotificationX\Admin\Scanner; |
| 3 | 3 | |
| 4 | -use NotificationX\Admin\Entries; | |
| 5 | -use NotificationX\Core\Database; | |
| 6 | 4 | use NotificationX\Core\Helper; |
| 7 | -use NotificationX\Core\Limiter; | |
| 8 | -use NotificationX\Core\PostType; | |
| 9 | 5 | use NotificationX\GetInstance; |
| 10 | 6 | use NotificationX\NotificationX; |
| 11 | 7 | use WP_REST_Server; |
| 12 | 8 | use WP_REST_Request; |
| @@ -51,9 +47,9 @@ | ||
| 51 | 47 | // Register the scan initiation endpoint |
| 52 | 48 | register_rest_route($namespace, '/scan', array( |
| 53 | 49 | 'methods' => WP_REST_Server::CREATABLE, |
| 54 | 50 | 'callback' => array($this, 'initiate_scan'), |
| 55 | - 'permission_callback' => '__return_true', | |
| 51 | + 'permission_callback' => array($this, 'edit_permission'), | |
| 56 | 52 | )); |
| 57 | 53 | |
| 58 | 54 | // Register the scan status endpoint |
| 59 | 55 | register_rest_route($namespace, '/scan/status', array( |
| @@ -58,14 +54,26 @@ | ||
| 58 | 54 | // Register the scan status endpoint |
| 59 | 55 | register_rest_route($namespace, '/scan/status', array( |
| 60 | 56 | 'methods' => WP_REST_Server::EDITABLE, |
| 61 | 57 | 'callback' => array($this, 'check_scan_status'), |
| 62 | - 'permission_callback' => '__return_true', | |
| 58 | + 'permission_callback' => array($this, 'edit_permission'), | |
| 63 | 59 | )); |
| 64 | 60 | |
| 65 | 61 | } |
| 66 | 62 | |
| 67 | 63 | /** |
| 64 | + * Permission check for the scanner routes. | |
| 65 | + * The cookie scanner is only used from the notification builder, | |
| 66 | + * which requires the edit_notificationx capability. | |
| 67 | + * | |
| 68 | + * @return bool | |
| 69 | + */ | |
| 70 | + public function edit_permission() | |
| 71 | + { | |
| 72 | + return current_user_can('edit_notificationx'); | |
| 73 | + } | |
| 74 | + | |
| 75 | + /** | |
| 68 | 76 | * Initiates a scan process for the provided URL. |
| 69 | 77 | * |
| 70 | 78 | * Validates the URL parameter and triggers the scan if it's valid. |
| 71 | 79 | * Responds with the scan status. |
| @@ -101,9 +109,8 @@ | ||
| 101 | 109 | */ |
| 102 | 110 | public function check_scan_status(WP_REST_Request $request) |
| 103 | 111 | { |
| 104 | 112 | $scanId = $request->get_param('scan_id'); |
| 105 | - $nx_id = $request->get_param('nx_id'); | |
| 106 | 113 | |
| 107 | 114 | // Retrieve the scan status from the database (implement this function as needed) |
| 108 | 115 | $status = $this->get_scan_status($scanId); |
| 109 | 116 | |
| @@ -128,68 +135,9 @@ | ||
| 128 | 135 | $categoryCount = $cookieData['category_count']; |
| 129 | 136 | $categorized = $cookieData['categorized']; |
| 130 | 137 | $data = [ 'last_scan_date' => Helper::nx_get_current_datetime(), 'status' => 'completed', 'stats' => $stats, 'cookies' => $cookies, 'category_count' => $categoryCount, 'categorized' => $categorized ]; |
| 131 | 138 | return new WP_REST_Response(['data' => $data], 200); |
| 132 | - | |
| 133 | - // Prepare stats entry with the category count | |
| 134 | - if (!empty($stats) && is_array($stats)) { | |
| 135 | - $stats['category_count'] = $categoryCount; | |
| 136 | - } | |
| 137 | - if (!empty($stats) && is_array($stats)) { | |
| 138 | - $stats['categorized'] = $categorized; | |
| 139 | - } | |
| 140 | - if( empty( $nx_id ) ) { | |
| 141 | - return new WP_REST_Response(['data' => $status], 200); | |
| 142 | - } | |
| 143 | - | |
| 144 | - // If cookies or stats exist, prepare them for database insertion | |
| 145 | - if ((!empty($cookies) && is_array($cookies)) || (!empty($stats) && is_array($stats))) { | |
| 146 | - $entriesToInsert = []; | |
| 147 | - | |
| 148 | - // Prepare cookies entry | |
| 149 | - if (!empty($cookies) && is_array($cookies)) { | |
| 150 | - $entriesToInsert[] = [ | |
| 151 | - 'nx_id' => $nx_id, | |
| 152 | - 'source' => 'gdpr_notification', | |
| 153 | - 'entry_key' => $scanId . '_cookies', | |
| 154 | - 'data' => $cookies, | |
| 155 | - 'created_at' => date('Y-m-d H:i:s'), | |
| 156 | - ]; | |
| 157 | - } | |
| 158 | - | |
| 159 | - // Prepare stats entry | |
| 160 | - if (!empty($stats) && is_array($stats)) { | |
| 161 | - $entriesToInsert[] = [ | |
| 162 | - 'nx_id' => $nx_id, | |
| 163 | - 'source' => 'gdpr_notification', | |
| 164 | - 'entry_key' => $scanId . '_stats', | |
| 165 | - 'data' => $stats, | |
| 166 | - 'created_at' => date('Y-m-d H:i:s'), | |
| 167 | - ]; | |
| 168 | - } | |
| 169 | - | |
| 170 | - // Insert data into the database | |
| 171 | - foreach ($entriesToInsert as $entry) { | |
| 172 | - // Check if an entry already exists | |
| 173 | - $isExists = Database::get_instance()->get_posts( | |
| 174 | - Database::$table_entries, 'count(*)', [ | |
| 175 | - 'nx_id' => $nx_id, | |
| 176 | - 'source' => 'gdpr_notification', | |
| 177 | - 'entry_key' => $entry['entry_key'], | |
| 178 | - ] | |
| 179 | - ); | |
| 180 | - | |
| 181 | - if (empty($isExists[0]['count(*)'])) { | |
| 182 | - $post = PostType::get_instance()->get_post($nx_id); | |
| 183 | - $canEntry = apply_filters("nx_can_entry_gdpr_notification", true, $entry, $post); | |
| 184 | - if ($canEntry) { | |
| 185 | - Limiter::get_instance()->remove($nx_id, 1); | |
| 186 | - Entries::get_instance()->insert_entry($entry); | |
| 187 | - } | |
| 188 | - } | |
| 189 | - } | |
| 190 | - } | |
| 191 | - } | |
| 139 | + } | |
| 192 | 140 | |
| 193 | 141 | return new WP_REST_Response(['data' => $status], 200); |
| 194 | 142 | } |
| 195 | 143 | |