Completed
Pull Request — master (#101)
by Stephanie
03:05
created

FrmStylesController::get_style_opts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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