Skip to content

Commit

Permalink
fixed frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
Mte90 committed Mar 27, 2019
1 parent ec43076 commit 674f27a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
26 changes: 14 additions & 12 deletions cmb2-field-ajax-search.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,23 @@ public function __construct() {
add_filter( 'cmb2_pre_field_display_term_ajax_search', array( $this, 'display' ), 10, 3 );

// Sanitize
add_action( 'cmb2_sanitize_post_ajax_search', array( $this, 'sanitize' ), 10, 4 );
add_action( 'cmb2_sanitize_user_ajax_search', array( $this, 'sanitize' ), 10, 4 );
add_action( 'cmb2_sanitize_term_ajax_search', array( $this, 'sanitize' ), 10, 4 );
//add_action( 'cmb2_sanitize_post_ajax_search', array( $this, 'sanitize' ), 10, 4 );
//add_action( 'cmb2_sanitize_user_ajax_search', array( $this, 'sanitize' ), 10, 4 );
//add_action( 'cmb2_sanitize_term_ajax_search', array( $this, 'sanitize' ), 10, 4 );

// Ajax request
add_action( 'wp_ajax_cmb_ajax_search_get_results', array( $this, 'get_results' ) );
}

public function convert_as_id_css( $name ) {
return str_replace( '__', '_', str_replace( '[', '_', str_replace( ']', '_', $name ) ) );
}

/**
* Render field
*/
public function render( $field, $value, $object_id, $object_type, $field_type ) {
$field_name = $field->_name();
$field_name = $this->convert_as_id_css($field->_name());
$default_limit = 1;

// Current filter is cmb2_render_{$object_to_search}_ajax_search ( post, user or term )
Expand All @@ -78,9 +82,9 @@ public function render( $field, $value, $object_id, $object_type, $field_type )
?>
<li>
<?php if( $field->args( 'sortable' ) ) : ?><span class="hndl"></span><?php endif; ?>
<input type="hidden" name="<?php echo $field_name; ?>[]" value="<?php echo $val; ?>">
<a href="<?php echo $this->object_link( $field_name, $val, $object_to_search ); ?>" target="_blank" class="edit-link">
<?php echo $this->object_text( $field_name, $val, $object_to_search ); ?>
<input type="hidden" name="<?php echo $field->_name(); ?>[]" value="<?php echo $val; ?>">
<a href="<?php echo $this->object_link( $field->_name(), $val, $object_to_search ); ?>" target="_blank" class="edit-link">
<?php echo $this->object_text( $field->_name(), $val, $object_to_search ); ?>
</a>
<a class="remover"><span class="dashicons dashicons-no"></span><span class="dashicons dashicons-dismiss"></span></a>
</li>
Expand Down Expand Up @@ -108,7 +112,7 @@ public function render( $field, $value, $object_id, $object_type, $field_type )

echo $field_type->input( array(
'type' => 'text',
'name' => $field_name . '_input',
'name' => $field->_name() . '_input',
'id' => $field_name . '_input',
'class' => 'cmb-ajax-search cmb-' . $object_to_search . '-ajax-search',
'value' => $input_value,
Expand Down Expand Up @@ -167,12 +171,10 @@ public function display( $pre_output, $field, $display ) {
* Optionally save the latitude/longitude values into two custom fields
*/
public function sanitize( $override_value, $value, $object_id, $field_args ) {
$fid = $field_args['id'];
$value = false;

if($field_args['render_row_cb'][0]->data_to_save[$field_args['id']]) {
if( isset( $field_args['render_row_cb'][0]->data_to_save[$field_args['id']] ) ) {
$value = $field_args['render_row_cb'][0]->data_to_save[$field_args['id']];
} else {
$value = false;
}

return $value;
Expand Down
13 changes: 7 additions & 6 deletions js/ajax-search.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
$(this).attr('data-ajax-search', true);

var input_id = $(this).attr('id'); // Field id with '_input' sufix (the searchable field)
var field_id = $(this).attr('id').replace( new RegExp('_input$'), '' ); // Field id, the true one field
var field_id = $(this).attr('id').replace( new RegExp('_input$'), '' ).replace(/[\[\]']+/g, '_'); // Field id, the true one field
var object_type = $(this).attr('data-object-type');
var query_args = $(this).attr('data-query-args');

Expand Down Expand Up @@ -50,15 +50,16 @@
},
onSelect: function ( suggestion ) {
$(this).devbridgeAutocomplete('clearCache');

var field_name = $(this).attr('id').replace( new RegExp('_input$'), '' );
var field_name = $(this).attr('name').replace( new RegExp('_input$'), '' );
var multiple = $(this).attr('data-multiple');
var limit = parseInt( $(this).attr('data-limit') );
var sortable = $(this).attr('data-sortable');

var field_name_temp = field_name.replace( /[\[\]']+/g, '_' );

if( multiple == 1 ) {
// Multiple
$('#' + field_name + '_results' ).append( '<li>' +
$('#' + field_name_temp + '_results' ).append( '<li>' +
( ( sortable == 1 ) ? '<span class="hndl"></span>' : '' ) +
'<input type="hidden" name="' + field_name + '[]" value="' + suggestion.id + '">' +
'<a href="' + suggestion.link + '" target="_blank" class="edit-link">' + suggestion.value + '</a>' +
Expand All @@ -68,7 +69,7 @@
$(this).val( '' );

// Checks if there is the max allowed results, limit < 0 means unlimited
if( limit > 0 && limit == $('#' + field_name + '_results li').length ) {
if( limit > 0 && limit == $('#' + field_name_temp + '_results li').length ) {
$(this).prop( 'disabled', 'disabled' );
} else {
$(this).focus();
Expand Down

0 comments on commit 674f27a

Please sign in to comment.