PluginProbe ʕ •ᴥ•ʔ
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). / 2.3.2
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). v2.3.2
3.0.3 3.0.2 3.0.1 trunk 2.2.14 2.2.15 2.2.16 2.2.17 2.2.18 2.2.19 2.3.0 2.3.1 2.3.10 2.3.11 2.3.12 2.3.13 2.3.14 2.3.2 2.3.3 2.3.4 2.3.5 2.3.6 2.3.7 2.3.8 2.3.9 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.5.0 2.5.1 2.5.2 2.6.0 2.6.1 2.6.2 2.6.3 2.6.4 2.6.5 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 2.7.8 2.7.9 2.7.91 2.7.92 2.7.93 2.8.0 2.8.1 2.8.2 2.8.3 2.8.4 2.8.5 2.9.0 2.9.1 2.9.2 2.9.3 3.0.0
commercebird / admin / includes / Traits / AjaxRequest.php
commercebird / admin / includes / Traits Last commit date
AjaxRequest.php 1 year ago LogWriter.php 1 year ago OptionStatus.php 1 year ago Singleton.php 1 year ago index.php 1 year ago
AjaxRequest.php
98 lines
1 <?php
2
3 namespace CommerceBird\Admin\Traits;
4
5 use CommerceBird\Admin\Template;
6
7 if ( ! defined( 'ABSPATH' ) ) {
8 exit;
9 }
10
11 trait AjaxRequest {
12
13 // Array to store registered AJAX requests
14 private array $request = array();
15 // Array to store registered AJAX response
16 private array $response = array( 'message' => 'Saved' );
17 // Array to store registered AJAX posted data
18 private array $data = array();
19 // Array to store registered AJAX errors
20 private array $errors = array();
21
22 private function load_actions() {
23 foreach ( self::ACTIONS as $action => $handler ) {
24 add_action(
25 $this->action( $action ),
26 array( $this, $handler ),
27 );
28 }
29 }
30
31 /**
32 * Serve data to AJAX request.
33 */
34 private function serve(): void {
35 if ( count( $this->errors ) > 0 ) {
36 wp_send_json_error( $this->errors );
37 }
38
39 wp_send_json_success( $this->response );
40 }
41
42 /**
43 * Verify AJAX request.
44 *
45 * @param array $keys The keys to verify (optional).
46 */
47 private function verify( array $keys = array() ): void {
48 check_ajax_referer( Template::NAME, 'security_token' );
49
50 // Initialize response and errors
51 $this->response = array( 'success' => true );
52 $this->errors = array();
53 $this->request = array_map( 'sanitize_text_field', wp_unslash( $_REQUEST ) );
54
55 // Attempt to retrieve JSON if POST is empty
56 if ( empty( $_POST ) ) {
57 $contents = trim( file_get_contents( 'php://input' ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
58
59 if ( $this->is_json( $contents ) ) {
60 $json_data = json_decode( $contents, true );
61
62 if ( ! empty( $json_data ) ) {
63 // Sanitize each value, but do not sanitize entire JSON string
64 $sanitized_data = array_map( 'sanitize_text_field', wp_unslash( $json_data ) );
65 $this->data = empty( $keys ) ? $sanitized_data : $this->extract_data( $sanitized_data, $keys );
66 }
67 }
68 }
69 }
70
71 /**
72 * Utility to check if a string is JSON.
73 */
74 private function is_json( string $string ): bool {
75 json_decode( $string );
76 return json_last_error() === JSON_ERROR_NONE;
77 }
78
79 /**
80 * Extracts data from an array using the given keys.
81 *
82 * @param array $sanitized The array from which to extract data.
83 * @param array $keys The keys to use for extraction.
84 *
85 * @return array The extracted data.
86 */
87 public function extract_data( array $sanitized, array $keys ): array {
88 return array_intersect_key( $sanitized, array_flip( $keys ) );
89 }
90
91 /**
92 * Register AJAX actions.
93 */
94 private function action( $action ): string {
95 return sprintf( 'wp_ajax_%s-%s', Template::NAME, $action );
96 }
97 }
98