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

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