PluginProbe ʕ •ᴥ•ʔ
LatePoint – Calendar Booking Plugin for Appointments and Events / 5.2.11
LatePoint – Calendar Booking Plugin for Appointments and Events v5.2.11
5.6.6 5.6.5 5.6.4 5.6.3 5.6.2 5.6.1 5.6.0 5.5.2 5.5.1 5.5.0 5.4.2 trunk 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.1.8 5.1.9 5.1.91 5.1.92 5.1.93 5.1.94 5.2.0 5.2.1 5.2.10 5.2.11 5.2.2 5.2.3 5.2.4 5.2.5 5.2.6 5.2.7 5.2.8 5.2.9 5.3.0 5.3.1 5.3.2 5.4.0 5.4.1
latepoint / lib / helpers / blocks_helper.php
latepoint / lib / helpers Last commit date
activities_helper.php 3 months ago agent_helper.php 3 months ago analytics_helper.php 4 months ago auth_helper.php 3 months ago blocks_helper.php 3 months ago booking_helper.php 3 months ago bricks_helper.php 3 months ago bundles_helper.php 3 months ago calendar_helper.php 3 months ago carts_helper.php 3 months ago connector_helper.php 3 months ago csv_helper.php 3 months ago customer_helper.php 3 months ago customer_import_helper.php 3 months ago database_helper.php 3 months ago debug_helper.php 3 months ago defaults_helper.php 3 months ago elementor_helper.php 3 months ago email_helper.php 3 months ago encrypt_helper.php 3 months ago events_helper.php 3 months ago form_helper.php 3 months ago icalendar_helper.php 3 months ago image_helper.php 3 months ago invoices_helper.php 3 months ago license_helper.php 3 months ago location_helper.php 3 months ago marketing_systems_helper.php 3 months ago meeting_systems_helper.php 3 months ago menu_helper.php 3 months ago meta_helper.php 3 months ago migrations_helper.php 3 months ago money_helper.php 3 months ago notifications_helper.php 3 months ago nps_survey_helper.php 3 months ago order_intent_helper.php 3 months ago orders_helper.php 3 months ago otp_helper.php 3 months ago pages_helper.php 3 months ago params_helper.php 3 months ago payments_helper.php 3 months ago price_breakdown_helper.php 3 months ago process_jobs_helper.php 3 months ago processes_helper.php 3 months ago replacer_helper.php 3 months ago resource_helper.php 3 months ago roles_helper.php 3 months ago router_helper.php 3 months ago service_helper.php 3 months ago sessions_helper.php 3 months ago settings_helper.php 3 months ago short_links_systems_helper.php 3 months ago shortcodes_helper.php 3 months ago sms_helper.php 3 months ago steps_helper.php 3 months ago stripe_connect_helper.php 3 months ago styles_helper.php 3 months ago support_topics_helper.php 3 months ago time_helper.php 3 months ago timeline_helper.php 3 months ago transaction_helper.php 3 months ago transaction_intent_helper.php 3 months ago util_helper.php 3 months ago version_specific_updates_helper.php 3 months ago whatsapp_helper.php 3 months ago work_periods_helper.php 3 months ago wp_datetime.php 3 months ago wp_user_helper.php 3 months ago
blocks_helper.php
513 lines
1 <?php
2
3 class OsBlockHelper {
4 public static function register_blocks() {
5 self::register_latepoint_category();
6 self::register_block_book_button();
7 self::register_block_book_form();
8 self::register_block_list_of_resources();
9 self::register_block_calendar();
10 self::register_block_customer_dashboard();
11 self::register_block_customer_login();
12 }
13
14 public static function localized_vars_for_blocks(): array {
15
16 $has_to_pick = [
17 'label' => __( 'Customer will pick', 'latepoint' ),
18 'value' => '',
19 ];
20 $localized_block_vars = [];
21
22 // AGENTS
23 $localized_block_vars['agents'] = [];
24 $localized_block_vars['selected_agents_options'][] = $has_to_pick;
25 $localized_block_vars['selected_agents_options'][] = [
26 'label' => __( 'Any Available', 'latepoint' ),
27 'value' => LATEPOINT_ANY_AGENT,
28 ];
29 $agents = new OsAgentModel();
30 $agents = $agents->get_results_as_models();
31 if ( $agents ) {
32 foreach ( $agents as $agent ) {
33 $localized_block_vars['selected_agents_options'][] = [
34 'label' => $agent->full_name,
35 'value' => $agent->id,
36 ];
37 $localized_block_vars['agents'][] = [
38 'name' => $agent->full_name,
39 'id' => $agent->id,
40 'title' => $agent->title,
41 'short_description' => $agent->short_description,
42 'avatar_url' => empty( $agent->avatar_image_id ) ? '' : $agent->get_avatar_url(),
43 ];
44 }
45 }
46
47 // SERVICES
48 $localized_block_vars['services'] = [];
49 $localized_block_vars['selected_services_options'][] = $has_to_pick;
50 $services = new OsServiceModel();
51 $services = $services->get_results_as_models();
52 if ( $services ) {
53 foreach ( $services as $service ) {
54 $localized_block_vars['selected_services_options'][] = [
55 'label' => $service->name,
56 'value' => $service->id,
57 ];
58 $localized_block_vars['services'][] = [
59 'name' => $service->name,
60 'id' => $service->id,
61 'image_url' => empty( $service->description_image_id ) ? '' : $service->get_description_image_url(),
62 'description' => $service->short_description,
63 'category_id' => $service->category_id,
64 'price_formatted' => ( $service->price_min > 0 ) ? ( ( $service->price_min != $service->price_max ) ? __( 'Starts at', 'latepoint' ) . ' ' . $service->price_min_formatted : $service->price_min_formatted ) : '',
65 ];
66 }
67 }
68
69
70 // SERVICE CATEGORIES
71 $localized_block_vars['service_categories'] = [];
72 $localized_block_vars['selected_service_categories_options'][] = [
73 'label' => __( 'Show All', 'latepoint' ),
74 'value' => '',
75 ];
76 $service_categories = new OsServiceCategoryModel();
77 $service_categories = $service_categories->get_results_as_models();
78 if ( $service_categories ) {
79 foreach ( $service_categories as $service_category ) {
80 $localized_block_vars['service_categories'][] = [
81 'name' => $service_category->name,
82 'id' => $service_category->id,
83 ];
84 $localized_block_vars['selected_service_categories_options'][] = [
85 'label' => $service_category->name,
86 'value' => $service_category->id,
87 ];
88 }
89 }
90
91
92 // LOCATIONS
93 $localized_block_vars['locations'] = [];
94 $localized_block_vars['selected_locations_options'][] = $has_to_pick;
95 $localized_block_vars['selected_locations_options'][] = [
96 'label' => __( 'Any Available', 'latepoint' ),
97 'value' => LATEPOINT_ANY_LOCATION,
98 ];
99 $locations = new OsLocationModel();
100 $locations = $locations->get_results_as_models();
101 if ( $locations ) {
102 foreach ( $locations as $location ) {
103 $localized_block_vars['selected_locations_options'][] = [
104 'label' => $location->name,
105 'value' => $location->id,
106 ];
107 $localized_block_vars['locations'][] = [
108 'name' => $location->name,
109 'id' => $location->id,
110 'category_id' => $location->category_id,
111 ];
112 }
113 }
114
115 $localized_block_vars['bundles'] = [];
116 $localized_block_vars['selected_bundles_options'][] = $has_to_pick;
117 $bundles = ( new OsBundleModel() )->should_be_active()->get_results_as_models();
118 if ( $bundles ) {
119 foreach ( $bundles as $bundle ) {
120 $localized_block_vars['selected_bundles_options'][] = [
121 'label' => $bundle->name,
122 'value' => $bundle->id,
123 ];
124 $localized_block_vars['bundles'][] = [
125 'name' => $bundle->name,
126 'id' => $bundle->id,
127 'price_formatted' => $bundle->get_formatted_charge_amount(),
128 'description' => $bundle->short_description,
129 ];
130 }
131 }
132
133 return $localized_block_vars;
134 }
135
136 public static function register_latepoint_category() {
137 add_filter(
138 'block_categories_all',
139 function ( $categories ) {
140 // Adding a new category.
141 $categories[] = [
142 'slug' => 'latepoint',
143 'title' => 'LatePoint',
144 ];
145 return $categories;
146 }
147 );
148 }
149
150 public static function register_block_book_button() {
151 register_block_type(
152 LATEPOINT_ABSPATH . 'blocks/build/book-button/block.json',
153 [
154 'render_callback' => 'OsBlockHelper::render_book_button',
155 'editor_script_handles' => [ 'latepoint-block-book-button' ],
156 ]
157 );
158 }
159
160 public static function register_block_book_form() {
161 register_block_type(
162 LATEPOINT_ABSPATH . 'blocks/build/book-form/block.json',
163 [
164 'render_callback' => 'OsBlockHelper::render_book_form',
165 'editor_script_handles' => [ 'latepoint-block-book-form' ],
166 ]
167 );
168 }
169
170 public static function register_block_list_of_resources() {
171 register_block_type(
172 LATEPOINT_ABSPATH . 'blocks/build/list-of-resources/block.json',
173 [
174 'render_callback' => 'OsBlockHelper::render_list_of_resources',
175 'editor_script_handles' => [ 'latepoint-block-list-of-resources' ],
176 ]
177 );
178 }
179
180
181 public static function register_block_calendar(): void {
182 register_block_type(
183 LATEPOINT_ABSPATH . 'blocks/build/calendar/block.json',
184 [
185 'render_callback' => 'OsBlockHelper::render_calendar',
186 'editor_script_handles' => [ 'latepoint-block-calendar' ],
187 ]
188 );
189 }
190
191 public static function register_block_customer_dashboard(): void {
192 register_block_type(
193 LATEPOINT_ABSPATH . 'blocks/build/customer-dashboard/block.json',
194 [
195 'render_callback' => 'OsBlockHelper::render_customer_dashboard',
196 'editor_script_handles' => [ 'latepoint-block-customer-dashboard' ],
197 ]
198 );
199 }
200
201 public static function register_block_customer_login(): void {
202 register_block_type(
203 LATEPOINT_ABSPATH . 'blocks/build/customer-login/block.json',
204 [
205 'render_callback' => 'OsBlockHelper::render_customer_login',
206 'editor_script_handles' => [ 'latepoint-block-customer-login' ],
207 ]
208 );
209 }
210
211
212 public static function render_book_button( $attributes, $content ) {
213 return do_shortcode( '[latepoint_book_button ' . self::attributes_to_data_params( $attributes ) . ']' );
214 }
215
216 public static function render_book_form( $attributes, $content ) {
217 return do_shortcode( '[latepoint_book_form ' . self::attributes_to_data_params( $attributes ) . ']' );
218 }
219
220 public static function add_block_styles_to_page() {
221 if ( is_page() && $styles = get_option( 'latepoint_blocks_styles_' . get_the_ID() ) ) {
222 wp_add_inline_style( 'latepoint-main-front', $styles );
223 }
224 }
225
226 public static function render_list_of_resources( $attributes, $content ) {
227 return do_shortcode( '[latepoint_resources ' . self::attributes_to_data_params( $attributes ) . ']' );
228 }
229
230 public static function render_calendar( $attributes, $content ) {
231 return do_shortcode( '[latepoint_calendar ' . self::attributes_to_data_params( $attributes ) . ']' );
232 }
233
234 public static function render_customer_dashboard( $attributes, $content ) {
235 return do_shortcode( '[latepoint_customer_dashboard ' . self::attributes_to_data_params( $attributes ) . ']' );
236 }
237
238 public static function render_customer_login( $attributes, $content ) {
239 return do_shortcode( '[latepoint_customer_login ' . self::attributes_to_data_params( $attributes ) . ']' );
240 }
241
242 /**
243 * Prepare data for shortcode
244 * @param array $settings
245 * @param array $allowed_params
246 *
247 * @return string
248 */
249 public static function attributes_to_data_params( array $settings, array $allowed_params = [] ): string {
250 $data_html = '';
251 foreach ( $settings as $key => $value ) {
252 if ( empty( $value ) ) {
253 continue;
254 }
255
256 if ( ! empty( $allowed_params ) && ! in_array( $key, $allowed_params ) ) {
257 continue;
258 }
259
260 if ( is_array( $value ) ) {
261 $value = implode( ',', $value );
262 }
263 if ( is_bool( $value ) ) {
264 $value = $value ? 'yes' : 'no';
265 }
266
267 $data_html .= $key . '="' . esc_attr( $value ) . '" ';
268 }
269
270 return $data_html;
271 }
272
273 /**
274 * When save page - save gutenberg blocks styles to options
275 * @param $page_id
276 * @return void
277 */
278 public static function save_blocks_styles( $page_id ): void {
279 if ( wp_is_post_revision( $page_id ) ) {
280 return;
281 }
282
283 $styles = '';
284
285 if ( has_block( 'latepoint/list-of-resources', $page_id ) ||
286 has_block( 'latepoint/book-button', $page_id )
287 ) {
288 $post_content = get_post_field( 'post_content', $page_id );
289 $blocks = parse_blocks( $post_content );
290 $styles = self::generate_styles_for_blocks( $blocks );
291 }
292
293 self::save_blocks_styles_in_options( $page_id, $styles );
294 }
295
296 private static function generate_styles_for_blocks( $blocks ): string {
297 $styles = '';
298
299 foreach ( $blocks as $block ) {
300 switch ( $block['blockName'] ) {
301 case 'latepoint/list-of-resources':
302 $styles .= self::generate_styles_for_list_of_resources( $block['attrs'] );
303 break;
304 case 'latepoint/book-button':
305 $styles .= self::generate_styles_for_book_button( $block['attrs'] );
306 break;
307 }
308
309 if ( ! empty( $block['innerBlocks'] ) ) {
310 $styles .= self::generate_styles_for_blocks( $block['innerBlocks'] );
311 }
312 }
313
314 return $styles;
315 }
316
317 /**
318 * Save gutenberg blocks styles in wp options. if styles is empty - delete option
319 * @param int $page_id
320 * @param string $styles
321 *
322 * @return void
323 */
324 private static function save_blocks_styles_in_options( int $page_id, string $styles = '' ): void {
325 $option_name = 'latepoint_blocks_styles_' . $page_id;
326 if ( trim( $styles ) == '' ) {
327 if ( get_option( $option_name ) ) {
328 delete_option( $option_name );
329 }
330 } else {
331 update_option( $option_name, $styles );
332 }
333 }
334
335 /**
336 * Styles for gutenberg block "Book Button"
337 * @param $attrs
338 *
339 * @return string
340 */
341 public static function generate_styles_for_book_button( $attrs ): string {
342 $styles = [];
343 if ( ! empty( $attrs['is_inherit'] ) ) {
344 return '';
345 }
346
347 $prefix = '.latepoint-book-button.latepoint-book-button-' . $attrs['id'];
348
349 $styles[ $prefix . ',' . $prefix . ':focus' ] = [
350 'font-weight' => $attrs['font_weight'] ?? '',
351 'text-transform' => $attrs['text_transform'] ?? '',
352 'font-family' => $attrs['font_family'] ?? '',
353 'line-height' => $attrs['line_height'] ?? '',
354 'letter-spacing' => $attrs['letter_spacing'] ?? '',
355 'border-color' => $attrs['border_color'] ?? '',
356 'border-width' => $attrs['border_width'] ?? '',
357 'border-style' => $attrs['border_style'] ?? '',
358 ];
359
360 if ( $styles[ $prefix ]['border-style'] == 'default' ) {
361 unset( $styles[ $prefix ]['border-style'] );
362 }
363
364 $styles[ $prefix . ':hover' ] = [
365 'background-color' => $attrs['bg_color_hover'] . ' !important' ?? '',
366 'color' => $attrs['text_color_hover'] . ' !important' ?? '',
367 'border-color' => $attrs['border_color_hover'] . ' !important' ?? '',
368 ];
369 return self::generate_css( $styles );
370 }
371
372 /**
373 * Styles for gutenberg block "List of Resources"
374 *
375 * @param array $attrs
376 * @return string
377 */
378 public static function generate_styles_for_list_of_resources( array $attrs ): string {
379
380 $styles = [];
381 if ( ! empty( $attrs['is_inherit'] ) ) {
382 return '';
383 }
384
385 $card_prefix = '.latepoint-resources-items-w .resource-item.resource-item-' . $attrs['id'];
386
387 $styles[ $card_prefix . ' .latepoint-book-button' ] = [
388 'background-color' => $attrs['button_bg_color'] ?? '',
389 'color' => $attrs['button_text_color'] ?? '',
390 'font-size' => $attrs['button_font_size'] ?? '',
391 'font-weight' => $attrs['font_weight'] ?? '',
392 'text-transform' => $attrs['text_transform'] ?? '',
393 'font-family' => $attrs['font_family'] ?? '',
394 'line-height' => $attrs['line_height'] ?? '',
395 'letter-spacing' => $attrs['letter_spacing'] ?? '',
396 'border-radius' => $attrs['button_border_radius'] ?? '',
397 'border-color' => $attrs['border_color'] ?? '',
398 'border-style' => $attrs['border_style'] ?? '',
399 'border-width' => $attrs['border_width'] ?? '',
400 'padding' => $attrs['padding'] ?? '',
401 ];
402
403 if ( $styles[ $card_prefix . ' .latepoint-book-button' ]['border-style'] == 'default' ) {
404 unset( $styles[ $card_prefix . ' .latepoint-book-button' ]['border-style'] );
405 }
406
407 $styles[ $card_prefix . ' .latepoint-book-button:hover' ] = [
408 'background-color' => $attrs['bg_color_hover'] ?? '',
409 'border-color' => $attrs['border_color_hover'] ?? '',
410 'color' => $attrs['text_color_hover'] ?? '',
411 ];
412 $styles[ $card_prefix ] = [
413 'padding' => $attrs['card_padding'] ?? '',
414 'box-shadow' => $attrs['card_box_shadow'] ?? '',
415 'border-style' => $attrs['card_border_style'] ?? '',
416 'border-radius' => $attrs['card_border_radius'] ?? '',
417 'border-width' => $attrs['card_border_width'] ?? '',
418 'border-color' => $attrs['card_border_color'] ?? '',
419 'background-color' => $attrs['card_bg_color'] ?? '',
420 ];
421 $styles[ $card_prefix . ' .ri-name > *' ] = [
422 'font-size' => $attrs['title_font_size'] ?? '',
423 'color' => $attrs['card_text_color'] ?? '',
424 ];
425 $styles[ $card_prefix . ':hover .ri-name > *' ] = [
426 'color' => $attrs['card_text_color_hover'] ?? '',
427 ];
428 $styles[ $card_prefix . ' .ri-price' ] = [
429 'font-size' => $attrs['price_font_size'] ?? '',
430 'color' => $attrs['card_price_color'] ?? '',
431 ];
432 $styles[ $card_prefix . ':hover .ri-price' ] = [
433 'color' => $attrs['card_price_color_hover'] ?? '',
434 ];
435 $styles[ $card_prefix . ' .ri-description' ] = [
436 'font-size' => $attrs['descr_font_size'] ?? '',
437 'color' => $attrs['card_descr_color'] ?? '',
438 ];
439 $styles[ $card_prefix . ':hover .ri-description' ] = [
440 'color' => $attrs['card_descr_color_hover'] ?? '',
441 ];
442
443 if ( $styles[ $card_prefix ]['card_border_style'] == 'default' ) {
444 unset( $styles[ $card_prefix ]['border-style'] );
445 }
446
447 $styles[ $card_prefix . ':hover' ] = [
448 'border-color' => $attrs['card_border_color_hover'] ?? '',
449 'background-color' => $attrs['card_bg_color_hover'] ?? '',
450 'color' => $attrs['card_text_color_hover'] ?? '',
451 ];
452 $styles[ $card_prefix . ':hover .ri-name h3' ] = [
453 'color' => $attrs['card_text_color_hover'] ?? '',
454 ];
455 $styles[ $card_prefix . ' .ri-name h3' ] = [
456 'color' => $attrs['card_text_color'] ?? '',
457 ];
458 return self::generate_css( $styles );
459 }
460
461 /**
462 * Convert array with styles to string
463 *
464 * @param array $selectors
465 * @return string
466 */
467 public static function generate_css( array $selectors ): string {
468 $styling_css = '';
469
470 foreach ( $selectors as $selector => $options ) {
471
472 $css = '';
473
474 foreach ( $options as $option_name => $option_val ) {
475 if ( empty( $option_val ) ) {
476 continue;
477 }
478 $css .= $option_name . ': ' . $option_val . ';';
479 }
480 if ( ! empty( $css ) ) {
481 $styling_css .= $selector . '{';
482 $styling_css .= $css . '}';
483 }
484 }
485
486 return $styling_css;
487 }
488
489 /**
490 * Retrieve Ids by resources
491 * @param string $resource
492 * @param array $settings
493 *
494 * @return string
495 */
496 public static function get_ids_from_resources( string $resource, array $settings ): array {
497 $resourceMap = [
498 'services' => 'item_ids_services',
499 'locations' => 'item_ids_locations',
500 'agents' => 'item_ids_agents',
501 'bundles' => 'item_ids_bundles',
502 ];
503
504 if ( ! array_key_exists( $resource, $resourceMap ) ) {
505 return [];
506 }
507
508 $idsKey = $resourceMap[ $resource ];
509
510 return ! empty( $settings[ $idsKey ] ) ? $settings[ $idsKey ] : [];
511 }
512 }
513