Completed
Push — master ( ec7df5...df6338 )
by Stephanie
03:50
created

FrmStylesController::maybe_enqueue_jquery_css()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 2
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
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( self::$post_type, array(
15
            'label' => __( 'Styles', 'formidable' ),
16
            'public' => false,
17
            'show_ui' => false,
18
            'capability_type' => 'page',
19
			'capabilities' => array(
20
				'edit_post'     => 'frm_change_settings',
21
				'edit_posts'    => 'frm_change_settings',
22
				'edit_others_posts' => 'frm_change_settings',
23
				'publish_posts' => 'frm_change_settings',
24
				'delete_post'   => 'frm_change_settings',
25
				'delete_posts'  => 'frm_change_settings',
26
				'read_private_posts' => 'read_private_posts',
27
			),
28
            'supports' => array(
29
				'title',
30
            ),
31
            'has_archive' => false,
32
            'labels' => array(
33
				'name' => __( 'Styles', 'formidable' ),
34
				'singular_name' => __( 'Style', 'formidable' ),
35
				'menu_name' => __( 'Style', 'formidable' ),
36
				'edit' => __( 'Edit' ),
37
				'add_new_item' => __( 'Create a New Style', 'formidable' ),
38
				'edit_item'    => __( 'Edit Style', 'formidable' ),
39
			),
40
        ) );
41
    }
42
43
    public static function menu() {
44
		add_submenu_page( 'formidable', 'Formidable | ' . __( 'Styles', 'formidable' ), __( 'Styles', 'formidable' ), 'frm_change_settings', 'formidable-styles', 'FrmStylesController::route' );
45
    }
46
47
	public static function admin_init() {
48
		if ( ! FrmAppHelper::is_admin_page( 'formidable-styles' ) ) {
49
			return;
50
		}
51
52
        self::load_pro_hooks();
53
54
		$style_tab = FrmAppHelper::get_param( 'frm_action', '', 'get', 'sanitize_title' );
55
		if ( $style_tab == 'manage' || $style_tab == 'custom_css' ) {
56
			// we only need to load these styles/scripts on the styler page
57
			return;
58
		}
59
60
		wp_enqueue_script( 'jquery-ui-datepicker' );
61
		wp_enqueue_style( 'wp-color-picker' );
62
		wp_enqueue_style( 'frm-custom-theme', admin_url( 'admin-ajax.php?action=frmpro_css' ) );
63
64
		$style = apply_filters( 'frm_style_head', false );
65
        if ( $style ) {
66
			wp_enqueue_style( 'frm-single-custom-theme', admin_url( 'admin-ajax.php?action=frmpro_load_css&flat=1' ) . '&' . http_build_query( $style->post_content ) );
67
        }
68
    }
69
70
	public static function enqueue_css( $register = 'enqueue' ) {
71
		global $frm_vars;
72
73
		$register_css = ( $register == 'register' );
74
75
		if ( ( $frm_vars['load_css'] || $register_css ) && ! FrmAppHelper::is_admin() ) {
76
			$frm_settings = FrmAppHelper::get_settings();
77
			if ( $frm_settings->load_style == 'none' ) {
78
				return;
79
			}
80
81
			$css = apply_filters( 'get_frm_stylesheet', self::custom_stylesheet() );
82
83
			if ( ! empty( $css ) ) {
84
				$css = (array) $css;
85
86
				$version = FrmAppHelper::plugin_version();
87
88
				foreach ( $css as $css_key => $file ) {
89
					if ( $register_css ) {
90
						$this_version = self::get_css_version( $css_key, $version );
91
						wp_register_style( $css_key, $file, array(), $this_version );
92
					}
93
94
					if ( 'all' == $frm_settings->load_style || $register != 'register' ) {
95
						wp_enqueue_style( $css_key );
96
					}
97
					unset( $css_key, $file );
98
				}
99
100
				if ( $frm_settings->load_style == 'all' ) {
101
					$frm_vars['css_loaded'] = true;
102
				}
103
			}
104
			unset( $css );
105
106
			add_filter( 'style_loader_tag', 'FrmStylesController::add_tags_to_css', 10, 2 );
107
		}
108
	}
109
110
	public static function custom_stylesheet() {
111
		global $frm_vars;
112
		$stylesheet_urls = array();
113
114
		if ( ! isset( $frm_vars['css_loaded'] ) || ! $frm_vars['css_loaded'] ) {
115
			//include css in head
116
			self::get_url_to_custom_style( $stylesheet_urls );
117
		}
118
119
		return $stylesheet_urls;
120
	}
