PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 5.2
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v5.2
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 / controllers / FrmStylesController.php

FrmStylesController.php in Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More 5.2, at classes/controllers/FrmStylesController.php

622 lines 17.3 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 class FrmStylesController {
7 public static $post_type = 'frm_styles';
8 public static $screen = 'formidable_page_formidable-styles';
9
10 public static function load_pro_hooks() {
11 if ( FrmAppHelper::pro_is_installed() ) {
12 FrmProStylesController::load_pro_hooks();
13 }
14 }
15
16 public static function register_post_types() {
17 register_post_type(
18 self::$post_type,
19 array(
20 'label' => __( 'Styles', 'formidable' ),
21 'public' => false,
22 'show_ui' => false,
23 'capability_type' => 'page',
24 'capabilities' => array(
25 'edit_post' => 'frm_change_settings',
26 'edit_posts' => 'frm_change_settings',
27 'edit_others_posts' => 'frm_change_settings',
28 'publish_posts' => 'frm_change_settings',
29 'delete_post' => 'frm_change_settings',
30 'delete_posts' => 'frm_change_settings',
31 'read_private_posts' => 'read_private_posts',
32 ),
33 'supports' => array(
34 'title',
35 ),
36 'has_archive' => false,
37 'labels' => array(
38 'name' => __( 'Styles', 'formidable' ),
39 'singular_name' => __( 'Style', 'formidable' ),
40 'menu_name' => __( 'Style', 'formidable' ),
41 'edit' => __( 'Edit', 'formidable' ),
42 'add_new_item' => __( 'Create a New Style', 'formidable' ),
43 'edit_item' => __( 'Edit Style', 'formidable' ),
44 ),
45 )
46 );
47 }
48
49 public static function menu() {
50 add_submenu_page( 'formidable', 'Formidable | ' . __( 'Styles', 'formidable' ), __( 'Styles', 'formidable' ), 'frm_change_settings', 'formidable-styles', 'FrmStylesController::route' );
51 add_submenu_page( 'themes.php', 'Formidable | ' . __( 'Styles', 'formidable' ), __( 'Forms', 'formidable' ), 'frm_change_settings', 'formidable-styles2', 'FrmStylesController::route' );
52 }
53
54 public static function admin_init() {
55 if ( ! FrmAppHelper::is_admin_page( 'formidable-styles' ) && ! FrmAppHelper::is_admin_page( 'formidable-styles2' ) ) {
56 return;
57 }
58
59 self::load_pro_hooks();
60
61 $style_tab = FrmAppHelper::get_param( 'frm_action', '', 'get', 'sanitize_title' );
62 if ( $style_tab === 'manage' || $style_tab === 'custom_css' ) {
63 // we only need to load these styles/scripts on the styler page
64 return;
65 }
66
67 $version = FrmAppHelper::plugin_version();
68 wp_enqueue_script( 'jquery-ui-datepicker' );
69 wp_enqueue_style( 'wp-color-picker' );
70 wp_enqueue_style( 'frm-custom-theme', admin_url( 'admin-ajax.php?action=frmpro_css' ), array(), $version );
71
72 $style = apply_filters( 'frm_style_head', false );
73 if ( $style ) {
74 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 );
75 }
76 }
77
78 /**
79 * @param string $register Either 'enqueue' or 'register'.
80 * @param bool $force True to enqueue/register the style if a form has not been loaded.
81 */
82 public static function enqueue_css( $register = 'enqueue', $force = false ) {
83 global $frm_vars;
84
85 $register_css = ( $register == 'register' );
86 $should_load = $force || ( ( $frm_vars['load_css'] || $register_css ) && ! FrmAppHelper::is_admin() );
87
88 if ( ! $should_load ) {
89 return;
90 }
91
92 $frm_settings = FrmAppHelper::get_settings();
93 if ( $frm_settings->load_style == 'none' ) {
94 return;
95 }
96
97 $css = apply_filters( 'get_frm_stylesheet', self::custom_stylesheet() );
98
99 if ( ! empty( $css ) ) {
100 $css = (array) $css;
101
102 $version = FrmAppHelper::plugin_version();
103
104 foreach ( $css as $css_key => $file ) {
105 if ( $register_css ) {
106 $this_version = self::get_css_version( $css_key, $version );
107 wp_register_style( $css_key, $file, array(), $this_version );
108 }
109
110 $load_on_all = ! FrmAppHelper::is_admin() && 'all' == $frm_settings->load_style;
111 if ( $load_on_all || $register != 'register' ) {
112 wp_enqueue_style( $css_key );
113 }
114 unset( $css_key, $file );
115 }
116
117 if ( $frm_settings->load_style == 'all' ) {
118 $frm_vars['css_loaded'] = true;
119 }
120 }
121 unset( $css );
122
123 add_filter( 'style_loader_tag', 'FrmStylesController::add_tags_to_css', 10, 2 );
124 }
125
126 public static function custom_stylesheet() {
127 global $frm_vars;
128 $stylesheet_urls = array();
129
130 if ( ! isset( $frm_vars['css_loaded'] ) || ! $frm_vars['css_loaded'] ) {
131 //include css in head
132 self::get_url_to_custom_style( $stylesheet_urls );
133 }
134
135 return $stylesheet_urls;
136 }
137
138 private static function get_url_to_custom_style( &$stylesheet_urls ) {
139 $file_name = '/css/' . self::get_file_name();
140 if ( is_readable( FrmAppHelper::plugin_path() . $file_name ) ) {
141 $url = FrmAppHelper::plugin_url() . $file_name;
142 } else {
143 $url = admin_url( 'admin-ajax.php?action=frmpro_css' );
144 }
145 $stylesheet_urls['formidable'] = $url;
146 }
147
148 /**
149 * Use a different stylesheet per site in a multisite install
150 *
151 * @since 3.0.03
152 */
153 public static function get_file_name() {
154 if ( is_multisite() ) {
155 $blog_id = get_current_blog_id();
156 $name = 'formidableforms' . absint( $blog_id ) . '.css';
157 } else {
158 $name = 'formidableforms.css';
159 }
160
161 return $name;
162 }
163
164 private static function get_css_version( $css_key, $version ) {
165 if ( 'formidable' == $css_key ) {
166 $this_version = get_option( 'frm_last_style_update' );
167 if ( ! $this_version ) {
168 $this_version = $version;
169 }
170 } else {
171 $this_version = $version;
172 }
173
174 return $this_version;
175 }
176
177 public static function add_tags_to_css( $tag, $handle ) {
178 if ( ( 'formidable' == $handle || 'jquery-theme' == $handle ) && strpos( $tag, ' property=' ) === false ) {
179 $frm_settings = FrmAppHelper::get_settings();
180 if ( $frm_settings->use_html ) {
181 $tag = str_replace( ' type="', ' property="stylesheet" type="', $tag );
182 }
183 }
184
185 return $tag;
186 }
187
188 public static function new_style( $return = '' ) {
189 self::load_styler( 'default' );
190 }
191
192 public static function duplicate() {
193 self::load_styler( 'default' );
194 }
195
196 public static function edit( $style_id = false, $message = '' ) {
197 if ( ! $style_id ) {
198 $style_id = FrmAppHelper::get_param( 'id', '', 'get', 'absint' );
199 if ( empty( $style_id ) ) {
200 $style_id = 'default';
201 }
202 }
203
204 if ( 'default' == $style_id ) {
205 $style = 'default';
206 } else {
207 $frm_style = new FrmStyle( $style_id );
208 $style = $frm_style->get_one();
209 $style = $style->ID;
210 }
211
212 self::load_styler( $style, $message );
213 }
214
215 public static function save() {
216 $frm_style = new FrmStyle();
217 $message = '';
218 $post_id = FrmAppHelper::get_post_param( 'ID', false, 'sanitize_title' );
219 $style_nonce = FrmAppHelper::get_post_param( 'frm_style', '', 'sanitize_text_field' );
220
221 if ( $post_id !== false && wp_verify_nonce( $style_nonce, 'frm_style_nonce' ) ) {
222 $id = $frm_style->update( $post_id );
223 if ( empty( $post_id ) && ! empty( $id ) ) {
224 // set the post id to the new style so it will be loaded for editing
225 $post_id = reset( $id );
226 }
227 // include the CSS that includes this style
228 echo '<link href="' . esc_url( admin_url( 'admin-ajax.php?action=frmpro_css' ) ) . '" type="text/css" rel="Stylesheet" class="frm-custom-theme" />';
229 $message = __( 'Your styling settings have been saved.', 'formidable' );
230 }
231
232 return self::edit( $post_id, $message );
233 }
234
235 public static function load_styler( $style, $message = '' ) {
236 global $frm_settings;
237
238 $frm_style = new FrmStyle();
239 $styles = $frm_style->get_all();
240
241 if ( is_numeric( $style ) ) {
242 $style = $styles[ $style ];
243 } elseif ( 'default' == $style ) {
244 $style = $frm_style->get_default_style( $styles );
245 }
246
247 self::add_meta_boxes();
248
249 include( FrmAppHelper::plugin_path() . '/classes/views/styles/show.php' );
250 }
251
252 /**
253 * @param string $message
254 * @param array|object $forms
255 */
256 private static function manage( $message = '', $forms = array() ) {
257 $frm_style = new FrmStyle();
258 $styles = $frm_style->get_all();
259 $default_style = $frm_style->get_default_style( $styles );
260
261 if ( empty( $forms ) ) {
262 $forms = FrmForm::get_published_forms();
263 }
264
265 include( FrmAppHelper::plugin_path() . '/classes/views/styles/manage.php' );
266 }
267
268 private static function manage_styles() {
269 $style_nonce = FrmAppHelper::get_post_param( 'frm_manage_style', '', 'sanitize_text_field' );
270 // phpcs:ignore WordPress.Security.NonceVerification.Missing
271 if ( ! $_POST || ! isset( $_POST['style'] ) || ! wp_verify_nonce( $style_nonce, 'frm_manage_style_nonce' ) ) {
272 return self::manage();
273 }
274
275 global $wpdb;
276
277 $forms = FrmForm::get_published_forms();
278 foreach ( $forms as $form ) {
279 $new_style = ( isset( $_POST['style'] ) && isset( $_POST['style'][ $form->id ] ) ) ? sanitize_text_field( wp_unslash( $_POST['style'][ $form->id ] ) ) : '';
280 $previous_style = ( isset( $_POST['prev_style'] ) && isset( $_POST['prev_style'][ $form->id ] ) ) ? sanitize_text_field( wp_unslash( $_POST['prev_style'][ $form->id ] ) ) : '';
281 if ( $new_style == $previous_style ) {
282 continue;
283 }
284
285 $form->options['custom_style'] = $new_style;
286
287 $wpdb->update( $wpdb->prefix . 'frm_forms', array( 'options' => maybe_serialize( $form->options ) ), array( 'id' => $form->id ) );
288 unset( $form );
289 }
290
291 $message = __( 'Your form styles have been saved.', 'formidable' );
292
293 return self::manage( $message, $forms );
294 }
295
296 /**
297 * @param string $message
298 * @param FrmStyle|null $style
299 * @return void
300 */
301 public static function custom_css( $message = '', $style = null ) {
302 if ( function_exists( 'wp_enqueue_code_editor' ) ) {
303 $id = 'frm_codemirror_box';
304 $settings = wp_enqueue_code_editor(
305 array(
306 'type' => 'text/css',
307 'codemirror' => array(
308 'indentUnit' => 2,
309 'tabSize' => 2,
310 ),
311 )
312 );
313 } else {
314 $settings = false;
315 }
316
317 if ( empty( $settings ) ) {
318 $id = 'frm_custom_css_box';
319 }
320
321 if ( ! isset( $style ) ) {
322 $frm_style = new FrmStyle();
323 $style = $frm_style->get_default_style();
324 }
325
326 include FrmAppHelper::plugin_path() . '/classes/views/styles/custom_css.php';
327 }
328
329 public static function save_css() {
330 $frm_style = new FrmStyle();
331
332 $message = '';
333 $post_id = FrmAppHelper::get_post_param( 'ID', false, 'sanitize_text_field' );
334 $nonce = FrmAppHelper::get_post_param( 'frm_custom_css', '', 'sanitize_text_field' );
335 if ( wp_verify_nonce( $nonce, 'frm_custom_css_nonce' ) ) {
336 $frm_style->update( $post_id );
337 $message = __( 'Your styling settings have been saved.', 'formidable' );
338 }
339
340 return self::custom_css( $message );
341 }
342
343 public static function route() {
344 $action = FrmAppHelper::get_param( 'frm_action', '', 'get', 'sanitize_title' );
345 FrmAppHelper::include_svg();
346
347 switch ( $action ) {
348 case 'edit':
349 case 'save':
350 case 'manage':
351 case 'manage_styles':
352 case 'custom_css':
353 case 'save_css':
354 return self::$action();
355 default:
356 do_action( 'frm_style_action_route', $action );
357 if ( apply_filters( 'frm_style_stop_action_route', false, $action ) ) {
358 return;
359 }
360
361 if ( 'new_style' == $action || 'duplicate' == $action ) {
362 return self::$action();
363 }
364
365 return self::edit();
366 }
367 }
368
369 public static function reset_styling() {
370 FrmAppHelper::permission_check( 'frm_change_settings' );
371 check_ajax_referer( 'frm_ajax', 'nonce' );
372
373 $frm_style = new FrmStyle();
374 $defaults = $frm_style->get_defaults();
375
376 echo json_encode( $defaults );
377 wp_die();
378 }
379
380 public static function change_styling() {
381 check_ajax_referer( 'frm_ajax', 'nonce' );
382
383 $frm_style = new FrmStyle();
384 $defaults = $frm_style->get_defaults();
385 $style = '';
386
387 echo '<style type="text/css">';
388 include( FrmAppHelper::plugin_path() . '/css/_single_theme.css.php' );
389 echo '</style>';
390 wp_die();
391 }
392
393 private static function add_meta_boxes() {
394
395 // setup meta boxes
396 $meta_boxes = array(
397 'general' => __( 'General', 'formidable' ),
398 'form-title' => __( 'Form Title', 'formidable' ),
399 'form-description' => __( 'Form Description', 'formidable' ),
400 'field-labels' => __( 'Field Labels', 'formidable' ),
401 'field-description' => __( 'Field Description', 'formidable' ),
402 'field-colors' => __( 'Field Colors', 'formidable' ),
403 'field-sizes' => __( 'Field Settings', 'formidable' ),
404 'check-box-radio-fields' => __( 'Check Box & Radio Fields', 'formidable' ),
405 'buttons' => __( 'Buttons', 'formidable' ),
406 'form-messages' => __( 'Form Messages', 'formidable' ),
407 );
408
409 /**
410 * Add custom boxes to the styling settings
411 *
412 * @since 2.3
413 */
414 $meta_boxes = apply_filters( 'frm_style_boxes', $meta_boxes );
415
416 foreach ( $meta_boxes as $nicename => $name ) {
417 add_meta_box( $nicename . '-style', $name, 'FrmStylesController::include_style_section', self::$screen, 'side', 'default', $nicename );
418 unset( $nicename, $name );
419 }
420 }
421
422 public static function include_style_section( $atts, $sec ) {
423 extract( $atts ); // phpcs:ignore WordPress.PHP.DontExtract
424 $style = $atts['style'];
425 FrmStylesHelper::prepare_color_output( $style->post_content, false );
426
427 $current_tab = FrmAppHelper::simple_get( 'page-tab', 'sanitize_title', 'default' );
428 $file_name = FrmAppHelper::plugin_path() . '/classes/views/styles/_' . $sec['args'] . '.php';
429
430 /**
431 * Set the location of custom styling settings right before
432 * loading onto the page. If your style box was named "progress",
433 * this hook name will be frm_style_settings_progress.
434 *
435 * @since 2.3
436 */
437 $file_name = apply_filters( 'frm_style_settings_' . $sec['args'], $file_name );
438
439 echo '<div class="frm_grid_container">';
440 include( $file_name );
441 echo '</div>';
442 }
443
444 public static function load_css() {
445 header( 'Content-type: text/css' );
446
447 $frm_style = new FrmStyle();
448 $defaults = $frm_style->get_defaults();
449 $style = '';
450
451 include FrmAppHelper::plugin_path() . '/css/_single_theme.css.php';
452 wp_die();
453 }
454
455 /**
456 * @return void
457 */
458 public static function load_saved_css() {
459 $css = get_transient( 'frmpro_css' );
460
461 ob_start();
462 include FrmAppHelper::plugin_path() . '/css/custom_theme.css.php';
463 $output = ob_get_clean();
464 $output = self::replace_relative_url( $output );
465
466 /**
467 * The API needs to load font icons through a custom URL.
468 *
469 * @since 5.2
470 *
471 * @param string $output
472 */
473 $output = apply_filters( 'frm_saved_css', $output );
474
475 echo $output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
476 wp_die();
477 }
478
479 /**
480 * Replaces relative URL with absolute URL.
481 *
482 * @since 4.11.03
483 *
484 * @param string $css CSS content.
485 * @return string
486 */
487 public static function replace_relative_url( $css ) {
488 $plugin_url = trailingslashit( FrmAppHelper::plugin_url() );
489 return str_replace(
490 array(
491 'url(../',
492 "url('../",
493 'url("../',
494 ),
495 array(
496 'url(' . $plugin_url,
497 "url('" . $plugin_url,
498 'url("' . $plugin_url,
499 ),
500 $css
501 );
502 }
503
504 /**
505 * Check if the Formidable styling should be loaded,
506 * then enqueue it for the footer
507 *
508 * @since 2.0
509 */
510 public static function enqueue_style() {
511 global $frm_vars;
512
513 if ( isset( $frm_vars['css_loaded'] ) && $frm_vars['css_loaded'] ) {
514 // The CSS has already been loaded.
515 return;
516 }
517
518 $frm_settings = FrmAppHelper::get_settings();
519 if ( $frm_settings->load_style != 'none' ) {
520 wp_enqueue_style( 'formidable' );
521 $frm_vars['css_loaded'] = true;
522 }
523 }
524
525 /**
526 * Get the stylesheets for the form settings page
527 */
528 public static function get_style_opts() {
529 $frm_style = new FrmStyle();
530 $styles = $frm_style->get_all();
531
532 return $styles;
533 }
534
535 public static function get_form_style( $form = 'default' ) {
536 $style = FrmFormsHelper::get_form_style( $form );
537
538 if ( empty( $style ) || 1 == $style ) {
539 $style = 'default';
540 }
541
542 $frm_style = new FrmStyle( $style );
543
544 return $frm_style->get_one();
545 }
546
547 /**
548 * @param string $class
549 * @param string $style
550 */
551 public static function get_form_style_class( $class, $style ) {
552 if ( 1 == $style ) {
553 $style = 'default';
554 }
555
556 $frm_style = new FrmStyle( $style );
557 $style = $frm_style->get_one();
558
559 if ( $style ) {
560 $class .= ' frm_style_' . $style->post_name;
561 self::maybe_add_rtl_class( $style, $class );
562 }
563
564 return $class;
565 }
566
567 /**
568 * @param object $style
569 * @param string $class
570 *
571 * @since 3.0
572 */
573 private static function maybe_add_rtl_class( $style, &$class ) {
574 $is_rtl = isset( $style->post_content['direction'] ) && 'rtl' === $style->post_content['direction'];
575 if ( $is_rtl ) {
576 $class .= ' frm_rtl';
577 }
578 }
579
580 /**
581 * @param string $val
582 */
583 public static function get_style_val( $val, $form = 'default' ) {
584 $style = self::get_form_style( $form );
585 if ( $style && isset( $style->post_content[ $val ] ) ) {
586 return $style->post_content[ $val ];
587 }
588 }
589
590 public static function show_entry_styles( $default_styles ) {
591 $frm_style = new FrmStyle( 'default' );
592 $style = $frm_style->get_one();
593
594 if ( ! $style ) {
595 return $default_styles;
596 }
597
598 foreach ( $default_styles as $name => $val ) {
599 $setting = $name;
600 if ( 'border_width' == $name ) {
601 $setting = 'field_border_width';
602 } elseif ( 'alt_bg_color' == $name ) {
603 $setting = 'bg_color_active';
604 }
605 $default_styles[ $name ] = $style->post_content[ $setting ];
606 unset( $name, $val );
607 }
608
609 return $default_styles;
610 }
611
612 public static function &important_style( $important, $field ) {
613 $important = self::get_style_val( 'important_style', $field['form_id'] );
614
615 return $important;
616 }
617
618 public static function do_accordion_sections( $screen, $context, $object ) {
619 return do_accordion_sections( $screen, $context, $object );
620 }
621 }
622