function schema_has_required_property( $additional_fields_schema ) {
return array_reduce(
array_keys( $additional_fields_schema ),
function ( $carry, $key ) use ( $additional_fields_schema ) {
return $carry || $additional_fields_schema[ $key ]['required'];
},
false
);
}
/**
* Sanitize and format additional fields object.
*
* @param array $fields Values being sanitized.
* @return array
*/
public function sanitize_additional_fields( $fields ) {
$properties = $this->get_additional_fields_schema();
$sanitization_utils = new SanitizationUtils();
$fields = $sanitization_utils->wp_kses_array(
array_reduce(
array_keys( $fields ),
function ( $carry, $key ) use ( $fields, $properties ) {
if ( ! isset( $properties[ $key ] ) ) {
return $carry;
}
$field_schema = $properties[ $key ];
$rest_sanitized = rest_sanitize_value_from_schema( wp_unslash( $fields[ $key ] ), $field_schema, $key );
$rest_sanitized = $this->additional_fields_controller->sanitize_field( $key, $rest_sanitized );
$carry[ $key ] = $rest_sanitized;
return $carry;
},
[]
)
);
return $sanitization_utils->wp_kses_array( $fields );
}
/**
* Validate additional fields object. This does not validate required fields nor customer validation rules because
* this may be a partial request. That will happen later when the full request is processed during POST. This only
* validates against the schema.
*
* @see rest_validate_value_from_schema
*
* @param array $fields Value being sanitized.
* @param \WP_REST_Request $request The Request.
* @return true|\WP_Error
*/
public function validate_additional_fields( $fields, $request ) {
$errors = new \WP_Error();
$fields = $this->sanitize_additional_fields( $fields );
$additional_field_schema = $this->get_additional_fields_schema();
// for PUT requests, we only want to validate the fields that are being updated.
if ( $request->get_method() === 'PUT' ) {
$additional_field_schema = array_intersect_key( $additional_field_schema, $fields );
}
// on POST, loop over the schema instead of the fields. This is to ensure missing fields are validated.
foreach ( $additional_field_schema as $key => $schema ) {
if ( ! isset( $fields[ $key ] ) && ! $schema['required'] ) {
// Optional fields can go missing.
continue;
}
$result = rest_validate_value_from_schema( $fields[ $key ] ?? null, $schema, $key );
if ( is_wp_error( $result ) && $result->has_errors() ) {
$location = $this->additional_fields_controller->get_field_location( $key );
foreach ( $result->get_error_codes() as $code ) {
$result->add_data(
array(
'location' => $location,
'key' => $key,
),
$code
);
}
$errors->merge_from( $result );
}
}
return $errors->has_errors() ? $errors : true;
}
}
Warning : Class 'Automattic\WooCommerce\StoreApi\Schemas\V1\CheckoutSchema' not found in /home/ecofinir/public_html/wp-content/plugins/woocommerce/src/StoreApi/deprecated.php on line 73
بایگانیها وخارزم معدن طلا - اکوفاین | مدیریتی ، مالی ، اقتصادی