DocumentObject.php
11 months ago
Validation.php
1 year ago
checkout-document-schema.json
11 months ago
json-schema-draft-07.json
1 year ago
Validation.php
182 lines
| 1 | <?php |
| 2 | declare( strict_types = 1); |
| 3 | |
| 4 | namespace Automattic\WooCommerce\Blocks\Domain\Services\CheckoutFieldsSchema; |
| 5 | |
| 6 | use Automattic\WooCommerce\Blocks\Domain\Services\CheckoutFieldsSchema\DocumentObject; |
| 7 | use Opis\JsonSchema\{ |
| 8 | Helper, |
| 9 | Validator |
| 10 | }; |
| 11 | use WP_Error; |
| 12 | |
| 13 | /** |
| 14 | * Service class validating checkout field schema. |
| 15 | */ |
| 16 | class Validation { |
| 17 | /** |
| 18 | * Meta schema. |
| 19 | * |
| 20 | * @var string |
| 21 | */ |
| 22 | private static $meta_schema_json = ''; |
| 23 | |
| 24 | /** |
| 25 | * Get the field schema with context. |
| 26 | * |
| 27 | * @param string $field_id The field ID. |
| 28 | * @param array $field_schema The field schema. |
| 29 | * @param string $context The context. |
| 30 | * @return array |
| 31 | */ |
| 32 | public static function get_field_schema_with_context( $field_id, $field_schema, $context ) { |
| 33 | $primary_key = 'checkout'; |
| 34 | $secondary_key = 'additional_fields'; |
| 35 | switch ( $context ) { |
| 36 | case 'billing_address': |
| 37 | case 'shipping_address': |
| 38 | $primary_key = 'customer'; |
| 39 | $secondary_key = $context; |
| 40 | break; |
| 41 | case 'contact': |
| 42 | $primary_key = 'customer'; |
| 43 | $secondary_key = 'additional_fields'; |
| 44 | break; |
| 45 | } |
| 46 | return [ |
| 47 | $primary_key => [ |
| 48 | 'properties' => [ |
| 49 | $secondary_key => [ |
| 50 | 'properties' => [ |
| 51 | $field_id => $field_schema, |
| 52 | ], |
| 53 | ], |
| 54 | ], |
| 55 | ], |
| 56 | ]; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Check if the schema is unwrapped (has cart, checkout, customer, as top level keys). |
| 61 | * |
| 62 | * @param array $schema The schema to check. |
| 63 | * @return bool |
| 64 | */ |
| 65 | private static function schema_is_unwrapped( $schema ) { |
| 66 | return isset( $schema['cart'] ) || isset( $schema['checkout'] ) || isset( $schema['customer'] ); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Validate the field rules. |
| 71 | * |
| 72 | * @param DocumentObject $document_object The document object to validate. |
| 73 | * @param array $rules The rules to validate against. |
| 74 | * @return bool|WP_Error |
| 75 | */ |
| 76 | public static function validate_document_object( DocumentObject $document_object, $rules ) { |
| 77 | if ( self::schema_is_unwrapped( $rules ) ) { |
| 78 | $rules = [ |
| 79 | '$schema' => 'http://json-schema.org/draft-07/schema#', |
| 80 | 'type' => 'object', |
| 81 | 'properties' => $rules, |
| 82 | ]; |
| 83 | } else { |
| 84 | if ( ! isset( $rules['$schema'] ) ) { |
| 85 | $rules['$schema'] = 'http://json-schema.org/draft-07/schema#'; |
| 86 | } |
| 87 | if ( ! isset( $rules['type'] ) ) { |
| 88 | $rules['type'] = 'object'; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | try { |
| 93 | $validator = new Validator(); |
| 94 | $result = $validator->validate( |
| 95 | Helper::toJSON( $document_object->get_data() ), |
| 96 | Helper::toJSON( $rules ) |
| 97 | ); |
| 98 | |
| 99 | if ( ! $result->hasError() ) { |
| 100 | return true; |
| 101 | } |
| 102 | } catch ( \Exception $e ) { |
| 103 | return new WP_Error( 'woocommerce_rest_checkout_validation_failed', __( 'Validation failed.', 'woocommerce' ) ); |
| 104 | } |
| 105 | |
| 106 | // Return generic error message. |
| 107 | return new WP_Error( 'woocommerce_rest_checkout_invalid_field', __( 'Invalid field.', 'woocommerce' ) ); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Check if the fields have defined schema. |
| 112 | * |
| 113 | * @param array $fields The fields. |
| 114 | * @return bool |
| 115 | */ |
| 116 | public static function has_field_schema( $fields ) { |
| 117 | $return = false; |
| 118 | |
| 119 | foreach ( $fields as $field ) { |
| 120 | if ( |
| 121 | ( ! empty( $field['validation'] ) && is_array( $field['validation'] ) ) || |
| 122 | ( ! empty( $field['required'] ) && is_array( $field['required'] ) ) || |
| 123 | ( ! empty( $field['hidden'] ) && is_array( $field['hidden'] ) ) |
| 124 | ) { |
| 125 | $return = true; |
| 126 | break; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | return $return; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Validate meta schema for field rules. |
| 135 | * |
| 136 | * @param mixed $rules The rules to validate. |
| 137 | * @return bool|WP_Error True if the field options are valid, a WP_Error otherwise. |
| 138 | */ |
| 139 | public static function is_valid_schema( $rules ) { |
| 140 | if ( ! is_array( $rules ) ) { |
| 141 | return new WP_Error( 'woocommerce_rest_checkout_invalid_field_schema', 'Rules must be defined as an array.' ); |
| 142 | } |
| 143 | |
| 144 | if ( empty( $rules ) ) { |
| 145 | return true; |
| 146 | } |
| 147 | |
| 148 | if ( self::schema_is_unwrapped( $rules ) ) { |
| 149 | $rules = [ |
| 150 | 'type' => 'object', |
| 151 | 'properties' => $rules, |
| 152 | ]; |
| 153 | } |
| 154 | |
| 155 | if ( empty( self::$meta_schema_json ) ) { |
| 156 | // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents |
| 157 | self::$meta_schema_json = file_get_contents( __DIR__ . '/json-schema-draft-07.json' ); |
| 158 | } |
| 159 | |
| 160 | $validator = new Validator(); |
| 161 | $result = $validator->validate( |
| 162 | Helper::toJSON( |
| 163 | [ |
| 164 | '$schema' => 'http://json-schema.org/draft-07/schema#', |
| 165 | 'type' => 'object', |
| 166 | 'properties' => [ |
| 167 | 'test' => $rules, |
| 168 | ], |
| 169 | 'required' => [ 'test' ], |
| 170 | ] |
| 171 | ), |
| 172 | self::$meta_schema_json |
| 173 | ); |
| 174 | |
| 175 | if ( $result->hasError() ) { |
| 176 | return new WP_Error( 'woocommerce_rest_checkout_invalid_field_schema', esc_html( (string) $result->error() ) ); |
| 177 | } |
| 178 | |
| 179 | return true; |
| 180 | } |
| 181 | } |
| 182 |