PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.0
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.0
6.35 6.34 6.33.1 6.33 6.32.1 6.32 6.31 6.25 6.25.1 6.26 6.26.1 6.27 6.28 6.29 6.3 6.3.1 6.3.2 6.30 6.4 6.4.1 6.4.2 6.5 6.5.1 6.5.2 6.5.3 All 141 releases
formidable / classes / helpers / FrmStylesPreviewHelper.php

FrmStylesPreviewHelper.php in Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More 6.0, at classes/helpers/FrmStylesPreviewHelper.php

297 lines 8.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) {
3 die( 'You are not allowed to call this page directly.' );
4 }
5
6 /**
7 * @since 6.0
8 */
9 class FrmStylesPreviewHelper {
10
11 /**
12 * @param int $form_id
13 */
14 private $form_id;
15
16 /**
17 * This is tracked so we can show a note that the CAPTCHA was turned off.
18 *
19 * @var bool
20 */
21 private $form_includes_captcha = false;
22
23 /**
24 * @param string|int $form_id
25 * @return void
26 */
27 public function __construct( $form_id ) {
28 $this->form_id = (int) $form_id;
29 }
30
31 /**
32 * Modify form behaviours for the styler preview.
33 *
34 * @since 6.0
35 *
36 * @return void
37 */
38 public function adjust_form_for_preview() {
39 add_filter( 'frm_run_antispam', '__return_false', 99 ); // Don't bother including the antispam token in the preview as the form isn't submitted.
40 add_filter( 'frm_run_honeypot', '__return_false' ); // We don't need the honeypot in the preview so leave it out.
41 $this->hide_captcha_fields();
42 $this->disable_javascript_validation();
43 }
44
45 /**
46 * Captcha does not initialize in the preview. Hide it so we don't see the label either.
47 *
48 * @since 6.0
49 *
50 * @return void
51 */
52 private function hide_captcha_fields() {
53 add_filter(
54 'frm_show_normal_field_type',
55 /**
56 * @param bool $show
57 * @param string $field_type
58 * @param string $target_field_type
59 * @return bool
60 */
61 function( $show, $field_type ) {
62 if ( 'captcha' === $field_type ) {
63 $show = false;
64 }
65 return $show;
66 },
67 10,
68 2
69 );
70 }
71
72 /**
73 * Turn off JavaScript validation for the preview.
74 * Without this we hit a "PHP Fatal error: Uncaught Error: Maximum function nesting level of '256' reached" error.
75 * This only happened with specific Look up fields when FrmProLookupFieldsController::get_independent_lookup_field_options is called in Pro.
76 *
77 * @since 6.0
78 *
79 * @return void
80 */
81 private function disable_javascript_validation() {
82 add_filter(
83 'frm_form_object',
84 function( $form ) {
85 $form->options['js_validate'] = false;
86 return $form;
87 }
88 );
89 }
90
91 /**
92 * @since 6.0
93 *
94 * @param string|int $form_id
95 * @return string
96 */
97 public function get_html_for_form_preview() {
98 // Force is_admin to false so the "Entry Key" field doesn't render in the preview.
99 add_filter( 'frm_is_admin', '__return_false' );
100
101 $target_form_preview_html = FrmFormsController::show_form( $this->form_id, '', 'auto', 'auto' );
102
103 $this->form_includes_captcha = wp_script_is( 'captcha-api', 'enqueued' );
104 if ( $this->form_includes_captcha ) {
105 // If a form includes a CAPTCHA field, don't try to load the CAPTCHA scripts for the visual styler preview.
106 wp_dequeue_script( 'captcha-api' );
107 }
108
109 // Return the is_admin status.
110 // Otherwise success messages won't use the proper mark up and will appear without the green background and padding.
111 remove_filter( 'frm_is_admin', '__return_false' );
112
113 return $target_form_preview_html;
114 }
115
116 /**
117 * @since 6.0
118 *
119 * @todo Only show the note once for a form per user per month or something.
120 *
121 * @return array<string>
122 */
123 public function get_notes_for_styler_preview() {
124 $notes = array();
125
126 $fallback_form_note = $this->get_fallback_form_note();
127 if ( is_string( $fallback_form_note ) ) {
128 $notes[] = $fallback_form_note;
129 }
130
131 $frm_settings = FrmAppHelper::get_settings();
132 if ( 'none' === $frm_settings->load_style ) {
133 $notes[] = function() {
134 printf(
135 // translators: %1$s: Anchor tag open, %2$s: Anchor tag close.
136 esc_html__( 'Formidable styles are disabled. This needs to be enabled in %1$sGlobal Settings%2$s.', 'formidable' ),
137 '<a href="' . esc_url( admin_url( 'admin.php?page=formidable-settings' ) ) . '">',
138 '</a>'
139 );
140 };
141 }
142
143 if ( class_exists( 'FrmProStylesController' ) && ! class_exists( 'FrmProStylesPreviewHelper' ) ) {
144 $notes[] = __( 'You are using an outdated version of Formidable Pro. Please update to version 6.0 to get access to all styler features.', 'formidable' );
145 }
146
147 if ( is_callable( 'FrmProStylesController::get_notes_for_styler_preview' ) ) {
148 $notes = array_merge( $notes, FrmProStylesController::get_notes_for_styler_preview() );
149 }
150
151 $disabled_features_note = $this->get_disabled_features_note();
152 if ( is_string( $disabled_features_note ) ) {
153 $notes[] = $disabled_features_note;
154 }
155
156 return $notes;
157 }
158
159 /**
160 * @since 6.0
161 *
162 * @return string|false
163 */
164 private function get_disabled_features_note() {
165 $disabled_features = array();
166
167 if ( $this->form_includes_captcha ) {
168 $disabled_features[] = __( 'Captcha fields are hidden.', 'formidable' );
169 }
170
171 if ( is_callable( 'FrmProStylesController::get_disabled_javascript_features' ) ) {
172 $disabled_pro_features = FrmProStylesController::get_disabled_javascript_features();
173 if ( is_string( $disabled_pro_features ) ) {
174 $disabled_features[] = $disabled_pro_features;
175 }
176 }
177
178 if ( $disabled_features ) {
179 return __( 'Not all JavaScript is loaded in this preview.', 'formidable' ) . ' ' . implode( ' ', $disabled_features );
180 }
181
182 return false;
183 }
184
185 /**
186 * @since 6.0
187 *
188 * @return string|false
189 */
190 private function get_fallback_form_note() {
191 if ( ! FrmAppHelper::simple_get( 'form', 'absint' ) ) {
192 return __( 'This form is being previewed because no form was selected. Use the form dropdown to select a new preview target.', 'formidable' );
193 }
194 return false;
195 }
196
197 /**
198 * Get all warnings to display above the visual styler preview.
199 *
200 * @since 6.0
201 *
202 * @param WP_Post|stdClass $style A new style is not a WP_Post object.
203 * @param WP_Post $default_style
204 * @param string $view Either 'list' or 'edit'.
205 * @return array<string>
206 */
207 public function get_warnings_for_styler_preview( $style, $default_style, $view ) {
208 $warnings = array();
209
210 if ( 'edit' === $view && $this->should_show_multiple_forms_warning( $style->ID, $default_style->ID ) ) {
211 $warnings[] = __( 'Changes that you will make to this style will apply to every form using this style.', 'formidable' );
212 }
213
214 return $warnings;
215 }
216
217 /**
218 * @since 6.0
219 *
220 * @param int $style_id
221 * @param int $default_style_id
222 * @return bool
223 */
224 private function should_show_multiple_forms_warning( $style_id, $default_style_id ) {
225 $is_default_style = $style_id === $default_style_id;
226
227 if ( ! $is_default_style ) {
228 // Also check for the conversational default.
229 $conversational_style_id = FrmDb::get_var( 'posts', array( 'post_name' => 'lines-no-boxes' ), 'ID' );
230 if ( $conversational_style_id && (int) $conversational_style_id === $style_id ) {
231 $is_default_style = true;
232 }
233 }
234
235 $form_count = FrmStylesHelper::get_form_count_for_style( $style_id, $is_default_style );
236
237 if ( $form_count <= 1 ) {
238 return false;
239 }
240
241 // Only show the warning once per user per style.
242 $user_id = get_current_user_id();
243 $meta_key = 'frm_dismiss_multiple_forms_warning_' . $style_id;
244 $meta = get_user_meta( $user_id, $meta_key, true );
245
246 if ( $meta ) {
247 return false;
248 }
249
250 add_user_meta( $user_id, $meta_key, 1 );
251 return true;
252 }
253
254 /**
255 * @since 6.0
256 *
257 * @param WP_Styles $styles
258 * @return void
259 */
260 public static function disable_conflicting_wp_admin_css( $styles ) {
261 if ( ! is_callable( array( $styles, 'remove' ) ) || ! array_key_exists( 'wp-admin', $styles->registered ) ) {
262 return;
263 }
264
265 $styles->remove( 'edit' );
266
267 $wp_admin_dependencies = $styles->registered['wp-admin']->deps;
268 $edit_key = array_search( 'edit', $wp_admin_dependencies );
269 if ( false === $edit_key ) {
270 return;
271 }
272
273 // Remove the edit dependency from wp-admin so it still loads, just without edit.css.
274 self::remove_wp_admin_dependency( $styles, 'edit' );
275 }
276
277 /**
278 * @since 6.0
279 *
280 * @param WP_Styles $styles
281 * @param string $key
282 * @return void
283 */
284 private static function remove_wp_admin_dependency( $styles, $key ) {
285 $dependencies = $styles->registered['wp-admin']->deps;
286 $index = array_search( $key, $dependencies );
287 if ( false === $index ) {
288 return;
289 }
290
291 unset( $dependencies[ $index ] );
292 $dependencies = array_values( $dependencies );
293
294 $styles->registered['wp-admin']->deps = $dependencies;
295 }
296 }
297