| @@ -8,9 +8,9 @@ | ||
| 8 | 8 | */ |
| 9 | 9 | class FrmStylesPreviewHelper { |
| 10 | 10 | |
| 11 | 11 | /** |
| 12 | - * @param int $form_id | |
| 12 | + * @var int | |
| 13 | 13 | */ |
| 14 | 14 | private $form_id; |
| 15 | 15 | |
| 16 | 16 | /** |
| @@ -31,13 +31,14 @@ | ||
| 31 | 31 | */ |
| 32 | 32 | private $default_label_position_field_ids; |
| 33 | 33 | |
| 34 | 34 | /** |
| 35 | - * @param string|int $form_id | |
| 35 | + * @param int|string $form_id | |
| 36 | + * | |
| 36 | 37 | * @return void |
| 37 | 38 | */ |
| 38 | 39 | public function __construct( $form_id ) { |
| 39 | - $this->form_id = (int) $form_id; | |
| 40 | + $this->form_id = (int) $form_id; | |
| 40 | 41 | $this->default_label_position_field_ids = array(); |
| 41 | 42 | } |
| 42 | 43 | |
| 43 | 44 | /** |
| @@ -47,20 +48,65 @@ | ||
| 47 | 48 | * |
| 48 | 49 | * @return void |
| 49 | 50 | */ |
| 50 | 51 | public function adjust_form_for_preview() { |
| 51 | - add_filter( 'frm_run_antispam', '__return_false', 99 ); // Don't bother including the antispam token in the preview as the form isn't submitted. | |
| 52 | - add_filter( 'frm_run_honeypot', '__return_false' ); // We don't need the honeypot in the preview so leave it out. | |
| 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' ); | |
| 53 | 57 | $this->hide_captcha_fields(); |
| 54 | 58 | $this->disable_javascript_validation(); |
| 55 | 59 | $this->add_a_div_class_for_default_label_positions(); |
| 60 | + $this->add_a_div_class_for_style_driven_alignment(); | |
| 56 | 61 | } |
| 57 | 62 | |
| 58 | 63 | /** |
| 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 | + /** | |
| 59 | 104 | * Add a frm-default-label-position class to any field with a default label position. |
| 60 | 105 | * A field with a custom label position shouldn't ever change in the styler preview. |
| 61 | 106 | * |
| 62 | 107 | * @since 6.0.1 |
| 108 | + * | |
| 63 | 109 | * @return void |
| 64 | 110 | */ |
| 65 | 111 | private function add_a_div_class_for_default_label_positions() { |
| 66 | 112 | // Only fields with no label position set hit this filter, so track those for the frm_field_div_classes filter. |
| @@ -67,12 +113,13 @@ | ||
| 67 | 113 | add_filter( |
| 68 | 114 | 'frm_html_label_position', |
| 69 | 115 | /** |
| 70 | 116 | * @param string $position |
| 71 | - * @param object|array $field | |
| 117 | + * @param array|object $field | |
| 118 | + * | |
| 72 | 119 | * @return string |
| 73 | 120 | */ |
| 74 | - function( $position, $field ) { | |
| 121 | + function ( $position, $field ) { | |
| 75 | 122 | if ( is_array( $field ) ) { |
| 76 | 123 | $this->default_label_position_field_ids[] = (int) $field['id']; |
| 77 | 124 | } |
| 78 | 125 | return $position; |
| @@ -84,12 +131,13 @@ | ||
| 84 | 131 | add_filter( |
| 85 | 132 | 'frm_field_div_classes', |
| 86 | 133 | /** |
| 87 | 134 | * @param string $classes |
| 88 | - * @param object|array $field | |
| 135 | + * @param array|object $field | |
| 136 | + * | |
| 89 | 137 | * @return string |
| 90 | 138 | */ |
| 91 | - function( $classes, $field ) { | |
| 139 | + function ( $classes, $field ) { | |
| 92 | 140 | if ( is_array( $field ) && in_array( (int) $field['id'], $this->default_label_position_field_ids, true ) ) { |
| 93 | 141 | $classes .= ' frm-default-label-position'; |
| 94 | 142 | } |
| 95 | 143 | return $classes; |
| @@ -112,15 +160,13 @@ | ||
| 112 | 160 | /** |
| 113 | 161 | * @param bool $show |
| 114 | 162 | * @param string $field_type |
| 115 | 163 | * @param string $target_field_type |
| 164 | + * | |
| 116 | 165 | * @return bool |
| 117 | 166 | */ |
| 118 | - function( $show, $field_type ) { | |
| 119 | - if ( 'captcha' === $field_type ) { | |
| 120 | - $show = false; | |
| 121 | - } | |
| 122 | - return $show; | |
| 167 | + function ( $show, $field_type ) { | |
| 168 | + return 'captcha' === $field_type ? false : $show; | |
| 123 | 169 | }, |
| 124 | 170 | 10, |
| 125 | 171 | 2 |
| 126 | 172 | ); |
| @@ -137,10 +183,18 @@ | ||
| 137 | 183 | */ |
| 138 | 184 | private function disable_javascript_validation() { |
| 139 | 185 | add_filter( |
| 140 | 186 | 'frm_form_object', |
| 141 | - function( $form ) { | |
| 142 | - $form->options['js_validate'] = false; | |
| 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 | + } | |
| 143 | 197 | return $form; |
| 144 | 198 | } |
| 145 | 199 | ); |
| 146 | 200 | } |
| @@ -147,9 +201,8 @@ | ||
| 147 | 201 | |
| 148 | 202 | /** |
| 149 | 203 | * @since 6.0 |
| 150 | 204 | * |
| 151 | - * @param string|int $form_id | |
| 152 | 205 | * @return string |
| 153 | 206 | */ |
| 154 | 207 | public function get_html_for_form_preview() { |
| 155 | 208 | // Force is_admin to false so the "Entry Key" field doesn't render in the preview. |
| @@ -157,8 +210,9 @@ | ||
| 157 | 210 | |
| 158 | 211 | $target_form_preview_html = FrmFormsController::show_form( $this->form_id, '', 'auto', 'auto' ); |
| 159 | 212 | |
| 160 | 213 | $this->form_includes_captcha = wp_script_is( 'captcha-api', 'enqueued' ); |
| 214 | + | |
| 161 | 215 | if ( $this->form_includes_captcha ) { |
| 162 | 216 | // If a form includes a CAPTCHA field, don't try to load the CAPTCHA scripts for the visual styler preview. |
| 163 | 217 | wp_dequeue_script( 'captcha-api' ); |
| 164 | 218 | } |
| @@ -172,23 +226,22 @@ | ||
| 172 | 226 | |
| 173 | 227 | /** |
| 174 | 228 | * @since 6.0 |
| 175 | 229 | * |
| 176 | - * @todo Only show the note once for a form per user per month or something. | |
| 177 | - * | |
| 178 | 230 | * @return array<string> |
| 179 | 231 | */ |
| 180 | 232 | public function get_notes_for_styler_preview() { |
| 181 | - $notes = array(); | |
| 233 | + $notes = array(); | |
| 234 | + $fallback_form_note = $this->get_fallback_form_note(); | |
| 182 | 235 | |
| 183 | - $fallback_form_note = $this->get_fallback_form_note(); | |
| 184 | 236 | if ( is_string( $fallback_form_note ) ) { |
| 185 | 237 | $notes[] = $fallback_form_note; |
| 186 | 238 | } |
| 187 | 239 | |
| 188 | 240 | $frm_settings = FrmAppHelper::get_settings(); |
| 241 | + | |
| 189 | 242 | if ( 'none' === $frm_settings->load_style ) { |
| 190 | - $notes[] = function() { | |
| 243 | + $notes[] = function () { | |
| 191 | 244 | printf( |
| 192 | 245 | // translators: %1$s: Anchor tag open, %2$s: Anchor tag close. |
| 193 | 246 | esc_html__( 'Formidable styles are disabled. This needs to be enabled in %1$sGlobal Settings%2$s.', 'formidable' ), |
| 194 | 247 | '<a href="' . esc_url( admin_url( 'admin.php?page=formidable-settings' ) ) . '">', |
| @@ -196,17 +249,14 @@ | ||
| 196 | 249 | ); |
| 197 | 250 | }; |
| 198 | 251 | } |
| 199 | 252 | |
| 200 | - if ( class_exists( 'FrmProStylesController' ) && ! class_exists( 'FrmProStylesPreviewHelper' ) ) { | |
| 201 | - $notes[] = __( 'You are using an outdated version of Formidable Pro. Please update to version 6.0 to get access to all styler features.', 'formidable' ); | |
| 202 | - } | |
| 203 | - | |
| 204 | 253 | if ( is_callable( 'FrmProStylesController::get_notes_for_styler_preview' ) ) { |
| 205 | 254 | $notes = array_merge( $notes, FrmProStylesController::get_notes_for_styler_preview() ); |
| 206 | 255 | } |
| 207 | 256 | |
| 208 | 257 | $disabled_features_note = $this->get_disabled_features_note(); |
| 258 | + | |
| 209 | 259 | if ( is_string( $disabled_features_note ) ) { |
| 210 | 260 | $notes[] = $disabled_features_note; |
| 211 | 261 | } |
| 212 | 262 | |
| @@ -215,35 +265,18 @@ | ||
| 215 | 265 | |
| 216 | 266 | /** |
| 217 | 267 | * @since 6.0 |
| 218 | 268 | * |
| 219 | - * @return string|false | |
| 269 | + * @return string | |
| 220 | 270 | */ |
| 221 | 271 | private function get_disabled_features_note() { |
| 222 | - $disabled_features = array(); | |
| 223 | - | |
| 224 | - if ( $this->form_includes_captcha ) { | |
| 225 | - $disabled_features[] = __( 'Captcha fields are hidden.', 'formidable' ); | |
| 226 | - } | |
| 227 | - | |
| 228 | - if ( is_callable( 'FrmProStylesController::get_disabled_javascript_features' ) ) { | |
| 229 | - $disabled_pro_features = FrmProStylesController::get_disabled_javascript_features(); | |
| 230 | - if ( is_string( $disabled_pro_features ) ) { | |
| 231 | - $disabled_features[] = $disabled_pro_features; | |
| 232 | - } | |
| 233 | - } | |
| 234 | - | |
| 235 | - if ( $disabled_features ) { | |
| 236 | - return __( 'Not all JavaScript is loaded in this preview.', 'formidable' ) . ' ' . implode( ' ', $disabled_features ); | |
| 237 | - } | |
| 238 | - | |
| 239 | - return false; | |
| 272 | + return __( 'Not all JavaScript is loaded in this preview. Some features will appear differently on the front end.', 'formidable' ); | |
| 240 | 273 | } |
| 241 | 274 | |
| 242 | 275 | /** |
| 243 | 276 | * @since 6.0 |
| 244 | 277 | * |
| 245 | - * @return string|false | |
| 278 | + * @return false|string | |
| 246 | 279 | */ |
| 247 | 280 | private function get_fallback_form_note() { |
| 248 | 281 | if ( ! FrmAppHelper::simple_get( 'form', 'absint' ) ) { |
| 249 | 282 | return __( 'This form is being previewed because no form was selected. Use the form dropdown to select a new preview target.', 'formidable' ); |
| @@ -255,11 +288,12 @@ | ||
| 255 | 288 | * Get all warnings to display above the visual styler preview. |
| 256 | 289 | * |
| 257 | 290 | * @since 6.0 |
| 258 | 291 | * |
| 259 | - * @param WP_Post|stdClass $style A new style is not a WP_Post object. | |
| 292 | + * @param stdClass|WP_Post $style A new style is not a WP_Post object. | |
| 260 | 293 | * @param WP_Post $default_style |
| 261 | 294 | * @param string $view Either 'list' or 'edit'. |
| 295 | + * | |
| 262 | 296 | * @return array<string> |
| 263 | 297 | */ |
| 264 | 298 | public function get_warnings_for_styler_preview( $style, $default_style, $view ) { |
| 265 | 299 | $warnings = array(); |
| @@ -275,8 +309,9 @@ | ||
| 275 | 309 | * @since 6.0 |
| 276 | 310 | * |
| 277 | 311 | * @param int $style_id |
| 278 | 312 | * @param int $default_style_id |
| 313 | + * | |
| 279 | 314 | * @return bool |
| 280 | 315 | */ |
| 281 | 316 | private function should_show_multiple_forms_warning( $style_id, $default_style_id ) { |
| 282 | 317 | $is_default_style = $style_id === $default_style_id; |
| @@ -283,8 +318,9 @@ | ||
| 283 | 318 | |
| 284 | 319 | if ( ! $is_default_style ) { |
| 285 | 320 | // Also check for the conversational default. |
| 286 | 321 | $conversational_style_id = FrmDb::get_var( 'posts', array( 'post_name' => 'lines-no-boxes' ), 'ID' ); |
| 322 | + | |
| 287 | 323 | if ( $conversational_style_id && (int) $conversational_style_id === $style_id ) { |
| 288 | 324 | $is_default_style = true; |
| 289 | 325 | } |
| 290 | 326 | } |
| @@ -311,8 +347,9 @@ | ||
| 311 | 347 | /** |
| 312 | 348 | * @since 6.0 |
| 313 | 349 | * |
| 314 | 350 | * @param WP_Styles $styles |
| 351 | + * | |
| 315 | 352 | * @return void |
| 316 | 353 | */ |
| 317 | 354 | public static function disable_conflicting_wp_admin_css( $styles ) { |
| 318 | 355 | if ( ! is_callable( array( $styles, 'remove' ) ) || ! array_key_exists( 'wp-admin', $styles->registered ) ) { |
| @@ -321,9 +358,10 @@ | ||
| 321 | 358 | |
| 322 | 359 | $styles->remove( 'edit' ); |
| 323 | 360 | |
| 324 | 361 | $wp_admin_dependencies = $styles->registered['wp-admin']->deps; |
| 325 | - $edit_key = array_search( 'edit', $wp_admin_dependencies ); | |
| 362 | + $edit_key = array_search( 'edit', $wp_admin_dependencies, true ); | |
| 363 | + | |
| 326 | 364 | if ( false === $edit_key ) { |
| 327 | 365 | return; |
| 328 | 366 | } |
| 329 | 367 | |
| @@ -331,17 +369,67 @@ | ||
| 331 | 369 | self::remove_wp_admin_dependency( $styles, 'edit' ); |
| 332 | 370 | } |
| 333 | 371 | |
| 334 | 372 | /** |
| 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 | + /** | |
| 335 | 420 | * @since 6.0 |
| 336 | 421 | * |
| 337 | 422 | * @param WP_Styles $styles |
| 338 | 423 | * @param string $key |
| 424 | + * | |
| 339 | 425 | * @return void |
| 340 | 426 | */ |
| 341 | 427 | private static function remove_wp_admin_dependency( $styles, $key ) { |
| 342 | 428 | $dependencies = $styles->registered['wp-admin']->deps; |
| 343 | - $index = array_search( $key, $dependencies ); | |
| 429 | + // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict | |
| 430 | + $index = array_search( $key, $dependencies ); | |
| 431 | + | |
| 344 | 432 | if ( false === $index ) { |
| 345 | 433 | return; |
| 346 | 434 | } |
| 347 | 435 | |