@@ -3,7 +3,7 @@ discard block |
||
3 | 3 | * Class MAG_CMB2_Field_Post_Search_Ajax |
4 | 4 | */ |
5 | 5 | |
6 | -if ( ! class_exists( 'MAG_CMB2_Field_Post_Search_Ajax' ) ) { |
|
6 | +if ( ! class_exists('MAG_CMB2_Field_Post_Search_Ajax')) { |
|
7 | 7 | |
8 | 8 | /** |
9 | 9 | * The LSX Post Search Field |
@@ -35,9 +35,9 @@ discard block |
||
35 | 35 | * Initialize the plugin by hooking into CMB2 |
36 | 36 | */ |
37 | 37 | public function __construct() { |
38 | - add_action( 'cmb2_render_post_search_ajax', array( $this, 'render' ), 10, 5 ); |
|
39 | - add_action( 'cmb2_sanitize_post_search_ajax', array( $this, 'sanitize' ), 10, 4 ); |
|
40 | - add_action( 'wp_ajax_cmb_post_search_ajax_get_results', array( $this, 'cmb_post_search_ajax_get_results' ) ); |
|
38 | + add_action('cmb2_render_post_search_ajax', array($this, 'render'), 10, 5); |
|
39 | + add_action('cmb2_sanitize_post_search_ajax', array($this, 'sanitize'), 10, 4); |
|
40 | + add_action('wp_ajax_cmb_post_search_ajax_get_results', array($this, 'cmb_post_search_ajax_get_results')); |
|
41 | 41 | } |
42 | 42 | |
43 | 43 | /** |
@@ -49,7 +49,7 @@ discard block |
||
49 | 49 | */ |
50 | 50 | public static function get_instance() { |
51 | 51 | // If the single instance hasn't been set, set it now. |
52 | - if ( null === self::$instance ) { |
|
52 | + if (null === self::$instance) { |
|
53 | 53 | self::$instance = new self(); |
54 | 54 | } |
55 | 55 | return self::$instance; |
@@ -58,37 +58,37 @@ discard block |
||
58 | 58 | /** |
59 | 59 | * Render field |
60 | 60 | */ |
61 | - public function render( $field, $value, $object_id, $object_type, $field_type ) { |
|
61 | + public function render($field, $value, $object_id, $object_type, $field_type) { |
|
62 | 62 | $this->setup_admin_scripts(); |
63 | 63 | $field_name = $field->_name(); |
64 | 64 | |
65 | - if ( $field->args( 'limit' ) > 1 ) { |
|
65 | + if ($field->args('limit') > 1) { |
|
66 | 66 | echo '<ul class="cmb-post-search-ajax-results" id="' . $field_name . '_results">'; |
67 | - if ( isset( $value ) && ! empty( $value ) ) { |
|
67 | + if (isset($value) && ! empty($value)) { |
|
68 | 68 | |
69 | - if ( ! is_array( $value ) ) { |
|
70 | - $value = explode( ',', $value ); |
|
69 | + if ( ! is_array($value)) { |
|
70 | + $value = explode(',', $value); |
|
71 | 71 | } |
72 | - if ( ! is_array( $value ) ) { |
|
73 | - $value = array( $value ); |
|
72 | + if ( ! is_array($value)) { |
|
73 | + $value = array($value); |
|
74 | 74 | } |
75 | - $value = array_unique( $value ); |
|
76 | - foreach ( $value as $val ) { |
|
77 | - $handle = ( $field->args( 'sortable' ) ) ? '<span class="hndl"></span>' : ''; |
|
75 | + $value = array_unique($value); |
|
76 | + foreach ($value as $val) { |
|
77 | + $handle = ($field->args('sortable')) ? '<span class="hndl"></span>' : ''; |
|
78 | 78 | $li_css = ''; |
79 | - if ( $field->args( 'object_type' ) == 'user' ) { |
|
80 | - $guid = get_edit_user_link( $val ); |
|
81 | - $user = get_userdata( $val ); |
|
79 | + if ($field->args('object_type') == 'user') { |
|
80 | + $guid = get_edit_user_link($val); |
|
81 | + $user = get_userdata($val); |
|
82 | 82 | $title = $user->display_name; |
83 | 83 | } else { |
84 | - $guid = get_edit_post_link( $val ); |
|
85 | - $title = get_the_title( $val ) . ' - ' . '#' . $val; |
|
86 | - if ( 'trash' === get_post_status( $val ) ) { |
|
84 | + $guid = get_edit_post_link($val); |
|
85 | + $title = get_the_title($val) . ' - ' . '#' . $val; |
|
86 | + if ('trash' === get_post_status($val)) { |
|
87 | 87 | $li_css = 'display:none;'; |
88 | 88 | } |
89 | - $post_parent = wp_get_post_parent_id( $val ); |
|
90 | - if ( 0 !== $post_parent && false !== $post_parent ) { |
|
91 | - $title = get_the_title( $post_parent ) . ' -> ' . $title; |
|
89 | + $post_parent = wp_get_post_parent_id($val); |
|
90 | + if (0 !== $post_parent && false !== $post_parent) { |
|
91 | + $title = get_the_title($post_parent) . ' -> ' . $title; |
|
92 | 92 | } |
93 | 93 | } |
94 | 94 | echo '<li style="' . $li_css . '">' . $handle . '<input type="hidden" name="' . $field_name . '_results[]" value="' . $val . '"><a href="' . $guid . '" target="_blank" class="edit-link">' . $title . '</a><a class="remover"><span class="dashicons dashicons-no"></span><span class="dashicons dashicons-dismiss"></span></a></li>'; |
@@ -96,10 +96,10 @@ discard block |
||
96 | 96 | } |
97 | 97 | echo '</ul>'; |
98 | 98 | $field_value = ''; |
99 | - if ( isset( $field->group ) ) { |
|
100 | - $store_name = str_replace( '][', '_', $field_name ); |
|
101 | - $store_name = str_replace( ']', '', $store_name ); |
|
102 | - $store_name = str_replace( '[', '_', $store_name ); |
|
99 | + if (isset($field->group)) { |
|
100 | + $store_name = str_replace('][', '_', $field_name); |
|
101 | + $store_name = str_replace(']', '', $store_name); |
|
102 | + $store_name = str_replace('[', '_', $store_name); |
|
103 | 103 | |
104 | 104 | echo $field_type->input( |
105 | 105 | array( |
@@ -107,19 +107,19 @@ discard block |
||
107 | 107 | 'id' => $field_name . '_store', |
108 | 108 | 'name' => $store_name . '_store', |
109 | 109 | 'class' => 'cmb-post-search-ajax-store', |
110 | - 'value' => implode( ',', $value ), |
|
110 | + 'value' => implode(',', $value), |
|
111 | 111 | 'desc' => false, |
112 | 112 | ) |
113 | 113 | ); |
114 | 114 | } |
115 | 115 | } else { |
116 | - if ( is_array( $value ) ) { |
|
116 | + if (is_array($value)) { |
|
117 | 117 | $value = $value[0]; |
118 | 118 | } |
119 | - if ( $field->args( 'object_type' ) == 'user' ) { |
|
120 | - $field_value = ( $value ? get_userdata( $value )->display_name : '' ); |
|
119 | + if ($field->args('object_type') == 'user') { |
|
120 | + $field_value = ($value ? get_userdata($value)->display_name : ''); |
|
121 | 121 | } else { |
122 | - $field_value = ( $value ? get_the_title( $value ) : '' ); |
|
122 | + $field_value = ($value ? get_the_title($value) : ''); |
|
123 | 123 | } |
124 | 124 | echo $field_type->input( |
125 | 125 | array( |
@@ -129,10 +129,10 @@ discard block |
||
129 | 129 | 'desc' => false, |
130 | 130 | ) |
131 | 131 | ); |
132 | - if ( isset( $field->group ) ) { |
|
133 | - $store_name = str_replace( '][', '_', $field_name ); |
|
134 | - $store_name = str_replace( ']', '', $store_name ); |
|
135 | - $store_name = str_replace( '[', '_', $store_name ); |
|
132 | + if (isset($field->group)) { |
|
133 | + $store_name = str_replace('][', '_', $field_name); |
|
134 | + $store_name = str_replace(']', '', $store_name); |
|
135 | + $store_name = str_replace('[', '_', $store_name); |
|
136 | 136 | |
137 | 137 | echo $field_type->input( |
138 | 138 | array( |
@@ -155,64 +155,64 @@ discard block |
||
155 | 155 | 'class' => 'cmb-post-search-ajax', |
156 | 156 | 'value' => $field_value, |
157 | 157 | 'desc' => false, |
158 | - 'data-limit' => $field->args( 'limit' ) ? $field->args( 'limit' ) : '1', |
|
159 | - 'data-sortable' => $field->args( 'sortable' ) ? $field->args( 'sortable' ) : '0', |
|
160 | - 'data-object' => $field->args( 'object_type' ) ? $field->args( 'object_type' ) : 'post', |
|
161 | - 'data-queryargs'=> $field->args( 'query_args' ) ? htmlspecialchars( json_encode( $field->args( 'query_args' ) ), ENT_QUOTES, 'UTF-8' ) : '' |
|
158 | + 'data-limit' => $field->args('limit') ? $field->args('limit') : '1', |
|
159 | + 'data-sortable' => $field->args('sortable') ? $field->args('sortable') : '0', |
|
160 | + 'data-object' => $field->args('object_type') ? $field->args('object_type') : 'post', |
|
161 | + 'data-queryargs'=> $field->args('query_args') ? htmlspecialchars(json_encode($field->args('query_args')), ENT_QUOTES, 'UTF-8') : '' |
|
162 | 162 | ) |
163 | 163 | ); |
164 | 164 | |
165 | - echo '<img src="' . admin_url( 'images/spinner.gif' ) . '" class="cmb-post-search-ajax-spinner" />'; |
|
165 | + echo '<img src="' . admin_url('images/spinner.gif') . '" class="cmb-post-search-ajax-spinner" />'; |
|
166 | 166 | |
167 | - $field_type->_desc( true, true ); |
|
167 | + $field_type->_desc(true, true); |
|
168 | 168 | |
169 | 169 | } |
170 | 170 | |
171 | 171 | /** |
172 | 172 | * Optionally save the latitude/longitude values into two custom fields |
173 | 173 | */ |
174 | - public function sanitize( $override_value, $value, $object_id, $field_args ) { |
|
174 | + public function sanitize($override_value, $value, $object_id, $field_args) { |
|
175 | 175 | $fid = ''; |
176 | - if ( isset( $field_args['id'] ) ) { |
|
176 | + if (isset($field_args['id'])) { |
|
177 | 177 | $fid = $field_args['id']; |
178 | 178 | } |
179 | 179 | |
180 | 180 | // IF the field is in a repeatable group, then get the info from the post data. |
181 | - if ( isset( $field_args['render_row_cb'][0]->group ) && ! empty( $field_args['render_row_cb'][0]->group ) ) { |
|
181 | + if (isset($field_args['render_row_cb'][0]->group) && ! empty($field_args['render_row_cb'][0]->group)) { |
|
182 | 182 | $new_index = ''; |
183 | 183 | $data_to_save = $field_args['render_row_cb'][0]->group->args['render_row_cb'][0]->data_to_save; |
184 | 184 | $oid = $field_args['_name']; |
185 | 185 | $iid = $field_args['_id']; |
186 | - $oid = explode( '[', $oid ); |
|
186 | + $oid = explode('[', $oid); |
|
187 | 187 | |
188 | - if ( is_array( $oid ) ) { |
|
188 | + if (is_array($oid)) { |
|
189 | 189 | $oid = $oid[0]; |
190 | 190 | } |
191 | 191 | |
192 | - if ( isset( $data_to_save[ $oid ] ) && ! empty( $data_to_save[ $oid ] ) ) { |
|
193 | - foreach( $data_to_save[ $oid ] as $index => $svalues ) { |
|
194 | - if ( isset( $svalues[ $iid ] ) && $value === $svalues[ $iid ] ) { |
|
192 | + if (isset($data_to_save[$oid]) && ! empty($data_to_save[$oid])) { |
|
193 | + foreach ($data_to_save[$oid] as $index => $svalues) { |
|
194 | + if (isset($svalues[$iid]) && $value === $svalues[$iid]) { |
|
195 | 195 | $new_index = $index; |
196 | 196 | } |
197 | 197 | } |
198 | 198 | } |
199 | 199 | |
200 | - if ( isset( $field_args['render_row_cb'][0]->group->index ) ) { |
|
200 | + if (isset($field_args['render_row_cb'][0]->group->index)) { |
|
201 | 201 | $new_index = $field_args['render_row_cb'][0]->group->index; |
202 | 202 | } |
203 | 203 | |
204 | - if ( '' !== $new_index ) { |
|
204 | + if ('' !== $new_index) { |
|
205 | 205 | $new_index = $oid . '_' . $new_index . '_' . $iid . '_store'; |
206 | - if ( ! empty( $data_to_save[ $new_index ] ) ) { |
|
207 | - $value = $data_to_save[ $new_index ]; |
|
206 | + if ( ! empty($data_to_save[$new_index])) { |
|
207 | + $value = $data_to_save[$new_index]; |
|
208 | 208 | } |
209 | 209 | } else { |
210 | 210 | $value = false; |
211 | 211 | } |
212 | - } else if ( ! empty( $field_args['render_row_cb'][0]->data_to_save[ $fid . '_results' ] ) ) { |
|
213 | - $value = $field_args['render_row_cb'][0]->data_to_save[ $fid . '_results' ]; |
|
214 | - } else if ( ! empty( $field_args['render_row_cb'][0]->data_to_save[ $fid . '_store' ] ) ) { |
|
215 | - $value = $field_args['render_row_cb'][0]->data_to_save[ $fid . '_store' ]; |
|
212 | + } else if ( ! empty($field_args['render_row_cb'][0]->data_to_save[$fid . '_results'])) { |
|
213 | + $value = $field_args['render_row_cb'][0]->data_to_save[$fid . '_results']; |
|
214 | + } else if ( ! empty($field_args['render_row_cb'][0]->data_to_save[$fid . '_store'])) { |
|
215 | + $value = $field_args['render_row_cb'][0]->data_to_save[$fid . '_store']; |
|
216 | 216 | } else { |
217 | 217 | $value = false; |
218 | 218 | } |
@@ -241,25 +241,25 @@ discard block |
||
241 | 241 | * Defines the url which is used to load local resources. Based on, and uses, |
242 | 242 | * the CMB2_Utils class from the CMB2 library. |
243 | 243 | */ |
244 | - public static function url( $path = '' ) { |
|
245 | - if ( self::$url ) { |
|
244 | + public static function url($path = '') { |
|
245 | + if (self::$url) { |
|
246 | 246 | return self::$url . $path; |
247 | 247 | } |
248 | 248 | |
249 | 249 | /** |
250 | 250 | * Set the variable cmb2_fpsa_dir |
251 | 251 | */ |
252 | - $cmb2_fpsa_dir = trailingslashit( dirname( __FILE__ ) ); |
|
252 | + $cmb2_fpsa_dir = trailingslashit(dirname(__FILE__)); |
|
253 | 253 | |
254 | 254 | /** |
255 | 255 | * Use CMB2_Utils to gather the url from cmb2_fpsa_dir |
256 | 256 | */ |
257 | - $cmb2_fpsa_url = CMB2_Utils::get_url_from_dir( $cmb2_fpsa_dir ); |
|
257 | + $cmb2_fpsa_url = CMB2_Utils::get_url_from_dir($cmb2_fpsa_dir); |
|
258 | 258 | |
259 | 259 | /** |
260 | 260 | * Filter the CMB2 FPSA location url |
261 | 261 | */ |
262 | - self::$url = trailingslashit( apply_filters( 'cmb2_fpsa_url', $cmb2_fpsa_url, self::VERSION ) ); |
|
262 | + self::$url = trailingslashit(apply_filters('cmb2_fpsa_url', $cmb2_fpsa_url, self::VERSION)); |
|
263 | 263 | |
264 | 264 | return self::$url . $path; |
265 | 265 | } |
@@ -269,14 +269,14 @@ discard block |
||
269 | 269 | */ |
270 | 270 | public function setup_admin_scripts() { |
271 | 271 | |
272 | - wp_register_script( 'jquery-devautocomplete', self::url( 'js/jquery.autocomplete.min.js' ), array( 'jquery' ), self::VERSION ); |
|
273 | - wp_register_script( 'mag-post-search-ajax', self::url( 'js/mag-post-search-ajax.js' ), array( 'jquery', 'jquery-devautocomplete', 'jquery-ui-sortable' ), self::VERSION ); |
|
274 | - wp_localize_script( 'mag-post-search-ajax', 'psa', array( |
|
275 | - 'ajaxurl' => admin_url( 'admin-ajax.php' ), |
|
276 | - 'nonce' => wp_create_nonce( 'mag_cmb_post_search_ajax_get_results' ) |
|
277 | - ) ); |
|
278 | - wp_enqueue_script( 'mag-post-search-ajax' ); |
|
279 | - wp_enqueue_style( 'mag-post-search-ajax', self::url( 'css/mag-post-search-ajax.css' ), array(), self::VERSION ); |
|
272 | + wp_register_script('jquery-devautocomplete', self::url('js/jquery.autocomplete.min.js'), array('jquery'), self::VERSION); |
|
273 | + wp_register_script('mag-post-search-ajax', self::url('js/mag-post-search-ajax.js'), array('jquery', 'jquery-devautocomplete', 'jquery-ui-sortable'), self::VERSION); |
|
274 | + wp_localize_script('mag-post-search-ajax', 'psa', array( |
|
275 | + 'ajaxurl' => admin_url('admin-ajax.php'), |
|
276 | + 'nonce' => wp_create_nonce('mag_cmb_post_search_ajax_get_results') |
|
277 | + )); |
|
278 | + wp_enqueue_script('mag-post-search-ajax'); |
|
279 | + wp_enqueue_style('mag-post-search-ajax', self::url('css/mag-post-search-ajax.css'), array(), self::VERSION); |
|
280 | 280 | |
281 | 281 | } |
282 | 282 | |
@@ -284,52 +284,52 @@ discard block |
||
284 | 284 | * Ajax request : get results |
285 | 285 | */ |
286 | 286 | public function cmb_post_search_ajax_get_results() { |
287 | - $nonce = sanitize_text_field( $_POST['psacheck'] ); |
|
288 | - if ( ! wp_verify_nonce( $nonce, 'mag_cmb_post_search_ajax_get_results' ) ) { |
|
289 | - die( json_encode( array( 'error' => __( 'Error : Unauthorized action' ) ) ) ); |
|
287 | + $nonce = sanitize_text_field($_POST['psacheck']); |
|
288 | + if ( ! wp_verify_nonce($nonce, 'mag_cmb_post_search_ajax_get_results')) { |
|
289 | + die(json_encode(array('error' => __('Error : Unauthorized action')))); |
|
290 | 290 | } else { |
291 | - $args = json_decode( stripslashes( htmlspecialchars_decode( sanitize_text_field( $_POST['query_args'] ) ) ), true ); |
|
292 | - $args['s'] = sanitize_text_field( $_POST['query'] ); |
|
291 | + $args = json_decode(stripslashes(htmlspecialchars_decode(sanitize_text_field($_POST['query_args']))), true); |
|
292 | + $args['s'] = sanitize_text_field($_POST['query']); |
|
293 | 293 | $datas = array(); |
294 | - if ( $_POST['object'] == 'user' ) { |
|
294 | + if ($_POST['object'] == 'user') { |
|
295 | 295 | |
296 | - $args['search'] = '*' . esc_attr( sanitize_text_field( $_POST['query'] ) ) . '*'; |
|
297 | - $users = new WP_User_Query( $args ); |
|
296 | + $args['search'] = '*' . esc_attr(sanitize_text_field($_POST['query'])) . '*'; |
|
297 | + $users = new WP_User_Query($args); |
|
298 | 298 | $results = $users->get_results(); |
299 | 299 | |
300 | - if ( ! empty( $results ) ) { |
|
301 | - foreach ( $results as $result ){ |
|
302 | - $user_info = get_userdata( $result->ID ); |
|
300 | + if ( ! empty($results)) { |
|
301 | + foreach ($results as $result) { |
|
302 | + $user_info = get_userdata($result->ID); |
|
303 | 303 | // Define filter "mag_cmb_post_search_ajax_result" to allow customize ajax results. |
304 | - $datas[] = apply_filters( 'mag_cmb_post_search_ajax_result', array( |
|
304 | + $datas[] = apply_filters('mag_cmb_post_search_ajax_result', array( |
|
305 | 305 | 'value' => $user_info->display_name, |
306 | 306 | 'data' => $result->ID, |
307 | - 'guid' => get_edit_user_link( $result->ID ), |
|
308 | - ) ); |
|
307 | + 'guid' => get_edit_user_link($result->ID), |
|
308 | + )); |
|
309 | 309 | } |
310 | 310 | } |
311 | 311 | } else { |
312 | - $results = new WP_Query( $args ); |
|
313 | - if ( $results->have_posts() ) : |
|
314 | - while ( $results->have_posts() ) : $results->the_post(); |
|
312 | + $results = new WP_Query($args); |
|
313 | + if ($results->have_posts()) : |
|
314 | + while ($results->have_posts()) : $results->the_post(); |
|
315 | 315 | // Define filter "mag_cmb_post_search_ajax_result" to allow customize ajax results. |
316 | 316 | |
317 | 317 | $title = get_the_title() . ' - ' . '#' . get_the_ID(); |
318 | - $post_parent = wp_get_post_parent_id( get_the_ID() ); |
|
319 | - if ( 0 !== $post_parent && false !== $post_parent ) { |
|
320 | - $title = get_the_title( $post_parent ) . ' -> ' . $title; |
|
318 | + $post_parent = wp_get_post_parent_id(get_the_ID()); |
|
319 | + if (0 !== $post_parent && false !== $post_parent) { |
|
320 | + $title = get_the_title($post_parent) . ' -> ' . $title; |
|
321 | 321 | } |
322 | 322 | |
323 | - $datas[] = apply_filters( 'mag_cmb_post_search_ajax_result', array( |
|
323 | + $datas[] = apply_filters('mag_cmb_post_search_ajax_result', array( |
|
324 | 324 | 'value' => $title, |
325 | 325 | 'data' => get_the_ID(), |
326 | 326 | 'guid' => get_edit_post_link(), |
327 | - ) ); |
|
327 | + )); |
|
328 | 328 | endwhile; |
329 | 329 | endif; |
330 | 330 | } |
331 | 331 | wp_reset_postdata(); |
332 | - die( json_encode( $datas ) ); |
|
332 | + die(json_encode($datas)); |
|
333 | 333 | } |
334 | 334 | } |
335 | 335 | } |