Completed
Push — master ( b65f87...90bd64 )
by Stephanie
02:43
created

FrmStylesController::manage_styles()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 14
nc 4
nop 0
dl 0
loc 23
rs 8.5906
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
62
        $version = FrmAppHelper::plugin_version();
63
		$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
64
		wp_enqueue_script( 'jquery-frm-themepicker', FrmAppHelper::plugin_url() . '/js/jquery/jquery-ui-themepicker' . $suffix . '.js', array( 'jquery' ), $version );
65
66
		wp_enqueue_style( 'frm-custom-theme', admin_url('admin-ajax.php?action=frmpro_css' ) );
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 ) );
71
        }
72
    }
73
74
	public static function enqueue_css( $register = 'enqueue' ) {
75
		global $frm_vars;
76
77
		$register_css = ( $register == 'register' );
78
79
		if ( ( $frm_vars['load_css'] || $register_css ) && ! FrmAppHelper::is_admin() ) {
80
			$frm_settings = FrmAppHelper::get_settings();
81
			if ( $frm_settings->load_style == 'none' ) {
82
				return;
83
			}
84
85
			$css = apply_filters( 'get_frm_stylesheet', self::custom_stylesheet() );
86
87
			if ( ! empty( $css ) ) {
88
				$version = FrmAppHelper::plugin_version();
89
90
				foreach ( (array) $css as $css_key => $file ) {
91
					if ( $register_css ) {
92
						$this_version = self::get_css_version( $css_key, $version );
93
						wp_register_style( $css_key, $file, array(), $this_version );
94
					}
95
96
					if ( 'all' == $frm_settings->load_style || $register != 'register' ) {
97
						wp_enqueue_style( $css_key );
98
					}
99
					unset( $css_key, $file );
100
				}
101
102
				if ( $frm_settings->load_style == 'all' ) {
103
					$frm_vars['css_loaded'] = true;
104
				}
105
			}
106
			unset( $css );
107
108
			add_filter( 'style_loader_tag', 'FrmStylesController::add_tags_to_css', 10, 2 );
109
		}
110
	}
111
112
	public static function custom_stylesheet() {
113
		global $frm_vars;
114
		$stylesheet_urls = array();
115
		self::maybe_enqueue_jquery_css();
116
117
		if ( ! isset( $frm_vars['css_loaded'] ) || ! $frm_vars['css_loaded'] ) {
118
			//include css in head
119
			self::get_url_to_custom_style( $stylesheet_urls );
120
		}
121
122
		return $stylesheet_urls;
123
	}
124
125
	private static function get_url_to_custom_style( &$stylesheet_urls ) {
126
		$uploads = FrmStylesHelper::get_upload_base();
127
		$saved_css_path = '/formidable/css/formidablepro.css';
128
		if ( is_readable( $uploads['basedir'] . $saved_css_path ) ) {
129
			$url = $uploads['baseurl'] . $saved_css_path;
130
		} else {
131
			$url = admin_url( 'admin-ajax.php?action=frmpro_css' );
132
		}
133
		$stylesheet_urls['formidable'] = $url;
134
	}
135
136
	private static function get_css_version( $css_key, $version ) {
137
		if ( 'formidable' == $css_key ) {
138
			$this_version = get_option( 'frm_last_style_update' );
139
			if ( ! $this_version ) {
140
				$this_version = $version;
141
			}
142
		} else {
143
			$this_version = $version;
144
		}
145
		return $this_version;
146
	}
147
148
	private static function maybe_enqueue_jquery_css() {
149
		global $frm_vars;
150
		if ( isset( $frm_vars['datepicker_loaded'] ) && ! empty( $frm_vars['datepicker_loaded'] ) ) {
151
			FrmStylesHelper::enqueue_jquery_css();
152
		}
153
	}
154
155
	public static function add_tags_to_css( $tag, $handle ) {
156
		if ( ( 'formidable' == $handle || 'jquery-theme' == $handle ) && strpos( $tag, ' property=' ) === false ) {
157
			$frm_settings = FrmAppHelper::get_settings();
158
			if ( $frm_settings->use_html ) {
159
				$tag = str_replace( ' type="', ' property="stylesheet" type="', $tag );
160
			}
161
		}
162
		return $tag;
163
	}
