@@ -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,29 +58,29 @@ 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 | |
67 | 67 | echo '<ul class="cmb-post-search-ajax-results" id="' . $field_name . '_results">'; |
68 | - if ( isset( $value ) && ! empty( $value ) ) { |
|
69 | - if ( ! is_array( $value ) ) { |
|
70 | - $value = array( $value ); |
|
68 | + if (isset($value) && ! empty($value)) { |
|
69 | + if ( ! is_array($value)) { |
|
70 | + $value = array($value); |
|
71 | 71 | } |
72 | - $value = array_unique( $value ); |
|
73 | - foreach ( $value as $val ) { |
|
74 | - $handle = ( $field->args( 'sortable' ) ) ? '<span class="hndl"></span>' : ''; |
|
72 | + $value = array_unique($value); |
|
73 | + foreach ($value as $val) { |
|
74 | + $handle = ($field->args('sortable')) ? '<span class="hndl"></span>' : ''; |
|
75 | 75 | $li_css = ''; |
76 | - if ( $field->args( 'object_type' ) == 'user' ) { |
|
77 | - $guid = get_edit_user_link( $val ); |
|
78 | - $user = get_userdata( $val ); |
|
76 | + if ($field->args('object_type') == 'user') { |
|
77 | + $guid = get_edit_user_link($val); |
|
78 | + $user = get_userdata($val); |
|
79 | 79 | $title = $user->display_name; |
80 | 80 | } else { |
81 | - $guid = get_edit_post_link( $val ); |
|
82 | - $title = get_the_title( $val ); |
|
83 | - if ( 'trash' === get_post_status( $val ) ) { |
|
81 | + $guid = get_edit_post_link($val); |
|
82 | + $title = get_the_title($val); |
|
83 | + if ('trash' === get_post_status($val)) { |
|
84 | 84 | $li_css = 'display:none;'; |
85 | 85 | } |
86 | 86 | } |
@@ -90,13 +90,13 @@ discard block |
||
90 | 90 | echo '</ul>'; |
91 | 91 | $field_value = ''; |
92 | 92 | } else { |
93 | - if ( is_array( $value ) ) { |
|
93 | + if (is_array($value)) { |
|
94 | 94 | $value = $value[0]; |
95 | 95 | } |
96 | - if ( $field->args( 'object_type' ) == 'user' ) { |
|
97 | - $field_value = ( $value ? get_userdata( $value )->display_name : '' ); |
|
96 | + if ($field->args('object_type') == 'user') { |
|
97 | + $field_value = ($value ? get_userdata($value)->display_name : ''); |
|
98 | 98 | } else { |
99 | - $field_value = ( $value ? get_the_title( $value ) : '' ); |
|
99 | + $field_value = ($value ? get_the_title($value) : ''); |
|
100 | 100 | } |
101 | 101 | echo $field_type->input( |
102 | 102 | array( |
@@ -106,10 +106,10 @@ discard block |
||
106 | 106 | 'desc' => false, |
107 | 107 | ) |
108 | 108 | ); |
109 | - if ( isset( $field->group ) ) { |
|
110 | - $store_name = str_replace( '][', '_', $field_name ); |
|
111 | - $store_name = str_replace( ']', '', $store_name ); |
|
112 | - $store_name = str_replace( '[', '_', $store_name ); |
|
109 | + if (isset($field->group)) { |
|
110 | + $store_name = str_replace('][', '_', $field_name); |
|
111 | + $store_name = str_replace(']', '', $store_name); |
|
112 | + $store_name = str_replace('[', '_', $store_name); |
|
113 | 113 | |
114 | 114 | echo $field_type->input( |
115 | 115 | array( |
@@ -132,59 +132,59 @@ discard block |
||
132 | 132 | 'class' => 'cmb-post-search-ajax', |
133 | 133 | 'value' => $field_value, |
134 | 134 | 'desc' => false, |
135 | - 'data-limit' => $field->args( 'limit' ) ? $field->args( 'limit' ) : '1', |
|
136 | - 'data-sortable' => $field->args( 'sortable' ) ? $field->args( 'sortable' ) : '0', |
|
137 | - 'data-object' => $field->args( 'object_type' ) ? $field->args( 'object_type' ) : 'post', |
|
138 | - 'data-queryargs'=> $field->args( 'query_args' ) ? htmlspecialchars( json_encode( $field->args( 'query_args' ) ), ENT_QUOTES, 'UTF-8' ) : '' |
|
135 | + 'data-limit' => $field->args('limit') ? $field->args('limit') : '1', |
|
136 | + 'data-sortable' => $field->args('sortable') ? $field->args('sortable') : '0', |
|
137 | + 'data-object' => $field->args('object_type') ? $field->args('object_type') : 'post', |
|
138 | + 'data-queryargs'=> $field->args('query_args') ? htmlspecialchars(json_encode($field->args('query_args')), ENT_QUOTES, 'UTF-8') : '' |
|
139 | 139 | ) |
140 | 140 | ); |
141 | 141 | |
142 | - echo '<img src="' . admin_url( 'images/spinner.gif' ) . '" class="cmb-post-search-ajax-spinner" />'; |
|
142 | + echo '<img src="' . admin_url('images/spinner.gif') . '" class="cmb-post-search-ajax-spinner" />'; |
|
143 | 143 | |
144 | - $field_type->_desc( true, true ); |
|
144 | + $field_type->_desc(true, true); |
|
145 | 145 | |
146 | 146 | } |
147 | 147 | |
148 | 148 | /** |
149 | 149 | * Optionally save the latitude/longitude values into two custom fields |
150 | 150 | */ |
151 | - public function sanitize( $override_value, $value, $object_id, $field_args ) { |
|
151 | + public function sanitize($override_value, $value, $object_id, $field_args) { |
|
152 | 152 | $fid = ''; |
153 | - if ( isset( $field_args['id'] ) ) { |
|
153 | + if (isset($field_args['id'])) { |
|
154 | 154 | $fid = $field_args['id']; |
155 | 155 | } |
156 | 156 | |
157 | 157 | // IF the field is in a repeatable group, then get the info from the post data. |
158 | - if ( isset( $field_args['render_row_cb'][0]->group ) && ! empty( $field_args['render_row_cb'][0]->group ) ) { |
|
158 | + if (isset($field_args['render_row_cb'][0]->group) && ! empty($field_args['render_row_cb'][0]->group)) { |
|
159 | 159 | $new_index = ''; |
160 | 160 | |
161 | 161 | $data_to_save = $field_args['render_row_cb'][0]->group->args['render_row_cb'][0]->data_to_save; |
162 | 162 | $oid = $field_args['_name']; |
163 | 163 | $iid = $field_args['_id']; |
164 | - $oid = explode( '[', $oid ); |
|
165 | - if ( is_array( $oid ) ) { |
|
164 | + $oid = explode('[', $oid); |
|
165 | + if (is_array($oid)) { |
|
166 | 166 | $oid = $oid[0]; |
167 | 167 | } |
168 | 168 | |
169 | - if ( isset( $data_to_save[ $oid ] ) && ! empty( $data_to_save[ $oid ] ) ) { |
|
170 | - foreach( $data_to_save[ $oid ] as $index => $svalues ) { |
|
171 | - if ( isset( $svalues[ $iid ] ) && $value === $svalues[ $iid ] ) { |
|
169 | + if (isset($data_to_save[$oid]) && ! empty($data_to_save[$oid])) { |
|
170 | + foreach ($data_to_save[$oid] as $index => $svalues) { |
|
171 | + if (isset($svalues[$iid]) && $value === $svalues[$iid]) { |
|
172 | 172 | $new_index = $index; |
173 | 173 | } |
174 | 174 | } |
175 | 175 | } |
176 | 176 | |
177 | - if ( '' !== $new_index ) { |
|
177 | + if ('' !== $new_index) { |
|
178 | 178 | $new_index = $oid . '_' . $new_index . '_' . $iid . '_store'; |
179 | 179 | |
180 | - if ( ! empty( $data_to_save[ $new_index ] ) ) { |
|
181 | - $value = $data_to_save[ $new_index ]; |
|
180 | + if ( ! empty($data_to_save[$new_index])) { |
|
181 | + $value = $data_to_save[$new_index]; |
|
182 | 182 | } |
183 | 183 | } else { |
184 | 184 | $value = false; |
185 | 185 | } |
186 | - } else if ( ! empty( $field_args['render_row_cb'][0]->data_to_save[ $fid . '_results' ] ) ) { |
|
187 | - $value = $field_args['render_row_cb'][0]->data_to_save[ $fid . '_results' ]; |
|
186 | + } else if ( ! empty($field_args['render_row_cb'][0]->data_to_save[$fid . '_results'])) { |
|
187 | + $value = $field_args['render_row_cb'][0]->data_to_save[$fid . '_results']; |
|
188 | 188 | } else { |
189 | 189 | $value = false; |
190 | 190 | } |
@@ -196,25 +196,25 @@ discard block |
||
196 | 196 | * Defines the url which is used to load local resources. Based on, and uses, |
197 | 197 | * the CMB2_Utils class from the CMB2 library. |
198 | 198 | */ |
199 | - public static function url( $path = '' ) { |
|
200 | - if ( self::$url ) { |
|
199 | + public static function url($path = '') { |
|
200 | + if (self::$url) { |
|
201 | 201 | return self::$url . $path; |
202 | 202 | } |
203 | 203 | |
204 | 204 | /** |
205 | 205 | * Set the variable cmb2_fpsa_dir |
206 | 206 | */ |
207 | - $cmb2_fpsa_dir = trailingslashit( dirname( __FILE__ ) ); |
|
207 | + $cmb2_fpsa_dir = trailingslashit(dirname(__FILE__)); |
|
208 | 208 | |
209 | 209 | /** |
210 | 210 | * Use CMB2_Utils to gather the url from cmb2_fpsa_dir |
211 | 211 | */ |
212 | - $cmb2_fpsa_url = CMB2_Utils::get_url_from_dir( $cmb2_fpsa_dir ); |
|
212 | + $cmb2_fpsa_url = CMB2_Utils::get_url_from_dir($cmb2_fpsa_dir); |
|
213 | 213 | |
214 | 214 | /** |
215 | 215 | * Filter the CMB2 FPSA location url |
216 | 216 | */ |
217 | - self::$url = trailingslashit( apply_filters( 'cmb2_fpsa_url', $cmb2_fpsa_url, self::VERSION ) ); |
|
217 | + self::$url = trailingslashit(apply_filters('cmb2_fpsa_url', $cmb2_fpsa_url, self::VERSION)); |
|
218 | 218 | |
219 | 219 | return self::$url . $path; |
220 | 220 | } |
@@ -224,14 +224,14 @@ discard block |
||
224 | 224 | */ |
225 | 225 | public function setup_admin_scripts() { |
226 | 226 | |
227 | - wp_register_script( 'jquery-devautocomplete', self::url( 'js/jquery.autocomplete.min.js' ), array( 'jquery' ), self::VERSION ); |
|
228 | - wp_register_script( 'mag-post-search-ajax', self::url( 'js/mag-post-search-ajax.js' ), array( 'jquery', 'jquery-devautocomplete', 'jquery-ui-sortable' ), self::VERSION ); |
|
229 | - wp_localize_script( 'mag-post-search-ajax', 'psa', array( |
|
230 | - 'ajaxurl' => admin_url( 'admin-ajax.php' ), |
|
231 | - 'nonce' => wp_create_nonce( 'mag_cmb_post_search_ajax_get_results' ) |
|
232 | - ) ); |
|
233 | - wp_enqueue_script( 'mag-post-search-ajax' ); |
|
234 | - wp_enqueue_style( 'mag-post-search-ajax', self::url( 'css/mag-post-search-ajax.css' ), array(), self::VERSION ); |
|
227 | + wp_register_script('jquery-devautocomplete', self::url('js/jquery.autocomplete.min.js'), array('jquery'), self::VERSION); |
|
228 | + wp_register_script('mag-post-search-ajax', self::url('js/mag-post-search-ajax.js'), array('jquery', 'jquery-devautocomplete', 'jquery-ui-sortable'), self::VERSION); |
|
229 | + wp_localize_script('mag-post-search-ajax', 'psa', array( |
|
230 | + 'ajaxurl' => admin_url('admin-ajax.php'), |
|
231 | + 'nonce' => wp_create_nonce('mag_cmb_post_search_ajax_get_results') |
|
232 | + )); |
|
233 | + wp_enqueue_script('mag-post-search-ajax'); |
|
234 | + wp_enqueue_style('mag-post-search-ajax', self::url('css/mag-post-search-ajax.css'), array(), self::VERSION); |
|
235 | 235 | |
236 | 236 | } |
237 | 237 | |
@@ -239,45 +239,45 @@ discard block |
||
239 | 239 | * Ajax request : get results |
240 | 240 | */ |
241 | 241 | public function cmb_post_search_ajax_get_results() { |
242 | - $nonce = sanitize_text_field( $_POST['psacheck'] ); |
|
243 | - if ( ! wp_verify_nonce( $nonce, 'mag_cmb_post_search_ajax_get_results' ) ) { |
|
244 | - die( json_encode( array( 'error' => __( 'Error : Unauthorized action' ) ) ) ); |
|
242 | + $nonce = sanitize_text_field($_POST['psacheck']); |
|
243 | + if ( ! wp_verify_nonce($nonce, 'mag_cmb_post_search_ajax_get_results')) { |
|
244 | + die(json_encode(array('error' => __('Error : Unauthorized action')))); |
|
245 | 245 | } else { |
246 | - $args = json_decode( stripslashes( htmlspecialchars_decode( sanitize_text_field( $_POST['query_args'] ) ) ), true ); |
|
247 | - $args['s'] = sanitize_text_field( $_POST['query'] ); |
|
246 | + $args = json_decode(stripslashes(htmlspecialchars_decode(sanitize_text_field($_POST['query_args']))), true); |
|
247 | + $args['s'] = sanitize_text_field($_POST['query']); |
|
248 | 248 | $datas = array(); |
249 | - if ( $_POST['object'] == 'user' ) { |
|
249 | + if ($_POST['object'] == 'user') { |
|
250 | 250 | |
251 | - $args['search'] = '*' . esc_attr( sanitize_text_field( $_POST['query'] ) ) . '*'; |
|
252 | - $users = new WP_User_Query( $args ); |
|
251 | + $args['search'] = '*' . esc_attr(sanitize_text_field($_POST['query'])) . '*'; |
|
252 | + $users = new WP_User_Query($args); |
|
253 | 253 | $results = $users->get_results(); |
254 | 254 | |
255 | - if ( ! empty( $results ) ) { |
|
256 | - foreach ( $results as $result ){ |
|
257 | - $user_info = get_userdata( $result->ID ); |
|
255 | + if ( ! empty($results)) { |
|
256 | + foreach ($results as $result) { |
|
257 | + $user_info = get_userdata($result->ID); |
|
258 | 258 | // Define filter "mag_cmb_post_search_ajax_result" to allow customize ajax results. |
259 | - $datas[] = apply_filters( 'mag_cmb_post_search_ajax_result', array( |
|
259 | + $datas[] = apply_filters('mag_cmb_post_search_ajax_result', array( |
|
260 | 260 | 'value' => $user_info->display_name, |
261 | 261 | 'data' => $result->ID, |
262 | - 'guid' => get_edit_user_link( $result->ID ), |
|
263 | - ) ); |
|
262 | + 'guid' => get_edit_user_link($result->ID), |
|
263 | + )); |
|
264 | 264 | } |
265 | 265 | } |
266 | 266 | } else { |
267 | - $results = new WP_Query( $args ); |
|
268 | - if ( $results->have_posts() ) : |
|
269 | - while ( $results->have_posts() ) : $results->the_post(); |
|
267 | + $results = new WP_Query($args); |
|
268 | + if ($results->have_posts()) : |
|
269 | + while ($results->have_posts()) : $results->the_post(); |
|
270 | 270 | // Define filter "mag_cmb_post_search_ajax_result" to allow customize ajax results. |
271 | - $datas[] = apply_filters( 'mag_cmb_post_search_ajax_result', array( |
|
271 | + $datas[] = apply_filters('mag_cmb_post_search_ajax_result', array( |
|
272 | 272 | 'value' => get_the_title() . ' - ' . '#' . get_the_ID(), |
273 | 273 | 'data' => get_the_ID(), |
274 | 274 | 'guid' => get_edit_post_link(), |
275 | - ) ); |
|
275 | + )); |
|
276 | 276 | endwhile; |
277 | 277 | endif; |
278 | 278 | } |
279 | 279 | wp_reset_postdata(); |
280 | - die( json_encode( $datas ) ); |
|
280 | + die(json_encode($datas)); |
|
281 | 281 | } |
282 | 282 | } |
283 | 283 | } |
@@ -29,7 +29,7 @@ discard block |
||
29 | 29 | * Contructor |
30 | 30 | */ |
31 | 31 | public function __construct() { |
32 | - add_action( 'init', array( $this, 'cmb2_post_search_ajax' ) ); |
|
32 | + add_action('init', array($this, 'cmb2_post_search_ajax')); |
|
33 | 33 | $this->load_vendors(); |
34 | 34 | } |
35 | 35 | |
@@ -43,7 +43,7 @@ discard block |
||
43 | 43 | public static function get_instance() { |
44 | 44 | |
45 | 45 | // If the single instance hasn't been set, set it now. |
46 | - if ( null === self::$instance ) { |
|
46 | + if (null === self::$instance) { |
|
47 | 47 | self::$instance = new self(); |
48 | 48 | } |
49 | 49 | |
@@ -56,7 +56,7 @@ discard block |
||
56 | 56 | */ |
57 | 57 | private function load_vendors() { |
58 | 58 | // Configure custom fields. |
59 | - if ( ! class_exists( 'CMB2' ) ) { |
|
59 | + if ( ! class_exists('CMB2')) { |
|
60 | 60 | require_once LSX_PROJECTS_PATH . 'vendor/CMB2/init.php'; |
61 | 61 | } |
62 | 62 | } |
@@ -67,11 +67,11 @@ discard block |
||
67 | 67 | * @return void |
68 | 68 | */ |
69 | 69 | public function get_post_types() { |
70 | - $post_types = apply_filters( 'lsx_projects_post_types', isset( $this->post_types ) ); |
|
71 | - foreach ( $post_types as $index => $post_type ) { |
|
72 | - $is_disabled = \cmb2_get_option( 'lsx_projects_options', $post_type . '_disabled', false ); |
|
73 | - if ( true === $is_disabled || 1 === $is_disabled || 'on' === $is_disabled ) { |
|
74 | - unset( $post_types[ $index ] ); |
|
70 | + $post_types = apply_filters('lsx_projects_post_types', isset($this->post_types)); |
|
71 | + foreach ($post_types as $index => $post_type) { |
|
72 | + $is_disabled = \cmb2_get_option('lsx_projects_options', $post_type . '_disabled', false); |
|
73 | + if (true === $is_disabled || 1 === $is_disabled || 'on' === $is_disabled) { |
|
74 | + unset($post_types[$index]); |
|
75 | 75 | } |
76 | 76 | } |
77 | 77 | return $post_types; |
@@ -84,7 +84,7 @@ discard block |
||
84 | 84 | */ |
85 | 85 | public function cmb2_post_search_ajax() { |
86 | 86 | require_once LSX_PROJECTS_PATH . 'vendor/lsx-field-post-search-ajax/cmb-field-post-search-ajax.php'; |
87 | - if ( method_exists( 'MAG_CMB2_Field_Post_Search_Ajax', 'get_instance' ) ) { |
|
87 | + if (method_exists('MAG_CMB2_Field_Post_Search_Ajax', 'get_instance')) { |
|
88 | 88 | $this->cmb2_post_search_ajax = \MAG_CMB2_Field_Post_Search_Ajax::get_instance(); |
89 | 89 | } |
90 | 90 | } |