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

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