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

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