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

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