121
122
	private static function get_url_to_custom_style( &$stylesheet_urls ) {
123
		$file_name = '/css/' . self::get_file_name();
124
		if ( is_readable( FrmAppHelper::plugin_path() . $file_name ) ) {
125
			$url = FrmAppHelper::plugin_url() . $file_name;
126
		} else {
127
			$url = admin_url( 'admin-ajax.php?action=frmpro_css' );
128
		}
129
		$stylesheet_urls['formidable'] = $url;
130
	}
131
132
	/**
133
	 * Use a different stylesheet per site in a multisite install
134
	 *
135
	 * @since 3.0.03
136
	 */
137
	public static function get_file_name() {
138
		if ( is_multisite() ) {
139
			$blog_id = get_current_blog_id();
140
			$name = 'formidableforms' . absint( $blog_id ) . '.css';
141
		} else {
142
			$name = 'formidableforms.css';
143
		}
144
		return $name;
145
	}
146
147
	private static function get_css_version( $css_key, $version ) {
148
		if ( 'formidable' == $css_key ) {
149
			$this_version = get_option( 'frm_last_style_update' );
150
			if ( ! $this_version ) {
151
				$this_version = $version;
152
			}
153
		} else {
154
			$this_version = $version;
155
		}
156
		return $this_version;
157
	}
158
159
	public static function add_tags_to_css( $tag, $handle ) {
160
		if ( ( 'formidable' == $handle || 'jquery-theme' == $handle ) && strpos( $tag, ' property=' ) === false ) {
161
			$frm_settings = FrmAppHelper::get_settings();
162
			if ( $frm_settings->use_html ) {
163
				$tag = str_replace( ' type="', ' property="stylesheet" type="', $tag );
164
			}
165
		}
166
		return $tag;
167
	}
168
169
	public static function new_style( $return = '' ) {
170
		self::load_styler( 'default' );
171
	}
172
173
	public static function duplicate() {
174
		self::load_styler( 'default' );
175
	}
176
177
	public static function edit( $style_id = false, $message = '' ) {
178
		if ( ! $style_id ) {
179
			$style_id = FrmAppHelper::get_param( 'id', '', 'get', 'absint' );
180
			if ( empty( $style_id ) ) {
181
				$style_id = 'default';
182
			}
183
		}
184
185
		if ( 'default' == $style_id ) {
186
			$style = 'default';
187
		} else {
188
			$frm_style = new FrmStyle( $style_id );
189
			$style = $frm_style->get_one();
190
			$style = $style->ID;
191
		}
192
193
		self::load_styler( $style, $message );
194
	}
195
196
	public static function save() {
197
		$frm_style = new FrmStyle();
198
		$message = '';
199
		$post_id = FrmAppHelper::get_post_param( 'ID', false, 'sanitize_title' );
200
		$style_nonce = FrmAppHelper::get_post_param( 'frm_style', '', 'sanitize_text_field' );
201
202
		if ( $post_id !== false && wp_verify_nonce( $style_nonce, 'frm_style_nonce' ) ) {
203
			$id = $frm_style->update( $post_id );
0 ignored issues
show
Bug introduced by
It seems like $post_id defined by \FrmAppHelper::get_post_...alse, 'sanitize_title') on line 199 can also be of type array; however, FrmStyle::update() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
204
			if ( empty( $post_id ) && ! empty( $id ) ) {
205
				// set the post id to the new style so it will be loaded for editing
206
				$post_id = reset( $id );
207
			}
208
			// include the CSS that includes this style
209
			echo '<link href="' . esc_url( admin_url( 'admin-ajax.php?action=frmpro_css' ) ) . '" type="text/css" rel="Stylesheet" class="frm-custom-theme" />';
210
			$message = __( 'Your styling settings have been saved.', 'formidable' );
211
		}
212
213
		return self::edit( $post_id, $message );
214
	}
215
216
	public static function load_styler( $style, $message = '' ) {
217
        global $frm_settings;
218
219
        $frm_style = new FrmStyle();
220
        $styles = $frm_style->get_all();
221
222
		if ( is_numeric( $style ) ) {
223
			$style = $styles[ $style ];
224
		} elseif ( 'default' == $style ) {
225
			$style = $frm_style->get_default_style( $styles );
226
		}
227
228
        self::add_meta_boxes();
229
230
		include( FrmAppHelper::plugin_path() . '/classes/views/styles/show.php' );
231
    }
232
233
	/**
234
	 * @param string $message
235
	 * @param array|object $forms
236
	 */