164
165
	public static function new_style( $return = '' ) {
166
        self::load_styler('default');
167
    }
168
169
	public static function duplicate() {
170
		self::load_styler('default');
171
	}
172
173
	public static function edit( $style_id = false, $message = '' ) {
174
        if ( ! $style_id ) {
175
			$style_id = FrmAppHelper::get_param( 'id', '', 'get', 'absint' );
176
            if ( empty($style_id) ) {
177
                $style_id = 'default';
178
            }
179
        }
180
181
        if ( 'default' == $style_id ) {
182
            $style = 'default';
183
        } else {
184
            $frm_style = new FrmStyle($style_id);
185
            $style = $frm_style->get_one();
186
            $style = $style->ID;
187
        }
188
189
        self::load_styler($style, $message);
190
    }
191
192
    public static function save() {
193
        $frm_style = new FrmStyle();
194
        $message = '';
195
		$post_id = FrmAppHelper::get_post_param( 'ID', false, 'sanitize_title' );
196
		$style_nonce = FrmAppHelper::get_post_param( 'frm_style', '', 'sanitize_text_field' );
197
198
		if ( $post_id !== false && wp_verify_nonce( $style_nonce, 'frm_style_nonce' ) ) {
199
            $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 195 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...
200
            if ( empty($post_id) && ! empty($id) ) {
201
                // set the post id to the new style so it will be loaded for editing
202
                $post_id = reset($id);
203
            }
204
            // include the CSS that includes this style
205
			echo '<link href="' . esc_url( admin_url( 'admin-ajax.php?action=frmpro_css' ) ) . '" type="text/css" rel="Stylesheet" class="frm-custom-theme" />';
206
            $message = __( 'Your styling settings have been saved.', 'formidable' );
207
        }
208
209
        return self::edit($post_id, $message);
210
    }
211
212
	public static function load_styler( $style, $message = '' ) {
213
        global $frm_settings;
214
215
        $frm_style = new FrmStyle();
216
        $styles = $frm_style->get_all();
217
218
        if ( is_numeric($style) ) {
219
            $style = $styles[ $style ];
220
        } else if ( 'default' == $style ) {
221
            $style = $frm_style->get_default_style($styles);
222
        }
223
224
        self::add_meta_boxes();
225
226
		include( FrmAppHelper::plugin_path() . '/classes/views/styles/show.php' );
227
    }
228
229
	/**
230
	 * @param string $message
231
	 * @param array|object $forms
232
	 */
233
	private static function manage( $message = '', $forms = array() ) {
234
        $frm_style = new FrmStyle();
235
        $styles = $frm_style->get_all();
236
        $default_style = $frm_style->get_default_style($styles);
237
238
        if ( empty($forms) ) {
239
            $forms = FrmForm::get_published_forms();
240
        }
241
242
		include( FrmAppHelper::plugin_path() . '/classes/views/styles/manage.php' );
243
    }
244
245
    private static function manage_styles() {
246
		$style_nonce = FrmAppHelper::get_post_param( 'frm_manage_style', '', 'sanitize_text_field' );
247
		if ( ! $_POST || ! isset( $_POST['style'] ) || ! wp_verify_nonce( $style_nonce, 'frm_manage_style_nonce' ) ) {
248
            return self::manage();
249
        }
250
251
        global $wpdb;
252
253
		$forms = FrmForm::get_published_forms();
254
        foreach ( $forms as $form ) {
255
            if ( $_POST['style'][ $form->id ] == $_POST['prev_style'][ $form->id ] ) {
256
                continue;
257
            }
258
259
            $form->options['custom_style'] = $_POST['style'][ $form->id ];
260
261
			$wpdb->update( $wpdb->prefix . 'frm_forms', array( 'options' => maybe_serialize( $form->options ) ), array( 'id' => $form->id ) );
262
            unset($form);
263
        }
264
265
        $message = __( 'Your form styles have been saved.', 'formidable' );
266
        return self::manage($message, $forms);
267
    }
