Automattic\WooCommerce\Blocks\Domain\Services\CheckoutFieldTypes
DateFieldType::process_type_options
Processes the options for a date field and returns the new field_options array.
Method of the class: DateFieldType{}
No Hooks.
Returns
array|false. The updated $field_data array, or false if an error was encountered.
Usage
// protected - for code of main (parent) or child class $result = $this->process_type_options( $field_data, $options );
- $field_data(array) (required)
- The field data array to be updated.
- $options(array) (required)
- The options supplied during field registration.
DateFieldType::process_type_options() DateFieldType::process type options code WC 11.3.0
protected function process_type_options( array $field_data, array $options ) {
$id = $options['id'];
foreach ( array( 'min', 'max' ) as $constraint ) {
// Both are optional. Drop the key entirely rather than carrying a null through to the client.
if ( ! isset( $options[ $constraint ] ) ) {
unset( $field_data[ $constraint ] );
continue;
}
$value = $options[ $constraint ];
if ( ! is_string( $value ) ) {
return $this->registration_error(
$id,
sprintf( 'The "%s" property of a "date" field must be a date in YYYY-MM-DD format or an ISO 8601-2 duration such as "P1D" or "-P18Y".', $constraint ),
'11.2.0'
);
}
$value = trim( $value );
if ( $this->is_absolute_date( $value ) ) {
if ( null === $this->parse_date( $value ) ) {
return $this->registration_error(
$id,
sprintf( 'The "%s" property of a "date" field must be a real calendar date, and "%s" is not.', $constraint, $value ),
'11.2.0'
);
}
$field_data[ $constraint ] = $value;
continue;
}
// DateInterval implements ISO 8601-1, which has no sign, so the ISO 8601-2 sign is peeled off first.
$sign = 1;
$body = $value;
if ( '' !== $body && in_array( $body[0], array( '+', '-' ), true ) ) {
$sign = '-' === $body[0] ? -1 : 1;
$body = substr( $body, 1 );
}
try {
$interval = self::parse_duration( $body );
} catch ( \Exception $e ) {
return $this->registration_error(
$id,
sprintf( 'The "%s" property of a "date" field must be a date in YYYY-MM-DD format or an ISO 8601-2 duration such as "P1D" or "-P18Y", and "%s" is neither.', $constraint, $value ),
'11.2.0'
);
}
// A date field has no sub-day precision, so a time component means the caller meant something else.
if ( $interval->h || $interval->i || $interval->s || $interval->f ) {
return $this->registration_error(
$id,
sprintf( 'The "%s" property of a "date" field must be a duration in whole days, but "%s" includes a time component.', $constraint, $value ),
'11.2.0'
);
}
// Keep the duration unresolved and drop an explicit plus sign.
$field_data[ $constraint ] = ( -1 === $sign ? '-' : '' ) . $body;
}
return $field_data;
}