PluginProbe ʕ •ᴥ•ʔ
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). / 2.2.15
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). v2.2.15
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
AjaxRequest.php
107 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 the response and errors
51 $this->response = array(
52 'success' => true,
53 );
54 $this->errors = array();
55 $this->request = array_map( 'sanitize_text_field', wp_unslash( $_REQUEST ) );
56
57 // Try to detect JSON content in the request if specific data key is not used
58 $json_data = null;
59
60 if ( empty( $_POST ) ) {
61 // Attempt to retrieve raw JSON content if POST is empty
62 $contents = trim( file_get_contents( 'php://input' ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
63
64 // Check if contents contain valid JSON
65 if ( $this->is_json( $contents ) ) {
66 $json_data = sanitize_text_field( $contents );
67 }
68 }
69
70 // Decode JSON if found, then extract data
71 if ( $json_data ) {
72 $decode = json_decode( $json_data, true );
73 if ( ! empty( $decode ) ) {
74 $data = $this->extract_data( $decode, $keys );
75 $this->data = ! empty( $data ) ? $data : array();
76 }
77 }
78 }
79
80 /**
81 * Utility to check if a string is JSON.
82 */
83 private function is_json( string $string ): bool {
84 json_decode( $string );
85 return json_last_error() === JSON_ERROR_NONE;
86 }
87
88 /**
89 * Extracts data from an array using the given keys.
90 *
91 * @param array $sanitized The array from which to extract data.
92 * @param array $keys The keys to use for extraction.
93 *
94 * @return array The extracted data.
95 */
96 public function extract_data( array $sanitized, array $keys ): array {
97 return array_intersect_key( $sanitized, array_flip( $keys ) );
98 }
99
100 /**
101 * Register AJAX actions.
102 */
103 private function action( $action ): string {
104 return sprintf( 'wp_ajax_%s-%s', Template::NAME, $action );
105 }
106 }
107