268
269
    public static function custom_css( $message = '', $style = null ) {
270
		if ( function_exists('wp_enqueue_code_editor') ) {
271
			$id = 'frm_codemirror_box';
272
			$settings = wp_enqueue_code_editor( array(
273
				'type' => 'text/css',
274
				'codemirror' => array(
275
					'indentUnit' => 2,
276
					'tabSize' => 2,
277
				),
278
			) );
279
		} else {
280
			$id = 'frm_custom_css_box';
281
			$settings = array();
282
			wp_enqueue_style('codemirror', FrmAppHelper::plugin_url() . '/css/codemirror.css');
283
			wp_enqueue_script('codemirror', FrmAppHelper::plugin_url() . '/js/codemirror/codemirror.js', array(), '4.7');
284
			wp_enqueue_script( 'codemirror-css', FrmAppHelper::plugin_url() . '/js/codemirror/css.js', array( 'codemirror' ), '4.7' );
285
		}
286
287
        if ( ! isset($style) ) {
288
            $frm_style = new FrmStyle();
289
            $style = $frm_style->get_default_style();
290
        }
291
292
		include( FrmAppHelper::plugin_path() . '/classes/views/styles/custom_css.php' );
293
    }
294
295
    public static function save_css() {
296
        $frm_style = new FrmStyle();
297
298
        $message = '';
299
		$post_id = FrmAppHelper::get_post_param( 'ID', false, 'sanitize_text_field' );
300
		$nonce = FrmAppHelper::get_post_param( 'frm_custom_css', '', 'sanitize_text_field' );
301
		if ( wp_verify_nonce( $nonce, 'frm_custom_css_nonce' ) ) {
302
            $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 299 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...
303
            $message = __( 'Your styling settings have been saved.', 'formidable' );
304
        }
305
306
        return self::custom_css($message);
307
    }
308
309
    public static function route() {
310
		$action = FrmAppHelper::get_param( 'frm_action', '', 'get', 'sanitize_title' );
311
312
        switch ( $action ) {
313
            case 'edit':
314
            case 'save':
315
            case 'manage':
316
            case 'manage_styles':
317
            case 'custom_css':
318
            case 'save_css':
319
				return self::$action();
320
            default:
321
            	do_action( 'frm_style_action_route', $action );
322
            	if ( apply_filters( 'frm_style_stop_action_route', false, $action ) ) {
323
                	return;
324
            	}
325
326
                if ( 'new_style' == $action || 'duplicate' == $action ) {
327
                    return self::$action();
328
                }
329
330
                return self::edit();
331
        }
332
    }
333
334 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...
335
		FrmAppHelper::permission_check('frm_change_settings');
336
        check_ajax_referer( 'frm_ajax', 'nonce' );
337
338
        $frm_style = new FrmStyle();
339
        $defaults = $frm_style->get_defaults();
340
341
        echo json_encode( $defaults );
342
        wp_die();
343
    }
344
345
    public static function change_styling() {
346
        check_ajax_referer( 'frm_ajax', 'nonce' );
347
348
        $frm_style = new FrmStyle();
349
        $defaults = $frm_style->get_defaults();
350
		$style = '';
351
352
        // remove the # from the colors
353
        foreach ( $_GET['frm_style_setting']['post_content'] as $k => $v ) {
354
            if ( ! is_array($v) && strpos($v, '#') === 0 ) {
355
                $_GET['frm_style_setting']['post_content'][ $k ] = str_replace( '#', '', $v );
356
            }
357
        }
358
359
        echo '<style type="text/css">';
360
		include( FrmAppHelper::plugin_path() . '/css/_single_theme.css.php' );
361
        echo '</style>';
362
        wp_die();
363
    }
364
365
    private static function add_meta_boxes() {
366
367
        // setup meta boxes
368
        $meta_boxes = array(
369
            'general'           => __( 'General', 'formidable' ),
370
            'form-title'        => __( 'Form Title', 'formidable' ),
371
            'form-description'  => __( 'Form Description', 'formidable' ),
372
            'field-labels'      => __( 'Field Labels', 'formidable' ),
373
            'field-description' => __( 'Field Description', 'formidable' ),
374
            'field-colors'      => __( 'Field Colors', 'formidable' ),
375
            'field-sizes'       => __( 'Field Settings', 'formidable' ),
376
            'check-box-radio-fields' => __( 'Check Box & Radio Fields', 'formidable' ),
377
            'buttons'           => __( 'Buttons', 'formidable' ),
378
            'form-messages'     => __( 'Form Messages', 'formidable' ),
379
        );
380
381
		/**
382
		 * Add custom boxes to the styling settings
383
		 * @since 2.3
384
		 */
385
		$meta_boxes = apply_filters( 'frm_style_boxes', $meta_boxes );
386
387
        foreach ( $meta_boxes as $nicename => $name ) {
388
			add_meta_box( $nicename . '-style', $name, 'FrmStylesController::include_style_section', self::$screen, 'side', 'default', $nicename );
389
            unset($nicename, $name);
390
        }
391
    }
