Automattic\WooCommerce\Blocks\Domain\Services\CheckoutFieldTypes
DateFieldType::process_type_options
Processes the options for a date field and returns the new field_options array.
Метод класса: DateFieldType{}
Хуков нет.
Возвращает
array|false. The updated $field_data array, or false if an error was encountered.
Использование
// protected - в коде основоного (родительского) или дочернего класса $result = $this->process_type_options( $field_data, $options );
- $field_data(массив) (обязательный)
- The field data array to be updated.
- $options(массив) (обязательный)
- The options supplied during field registration.
Код DateFieldType::process_type_options() DateFieldType::process type options WC 11.2.2
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;
}