Completed
Pull Request — master (#165)
by Stephanie
02:25
created

FrmStylesController::admin_init()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

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