237
	private static function manage( $message = '', $forms = array() ) {
238
		$frm_style = new FrmStyle();
239
		$styles = $frm_style->get_all();
240
		$default_style = $frm_style->get_default_style( $styles );
241
242
		if ( empty( $forms ) ) {
243
			$forms = FrmForm::get_published_forms();
244
		}
245
246
		include( FrmAppHelper::plugin_path() . '/classes/views/styles/manage.php' );
247
	}
248
249
    private static function manage_styles() {
250
		$style_nonce = FrmAppHelper::get_post_param( 'frm_manage_style', '', 'sanitize_text_field' );
251
		if ( ! $_POST || ! isset( $_POST['style'] ) || ! wp_verify_nonce( $style_nonce, 'frm_manage_style_nonce' ) ) {
252
            return self::manage();
253
        }
254
255
        global $wpdb;
256
257
		$forms = FrmForm::get_published_forms();
258
        foreach ( $forms as $form ) {
259
            if ( $_POST['style'][ $form->id ] == $_POST['prev_style'][ $form->id ] ) {
260
                continue;
261
            }
262
263
            $form->options['custom_style'] = $_POST['style'][ $form->id ];
264
265
			$wpdb->update( $wpdb->prefix . 'frm_forms', array( 'options' => maybe_serialize( $form->options ) ), array( 'id' => $form->id ) );
266
			unset( $form );
267
        }
268
269
        $message = __( 'Your form styles have been saved.', 'formidable' );
270
		return self::manage( $message, $forms );
271
    }
272
273
    public static function custom_css( $message = '', $style = null ) {
274
		if ( function_exists( 'wp_enqueue_code_editor' ) ) {
275
			$id = 'frm_codemirror_box';
276
			$settings = wp_enqueue_code_editor( array(
277
				'type' => 'text/css',
278
				'codemirror' => array(
279
					'indentUnit' => 2,
280
					'tabSize' => 2,
281
				),
282
			) );
283
		} else {
284
			$id = 'frm_custom_css_box';
285
			$settings = array();
286
			wp_enqueue_style( 'codemirror', FrmAppHelper::plugin_url() . '/css/codemirror.css' );
287
			wp_enqueue_script( 'codemirror', FrmAppHelper::plugin_url() . '/js/codemirror/codemirror.js', array(), '4.7' );
288
			wp_enqueue_script( 'codemirror-css', FrmAppHelper::plugin_url() . '/js/codemirror/css.js', array( 'codemirror' ), '4.7' );
289
		}
290
291
		if ( ! isset( $style ) ) {
292
			$frm_style = new FrmStyle();
293
			$style = $frm_style->get_default_style();
294
		}
295
296
		include( FrmAppHelper::plugin_path() . '/classes/views/styles/custom_css.php' );
297
	}
298
299
	public static function save_css() {
300
		$frm_style = new FrmStyle();
301
302
		$message = '';
303
		$post_id = FrmAppHelper::get_post_param( 'ID', false, 'sanitize_text_field' );
304
		$nonce = FrmAppHelper::get_post_param( 'frm_custom_css', '', 'sanitize_text_field' );
305
		if ( wp_verify_nonce( $nonce, 'frm_custom_css_nonce' ) ) {
306
			$frm_style->update( $post_id );
0 ignored issues
show
Bug introduced by
It seems like $post_id defined by \FrmAppHelper::get_post_... 'sanitize_text_field') on line 303 can also be of type array; however, FrmStyle::update() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
307
			$message = __( 'Your styling settings have been saved.', 'formidable' );
308
		}
309
310
		return self::custom_css( $message );
311
	}
312
313
    public static function route() {
314
		$action = FrmAppHelper::get_param( 'frm_action', '', 'get', 'sanitize_title' );
315
316
        switch ( $action ) {
317
            case 'edit':
318
            case 'save':
319
            case 'manage':
320
            case 'manage_styles':
321
            case 'custom_css':
322
            case 'save_css':
323
				return self::$action();
324
            default:
325
            	do_action( 'frm_style_action_route', $action );
326
            	if ( apply_filters( 'frm_style_stop_action_route', false, $action ) ) {
327
                	return;
328
            	}
329
330
                if ( 'new_style' == $action || 'duplicate' == $action ) {
331
                    return self::$action();
332
                }
333
334
                return self::edit();
335
        }
336
    }
337
338 View Code Duplication
    public static function reset_styling() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
339
		FrmAppHelper::permission_check( 'frm_change_settings' );
340
        check_ajax_referer( 'frm_ajax', 'nonce' );
341
342
        $frm_style = new FrmStyle();
343
        $defaults = $frm_style->get_defaults();
344
345
        echo json_encode( $defaults );
346
        wp_die();
347
    }
