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 |