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 +198 -6 11.011.8.4 View file →
@@ -1,4 +1,4 @@
1 1 <?php
2 2 /**
3 3 * Booking Form Builder - Preview Service (Option A).
4 4 *
@@ -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 *
@@ -235,9 +246,9 @@
235 246 wp_enqueue_script(
236 247 'wpbc-bfb-preview',
237 248 wpbc_plugin_url( '/includes/page-form-builder/preview/_out/bfb-preview.js' ),
238 249 array( 'wpbc-bfb_builder' ),
239 - '1.0.0',
250 + WP_BK_VERSION_NUM,
240 251 true
241 252 );
242 253 wp_enqueue_style(
243 254 'wpbc-bfb-preview-mode',
@@ -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();
@@ -659,9 +670,11 @@
659 670 }
660 671
661 672 $shortcode = '[booking resource_id="' . esc_attr( $resource_id ) . '" form_type="' . esc_attr( $form_name ) . '" form_status="preview"]';
662 673
663 - return do_shortcode( $shortcode );
674 + $preview_html = do_shortcode( $shortcode );
675 +
676 + return $this->filter_wrapped_html_for_preview_form_style( $preview_html, array(), $resource_id, $form_name );
664 677 }
665 678
666 679 public function filter_form_advanced_form_for_preview( $advanced_form, $form_id ) {
667 680
@@ -725,8 +738,172 @@
725 738 return $this->current_preview_data['structure'];
726 739 }
727 740
728 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 +
809 + $custom_options = wpbc_bfb_settings__get_custom_form_style_options( $this->current_preview_data['form_style'] );
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
818 + );
819 + }
820 +
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 + /**
729 906 * Hide the preview page from the Pages list in admin.
730 907 *
731 908 * @param WP_Query $query Main query.
732 909 */
@@ -769,9 +946,9 @@
769 946 * @param array $structure Decoded BFB structure.
770 947 *
771 948 * @return array|false { preview_url, token } or false on failure.
772 949 */
773 - public function create_preview_session( $preview_form_id, $user_id, $structure, $form_name = 'standard', $advanced_form = '', $content_form = '' ) {
950 + public function create_preview_session( $preview_form_id, $user_id, $structure, $form_name = 'standard', $advanced_form = '', $content_form = '', $form_style = array() ) {
774 951
775 952
776 953 $preview_form_id = (int) $preview_form_id;
777 954 $user_id = (int) $user_id;
@@ -779,8 +956,22 @@
779 956 $form_name = sanitize_text_field( (string) $form_name );
780 957 if ( '' === $form_name ) {
781 958 $form_name = 'standard';
782 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'] : '';
963 + $sanitized_style = wpbc_bfb_settings__sanitize_form_style( $style_key );
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 );
966 + $form_style = array_merge(
967 + array(
968 + 'booking_form_style' => $sanitized_style,
969 + ),
970 + $custom_style,
971 + $accent_style
972 + );
973 + }
783 974 if ( $preview_form_id <= 0 ) {
784 975 $preview_form_id = 1;
785 976 }
786 977 if ( $user_id <= 0 ) {
@@ -805,8 +996,9 @@
805 996 'structure' => $structure,
806 997 'time' => time(),
807 998 'advanced_form' => (string) $advanced_form,
808 999 'content_form' => (string) $content_form,
1000 + 'form_style' => $form_style,
809 1001 );
810 1002
811 1003 set_transient( $transient_key, $payload, 10 * MINUTE_IN_SECONDS );
812 1004
@@ -948,5 +1140,5 @@
948 1140 </div>
949 1141 <span><?php esc_html_e( 'Loading', 'booking' ); ?>...</span>
950 1142 </div>
951 1143 <?php
952 -}
1144 +}