Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 39 additions & 2 deletions wds-cmb2-date-range-field.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public function hooks() {
add_action( 'init', array( $this, 'init' ) );
add_action( 'cmb2_render_date_range', array( $this, 'render' ), 10, 5 );
add_filter( 'cmb2_sanitize_date_range', array( $this, 'sanitize' ), 10, 2 );
add_action( 'cmb2_save_field', array( $this, 'save_split_date_range' ), 10, 4 );
}

/**
Expand Down Expand Up @@ -168,8 +169,6 @@ function render( $field, $escaped_value, $field_object_id, $field_object_type, $
$field_type->_id(),
$a['desc']
);
?>
<?php
}

/**
Expand All @@ -192,6 +191,44 @@ function sanitize( $override_value, $value ) {
return $value;

}

/**
* Save the start date and end date into separate fields if the argugment split_date_range is true
*
* @param string $field_id The current field id paramater.
* @param bool $updated Whether the metadata update action occurred.
* @param string $action Action performed. Could be "repeatable", "updated", or "removed".
* @param CMB2_Field object $field This field object
*/
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'] );
}
}
}

/**
Expand Down