Merge pull request #14 from Mte90/repeatable

Repeatable
This commit is contained in:
rubengc 2020-07-06 11:11:23 +02:00 committed by GitHub
commit d6ce6047ff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 45 additions and 30 deletions

View File

@ -27,7 +27,7 @@ if( ! class_exists( 'CMB2_Field_Ajax_Search' ) ) {
/** /**
* Current version number * Current version number
*/ */
const VERSION = '1.0.2'; const VERSION = '1.0.3';
/** /**
* Initialize the plugin by hooking into CMB2 * Initialize the plugin by hooking into CMB2
@ -46,7 +46,7 @@ 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 );
@ -54,17 +54,25 @@ if( ! class_exists( 'CMB2_Field_Ajax_Search' ) ) {
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 )
$object_to_search = str_replace( 'cmb2_render_', '', str_replace( '_ajax_search', '', current_filter() ) ); $object_to_search = str_replace( 'cmb2_render_', '', str_replace( '_ajax_search', '', current_filter() ) );
if( $field->args( 'multiple' ) == true ) { if( ! is_array( $value ) && strpos( $value, ', ' ) ) {
$value = explode(', ', $value);
}
if( $field->args( 'multiple-item' ) == true ) {
$default_limit = -1; // 0 or -1 means unlimited $default_limit = -1; // 0 or -1 means unlimited
?><ul id="<?php echo $field_name; ?>_results" class="cmb-ajax-search-results cmb-<?php echo $object_to_search; ?>-ajax-search-results"><?php ?><ul id="<?php echo $field_name; ?>_results" class="cmb-ajax-search-results cmb-<?php echo $object_to_search; ?>-ajax-search-results"><?php
@ -78,9 +86,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>
@ -98,7 +106,7 @@ if( ! class_exists( 'CMB2_Field_Ajax_Search' ) ) {
echo $field_type->input( array( echo $field_type->input( array(
'type' => 'hidden', 'type' => 'hidden',
'name' => $field_name, 'name' => $field->_name(),
'value' => $value, 'value' => $value,
'desc' => false 'desc' => false
) ); ) );
@ -108,12 +116,12 @@ 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(),
'id' => $field_name . '_input', 'id' => $field_name,
'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,
'desc' => false, 'desc' => false,
'data-multiple' => $field->args( 'multiple' ) ? $field->args( 'multiple' ) : '0', 'data-multiple' => $field->args( 'multiple-item' ) ? $field->args( 'multiple-item' ) : '0',
'data-limit' => $field->args( 'limit' ) ? $field->args( 'limit' ) : $default_limit, 'data-limit' => $field->args( 'limit' ) ? $field->args( 'limit' ) : $default_limit,
'data-sortable' => $field->args( 'sortable' ) ? $field->args( 'sortable' ) : '0', 'data-sortable' => $field->args( 'sortable' ) ? $field->args( 'sortable' ) : '0',
'data-object-type' => $object_to_search, 'data-object-type' => $object_to_search,
@ -167,15 +175,16 @@ 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']; if ( !is_array( $value ) || !( array_key_exists('repeatable', $field_args ) && $field_args['repeatable'] == TRUE ) ) {
return $override_value;
}
if($field_args['render_row_cb'][0]->data_to_save[$field_args['id']]) { $new_values = array();
$value = $field_args['render_row_cb'][0]->data_to_save[$field_args['id']]; foreach ( $value as $key => $val ) {
} else { $new_values[$key] = array_filter( array_map( 'sanitize_text_field', $val ) );
$value = false; }
}
return $value; return array_filter( array_values( $new_values ) );
} }
/** /**

View File

@ -3,8 +3,7 @@
$('.cmb-ajax-search:not([data-ajax-search="true"])').each(function () { $('.cmb-ajax-search:not([data-ajax-search="true"])').each(function () {
$(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 field_id = $(this).attr('id').replace(/[\[\]']+/g, '_'); // Field id, the true one field
var field_id = $(this).attr('id').replace( new RegExp('_input$'), '' ); // 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,16 +50,17 @@
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');
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.substring(1).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.substring(1) + '[]" 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>' +
'<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>' );
@ -68,14 +68,14 @@
$(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();
} }
} else { } else {
// Singular // Singular
$('input[name=' + field_name + ']').val( suggestion.id ).change(); $('input[name="' + field_name + '"]').val( suggestion.id ).change();
} }
} }
}, },

View File

@ -62,7 +62,7 @@ function cmb2_ajax_search_metabox() {
'desc' => __( 'Field description (optional)', 'cmb2' ), 'desc' => __( 'Field description (optional)', 'cmb2' ),
'id' => $prefix . 'posts', 'id' => $prefix . 'posts',
'type' => 'post_ajax_search', 'type' => 'post_ajax_search',
'multiple' => true, 'multiple-item' => true,
'limit' => 10, 'limit' => 10,
'query_args' => array( 'query_args' => array(
'post_type' => array( 'post', 'page' ), 'post_type' => array( 'post', 'page' ),
@ -88,7 +88,7 @@ function cmb2_ajax_search_metabox() {
'desc' => __( 'Field description (optional)', 'cmb2' ), 'desc' => __( 'Field description (optional)', 'cmb2' ),
'id' => $prefix . 'users', 'id' => $prefix . 'users',
'type' => 'user_ajax_search', 'type' => 'user_ajax_search',
'multiple' => true, 'multiple-item' => true,
'limit' => 5, 'limit' => 5,
'query_args' => array( 'query_args' => array(
'role__not_in' => array( 'Administrator', 'Editor' ), 'role__not_in' => array( 'Administrator', 'Editor' ),
@ -113,7 +113,7 @@ function cmb2_ajax_search_metabox() {
'desc' => __( 'Field description (optional)', 'cmb2' ), 'desc' => __( 'Field description (optional)', 'cmb2' ),
'id' => $prefix . 'terms', 'id' => $prefix . 'terms',
'type' => 'term_ajax_search', 'type' => 'term_ajax_search',
'multiple' => true, 'multiple-item' => true,
'limit' => -1, 'limit' => -1,
'query_args' => array( 'query_args' => array(
'taxonomy' => 'post_tag', 'taxonomy' => 'post_tag',
@ -156,6 +156,12 @@ If multiple == true will return an array of IDs of attached object:
## Changelog ## Changelog
### 1.0.3
* Fixed issues with repeatable fields
* Removed unused code
* Moved to new paramenter `multiple-item` to avoid conflicts with CMB2
### 1.0.2 ### 1.0.2
* Updated devbridgeAutocomplete lib * Updated devbridgeAutocomplete lib