AI
1 year ago
Agentic
7 months ago
AbstractAddressSchema.php
1 year ago
AbstractSchema.php
1 month ago
BatchSchema.php
2 years ago
BillingAddressSchema.php
2 years ago
CartCouponSchema.php
1 year ago
CartExtensionsSchema.php
1 year ago
CartFeeSchema.php
2 years ago
CartItemSchema.php
1 month ago
CartSchema.php
2 months ago
CartShippingRateSchema.php
1 month ago
CheckoutOrderSchema.php
2 years ago
CheckoutSchema.php
1 month ago
ErrorSchema.php
2 years ago
ImageAttachmentSchema.php
3 months ago
ItemSchema.php
11 months ago
OrderCouponSchema.php
2 years ago
OrderFeeSchema.php
2 years ago
OrderItemSchema.php
1 month ago
OrderSchema.php
2 months ago
PatternsSchema.php
1 year ago
ProductAttributeSchema.php
2 years ago
ProductAttributeTermSchema.php
1 month ago
ProductBrandSchema.php
1 year ago
ProductCategorySchema.php
2 years ago
ProductCollectionDataSchema.php
11 months ago
ProductReviewSchema.php
2 years ago
ProductSchema.php
1 month ago
ShippingAddressSchema.php
2 years ago
ShopperListItemSchema.php
1 month ago
ShopperListSchema.php
1 month ago
TermSchema.php
2 years ago
AbstractAddressSchema.php
319 lines
| 1 | <?php |
| 2 | declare( strict_types = 1); |
| 3 | namespace Automattic\WooCommerce\StoreApi\Schemas\V1; |
| 4 | |
| 5 | use Automattic\WooCommerce\StoreApi\Utilities\SanitizationUtils; |
| 6 | use Automattic\WooCommerce\StoreApi\Utilities\ValidationUtils; |
| 7 | use Automattic\WooCommerce\Blocks\Domain\Services\CheckoutFields; |
| 8 | use Automattic\WooCommerce\StoreApi\Schemas\ExtendSchema; |
| 9 | use Automattic\WooCommerce\StoreApi\SchemaController; |
| 10 | use Automattic\WooCommerce\Blocks\Package; |
| 11 | |
| 12 | /** |
| 13 | * AddressSchema class. |
| 14 | * |
| 15 | * Provides a generic address schema for composition in other schemas. |
| 16 | */ |
| 17 | abstract class AbstractAddressSchema extends AbstractSchema { |
| 18 | |
| 19 | /** |
| 20 | * Additional fields controller. |
| 21 | * |
| 22 | * @var CheckoutFields |
| 23 | */ |
| 24 | protected $additional_fields_controller; |
| 25 | |
| 26 | /** |
| 27 | * Constructor. |
| 28 | * |
| 29 | * @param ExtendSchema $extend ExtendSchema instance. |
| 30 | * @param SchemaController $controller Schema Controller instance. |
| 31 | */ |
| 32 | public function __construct( ExtendSchema $extend, SchemaController $controller ) { |
| 33 | parent::__construct( $extend, $controller ); |
| 34 | $this->additional_fields_controller = Package::container()->get( CheckoutFields::class ); |
| 35 | } |
| 36 | /** |
| 37 | * Term properties. |
| 38 | * |
| 39 | * @internal Note that required properties don't require values, just that they are included in the request. |
| 40 | * @return array |
| 41 | */ |
| 42 | public function get_properties() { |
| 43 | return array_merge( |
| 44 | [ |
| 45 | 'first_name' => [ |
| 46 | 'description' => __( 'First name', 'woocommerce' ), |
| 47 | 'type' => 'string', |
| 48 | 'context' => [ 'view', 'edit' ], |
| 49 | 'required' => true, |
| 50 | ], |
| 51 | 'last_name' => [ |
| 52 | 'description' => __( 'Last name', 'woocommerce' ), |
| 53 | 'type' => 'string', |
| 54 | 'context' => [ 'view', 'edit' ], |
| 55 | 'required' => true, |
| 56 | ], |
| 57 | 'company' => [ |
| 58 | 'description' => __( 'Company', 'woocommerce' ), |
| 59 | 'type' => 'string', |
| 60 | 'context' => [ 'view', 'edit' ], |
| 61 | 'required' => true, |
| 62 | ], |
| 63 | 'address_1' => [ |
| 64 | 'description' => __( 'Address', 'woocommerce' ), |
| 65 | 'type' => 'string', |
| 66 | 'context' => [ 'view', 'edit' ], |
| 67 | 'required' => true, |
| 68 | ], |
| 69 | 'address_2' => [ |
| 70 | 'description' => __( 'Apartment, suite, etc.', 'woocommerce' ), |
| 71 | 'type' => 'string', |
| 72 | 'context' => [ 'view', 'edit' ], |
| 73 | 'required' => true, |
| 74 | ], |
| 75 | 'city' => [ |
| 76 | 'description' => __( 'City', 'woocommerce' ), |
| 77 | 'type' => 'string', |
| 78 | 'context' => [ 'view', 'edit' ], |
| 79 | 'required' => true, |
| 80 | ], |
| 81 | 'state' => [ |
| 82 | 'description' => __( 'State/County code, or name of the state, county, province, or district.', 'woocommerce' ), |
| 83 | 'type' => 'string', |
| 84 | 'context' => [ 'view', 'edit' ], |
| 85 | 'required' => true, |
| 86 | ], |
| 87 | 'postcode' => [ |
| 88 | 'description' => __( 'Postal code', 'woocommerce' ), |
| 89 | 'type' => 'string', |
| 90 | 'context' => [ 'view', 'edit' ], |
| 91 | 'required' => true, |
| 92 | ], |
| 93 | 'country' => [ |
| 94 | 'description' => __( 'Country/Region code in ISO 3166-1 alpha-2 format.', 'woocommerce' ), |
| 95 | 'type' => 'string', |
| 96 | 'context' => [ 'view', 'edit' ], |
| 97 | 'required' => true, |
| 98 | ], |
| 99 | 'phone' => [ |
| 100 | 'description' => __( 'Phone', 'woocommerce' ), |
| 101 | 'type' => 'string', |
| 102 | 'context' => [ 'view', 'edit' ], |
| 103 | 'required' => true, |
| 104 | ], |
| 105 | ], |
| 106 | $this->get_additional_address_fields_schema(), |
| 107 | ); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Sanitize and format the given address object. |
| 112 | * |
| 113 | * @param array $address Value being sanitized. |
| 114 | * @param \WP_REST_Request $request The Request. |
| 115 | * @param string $param The param being sanitized. |
| 116 | * @return array |
| 117 | */ |
| 118 | public function sanitize_callback( $address, $request, $param ) { |
| 119 | $validation_util = new ValidationUtils(); |
| 120 | $sanitization_util = new SanitizationUtils(); |
| 121 | $address = (array) $address; |
| 122 | $schema = $this->get_properties(); |
| 123 | // omit all keys from address that are not in the schema. This should account for email. |
| 124 | $address = array_intersect_key( $address, $schema ); |
| 125 | $address = array_reduce( |
| 126 | array_keys( $address ), |
| 127 | function ( $carry, $key ) use ( $address, $validation_util, $schema ) { |
| 128 | switch ( $key ) { |
| 129 | case 'country': |
| 130 | $carry[ $key ] = wc_strtoupper( sanitize_text_field( wp_unslash( $address[ $key ] ) ) ); |
| 131 | break; |
| 132 | case 'state': |
| 133 | $carry[ $key ] = $validation_util->format_state( sanitize_text_field( wp_unslash( $address[ $key ] ) ), $address['country'] ); |
| 134 | break; |
| 135 | case 'postcode': |
| 136 | $carry[ $key ] = $address['postcode'] ? wc_format_postcode( sanitize_text_field( wp_unslash( $address['postcode'] ) ), $address['country'] ) : ''; |
| 137 | break; |
| 138 | default: |
| 139 | $carry[ $key ] = rest_sanitize_value_from_schema( wp_unslash( $address[ $key ] ), $schema[ $key ], $key ); |
| 140 | break; |
| 141 | } |
| 142 | if ( $this->additional_fields_controller->is_field( $key ) ) { |
| 143 | $carry[ $key ] = $this->additional_fields_controller->sanitize_field( $key, $carry[ $key ] ); |
| 144 | } |
| 145 | return $carry; |
| 146 | }, |
| 147 | [] |
| 148 | ); |
| 149 | |
| 150 | return $sanitization_util->wp_kses_array( $address ); |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Validate the given address object. |
| 155 | * |
| 156 | * @see rest_validate_value_from_schema |
| 157 | * |
| 158 | * @param array $address Value being sanitized. |
| 159 | * @param \WP_REST_Request $request The Request. |
| 160 | * @param string $param The param being sanitized. |
| 161 | * @return true|\WP_Error |
| 162 | */ |
| 163 | public function validate_callback( $address, $request, $param ) { |
| 164 | $errors = new \WP_Error(); |
| 165 | $address = (array) $address; |
| 166 | $validation_util = new ValidationUtils(); |
| 167 | $schema = $this->get_properties(); |
| 168 | |
| 169 | // Omit all keys from address that are not in the schema. This should account for email. |
| 170 | $address = array_intersect_key( $address, $schema ); |
| 171 | |
| 172 | // The flow is Validate -> Sanitize -> Re-Validate |
| 173 | // First validation step is to ensure fields match their schema, then we sanitize to put them in the |
| 174 | // correct format, and finally the second validation step is to ensure the correctly-formatted values |
| 175 | // match what we expect (postcode etc.). |
| 176 | foreach ( $address as $key => $value ) { |
| 177 | // Only run specific validation on properties that are defined in the schema and present in the address. |
| 178 | // This is for partial address pushes when only part of a customer address is sent. |
| 179 | // Full schema address validation still happens later, so empty, required values are disallowed. |
| 180 | if ( empty( $schema[ $key ] ) || empty( $address[ $key ] ) ) { |
| 181 | continue; |
| 182 | } |
| 183 | |
| 184 | if ( is_wp_error( rest_validate_value_from_schema( $value, $schema[ $key ], $key ) ) ) { |
| 185 | $errors->add( |
| 186 | 'invalid_' . $key, |
| 187 | sprintf( |
| 188 | /* translators: %s: field name */ |
| 189 | __( 'Invalid %s provided.', 'woocommerce' ), |
| 190 | $key |
| 191 | ) |
| 192 | ); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | // This condition will be true if any validation errors were encountered, e.g. wrong type supplied or invalid |
| 197 | // option in enum fields. |
| 198 | if ( $errors->has_errors() ) { |
| 199 | return $errors; |
| 200 | } |
| 201 | |
| 202 | $address = $this->sanitize_callback( $address, $request, $param ); |
| 203 | |
| 204 | if ( ! empty( $address['country'] ) && ! in_array( $address['country'], array_keys( wc()->countries->get_countries() ), true ) ) { |
| 205 | $errors->add( |
| 206 | 'invalid_country', |
| 207 | sprintf( |
| 208 | /* translators: %s valid country codes */ |
| 209 | __( 'Invalid country code provided. Must be one of: %s', 'woocommerce' ), |
| 210 | implode( ', ', array_keys( wc()->countries->get_countries() ) ) |
| 211 | ) |
| 212 | ); |
| 213 | return $errors; |
| 214 | } |
| 215 | |
| 216 | if ( ! empty( $address['state'] ) && ! $validation_util->validate_state( $address['state'], $address['country'] ) ) { |
| 217 | $errors->add( |
| 218 | 'invalid_state', |
| 219 | sprintf( |
| 220 | /* translators: %1$s given state, %2$s valid states */ |
| 221 | __( 'The provided state (%1$s) is not valid. Must be one of: %2$s', 'woocommerce' ), |
| 222 | esc_html( $address['state'] ), |
| 223 | implode( ', ', array_keys( $validation_util->get_states_for_country( $address['country'] ) ) ) |
| 224 | ) |
| 225 | ); |
| 226 | } |
| 227 | |
| 228 | if ( ! empty( $address['postcode'] ) && ! \WC_Validation::is_postcode( $address['postcode'], $address['country'] ) ) { |
| 229 | $errors->add( |
| 230 | 'invalid_postcode', |
| 231 | __( 'The provided postcode / ZIP is not valid', 'woocommerce' ) |
| 232 | ); |
| 233 | } |
| 234 | |
| 235 | if ( ! empty( $address['phone'] ) ) { |
| 236 | // This is a safe sanitize to prevent copy-paste issues with invisible chars. Won't ensure validation. |
| 237 | $address['phone'] = wc_remove_non_displayable_chars( $address['phone'] ); |
| 238 | |
| 239 | if ( ! \WC_Validation::is_phone( $address['phone'] ) ) { |
| 240 | $errors->add( |
| 241 | 'invalid_phone', |
| 242 | __( 'The provided phone number is not valid', 'woocommerce' ) |
| 243 | ); |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | // Get additional field keys here as we need to know if they are present in the address for validation. |
| 248 | $additional_keys = array_keys( $this->get_additional_address_fields_schema() ); |
| 249 | |
| 250 | foreach ( array_keys( $address ) as $key ) { |
| 251 | // Skip email here it will be validated in BillingAddressSchema. |
| 252 | if ( 'email' === $key ) { |
| 253 | continue; |
| 254 | } |
| 255 | |
| 256 | // Only run specific validation on properties that are defined in the schema and present in the address. |
| 257 | // This is for partial address pushes when only part of a customer address is sent. |
| 258 | // Full schema address validation still happens later, so empty, required values are disallowed. |
| 259 | if ( empty( $schema[ $key ] ) || empty( $address[ $key ] ) ) { |
| 260 | continue; |
| 261 | } |
| 262 | |
| 263 | $field_schema = $schema[ $key ]; |
| 264 | $field_value = isset( $address[ $key ] ) ? $address[ $key ] : null; |
| 265 | $result = rest_validate_value_from_schema( $field_value, $field_schema, $key ); |
| 266 | |
| 267 | if ( is_wp_error( $result ) && $result->has_errors() ) { |
| 268 | $errors->merge_from( $result ); |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | return $errors->has_errors( $errors ) ? $errors : true; |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * Get additional address fields schema. |
| 277 | * |
| 278 | * @return array |
| 279 | */ |
| 280 | protected function get_additional_address_fields_schema() { |
| 281 | $additional_fields_keys = $this->additional_fields_controller->get_address_fields_keys(); |
| 282 | $fields = $this->additional_fields_controller->get_additional_fields(); |
| 283 | |
| 284 | $address_fields = array_filter( |
| 285 | $fields, |
| 286 | function ( $key ) use ( $additional_fields_keys ) { |
| 287 | return in_array( $key, $additional_fields_keys, true ); |
| 288 | }, |
| 289 | ARRAY_FILTER_USE_KEY |
| 290 | ); |
| 291 | |
| 292 | $schema = []; |
| 293 | foreach ( $address_fields as $key => $field ) { |
| 294 | $field_schema = [ |
| 295 | 'description' => $field['label'], |
| 296 | 'type' => 'string', |
| 297 | 'context' => [ 'view', 'edit' ], |
| 298 | 'required' => $this->additional_fields_controller->is_conditional_field( $field ) ? false : true === $field['required'], |
| 299 | ]; |
| 300 | |
| 301 | if ( 'select' === $field['type'] ) { |
| 302 | $field_schema['enum'] = array_map( |
| 303 | function ( $option ) { |
| 304 | return $option['value']; |
| 305 | }, |
| 306 | $field['options'] |
| 307 | ); |
| 308 | } |
| 309 | |
| 310 | if ( 'checkbox' === $field['type'] ) { |
| 311 | $field_schema['type'] = 'boolean'; |
| 312 | } |
| 313 | |
| 314 | $schema[ $key ] = $field_schema; |
| 315 | } |
| 316 | return $schema; |
| 317 | } |
| 318 | } |
| 319 |