fixed frontend

This commit is contained in:
Daniele Scasciafratte 2019-03-27 17:42:46 +01:00
parent ec43076819
commit 674f27a0d4
2 changed files with 21 additions and 18 deletions

View File

@ -46,19 +46,23 @@ if( ! class_exists( 'CMB2_Field_Ajax_Search' ) ) {
add_filter( 'cmb2_pre_field_display_term_ajax_search', array( $this, 'display' ), 10, 3 ); add_filter( 'cmb2_pre_field_display_term_ajax_search', array( $this, 'display' ), 10, 3 );
// Sanitize // Sanitize
add_action( 'cmb2_sanitize_post_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_user_ajax_search', array( $this, 'sanitize' ), 10, 4 );
add_action( 'cmb2_sanitize_term_ajax_search', array( $this, 'sanitize' ), 10, 4 ); //add_action( 'cmb2_sanitize_term_ajax_search', array( $this, 'sanitize' ), 10, 4 );
// Ajax request // Ajax request
add_action( 'wp_ajax_cmb_ajax_search_get_results', array( $this, 'get_results' ) ); 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 * Render field
*/ */
public function render( $field, $value, $object_id, $object_type, $field_type ) { 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; $default_limit = 1;
// Current filter is cmb2_render_{$object_to_search}_ajax_search ( post, user or term ) // Current filter is cmb2_render_{$object_to_search}_ajax_search ( post, user or term )
@ -78,9 +82,9 @@ if( ! class_exists( 'CMB2_Field_Ajax_Search' ) ) {
?> ?>
<li> <li>
<?php if( $field->args( 'sortable' ) ) : ?><span class="hndl"></span><?php endif; ?> <?php if( $field->args( 'sortable' ) ) : ?><span class="hndl"></span><?php endif; ?>
<input type="hidden" name="<?php echo $field_name; ?>[]" value="<?php echo $val; ?>"> <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"> <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 ); ?> <?php echo $this->object_text( $field->_name(), $val, $object_to_search ); ?>
</a> </a>
<a class="remover"><span class="dashicons dashicons-no"></span><span class="dashicons dashicons-dismiss"></span></a> <a class="remover"><span class="dashicons dashicons-no"></span><span class="dashicons dashicons-dismiss"></span></a>
</li> </li>
@ -108,7 +112,7 @@ if( ! class_exists( 'CMB2_Field_Ajax_Search' ) ) {
echo $field_type->input( array( echo $field_type->input( array(
'type' => 'text', 'type' => 'text',
'name' => $field_name . '_input', 'name' => $field->_name() . '_input',
'id' => $field_name . '_input', 'id' => $field_name . '_input',
'class' => 'cmb-ajax-search cmb-' . $object_to_search . '-ajax-search', 'class' => 'cmb-ajax-search cmb-' . $object_to_search . '-ajax-search',
'value' => $input_value, 'value' => $input_value,
@ -167,12 +171,10 @@ if( ! class_exists( 'CMB2_Field_Ajax_Search' ) ) {
* Optionally save the latitude/longitude values into two custom fields * Optionally save the latitude/longitude values into two custom fields
*/ */
public function sanitize( $override_value, $value, $object_id, $field_args ) { 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']]; $value = $field_args['render_row_cb'][0]->data_to_save[$field_args['id']];
} else {
$value = false;
} }
return $value; return $value;

View File

@ -4,7 +4,7 @@
$(this).attr('data-ajax-search', true); $(this).attr('data-ajax-search', true);
var input_id = $(this).attr('id'); // Field id with '_input' sufix (the searchable field) 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 object_type = $(this).attr('data-object-type');
var query_args = $(this).attr('data-query-args'); var query_args = $(this).attr('data-query-args');
@ -51,14 +51,15 @@
onSelect: function ( suggestion ) { onSelect: function ( suggestion ) {
$(this).devbridgeAutocomplete('clearCache'); $(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 multiple = $(this).attr('data-multiple');
var limit = parseInt( $(this).attr('data-limit') ); var limit = parseInt( $(this).attr('data-limit') );
var sortable = $(this).attr('data-sortable'); var sortable = $(this).attr('data-sortable');
var field_name_temp = field_name.replace( /[\[\]']+/g, '_' );
if( multiple == 1 ) { if( multiple == 1 ) {
// Multiple // Multiple
$('#' + field_name + '_results' ).append( '<li>' + $('#' + field_name_temp + '_results' ).append( '<li>' +
( ( sortable == 1 ) ? '<span class="hndl"></span>' : '' ) + ( ( sortable == 1 ) ? '<span class="hndl"></span>' : '' ) +
'<input type="hidden" name="' + field_name + '[]" value="' + suggestion.id + '">' + '<input type="hidden" name="' + field_name + '[]" value="' + suggestion.id + '">' +
'<a href="' + suggestion.link + '" target="_blank" class="edit-link">' + suggestion.value + '</a>' + '<a href="' + suggestion.link + '" target="_blank" class="edit-link">' + suggestion.value + '</a>' +
@ -68,7 +69,7 @@
$(this).val( '' ); $(this).val( '' );
// Checks if there is the max allowed results, limit < 0 means unlimited // 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' ); $(this).prop( 'disabled', 'disabled' );
} else { } else {
$(this).focus(); $(this).focus();