348
349
	public static function change_styling() {
350
		check_ajax_referer( 'frm_ajax', 'nonce' );
351
352
		$frm_style = new FrmStyle();
353
		$defaults = $frm_style->get_defaults();
354
		$style = '';
355
356
		echo '<style type="text/css">';
357
		include( FrmAppHelper::plugin_path() . '/css/_single_theme.css.php' );
358
		echo '</style>';
359
		wp_die();
360
	}
361
362
    private static function add_meta_boxes() {
363
364
        // setup meta boxes
365
        $meta_boxes = array(
366
            'general'           => __( 'General', 'formidable' ),
367
            'form-title'        => __( 'Form Title', 'formidable' ),
368
            'form-description'  => __( 'Form Description', 'formidable' ),
369
            'field-labels'      => __( 'Field Labels', 'formidable' ),
370
            'field-description' => __( 'Field Description', 'formidable' ),
371
            'field-colors'      => __( 'Field Colors', 'formidable' ),
372
            'field-sizes'       => __( 'Field Settings', 'formidable' ),
373
            'check-box-radio-fields' => __( 'Check Box & Radio Fields', 'formidable' ),
374
            'buttons'           => __( 'Buttons', 'formidable' ),
375
            'form-messages'     => __( 'Form Messages', 'formidable' ),
376
        );
377
378
		/**
379
		 * Add custom boxes to the styling settings
380
		 * @since 2.3
381
		 */
382
		$meta_boxes = apply_filters( 'frm_style_boxes', $meta_boxes );
383
384
		foreach ( $meta_boxes as $nicename => $name ) {
385
			add_meta_box( $nicename . '-style', $name, 'FrmStylesController::include_style_section', self::$screen, 'side', 'default', $nicename );
386
			unset( $nicename, $name );
387
		}
388
	}
389
390
	public static function include_style_section( $atts, $sec ) {
391
		extract( $atts );
0 ignored issues
show
introduced by
extract() usage is highly discouraged, due to the complexity and unintended issues it might cause.
Loading history...
392
		$style = $atts['style'];
393
		FrmStylesHelper::prepare_color_output( $style->post_content, false );
394
395
		$current_tab = FrmAppHelper::simple_get( 'page-tab', 'sanitize_title', 'default' );
396
		$file_name = FrmAppHelper::plugin_path() . '/classes/views/styles/_' . $sec['args'] . '.php';
397
398
		/**
399
		 * Set the location of custom styling settings right before
400
		 * loading onto the page. If your style box was named "progress",
401
		 * this hook name will be frm_style_settings_progress.
402
		 *
403
		 * @since 2.3
404
		 */
405
		$file_name = apply_filters( 'frm_style_settings_' . $sec['args'], $file_name );
406
407
		include( $file_name );
408
    }
409
410 View Code Duplication
    public static function load_css() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
411
		header( 'Content-type: text/css' );
412
413
        $frm_style = new FrmStyle();
414
        $defaults = $frm_style->get_defaults();
415
		$style = '';
416
417
		include( FrmAppHelper::plugin_path() . '/css/_single_theme.css.php' );
418
        wp_die();
419
    }
420
421
	public static function load_saved_css() {
422
		$css = get_transient( 'frmpro_css' );
423
424
		include( FrmAppHelper::plugin_path() . '/css/custom_theme.css.php' );
425
		wp_die();
426
	}
427
428
    /**
429
     * Check if the Formidable styling should be loaded,
430
     * then enqueue it for the footer
431
     * @since 2.0
432
     */
433
    public static function enqueue_style() {
434
        global $frm_vars;
435
436
        if ( isset( $frm_vars['css_loaded'] ) && $frm_vars['css_loaded'] ) {
437
            // the CSS has already been loaded
438
            return;
439
        }
440
441
        $frm_settings = FrmAppHelper::get_settings();
442
        if ( $frm_settings->load_style != 'none' ) {
443
            wp_enqueue_style( 'formidable' );
444
            $frm_vars['css_loaded'] = true;
445
        }
446
    }
447
448
    // Get the stylesheets for the form settings page
449
    public static function get_style_opts() {
450
        $frm_style = new FrmStyle();
451
        $styles = $frm_style->get_all();
452
453
        return $styles;
454
    }
455
456
    public static function get_form_style( $form = 'default' ) {
457
        $style = FrmFormsHelper::get_form_style( $form );
458
459
        if ( empty( $style ) || 1 == $style ) {
460
            $style = 'default';
461
        }
462
463
        $frm_style = new FrmStyle( $style );
464
        return $frm_style->get_one();
465
    }
466
467
    /**
468
     * @param string $class
469
     * @param string $style
470
     */
