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