PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / trunk
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More vtrunk
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/controllers/FrmStylesController.php +343 -119 6.25 → trunk View file →
@@ -19,8 +19,20 @@
19 19 * @var string|null
20 20 */
21 21 private static $message;
22 22
23 + /**
24 + * Cache of the active style object keyed by form ID.
25 + *
26 + * @since 6.32
27 + *
28 + * @var array
29 + */
30 + private static $active_style = array();
31 +
32 + /**
33 + * @return void
34 + */
23 35 public static function load_pro_hooks() {
24 36 if ( FrmAppHelper::pro_is_installed() ) {
25 37 FrmProStylesController::load_pro_hooks();
26 38 }
@@ -25,8 +37,11 @@
25 37 FrmProStylesController::load_pro_hooks();
26 38 }
27 39 }
28 40
41 + /**
42 + * @return void
43 + */
29 44 public static function register_post_types() {
30 45 register_post_type(
31 46 self::$post_type,
32 47 array(
@@ -67,10 +82,10 @@
67 82 *
68 83 * @return void
69 84 */
70 85 public static function menu() {
71 - add_submenu_page( 'formidable', 'Formidable | ' . __( 'Styles', 'formidable' ), __( 'Styles', 'formidable' ), 'frm_change_settings', 'formidable-styles', 'FrmStylesController::route' );
72 - add_submenu_page( 'themes.php', 'Formidable | ' . __( 'Styles', 'formidable' ), __( 'Forms', 'formidable' ), 'frm_change_settings', 'formidable-styles2', 'FrmStylesController::route' );
86 + add_submenu_page( 'formidable', 'Formidable | ' . __( 'Styles', 'formidable' ), __( 'Styles', 'formidable' ), 'frm_change_settings', 'formidable-styles', 'FrmStylesController::route' ); // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong
87 + add_submenu_page( 'themes.php', 'Formidable | ' . __( 'Styles', 'formidable' ), __( 'Forms', 'formidable' ), 'frm_change_settings', 'formidable-styles2', 'FrmStylesController::route' ); // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong
73 88 }
74 89
75 90 /**
76 91 * Remove filters for the visual styler preview.
@@ -128,13 +143,14 @@
128 143 if ( FrmAppHelper::is_style_editor_page( 'edit' ) ) {
129 144 wp_enqueue_style( 'wp-color-picker' );
130 145 }
131 146
132 - wp_enqueue_style( 'frm-custom-theme', admin_url( 'admin-ajax.php?action=frmpro_css' ), array(), $version );
147 + wp_enqueue_style( 'frm-custom-theme', admin_url( 'admin-ajax.php?action=frmpro_css&frm_scope_custom_css=1' ), array(), $version );
133 148
134 149 $style = apply_filters( 'frm_style_head', false );
150 +
135 151 if ( $style ) {
136 - wp_enqueue_style( 'frm-single-custom-theme', admin_url( 'admin-ajax.php?action=frmpro_load_css&flat=1' ) . '&' . http_build_query( $style->post_content ), array(), $version );
152 + wp_enqueue_style( 'frm-single-custom-theme', admin_url( 'admin-ajax.php?action=frmpro_load_css&flat=1' ) . '&' . http_build_query( $style->post_content ), array(), $version ); // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong
137 153 }
138 154 }
139 155
140 156 /**
@@ -161,8 +177,9 @@
161 177
162 178 /**
163 179 * @param string $register Either 'enqueue' or 'register'.
164 180 * @param bool $force True to enqueue/register the style if a form has not been loaded.
181 + *
165 182 * @return void
166 183 */
167 184 public static function enqueue_css( $register = 'enqueue', $force = false ) {
168 185 global $frm_vars;
@@ -174,8 +191,9 @@
174 191 return;
175 192 }
176 193
177 194 $frm_settings = FrmAppHelper::get_settings();
195 +
178 196 if ( $frm_settings->load_style === 'none' ) {
179 197 return;
180 198 }
181 199
@@ -191,8 +209,9 @@
191 209 wp_register_style( $css_key, $file, array(), $this_version );
192 210 }
193 211
194 212 $load_on_all = ! FrmAppHelper::is_admin() && 'all' === $frm_settings->load_style;
213 +
195 214 if ( $load_on_all || $register !== 'register' ) {
196 215 wp_enqueue_style( $css_key );
197 216 }
198 217 unset( $css_key, $file );
@@ -223,18 +242,22 @@
223 242 }
224 243
225 244 /**
226 245 * @param array $stylesheet_urls
246 + *
227 247 * @return void
228 248 */
229 249 private static function get_url_to_custom_style( &$stylesheet_urls ) {
230 - $file_name = '/css/' . self::get_file_name();
231 - if ( is_readable( FrmAppHelper::plugin_path() . $file_name ) ) {
232 - $url = FrmAppHelper::plugin_url() . $file_name;
233 - } else {
234 - $url = admin_url( 'admin-ajax.php?action=frmpro_css' );
250 + $add_css_to_uploads_dir = FrmStyle::add_css_to_uploads_dir();
251 + $file_path = FrmStyle::get_generated_css_file_path( $add_css_to_uploads_dir ) . '/' . self::get_file_name();
252 +
253 + if ( ! is_readable( $file_path ) ) {
254 + $stylesheet_urls['formidable'] = admin_url( 'admin-ajax.php?action=frmpro_css' );
255 + return;
235 256 }
236 - $stylesheet_urls['formidable'] = $url;
257 +
258 + $base_url = $add_css_to_uploads_dir ? wp_upload_dir()['baseurl'] . '/formidable/css/' : FrmAppHelper::plugin_url() . '/css/';
259 + $stylesheet_urls['formidable'] = $base_url . self::get_file_name();
237 260 }
238 261
239 262 /**
240 263 * Use a different stylesheet per site in a multisite install
@@ -239,42 +262,44 @@
239 262 /**
240 263 * Use a different stylesheet per site in a multisite install
241 264 *
242 265 * @since 3.0.03
266 + *
267 + * @return string
243 268 */
244 269 public static function get_file_name() {
245 270 if ( is_multisite() ) {
246 - $blog_id = get_current_blog_id();
247 - $name = 'formidableforms' . absint( $blog_id ) . '.css';
248 - } else {
249 - $name = 'formidableforms.css';
271 + return 'formidableforms' . absint( get_current_blog_id() ) . '.css';
250 272 }
251 273
252 - return $name;
274 + return 'formidableforms.css';
253 275 }
254 276
277 + /**
278 + * @param string $css_key
279 + * @param string $version
280 + *
281 + * @return string
282 + */
255 283 private static function get_css_version( $css_key, $version ) {
256 - if ( 'formidable' == $css_key ) {
257 - $this_version = get_option( 'frm_last_style_update' );
258 - if ( ! $this_version ) {
259 - $this_version = $version;
260 - }
261 - } else {
262 - $this_version = $version;
284 + if ( 'formidable' !== $css_key ) {
285 + return $version;
263 286 }
264 287
265 - return $this_version;
288 + $this_version = get_option( 'frm_last_style_update' );
289 +
290 + return $this_version ? $this_version : $version;
266 291 }
267 292
268 293 /**
269 294 * @param string $tag
270 295 * @param string $handle
296 + *
271 297 * @return string
272 298 */
273 299 public static function add_tags_to_css( $tag, $handle ) {
274 - if ( ( 'formidable' === $handle || 'jquery-theme' === $handle ) && strpos( $tag, ' property=' ) === false ) {
275 - $frm_settings = FrmAppHelper::get_settings();
276 - $tag = str_replace( ' type="', ' property="stylesheet" type="', $tag );
300 + if ( ( 'formidable' === $handle || 'jquery-theme' === $handle ) && ! str_contains( $tag, ' property=' ) ) {
301 + return str_replace( ' type="', ' property="stylesheet" type="', $tag );
277 302 }
278 303
279 304 return $tag;
280 305 }
@@ -322,10 +347,11 @@
322 347
323 348 self::setup_styles_and_scripts_for_styler();
324 349
325 350 $style_id = self::get_style_id_for_styler();
351 +
326 352 if ( ! $style_id ) {
327 - $error_args = array(
353 + $error_args = array(
328 354 'title' => __( 'No styles', 'formidable' ),
329 355 'body' => __( 'You must have a style to use the Visual Styler.', 'formidable' ),
330 356 'cancel_url' => admin_url( 'admin.php?page=formidable' ),
331 357 );
@@ -333,8 +359,9 @@
333 359 return;
334 360 }
335 361
336 362 $form_id = FrmAppHelper::simple_get( 'form', 'absint', 0 );
363 +
337 364 if ( ! $form_id ) {
338 365 $form_id = self::get_form_id_for_style( $style_id );
339 366 }
340 367
@@ -340,9 +367,9 @@
340 367
341 368 $form = FrmForm::getOne( $form_id );
342 369
343 370 if ( ! is_object( $form ) ) {
344 - $error_args = array(
371 + $error_args = array(
345 372 'title' => __( 'No forms', 'formidable' ),
346 373 'body' => __( 'You must have a form to use the Visual Styler.', 'formidable' ),
347 374 'cancel_url' => admin_url( 'admin.php?page=formidable' ),
348 375 );
@@ -349,11 +376,10 @@
349 376 FrmAppController::show_error_modal( $error_args );
350 377 return;
351 378 }
352 379
353 - $frm_style = new FrmStyle( $style_id );
354 - $active_style = $frm_style->get_one();
355 - $default_style = self::get_default_style();
380 + $frm_style = new FrmStyle( $style_id );
381 + $active_style = $frm_style->get_one();
356 382
357 383 self::disable_admin_page_styling_on_submit_buttons();
358 384
359 385 /**
@@ -362,9 +388,9 @@
362 388 * @param array{form:\stdClass} $data
363 389 */
364 390 do_action( 'frm_before_render_style_page', compact( 'form' ) );
365 391
366 - self::render_style_page( $active_style, $form, $default_style );
392 + self::render_style_page( $active_style, $form, self::get_default_style() );
367 393 }
368 394
369 395 /**
370 396 * @since 6.0
@@ -372,8 +398,9 @@
372 398 * @return int
373 399 */
374 400 private static function get_style_id_for_styler() {
375 401 $action = FrmAppHelper::simple_get( 'frm_action' );
402 +
376 403 if ( 'duplicate' === $action ) {
377 404 // The duplicate action uses style_id instead of id for better backward compatibility.
378 405 return FrmAppHelper::simple_get( 'style_id', 'absint', 0 );
379 406 }
@@ -378,8 +405,9 @@
378 405 return FrmAppHelper::simple_get( 'style_id', 'absint', 0 );
379 406 }
380 407
381 408 $style_id = FrmAppHelper::simple_get( 'id', 'absint', 0 );
409 +
382 410 if ( $style_id ) {
383 411 // Always use the style ID from the URL if one is specified.
384 412 return $style_id;
385 413 }
@@ -384,8 +412,9 @@
384 412 return $style_id;
385 413 }
386 414
387 415 $request_form_id = FrmAppHelper::simple_get( 'form', 'absint', 0 );
416 +
388 417 if ( $request_form_id && is_callable( 'FrmProStylesController::get_active_style_for_form' ) ) {
389 418 return FrmProStylesController::get_active_style_for_form( $request_form_id )->ID;
390 419 }
391 420
@@ -397,8 +426,9 @@
397 426 *
398 427 * @since 6.0
399 428 *
400 429 * @param int $style_id
430 + *
401 431 * @return int
402 432 */
403 433 private static function get_form_id_for_style( $style_id ) {
404 434 $check = serialize( array( 'custom_style' => (string) $style_id ) );
@@ -411,9 +441,8 @@
411 441 )
412 442 );
413 443
414 444 if ( ! $form_id ) {
415 - // TODO: Show a message why a random form is being shown (because no form is assigned to the style).
416 445 // Fallback to any form.
417 446 $where = array(
418 447 'status' => 'published',
419 448 // Make sure it's not a repeater.
@@ -445,11 +474,10 @@
445 474 *
446 475 * @return WP_Post
447 476 */
448 477 private static function get_default_style() {
449 - $frm_style = new FrmStyle( 'default' );
450 - $default_style = $frm_style->get_one();
451 - return $default_style;
478 + $frm_style = new FrmStyle( 'default' );
479 + return $frm_style->get_one();
452 480 }
453 481
454 482 /**
455 483 * Save style for form (from Styler list page) via a POST action.
@@ -459,13 +487,15 @@
459 487 * @return void
460 488 */
461 489 private static function save_form_style() {
462 490 $permission_error = FrmAppHelper::permission_nonce_error( 'frm_edit_forms', 'frm_save_form_style', 'frm_save_form_style_nonce' );
491 +
463 492 if ( $permission_error !== false ) {
464 493 wp_die( 'Unable to save form', '', 403 );
465 494 }
466 495
467 496 $style_id = FrmAppHelper::get_post_param( 'style_id', 0, 'absint' );
497 +
468 498 if ( $style_id && ! self::confirm_style_exists_before_setting( $style_id ) ) {
469 499 wp_die( esc_html__( 'Invalid target style', 'formidable' ), esc_html__( 'Invalid target style', 'formidable' ), 400 );
470 500 return;
471 501 }
@@ -485,8 +515,9 @@
485 515 return;
486 516 }
487 517
488 518 $form_id = FrmAppHelper::get_post_param( 'form_id', 0, 'absint' );
519 +
489 520 if ( ! $form_id ) {
490 521 wp_die( esc_html__( 'No form specified', 'formidable' ), esc_html__( 'No form specified', 'formidable' ), 400 );
491 522 return;
492 523 }
@@ -491,18 +522,21 @@
491 522 return;
492 523 }
493 524
494 525 $form = FrmForm::getOne( $form_id );
526 +
495 527 if ( ! $form ) {
496 528 wp_die( esc_html__( 'Form does not exist', 'formidable' ), esc_html__( 'Form does not exist', 'formidable' ), 400 );
497 529 return;
498 530 }
499 531
532 + $previous_style_id = $form->options['custom_style'];
533 +
500 534 // If the default style is selected, use the "Always use default" legacy option instead of the default style.
501 535 // There's also a check here for conversational forms.
502 536 // Without the check it isn't possible to select "Default" because "Always use default" will convert to "Lines" dynamically.
503 - $default_style = self::get_default_style();
504 - if ( $style_id === $default_style->ID && empty( $form->options['chat'] ) ) {
537 +
538 + if ( $style_id === self::get_default_style()->ID && empty( $form->options['chat'] ) ) {
505 539 $style_id = 1;
506 540 }
507 541
508 542 // We want to save a string for consistency. FrmStylesHelper::get_form_count_for_style expects the custom style ID is a string.
@@ -507,13 +541,27 @@
507 541
508 542 // We want to save a string for consistency. FrmStylesHelper::get_form_count_for_style expects the custom style ID is a string.
509 543 $form->options['custom_style'] = (string) $style_id;
510 544
545 + if ( $previous_style_id === $form->options['custom_style'] ) {
546 + // Exit early before updating DB if nothing actually changed.
547 + self::$message = __( 'Successfully updated style.', 'formidable' );
548 + return;
549 + }
550 +
511 551 global $wpdb;
512 552 $wpdb->update( $wpdb->prefix . 'frm_forms', array( 'options' => maybe_serialize( $form->options ) ), array( 'id' => $form->id ) );
513 553
514 554 FrmForm::clear_form_cache();
515 555
556 + /**
557 + * @since 6.25.1
558 + *
559 + * @param int $form_id
560 + * @param int $style_id
561 + */
562 + do_action( 'frm_after_changed_form_style', absint( $form_id ), absint( $style_id ) );
563 +
516 564 self::$message = __( 'Successfully updated style.', 'formidable' );
517 565 }
518 566
519 567 /**
@@ -519,8 +567,9 @@
519 567 /**
520 568 * Validate that we're assigning a form to a style that actually exists before assigning it to a form.
521 569 *
522 570 * @param int $style_id
571 + *
523 572 * @return bool True if the style actually exists.
524 573 */
525 574 private static function confirm_style_exists_before_setting( $style_id ) {
526 575 global $wpdb;
@@ -560,8 +609,9 @@
560 609 *
561 610 * @param stdClass|WP_Post $active_style
562 611 * @param stdClass $form
563 612 * @param WP_Post $default_style
613 + *
564 614 * @return void
565 615 */
566 616 private static function render_style_page( $active_style, $form, $default_style ) {
567 617 $style_views_path = self::get_views_path();
@@ -583,22 +633,44 @@
583 633 $style = $active_style;
584 634 break;
585 635
586 636 case 'duplicate':
587 - $style = clone $active_style;
588 - $new_style = $frm_style->get_new();
637 + $style = clone $active_style;
638 + $new_style = $frm_style->get_new();
639 + $new_name = self::get_new_style_post_name( $new_style->post_name );
640 +
641 + // The single style custom CSS is nested under the old style's scope. Re-scope it to the
642 + // new style's scope so it unnests correctly for display and re-nests correctly on save.
643 + if ( ! empty( $style->post_content['single_style_custom_css'] ) ) {
644 + $css_scope_helper = new FrmCssScopeHelper();
645 + $unnested_css = $css_scope_helper->unnest( $style->post_content['single_style_custom_css'], 'frm_style_' . $style->post_name ); // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong
646 + $style->post_content['single_style_custom_css'] = $css_scope_helper->nest( $unnested_css, 'frm_style_' . $new_name );
647 + }
648 +
589 649 $style->ID = $new_style->ID;
590 - $style->post_name = $new_style->post_name;
591 - unset( $new_style );
650 + $style->post_name = $new_name;
651 + unset( $new_style, $new_name );
592 652 break;
593 653
594 654 case 'new_style':
595 - $style = $frm_style->get_new();
655 + $style = $frm_style->get_new();
656 + $style->post_name = self::get_new_style_post_name( $style->post_name );
596 657 break;
597 - }
658 + }//end switch
598 659
599 660 if ( in_array( $view, array( 'duplicate', 'new_style' ), true ) ) {
600 - $style->post_title = FrmAppHelper::simple_get( 'style_name' );
661 + // A new or duplicated style has no ID yet, so the Rename modal updates the post_title input
662 + // instead of calling an endpoint. Prefer that posted title over $_GET['style_name'] when present.
663 + $posted_title = '';
664 +
665 + // phpcs:ignore WordPress.Security.NonceVerification.Missing
666 + if ( isset( $_POST['frm_style_setting']['post_title'] ) ) {
667 + // The nonce is verified in FrmStylesController::save_style before this renders.
668 + // phpcs:ignore WordPress.Security.NonceVerification.Missing
669 + $posted_title = sanitize_text_field( wp_unslash( $_POST['frm_style_setting']['post_title'] ) );
670 + }
671 +
672 + $style->post_title = '' !== $posted_title ? $posted_title : FrmAppHelper::simple_get( 'style_name' );
601 673 $style->menu_order = 0;
602 674 }
603 675
604 676 if ( ! isset( $style ) ) {
@@ -622,8 +694,27 @@
622 694 include $style_views_path . 'show.php';
623 695 }
624 696
625 697 /**
698 + * Derive the temporary post_name (CSS scope slug) for a new or duplicated style from the chosen
699 + * style name so the displayed scope matches the slug WordPress will store on save.
700 + *
701 + * Falls back to the auto-generated key when no style name was provided so the scope is never empty.
702 + *
703 + * @since 6.32
704 + *
705 + * @param string $fallback The auto-generated post_name to use when no style name is chosen.
706 + *
707 + * @return string
708 + */
709 + private static function get_new_style_post_name( $fallback ) {
710 + $style_name = FrmAppHelper::simple_get( 'style_name' );
711 + $slug = $style_name ? sanitize_title( $style_name ) : '';
712 +
713 + return $slug ? $slug : $fallback;
714 + }
715 +
716 + /**
626 717 * @since 6.0
627 718 *
628 719 * @return string
629 720 */
@@ -636,8 +727,9 @@
636 727 *
637 728 * @since 6.0
638 729 *
639 730 * @param stdClass|WP_Post $style A new style is not a WP_Post object.
731 + *
640 732 * @return void
641 733 */
642 734 private static function force_form_style( $style ) {
643 735 add_filter(
@@ -648,9 +740,9 @@
648 740 /**
649 741 * @param string $class
650 742 */
651 743 function ( $class ) {
652 - return $class && 0 !== strpos( $class, 'frm_style_' );
744 + return $class && ! str_starts_with( $class, 'frm_style_' );
653 745 }
654 746 );
655 747 $split[] = 'frm_style_' . $style->post_name;
656 748 return implode( ' ', $split );
@@ -665,8 +757,10 @@
665 757 *
666 758 * @return void
667 759 */
668 760 public static function save_style() {
761 + FrmAppHelper::permission_check( 'frm_change_settings' );
762 +
669 763 $frm_style = new FrmStyle();
670 764 $post_id = FrmAppHelper::get_post_param( 'ID', false, 'sanitize_title' );
671 765 $style_nonce = FrmAppHelper::get_post_param( 'frm_style', '', 'sanitize_text_field' );
672 766
@@ -676,8 +770,9 @@
676 770 return;
677 771 }
678 772
679 773 $id = $frm_style->update( $post_id );
774 +
680 775 if ( ! $post_id && $id ) {
681 776 self::maybe_redirect_after_save( $id );
682 777 // Set the post id to the new style so it will be loaded for editing.
683 778 $post_id = reset( $id );
@@ -682,8 +777,15 @@
682 777 // Set the post id to the new style so it will be loaded for editing.
683 778 $post_id = reset( $id );
684 779 }
685 780
781 + /**
782 + * @since 6.25.1
783 + *
784 + * @param int $post_id
785 + */
786 + do_action( 'frm_after_saved_style', absint( $post_id ) );
787 +
686 788 self::$message = __( 'Your styling settings have been saved.', 'formidable' );
687 789 }
688 790
689 791 /**
@@ -702,19 +804,20 @@
702 804 *
703 805 * @since 6.0
704 806 *
705 807 * @param array $ids
808 + *
706 809 * @return void
707 810 */
708 811 private static function maybe_redirect_after_save( $ids ) {
709 - $referer = FrmAppHelper::get_server_value( 'HTTP_REFERER' );
710 - $parsed = parse_url( $referer );
711 - $query = $parsed['query'];
712 -
812 + $referer = FrmAppHelper::get_server_value( 'HTTP_REFERER' );
813 + $parsed = parse_url( $referer );
814 + $query = $parsed['query'];
713 815 $current_action = false;
714 816 $actions_to_redirect = array( 'duplicate', 'new_style' );
817 +
715 818 foreach ( $actions_to_redirect as $action ) {
716 - if ( false !== strpos( $query, 'frm_action=' . $action ) ) {
819 + if ( str_contains( $query, 'frm_action=' . $action ) ) {
717 820 $current_action = $action;
718 821 break;
719 822 }
720 823 }
@@ -724,10 +827,9 @@
724 827 return;
725 828 }
726 829
727 830 parse_str( $query, $parsed_query );
728 - $form_id = ! empty( $parsed_query['form'] ) ? absint( $parsed_query['form'] ) : 0;
729 -
831 + $form_id = ! empty( $parsed_query['form'] ) ? absint( $parsed_query['form'] ) : 0;
730 832 $style = new stdClass();
731 833 $style->ID = end( $ids );
732 834 wp_safe_redirect( esc_url_raw( FrmStylesHelper::get_edit_url( $style, $form_id ) ) );
733 835 die();
@@ -737,8 +839,9 @@
737 839 * @since 6.0
738 840 *
739 841 * @param string $message
740 842 * @param array|object $forms
843 + *
741 844 * @return void
742 845 */
743 846 public static function manage( $message = '', $forms = array() ) {
744 847 $frm_style = new FrmStyle();
@@ -762,8 +865,9 @@
762 865 private static function manage_styles() {
763 866 global $wpdb;
764 867
765 868 $forms = FrmForm::get_published_forms();
869 +
766 870 foreach ( $forms as $form ) {
767 871 if ( ! isset( $_POST['style'] ) || ! isset( $_POST['style'][ $form->id ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
768 872 continue;
769 873 }
@@ -779,15 +883,28 @@
779 883 /**
780 884 * Echo content for the Custom CSS page.
781 885 *
782 886 * @param string $message
887 + * @param array $extra_args An array of extra arguments.
888 + *
783 889 * @return void
784 890 */
785 - public static function custom_css( $message = '' ) {
786 - $settings = self::enqueue_codemirror();
787 - $id = $settings ? 'frm_codemirror_box' : 'frm_custom_css_box';
788 - $custom_css = self::get_custom_css();
891 + public static function custom_css( $message = '', $extra_args = array() ) {
892 + $id = $extra_args['id'] ?? 'frm_codemirror_box';
893 + $settings = self::enqueue_codemirror( $id, $extra_args['placeholder'] ?? '' );
894 + $id = $settings ? $id : 'frm_custom_css_box';
895 + $show_errors = $extra_args['show_errors'] ?? true;
896 + $custom_css = $extra_args['custom_css'] ?? self::get_custom_css();
897 + $heading = $extra_args['heading'] ?? __( 'You can add custom css here or in your theme style.css. Any CSS added here will be used anywhere the Formidable CSS is loaded.', 'formidable' ); // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong
898 + $textarea_params = ! empty( $extra_args['textarea_params'] ) ? $extra_args['textarea_params'] : array(
899 + 'name' => 'frm_custom_css',
900 + 'id' => $id,
901 + );
789 902
903 + if ( $settings ) {
904 + $textarea_params['class'] = 'hide-if-js';
905 + }
906 +
790 907 include FrmAppHelper::plugin_path() . '/classes/views/styles/custom_css.php';
791 908 }
792 909
793 910 /**
@@ -794,22 +911,33 @@
794 911 * Get custom CSS code entered in the Custom CSS page.
795 912 *
796 913 * @since 6.0
797 914 *
915 + * @param array|null $single_style_settings The single style settings.
916 + *
798 917 * @return string
799 918 */
800 - public static function get_custom_css() {
919 + public static function get_custom_css( $single_style_settings = null ) {
920 + if ( $single_style_settings ) {
921 + // If the single style settings are passed, return the custom CSS from the single style settings.
922 + if ( ! empty( $single_style_settings['single_style_custom_css'] ) && ! empty( $single_style_settings['enable_style_custom_css'] ) ) {
923 + return $single_style_settings['single_style_custom_css'];
924 + }
925 +
926 + return '';
927 + }
928 +
801 929 $settings = FrmAppHelper::get_settings();
930 +
802 931 if ( is_string( $settings->custom_css ) ) {
803 932 return $settings->custom_css;
804 933 }
805 934
806 935 // If it does not exist, check the default style as a fallback.
807 - $frm_style = new FrmStyle();
808 - $style = $frm_style->get_default_style();
809 - $custom_css = $style->post_content['custom_css'];
936 + $frm_style = new FrmStyle();
937 + $style = $frm_style->get_default_style();
810 938
811 - return $custom_css;
939 + return $style->post_content['custom_css'];
812 940 }
813 941
814 942 /**
815 943 * Enqueue assets for codemirror, built into WordPress since 4.9.
@@ -816,11 +944,14 @@
816 944 * The Custom CSS page uses codemirror.
817 945 *
818 946 * @since 6.0 Previously this code was embedded in self::custom_css.
819 947 *
948 + * @param string $id The ID of the codemirror box.
949 + * @param string $placeholder The placeholder text for the codemirror box.
950 + *
820 951 * @return array|false
821 952 */
822 - private static function enqueue_codemirror() {
953 + private static function enqueue_codemirror( $id = 'frm_codemirror_box', $placeholder = '' ) {
823 954 if ( ! function_exists( 'wp_enqueue_code_editor' ) ) {
824 955 // The WordPress version is likely older than 4.9.
825 956 return false;
826 957 }
@@ -833,8 +964,9 @@
833 964 'tabSize' => 2,
834 965 // As the codemirror box only appears once you click into the Custom CSS tab, we need to auto-refresh.
835 966 // Otherwise the line numbers all end up with a 1px width causing overlap issues with the text in the content.
836 967 'autoRefresh' => true,
968 + 'placeholder' => $placeholder,
837 969 ),
838 970 )
839 971 );
840 972
@@ -841,9 +973,11 @@
841 973 if ( $settings ) {
842 974 wp_add_inline_script(
843 975 'code-editor',
844 976 sprintf(
845 - 'jQuery( function() { wp.codeEditor.initialize( \'frm_codemirror_box\', %s ); } );',
977 + 'jQuery( function() { window.%s_wp_editor = wp.codeEditor.initialize( \'%s\', %s ); } );',
978 + $id,
979 + $id,
846 980 wp_json_encode( $settings )
847 981 )
848 982 );
849 983 }
@@ -895,14 +1029,14 @@
895 1029 FrmAppHelper::permission_check( 'frm_change_settings' );
896 1030 check_ajax_referer( 'frm_ajax', 'nonce' );
897 1031
898 1032 $style_id = FrmAppHelper::get_post_param( 'style_id', '', 'absint' );
1033 +
899 1034 if ( ! $style_id ) {
900 1035 // A style ID is not sent when resetting on the edit page.
901 1036 // Instead of resetting the style, send the defaults back so the inputs can be updated with JavaScript.
902 1037 $frm_style = new FrmStyle();
903 - $defaults = $frm_style->get_defaults();
904 - echo json_encode( $defaults );
1038 + echo json_encode( $frm_style->get_defaults() );
905 1039 wp_die();
906 1040 }
907 1041
908 1042 $frm_style = new FrmStyle();
@@ -955,9 +1089,9 @@
955 1089 /**
956 1090 * @return void
957 1091 */
958 1092 public static function add_meta_boxes() {
959 - // setup meta boxes
1093 + // Setup meta boxes
960 1094 $meta_boxes = array(
961 1095 'general' => __( 'General', 'formidable' ),
962 1096 'form-title' => __( 'Form Title', 'formidable' ),
963 1097 'form-description' => __( 'Form Description', 'formidable' ),
@@ -987,8 +1121,9 @@
987 1121
988 1122 /**
989 1123 * @param array $atts
990 1124 * @param array $sec
1125 + *
991 1126 * @return void
992 1127 */
993 1128 public static function include_style_section( $atts, $sec ) {
994 1129 extract( $atts ); // phpcs:ignore WordPress.PHP.DontExtract
@@ -1059,9 +1194,10 @@
1059 1194 * @return void
1060 1195 */
1061 1196 public static function maybe_hide_sample_form_error_message() {
1062 1197 $referer = FrmAppHelper::get_server_value( 'HTTP_REFERER' );
1063 - if ( false !== strpos( $referer, 'admin.php?page=formidable-styles' ) ) {
1198 +
1199 + if ( str_contains( $referer, 'admin.php?page=formidable-styles' ) ) {
1064 1200 echo '#frm_broken_styles_warning { display: none; }';
1065 1201 }
1066 1202 }
1067 1203
@@ -1070,8 +1206,9 @@
1070 1206 *
1071 1207 * @since 4.11.03
1072 1208 *
1073 1209 * @param string $css CSS content.
1210 + *
1074 1211 * @return string
1075 1212 */
1076 1213 public static function replace_relative_url( $css ) {
1077 1214 $plugin_url = trailingslashit( FrmAppHelper::plugin_url() );
@@ -1094,22 +1231,27 @@
1094 1231 * Check if the Formidable styling should be loaded,
1095 1232 * then enqueue it for the footer
1096 1233 *
1097 1234 * @since 2.0
1235 + *
1236 + * @return void
1098 1237 */
1099 1238 public static function enqueue_style() {
1100 1239 global $frm_vars;
1101 1240
1102 - if ( isset( $frm_vars['css_loaded'] ) && $frm_vars['css_loaded'] ) {
1241 + if ( ! empty( $frm_vars['css_loaded'] ) ) {
1103 1242 // The CSS has already been loaded.
1104 1243 return;
1105 1244 }
1106 1245
1107 1246 $frm_settings = FrmAppHelper::get_settings();
1108 - if ( $frm_settings->load_style !== 'none' ) {
1109 - wp_enqueue_style( 'formidable' );
1110 - $frm_vars['css_loaded'] = true;
1247 +
1248 + if ( $frm_settings->load_style === 'none' ) {
1249 + return;
1111 1250 }
1251 +
1252 + wp_enqueue_style( 'formidable' );
1253 + $frm_vars['css_loaded'] = true;
1112 1254 }
1113 1255
1114 1256 /**
1115 1257 * Get the stylesheets for the form settings page
@@ -1117,11 +1259,9 @@
1117 1259 * @return array<WP_Post>
1118 1260 */
1119 1261 public static function get_style_opts() {
1120 1262 $frm_style = new FrmStyle();
1121 - $styles = $frm_style->get_all();
1122 -
1123 - return $styles;
1263 + return $frm_style->get_all();
1124 1264 }
1125 1265
1126 1266 /**
1127 1267 * Get the style post object for a target form.
@@ -1126,14 +1266,16 @@
1126 1266 /**
1127 1267 * Get the style post object for a target form.
1128 1268 *
1129 1269 * @param bool|object|string $form
1270 + *
1130 1271 * @return WP_Post|null
1131 1272 */
1132 1273 public static function get_form_style( $form = 'default' ) {
1133 1274 $style = FrmFormsHelper::get_form_style( $form );
1134 1275
1135 - if ( empty( $style ) || 1 == $style ) {
1276 + // phpcs:ignore Universal.Operators.StrictComparisons
1277 + if ( ! $style || 1 == $style ) {
1136 1278 $style = 'default';
1137 1279 }
1138 1280
1139 1281 $frm_style = new FrmStyle( $style );
@@ -1140,12 +1282,59 @@
1140 1282 return $frm_style->get_one();
1141 1283 }
1142 1284
1143 1285 /**
1286 + * Get the active style object for a field's form.
1287 + *
1288 + * @since 6.32
1289 + *
1290 + * @param array|int $field The 'field' array.
1291 + *
1292 + * @return object
1293 + */
1294 + public static function get_active_style( $field ) {
1295 + if ( ! is_array( $field ) ) {
1296 + return new stdClass();
1297 + }
1298 +
1299 + $form_id = $field['parent_form_id'] ?? $field['form_id'];
1300 +
1301 + if ( isset( self::$active_style[ $form_id ] ) ) {
1302 + return self::$active_style[ $form_id ];
1303 + }
1304 +
1305 + $active_style = self::get_form_style( $form_id );
1306 +
1307 + if ( ! is_object( $active_style ) ) {
1308 + $active_style = new stdClass();
1309 + }
1310 +
1311 + self::$active_style[ $form_id ] = $active_style;
1312 +
1313 + return self::$active_style[ $form_id ];
1314 + }
1315 +
1316 + /**
1317 + * Get the style setting key that stores the alignment for a field type.
1318 + *
1319 + * @since 6.32
1320 + *
1321 + * @param string $field_type
1322 + *
1323 + * @return string
1324 + */
1325 + public static function get_align_key_for_style_settings( $field_type ) {
1326 + return 'checkbox' === $field_type ? 'check_align' : 'radio_align';
1327 + }
1328 +
1329 + /**
1144 1330 * @param string $class
1145 1331 * @param string $style
1332 + *
1333 + * @return string
1146 1334 */
1147 1335 public static function get_form_style_class( $class, $style ) {
1336 + // phpcs:ignore Universal.Operators.StrictComparisons
1148 1337 if ( 1 == $style ) {
1149 1338 $style = 'default';
1150 1339 }
1151 1340
@@ -1161,13 +1350,17 @@
1161 1350 }
1162 1351
1163 1352 /**
1164 1353 * @since 3.0
1354 + *
1165 1355 * @param object $style
1166 1356 * @param string $class
1357 + *
1358 + * @return void
1167 1359 */
1168 1360 private static function maybe_add_rtl_class( $style, &$class ) {
1169 1361 $is_rtl = isset( $style->post_content['direction'] ) && 'rtl' === $style->post_content['direction'];
1362 +
1170 1363 if ( $is_rtl ) {
1171 1364 $class .= ' frm_rtl';
1172 1365 }
1173 1366 }
@@ -1172,17 +1365,28 @@
1172 1365 }
1173 1366 }
1174 1367
1175 1368 /**
1176 - * @param string $val
1369 + * @param string $val Style setting key.
1370 + * @param int|string $form Form ID or 'default'.
1371 + *
1372 + * @return mixed
1177 1373 */
1178 1374 public static function get_style_val( $val, $form = 'default' ) {
1179 1375 $style = self::get_form_style( $form );
1376 +
1180 1377 if ( $style && isset( $style->post_content[ $val ] ) ) {
1181 1378 return $style->post_content[ $val ];
1182 1379 }
1380 +
1381 + return null;
1183 1382 }
1184 1383
1384 + /**
1385 + * @param array $default_styles
1386 + *
1387 + * @return array
1388 + */
1185 1389 public static function show_entry_styles( $default_styles ) {
1186 1390 $frm_style = new FrmStyle( 'default' );
1187 1391 $style = $frm_style->get_one();
1188 1392
@@ -1191,13 +1395,15 @@
1191 1395 }
1192 1396
1193 1397 foreach ( $default_styles as $name => $val ) {
1194 1398 $setting = $name;
1195 - if ( 'border_width' == $name ) {
1399 +
1400 + if ( 'border_width' === $name ) {
1196 1401 $setting = 'field_border_width';
1197 - } elseif ( 'alt_bg_color' == $name ) {
1402 + } elseif ( 'alt_bg_color' === $name ) {
1198 1403 $setting = 'bg_color_active';
1199 1404 }
1405 +
1200 1406 $default_styles[ $name ] = $style->post_content[ $setting ];
1201 1407 unset( $name, $val );
1202 1408 }
1203 1409
@@ -1203,12 +1409,16 @@
1203 1409
1204 1410 return $default_styles;
1205 1411 }
1206 1412
1207 - public static function &important_style( $important, $field ) {
1208 - $important = self::get_style_val( 'important_style', $field['form_id'] );
1209 -
1210 - return $important;
1413 + /**
1414 + * @param string $important
1415 + * @param array $field
1416 + *
1417 + * @return string
1418 + */
1419 + public static function important_style( $important, $field ) {
1420 + return self::get_style_val( 'important_style', $field['form_id'] );
1211 1421 }
1212 1422
1213 1423 /**
1214 1424 * Duplicate of WordPress do_accordion_section function, it adds an additional svg icon support.
@@ -1214,14 +1424,18 @@
1214 1424 * Duplicate of WordPress do_accordion_section function, it adds an additional svg icon support.
1215 1425 *
1216 1426 * @since 6.8.3
1217 1427 *
1428 + * @param string|WP_Screen $screen Screen ID or screen object.
1429 + * @param string $context Meta box context.
1430 + * @param mixed $data_object Data object passed to callbacks.
1431 + *
1218 1432 * @return int
1219 1433 */
1220 - public static function do_accordion_sections( $screen, $context, $data_object ) {
1434 + public static function do_accordion_sections( $screen, $context, $data_object ) { // phpcs:ignore SlevomatCodingStandard.Complexity.Cognitive.ComplexityTooHigh
1221 1435 global $wp_meta_boxes;
1222 1436
1223 - // the symbol id from icons.svg
1437 + // The symbol id from icons.svg
1224 1438 $icon_ids = array(
1225 1439 'ranking-fields-style' => 'frm_chart_bar_icon',
1226 1440 'section-fields-style' => 'frm-form-title-style',
1227 1441 );
@@ -1227,9 +1441,9 @@
1227 1441 );
1228 1442
1229 1443 wp_enqueue_script( 'accordion' );
1230 1444
1231 - if ( empty( $screen ) ) {
1445 + if ( ! $screen ) {
1232 1446 $screen = get_current_screen();
1233 1447 } elseif ( is_string( $screen ) ) {
1234 1448 $screen = convert_to_screen( $screen );
1235 1449 }
@@ -1235,8 +1449,9 @@
1235 1449 }
1236 1450
1237 1451 $page = $screen->id;
1238 1452
1453 + // phpcs:disable Generic.WhiteSpace.ScopeIndent
1239 1454 ?>
1240 1455 <div id="side-sortables" class="accordion-container">
1241 1456 <ul class="outer-border">
1242 1457 <?php
@@ -1244,44 +1459,46 @@
1244 1459 $first_open = false;
1245 1460
1246 1461 if ( isset( $wp_meta_boxes[ $page ][ $context ] ) ) {
1247 1462 foreach ( array( 'high', 'core', 'default', 'low' ) as $priority ) {
1248 - if ( isset( $wp_meta_boxes[ $page ][ $context ][ $priority ] ) ) {
1249 - foreach ( $wp_meta_boxes[ $page ][ $context ][ $priority ] as $box ) {
1250 - if ( false === $box || ! $box['title'] ) {
1251 - continue;
1252 - }
1463 + if ( ! isset( $wp_meta_boxes[ $page ][ $context ][ $priority ] ) ) {
1464 + continue;
1465 + }
1253 1466
1254 - ++$i;
1255 - $icon_id = array_key_exists( $box['id'], $icon_ids ) ? $icon_ids[ $box['id'] ] : 'frm-' . $box['id'];
1467 + foreach ( $wp_meta_boxes[ $page ][ $context ][ $priority ] as $box ) {
1468 + if ( false === $box || ! $box['title'] ) {
1469 + continue;
1470 + }
1256 1471
1257 - $open_class = '';
1258 - if ( ! $first_open ) {
1259 - $first_open = true;
1260 - $open_class = 'open';
1261 - }
1472 + ++$i;
1473 + $icon_id = array_key_exists( $box['id'], $icon_ids ) ? $icon_ids[ $box['id'] ] : 'frm-' . $box['id'];
1474 + $open_class = '';
1262 1475
1263 - $accordion_content_id = 'frm_style_section_' . $box['id'];
1264 - ?>
1265 - <li class="control-section accordion-section <?php echo esc_attr( $open_class ); ?> <?php echo esc_attr( $box['id'] ); ?>" id="<?php echo esc_attr( $box['id'] ); ?>">
1266 - <h3 class="accordion-section-title hndle">
1267 - <?php
1268 - FrmAppHelper::icon_by_class( 'frmfont ' . $icon_id . ' frm_svg24' );
1269 - echo esc_html( $box['title'] );
1270 - ?>
1271 - <button type="button" aria-expanded="<?php echo esc_attr( 'open' === $open_class ? 'true' : 'false' ); ?>" aria-controls="<?php echo esc_attr( $accordion_content_id ); ?>" aria-label="<?php echo esc_attr( $box['title'] ); ?>">
1272 - <?php FrmAppHelper::icon_by_class( 'frmfont frm_arrowdown8_icon' ); ?>
1273 - </button>
1274 - </h3>
1275 - <div class="accordion-section-content <?php postbox_classes( $box['id'], $page ); ?>" id="<?php echo esc_attr( $accordion_content_id ); ?>">
1276 - <div class="inside">
1277 - <?php call_user_func( $box['callback'], $data_object, $box ); ?>
1278 - </div><!-- .inside -->
1279 - </div><!-- .accordion-section-content -->
1280 - </li><!-- .accordion-section -->
1281 - <?php
1282 - }//end foreach
1283 - }//end if
1476 + if ( ! $first_open ) {
1477 + $first_open = true;
1478 + $open_class = 'open';
1479 + }
1480 +
1481 + $accordion_content_id = 'frm_style_section_' . $box['id'];
1482 + ?>
1483 + <li class="control-section accordion-section <?php echo esc_attr( $open_class ); ?> <?php echo esc_attr( $box['id'] ); ?>" id="<?php echo esc_attr( $box['id'] ); ?>"><?php // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong ?>
1484 + <h3 class="accordion-section-title hndle">
1485 + <?php
1486 + FrmAppHelper::icon_by_class( 'frmfont ' . $icon_id . ' frm_svg24' );
1487 + echo esc_html( $box['title'] );
1488 + ?>
1489 + <button type="button" aria-expanded="<?php echo esc_attr( 'open' === $open_class ? 'true' : 'false' ); ?>" aria-controls="<?php echo esc_attr( $accordion_content_id ); ?>" aria-label="<?php echo esc_attr( $box['title'] ); ?>"><?php // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong ?>
1490 + <?php FrmAppHelper::icon_by_class( 'frmfont frm_arrowdown8_icon' ); ?>
1491 + </button>
1492 + </h3>
1493 + <div class="accordion-section-content <?php postbox_classes( $box['id'], $page ); ?>" id="<?php echo esc_attr( $accordion_content_id ); ?>">
1494 + <div class="inside">
1495 + <?php call_user_func( $box['callback'], $data_object, $box ); ?>
1496 + </div><!-- .inside -->
1497 + </div><!-- .accordion-section-content -->
1498 + </li><!-- .accordion-section -->
1499 + <?php
1500 + }//end foreach
1284 1501 }//end foreach
1285 1502 }//end if
1286 1503 ?>
1287 1504 </ul><!-- .outer-border -->
@@ -1286,8 +1503,9 @@
1286 1503 ?>
1287 1504 </ul><!-- .outer-border -->
1288 1505 </div><!-- .accordion-container -->
1289 1506 <?php
1507 + // phpcs:enable Generic.WhiteSpace.ScopeIndent
1290 1508 return $i;
1291 1509 }
1292 1510
1293 1511 /**
@@ -1298,8 +1516,9 @@
1298 1516 * @return void
1299 1517 */
1300 1518 public static function rename_style() {
1301 1519 $permission_error = FrmAppHelper::permission_nonce_error( 'frm_edit_forms', 'nonce', 'frm_ajax' );
1520 +
1302 1521 if ( $permission_error !== false ) {
1303 1522 $data = array(
1304 1523 'message' => __( 'Unable to rename style', 'formidable' ),
1305 1524 );
@@ -1317,9 +1536,13 @@
1317 1536 wp_send_json_error( $data, 400 );
1318 1537 die();
1319 1538 }
1320 1539
1540 + /**
1541 + * @var WP_Post|null $post
1542 + */
1321 1543 $post = get_post( $style_id );
1544 +
1322 1545 if ( ! $post || $post->post_type !== self::$post_type ) {
1323 1546 $data = array(
1324 1547 'message' => __( 'The style you are renaming either does not exist or it is not a style', 'formidable' ),
1325 1548 );
@@ -1340,8 +1563,9 @@
1340 1563 *
1341 1564 * @since 6.0
1342 1565 *
1343 1566 * @param WP_Styles $styles
1567 + *
1344 1568 * @return void
1345 1569 */
1346 1570 public static function disable_conflicting_wp_admin_css( $styles ) {
1347 1571 if ( ! FrmAppHelper::is_style_editor_page() ) {