PluginProbe
Booking Calendar / 11.8.4
Booking Calendar v11.8.4
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 10.11 All 204 releases
← All changes | includes/page-form-builder/preview/bfb-preview.php +219 -204 11.411.8.4 View file →
@@ -150,9 +150,9 @@
150 150 }
151 151
152 152 public function add_noindex_to_preview_page( $robots ) {
153 153
154 - if ( ! is_page() ) {
154 + if ( ! $this->is_main_query_page() ) {
155 155 return $robots;
156 156 }
157 157
158 158 $page_id = (int) get_queried_object_id();
@@ -167,8 +167,19 @@
167 167 return $robots;
168 168 }
169 169
170 170 /**
171 + * Check the main query directly without calling a conditional query tag too early.
172 + *
173 + * @return bool
174 + */
175 + protected function is_main_query_page() {
176 + return isset( $GLOBALS['wp_query'] )
177 + && ( $GLOBALS['wp_query'] instanceof WP_Query )
178 + && $GLOBALS['wp_query']->is_page();
179 + }
180 +
181 + /**
171 182 * Resolve capability used for preview / Builder access.
172 183 *
173 184 * Uses wpbc_bfb_get_manage_cap() when available, falls back to manage_options.
174 185 *
@@ -232,14 +243,14 @@
232 243 return;
233 244 }
234 245 // BFB preview script on the Builder admin page.
235 246 wp_enqueue_script(
236 - 'wpbc-bfb-preview',
237 - wpbc_plugin_url( '/includes/page-form-builder/preview/_out/bfb-preview.js' ),
238 - array( 'wpbc-bfb_builder' ),
239 - WP_BK_VERSION_NUM,
240 - true
241 - );
247 + 'wpbc-bfb-preview',
248 + wpbc_plugin_url( '/includes/page-form-builder/preview/_out/bfb-preview.js' ),
249 + array( 'wpbc-bfb_builder' ),
250 + WP_BK_VERSION_NUM,
251 + true
252 + );
242 253 wp_enqueue_style(
243 254 'wpbc-bfb-preview-mode',
244 255 wpbc_plugin_url( '/includes/page-form-builder/preview/_out/bfb-preview.css' ),
245 256 array(),
@@ -617,9 +628,9 @@
617 628 * @return string
618 629 */
619 630 public function filter_the_content_for_preview( $content ) {
620 631
621 - if ( ! is_page() ) {
632 + if ( ! $this->is_main_query_page() ) {
622 633 return $content;
623 634 }
624 635
625 636 $page_id = get_the_ID();
@@ -644,13 +655,13 @@
644 655
645 656 // Store preview data for this request so other hooks can access it.
646 657 $this->current_preview_data = $preview_data;
647 658
648 - // Optional: expose a filter so BFB structure loader can override structure per preview.
649 - // Your wpbc_bfb_get_form_structure() can apply this filter when resolving structure.
650 - add_filter( 'wpbc_bfb_get_form_structure', array( $this, 'filter_form_structure_for_preview' ), 10, 2 );
651 - add_filter( 'wpbc_bfb_get_form_advanced_form', array( $this, 'filter_form_advanced_form_for_preview' ), 10, 2 );
652 - add_filter( 'wpbc_bfb_get_form_content_form', array( $this, 'filter_form_content_form_for_preview' ), 10, 2 );
659 + // Optional: expose a filter so BFB structure loader can override structure per preview.
660 + // Your wpbc_bfb_get_form_structure() can apply this filter when resolving structure.
661 + add_filter( 'wpbc_bfb_get_form_structure', array( $this, 'filter_form_structure_for_preview' ), 10, 2 );
662 + add_filter( 'wpbc_bfb_get_form_advanced_form', array( $this, 'filter_form_advanced_form_for_preview' ), 10, 2 );
663 + add_filter( 'wpbc_bfb_get_form_content_form', array( $this, 'filter_form_content_form_for_preview' ), 10, 2 );
653 664
654 665 $resource_id = WPBC_FE_Attr_Postprocessor::get_default_booking_resource_id();
655 666
656 667 $form_name = isset( $preview_data['form_name'] ) ? sanitize_text_field( wp_unslash( $preview_data['form_name'] ) ) : 'standard';
@@ -657,15 +668,15 @@
657 668 if ( '' === $form_name ) {
658 669 $form_name = 'standard';
659 670 }
660 671
661 - $shortcode = '[booking resource_id="' . esc_attr( $resource_id ) . '" form_type="' . esc_attr( $form_name ) . '" form_status="preview"]';
662 -
663 - $preview_html = do_shortcode( $shortcode );
664 -
665 - return $this->filter_wrapped_html_for_preview_form_style( $preview_html, array(), $resource_id, $form_name );
666 - }
672 + $shortcode = '[booking resource_id="' . esc_attr( $resource_id ) . '" form_type="' . esc_attr( $form_name ) . '" form_status="preview"]';
667 673
674 + $preview_html = do_shortcode( $shortcode );
675 +
676 + return $this->filter_wrapped_html_for_preview_form_style( $preview_html, array(), $resource_id, $form_name );
677 + }
678 +
668 679 public function filter_form_advanced_form_for_preview( $advanced_form, $form_id ) {
669 680
670 681 if ( empty( $this->current_preview_data ) ) {
671 682 return $advanced_form;
@@ -709,9 +720,9 @@
709 720 * @param int $form_id Booking form ID.
710 721 *
711 722 * @return array
712 723 */
713 - public function filter_form_structure_for_preview( $structure, $form_id ) {
724 + public function filter_form_structure_for_preview( $structure, $form_id ) {
714 725
715 726 if ( empty( $this->current_preview_data ) ) {
716 727 return $structure;
717 728 }
@@ -723,174 +734,176 @@
723 734 if ( empty( $this->current_preview_data['structure'] ) || ! is_array( $this->current_preview_data['structure'] ) ) {
724 735 return $structure;
725 736 }
726 737
727 - return $this->current_preview_data['structure'];
728 - }
729 -
730 - /**
731 - * Apply unsaved global Form Style values to the preview iframe only.
732 - *
733 - * @param string $wrapped_html Wrapped booking form HTML.
734 - * @param array $bfb_settings BFB settings.
735 - * @param int $resource_id Booking resource ID.
736 - * @param string $custom_booking_form_name Form slug.
737 - * @return string
738 - */
739 - public function filter_wrapped_html_for_preview_form_style( $wrapped_html, $bfb_settings, $resource_id, $custom_booking_form_name ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed
740 -
741 - $preview_style = $this->get_preview_form_style();
742 - if ( empty( $preview_style ) ) {
743 - return $wrapped_html;
744 - }
745 -
746 - $style = isset( $preview_style['booking_form_style'] ) ? wpbc_bfb_settings__sanitize_form_style( $preview_style['booking_form_style'] ) : wpbc_bfb_settings__get_default_form_style();
747 - $preset = wpbc_bfb_settings__get_form_style_preset( $style );
748 -
749 - $wrapped_html = $this->remove_classes_from_first_form_wrapper(
750 - $wrapped_html,
751 - array(
752 - 'wpbc_theme_dark_1',
753 - 'wpbc_bfb_form_appearance_custom',
754 - )
755 - );
756 - $wrapped_html = $this->remove_classes_from_first_tag_with_classes(
757 - $wrapped_html,
758 - array( 'wpbc_bfb_form' ),
759 - array(
760 - 'wpbc_theme_dark_1',
761 - 'wpbc_bfb_form_appearance_custom',
762 - )
763 - );
764 -
765 - $theme_class = isset( $preset['theme_class'] ) ? sanitize_html_class( (string) $preset['theme_class'] ) : '';
766 - if ( '' !== $theme_class ) {
767 - $wrapped_html = WPBC_FE_Form_Style_Injector::add_class_to_first_tag_with_classes( $wrapped_html, array( 'wpbc_container', 'wpbc_form' ), $theme_class );
768 - }
769 -
770 - $css_vars = wpbc_bfb_settings__get_form_style_css_vars( $style, $preview_style );
771 - if ( ! empty( $css_vars ) ) {
772 - $wrapped_html = WPBC_FE_Form_Style_Injector::inject_css_vars_into_form_wrapper( $wrapped_html, $css_vars );
773 - $wrapped_html = WPBC_FE_Form_Style_Injector::inject_css_vars_into_bfb_root( $wrapped_html, $css_vars );
774 - }
775 -
776 - if ( wpbc_bfb_settings__is_custom_form_style( $style ) ) {
777 - $wrapped_html = WPBC_FE_Form_Style_Injector::add_class_to_first_tag_with_classes( $wrapped_html, array( 'wpbc_container', 'wpbc_form' ), 'wpbc_bfb_form_appearance_custom' );
778 - $wrapped_html = WPBC_FE_Form_Style_Injector::add_class_to_first_tag_with_classes( $wrapped_html, array( 'wpbc_bfb_form' ), 'wpbc_bfb_form_appearance_custom' );
779 - }
780 -
781 - return $wrapped_html;
782 - }
783 -
784 - /**
785 - * Get sanitized preview Form Style override from the current transient data.
786 - *
787 - * @return array
788 - */
789 - protected function get_preview_form_style() {
790 -
791 - if ( empty( $this->current_preview_data['form_style'] ) || ! is_array( $this->current_preview_data['form_style'] ) ) {
792 - return array();
793 - }
794 -
795 - $style = isset( $this->current_preview_data['form_style']['booking_form_style'] ) ? $this->current_preview_data['form_style']['booking_form_style'] : '';
796 - $style = wpbc_bfb_settings__sanitize_form_style( $style );
797 -
738 + return $this->current_preview_data['structure'];
739 + }
740 +
741 + /**
742 + * Apply unsaved global Form Style values to the preview iframe only.
743 + *
744 + * @param string $wrapped_html Wrapped booking form HTML.
745 + * @param array $bfb_settings BFB settings.
746 + * @param int $resource_id Booking resource ID.
747 + * @param string $custom_booking_form_name Form slug.
748 + * @return string
749 + */
750 + public function filter_wrapped_html_for_preview_form_style( $wrapped_html, $bfb_settings, $resource_id, $custom_booking_form_name ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed
751 +
752 + $preview_style = $this->get_preview_form_style();
753 + if ( empty( $preview_style ) ) {
754 + return $wrapped_html;
755 + }
756 +
757 + $style = isset( $preview_style['booking_form_style'] ) ? wpbc_bfb_settings__sanitize_form_style( $preview_style['booking_form_style'] ) : wpbc_bfb_settings__get_default_form_style();
758 + $preset = wpbc_bfb_settings__get_form_style_preset( $style );
759 +
760 + $wrapped_html = $this->remove_classes_from_first_form_wrapper(
761 + $wrapped_html,
762 + array(
763 + 'wpbc_theme_dark_1',
764 + 'wpbc_bfb_form_appearance_custom',
765 + )
766 + );
767 + $wrapped_html = $this->remove_classes_from_first_tag_with_classes(
768 + $wrapped_html,
769 + array( 'wpbc_bfb_form' ),
770 + array(
771 + 'wpbc_theme_dark_1',
772 + 'wpbc_bfb_form_appearance_custom',
773 + )
774 + );
775 +
776 + $theme_class = isset( $preset['theme_class'] ) ? sanitize_html_class( (string) $preset['theme_class'] ) : '';
777 + if ( '' !== $theme_class ) {
778 + $wrapped_html = WPBC_FE_Form_Style_Injector::add_class_to_first_tag_with_classes( $wrapped_html, array( 'wpbc_container', 'wpbc_form' ), $theme_class );
779 + }
780 +
781 + $css_vars = wpbc_bfb_settings__get_form_style_css_vars( $style, $preview_style );
782 + if ( ! empty( $css_vars ) ) {
783 + $wrapped_html = WPBC_FE_Form_Style_Injector::inject_css_vars_into_form_wrapper( $wrapped_html, $css_vars );
784 + $wrapped_html = WPBC_FE_Form_Style_Injector::inject_css_vars_into_bfb_root( $wrapped_html, $css_vars );
785 + }
786 +
787 + if ( wpbc_bfb_settings__is_custom_form_style( $style ) ) {
788 + $wrapped_html = WPBC_FE_Form_Style_Injector::add_class_to_first_tag_with_classes( $wrapped_html, array( 'wpbc_container', 'wpbc_form' ), 'wpbc_bfb_form_appearance_custom' );
789 + $wrapped_html = WPBC_FE_Form_Style_Injector::add_class_to_first_tag_with_classes( $wrapped_html, array( 'wpbc_bfb_form' ), 'wpbc_bfb_form_appearance_custom' );
790 + }
791 +
792 + return $wrapped_html;
793 + }
794 +
795 + /**
796 + * Get sanitized preview Form Style override from the current transient data.
797 + *
798 + * @return array
799 + */
800 + protected function get_preview_form_style() {
801 +
802 + if ( empty( $this->current_preview_data['form_style'] ) || ! is_array( $this->current_preview_data['form_style'] ) ) {
803 + return array();
804 + }
805 +
806 + $style = isset( $this->current_preview_data['form_style']['booking_form_style'] ) ? $this->current_preview_data['form_style']['booking_form_style'] : '';
807 + $style = wpbc_bfb_settings__sanitize_form_style( $style );
808 +
798 809 $custom_options = wpbc_bfb_settings__get_custom_form_style_options( $this->current_preview_data['form_style'] );
799 -
800 - return array_merge(
801 - array(
802 - 'booking_form_style' => $style,
803 - ),
804 - $custom_options
810 + $accent_options = wpbc_bfb_settings__get_form_accent_options( $this->current_preview_data['form_style'] );
811 +
812 + return array_merge(
813 + array(
814 + 'booking_form_style' => $style,
815 + ),
816 + $custom_options,
817 + $accent_options
805 818 );
806 - }
807 -
808 - /**
809 - * Remove classes from the first outer booking form wrapper.
810 - *
811 - * @param string $html HTML.
812 - * @param array $class_names Class names to remove.
813 - * @return string
814 - */
815 - protected function remove_classes_from_first_form_wrapper( $html, $class_names ) {
816 -
817 - return $this->remove_classes_from_first_tag_with_classes( $html, array( 'wpbc_container', 'wpbc_form' ), $class_names );
818 - }
819 -
820 - /**
821 - * Remove classes from the first tag that has all required classes.
822 - *
823 - * @param string $html HTML.
824 - * @param array $required_classes Required classes.
825 - * @param array $class_names Class names to remove.
826 - * @return string
827 - */
828 - protected function remove_classes_from_first_tag_with_classes( $html, $required_classes, $class_names ) {
829 -
830 - $html = (string) $html;
831 - $required_classes = is_array( $required_classes ) ? $required_classes : array();
832 - $class_names = is_array( $class_names ) ? $class_names : array();
833 - $remove_map = array();
834 - $required_map = array();
835 -
836 - foreach ( $required_classes as $class_name ) {
837 - $class_name = sanitize_html_class( (string) $class_name );
838 - if ( '' !== $class_name ) {
839 - $required_map[ $class_name ] = true;
840 - }
841 - }
842 -
843 - foreach ( $class_names as $class_name ) {
844 - $class_name = sanitize_html_class( (string) $class_name );
845 - if ( '' !== $class_name ) {
846 - $remove_map[ $class_name ] = true;
847 - }
848 - }
849 -
850 - if ( empty( $remove_map ) || empty( $required_map ) ) {
851 - return $html;
852 - }
853 -
854 - $done = false;
855 - $out = preg_replace_callback(
856 - '/<div\b[^>]*\bclass\s*=\s*(["\'])(.*?)\1[^>]*>/i',
857 - function ( $matches ) use ( &$done, $remove_map, $required_map ) {
858 -
859 - $tag = $matches[0];
860 - if ( $done ) {
861 - return $tag;
862 - }
863 -
864 - $quote = $matches[1];
865 - $classes = preg_split( '/\s+/', trim( (string) $matches[2] ) );
866 - $classes = is_array( $classes ) ? $classes : array();
867 -
868 - foreach ( $required_map as $required_class => $unused ) {
869 - if ( ! in_array( $required_class, $classes, true ) ) {
870 - return $tag;
871 - }
872 - }
873 -
874 - $filtered = array();
875 - foreach ( $classes as $class_name ) {
876 - if ( '' === $class_name || isset( $remove_map[ $class_name ] ) ) {
877 - continue;
878 - }
879 - $filtered[] = $class_name;
880 - }
881 -
882 - $done = true;
883 -
884 - return preg_replace( '/\bclass\s*=\s*(["\'])(.*?)\1/i', 'class=' . $quote . esc_attr( implode( ' ', $filtered ) ) . $quote, $tag, 1 );
885 - },
886 - $html
887 - );
888 -
889 - return ( null === $out ) ? $html : $out;
890 - }
819 + }
891 820
892 821 /**
822 + * Remove classes from the first outer booking form wrapper.
823 + *
824 + * @param string $html HTML.
825 + * @param array $class_names Class names to remove.
826 + * @return string
827 + */
828 + protected function remove_classes_from_first_form_wrapper( $html, $class_names ) {
829 +
830 + return $this->remove_classes_from_first_tag_with_classes( $html, array( 'wpbc_container', 'wpbc_form' ), $class_names );
831 + }
832 +
833 + /**
834 + * Remove classes from the first tag that has all required classes.
835 + *
836 + * @param string $html HTML.
837 + * @param array $required_classes Required classes.
838 + * @param array $class_names Class names to remove.
839 + * @return string
840 + */
841 + protected function remove_classes_from_first_tag_with_classes( $html, $required_classes, $class_names ) {
842 +
843 + $html = (string) $html;
844 + $required_classes = is_array( $required_classes ) ? $required_classes : array();
845 + $class_names = is_array( $class_names ) ? $class_names : array();
846 + $remove_map = array();
847 + $required_map = array();
848 +
849 + foreach ( $required_classes as $class_name ) {
850 + $class_name = sanitize_html_class( (string) $class_name );
851 + if ( '' !== $class_name ) {
852 + $required_map[ $class_name ] = true;
853 + }
854 + }
855 +
856 + foreach ( $class_names as $class_name ) {
857 + $class_name = sanitize_html_class( (string) $class_name );
858 + if ( '' !== $class_name ) {
859 + $remove_map[ $class_name ] = true;
860 + }
861 + }
862 +
863 + if ( empty( $remove_map ) || empty( $required_map ) ) {
864 + return $html;
865 + }
866 +
867 + $done = false;
868 + $out = preg_replace_callback(
869 + '/<div\b[^>]*\bclass\s*=\s*(["\'])(.*?)\1[^>]*>/i',
870 + function ( $matches ) use ( &$done, $remove_map, $required_map ) {
871 +
872 + $tag = $matches[0];
873 + if ( $done ) {
874 + return $tag;
875 + }
876 +
877 + $quote = $matches[1];
878 + $classes = preg_split( '/\s+/', trim( (string) $matches[2] ) );
879 + $classes = is_array( $classes ) ? $classes : array();
880 +
881 + foreach ( $required_map as $required_class => $unused ) {
882 + if ( ! in_array( $required_class, $classes, true ) ) {
883 + return $tag;
884 + }
885 + }
886 +
887 + $filtered = array();
888 + foreach ( $classes as $class_name ) {
889 + if ( '' === $class_name || isset( $remove_map[ $class_name ] ) ) {
890 + continue;
891 + }
892 + $filtered[] = $class_name;
893 + }
894 +
895 + $done = true;
896 +
897 + return preg_replace( '/\bclass\s*=\s*(["\'])(.*?)\1/i', 'class=' . $quote . esc_attr( implode( ' ', $filtered ) ) . $quote, $tag, 1 );
898 + },
899 + $html
900 + );
901 +
902 + return ( null === $out ) ? $html : $out;
903 + }
904 +
905 + /**
893 906 * Hide the preview page from the Pages list in admin.
894 907 *
895 908 * @param WP_Query $query Main query.
896 909 */
@@ -933,33 +946,35 @@
933 946 * @param array $structure Decoded BFB structure.
934 947 *
935 948 * @return array|false { preview_url, token } or false on failure.
936 949 */
937 - public function create_preview_session( $preview_form_id, $user_id, $structure, $form_name = 'standard', $advanced_form = '', $content_form = '', $form_style = array() ) {
950 + public function create_preview_session( $preview_form_id, $user_id, $structure, $form_name = 'standard', $advanced_form = '', $content_form = '', $form_style = array() ) {
938 951
939 952
940 953 $preview_form_id = (int) $preview_form_id;
941 954 $user_id = (int) $user_id;
942 955 $structure = ( is_array( $structure ) ? $structure : array() );
943 - $form_name = sanitize_text_field( (string) $form_name );
944 - if ( '' === $form_name ) {
945 - $form_name = 'standard';
946 - }
947 - $form_style = is_array( $form_style ) ? $form_style : array();
948 - if ( ! empty( $form_style ) ) {
949 - $style_key = isset( $form_style['booking_form_style'] ) ? $form_style['booking_form_style'] : '';
956 + $form_name = sanitize_text_field( (string) $form_name );
957 + if ( '' === $form_name ) {
958 + $form_name = 'standard';
959 + }
960 + $form_style = is_array( $form_style ) ? $form_style : array();
961 + if ( ! empty( $form_style ) ) {
962 + $style_key = isset( $form_style['booking_form_style'] ) ? $form_style['booking_form_style'] : '';
950 963 $sanitized_style = wpbc_bfb_settings__sanitize_form_style( $style_key );
951 964 $custom_style = wpbc_bfb_settings__get_custom_form_style_options( $form_style );
965 + $accent_style = wpbc_bfb_settings__get_form_accent_options( $form_style );
952 966 $form_style = array_merge(
953 - array(
954 - 'booking_form_style' => $sanitized_style,
955 - ),
956 - $custom_style
967 + array(
968 + 'booking_form_style' => $sanitized_style,
969 + ),
970 + $custom_style,
971 + $accent_style
957 972 );
958 - }
959 - if ( $preview_form_id <= 0 ) {
960 - $preview_form_id = 1;
961 - }
973 + }
974 + if ( $preview_form_id <= 0 ) {
975 + $preview_form_id = 1;
976 + }
962 977 if ( $user_id <= 0 ) {
963 978 return false;
964 979 }
965 980
@@ -978,13 +993,13 @@
978 993 'form_id' => $preview_form_id,
979 994 'form_name' => $form_name,
980 995 'scope' => 'preview',
981 996 'structure' => $structure,
982 - 'time' => time(),
983 - 'advanced_form' => (string) $advanced_form,
984 - 'content_form' => (string) $content_form,
985 - 'form_style' => $form_style,
986 - );
997 + 'time' => time(),
998 + 'advanced_form' => (string) $advanced_form,
999 + 'content_form' => (string) $content_form,
1000 + 'form_style' => $form_style,
1001 + );
987 1002
988 1003 set_transient( $transient_key, $payload, 10 * MINUTE_IN_SECONDS );
989 1004
990 1005 $preview_url = add_query_arg( array(
@@ -1125,5 +1140,5 @@
1125 1140 </div>
1126 1141 <span><?php esc_html_e( 'Loading', 'booking' ); ?>...</span>
1127 1142 </div>
1128 1143 <?php
1129 -}
1144 +}