booking
/
includes
/
page-appointment-services
/
catalog
/
class-wpbc-appointment-services-catalog-request.php
class-wpbc-appointment-services-catalog-request.php in Booking Calendar 11.9, at includes/page-appointment-services/catalog/class-wpbc-appointment-services-catalog-request.php
| 1 | <?php |
| 2 | /** |
| 3 | * Appointment Services catalog domain request. |
| 4 | * |
| 5 | * @package Booking Calendar |
| 6 | * @since 11.6.0 |
| 7 | */ |
| 8 | |
| 9 | if ( ! defined( 'ABSPATH' ) ) { |
| 10 | exit; |
| 11 | } |
| 12 | |
| 13 | /** |
| 14 | * Validate Service-only filters without adding domain knowledge to the shared catalog. |
| 15 | * |
| 16 | * Status and Provider filters are intentionally normalized beside the Service |
| 17 | * endpoint because neither field is meaningful to another catalog domain. |
| 18 | */ |
| 19 | final class WPBC_Appointment_Services_Catalog_Request { |
| 20 | |
| 21 | /** |
| 22 | * Normalized Service filters. |
| 23 | * |
| 24 | * @var array<string,mixed> |
| 25 | */ |
| 26 | private $values = array(); |
| 27 | |
| 28 | /** |
| 29 | * Create a validated Service request. |
| 30 | * |
| 31 | * @param mixed $request_values Untrusted Service filter values. |
| 32 | * |
| 33 | * @return WPBC_Appointment_Services_Catalog_Request|WP_Error Valid request or safe validation error. |
| 34 | */ |
| 35 | public static function create( $request_values = array() ) { |
| 36 | $request_values = is_array( $request_values ) ? $request_values : array(); |
| 37 | $status = 'all'; |
| 38 | if ( array_key_exists( 'status', $request_values ) ) { |
| 39 | if ( ! is_scalar( $request_values['status'] ) ) { |
| 40 | return new WP_Error( 'wpbc_appointment_services_invalid_status', __( 'The requested Service status is invalid.', 'booking' ) ); |
| 41 | } |
| 42 | $status = sanitize_key( (string) $request_values['status'] ); |
| 43 | } |
| 44 | if ( ! in_array( $status, array( 'all', 'active', 'inactive', 'archived' ), true ) ) { |
| 45 | return new WP_Error( 'wpbc_appointment_services_invalid_status', __( 'The requested Service status is invalid.', 'booking' ) ); |
| 46 | } |
| 47 | |
| 48 | $resource_id = 0; |
| 49 | if ( array_key_exists( 'resource_id', $request_values ) ) { |
| 50 | if ( ! is_scalar( $request_values['resource_id'] ) || ! preg_match( '/^\d+$/', (string) $request_values['resource_id'] ) ) { |
| 51 | return new WP_Error( 'wpbc_appointment_services_invalid_provider', __( 'The requested Provider is invalid.', 'booking' ) ); |
| 52 | } |
| 53 | $resource_id = absint( $request_values['resource_id'] ); |
| 54 | } |
| 55 | $provider_options = wpbc_appointment_services_get_provider_options(); |
| 56 | if ( $resource_id && ! isset( $provider_options[ $resource_id ] ) ) { |
| 57 | return new WP_Error( 'wpbc_appointment_services_invalid_provider', __( 'The requested Provider is invalid.', 'booking' ) ); |
| 58 | } |
| 59 | |
| 60 | $request = new self(); |
| 61 | $request->values = array( |
| 62 | 'status' => $status, |
| 63 | 'resource_id' => $resource_id, |
| 64 | ); |
| 65 | |
| 66 | return $request; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Return one normalized filter. |
| 71 | * |
| 72 | * @param string $key Filter key. |
| 73 | * @param mixed $default Fallback value. |
| 74 | * |
| 75 | * @return mixed Normalized value or fallback. |
| 76 | */ |
| 77 | public function get( $key, $default = null ) { |
| 78 | return array_key_exists( $key, $this->values ) ? $this->values[ $key ] : $default; |
| 79 | } |
| 80 | } |
| 81 |