471
	public static function get_form_style_class( $class, $style ) {
472
		if ( 1 == $style ) {
473
			$style = 'default';
474
		}
475
476
		$frm_style = new FrmStyle( $style );
477
		$style = $frm_style->get_one();
478
479
		if ( $style ) {
480
			$class .= ' frm_style_' . $style->post_name;
481
			self::maybe_add_rtl_class( $style, $class );
482
		}
483
484
		return $class;
485
	}
486
487
	/**
488
	 * @param object $style
489
	 * @param string $class
490
	 *
491
	 * @since 3.0
492
	 */
493
	private static function maybe_add_rtl_class( $style, &$class ) {
494
		$is_rtl = isset( $style->post_content['direction'] ) && 'rtl' === $style->post_content['direction'];
495
		if ( $is_rtl ) {
496
			$class .= ' frm_rtl';
497
		}
498
	}
499
500
    /**
501
     * @param string $val
502
     */
503
	public static function get_style_val( $val, $form = 'default' ) {
504
		$style = self::get_form_style( $form );
505
		if ( $style && isset( $style->post_content[ $val ] ) ) {
506
			return $style->post_content[ $val ];
507
		}
508
	}
509
510
	public static function show_entry_styles( $default_styles ) {
511
		$frm_style = new FrmStyle( 'default' );
512
        $style = $frm_style->get_one();
513
514
        if ( ! $style ) {
515
            return $default_styles;
516
        }
517
518
        foreach ( $default_styles as $name => $val ) {
519
            $setting = $name;
520
            if ( 'border_width' == $name ) {
521
                $setting = 'field_border_width';
522
            } else if ( 'alt_bg_color' == $name ) {
523
                $setting = 'bg_color_active';
524
            }
525
            $default_styles[ $name ] = $style->post_content[ $setting ];
526
			unset( $name, $val );
527
        }
528
529
        return $default_styles;
530
    }
531
532
	public static function &important_style( $important, $field ) {
533
		$important = self::get_style_val( 'important_style', $field['form_id'] );
534
        return $important;
535
    }
536
537
    /**
538
     * Fallback for WP < 3.6
539
     */
540
    public static function do_accordion_sections( $screen, $context, $object ) {
541
        if ( function_exists( 'do_accordion_sections' ) ) {
542
            return do_accordion_sections( $screen, $context, $object );
543
        }
544
545
    	global $wp_meta_boxes;
546
547
        $screen = 'formidable_page_formidable-styles';
548
        $screen = convert_to_screen( $screen );
549
550
    	$page = $screen->id;
551
552
    	$hidden = get_hidden_meta_boxes( $screen );
553
    	?>
554
    	<div id="side-sortables" class="accordion-container">
555
    	<?php
556
    	$i = 0;
557
    	$first_open = false;
558
    	do {
559
			if ( ! isset( $wp_meta_boxes ) || ! isset( $wp_meta_boxes[ $page ] ) || ! isset( $wp_meta_boxes[ $page ][ $context ] ) ) {
560
				break;
561
			}
562
563
    		foreach ( array( 'high', 'core', 'default', 'low' ) as $priority ) {
564
    			if ( isset( $wp_meta_boxes[ $page ][ $context ][ $priority ] ) ) {
565
    				foreach ( $wp_meta_boxes[ $page ][ $context ][ $priority ] as $box ) {
566
    					if ( false == $box || ! $box['title'] ) {
567
    						continue;
568
						}
569
570
    					$i++;
571
    					$hidden_class = in_array( $box['id'], $hidden ) ? 'hide-if-js' : '';
572
573
    					if ( ! $first_open && empty( $hidden_class ) ) {
574
    						$first_open = true;
575
    					}
576
577
    					?>
578
						<div class="postbox <?php echo esc_attr( $box['id'] ); ?>">
579
						<div class="handlediv" title="<?php esc_attr_e( 'Click to toggle', 'formidable' ); ?>"><br/></div>
580
                        <h3 class='hndle'><span><?php echo esc_html( $box['title'] ); ?></span></h3>
581
                            <div class="accordion-section-content <?php postbox_classes( $box['id'], $page ); ?>">
582
                                <div class="inside">
583
    								<?php call_user_func( $box['callback'], $object, $box ); ?>
584
    							</div><!-- .inside -->
585
    						</div><!-- .accordion-section-content -->
586
    					</div><!-- .postbox -->
587
    					<?php
588
    				}
589
    			}
590
    		}
591
    	} while ( 0 );
592
    	?>
593
    	</div><!-- .accordion-container -->
594
    	<?php
595
    	return $i;
596
    }
597
}
598