$attribute['default'], $metadata['attributes'] ); } public static function validate( string $kind, array $input ): array|WP_Error { $defaults = self::defaults( $kind ); // WordPress core wrapper attributes are not business or transport parameters. unset( $input['className'], $input['anchor'], $input['metadata'] ); if ( array_diff_key( $input, $defaults + array( 'serviceSlug' => '' ) ) ) { return Services::error( 'embed_parameters', 400 ); } $values = $input + $defaults; foreach ( array( 'serviceId', 'connectionAccountId', 'pageSize' ) as $key ) { if ( array_key_exists( $key, $values ) && ( ! is_int( $values[ $key ] ) || $values[ $key ] < 0 || $values[ $key ] > 2147483647 ) ) { return Services::error( 'embed_parameters', 400 ); } } if ( isset( $values['pageSize'] ) && ( $values['pageSize'] < 1 || $values['pageSize'] > 100 ) ) { return Services::error( 'embed_parameters', 400 ); } foreach ( array( 'locale' => array( 'auto', 'hu', 'en', 'de' ), 'styleSource' => array( 'inherit', 'service' ), 'resourceSelector' => array( 'hidden', 'optional' ), 'typeFilter' => array( 'all', 'single', 'group' ), 'layout' => array( 'list', 'grid' ), ) as $key => $options ) { if ( array_key_exists( $key, $values ) && ! in_array( $values[ $key ], $options, true ) ) { return Services::error( 'embed_parameters', 400 ); } } foreach ( array( 'showTitle', 'showDescription', 'showPrice' ) as $key ) { if ( array_key_exists( $key, $values ) && ! is_bool( $values[ $key ] ) ) { return Services::error( 'embed_parameters', 400 ); } } if ( array_key_exists( 'serviceSlug', $values ) && ( 'booking' !== $kind || ! is_string( $values['serviceSlug'] ) || ! preg_match( '/\A[A-Za-z0-9_-]{1,255}\z/', $values['serviceSlug'] ) || $values['serviceId'] > 0 ) ) { return Services::error( 'embed_parameters', 400 ); } return $values; } public static function shortcode( array $attributes ): array|WP_Error { $map = array( 'service_id' => 'serviceId', 'service' => 'serviceSlug', 'connection_account_id' => 'connectionAccountId', 'resource_selector' => 'resourceSelector', 'locale' => 'locale', 'style_source' => 'styleSource', 'show_title' => 'showTitle', 'show_description' => 'showDescription', 'show_price' => 'showPrice', 'type' => 'typeFilter', 'layout' => 'layout', 'page_size' => 'pageSize', ); $kind = isset( $attributes['service_id'] ) || isset( $attributes['service'] ) ? 'booking' : 'services'; if ( isset( $attributes['service_id'], $attributes['service'] ) ) { return Services::error( 'embed_parameters', 400 ); } $input = array(); foreach ( $attributes as $key => $value ) { if ( ! isset( $map[ $key ] ) || ! is_string( $value ) ) { return Services::error( 'embed_parameters', 400 ); } if ( in_array( $key, array( 'service_id', 'connection_account_id', 'page_size' ), true ) ) { if ( ! preg_match( '/\A[0-9]{1,10}\z/', $value ) ) { return Services::error( 'embed_parameters', 400 ); } $value = (int) $value; } elseif ( str_starts_with( $key, 'show_' ) ) { if ( ! in_array( $value, array( 'true', 'false', '1', '0' ), true ) ) { return Services::error( 'embed_parameters', 400 ); } $value = in_array( $value, array( 'true', '1' ), true ); } $input[ $map[ $key ] ] = $value; } $values = self::validate( $kind, $input ); return is_wp_error( $values ) ? $values : array( 'kind' => $kind, 'attributes' => $values, ); } public static function code( string $kind, array $attributes ): string { $parts = array( '[waitlistize', 'connection_account_id="' . $attributes['connectionAccountId'] . '"' ); $map = array( 'serviceId' => 'service_id', 'resourceSelector' => 'resource_selector', 'locale' => 'locale', 'styleSource' => 'style_source', 'showTitle' => 'show_title', 'showDescription' => 'show_description', 'showPrice' => 'show_price', 'typeFilter' => 'type', 'layout' => 'layout', 'pageSize' => 'page_size', ); foreach ( self::defaults( $kind ) as $key => $default ) { if ( isset( $map[ $key ] ) ) { $value = $attributes[ $key ]; $parts[] = $map[ $key ] . '="' . ( is_bool( $value ) ? ( $value ? 'true' : 'false' ) : $value ) . '"'; } } return implode( ' ', $parts ) . ']'; } }