From 6a720d8e7ed44fef75a941a8f884fccc0b325eab Mon Sep 17 00:00:00 2001 From: Jonathan Stegall Date: Wed, 25 Jul 2018 11:26:31 -0500 Subject: [PATCH 1/8] check for 1 as limit instead of greater than 1 to allow for -1 --- cmb-field-post-search-ajax.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmb-field-post-search-ajax.php b/cmb-field-post-search-ajax.php index 12b366e..b3a13ff 100644 --- a/cmb-field-post-search-ajax.php +++ b/cmb-field-post-search-ajax.php @@ -43,7 +43,7 @@ public function render( $field, $value, $object_id, $object_type, $field_type ) $this->setup_admin_scripts(); $field_name = $field->_name(); - if($field->args( 'limit' ) > 1){ + if(1 !== $field->args( 'limit' )){ echo ''; $field_value = ''; - } - else{ - if(is_array($value)){ $value = $value[0]; } - if( $field->args( 'object_type' ) == 'user' ){ - $field_value = ($value ? get_userdata($value)->display_name : ''); + } else { + if ( is_array( $value ) ) { + $value = $value[0]; } - else{ - $field_value = ($value ? get_the_title($value) : ''); + if ( 'user' === $field->args( 'object_type' ) ) { + $field_value = ( $value ? get_userdata( $value )->display_name : '' ); + } else { + $field_value = ( $value ? get_the_title( $value ) : '' ); } - echo $field_type->input( array( - 'type' => 'hidden', - 'name' => $field_name . '_results', - 'value' => $value, - 'desc' => false - ) ); + echo $field_type->input( + array( + 'type' => 'hidden', + 'name' => $field_name . '_results', + 'value' => $value, + 'desc' => false, + ) + ); } - echo $field_type->input( array( - 'type' => 'text', - 'name' => $field_name, - 'id' => $field_name, - 'class' => 'cmb-post-search-ajax', - 'value' => $field_value, - 'desc' => false, - 'data-limit' => $field->args( 'limit' ) ? $field->args( 'limit' ) : '1', - 'data-sortable' => $field->args( 'sortable' ) ? $field->args( 'sortable' ) : '0', - 'data-object' => $field->args( 'object_type' ) ? $field->args( 'object_type' ) : 'post', - 'data-queryargs'=> $field->args( 'query_args' ) ? htmlspecialchars( json_encode( $field->args( 'query_args' ) ), ENT_QUOTES, 'UTF-8' ) : '' - ) ); - - echo ''; + echo $field_type->input( + array( + 'type' => 'text', + 'name' => $field_name, + 'id' => $field_name, + 'class' => 'cmb-post-search-ajax', + 'value' => $field_value, + 'desc' => false, + 'data-limit' => $field->args( 'limit' ) ? $field->args( 'limit' ) : '1', + 'data-sortable' => $field->args( 'sortable' ) ? $field->args( 'sortable' ) : '0', + 'data-object' => $field->args( 'object_type' ) ? $field->args( 'object_type' ) : 'post', + 'data-queryargs' => $field->args( 'query_args' ) ? htmlspecialchars( json_encode( $field->args( 'query_args' ) ), ENT_QUOTES, 'UTF-8' ) : '', + ) + ); + + echo ''; $field_type->_desc( true, true ); @@ -104,17 +109,16 @@ public function render( $field, $value, $object_id, $object_type, $field_type ) */ public function sanitize( $override_value, $value, $object_id, $field_args ) { $fid = $field_args['id']; - if( !empty( $field_args['render_row_cb'][0]->data_to_save[$fid.'_results'] ) ){ - $value = $field_args['render_row_cb'][0]->data_to_save[$fid.'_results']; - } - else{ + if ( ! empty( $field_args['render_row_cb'][0]->data_to_save[ $fid . '_results' ] ) ) { + $value = $field_args['render_row_cb'][0]->data_to_save[ $fid . '_results' ]; + } else { $value = false; } return $value; } /** - * Defines the url which is used to load local resources. Based on, and uses, + * Defines the url which is used to load local resources. Based on, and uses, * the CMB2_Utils class from the CMB2 library. */ public static function url( $path = '' ) { @@ -129,7 +133,7 @@ public static function url( $path = '' ) { /** * Use CMB2_Utils to gather the url from cmb2_fpsa_dir - */ + */ $cmb2_fpsa_url = CMB2_Utils::get_url_from_dir( $cmb2_fpsa_dir ); /** @@ -145,12 +149,16 @@ public static function url( $path = '' ) { */ public function setup_admin_scripts() { - wp_register_script( 'jquery-autocomplete', self::url( 'js/jquery.autocomplete.min.js' ), array( 'jquery' ), self::VERSION ); + wp_register_script( 'jquery-autocomplete', self::url( 'js/jquery.autocomplete.min.js' ), array( 'jquery' ), '1.4.8' ); wp_register_script( 'mag-post-search-ajax', self::url( 'js/mag-post-search-ajax.js' ), array( 'jquery', 'jquery-autocomplete', 'jquery-ui-sortable' ), self::VERSION ); - wp_localize_script( 'mag-post-search-ajax', 'psa', array( - 'ajaxurl' => admin_url( 'admin-ajax.php' ), - 'nonce' => wp_create_nonce( 'mag_cmb_post_search_ajax_get_results' ) - ) ); + wp_localize_script( + 'mag-post-search-ajax', + 'psa', + array( + 'ajaxurl' => admin_url( 'admin-ajax.php' ), + 'nonce' => wp_create_nonce( 'mag_cmb_post_search_ajax_get_results' ), + ) + ); wp_enqueue_script( 'mag-post-search-ajax' ); wp_enqueue_style( 'mag-post-search-ajax', self::url( 'css/mag-post-search-ajax.css' ), array(), self::VERSION ); @@ -163,45 +171,51 @@ public function cmb_post_search_ajax_get_results() { $nonce = $_POST['psacheck']; if ( ! wp_verify_nonce( $nonce, 'mag_cmb_post_search_ajax_get_results' ) ) { die( json_encode( array( 'error' => __( 'Error : Unauthorized action' ) ) ) ); - } - else { - $args = json_decode(stripslashes(htmlspecialchars_decode($_POST['query_args'])), true); - $args['s'] = $_POST['query']; - $datas = array(); - if( $_POST['object'] == 'user' ){ - $args['search'] = '*'.esc_attr($_POST['query']).'*'; - $users = new WP_User_Query( $args ); - $results = $users->get_results(); - if (!empty($results)) { - foreach( $results as $result ){ - $user_info = get_userdata($result->ID); + } else { + $args = json_decode( stripslashes( htmlspecialchars_decode( $_POST['query_args'] ) ), true ); + $args['s'] = $_POST['query']; + $datas = array(); + if ( 'user' === $_POST['object'] ) { + $args['search'] = '*' . esc_attr( $_POST['query'] ) . '*'; + $users = new WP_User_Query( $args ); + $results = $users->get_results(); + if ( ! empty( $results ) ) { + foreach ( $results as $result ) { + $user_info = get_userdata( $result->ID ); // Define filter "mag_cmb_post_search_ajax_result" to allow customize ajax results. - $datas[] = apply_filters( 'mag_cmb_post_search_ajax_result', array( - 'value' => $user_info->display_name, - 'data' => $result->ID, - 'guid' => get_edit_user_link($result->ID) - ) ); + $datas[] = apply_filters( + 'mag_cmb_post_search_ajax_result', + array( + 'value' => $user_info->display_name, + 'data' => $result->ID, + 'guid' => get_edit_user_link( $result->ID ), + ) + ); } } - }else{ - $results = new WP_Query( $args ); + } else { + $results = new WP_Query( $args ); if ( $results->have_posts() ) : - while ( $results->have_posts() ) : $results->the_post(); + while ( $results->have_posts() ) : + $results->the_post(); // Define filter "mag_cmb_post_search_ajax_result" to allow customize ajax results. - $datas[] = apply_filters( 'mag_cmb_post_search_ajax_result', array( - 'value' => get_the_title(), - 'data' => get_the_ID(), - 'guid' => get_edit_post_link() - ) ); + $datas[] = apply_filters( + 'mag_cmb_post_search_ajax_result', + array( + 'value' => get_the_title(), + 'data' => get_the_ID(), + 'guid' => get_edit_post_link(), + ) + ); endwhile; - endif; - } + endif; + } wp_reset_postdata(); - die( json_encode( $datas ) ); + die( json_encode( $datas ) ); } } } - + } $mag_cmb2_field_post_search_ajax = new MAG_CMB2_Field_Post_Search_Ajax(); From 685c43d333f9d358ebe7124d3796ec2117ee2ca5 Mon Sep 17 00:00:00 2001 From: Jonathan Stegall Date: Wed, 18 Sep 2019 21:13:44 -0500 Subject: [PATCH 7/8] wp code standards on the example file --- example-field-setup.php | 133 +++++++++++++++++++++------------------- 1 file changed, 71 insertions(+), 62 deletions(-) diff --git a/example-field-setup.php b/example-field-setup.php index 765d149..750b1fd 100644 --- a/example-field-setup.php +++ b/example-field-setup.php @@ -9,73 +9,82 @@ * @return array */ function cmb2_post_search_ajax_metaboxes_example() { - - $example_meta = new_cmb2_box( array( - 'id' => 'cmb2_post_search_ajax_field', - 'title' => __( 'Related Posts', 'cmb2' ), - 'object_types' => array( 'post' ), // Post type - 'context' => 'normal', - 'priority' => 'high', - 'show_names' => true, // Show field names on the left - ) ); - - $example_meta->add_field( array( - 'name' => __( 'Example Multiple', 'cmb2' ), - 'id' => 'cmb2_post_search_ajax_demo_multiple', - 'type' => 'post_search_ajax', - 'desc' => __( '(Start typing post title)', 'cmb2' ), - // Optional : - 'limit' => 10, // Limit selection to X items only (default 1) - 'sortable' => true, // Allow selected items to be sortable (default false) - 'query_args' => array( - 'post_type' => array( 'post' ), - 'post_status' => array( 'publish' ), - 'posts_per_page' => -1 + $example_meta = new_cmb2_box( + array( + 'id' => 'cmb2_post_search_ajax_field', + 'title' => __( 'Related Posts', 'cmb2' ), + 'object_types' => array( 'post' ), // Post type + 'context' => 'normal', + 'priority' => 'high', + 'show_names' => true, // Show field names on the left ) - ) ); - - $example_meta->add_field( array( - 'name' => __( 'Example Single', 'cmb2' ), - 'id' => 'cmb2_post_search_ajax_demo_single', - 'type' => 'post_search_ajax', - 'desc' => __( '(Start typing post title)', 'cmb2' ), - // Optional : - 'limit' => 1, // Limit selection to X items only (default 1) - 'sortable' => false, // Allow selected items to be sortable (default false) - 'query_args' => array( - 'post_type' => array( 'post' ), - 'post_status' => array( 'publish' ), - 'posts_per_page' => -1 + ); + + $example_meta->add_field( + array( + 'name' => __( 'Example Multiple', 'cmb2' ), + 'id' => 'cmb2_post_search_ajax_demo_multiple', + 'type' => 'post_search_ajax', + 'desc' => __( '(Start typing post title)', 'cmb2' ), + // Optional : + 'limit' => 10, // Limit selection to X items only (default 1) + 'sortable' => true, // Allow selected items to be sortable (default false) + 'query_args' => array( + 'post_type' => array( 'post' ), + 'post_status' => array( 'publish' ), + 'posts_per_page' => -1, + ), + ) + ); + + $example_meta->add_field( + array( + 'name' => __( 'Example Single', 'cmb2' ), + 'id' => 'cmb2_post_search_ajax_demo_single', + 'type' => 'post_search_ajax', + 'desc' => __( '(Start typing post title)', 'cmb2' ), + // Optional : + 'limit' => 1, // Limit selection to X items only (default 1) + 'sortable' => false, // Allow selected items to be sortable (default false) + 'query_args' => array( + 'post_type' => array( 'post' ), + 'post_status' => array( 'publish' ), + 'posts_per_page' => -1, + ), ) - ) ); + ); - $example_meta->add_field( array( - 'name' => __( 'Test user multiple', 'cmb2' ), - 'id' => 'cmb2_post_search_ajax_demo_user_multiple', - 'type' => 'post_search_ajax', - 'desc' => __( '(Start typing post title)', 'cmb2' ), - // Optional : - 'limit' => 10, // Limit selection to X items only (default 1) - 'sortable' => true, // Allow selected items to be sortable (default false) - 'object_type' => 'user', // Define queried object type (Available : post, user, term - Default : post) - 'query_args' => array( - 'blog_id' => '1', + $example_meta->add_field( + array( + 'name' => __( 'Test user multiple', 'cmb2' ), + 'id' => 'cmb2_post_search_ajax_demo_user_multiple', + 'type' => 'post_search_ajax', + 'desc' => __( '(Start typing post title)', 'cmb2' ), + // Optional : + 'limit' => 10, // Limit selection to X items only (default 1) + 'sortable' => true, // Allow selected items to be sortable (default false) + 'object_type' => 'user', // Define queried object type (Available : post, user, term - Default : post) + 'query_args' => array( + 'blog_id' => '1', + ), ) - ) ); + ); - $example_meta->add_field( array( - 'name' => __( 'Test user single', 'cmb2' ), - 'id' => 'cmb2_post_search_ajax_demo_user_single', - 'type' => 'post_search_ajax', - 'desc' => __( '(Start typing post title)', 'cmb2' ), - // Optional : - 'limit' => 1, // Limit selection to X items only (default 1) - 'sortable' => false, // Allow selected items to be sortable (default false) - 'object_type' => 'user', // Define queried object type (Available : post, user, term - Default : post) - 'query_args' => array( - 'role' => 'Administrator' + $example_meta->add_field( + array( + 'name' => __( 'Test user single', 'cmb2' ), + 'id' => 'cmb2_post_search_ajax_demo_user_single', + 'type' => 'post_search_ajax', + 'desc' => __( '(Start typing post title)', 'cmb2' ), + // Optional : + 'limit' => 1, // Limit selection to X items only (default 1) + 'sortable' => false, // Allow selected items to be sortable (default false) + 'object_type' => 'user', // Define queried object type (Available : post, user, term - Default : post) + 'query_args' => array( + 'role' => 'Administrator', + ), ) - ) ); - + ); + } add_action( 'cmb2_init', 'cmb2_post_search_ajax_metaboxes_example' ); From 89b8bc6bba4c22ae5c971b33f47d22d9829b7ede Mon Sep 17 00:00:00 2001 From: Jonathan Stegall Date: Wed, 18 Sep 2019 21:37:20 -0500 Subject: [PATCH 8/8] update version of autocomplete plugin register --- cmb-field-post-search-ajax.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmb-field-post-search-ajax.php b/cmb-field-post-search-ajax.php index 599290c..fe8fb8c 100644 --- a/cmb-field-post-search-ajax.php +++ b/cmb-field-post-search-ajax.php @@ -149,7 +149,7 @@ public static function url( $path = '' ) { */ public function setup_admin_scripts() { - wp_register_script( 'jquery-autocomplete', self::url( 'js/jquery.autocomplete.min.js' ), array( 'jquery' ), '1.4.8' ); + wp_register_script( 'jquery-autocomplete', self::url( 'js/jquery.autocomplete.min.js' ), array( 'jquery' ), '1.4.10' ); wp_register_script( 'mag-post-search-ajax', self::url( 'js/mag-post-search-ajax.js' ), array( 'jquery', 'jquery-autocomplete', 'jquery-ui-sortable' ), self::VERSION ); wp_localize_script( 'mag-post-search-ajax',