Skip to content

Added the possibility to split date range values#17

Open
RubenMartins wants to merge 6 commits into
WebDevStudios:masterfrom
RubenMartins:patch-1
Open

Added the possibility to split date range values#17
RubenMartins wants to merge 6 commits into
WebDevStudios:masterfrom
RubenMartins:patch-1

Conversation

@RubenMartins
Copy link
Copy Markdown

This will allow to query more effectively between dates.

Props to @rasmustaarnby #9

Can you review this @jtsternberg please?

This will allow to query more effectively between dates.
Props to @rasmustaarnby
@RubenMartins
Copy link
Copy Markdown
Author

Some notes:

  1. Should the split_date_range arg be set to true as default?
  2. 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?

Let me know your thoughts.

Copy link
Copy Markdown
Contributor

@jtsternberg jtsternberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't a repo I have access to, but here's a quick review.

  1. Should the split_date_range arg be set to true as default?
  2. 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?
  1. no, because backwards-compatibility should be maintained
  2. Probably not a bad idea

Comment thread wds-cmb2-date-range-field.php Outdated
Comment thread wds-cmb2-date-range-field.php Outdated
Comment thread wds-cmb2-date-range-field.php Outdated
@tw2113
Copy link
Copy Markdown
Member

tw2113 commented Apr 22, 2021

I can merge when @jtsternberg is approving with what he sees for this.

RubenMartins and others added 3 commits April 22, 2021 18:28
Co-authored-by: Justin Sternberg <me@jtsternberg.com>
Co-authored-by: Justin Sternberg <me@jtsternberg.com>
@RubenMartins
Copy link
Copy Markdown
Author

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

@RubenMartins
Copy link
Copy Markdown
Author

This isn't a repo I have access to, but here's a quick review.

  1. Should the split_date_range arg be set to true as default?
  2. 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?
  1. no, because backwards-compatibility should be maintained
  2. 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.

Copy link
Copy Markdown
Author

@RubenMartins RubenMartins left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jtsternberg this is not correct, can you please let me know how to accomplish the class call to CMB2_Field?

@RubenMartins
Copy link
Copy Markdown
Author

@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?

@jtsternberg
Copy link
Copy Markdown
Contributor

@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'] );
		}
	}

Replace the use of direct update_meta to set functions from cmb2 update_data() and remove_data()
@RubenMartins
Copy link
Copy Markdown
Author

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants