Skip to content

Commit

Permalink
Merge pull request #13 from alexis-magina/1.1.3
Browse files Browse the repository at this point in the history
1.1.3
  • Loading branch information
alexis-magina authored Mar 27, 2017
2 parents 169c925 + e8d6b00 commit 6b24705
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 21 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,7 @@ If field limit == 1, this will return only the single attached post ID.
* Added a filter "mag_cmb_post_search_ajax_result" to allow customize ajax results
* Fixed a minor bug of encoding chars in autocomplete results

### 1.1.3
* Add Support for user queries
* Fixed issue #11 : sorting problem
* Fixed conflict with ui-autocomplete
64 changes: 48 additions & 16 deletions cmb-field-post-search-ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Plugin URI: https://github.com/alexis-magina/cmb2-field-post-search-ajax
GitHub Plugin URI: https://github.com/alexis-magina/cmb2-field-post-search-ajax
Description: CMB2 field type to attach posts to each others.
Version: 1.1.2
Version: 1.1.3
Author: Magina
Author URI: http://magina.fr/
License: GPLv2+
Expand All @@ -20,7 +20,7 @@ class MAG_CMB2_Field_Post_Search_Ajax {
/**
* Current version number
*/
const VERSION = '1.1.2';
const VERSION = '1.1.3';

/**
* The url which is used to load local resources
Expand Down Expand Up @@ -49,15 +49,29 @@ public function render( $field, $value, $object_id, $object_type, $field_type )
if( !is_array($value) ){ $value = array($value); }
foreach($value as $val){
$handle = ($field->args( 'sortable' )) ? '<span class="hndl"></span>' : '';
echo '<li>'.$handle.'<input type="hidden" name="'.$field_name.'_results[]" value="'.$val.'"><a href="'.get_edit_post_link($val).'" target="_blank" class="edit-link">'.get_the_title($val).'</a><a class="remover"><span class="dashicons dashicons-no"></span><span class="dashicons dashicons-dismiss"></span></a></li>';
if( $field->args( 'object_type' ) == 'user' ){
$guid = get_edit_user_link($val);
$user = get_userdata($val);
$title = $user->display_name;
}
else{
$guid = get_edit_post_link($val);
$title = get_the_title($val);
}
echo '<li>'.$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>';
}
}
echo '</ul>';
$field_value = '';
}
else{
if(is_array($value)){ $value = $value[0]; }
$field_value = ($value ? get_the_title($value) : '');
if(is_array($value)){ $value = $value[0]; }
if( $field->args( 'object_type' ) == 'user' ){
$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',
Expand All @@ -75,6 +89,7 @@ public function render( $field, $value, $object_id, $object_type, $field_type )
'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' ) : ''
) );

Expand Down Expand Up @@ -152,18 +167,35 @@ public function cmb_post_search_ajax_get_results() {
else {
$args = json_decode(stripslashes(htmlspecialchars_decode($_POST['query_args'])), true);
$args['s'] = $_POST['query'];
$results = new WP_Query( $args );
$datas = array();
if ( $results->have_posts() ) :
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()
) );
endwhile;
endif;
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);
// 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)
) );
}
}
}else{
$results = new WP_Query( $args );
if ( $results->have_posts() ) :
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()
) );
endwhile;
endif;
}
wp_reset_postdata();
die( json_encode( $datas ) );
}
Expand Down
30 changes: 29 additions & 1 deletion example-field-setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,34 @@ function cmb2_post_search_ajax_metaboxes_example() {
'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 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' );
add_action( 'cmb2_init', 'cmb2_post_search_ajax_metaboxes_example' );
2 changes: 1 addition & 1 deletion js/jquery.autocomplete.min.js

Large diffs are not rendered by default.

8 changes: 5 additions & 3 deletions js/mag-post-search-ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

var fid = $(this).attr('id');
var query_args = $(this).attr('data-queryargs');
var object = $(this).attr('data-object');
$(this).autocomplete({
serviceUrl: psa.ajaxurl,
type: 'POST',
Expand Down Expand Up @@ -34,6 +35,7 @@
params:{
action : 'cmb_post_search_ajax_get_results',
psacheck : psa.nonce,
object : object,
query_args : query_args,
},
onSearchStart: function(){
Expand All @@ -48,7 +50,7 @@
var limit = $(this).attr('data-limit');
var sortable = $(this).attr('data-sortable');
if( limit > 1 ){
var handle = (sortable === 1) ? '<span class="hndl"></span>' : '';
var handle = (sortable == 1) ? '<span class="hndl"></span>' : '';
$('#'+lid).append('<li>'+handle+'<input type="hidden" name="'+lid+'[]" value="'+suggestion.data+'"><a href="'+suggestion.guid+'" target="_blank" class="edit-link">'+suggestion.value+'</a><a class="remover"><span class="dashicons dashicons-no"></span><span class="dashicons dashicons-dismiss"></span></a></li>');
$(this).val('');
if( limit === $('#' + lid + ' li').length ){
Expand All @@ -64,15 +66,15 @@
}
});

if($(this).attr('data-sortable') === 1){
if($(this).attr('data-sortable') == 1){
$('#'+fid+'_results').sortable({
handle : '.hndl',
placeholder : 'ui-state-highlight',
forcePlaceholderSize : true
});
}

if($(this).attr('data-limit') === 1){
if($(this).attr('data-limit') == 1){
$(this).on('blur', function(){
if($(this).val() === ''){
var lid = $(this).attr('id') + '_results';
Expand Down

0 comments on commit 6b24705

Please sign in to comment.