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
← All changes | classes/helpers/FrmStylesPreviewHelper.php +45 -190 6.326.0 View file →
@@ -8,9 +8,9 @@
8 8 */
9 9 class FrmStylesPreviewHelper {
10 10
11 11 /**
12 - * @var int
12 + * @param int $form_id
13 13 */
14 14 private $form_id;
15 15
16 16 /**
@@ -20,26 +20,13 @@
20 20 */
21 21 private $form_includes_captcha = false;
22 22
23 23 /**
24 - * Track the field ids with the default label position.
25 - * A field with a default label position will have a frm-default-label-position class in the styler preview.
26 - * Only fields with this class will change when you update the position setting in the styler.
27 - *
28 - * @since 6.0.1
29 - *
30 - * @var array
31 - */
32 - private $default_label_position_field_ids;
33 -
34 - /**
35 - * @param int|string $form_id
36 - *
24 + * @param string|int $form_id
37 25 * @return void
38 26 */
39 27 public function __construct( $form_id ) {
40 - $this->form_id = (int) $form_id;
41 - $this->default_label_position_field_ids = array();
28 + $this->form_id = (int) $form_id;
42 29 }
43 30
44 31 /**
45 32 * Modify form behaviours for the styler preview.
@@ -48,107 +35,15 @@
48 35 *
49 36 * @return void
50 37 */
51 38 public function adjust_form_for_preview() {
52 - // Don't bother including the antispam token in the preview as the form isn't submitted.
53 - add_filter( 'frm_run_antispam', '__return_false', 99 );
54 -
55 - // We don't need the honeypot in the preview so leave it out.
56 - add_filter( 'frm_run_honeypot', '__return_false' );
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.
57 41 $this->hide_captcha_fields();
58 42 $this->disable_javascript_validation();
59 - $this->add_a_div_class_for_default_label_positions();
60 - $this->add_a_div_class_for_style_driven_alignment();
61 43 }
62 44
63 45 /**
64 - * Add a marker class to radio and checkbox fields that use the style's option
65 - * alignment setting rather than a field level override.
66 - *
67 - * Only fields with this class change when the radio or checkbox alignment style
68 - * setting is updated in the styler preview. Fields with their own alignment
69 - * override are left untouched.
70 - *
71 - * @since 6.32
72 - *
73 - * @return void
74 - */
75 - private function add_a_div_class_for_style_driven_alignment() {
76 - add_filter(
77 - 'frm_field_div_classes',
78 - /**
79 - * @param string $classes
80 - * @param array|object $field
81 - *
82 - * @return string
83 - */
84 - function ( $classes, $field ) {
85 - if ( ! FrmField::is_radio( $field ) && ! FrmField::is_checkbox( $field ) ) {
86 - return $classes;
87 - }
88 -
89 - // Mirror FrmFieldType::get_container_class(): an override only exists in Pro.
90 - $align = FrmAppHelper::pro_is_installed() ? FrmField::get_option( $field, 'align' ) : '';
91 -
92 - if ( ! $align ) {
93 - $classes .= ' frm-default-option-align';
94 - }
95 -
96 - return $classes;
97 - },
98 - 10,
99 - 2
100 - );
101 - }
102 -
103 - /**
104 - * Add a frm-default-label-position class to any field with a default label position.
105 - * A field with a custom label position shouldn't ever change in the styler preview.
106 - *
107 - * @since 6.0.1
108 - *
109 - * @return void
110 - */
111 - private function add_a_div_class_for_default_label_positions() {
112 - // Only fields with no label position set hit this filter, so track those for the frm_field_div_classes filter.
113 - add_filter(
114 - 'frm_html_label_position',
115 - /**
116 - * @param string $position
117 - * @param array|object $field
118 - *
119 - * @return string
120 - */
121 - function ( $position, $field ) {
122 - if ( is_array( $field ) ) {
123 - $this->default_label_position_field_ids[] = (int) $field['id'];
124 - }
125 - return $position;
126 - },
127 - 10,
128 - 2
129 - );
130 -
131 - add_filter(
132 - 'frm_field_div_classes',
133 - /**
134 - * @param string $classes
135 - * @param array|object $field
136 - *
137 - * @return string
138 - */
139 - function ( $classes, $field ) {
140 - if ( is_array( $field ) && in_array( (int) $field['id'], $this->default_label_position_field_ids, true ) ) {
141 - $classes .= ' frm-default-label-position';
142 - }
143 - return $classes;
144 - },
145 - 10,
146 - 2
147 - );
148 - }
149 -
150 - /**
151 46 * Captcha does not initialize in the preview. Hide it so we don't see the label either.
152 47 *
153 48 * @since 6.0
154 49 *
@@ -160,13 +55,15 @@
160 55 /**
161 56 * @param bool $show
162 57 * @param string $field_type
163 58 * @param string $target_field_type
164 - *
165 59 * @return bool
166 60 */
167 - function ( $show, $field_type ) {
168 - return 'captcha' === $field_type ? false : $show;
61 + function( $show, $field_type ) {
62 + if ( 'captcha' === $field_type ) {
63 + $show = false;
64 + }
65 + return $show;
169 66 },
170 67 10,
171 68 2
172 69 );
@@ -183,18 +80,10 @@
183 80 */
184 81 private function disable_javascript_validation() {
185 82 add_filter(
186 83 'frm_form_object',
187 - /**
188 - * @param stdClass|null $form
189 - * @param int $form_id
190 - *
191 - * @return stdClass|null
192 - */
193 - function ( $form ) {
194 - if ( is_object( $form ) && is_array( $form->options ) ) {
195 - $form->options['js_validate'] = false;
196 - }
84 + function( $form ) {
85 + $form->options['js_validate'] = false;
197 86 return $form;
198 87 }
199 88 );
200 89 }
@@ -201,8 +90,9 @@
201 90
202 91 /**
203 92 * @since 6.0
204 93 *
94 + * @param string|int $form_id
205 95 * @return string
206 96 */
207 97 public function get_html_for_form_preview() {
208 98 // Force is_admin to false so the "Entry Key" field doesn't render in the preview.
@@ -210,9 +100,8 @@
210 100
211 101 $target_form_preview_html = FrmFormsController::show_form( $this->form_id, '', 'auto', 'auto' );
212 102
213 103 $this->form_includes_captcha = wp_script_is( 'captcha-api', 'enqueued' );
214 -
215 104 if ( $this->form_includes_captcha ) {
216 105 // If a form includes a CAPTCHA field, don't try to load the CAPTCHA scripts for the visual styler preview.
217 106 wp_dequeue_script( 'captcha-api' );
218 107 }
@@ -226,22 +115,23 @@
226 115
227 116 /**
228 117 * @since 6.0
229 118 *
119 + * @todo Only show the note once for a form per user per month or something.
120 + *
230 121 * @return array<string>
231 122 */
232 123 public function get_notes_for_styler_preview() {
233 - $notes = array();
124 + $notes = array();
125 +
234 126 $fallback_form_note = $this->get_fallback_form_note();
235 -
236 127 if ( is_string( $fallback_form_note ) ) {
237 128 $notes[] = $fallback_form_note;
238 129 }
239 130
240 131 $frm_settings = FrmAppHelper::get_settings();
241 -
242 132 if ( 'none' === $frm_settings->load_style ) {
243 - $notes[] = function () {
133 + $notes[] = function() {
244 134 printf(
245 135 // translators: %1$s: Anchor tag open, %2$s: Anchor tag close.
246 136 esc_html__( 'Formidable styles are disabled. This needs to be enabled in %1$sGlobal Settings%2$s.', 'formidable' ),
247 137 '<a href="' . esc_url( admin_url( 'admin.php?page=formidable-settings' ) ) . '">',
@@ -249,14 +139,17 @@
249 139 );
250 140 };
251 141 }
252 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 +
253 147 if ( is_callable( 'FrmProStylesController::get_notes_for_styler_preview' ) ) {
254 148 $notes = array_merge( $notes, FrmProStylesController::get_notes_for_styler_preview() );
255 149 }
256 150
257 151 $disabled_features_note = $this->get_disabled_features_note();
258 -
259 152 if ( is_string( $disabled_features_note ) ) {
260 153 $notes[] = $disabled_features_note;
261 154 }
262 155
@@ -265,18 +158,35 @@
265 158
266 159 /**
267 160 * @since 6.0
268 161 *
269 - * @return string
162 + * @return string|false
270 163 */
271 164 private function get_disabled_features_note() {
272 - return __( 'Not all JavaScript is loaded in this preview. Some features will appear differently on the front end.', 'formidable' );
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;
273 183 }
274 184
275 185 /**
276 186 * @since 6.0
277 187 *
278 - * @return false|string
188 + * @return string|false
279 189 */
280 190 private function get_fallback_form_note() {
281 191 if ( ! FrmAppHelper::simple_get( 'form', 'absint' ) ) {
282 192 return __( 'This form is being previewed because no form was selected. Use the form dropdown to select a new preview target.', 'formidable' );
@@ -288,12 +198,11 @@
288 198 * Get all warnings to display above the visual styler preview.
289 199 *
290 200 * @since 6.0
291 201 *
292 - * @param stdClass|WP_Post $style A new style is not a WP_Post object.
202 + * @param WP_Post|stdClass $style A new style is not a WP_Post object.
293 203 * @param WP_Post $default_style
294 204 * @param string $view Either 'list' or 'edit'.
295 - *
296 205 * @return array<string>
297 206 */
298 207 public function get_warnings_for_styler_preview( $style, $default_style, $view ) {
299 208 $warnings = array();
@@ -309,9 +218,8 @@
309 218 * @since 6.0
310 219 *
311 220 * @param int $style_id
312 221 * @param int $default_style_id
313 - *
314 222 * @return bool
315 223 */
316 224 private function should_show_multiple_forms_warning( $style_id, $default_style_id ) {
317 225 $is_default_style = $style_id === $default_style_id;
@@ -318,9 +226,8 @@
318 226
319 227 if ( ! $is_default_style ) {
320 228 // Also check for the conversational default.
321 229 $conversational_style_id = FrmDb::get_var( 'posts', array( 'post_name' => 'lines-no-boxes' ), 'ID' );
322 -
323 230 if ( $conversational_style_id && (int) $conversational_style_id === $style_id ) {
324 231 $is_default_style = true;
325 232 }
326 233 }
@@ -347,9 +254,8 @@
347 254 /**
348 255 * @since 6.0
349 256 *
350 257 * @param WP_Styles $styles
351 - *
352 258 * @return void
353 259 */
354 260 public static function disable_conflicting_wp_admin_css( $styles ) {
355 261 if ( ! is_callable( array( $styles, 'remove' ) ) || ! array_key_exists( 'wp-admin', $styles->registered ) ) {
@@ -358,10 +264,9 @@
358 264
359 265 $styles->remove( 'edit' );
360 266
361 267 $wp_admin_dependencies = $styles->registered['wp-admin']->deps;
362 - $edit_key = array_search( 'edit', $wp_admin_dependencies, true );
363 -
268 + $edit_key = array_search( 'edit', $wp_admin_dependencies );
364 269 if ( false === $edit_key ) {
365 270 return;
366 271 }
367 272
@@ -369,67 +274,17 @@
369 274 self::remove_wp_admin_dependency( $styles, 'edit' );
370 275 }
371 276
372 277 /**
373 - * Provides few fixes for style preview.
374 - * Fix the "Width" from Fields Settings to get reflected on each style preview ajax update.
375 - * Fix the Radio & Checkbox "Single Row" or "Multiple Row" to get reflected in style preview ajax update.
376 - *
377 - * @since 6.14
378 - *
379 - * @param array $settings The style options.
380 - * @param bool $is_preview
381 - *
382 - * @return void
383 - */
384 - public static function get_additional_preview_style( $settings, $is_preview = false ) {
385 - if ( ! $is_preview ) {
386 - return;
387 - }
388 -
389 - $radio_display_type = 'inline' === $settings['radio_align'] ? 'inline-block' : 'block';
390 - $check_display_type = 'inline' === $settings['check_align'] ? 'inline-block' : 'block';
391 -
392 - $style = '#frm_style_preview .with_frm_style input[type=text],
393 - #frm_style_preview .with_frm_style input[type=password],
394 - #frm_style_preview .with_frm_style input[type=email],
395 - #frm_style_preview .with_frm_style input[type=number],
396 - #frm_style_preview .with_frm_style input[type=url],
397 - #frm_style_preview .with_frm_style input[type=tel],
398 - #frm_style_preview .with_frm_style input[type=phone],
399 - #frm_style_preview .with_frm_style input[type=search],
400 - #frm_style_preview .with_frm_style textarea,
401 - #frm_style_preview .frm_form_fields_style,
402 - #frm_style_preview .with_frm_style .frm_scroll_box .frm_opt_container,
403 - #frm_style_preview .frm_form_fields_active_style,
404 - #frm_style_preview .frm_form_fields_error_style,
405 - #frm_style_preview .with_frm_style .frm-card-element.StripeElement,
406 - #frm_style_preview .with_frm_style .frm_slimselect.ss-main {
407 - width: var(--field-width);
408 - }
409 - #frm_style_preview .with_frm_style .frm_radio {
410 - display:' . $radio_display_type . ';
411 - }
412 - #frm_style_preview .with_frm_style .frm_checkbox {
413 - display:' . $check_display_type . ';
414 - }';
415 -
416 - echo esc_html( $style );
417 - }
418 -
419 - /**
420 278 * @since 6.0
421 279 *
422 280 * @param WP_Styles $styles
423 281 * @param string $key
424 - *
425 282 * @return void
426 283 */
427 284 private static function remove_wp_admin_dependency( $styles, $key ) {
428 285 $dependencies = $styles->registered['wp-admin']->deps;
429 - // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict
430 - $index = array_search( $key, $dependencies );
431 -
286 + $index = array_search( $key, $dependencies );
432 287 if ( false === $index ) {
433 288 return;
434 289 }
435 290