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

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