PluginProbe
MotoPress Hotel Booking for Divi / trunk
MotoPress Hotel Booking for Divi vtrunk
trunk 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.1.0 2.0.0
mphb-divi / src / renderers.php

renderers.php in MotoPress Hotel Booking for Divi trunk, at src/renderers.php

979 lines 30.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) {
4 exit;
5 }
6
7 use MPHB\Views\LoopRoomTypeView;
8 use MPHB\Views\SingleRoomTypeView;
9
10 if ( ! class_exists( 'MPHB_Divi_Renderers' ) ) {
11 class MPHB_Divi_Renderers {
12 private static $gallery_params = array();
13 private static $gallery_is_slider = false;
14 private static $attributes_custom = array();
15 private static $attributes_hidden = array();
16 private static $attributes_removed_actions = array();
17 private static $attribute_removed_actions = array();
18 private static $attribute_actions = array(
19 'adults' => array(
20 'label' => array(
21 'mphb_render_single_room_type_before_adults' => '_renderAdultsTitle',
22 ),
23 'default' => array(
24 'mphb_render_single_room_type_before_adults' => '_renderAdultsListItemOpen',
25 'mphb_render_single_room_type_after_adults' => '_renderAdultsListItemClose',
26 ),
27 ),
28 'children' => array(
29 'label' => array(
30 'mphb_render_single_room_type_before_children' => '_renderChildrenTitle',
31 ),
32 'default' => array(
33 'mphb_render_single_room_type_before_children' => '_renderChildrenListItemOpen',
34 'mphb_render_single_room_type_after_children' => '_renderChildrenListItemClose',
35 ),
36 ),
37 'capacity' => array(
38 'label' => array(
39 'mphb_render_single_room_type_before_total_capacity' => '_renderTotalCapacityTitle',
40 ),
41 'default' => array(
42 'mphb_render_single_room_type_before_total_capacity' => '_renderTotalCapacityListItemOpen',
43 'mphb_render_single_room_type_after_total_capacity' => '_renderTotalCapacityListItemClose',
44 ),
45 ),
46 'amenities' => array(
47 'label' => array(
48 'mphb_render_single_room_type_before_facilities' => '_renderFacilitiesTitle',
49 ),
50 'default' => array(
51 'mphb_render_single_room_type_before_facilities' => '_renderFacilitiesListItemOpen',
52 'mphb_render_single_room_type_after_facilities' => '_renderFacilitiesListItemClose',
53 ),
54 ),
55 'view' => array(
56 'label' => array(
57 'mphb_render_single_room_type_before_view' => '_renderViewTitle',
58 ),
59 'default' => array(
60 'mphb_render_single_room_type_before_view' => '_renderViewListItemOpen',
61 'mphb_render_single_room_type_after_view' => '_renderViewListItemClose',
62 ),
63 ),
64 'size' => array(
65 'label' => array(
66 'mphb_render_single_room_type_before_size' => '_renderSizeTitle',
67 ),
68 'default' => array(
69 'mphb_render_single_room_type_before_size' => '_renderSizeListItemOpen',
70 'mphb_render_single_room_type_after_size' => '_renderSizeListItemClose',
71 ),
72 ),
73 'bed-types' => array(
74 'label' => array(
75 'mphb_render_single_room_type_before_bed_type' => '_renderBedTypeTitle',
76 ),
77 'default' => array(
78 'mphb_render_single_room_type_before_bed_type' => '_renderBedTypeListItemOpen',
79 'mphb_render_single_room_type_after_bed_type' => '_renderBedTypeListItemClose',
80 ),
81 ),
82 'categories' => array(
83 'label' => array(
84 'mphb_render_single_room_type_before_categories' => '_renderCategoriesTitle',
85 ),
86 'default' => array(
87 'mphb_render_single_room_type_before_categories' => '_renderCategoriesListItemOpen',
88 'mphb_render_single_room_type_after_categories' => '_renderCategoriesListItemClose',
89 ),
90 ),
91 'custom' => array(
92 'label' => array(
93 'mphb_render_single_room_type_before_custom_attribute' => '_renderCustomAttributesTitle',
94 ),
95 'default' => array(
96 'mphb_render_single_room_type_before_custom_attribute' => '_renderCustomAttributesListItemOpen',
97 'mphb_render_single_room_type_after_custom_attribute' => '_renderCustomAttributesListItemClose',
98 ),
99 ),
100 );
101 private static $attributes_setting_to_action = array(
102 'adults' => array( 'renderAdults' ),
103 'children' => array( 'renderChildren' ),
104 'capacity' => array( 'renderTotalCapacity' ),
105 'amenities' => array( 'renderFacilities' ),
106 'view' => array( 'renderView' ),
107 'size' => array( 'renderSize' ),
108 'bed-types' => array( 'renderBedType' ),
109 'categories' => array( 'renderCategories' ),
110 );
111
112 public static function search_availability( $args = array() ) {
113 $defaults = array(
114 'check_in_date' => '',
115 'check_out_date' => '',
116 'adults' => '',
117 'children' => '',
118 'attributes' => '',
119 'form_style' => 'default',
120 'class' => '',
121 );
122
123 $args = wp_parse_args( $args, $defaults );
124
125 if ( 'default' !== $args['form_style'] ) {
126 $args['class'] .= ' ' . $args['form_style'];
127 }
128
129 return do_shortcode(
130 self::build_shortcode(
131 'mphb_availability_search',
132 array(
133 'attributes' => $args['attributes'],
134 'check_in_date' => $args['check_in_date'],
135 'check_out_date' => $args['check_out_date'],
136 'adults' => $args['adults'],
137 'children' => $args['children'],
138 'class' => trim( $args['class'] ),
139 )
140 )
141 );
142 }
143
144 public static function rooms( $args = array() ) {
145 $defaults = array(
146 'title' => '',
147 'featured_image' => '',
148 'gallery' => '',
149 'excerpt' => '',
150 'details' => '',
151 'price' => '',
152 'view_button' => '',
153 'book_button' => '',
154 'ids' => '',
155 'category' => '',
156 'tags' => '',
157 'relation' => '',
158 'posts_per_page' => '',
159 'orderby' => '',
160 'order' => '',
161 'meta_key' => '',
162 'meta_value' => '',
163 'class' => '',
164 );
165
166 $args = wp_parse_args( $args, $defaults );
167
168 return do_shortcode( self::build_shortcode( 'mphb_rooms', $args ) );
169 }
170
171 public static function single_accommodation( $args = array() ) {
172 $defaults = array(
173 'title' => '',
174 'featured_image' => '',
175 'gallery' => '',
176 'excerpt' => '',
177 'details' => '',
178 'price' => '',
179 'view_button' => '',
180 'book_button' => '',
181 'id' => '',
182 'class' => '',
183 );
184
185 $args = wp_parse_args( $args, $defaults );
186
187 if ( '' === $args['id'] ) {
188 return esc_html__( 'Please insert accommodation id.', 'mphb-divi' );
189 }
190
191 return do_shortcode( self::build_shortcode( 'mphb_room', $args ) );
192 }
193
194 public static function services( $args = array() ) {
195 $defaults = array(
196 'ids' => '',
197 'class' => '',
198 'posts_per_page' => '',
199 'orderby' => '',
200 'order' => '',
201 'meta_key' => '',
202 'meta_type' => '',
203 );
204
205 $args = wp_parse_args( $args, $defaults );
206
207 return do_shortcode( self::build_shortcode( 'mphb_services', $args ) );
208 }
209
210 public static function rates( $args = array() ) {
211 $defaults = array(
212 'id' => '',
213 'class' => '',
214 );
215
216 $args = wp_parse_args( $args, $defaults );
217
218 if ( '' === $args['id'] ) {
219 return esc_html__( 'Please insert accommodation id.', 'mphb-divi' );
220 }
221
222 return do_shortcode( self::build_shortcode( 'mphb_rates', $args ) );
223 }
224
225 public static function booking_form( $args = array() ) {
226 $defaults = array(
227 'id' => '',
228 'class' => '',
229 'form_style' => 'default',
230 );
231
232 $args = wp_parse_args( $args, $defaults );
233
234 if ( 'default' !== $args['form_style'] ) {
235 $args['class'] .= ' ' . $args['form_style'];
236 }
237
238 if ( '' === $args['id'] ) {
239 return esc_html__( 'Please insert accommodation id.', 'mphb-divi' );
240 }
241
242 return do_shortcode(
243 self::build_shortcode(
244 'mphb_availability',
245 array(
246 'class' => trim( $args['class'] ),
247 'id' => $args['id'],
248 )
249 )
250 );
251 }
252
253 public static function availability_calendar( $args = array() ) {
254 $defaults = array(
255 'id' => '',
256 'monthstoshow' => '2,3',
257 'display_price' => false,
258 'truncate_price' => true,
259 'display_currency' => false,
260 'class' => '',
261 );
262
263 $args = wp_parse_args( $args, $defaults );
264
265 if ( '' === $args['id'] ) {
266 return esc_html__( 'Please insert accommodation id.', 'mphb-divi' );
267 }
268
269 return do_shortcode(
270 self::build_shortcode(
271 'mphb_availability_calendar',
272 array(
273 'class' => $args['class'],
274 'id' => $args['id'],
275 'monthstoshow' => $args['monthstoshow'],
276 'display_price' => 'on' === $args['display_price'] ? 1 : 0,
277 'truncate_price' => 'on' === $args['truncate_price'] ? 1 : 0,
278 'display_currency' => 'on' === $args['display_currency'] ? 1 : 0,
279 )
280 )
281 );
282 }
283
284 public static function booking_confirmation( $args = array() ) {
285 $args = wp_parse_args( $args, array( 'class' => '' ) );
286 return do_shortcode( self::build_shortcode( 'mphb_booking_confirmation', $args ) );
287 }
288
289 public static function checkout( $args = array() ) {
290 $args = wp_parse_args( $args, array( 'class' => '' ) );
291 return do_shortcode( self::build_shortcode( 'mphb_checkout', $args ) );
292 }
293
294 public static function search_results( $args = array() ) {
295 $defaults = array(
296 'title' => '',
297 'featured_image' => '',
298 'gallery' => '',
299 'excerpt' => '',
300 'details' => '',
301 'price' => '',
302 'view_button' => '',
303 'book_button' => '',
304 'orderby' => '',
305 'order' => '',
306 'meta_key' => '',
307 'meta_type' => '',
308 'default_sorting' => '',
309 'class' => '',
310 );
311
312 $args = wp_parse_args( $args, $defaults );
313
314 return do_shortcode( self::build_shortcode( 'mphb_search_results', $args ) );
315 }
316
317 public static function accommodation_title( $attrs = array() ) {
318 $id = self::resolve_accommodation_id( $attrs );
319
320 if ( ! self::is_valid_room_type( $id ) ) {
321 return '';
322 }
323
324 $link_to_post = isset( $attrs['link_to_post'] ) && 'on' === $attrs['link_to_post'];
325
326 if ( $link_to_post ) {
327 add_action( 'mphb_render_single_room_type_before_title', array( __CLASS__, 'render_link_open' ), 15 );
328 add_action( 'mphb_render_single_room_type_after_title', array( __CLASS__, 'render_link_close' ), 5 );
329 }
330
331 try {
332 return self::render_room_type_query(
333 $id,
334 static function () {
335 SingleRoomTypeView::renderTitle();
336 }
337 );
338 } finally {
339 if ( $link_to_post ) {
340 remove_action( 'mphb_render_single_room_type_before_title', array( __CLASS__, 'render_link_open' ), 15 );
341 remove_action( 'mphb_render_single_room_type_after_title', array( __CLASS__, 'render_link_close' ), 5 );
342 }
343 }
344 }
345
346 public static function accommodation_featured_image( $attrs = array() ) {
347 $id = self::resolve_accommodation_id( $attrs );
348
349 if ( ! self::is_valid_room_type( $id ) ) {
350 return '';
351 }
352
353 $link_to_post = isset( $attrs['link_to_post'] ) && 'on' === $attrs['link_to_post'];
354 $size = isset( $attrs['image_size'] ) ? $attrs['image_size'] : 'large';
355
356 return self::capture(
357 static function () use ( $id, $link_to_post, $size ) {
358 ?>
359 <div class="mphb-single-room-type-thumbnail">
360 <?php if ( $link_to_post ) : ?>
361 <a href="<?php echo esc_url( get_permalink( $id ) ); ?>">
362 <?php endif; ?>
363 <?php
364 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
365 echo mphb_tmpl_get_room_type_image( $id, $size );
366 ?>
367 <?php if ( $link_to_post ) : ?>
368 </a>
369 <?php endif; ?>
370 </div>
371 <?php
372 }
373 );
374 }
375
376 public static function accommodation_content( $attrs = array() ) {
377 $id = self::resolve_accommodation_id( $attrs );
378
379 if ( ! self::is_valid_room_type( $id ) ) {
380 return '';
381 }
382
383 $query = new WP_Query(
384 array(
385 'p' => $id,
386 'post_type' => 'mphb_room_type',
387 )
388 );
389
390 $output = self::capture(
391 static function () use ( $query ) {
392 if ( $query->have_posts() ) {
393 while ( $query->have_posts() ) {
394 $query->the_post();
395 the_content();
396 }
397 }
398 }
399 );
400
401 wp_reset_postdata();
402
403 return $output;
404 }
405
406 public static function accommodation_price( $attrs = array() ) {
407 $id = self::resolve_accommodation_id( $attrs );
408
409 if ( ! self::is_valid_room_type( $id ) ) {
410 return '';
411 }
412
413 return self::with_room_type(
414 $id,
415 static function () {
416 return self::capture(
417 static function () {
418 SingleRoomTypeView::renderDefaultOrForDatesPrice();
419 }
420 );
421 }
422 );
423 }
424
425 public static function accommodation_gallery( $attrs = array() ) {
426 $id = self::resolve_accommodation_id( $attrs );
427
428 if ( ! self::is_valid_room_type( $id ) ) {
429 return '';
430 }
431
432 $defaults = array(
433 'link_to' => '',
434 'columns' => '4',
435 'image_size' => '',
436 'is_lightbox' => '',
437 );
438
439 self::$gallery_params = wp_parse_args( $attrs, $defaults );
440 self::$gallery_is_slider = isset( $attrs['is_slider'] ) && 'on' === $attrs['is_slider'];
441
442 return self::with_room_type(
443 $id,
444 static function () {
445 self::apply_gallery_filters();
446
447 try {
448 return self::capture(
449 static function () {
450 if ( self::$gallery_is_slider ) {
451 LoopRoomTypeView::renderGallery();
452 } else {
453 SingleRoomTypeView::renderGallery();
454 }
455 }
456 );
457 } finally {
458 self::restore_gallery_filters();
459 }
460 }
461 );
462 }
463
464 public static function accommodation_attributes( $attrs = array() ) {
465 $id = self::resolve_accommodation_id( $attrs );
466
467 if ( ! self::is_valid_room_type( $id ) ) {
468 return '';
469 }
470
471 $available_attributes = array_keys( self::get_accommodation_attributes() );
472 $selected_attributes = ! empty( $attrs['selected_attributes'] ) ? explode( ',', $attrs['selected_attributes'] ) : $available_attributes;
473 self::$attributes_hidden = array_diff( $available_attributes, $selected_attributes );
474 self::$attributes_removed_actions = array();
475
476 return self::with_room_type(
477 $id,
478 static function () {
479 self::apply_attributes_filters();
480
481 try {
482 return self::capture(
483 static function () {
484 SingleRoomTypeView::renderAttributes();
485 }
486 );
487 } finally {
488 self::restore_attributes_filters();
489 }
490 }
491 );
492 }
493
494 public static function accommodation_attribute( $attrs = array() ) {
495 $id = self::resolve_accommodation_id( $attrs );
496
497 if ( ! self::is_valid_room_type( $id ) ) {
498 return '';
499 }
500
501 $attribute = isset( $attrs['selected_attribute'] ) ? $attrs['selected_attribute'] : 'adults';
502 $show_label = isset( $attrs['show_label'] ) && in_array( $attrs['show_label'], array( 'on', 'yes' ), true );
503 self::$attribute_removed_actions = array();
504
505 return self::with_room_type(
506 $id,
507 static function () use ( $attribute, $show_label ) {
508 return self::capture(
509 static function () use ( $attribute, $show_label ) {
510 self::render_attribute( $attribute, $show_label );
511 }
512 );
513 }
514 );
515 }
516
517 public static function render_link_open() {
518 ?>
519 <a href="<?php the_permalink(); ?>">
520 <?php
521 }
522
523 public static function render_link_close() {
524 ?>
525 </a>
526 <?php
527 }
528
529 public static function filter_gallery_link( $link ) {
530 if ( ! empty( self::$gallery_params['link_to'] ) ) {
531 $link = self::$gallery_params['link_to'];
532 }
533
534 if ( self::$gallery_is_slider ) {
535 $link = 'none';
536 }
537
538 return $link;
539 }
540
541 public static function filter_gallery_columns( $columns ) {
542 if ( ! empty( self::$gallery_params['columns'] ) ) {
543 $columns = self::$gallery_params['columns'];
544 }
545
546 return $columns;
547 }
548
549 public static function filter_gallery_image_size( $size ) {
550 if ( ! empty( self::$gallery_params['image_size'] ) ) {
551 return self::$gallery_params['image_size'];
552 }
553
554 return $size;
555 }
556
557 public static function filter_gallery_lightbox( $lightbox ) {
558 if ( isset( self::$gallery_params['is_lightbox'] ) && '' !== self::$gallery_params['is_lightbox'] ) {
559 return 'on' === self::$gallery_params['is_lightbox'];
560 }
561
562 return $lightbox;
563 }
564
565 public static function filter_gallery_nav_slider() {
566 return false;
567 }
568
569 public static function remove_default_slider_wrapper() {
570 remove_action( 'mphb_render_loop_room_type_before_gallery', array( '\MPHB\Views\LoopRoomTypeView', '_renderImagesWrapperOpen' ), 10 );
571 remove_action( 'mphb_render_loop_room_type_after_gallery', array( '\MPHB\Views\LoopRoomTypeView', '_renderImagesWrapperClose' ), 20 );
572 }
573
574 public static function filter_slider_classes( $slider_class ) {
575 return 'mphb-flexslider-gallery-wrapper';
576 }
577
578 public static function render_slider_wrapper_open() {
579 ?>
580 <div class="mphb-room-type-gallery-wrapper mphb-single-room-type-gallery-wrapper">
581 <?php
582 }
583
584 public static function render_slider_wrapper_close() {
585 ?>
586 </div>
587 <?php
588 }
589
590 public static function filter_slider_attributes( $atts ) {
591 $atts['minItems'] = 1;
592 $atts['maxItems'] = (int) self::$gallery_params['columns'] ? (int) self::$gallery_params['columns'] : 1;
593 $atts['move'] = 1;
594 $atts['itemWidth'] = floor( 100 / $atts['maxItems'] );
595
596 return $atts;
597 }
598
599 public static function remove_attributes_title() {
600 $title_priority = has_action( 'mphb_render_single_room_type_before_attributes', array( '\MPHB\Views\SingleRoomTypeView', '_renderAttributesTitle' ) );
601
602 if ( $title_priority ) {
603 remove_action( 'mphb_render_single_room_type_before_attributes', array( '\MPHB\Views\SingleRoomTypeView', '_renderAttributesTitle' ), $title_priority );
604 }
605
606 remove_action( 'mphb_render_single_room_type_before_attributes', array( __CLASS__, 'remove_attributes_title' ), 0 );
607 }
608
609 public static function filter_attributes() {
610 foreach ( self::$attributes_setting_to_action as $setting => $actions ) {
611 if ( ! in_array( $setting, self::$attributes_hidden, true ) ) {
612 continue;
613 }
614
615 foreach ( $actions as $action ) {
616 $priority = has_action( 'mphb_render_single_room_type_attributes', array( '\MPHB\Views\SingleRoomTypeView', $action ) );
617
618 if ( $priority ) {
619 remove_action( 'mphb_render_single_room_type_attributes', array( '\MPHB\Views\SingleRoomTypeView', $action ), $priority );
620 self::$attributes_removed_actions[ $action ] = $priority;
621 }
622 }
623 }
624 }
625
626 private static function build_shortcode( $tag, $attrs ) {
627 $parts = array();
628
629 foreach ( $attrs as $name => $value ) {
630 $parts[] = sprintf( '%s="%s"', $name, esc_attr( (string) $value ) );
631 }
632
633 return '[' . $tag . ' ' . implode( ' ', $parts ) . ']';
634 }
635
636 private static function resolve_accommodation_id( $attrs ) {
637 if ( isset( $attrs['accommodation_id'] ) && 'current' !== $attrs['accommodation_id'] ) {
638 return (int) $attrs['accommodation_id'];
639 }
640
641 return self::current_post_id();
642 }
643
644 private static function current_post_id() {
645 $post_id = get_the_ID();
646
647 if ( $post_id ) {
648 return (int) $post_id;
649 }
650
651 $post_id = get_queried_object_id();
652
653 if ( $post_id ) {
654 return (int) $post_id;
655 }
656
657 global $post;
658
659 return isset( $post->ID ) ? (int) $post->ID : 0;
660 }
661
662 private static function is_valid_room_type( $id ) {
663 return $id && 'mphb_room_type' === get_post_type( $id );
664 }
665
666 private static function capture( $callback ) {
667 ob_start();
668 $callback();
669 return ob_get_clean();
670 }
671
672 private static function with_room_type( $id, $callback ) {
673 $current_room_type = MPHB()->getCurrentRoomType();
674 $fallback_id = self::current_post_id();
675
676 MPHB()->setCurrentRoomType( $id );
677
678 try {
679 return $callback();
680 } finally {
681 MPHB()->setCurrentRoomType( $current_room_type ? $current_room_type->getId() : $fallback_id );
682 }
683 }
684
685 private static function render_room_type_query( $id, $callback ) {
686 $query = new WP_Query(
687 array(
688 'p' => $id,
689 'post_type' => 'mphb_room_type',
690 )
691 );
692
693 $output = self::capture(
694 static function () use ( $query, $callback ) {
695 if ( $query->have_posts() ) {
696 while ( $query->have_posts() ) {
697 $query->the_post();
698 $callback();
699 }
700 }
701 }
702 );
703
704 wp_reset_postdata();
705
706 return $output;
707 }
708
709 private static function get_accommodation_attributes() {
710 // phpcs:disable WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase
711 global $mphbAttributes; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase.Found
712
713 $attributes = array(
714 'adults' => __( 'Adults', 'mphb-divi' ),
715 'children' => __( 'Children', 'mphb-divi' ),
716 'capacity' => __( 'Capacity', 'mphb-divi' ),
717 'amenities' => __( 'Amenities', 'mphb-divi' ),
718 'view' => __( 'View', 'mphb-divi' ),
719 'size' => __( 'Size', 'mphb-divi' ),
720 'bed-types' => __( 'Bed Types', 'mphb-divi' ),
721 'categories' => __( 'Categories', 'mphb-divi' ),
722 );
723
724 foreach ( (array) $mphbAttributes as $custom_attribute ) {
725 $attributes[ $custom_attribute['attributeName'] ] = $custom_attribute['title'];
726 }
727 // phpcs:enable WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase
728
729 return $attributes;
730 }
731
732 private static function apply_gallery_filters() {
733 if ( self::$gallery_is_slider ) {
734 add_filter( 'mphb_loop_room_type_gallery_main_slider_image_link', array( __CLASS__, 'filter_gallery_link' ) );
735 add_filter( 'mphb_loop_room_type_gallery_main_slider_columns', array( __CLASS__, 'filter_gallery_columns' ) );
736 add_filter( 'mphb_loop_room_type_gallery_main_slider_image_size', array( __CLASS__, 'filter_gallery_image_size' ) );
737 add_filter( 'mphb_loop_room_type_gallery_use_nav_slider', array( __CLASS__, 'filter_gallery_nav_slider' ) );
738 add_action( 'mphb_render_loop_room_type_before_gallery', array( __CLASS__, 'remove_default_slider_wrapper' ), 1 );
739 add_action( 'mphb_render_loop_room_type_before_gallery', array( __CLASS__, 'render_slider_wrapper_open' ) );
740 add_action( 'mphb_render_loop_room_type_after_gallery', array( __CLASS__, 'render_slider_wrapper_close' ) );
741 add_filter( 'mphb_loop_room_type_gallery_main_slider_wrapper_class', array( __CLASS__, 'filter_slider_classes' ) );
742 add_filter( 'mphb_loop_room_type_gallery_main_slider_flexslider_options', array( __CLASS__, 'filter_slider_attributes' ) );
743 } else {
744 add_filter( 'mphb_single_room_type_gallery_image_link', array( __CLASS__, 'filter_gallery_link' ) );
745 add_filter( 'mphb_single_room_type_gallery_columns', array( __CLASS__, 'filter_gallery_columns' ) );
746 add_filter( 'mphb_single_room_type_gallery_image_size', array( __CLASS__, 'filter_gallery_image_size' ) );
747 add_filter( 'mphb_single_room_type_gallery_use_magnific', array( __CLASS__, 'filter_gallery_lightbox' ) );
748 }
749 }
750
751 private static function restore_gallery_filters() {
752 if ( self::$gallery_is_slider ) {
753 remove_filter( 'mphb_loop_room_type_gallery_main_slider_image_link', array( __CLASS__, 'filter_gallery_link' ) );
754 remove_filter( 'mphb_loop_room_type_gallery_main_slider_columns', array( __CLASS__, 'filter_gallery_columns' ) );
755 remove_filter( 'mphb_loop_room_type_gallery_main_slider_image_size', array( __CLASS__, 'filter_gallery_image_size' ) );
756 remove_filter( 'mphb_loop_room_type_gallery_use_nav_slider', array( __CLASS__, 'filter_gallery_nav_slider' ) );
757 remove_action( 'mphb_render_loop_room_type_before_gallery', array( __CLASS__, 'remove_default_slider_wrapper' ), 1 );
758 remove_action( 'mphb_render_loop_room_type_before_gallery', array( __CLASS__, 'render_slider_wrapper_open' ) );
759 remove_action( 'mphb_render_loop_room_type_after_gallery', array( __CLASS__, 'render_slider_wrapper_close' ) );
760 remove_filter( 'mphb_loop_room_type_gallery_main_slider_wrapper_class', array( __CLASS__, 'filter_slider_classes' ) );
761 remove_filter( 'mphb_loop_room_type_gallery_main_slider_flexslider_options', array( __CLASS__, 'filter_slider_attributes' ) );
762 } else {
763 remove_filter( 'mphb_single_room_type_gallery_image_link', array( __CLASS__, 'filter_gallery_link' ) );
764 remove_filter( 'mphb_single_room_type_gallery_columns', array( __CLASS__, 'filter_gallery_columns' ) );
765 remove_filter( 'mphb_single_room_type_gallery_image_size', array( __CLASS__, 'filter_gallery_image_size' ) );
766 remove_filter( 'mphb_single_room_type_gallery_use_magnific', array( __CLASS__, 'filter_gallery_lightbox' ) );
767 }
768
769 self::$gallery_params = array();
770 self::$gallery_is_slider = false;
771 }
772
773 private static function apply_attributes_filters() {
774 add_action( 'mphb_render_single_room_type_before_attributes', array( __CLASS__, 'remove_attributes_title' ), 0 );
775 add_action( 'mphb_render_single_room_type_before_attributes', array( __CLASS__, 'filter_attributes' ) );
776
777 // phpcs:disable WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase
778 global $mphbAttributes;
779 self::$attributes_custom = $mphbAttributes;
780
781 foreach ( self::$attributes_custom as $slug => $attribute ) {
782 if ( in_array( $slug, self::$attributes_hidden, true ) ) {
783 $mphbAttributes[ $slug ]['visible'] = false;
784 }
785 }
786 // phpcs:enable WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase
787 }
788
789 private static function restore_attributes_filters() {
790 foreach ( self::$attributes_removed_actions as $action => $priority ) {
791 add_action( 'mphb_render_single_room_type_attributes', array( '\MPHB\Views\SingleRoomTypeView', $action ), $priority );
792 }
793
794 // phpcs:disable WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase
795 global $mphbAttributes; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase.Found
796 $mphbAttributes = self::$attributes_custom;
797 // phpcs:enable WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase
798
799 remove_action( 'mphb_render_single_room_type_before_attributes', array( __CLASS__, 'remove_attributes_title' ), 0 );
800 remove_action( 'mphb_render_single_room_type_before_attributes', array( __CLASS__, 'filter_attributes' ) );
801
802 self::$attributes_custom = array();
803 self::$attributes_hidden = array();
804 self::$attributes_removed_actions = array();
805 }
806
807 private static function render_attribute( $attribute, $show_label ) {
808 $custom_attribute = '';
809 $all_attributes = self::get_accommodation_attributes();
810
811 // phpcs:disable WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase
812 global $mphbAttributes; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase.Found
813 $original_attributes = $mphbAttributes;
814 // phpcs:enable WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase
815
816 if ( isset( $all_attributes[ $attribute ] ) && ! isset( self::$attribute_actions[ $attribute ] ) ) {
817 $custom_attribute = $attribute;
818 $attribute = 'custom';
819 }
820
821 // phpcs:ignore WordPress.NamingConventions
822 do_action( 'mphb-templates/blocks/attribute/before', $attribute, $custom_attribute );
823
824 try {
825 self::before_attribute_render( $attribute, $show_label, $custom_attribute );
826
827 switch ( $attribute ) {
828 case 'adults':
829 self::render_attribute_wrap(
830 'mphb-room-type-adults-capacity',
831 static function () {
832 SingleRoomTypeView::renderAdults();
833 }
834 );
835 break;
836 case 'children':
837 self::render_attribute_wrap(
838 'mphb-room-type-children-capacity',
839 static function () {
840 SingleRoomTypeView::renderChildren();
841 }
842 );
843 break;
844 case 'capacity':
845 self::render_attribute_wrap(
846 'mphb-room-type-total-capacity',
847 static function () {
848 SingleRoomTypeView::renderTotalCapacity();
849 }
850 );
851 break;
852 case 'amenities':
853 self::render_attribute_wrap(
854 'mphb-room-type-facilities',
855 static function () {
856 SingleRoomTypeView::renderFacilities();
857 }
858 );
859 break;
860 case 'view':
861 self::render_attribute_wrap(
862 'mphb-room-type-view',
863 static function () {
864 SingleRoomTypeView::renderView();
865 }
866 );
867 break;
868 case 'size':
869 self::render_attribute_wrap(
870 'mphb-room-type-size',
871 static function () {
872 SingleRoomTypeView::renderSize();
873 }
874 );
875 break;
876 case 'bed-types':
877 self::render_attribute_wrap(
878 'mphb-room-type-bed-type',
879 static function () {
880 SingleRoomTypeView::renderBedType();
881 }
882 );
883 break;
884 case 'categories':
885 self::render_attribute_wrap(
886 'mphb-room-type-categories',
887 static function () {
888 SingleRoomTypeView::renderCategories();
889 }
890 );
891 break;
892 case 'custom':
893 self::render_attribute_wrap(
894 'mphb-room-type-' . $custom_attribute . ' mphb-room-type-custom-attribute',
895 static function () {
896 SingleRoomTypeView::renderCustomAttributes();
897 }
898 );
899 break;
900 default:
901 ?>
902 <div class="mphb-single-room-type-attribute mphb-room-type-undefined-attribute">
903 <?php esc_html_e( 'Please choose an attribute from available ones.', 'mphb-divi' ); ?>
904 </div>
905 <?php
906 break;
907 }
908 } finally {
909 // phpcs:ignore WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase
910 $mphbAttributes = $original_attributes;
911 self::after_attribute_render();
912 }
913
914 // phpcs:ignore WordPress.NamingConventions
915 do_action( 'mphb-templates/blocks/attribute/after', $attribute, $custom_attribute );
916 }
917
918 private static function render_attribute_wrap( $class_name, $callback ) {
919 ?>
920 <div class="mphb-single-room-type-attribute <?php echo esc_attr( $class_name ); ?>">
921 <?php $callback(); ?>
922 </div>
923 <?php
924 }
925
926 private static function before_attribute_render( $attribute, $show_label, $custom_attribute ) {
927 if ( ! isset( self::$attribute_actions[ $attribute ] ) ) {
928 return;
929 }
930
931 foreach ( self::$attribute_actions[ $attribute ]['default'] as $action => $callback ) {
932 $priority = has_action( $action, array( '\MPHB\Views\SingleRoomTypeView', $callback ) );
933
934 if ( $priority ) {
935 remove_action( $action, array( '\MPHB\Views\SingleRoomTypeView', $callback ), $priority );
936 self::$attribute_removed_actions[ $callback ] = array(
937 'action' => $action,
938 'priority' => $priority,
939 );
940 }
941 }
942
943 if ( ! $show_label ) {
944 foreach ( self::$attribute_actions[ $attribute ]['label'] as $action => $callback ) {
945 $priority = has_action( $action, array( '\MPHB\Views\SingleRoomTypeView', $callback ) );
946
947 if ( $priority ) {
948 remove_action( $action, array( '\MPHB\Views\SingleRoomTypeView', $callback ), $priority );
949 self::$attribute_removed_actions[ $callback ] = array(
950 'action' => $action,
951 'priority' => $priority,
952 );
953 }
954 }
955 }
956
957 if ( 'custom' === $attribute ) {
958 // phpcs:disable WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase
959 global $mphbAttributes; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase.Found
960
961 foreach ( (array) $mphbAttributes as $key => $current_attribute ) {
962 if ( $key !== $custom_attribute ) {
963 $mphbAttributes[ $key ]['visible'] = false;
964 }
965 }
966 // phpcs:enable WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase
967 }
968 }
969
970 private static function after_attribute_render() {
971 foreach ( self::$attribute_removed_actions as $callback => $hook ) {
972 add_action( $hook['action'], array( '\MPHB\Views\SingleRoomTypeView', $callback ), $hook['priority'] );
973 }
974
975 self::$attribute_removed_actions = array();
976 }
977 }
978 }
979