PluginProbe
Booking Calendar / 11.9
Booking Calendar v11.9
11.9 11.8.4 11.8.3 11.8.2 11.8.1 11.8 11.7 11.6.1 11.6 11.5 11.4.3 11.4.2 11.4.1 11.4 11.3 11.2.1 11.2 11.1 11.0 10.15.7 10.15.6 10.1.3 10.10 10.10.1 10.10.2 All 205 releases
booking / includes / booking-resource-selector / booking-resource-selector__catalog.php

booking-resource-selector__catalog.php in Booking Calendar 11.9, at includes/booking-resource-selector/booking-resource-selector__catalog.php

159 lines 7.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Public Booking Resource catalogue for the selector shortcode.
4 *
5 * @package Booking Calendar
6 */
7
8 if ( ! defined( 'ABSPATH' ) ) {
9 exit;
10 }
11
12 /**
13 * Normalize one Booking Resource record for public selection.
14 *
15 * @param mixed $resource_record Raw resource object or array.
16 * @param int $fallback_id Resource ID supplied by the keyed collection.
17 * @param array<string,mixed> $resource_options Public searchable-resource presentation options.
18 *
19 * @return array<string,mixed>|null Public resource data, or null when invalid.
20 */
21 function wpbc_booking_resource_selector_normalize_resource( $resource_record, $fallback_id = 0, $resource_options = array() ) {
22 $resource_record = is_object( $resource_record ) ? get_object_vars( $resource_record ) : (array) $resource_record;
23 $resource_id = ! empty( $resource_record['id'] ) ? absint( $resource_record['id'] ) : absint( $fallback_id );
24 if ( ! $resource_id ) {
25 return null;
26 }
27
28 $resource_title = ! empty( $resource_record['title'] ) ? wp_strip_all_tags( wpbc_lang( $resource_record['title'] ) ) : '';
29 if ( '' === $resource_title ) {
30 /* translators: %d: Booking Resource ID. */
31 $resource_title = sprintf( __( 'Booking Resource #%d', 'booking' ), $resource_id );
32 }
33
34 $image_value = '';
35 if ( ! empty( $resource_record['image_url'] ) ) {
36 $image_value = $resource_record['image_url'];
37 } elseif ( ! empty( $resource_record['picture'] ) ) {
38 $image_value = $resource_record['picture'];
39 } elseif ( ! empty( $resource_options['picture'] ) ) {
40 $image_value = $resource_options['picture'];
41 }
42 if ( is_array( $image_value ) ) {
43 $image_value = reset( $image_value );
44 }
45 $image_url = is_scalar( $image_value ) ? esc_url_raw( wpbc_lang( (string) $image_value ) ) : '';
46
47 return array(
48 'resource_id' => $resource_id,
49 'title' => $resource_title,
50 'description' => ! empty( $resource_record['description'] ) ? wp_strip_all_tags( wpbc_lang( $resource_record['description'] ) ) : '',
51 'image_url' => $image_url,
52 'parent_id' => isset( $resource_record['parent'] ) ? absint( $resource_record['parent'] ) : 0,
53 'count' => isset( $resource_record['count'] ) ? absint( $resource_record['count'] ) : 0,
54 );
55 }
56
57 /**
58 * Build the public Booking Resource catalogue in configured display order.
59 *
60 * Commercial editions supply their resource cache through the established
61 * filter. The free edition falls back to its one canonical resource.
62 *
63 * @param array<string,mixed> $config Normalized selector configuration.
64 *
65 * @return array<int,array<string,mixed>> Resources keyed by resource ID.
66 */
67 function wpbc_booking_resource_selector_get_catalog( $config ) {
68 $config = wpbc_booking_resource_selector_normalize_config( $config );
69 if ( function_exists( 'wpbc_booking_resource_catalog_query_service' ) ) {
70 $resources = wpbc_booking_resource_catalog_query_service()->get_resources(
71 array(
72 'resource_ids' => $config['resource_ids'],
73 'include_summaries' => true,
74 )
75 );
76
77 return (array) apply_filters( 'wpbc_booking_resource_selector_public_catalog', $resources, $config );
78 }
79
80 $raw_resources = (array) apply_bk_filter( 'wpdebk_get_keyed_all_bk_resources', array() );
81 $search_options = array();
82 if ( function_exists( 'wpbc_searchable_resources__get_all_options' ) ) {
83 $search_options = (array) wpbc_searchable_resources__get_all_options();
84 }
85 $resources = array();
86
87 foreach ( $raw_resources as $resource_key => $resource_record ) {
88 $normalized_resource_id = absint( $resource_key );
89 if ( is_object( $resource_record ) && ! empty( $resource_record->id ) ) {
90 $normalized_resource_id = absint( $resource_record->id );
91 } elseif ( is_array( $resource_record ) && ! empty( $resource_record['id'] ) ) {
92 $normalized_resource_id = absint( $resource_record['id'] );
93 }
94 $resource_options = array();
95 if ( isset( $search_options[ $normalized_resource_id ] ) && is_array( $search_options[ $normalized_resource_id ] ) ) {
96 $resource_options = $search_options[ $normalized_resource_id ];
97 }
98 if ( function_exists( 'wpbc_booking_resource_content_repository' ) ) {
99 $raw_resource_array = is_object( $resource_record ) ? get_object_vars( $resource_record ) : (array) $resource_record;
100 $raw_title = isset( $raw_resource_array['title'] ) ? (string) $raw_resource_array['title'] : '';
101 $shared_content = wpbc_booking_resource_content_repository()->get( $normalized_resource_id, $raw_title, true );
102 $raw_resource_array['description'] = $shared_content['description'];
103 $raw_resource_array['image_url'] = $shared_content['picture_url'];
104 $resource_record = $raw_resource_array;
105 }
106 $resource = wpbc_booking_resource_selector_normalize_resource( $resource_record, $resource_key, $resource_options );
107 if ( $resource ) {
108 $resources[ $resource['resource_id'] ] = $resource;
109 }
110 }
111
112 if ( empty( $resources ) && ! class_exists( 'wpdev_bk_personal' ) ) {
113 $resource_id = class_exists( 'WPBC_FE_Attr_Postprocessor' ) ? WPBC_FE_Attr_Postprocessor::get_default_booking_resource_id() : 1;
114 $shared_content = function_exists( 'wpbc_booking_resource_content_repository' )
115 ? wpbc_booking_resource_content_repository()->get( $resource_id )
116 : array();
117 $resource_title = ! empty( $shared_content['title'] )
118 ? wpbc_lang( $shared_content['title'] )
119 : ( function_exists( 'wpbc_get_resource_title' ) ? wpbc_get_resource_title( $resource_id ) : '' );
120 $resources[ $resource_id ] = array(
121 'resource_id' => absint( $resource_id ),
122 'title' => $resource_title ? wp_strip_all_tags( wpbc_lang( $resource_title ) ) : __( 'Default Booking Resource', 'booking' ),
123 'description' => ! empty( $shared_content['description'] ) ? wp_strip_all_tags( wpbc_lang( $shared_content['description'] ) ) : '',
124 'image_url' => ! empty( $shared_content['picture_url'] ) ? esc_url_raw( wpbc_lang( $shared_content['picture_url'] ) ) : '',
125 'parent_id' => 0,
126 'count' => 0,
127 );
128 }
129
130 if ( $config['resource_ids'] ) {
131 $ordered_resources = array();
132 foreach ( $config['resource_ids'] as $resource_id ) {
133 if ( isset( $resources[ $resource_id ] ) ) {
134 $ordered_resources[ $resource_id ] = $resources[ $resource_id ];
135 }
136 }
137 $resources = $ordered_resources;
138 }
139
140 return (array) apply_filters( 'wpbc_booking_resource_selector_public_catalog', $resources, $config );
141 }
142
143 /**
144 * Return one selectable Booking Resource from a public catalogue.
145 *
146 * @param array<int,array<string,mixed>> $catalog Public resource catalogue.
147 * @param int $resource_id Requested Booking Resource ID.
148 *
149 * @return array<string,mixed>|WP_Error Resource or a public validation error.
150 */
151 function wpbc_booking_resource_selector_get_resource( $catalog, $resource_id ) {
152 $resource_id = absint( $resource_id );
153 if ( ! $resource_id || empty( $catalog[ $resource_id ] ) ) {
154 return new WP_Error( 'resource_selector_resource_unavailable', __( 'The selected Booking Resource is not available. Please choose another resource.', 'booking' ) );
155 }
156
157 return $catalog[ $resource_id ];
158 }
159