392
393
	public static function include_style_section( $atts, $sec ) {
394
        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...
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
        }
482
483
        return $class;
484
    }
485
486
    /**
487
     * @param string $val
488
     */
489
	public static function get_style_val( $val, $form = 'default' ) {
490
        $style = self::get_form_style($form);
491
        if ( $style && isset( $style->post_content[ $val ] ) ) {
492
            return $style->post_content[ $val ];
493
        }
494
    }
495
496
	public static function show_entry_styles( $default_styles ) {
497
        $frm_style = new FrmStyle('default');
498
        $style = $frm_style->get_one();
499
500
        if ( ! $style ) {
501
            return $default_styles;
502
        }
503
504
        foreach ( $default_styles as $name => $val ) {
505
            $setting = $name;
506
            if ( 'border_width' == $name ) {
507
                $setting = 'field_border_width';
508
            } else if ( 'alt_bg_color' == $name ) {
509
                $setting = 'bg_color_active';
510
            }
511
            $default_styles[ $name ] = $style->post_content[ $setting ];
512
            unset($name, $val);
513
        }
514
515
        return $default_styles;
516
    }
517
518
	public static function &important_style( $important, $field ) {
519
        $important = self::get_style_val('important_style', $field['form_id']);
520
        return $important;
521
    }
522
523
    /**
524
     * Fallback for WP < 3.6
525
     */
526
    public static function do_accordion_sections( $screen, $context, $object ) {
527
        if ( function_exists( 'do_accordion_sections' ) ) {
528
            return do_accordion_sections( $screen, $context, $object );
529
        }
530
531
    	global $wp_meta_boxes;
532
533
        $screen = 'formidable_page_formidable-styles';
534
        $screen = convert_to_screen( $screen );
535
536
    	$page = $screen->id;
537
538
    	$hidden = get_hidden_meta_boxes( $screen );
539
    	?>
540
    	<div id="side-sortables" class="accordion-container">
541
    	<?php
542
    	$i = 0;
543
    	$first_open = false;
544
    	do {
545
			if ( ! isset( $wp_meta_boxes ) || ! isset( $wp_meta_boxes[ $page ] ) || ! isset( $wp_meta_boxes[ $page ][ $context ] ) ) {
546
				break;
547
			}
548
549
    		foreach ( array( 'high', 'core', 'default', 'low' ) as $priority ) {
550
    			if ( isset( $wp_meta_boxes[ $page ][ $context ][ $priority ] ) ) {
551
    				foreach ( $wp_meta_boxes[ $page ][ $context ][ $priority ] as $box ) {
552
    					if ( false == $box || ! $box['title'] ) {
553
    						continue;
554
						}
555
556
    					$i++;
557
    					$hidden_class = in_array( $box['id'], $hidden ) ? 'hide-if-js' : '';
558
559
    					if ( ! $first_open && empty( $hidden_class ) ) {
560
    						$first_open = true;
561
    					}
562
563
    					?>
564
						<div class="postbox <?php echo esc_attr( $box['id'] ); ?>">
565
						<div class="handlediv" title="<?php esc_attr_e( 'Click to toggle', 'formidable' ) ?>"><br/></div>
566
                        <h3 class='hndle'><span><?php echo esc_html( $box['title'] ); ?></span></h3>
567
                            <div class="accordion-section-content <?php postbox_classes( $box['id'], $page ); ?>">
568
                                <div class="inside">
569
    								<?php call_user_func( $box['callback'], $object, $box ); ?>
570
    							</div><!-- .inside -->
571
    						</div><!-- .accordion-section-content -->
572
    					</div><!-- .postbox -->
573
    					<?php
574
    				}
575
    			}
576
    		}
577
    	} while ( 0 );
578
    	?>
579
    	</div><!-- .accordion-container -->
580
    	<?php
581
    	return $i;
582
    }
583
}
584