PluginProbe
Bookit — Booking & Appointment Calendar / 2.6.0.5
Bookit — Booking & Appointment Calendar v2.6.0.5
2.6.0.5 2.6.0.4 2.6.0.3 2.6.0.2 2.6.0.1 2.6.0 trunk 1.2 1.2.2 1.2.3 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 All 62 releases
bookit / includes / classes / BookitController.php

BookitController.php in Bookit — Booking & Appointment Calendar 2.6.0.5, at includes/classes/BookitController.php

282 lines 8.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Bookit\Classes;
4
5 use Bookit\Classes\Database\Categories;
6 use Bookit\Classes\Database\Customers;
7 use Bookit\Classes\Database\Services;
8 use Bookit\Classes\Database\Staff;
9 use Bookit\Classes\Admin\SettingsController;
10 use Bookit\Helpers\AddonHelper;
11 use Bookit\Helpers\TimeSlotHelper;
12
13 class BookitController {
14
15 /**
16 * Bookit Calendar Frontend
17 */
18 public static function init() {
19 add_shortcode( 'bookit', array( self::class, 'render_shortcode' ) );
20 }
21
22 /**
23 * Enqueue Frontend Styles & Scripts
24 */
25 /**
26 * @param $settings
27 */
28 public static function enqueue_styles_scripts( $settings ) {
29 wp_enqueue_script( 'bookit-app', BOOKIT_URL . 'assets/dist/frontend/js/app.js', array(), BOOKIT_VERSION );
30
31 $paymentAddon = AddonHelper::getAddonDataByName( AddonHelper::$paymentAddon );
32
33 wp_enqueue_script( 'bookit-stripe-js', 'https://js.stripe.com/v3/', array(), false, false ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.NoExplicitVersion
34
35 $styles = BOOKIT_URL . 'assets/dist/frontend/css/app.css';
36
37 if ( 'true' == $settings['custom_colors_enabled'] ) {
38 $upload = wp_upload_dir();
39 $styles_path = $upload['basedir'] . '/bookit/app.css';
40 if ( file_exists( $styles_path ) ) {
41 $styles = $upload['baseurl'] . '/bookit/app.css';
42 }
43 }
44
45 wp_enqueue_style( 'bookit-app', $styles, array(), intval( get_option( 'bookit_styles_version', BOOKIT_VERSION ) ) );
46
47 $ajax_data = array(
48 'ajax_url' => admin_url( 'admin-ajax.php' ),
49 'translations' => Translations::get_frontend_translations(),
50 'nonces' => Nonces::get_frontend_nonces(),
51 );
52
53 wp_localize_script( 'bookit-app', 'bookit_window', $ajax_data );
54 }
55
56 /**
57 * Render Frontend Bookit Calendar
58 *
59 * @param bool $display
60 * @param bool $is_admin
61 * @param null $category_id
62 * @param null $service_id
63 * @param null $staff_id
64 *
65 * @return bool|string
66 */
67 public static function render_calendar( $display = false, $category_id = null, $service_id = null, $staff_id = null, $theme = null ) {
68
69 $paymentAddon = AddonHelper::getAddonDataByName( AddonHelper::$paymentAddon );
70 $bookitPaymentsActive = $paymentAddon['isCanUse'] ?? false;
71
72 $shortcodeAttributes = array(
73 'category_id' => $category_id,
74 'service_id' => $service_id,
75 'staff_id' => $staff_id,
76 );
77 $base_data = self::get_base_data_by_attributes( $shortcodeAttributes );
78 $categories = $base_data['categories'];
79 $services = $base_data['services'];
80 $staff = $base_data['staff'];
81 $settings = SettingsController::get_settings();
82 $settings['date_format'] = bookit_php_to_moment( get_option( 'date_format' ) );
83 $settings['time_format'] = bookit_php_to_moment( get_option( 'time_format' ) );
84 $settings['pro_active'] = bookit_pro_active();
85 $settings['payment_active'] = $bookitPaymentsActive ? true : false;
86 $user = ( is_user_logged_in() ) ? wp_get_current_user() : null;
87 $language = substr( get_bloginfo( 'language' ), 0, 2 );
88 $navigation = self::get_step_naviation();
89
90 $time_format = get_option( 'time_format' );
91 $service_start = 0;
92 $service_end = TimeSlotHelper::DAY_IN_SECONDS;
93 $time_slot_list = TimeSlotHelper::getTimeList( $service_start, $service_end );
94
95 if ( ! empty( $services ) ) {
96 foreach ( $services as &$service ) {
97 if ( is_array( $service ) ) {
98 $service['icon_url'] = ( ! empty( $service['icon_id'] ) ) ? wp_get_attachment_url( $service['icon_id'] ) : null;
99 }
100 }
101 }
102
103 if ( ! empty( $user->ID ) ) {
104 $user = (object) array_merge( (array) $user->data, array( 'customer' => Customers::get( 'wp_user_id', $user->ID ) ) );
105 }
106
107 if ( count( $categories ) <= 1 ) {
108 $key = array_search( 'category', array_column( $navigation, 'key' ) );
109 array_splice( $navigation, $key, 1 );
110 }
111
112 if ( ! empty( $service_id ) || ( count( $services ) == 1 && ( count( $categories ) == 1 || ! empty( $category_id ) ) ) ) {
113 $key = array_search( 'service', array_column( $navigation, 'key' ) );
114 array_splice( $navigation, $key, 1 );
115 }
116
117 self::enqueue_styles_scripts( $settings );
118
119 $data = array(
120 'attributes' => $shortcodeAttributes,
121 'categories' => $categories,
122 'services' => $services,
123 'staff' => $staff,
124 'settings' => $settings,
125 'user' => $user,
126 'language' => $language,
127 'slot_list' => $time_slot_list,
128 'navigation' => $navigation,
129 'theme' => $theme, // choosen in shortcode theme
130 'time_format' => $time_format,
131 );
132
133 return Template::load_template( 'bookit-calendar', $data, $display );
134 }
135
136 protected static function get_base_data_by_attributes( $shortcodeAttributes ) {
137 $result = array(
138 'staff' => array(),
139 'services' => array(),
140 'categories' => array(),
141 );
142
143 if ( ! empty( $shortcodeAttributes['staff_id'] ) ) {
144 $result['staff'] = Staff::get_one( $shortcodeAttributes['staff_id'] );
145 $result['services'] = Services::get_staff_services( $shortcodeAttributes['staff_id'] );
146 $result['categories'] = Categories::get_categories_by_ids( array_unique( array_column( $result['services'], 'category_id' ) ) );
147
148 } else {
149 $result['staff'] = Staff::get_all();
150 if ( ! empty( $shortcodeAttributes['service_id'] ) ) {
151 $service = Services::get( 'id', $shortcodeAttributes['service_id'] );
152 $result['services'] = array( $service );
153 $shortcodeAttributes['category_id'] = ( property_exists( $service, 'category_id' ) && ! empty( $service->category_id ) ) ? $service->category_id : null;
154 } else {
155 $result['services'] = Services::get_assigned_to_staff();
156 }
157
158 if ( ! empty( $shortcodeAttributes['category_id'] ) ) {
159 $result['categories']
160 = Categories::get_one( $shortcodeAttributes['category_id'] );
161 } else {
162 $result['categories'] = Categories::get_with_exist_services();
163 }
164 }
165
166 return $result;
167 }
168
169 /**
170 * @since 2.6.0 Prepend a login step for Registered mode when the visitor is logged out.
171 */
172 private static function get_step_naviation() {
173 $step_navigation = array();
174
175 $settings = SettingsController::get_settings();
176 if ( 'registered' == $settings['booking_type'] && ! is_user_logged_in() ) {
177 $step_navigation[] = array(
178 'key' => 'auth',
179 'menu' => __( 'Login', 'bookit' ),
180 'title' => __( 'Login', 'bookit' ),
181 'requiredFields' => array(),
182 'validation' => false,
183 );
184 }
185
186 $step_navigation = array_merge( $step_navigation, array(
187 array(
188 'key' => 'category',
189 'menu' => __( 'Category', 'bookit' ),
190 'title' => __( 'Select Category', 'bookit' ),
191 'requiredFields' => array(),
192 'validation' => false,
193 ),
194 array(
195 'key' => 'service',
196 'menu' => __( 'Service', 'bookit' ),
197 'title' => __( 'Select Service', 'bookit' ),
198 'requiredFields' => array( 'category_id' ),
199 'validation' => false,
200 ),
201 array(
202 'key' => 'dateTime',
203 'menu' => __( 'Date', 'bookit' ),
204 'title' => __( 'Select Time & Employee', 'bookit' ),
205 'requiredFields' => array( 'category_id', 'service_id' ),
206 'validation' => false,
207 ),
208 array(
209 'key' => 'detailsForm',
210 'menu' => __( 'Details', 'bookit' ),
211 'title' => __( 'Details', 'bookit' ),
212 'requiredFields' => array(
213 'service_id',
214 'staff_id',
215 'date_timestamp',
216 'start_time',
217 ),
218 'validation' => true,
219 ),
220 array(
221 'key' => 'payment',
222 'menu' => __( 'Payment', 'bookit' ),
223 'title' => __( 'Payment', 'bookit' ),
224 'requiredFields' => array(
225 'service_id',
226 'staff_id',
227 'date_timestamp',
228 'start_time',
229 'end_time',
230 'email',
231 'full_name',
232 ),
233 'validation' => false,
234 ),
235 array(
236 'key' => 'confirmation',
237 'menu' => __( 'Confirmation', 'bookit' ),
238 'title' => __( 'Confirmation', 'bookit' ),
239 'requiredFields' => array(
240 'service_id',
241 'staff_id',
242 'date_timestamp',
243 'start_time',
244 'end_time',
245 'email',
246 'full_name',
247 'payment_method',
248 ),
249 'validation' => true,
250 ), //todo
251 array(
252 'key' => 'result',
253 'requiredFields' => array(),
254 'validation' => false,
255 ),
256 ) );
257
258 return $step_navigation;
259 }
260
261 /**
262 * Shortcode
263 *
264 * @param $atts
265 *
266 * @return bool|string
267 */
268 public static function render_shortcode( $atts ) {
269 $atts = shortcode_atts(
270 array(
271 'category' => null,
272 'service' => null,
273 'staff' => null,
274 'theme' => null,
275 ),
276 $atts,
277 'bookit'
278 );
279 return self::render_calendar( false, $atts['category'], $atts['service'], $atts['staff'], $atts['theme'] );
280 }
281 }
282