Completed
Push — master ( cc0f2c...3f1f3b )
by Stephanie
03:33
created

FrmStylesController::custom_css()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 25
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

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