Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/woocomme.../src/Blocks/Domain/Services
File: CheckoutFields.php
<?php
[0] Fix | Delete
declare( strict_types = 1);
[1] Fix | Delete
[2] Fix | Delete
namespace Automattic\WooCommerce\Blocks\Domain\Services;
[3] Fix | Delete
[4] Fix | Delete
use Automattic\WooCommerce\Blocks\Utils\CartCheckoutUtils;
[5] Fix | Delete
use Automattic\WooCommerce\Blocks\Assets\AssetDataRegistry;
[6] Fix | Delete
use Automattic\WooCommerce\Blocks\Domain\Services\CheckoutFieldsSchema\{
[7] Fix | Delete
DocumentObject, Validation
[8] Fix | Delete
};
[9] Fix | Delete
use WC_Customer;
[10] Fix | Delete
use WC_Data;
[11] Fix | Delete
use WC_Order;
[12] Fix | Delete
use WP_Error;
[13] Fix | Delete
[14] Fix | Delete
/**
[15] Fix | Delete
* Service class managing checkout fields and its related extensibility points.
[16] Fix | Delete
*/
[17] Fix | Delete
class CheckoutFields {
[18] Fix | Delete
[19] Fix | Delete
/**
[20] Fix | Delete
* Additional checkout fields.
[21] Fix | Delete
*
[22] Fix | Delete
* @var array
[23] Fix | Delete
*/
[24] Fix | Delete
private $additional_fields = [];
[25] Fix | Delete
[26] Fix | Delete
/**
[27] Fix | Delete
* Fields locations.
[28] Fix | Delete
*
[29] Fix | Delete
* @var array
[30] Fix | Delete
*/
[31] Fix | Delete
private $fields_locations;
[32] Fix | Delete
[33] Fix | Delete
/**
[34] Fix | Delete
* Supported field types
[35] Fix | Delete
*
[36] Fix | Delete
* @var array
[37] Fix | Delete
*/
[38] Fix | Delete
private $supported_field_types = [ 'text', 'select', 'checkbox' ];
[39] Fix | Delete
[40] Fix | Delete
/**
[41] Fix | Delete
* Groups of fields to be saved.
[42] Fix | Delete
*
[43] Fix | Delete
* @var array
[44] Fix | Delete
*/
[45] Fix | Delete
private $groups = [ 'billing', 'shipping', 'other' ];
[46] Fix | Delete
[47] Fix | Delete
/**
[48] Fix | Delete
* Instance of the asset data registry.
[49] Fix | Delete
*
[50] Fix | Delete
* @var AssetDataRegistry
[51] Fix | Delete
*/
[52] Fix | Delete
private $asset_data_registry;
[53] Fix | Delete
[54] Fix | Delete
/**
[55] Fix | Delete
* Billing fields meta key.
[56] Fix | Delete
*
[57] Fix | Delete
* @var string
[58] Fix | Delete
*/
[59] Fix | Delete
const BILLING_FIELDS_PREFIX = '_wc_billing/';
[60] Fix | Delete
[61] Fix | Delete
/**
[62] Fix | Delete
* Shipping fields meta key.
[63] Fix | Delete
*
[64] Fix | Delete
* @var string
[65] Fix | Delete
*/
[66] Fix | Delete
const SHIPPING_FIELDS_PREFIX = '_wc_shipping/';
[67] Fix | Delete
[68] Fix | Delete
/**
[69] Fix | Delete
* Additional fields meta key.
[70] Fix | Delete
*
[71] Fix | Delete
* @var string
[72] Fix | Delete
* @deprecated 8.9.0 Use OTHER_FIELDS_PREFIX instead.
[73] Fix | Delete
*/
[74] Fix | Delete
const ADDITIONAL_FIELDS_PREFIX = '_wc_additional/';
[75] Fix | Delete
[76] Fix | Delete
/**
[77] Fix | Delete
* Other fields meta key.
[78] Fix | Delete
*
[79] Fix | Delete
* @var string
[80] Fix | Delete
*/
[81] Fix | Delete
const OTHER_FIELDS_PREFIX = '_wc_other/';
[82] Fix | Delete
[83] Fix | Delete
/**
[84] Fix | Delete
* Sets up core fields.
[85] Fix | Delete
*
[86] Fix | Delete
* @param AssetDataRegistry $asset_data_registry Instance of the asset data registry.
[87] Fix | Delete
*/
[88] Fix | Delete
public function __construct( AssetDataRegistry $asset_data_registry ) {
[89] Fix | Delete
$this->asset_data_registry = $asset_data_registry;
[90] Fix | Delete
$this->fields_locations = [
[91] Fix | Delete
// omit email from shipping and billing fields.
[92] Fix | Delete
'address' => array_merge( \array_diff_key( $this->get_core_fields_keys(), array( 'email' ) ) ),
[93] Fix | Delete
'contact' => array( 'email' ),
[94] Fix | Delete
'order' => [],
[95] Fix | Delete
];
[96] Fix | Delete
}
[97] Fix | Delete
[98] Fix | Delete
/**
[99] Fix | Delete
* Initialize hooks.
[100] Fix | Delete
*/
[101] Fix | Delete
public function init() {
[102] Fix | Delete
add_filter( 'woocommerce_get_country_locale_default', array( $this, 'update_default_locale_with_fields' ) );
[103] Fix | Delete
add_action( 'woocommerce_blocks_checkout_enqueue_data', array( $this, 'add_fields_data' ) );
[104] Fix | Delete
add_action( 'woocommerce_blocks_cart_enqueue_data', array( $this, 'add_fields_data' ) );
[105] Fix | Delete
add_filter( 'woocommerce_customer_allowed_session_meta_keys', array( $this, 'add_session_meta_keys' ) );
[106] Fix | Delete
}
[107] Fix | Delete
[108] Fix | Delete
/**
[109] Fix | Delete
* Add fields data to the asset data registry.
[110] Fix | Delete
*/
[111] Fix | Delete
public function add_fields_data() {
[112] Fix | Delete
$this->asset_data_registry->add( 'defaultFields', array_merge( $this->get_core_fields(), $this->get_additional_fields() ) );
[113] Fix | Delete
$this->asset_data_registry->add( 'addressFieldsLocations', $this->fields_locations );
[114] Fix | Delete
}
[115] Fix | Delete
[116] Fix | Delete
/**
[117] Fix | Delete
* Add session meta keys.
[118] Fix | Delete
*
[119] Fix | Delete
* This is an allow-list of meta data keys which we want to store in session.
[120] Fix | Delete
*
[121] Fix | Delete
* @param array $keys Session meta keys.
[122] Fix | Delete
* @return array
[123] Fix | Delete
*/
[124] Fix | Delete
public function add_session_meta_keys( $keys ) {
[125] Fix | Delete
$meta_keys = array();
[126] Fix | Delete
try {
[127] Fix | Delete
foreach ( $this->get_additional_fields() as $field_key => $field ) {
[128] Fix | Delete
if ( 'address' === $field['location'] ) {
[129] Fix | Delete
$meta_keys[] = self::BILLING_FIELDS_PREFIX . $field_key;
[130] Fix | Delete
$meta_keys[] = self::SHIPPING_FIELDS_PREFIX . $field_key;
[131] Fix | Delete
} else {
[132] Fix | Delete
$meta_keys[] = self::OTHER_FIELDS_PREFIX . $field_key;
[133] Fix | Delete
}
[134] Fix | Delete
}
[135] Fix | Delete
} catch ( \Throwable $e ) {
[136] Fix | Delete
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error
[137] Fix | Delete
trigger_error(
[138] Fix | Delete
sprintf(
[139] Fix | Delete
'Error adding session meta keys for checkout fields. %s',
[140] Fix | Delete
esc_attr( $e->getMessage() )
[141] Fix | Delete
),
[142] Fix | Delete
E_USER_WARNING
[143] Fix | Delete
);
[144] Fix | Delete
[145] Fix | Delete
return $keys;
[146] Fix | Delete
}
[147] Fix | Delete
[148] Fix | Delete
return array_merge( $keys, $meta_keys );
[149] Fix | Delete
}
[150] Fix | Delete
[151] Fix | Delete
/**
[152] Fix | Delete
* If a field does not declare a sanitization callback, this is the default sanitization callback.
[153] Fix | Delete
*
[154] Fix | Delete
* @param mixed $value Value to sanitize.
[155] Fix | Delete
* @param array $field Field data.
[156] Fix | Delete
* @return mixed
[157] Fix | Delete
*/
[158] Fix | Delete
public function default_sanitize_callback( $value, $field ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed
[159] Fix | Delete
return $value;
[160] Fix | Delete
}
[161] Fix | Delete
[162] Fix | Delete
/**
[163] Fix | Delete
* If a field does not declare a validation callback, this is the default validation callback.
[164] Fix | Delete
*
[165] Fix | Delete
* @param mixed $value Value to sanitize.
[166] Fix | Delete
* @param array $field Field data.
[167] Fix | Delete
* @return WP_Error|void If there is a validation error, return an WP_Error object.
[168] Fix | Delete
*/
[169] Fix | Delete
public function default_validate_callback( $value, $field ) {
[170] Fix | Delete
if ( true === $field['required'] && empty( $value ) ) {
[171] Fix | Delete
return new WP_Error(
[172] Fix | Delete
'woocommerce_required_checkout_field',
[173] Fix | Delete
sprintf(
[174] Fix | Delete
// translators: %s is field key.
[175] Fix | Delete
__( 'The field %s is required.', 'woocommerce' ),
[176] Fix | Delete
$field['id']
[177] Fix | Delete
)
[178] Fix | Delete
);
[179] Fix | Delete
}
[180] Fix | Delete
}
[181] Fix | Delete
[182] Fix | Delete
/**
[183] Fix | Delete
* Registers an additional field for Checkout.
[184] Fix | Delete
*
[185] Fix | Delete
* @param array $options The field options.
[186] Fix | Delete
*
[187] Fix | Delete
* @return WP_Error|void True if the field was registered, a WP_Error otherwise.
[188] Fix | Delete
*/
[189] Fix | Delete
public function register_checkout_field( $options ) {
[190] Fix | Delete
// Check the options and show warnings if they're not supplied. Return early if an error that would prevent registration is encountered.
[191] Fix | Delete
if ( false === $this->validate_options( $options ) ) {
[192] Fix | Delete
return;
[193] Fix | Delete
}
[194] Fix | Delete
[195] Fix | Delete
// The above validate_options function ensures these options are valid. Type might not be supplied but then it defaults to text.
[196] Fix | Delete
$field_data = wp_parse_args(
[197] Fix | Delete
$options,
[198] Fix | Delete
[
[199] Fix | Delete
'id' => '',
[200] Fix | Delete
'label' => '',
[201] Fix | Delete
/* translators: %s Field label. */
[202] Fix | Delete
'optionalLabel' => sprintf( __( '%s (optional)', 'woocommerce' ), $options['label'] ),
[203] Fix | Delete
'location' => '',
[204] Fix | Delete
'type' => 'text',
[205] Fix | Delete
'hidden' => false,
[206] Fix | Delete
'required' => false,
[207] Fix | Delete
'attributes' => [],
[208] Fix | Delete
'show_in_order_confirmation' => true,
[209] Fix | Delete
'sanitize_callback' => array( $this, 'default_sanitize_callback' ),
[210] Fix | Delete
'validate_callback' => array( $this, 'default_validate_callback' ),
[211] Fix | Delete
'validation' => [],
[212] Fix | Delete
],
[213] Fix | Delete
);
[214] Fix | Delete
[215] Fix | Delete
$field_data['attributes'] = $this->register_field_attributes( $field_data['id'], $field_data['attributes'] );
[216] Fix | Delete
$field_data = $this->process_field_options( $field_data, $options );
[217] Fix | Delete
[218] Fix | Delete
// $field_data will be false if an error that will prevent the field being registered is encountered.
[219] Fix | Delete
if ( false === $field_data ) {
[220] Fix | Delete
return;
[221] Fix | Delete
}
[222] Fix | Delete
[223] Fix | Delete
// Insert new field into the correct location array.
[224] Fix | Delete
$this->additional_fields[ $field_data['id'] ] = $field_data;
[225] Fix | Delete
$this->fields_locations[ $field_data['location'] ][] = $field_data['id'];
[226] Fix | Delete
}
[227] Fix | Delete
[228] Fix | Delete
/**
[229] Fix | Delete
* Returns true if the field is required. Takes rules into consideration if a document object is provided.
[230] Fix | Delete
*
[231] Fix | Delete
* @param array|string $field The field array or field key.
[232] Fix | Delete
* @param DocumentObject|null $document_object The document object.
[233] Fix | Delete
* @return bool
[234] Fix | Delete
*/
[235] Fix | Delete
public function is_required_field( $field, $document_object = null ) {
[236] Fix | Delete
if ( is_string( $field ) ) {
[237] Fix | Delete
$field = $this->additional_fields[ $field ] ?? [];
[238] Fix | Delete
}
[239] Fix | Delete
[240] Fix | Delete
if ( empty( $field ) ) {
[241] Fix | Delete
return false;
[242] Fix | Delete
}
[243] Fix | Delete
[244] Fix | Delete
if ( $document_object ) {
[245] Fix | Delete
// Hidden fields cannot be required.
[246] Fix | Delete
if ( $this->is_hidden_field( $field, $document_object ) ) {
[247] Fix | Delete
return false;
[248] Fix | Delete
}
[249] Fix | Delete
if ( $this->contains_valid_rules( $field['required'] ) ) {
[250] Fix | Delete
return true === Validation::validate_document_object( $document_object, $field['required'] );
[251] Fix | Delete
}
[252] Fix | Delete
}
[253] Fix | Delete
return true === $field['required'];
[254] Fix | Delete
}
[255] Fix | Delete
[256] Fix | Delete
/**
[257] Fix | Delete
* Returns true if the field is hidden. Takes rules into consideration if a document object is provided.
[258] Fix | Delete
*
[259] Fix | Delete
* @param array|string $field The field array or field key.
[260] Fix | Delete
* @param DocumentObject|null $document_object The document object.
[261] Fix | Delete
* @return bool
[262] Fix | Delete
*/
[263] Fix | Delete
public function is_hidden_field( $field, $document_object = null ) {
[264] Fix | Delete
if ( is_string( $field ) ) {
[265] Fix | Delete
$field = $this->additional_fields[ $field ] ?? [];
[266] Fix | Delete
}
[267] Fix | Delete
if ( $document_object && $this->contains_valid_rules( $field['hidden'] ) ) {
[268] Fix | Delete
return true === Validation::validate_document_object( $document_object, $field['hidden'] );
[269] Fix | Delete
}
[270] Fix | Delete
return false; // Fields cannot be registered as hidden.
[271] Fix | Delete
}
[272] Fix | Delete
[273] Fix | Delete
/**
[274] Fix | Delete
* Returns true if the field is conditionally required or rendered.
[275] Fix | Delete
*
[276] Fix | Delete
* @param array|string $field The field array or field key.
[277] Fix | Delete
* @return bool
[278] Fix | Delete
*/
[279] Fix | Delete
public function is_conditional_field( $field ) {
[280] Fix | Delete
if ( is_string( $field ) ) {
[281] Fix | Delete
$field = $this->additional_fields[ $field ] ?? [];
[282] Fix | Delete
}
[283] Fix | Delete
return $this->contains_valid_rules( $field['required'] ) || $this->contains_valid_rules( $field['hidden'] );
[284] Fix | Delete
}
[285] Fix | Delete
[286] Fix | Delete
/**
[287] Fix | Delete
* Validates a field against the given document object and context.
[288] Fix | Delete
*
[289] Fix | Delete
* @param array $field The field.
[290] Fix | Delete
* @param DocumentObject|null $document_object The document object.
[291] Fix | Delete
* @return bool|\WP_Error True if the field is valid, a WP_Error otherwise.
[292] Fix | Delete
*/
[293] Fix | Delete
public function is_valid_field( $field, $document_object = null ) {
[294] Fix | Delete
if ( $document_object && $this->contains_valid_rules( $field['validation'] ) ) {
[295] Fix | Delete
$field_schema = Validation::get_field_schema_with_context( $field['id'], $field['validation'], $document_object->get_context() );
[296] Fix | Delete
return Validation::validate_document_object( $document_object, $field_schema );
[297] Fix | Delete
}
[298] Fix | Delete
return true;
[299] Fix | Delete
}
[300] Fix | Delete
[301] Fix | Delete
/**
[302] Fix | Delete
* Returns true if the property is an array and not empty.
[303] Fix | Delete
*
[304] Fix | Delete
* @param mixed $property The property to check.
[305] Fix | Delete
* @return bool
[306] Fix | Delete
*/
[307] Fix | Delete
protected function contains_valid_rules( $property ) {
[308] Fix | Delete
return is_array( $property ) && ! empty( $property );
[309] Fix | Delete
}
[310] Fix | Delete
[311] Fix | Delete
/**
[312] Fix | Delete
* Returns the validate callback for a given field.
[313] Fix | Delete
*
[314] Fix | Delete
* @param array $field The field.
[315] Fix | Delete
* @param DocumentObject|null $document_object The document object.
[316] Fix | Delete
* @return callable The validate callback.
[317] Fix | Delete
*/
[318] Fix | Delete
public function get_validate_callback( $field, $document_object = null ) {
[319] Fix | Delete
if ( is_string( $field ) ) {
[320] Fix | Delete
$field = $this->additional_fields[ $field ] ?? [];
[321] Fix | Delete
}
[322] Fix | Delete
if ( $document_object && $this->contains_valid_rules( $field['validation'] ) ) {
[323] Fix | Delete
return function ( $field_value, $field ) use ( $document_object ) {
[324] Fix | Delete
$errors = new WP_Error();
[325] Fix | Delete
[326] Fix | Delete
// Only validate if we have a field.
[327] Fix | Delete
if ( ! $field ) {
[328] Fix | Delete
return true;
[329] Fix | Delete
}
[330] Fix | Delete
[331] Fix | Delete
// Evaluate custom validation schema rules on the field.
[332] Fix | Delete
$validate_result = $this->is_valid_field( $field, $document_object );
[333] Fix | Delete
[334] Fix | Delete
if ( is_wp_error( $validate_result ) ) {
[335] Fix | Delete
/* translators: %s: is the field label */
[336] Fix | Delete
$error_message = sprintf( __( 'Please provide a valid %s', 'woocommerce' ), $field['label'] );
[337] Fix | Delete
$error_code = 'woocommerce_invalid_checkout_field';
[338] Fix | Delete
$errors->add( $error_code, $error_message );
[339] Fix | Delete
}
[340] Fix | Delete
[341] Fix | Delete
return $errors->has_errors() ? $errors : true;
[342] Fix | Delete
};
[343] Fix | Delete
}
[344] Fix | Delete
return $field['validate_callback'] ?? null;
[345] Fix | Delete
}
[346] Fix | Delete
[347] Fix | Delete
/**
[348] Fix | Delete
* Deregister a checkout field.
[349] Fix | Delete
*
[350] Fix | Delete
* @param string $field_id The field ID.
[351] Fix | Delete
*
[352] Fix | Delete
* @internal
[353] Fix | Delete
*/
[354] Fix | Delete
public function deregister_checkout_field( $field_id ) {
[355] Fix | Delete
if ( empty( $this->additional_fields[ $field_id ] ) ) {
[356] Fix | Delete
return;
[357] Fix | Delete
}
[358] Fix | Delete
[359] Fix | Delete
$location = $this->get_field_location( $field_id );
[360] Fix | Delete
[361] Fix | Delete
if ( ! $location ) {
[362] Fix | Delete
return;
[363] Fix | Delete
}
[364] Fix | Delete
[365] Fix | Delete
// Remove the field from the fields_locations array.
[366] Fix | Delete
$this->fields_locations[ $location ] = array_diff( $this->fields_locations[ $location ], array( $field_id ) );
[367] Fix | Delete
[368] Fix | Delete
// Remove the field from the additional_fields array.
[369] Fix | Delete
unset( $this->additional_fields[ $field_id ] );
[370] Fix | Delete
}
[371] Fix | Delete
[372] Fix | Delete
/**
[373] Fix | Delete
* Validates the "base" options (id, label, location) and shows warnings if they're not supplied.
[374] Fix | Delete
*
[375] Fix | Delete
* @param array $options The options supplied during field registration.
[376] Fix | Delete
* @return bool false if an error was encountered, true otherwise.
[377] Fix | Delete
*/
[378] Fix | Delete
private function validate_options( &$options ) {
[379] Fix | Delete
if ( empty( $options['id'] ) ) {
[380] Fix | Delete
_doing_it_wrong( 'woocommerce_register_additional_checkout_field', 'A checkout field cannot be registered without an id.', '8.6.0' );
[381] Fix | Delete
return false;
[382] Fix | Delete
}
[383] Fix | Delete
[384] Fix | Delete
// Having fewer than 2 after exploding around a / means there is no namespace.
[385] Fix | Delete
if ( count( explode( '/', $options['id'] ) ) < 2 ) {
[386] Fix | Delete
$message = sprintf( 'Unable to register field with id: "%s". %s', $options['id'], 'A checkout field id must consist of namespace/name.' );
[387] Fix | Delete
_doing_it_wrong( 'woocommerce_register_additional_checkout_field', esc_html( $message ), '8.6.0' );
[388] Fix | Delete
return false;
[389] Fix | Delete
}
[390] Fix | Delete
[391] Fix | Delete
if ( empty( $options['label'] ) ) {
[392] Fix | Delete
$message = sprintf( 'Unable to register field with id: "%s". %s', $options['id'], 'The field label is required.' );
[393] Fix | Delete
_doing_it_wrong( 'woocommerce_register_additional_checkout_field', esc_html( $message ), '8.6.0' );
[394] Fix | Delete
return false;
[395] Fix | Delete
}
[396] Fix | Delete
[397] Fix | Delete
if ( empty( $options['location'] ) ) {
[398] Fix | Delete
$message = sprintf( 'Unable to register field with id: "%s". %s', $options['id'], 'The field location is required.' );
[399] Fix | Delete
_doing_it_wrong( 'woocommerce_register_additional_checkout_field', esc_html( $message ), '8.6.0' );
[400] Fix | Delete
return false;
[401] Fix | Delete
}
[402] Fix | Delete
[403] Fix | Delete
if ( 'additional' === $options['location'] ) {
[404] Fix | Delete
wc_deprecated_argument( 'location', '8.9.0', 'The "additional" location is deprecated. Use "order" instead.' );
[405] Fix | Delete
$options['location'] = 'order';
[406] Fix | Delete
}
[407] Fix | Delete
[408] Fix | Delete
if ( ! in_array( $options['location'], array_keys( $this->fields_locations ), true ) ) {
[409] Fix | Delete
$message = sprintf( 'Unable to register field with id: "%s". %s', $options['id'], 'The field location is invalid.' );
[410] Fix | Delete
_doing_it_wrong( 'woocommerce_register_additional_checkout_field', esc_html( $message ), '8.6.0' );
[411] Fix | Delete
return false;
[412] Fix | Delete
}
[413] Fix | Delete
[414] Fix | Delete
// At this point, the essentials fields and its location should be set and valid.
[415] Fix | Delete
$location = $options['location'];
[416] Fix | Delete
$id = $options['id'];
[417] Fix | Delete
[418] Fix | Delete
// Check to see if field is already in the array.
[419] Fix | Delete
if ( ! empty( $this->additional_fields[ $id ] ) || in_array( $id, $this->fields_locations[ $location ], true ) ) {
[420] Fix | Delete
$message = sprintf( 'Unable to register field with id: "%s". %s', $id, 'The field is already registered.' );
[421] Fix | Delete
_doing_it_wrong( 'woocommerce_register_additional_checkout_field', esc_html( $message ), '8.6.0' );
[422] Fix | Delete
return false;
[423] Fix | Delete
}
[424] Fix | Delete
[425] Fix | Delete
if ( ! empty( $options['type'] ) ) {
[426] Fix | Delete
if ( ! in_array( $options['type'], $this->supported_field_types, true ) ) {
[427] Fix | Delete
$message = sprintf(
[428] Fix | Delete
'Unable to register field with id: "%s". Registering a field with type "%s" is not supported. The supported types are: %s.',
[429] Fix | Delete
$id,
[430] Fix | Delete
$options['type'],
[431] Fix | Delete
implode( ', ', $this->supported_field_types )
[432] Fix | Delete
);
[433] Fix | Delete
_doing_it_wrong( 'woocommerce_register_additional_checkout_field', esc_html( $message ), '8.6.0' );
[434] Fix | Delete
return false;
[435] Fix | Delete
}
[436] Fix | Delete
}
[437] Fix | Delete
[438] Fix | Delete
if ( ! empty( $options['sanitize_callback'] ) && ! is_callable( $options['sanitize_callback'] ) ) {
[439] Fix | Delete
$message = sprintf( 'Unable to register field with id: "%s". %s', $id, 'The sanitize_callback must be a valid callback.' );
[440] Fix | Delete
_doing_it_wrong( 'woocommerce_register_additional_checkout_field', esc_html( $message ), '8.6.0' );
[441] Fix | Delete
return false;
[442] Fix | Delete
}
[443] Fix | Delete
[444] Fix | Delete
if ( ! empty( $options['validate_callback'] ) && ! is_callable( $options['validate_callback'] ) ) {
[445] Fix | Delete
$message = sprintf( 'Unable to register field with id: "%s". %s', $id, 'The validate_callback must be a valid callback.' );
[446] Fix | Delete
_doing_it_wrong( 'woocommerce_register_additional_checkout_field', esc_html( $message ), '8.6.0' );
[447] Fix | Delete
return false;
[448] Fix | Delete
}
[449] Fix | Delete
[450] Fix | Delete
if ( ! empty( $options['hidden'] ) && true === $options['hidden'] ) {
[451] Fix | Delete
// Hidden fields are not supported right now. They will be registered with hidden => false.
[452] Fix | Delete
$message = sprintf( 'Registering a field with hidden set to true is not supported. The field "%s" will be registered as visible.', $id );
[453] Fix | Delete
_doing_it_wrong( 'woocommerce_register_additional_checkout_field', esc_html( $message ), '8.6.0' );
[454] Fix | Delete
// Don't return here unlike the other fields because this is not an issue that will prevent registration.
[455] Fix | Delete
}
[456] Fix | Delete
[457] Fix | Delete
$rule_fields = [ 'required', 'hidden', 'validation' ];
[458] Fix | Delete
$allow_bool = [ 'required', 'hidden' ];
[459] Fix | Delete
[460] Fix | Delete
foreach ( $rule_fields as $rule_field ) {
[461] Fix | Delete
if ( ! empty( $options[ $rule_field ] ) ) {
[462] Fix | Delete
if ( in_array( $rule_field, $allow_bool, true ) && is_bool( $options[ $rule_field ] ) ) {
[463] Fix | Delete
continue;
[464] Fix | Delete
}
[465] Fix | Delete
[466] Fix | Delete
$valid = Validation::is_valid_schema( $options[ $rule_field ] );
[467] Fix | Delete
[468] Fix | Delete
if ( is_wp_error( $valid ) ) {
[469] Fix | Delete
$message = sprintf( 'Unable to register field with id: "%s". %s', $options['id'], $rule_field . ': ' . $valid->get_error_message() );
[470] Fix | Delete
_doing_it_wrong( 'woocommerce_register_additional_checkout_field', esc_html( $message ), '8.6.0' );
[471] Fix | Delete
return false;
[472] Fix | Delete
}
[473] Fix | Delete
}
[474] Fix | Delete
}
[475] Fix | Delete
[476] Fix | Delete
return true;
[477] Fix | Delete
}
[478] Fix | Delete
[479] Fix | Delete
/**
[480] Fix | Delete
* Processes the options for a field type and returns the new field_options array.
[481] Fix | Delete
*
[482] Fix | Delete
* @param array $field_data The field data array to be updated.
[483] Fix | Delete
* @param array $options The options supplied during field registration.
[484] Fix | Delete
* @return array The updated $field_data array.
[485] Fix | Delete
*/
[486] Fix | Delete
private function process_field_options( $field_data, $options ) {
[487] Fix | Delete
if ( 'checkbox' === $field_data['type'] ) {
[488] Fix | Delete
$field_data = $this->process_checkbox_field( $field_data, $options );
[489] Fix | Delete
} elseif ( 'select' === $field_data['type'] ) {
[490] Fix | Delete
$field_data = $this->process_select_field( $field_data, $options );
[491] Fix | Delete
}
[492] Fix | Delete
return $field_data;
[493] Fix | Delete
}
[494] Fix | Delete
[495] Fix | Delete
/**
[496] Fix | Delete
* Processes the options for a select field and returns the new field_options array.
[497] Fix | Delete
*
[498] Fix | Delete
* @param array $field_data The field data array to be updated.
[499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function