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

625 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 /**
330 * @return void
331 */
332 public static function save_css() {
333 $frm_style = new FrmStyle();
334
335 $message = '';
336 $post_id = FrmAppHelper::get_post_param( 'ID', false, 'sanitize_text_field' );
337 $nonce = FrmAppHelper::get_post_param( 'frm_custom_css', '', 'sanitize_text_field' );
338 if ( wp_verify_nonce( $nonce, 'frm_custom_css_nonce' ) ) {
339 $frm_style->update( $post_id );
340 $message = __( 'Your styling settings have been saved.', 'formidable' );
341 }
342
343 self::custom_css( $message );
344 }
345
346 public static function route() {
347 $action = FrmAppHelper::get_param( 'frm_action', '', 'get', 'sanitize_title' );
348 FrmAppHelper::include_svg();
349
350 switch ( $action ) {
351 case 'edit':
352 case 'save':
353 case 'manage':
354 case 'manage_styles':
355 case 'custom_css':
356 case 'save_css':
357 return self::$action();
358 default:
359 do_action( 'frm_style_action_route', $action );
360 if ( apply_filters( 'frm_style_stop_action_route', false, $action ) ) {
361 return;
362 }
363
364 if ( 'new_style' == $action || 'duplicate' == $action ) {
365 return self::$action();
366 }
367
368 return self::edit();
369 }
370 }
371
372 public static function reset_styling() {
373 FrmAppHelper::permission_check( 'frm_change_settings' );
374 check_ajax_referer( 'frm_ajax', 'nonce' );
375
376 $frm_style = new FrmStyle();
377 $defaults = $frm_style->get_defaults();
378
379 echo json_encode( $defaults );
380 wp_die();
381 }
382
383 public static function change_styling() {
384 check_ajax_referer( 'frm_ajax', 'nonce' );
385
386 $frm_style = new FrmStyle();
387 $defaults = $frm_style->get_defaults();
388 $style = '';
389
390 echo '<style type="text/css">';
391 include( FrmAppHelper::plugin_path() . '/css/_single_theme.css.php' );
392 echo '</style>';
393 wp_die();
394 }
395
396 private static function add_meta_boxes() {
397
398 // setup meta boxes
399 $meta_boxes = array(
400 'general' => __( 'General', 'formidable' ),
401 'form-title' => __( 'Form Title', 'formidable' ),
402 'form-description' => __( 'Form Description', 'formidable' ),
403 'field-labels' => __( 'Field Labels', 'formidable' ),
404 'field-description' => __( 'Field Description', 'formidable' ),
405 'field-colors' => __( 'Field Colors', 'formidable' ),
406 'field-sizes' => __( 'Field Settings', 'formidable' ),
407 'check-box-radio-fields' => __( 'Check Box & Radio Fields', 'formidable' ),
408 'buttons' => __( 'Buttons', 'formidable' ),
409 'form-messages' => __( 'Form Messages', 'formidable' ),
410 );
411
412 /**
413 * Add custom boxes to the styling settings
414 *
415 * @since 2.3
416 */
417 $meta_boxes = apply_filters( 'frm_style_boxes', $meta_boxes );
418
419 foreach ( $meta_boxes as $nicename => $name ) {
420 add_meta_box( $nicename . '-style', $name, 'FrmStylesController::include_style_section', self::$screen, 'side', 'default', $nicename );
421 unset( $nicename, $name );
422 }
423 }
424
425 public static function include_style_section( $atts, $sec ) {
426 extract( $atts ); // phpcs:ignore WordPress.PHP.DontExtract
427 $style = $atts['style'];
428 FrmStylesHelper::prepare_color_output( $style->post_content, false );
429
430 $current_tab = FrmAppHelper::simple_get( 'page-tab', 'sanitize_title', 'default' );
431 $file_name = FrmAppHelper::plugin_path() . '/classes/views/styles/_' . $sec['args'] . '.php';
432
433 /**
434 * Set the location of custom styling settings right before
435 * loading onto the page. If your style box was named "progress",
436 * this hook name will be frm_style_settings_progress.
437 *
438 * @since 2.3
439 */
440 $file_name = apply_filters( 'frm_style_settings_' . $sec['args'], $file_name );
441
442 echo '<div class="frm_grid_container">';
443 include( $file_name );
444 echo '</div>';
445 }
446
447 public static function load_css() {
448 header( 'Content-type: text/css' );
449
450 $frm_style = new FrmStyle();
451 $defaults = $frm_style->get_defaults();
452 $style = '';
453
454 include FrmAppHelper::plugin_path() . '/css/_single_theme.css.php';
455 wp_die();
456 }
457
458 /**
459 * @return void
460 */
461 public static function load_saved_css() {
462 $css = get_transient( 'frmpro_css' );
463
464 ob_start();
465 include FrmAppHelper::plugin_path() . '/css/custom_theme.css.php';
466 $output = ob_get_clean();
467 $output = self::replace_relative_url( $output );
468
469 /**
470 * The API needs to load font icons through a custom URL.
471 *
472 * @since 5.2
473 *
474 * @param string $output
475 */
476 $output = apply_filters( 'frm_saved_css', $output );
477
478 echo $output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
479 wp_die();
480 }
481
482 /**
483 * Replaces relative URL with absolute URL.
484 *
485 * @since 4.11.03
486 *
487 * @param string $css CSS content.
488 * @return string
489 */
490 public static function replace_relative_url( $css ) {
491 $plugin_url = trailingslashit( FrmAppHelper::plugin_url() );
492 return str_replace(
493 array(
494 'url(../',
495 "url('../",
496 'url("../',
497 ),
498 array(
499 'url(' . $plugin_url,
500 "url('" . $plugin_url,
501 'url("' . $plugin_url,
502 ),
503 $css
504 );
505 }
506
507 /**
508 * Check if the Formidable styling should be loaded,
509 * then enqueue it for the footer
510 *
511 * @since 2.0
512 */
513 public static function enqueue_style() {
514 global $frm_vars;
515
516 if ( isset( $frm_vars['css_loaded'] ) && $frm_vars['css_loaded'] ) {
517 // The CSS has already been loaded.
518 return;
519 }
520
521 $frm_settings = FrmAppHelper::get_settings();
522 if ( $frm_settings->load_style != 'none' ) {
523 wp_enqueue_style( 'formidable' );
524 $frm_vars['css_loaded'] = true;
525 }
526 }
527
528 /**
529 * Get the stylesheets for the form settings page
530 */
531 public static function get_style_opts() {
532 $frm_style = new FrmStyle();
533 $styles = $frm_style->get_all();
534
535 return $styles;
536 }
537
538 public static function get_form_style( $form = 'default' ) {
539 $style = FrmFormsHelper::get_form_style( $form );
540
541 if ( empty( $style ) || 1 == $style ) {
542 $style = 'default';
543 }
544
545 $frm_style = new FrmStyle( $style );
546
547 return $frm_style->get_one();
548 }
549
550 /**
551 * @param string $class
552 * @param string $style
553 */
554 public static function get_form_style_class( $class, $style ) {
555 if ( 1 == $style ) {
556 $style = 'default';
557 }
558
559 $frm_style = new FrmStyle( $style );
560 $style = $frm_style->get_one();
561
562 if ( $style ) {
563 $class .= ' frm_style_' . $style->post_name;
564 self::maybe_add_rtl_class( $style, $class );
565 }
566
567 return $class;
568 }
569
570 /**
571 * @param object $style
572 * @param string $class
573 *
574 * @since 3.0
575 */
576 private static function maybe_add_rtl_class( $style, &$class ) {
577 $is_rtl = isset( $style->post_content['direction'] ) && 'rtl' === $style->post_content['direction'];
578 if ( $is_rtl ) {
579 $class .= ' frm_rtl';
580 }
581 }
582
583 /**
584 * @param string $val
585 */
586 public static function get_style_val( $val, $form = 'default' ) {
587 $style = self::get_form_style( $form );
588 if ( $style && isset( $style->post_content[ $val ] ) ) {
589 return $style->post_content[ $val ];
590 }
591 }
592
593 public static function show_entry_styles( $default_styles ) {
594 $frm_style = new FrmStyle( 'default' );
595 $style = $frm_style->get_one();
596
597 if ( ! $style ) {
598 return $default_styles;
599 }
600
601 foreach ( $default_styles as $name => $val ) {
602 $setting = $name;
603 if ( 'border_width' == $name ) {
604 $setting = 'field_border_width';
605 } elseif ( 'alt_bg_color' == $name ) {
606 $setting = 'bg_color_active';
607 }
608 $default_styles[ $name ] = $style->post_content[ $setting ];
609 unset( $name, $val );
610 }
611
612 return $default_styles;
613 }
614
615 public static function &important_style( $important, $field ) {
616 $important = self::get_style_val( 'important_style', $field['form_id'] );
617
618 return $important;
619 }
620
621 public static function do_accordion_sections( $screen, $context, $object ) {
622 return do_accordion_sections( $screen, $context, $object );
623 }
624 }
625