Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#22 fix to allow limit -1 #23

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/vendor/
# generated
release/
184 changes: 99 additions & 85 deletions cmb-field-post-search-ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
/**
* Class MAG_CMB2_Field_Post_Search_Ajax
*/
if( ! class_exists( 'MAG_CMB2_Field_Post_Search_Ajax' ) ) {
if ( ! class_exists( 'MAG_CMB2_Field_Post_Search_Ajax' ) ) {

class MAG_CMB2_Field_Post_Search_Ajax {

/**
Expand All @@ -39,61 +39,66 @@ public function __construct() {
/**
* Render field
*/
public function render( $field, $value, $object_id, $object_type, $field_type ) {
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 '<ul class="cmb-post-search-ajax-results" id="' . $field_name . '_results">';
if( isset($value) && !empty($value) ){
if( !is_array($value) ){ $value = array($value); }
foreach($value as $val){
$handle = ($field->args( 'sortable' )) ? '<span class="hndl"></span>' : '';
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);
if ( isset( $value ) && ! empty( $value ) ) {
if ( ! is_array( $value ) ) {
$value = array( $value );
}
foreach ( $value as $val ) {
$handle = ( $field->args( 'sortable' ) ) ? '<span class="hndl"></span>' : '';
if ( 'user' === $field->args( 'object_type' ) ) {
$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 '<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>';
echo '</ul>';
$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 '<img src="'.admin_url( 'images/spinner.gif' ).'" class="cmb-post-search-ajax-spinner" />';
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 '<img src="' . admin_url( 'images/spinner.gif' ) . '" class="cmb-post-search-ajax-spinner">';

$field_type->_desc( true, true );

Expand All @@ -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 = '' ) {
Expand All @@ -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 );

/**
Expand All @@ -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.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', '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 );

Expand All @@ -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();
19 changes: 19 additions & 0 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading