PluginProbe
Booking Calendar / 11.5
Booking Calendar v11.5
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.5, at includes/booking-resource-selector/booking-resource-selector__catalog.php

135 lines 5.5 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 $raw_resources = (array) apply_bk_filter( 'wpdebk_get_keyed_all_bk_resources', array() );
70 $search_options = array();
71 if ( function_exists( 'wpbc_searchable_resources__get_all_options' ) ) {
72 $search_options = (array) wpbc_searchable_resources__get_all_options();
73 }
74 $resources = array();
75
76 foreach ( $raw_resources as $resource_key => $resource_record ) {
77 $normalized_resource_id = absint( $resource_key );
78 if ( is_object( $resource_record ) && ! empty( $resource_record->id ) ) {
79 $normalized_resource_id = absint( $resource_record->id );
80 } elseif ( is_array( $resource_record ) && ! empty( $resource_record['id'] ) ) {
81 $normalized_resource_id = absint( $resource_record['id'] );
82 }
83 $resource_options = array();
84 if ( isset( $search_options[ $normalized_resource_id ] ) && is_array( $search_options[ $normalized_resource_id ] ) ) {
85 $resource_options = $search_options[ $normalized_resource_id ];
86 }
87 $resource = wpbc_booking_resource_selector_normalize_resource( $resource_record, $resource_key, $resource_options );
88 if ( $resource ) {
89 $resources[ $resource['resource_id'] ] = $resource;
90 }
91 }
92
93 if ( empty( $resources ) && ! class_exists( 'wpdev_bk_personal' ) ) {
94 $resource_id = class_exists( 'WPBC_FE_Attr_Postprocessor' ) ? WPBC_FE_Attr_Postprocessor::get_default_booking_resource_id() : 1;
95 $resource_title = function_exists( 'wpbc_get_resource_title' ) ? wpbc_get_resource_title( $resource_id ) : '';
96 $resources[ $resource_id ] = array(
97 'resource_id' => absint( $resource_id ),
98 'title' => $resource_title ? wp_strip_all_tags( wpbc_lang( $resource_title ) ) : __( 'Default Booking Resource', 'booking' ),
99 'description' => '',
100 'image_url' => '',
101 'parent_id' => 0,
102 'count' => 0,
103 );
104 }
105
106 if ( $config['resource_ids'] ) {
107 $ordered_resources = array();
108 foreach ( $config['resource_ids'] as $resource_id ) {
109 if ( isset( $resources[ $resource_id ] ) ) {
110 $ordered_resources[ $resource_id ] = $resources[ $resource_id ];
111 }
112 }
113 $resources = $ordered_resources;
114 }
115
116 return (array) apply_filters( 'wpbc_booking_resource_selector_public_catalog', $resources, $config );
117 }
118
119 /**
120 * Return one selectable Booking Resource from a public catalogue.
121 *
122 * @param array<int,array<string,mixed>> $catalog Public resource catalogue.
123 * @param int $resource_id Requested Booking Resource ID.
124 *
125 * @return array<string,mixed>|WP_Error Resource or a public validation error.
126 */
127 function wpbc_booking_resource_selector_get_resource( $catalog, $resource_id ) {
128 $resource_id = absint( $resource_id );
129 if ( ! $resource_id || empty( $catalog[ $resource_id ] ) ) {
130 return new WP_Error( 'resource_selector_resource_unavailable', __( 'The selected Booking Resource is not available. Please choose another resource.', 'booking' ) );
131 }
132
133 return $catalog[ $resource_id ];
134 }
135