PluginProbe
404 Solution / 4.1.19
404 Solution v4.1.19
4.3.5 4.3.4 4.3.3 4.3.2 4.3.1 4.3.0 4.2.0 4.1.19 4.1.18 4.1.17 4.1.16 4.1.15 4.1.13 4.1.12 4.1.11 4.1.10 4.1.9 4.1.8 4.1.7 4.1.6 4.1.5 4.1.4 4.1.3 trunk 2.30.0 All 109 releases
404-solution / includes / ajax / AjaxSecurityTrait.php

AjaxSecurityTrait.php in 404 Solution 4.1.19, at includes/ajax/AjaxSecurityTrait.php

41 lines 1.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if (!defined('ABSPATH')) {
4 exit;
5 }
6
7 /**
8 * Shared nonce + admin permission gate for AJAX handlers.
9 *
10 * Extracts the nonce from POST or GET, verifies it, and checks
11 * userIsPluginAdmin(). On failure, sends a JSON 403 and terminates.
12 */
13 trait ABJ_404_Solution_AjaxSecurityTrait {
14
15 /**
16 * Verify nonce and admin permissions. Sends JSON error and terminates on failure.
17 *
18 * @param string $action The nonce action string.
19 * @param string $nonceParam The POST/GET parameter name holding the nonce (default 'nonce').
20 * @return void
21 */
22 private static function requireAdminWithNonce(string $action, string $nonceParam = 'nonce'): void {
23 $nonce = '';
24 if (isset($_POST[$nonceParam]) && is_string($_POST[$nonceParam])) {
25 $nonce = sanitize_text_field($_POST[$nonceParam]);
26 } elseif (isset($_GET[$nonceParam]) && is_string($_GET[$nonceParam])) {
27 $nonce = sanitize_text_field($_GET[$nonceParam]);
28 }
29
30 if (!wp_verify_nonce($nonce, $action)) {
31 wp_send_json_error(array('message' => __('Invalid security token', '404-solution')), 403);
32 return; // @phpstan-ignore deadCode.unreachable
33 }
34
35 if (!abj_service('plugin_logic')->userIsPluginAdmin()) {
36 wp_send_json_error(array('message' => __('Unauthorized', '404-solution')), 403);
37 return; // @phpstan-ignore deadCode.unreachable
38 }
39 }
40 }
41