balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

class-redux-ajax-select2.php (2990B)


      1 <?php
      2 /**
      3  * Redux Select2 AJAX Class
      4  *
      5  * @class   Redux_AJAX_Select2
      6  * @version 4.0.0
      7  * @package Redux Framework/Classes
      8  */
      9 
     10 defined( 'ABSPATH' ) || exit;
     11 
     12 if ( ! class_exists( 'Redux_AJAX_Select2', false ) ) {
     13 
     14 	/**
     15 	 * Class Redux_AJAX_Select2
     16 	 */
     17 	class Redux_AJAX_Select2 extends Redux_Class {
     18 
     19 		/**
     20 		 * Redux_AJAX_Select2 constructor.
     21 		 *
     22 		 * @param object $parent ReduxFramework object pointer.
     23 		 */
     24 		public function __construct( $parent ) {
     25 			parent::__construct( $parent );
     26 			// phpcs:ignore WordPress.NamingConventions.ValidHookName
     27 			add_action( "wp_ajax_redux_{$parent->args['opt_name']}_select2", array( $this, 'ajax' ) );
     28 		}
     29 
     30 		/**
     31 		 * AJAX callback for select2 match search.
     32 		 */
     33 		public function ajax() {
     34 			$core = $this->core();
     35 
     36 			if ( isset( $_REQUEST['nonce'] ) && isset( $_REQUEST['action'] ) ) {
     37 				if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_REQUEST['nonce'] ) ), sanitize_text_field( wp_unslash( $_REQUEST['action'] ) ) ) ) {
     38 					wp_send_json_error( esc_html__( 'Invalid security credential.  Please reload the page and try again.', 'redux-framework' ) );
     39 				}
     40 
     41 				if ( ! Redux_Helpers::current_user_can( $this->parent->args['page_permissions'] ) ) {
     42 					wp_send_json_error( esc_html__( 'Invalid user capability.  Please reload the page and try again.', 'redux-framework' ) );
     43 				}
     44 
     45 				if ( isset( $_REQUEST['data'] ) ) {
     46 
     47 					$args = isset( $_REQUEST['data_args'] ) ? json_decode( sanitize_text_field( wp_unslash( $_REQUEST['data_args'] ) ), true ) : array();
     48 					$args = wp_parse_args(
     49 						$args,
     50 						array(
     51 							's' => isset( $_REQUEST['q'] ) && ! empty( $_REQUEST['q'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['q'] ) ) : '',
     52 						)
     53 					);
     54 
     55 					$criteria = '';
     56 					if ( isset( $_REQUEST['q'] ) && ! empty( $_REQUEST['q'] ) ) {
     57 						$criteria  = sanitize_text_field( wp_unslash( $_REQUEST['q'] ) );
     58 						$args['s'] = $criteria;
     59 					}
     60 
     61 					$return = $core->wordpress_data->get( sanitize_text_field( wp_unslash( $_REQUEST['data'] ) ), $args );
     62 
     63 					if ( is_array( $return ) && ! empty( $_REQUEST['action'] ) ) {
     64 						if ( ! empty( $args['s'] ) ) {
     65 							$keys   = array_keys( $return );
     66 							$values = array_values( $return );
     67 
     68 							$to_json = array();
     69 
     70 							// Search all the values.
     71 							$search_values = preg_grep( '~' . $args['s'] . '~i', $values );
     72 							if ( ! empty( $search_values ) ) {
     73 								foreach ( $search_values as $id => $val ) {
     74 									$to_json[ $keys[ $id ] ] = array(
     75 										'id'   => $keys[ $id ],
     76 										'text' => $val,
     77 									);
     78 								}
     79 							}
     80 							// Search all the keys.
     81 							$search_keys = preg_grep( '~' . $args['s'] . '~i', $keys );
     82 							if ( ! empty( $search_keys ) ) {
     83 								foreach ( $search_keys as $id => $val ) {
     84 									$to_json[ $val ] = array(
     85 										'id'   => $val,
     86 										'text' => $values[ $id ],
     87 									);
     88 								}
     89 							}
     90 							wp_send_json_success( array_values( $to_json ) );
     91 						}
     92 					}
     93 				}
     94 			}
     95 		}
     96 	}
     97 }