Added the possibility to split date range values
This will allow to query more effectively between dates.
Props to @rasmustaarnby #9
Can you review this @jtsternberg please?
Some notes:
- Should the
split_date_rangearg be set to true as default? - Should be removed the default meta_key and meta_value in DB which is in json when the
split_date_rangeis set to true?
Let me know your thoughts.
I can merge when @jtsternberg is approving with what he sees for this.
Thank you @jtsternberg for the quick review and comments. When possible can you give it another review? Should be ready to ship with this last commits @tw2113
Again thank you both for the quick reply
This isn't a repo I have access to, but here's a quick review.
- Should the split_date_range arg be set to true as default?
- Should be removed the default meta_key and meta_value in DB which is in json when the split_date_range is set to true?
- no, because backwards-compatibility should be maintained
- Probably not a bad idea
Unfortunately I can't remove the 2. as the daterangepicker js is using it to populate the field value on load.
@jtsternberg this is not correct, can you please let me know how to accomplish the class call to CMB2_Fiel
Any updates on this please?
@RubenMartins try this:
function save_split_date_range( $field_id, $updated, $action, $cmb2_field ) {
if ( ! $updated || 'repeatable' === $action || ! $cmb2_field->args( 'split_date_range' ) ) {
return;
}
$start_field = $cmb2_field->get_field_clone( array(
'id' => $field_id . '_start',
) );
$end_field = $cmb2_field->get_field_clone( array(
'id' => $field_id . '_end',
) );
if ( $action === 'removed' ) {
$start_field->remove_data();
$end_field->remove_data();
return;
}
$value = json_decode( $cmb2_field->value, true );
if ( is_array( $value ) ) {
$value = array_map( 'sanitize_text_field', $value );
$start_field->update_data( $value['start'] );
$end_field->update_data( $value['end'] );
}
}
@RubenMartins try this:
function save_split_date_range( $field_id, $updated, $action, $cmb2_field ) { if ( ! $updated || 'repeatable' === $action || ! $cmb2_field->args( 'split_date_range' ) ) { return; } $start_field = $cmb2_field->get_field_clone( array( 'id' => $field_id . '_start', ) ); $end_field = $cmb2_field->get_field_clone( array( 'id' => $field_id . '_end', ) ); if ( $action === 'removed' ) { $start_field->remove_data(); $end_field->remove_data(); return; } $value = json_decode( $cmb2_field->value, true ); if ( is_array( $value ) ) { $value = array_map( 'sanitize_text_field', $value ); $start_field->update_data( $value['start'] ); $end_field->update_data( $value['end'] ); } }
Thank you @jtsternberg this is way better. When possible please review the pr again.