Completed
Push — master ( dc5618...7f900e )
by Stephanie
02:52
created

FrmStylesController::change_styling()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 3
nop 0
dl 0
loc 18
rs 9.2
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 = '' ) {
0 ignored issues
show
Unused Code introduced by
The parameter $return is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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' );
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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' ) ) {
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
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 ] ) {
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
Loading history...
introduced by
Detected usage of a non-validated input variable: $_POST
Loading history...
256
                continue;
257
            }
258
259
            $form->options['custom_style'] = $_POST['style'][ $form->id ];
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
Loading history...
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
        wp_enqueue_style('codemirror', FrmAppHelper::plugin_url() . '/css/codemirror.css');
271
        wp_enqueue_script('codemirror', FrmAppHelper::plugin_url() . '/js/codemirror/codemirror.js', array(), '4.7');
272
        wp_enqueue_script( 'codemirror-css', FrmAppHelper::plugin_url() . '/js/codemirror/css.js', array( 'codemirror' ), '4.7' );
273
274
        if ( ! isset($style) ) {
275
            $frm_style = new FrmStyle();
276
            $style = $frm_style->get_default_style();
277
        }
278
279
		include( FrmAppHelper::plugin_path() . '/classes/views/styles/custom_css.php' );
280
    }
281
282
    public static function save_css() {
283
        $frm_style = new FrmStyle();
284
285
        $message = '';
286
		$post_id = FrmAppHelper::get_post_param( 'ID', false, 'sanitize_text_field' );
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
287
		$nonce = FrmAppHelper::get_post_param( 'frm_custom_css', '', 'sanitize_text_field' );
288
		if ( wp_verify_nonce( $nonce, 'frm_custom_css_nonce' ) ) {
289
            $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 286 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...
290
            $message = __( 'Your styling settings have been saved.', 'formidable' );
291
        }
292
293
        return self::custom_css($message);
294
    }
295
296
    public static function route() {
297
		$action = FrmAppHelper::get_param( 'frm_action', '', 'get', 'sanitize_title' );
298
299
        switch ( $action ) {
300
            case 'edit':
301
            case 'save':
302
            case 'manage':
303
            case 'manage_styles':
304
            case 'custom_css':
305
            case 'save_css':
306
				return self::$action();
307
            default:
308
            	do_action( 'frm_style_action_route', $action );
309
            	if ( apply_filters( 'frm_style_stop_action_route', false, $action ) ) {
310
                	return;
311
            	}
312
313
                if ( 'new_style' == $action || 'duplicate' == $action ) {
314
                    return self::$action();
315
                }
316
317
                return self::edit();
318
        }
319
    }
320
321
    public static function reset_styling() {
322
		FrmAppHelper::permission_check('frm_change_settings');
323
        check_ajax_referer( 'frm_ajax', 'nonce' );
324
325
        $frm_style = new FrmStyle();
326
        $defaults = $frm_style->get_defaults();
327
328
        echo json_encode( $defaults );
329
        wp_die();
330
    }
331
332
    public static function change_styling() {
333
        check_ajax_referer( 'frm_ajax', 'nonce' );
334
335
        $frm_style = new FrmStyle();
336
        $defaults = $frm_style->get_defaults();
337
338
        // remove the # from the colors
339
        foreach ( $_GET['frm_style_setting']['post_content'] as $k => $v ) {
340
            if ( ! is_array($v) && strpos($v, '#') === 0 ) {
341
                $_GET['frm_style_setting']['post_content'][ $k ] = str_replace( '#', '', $v );
342
            }
343
        }
344
345
        echo '<style type="text/css">';
346
		include( FrmAppHelper::plugin_path() . '/css/_single_theme.css.php' );
347
        echo '</style>';
348
        wp_die();
349
    }
350
351
    private static function add_meta_boxes() {
352
353
        // setup meta boxes
354
        $meta_boxes = array(
355
            'general'           => __( 'General', 'formidable' ),
356
            'form-title'        => __( 'Form Title', 'formidable' ),
357
            'form-description'  => __( 'Form Description', 'formidable' ),
358
            'field-labels'      => __( 'Field Labels', 'formidable' ),
359
            'field-description' => __( 'Field Description', 'formidable' ),
360
            'field-colors'      => __( 'Field Colors', 'formidable' ),
361
            'field-sizes'       => __( 'Field Settings', 'formidable' ),
362
            'check-box-radio-fields' => __( 'Check Box & Radio Fields', 'formidable' ),
363
            'section-fields'    => __( 'Section Fields', 'formidable' ),
364
            'date-fields'       => __( 'Date Fields', 'formidable' ),
365
            'buttons'           => __( 'Buttons', 'formidable' ),
366
            'form-messages'     => __( 'Form Messages', 'formidable' ),
367
        );
368
369
        foreach ( $meta_boxes as $nicename => $name ) {
370
			add_meta_box( $nicename . '-style', $name, 'FrmStylesController::include_style_section', self::$screen, 'side', 'default', $nicename );
371
            unset($nicename, $name);
372
        }
373
    }
374
375
	public static function include_style_section( $atts, $sec ) {
376
        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...
377
		$current_tab = FrmAppHelper::simple_get( 'page-tab', 'sanitize_title', 'default' );
378
		include( FrmAppHelper::plugin_path() . '/classes/views/styles/_' . $sec['args'] . '.php' );
379
    }
380
381
    public static function load_css() {
382
        header('Content-type: text/css');
383
384
        $frm_style = new FrmStyle();
385
        $defaults = $frm_style->get_defaults();
386
387
		include( FrmAppHelper::plugin_path() . '/css/_single_theme.css.php' );
388
        wp_die();
389
    }
390
391
	public static function load_saved_css() {
392
		$css = get_transient( 'frmpro_css' );
393
394
		include( FrmAppHelper::plugin_path() . '/css/custom_theme.css.php' );
395
		wp_die();
396
	}
397
398
    /**
399
     * Check if the Formidable styling should be loaded,
400
     * then enqueue it for the footer
401
     * @since 2.0
402
     */
403
    public static function enqueue_style() {
404
        global $frm_vars;
405
406
        if ( isset( $frm_vars['css_loaded'] ) && $frm_vars['css_loaded'] ) {
407
            // the CSS has already been loaded
408
            return;
409
        }
410
411
        $frm_settings = FrmAppHelper::get_settings();
412
        if ( $frm_settings->load_style != 'none' ) {
413
            wp_enqueue_style( 'formidable' );
414
            $frm_vars['css_loaded'] = true;
415
        }
416
    }
417
418
    // Get the stylesheets for the form settings page
419
    public static function get_style_opts() {
420
        $frm_style = new FrmStyle();
421
        $styles = $frm_style->get_all();
422
423
        return $styles;
424
    }
425
426
    public static function get_form_style( $form = 'default' ) {
427
        $style = FrmFormsHelper::get_form_style( $form );
428
429
        if ( empty( $style ) || 1 == $style ) {
430
            $style = 'default';
431
        }
432
433
        $frm_style = new FrmStyle( $style );
434
        return $frm_style->get_one();
435
    }
436
437
    /**
438
     * @param string $class
439
     * @param string $style
440
     */
441
	public static function get_form_style_class( $class, $style ) {
442
        if ( 1 == $style ) {
443
            $style = 'default';
444
        }
445
446
        $frm_style = new FrmStyle($style);
447
        $style = $frm_style->get_one();
448
449
        if ( $style ) {
450
			$class .= ' frm_style_' . $style->post_name;
451
        }
452
453
        return $class;
454
    }
455
456
    /**
457
     * @param string $val
458
     */
459
	public static function get_style_val( $val, $form = 'default' ) {
460
        $style = self::get_form_style($form);
461
        if ( $style && isset( $style->post_content[ $val ] ) ) {
462
            return $style->post_content[ $val ];
463
        }
464
    }
465
466
	public static function show_entry_styles( $default_styles ) {
467
        $frm_style = new FrmStyle('default');
468
        $style = $frm_style->get_one();
469
470
        if ( ! $style ) {
471
            return $default_styles;
472
        }
473
474
        foreach ( $default_styles as $name => $val ) {
475
            $setting = $name;
476
            if ( 'border_width' == $name ) {
477
                $setting = 'field_border_width';
478
            } else if ( 'alt_bg_color' == $name ) {
479
                $setting = 'bg_color_active';
480
            }
481
            $default_styles[ $name ] = $style->post_content[ $setting ];
482
            unset($name, $val);
483
        }
484
485
        return $default_styles;
486
    }
487
488
	public static function &important_style( $important, $field ) {
0 ignored issues
show
Unused Code introduced by
The parameter $important is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
489
        $important = self::get_style_val('important_style', $field['form_id']);
490
        return $important;
491
    }
492
493
    /**
494
     * Fallback for WP < 3.6
495
     */
496
    public static function do_accordion_sections( $screen, $context, $object ) {
497
        if ( function_exists( 'do_accordion_sections' ) ) {
498
            return do_accordion_sections( $screen, $context, $object );
499
        }
500
501
    	global $wp_meta_boxes;
502
503
        $screen = 'formidable_page_formidable-styles';
504
        $screen = convert_to_screen( $screen );
505
506
    	$page = $screen->id;
507
508
    	$hidden = get_hidden_meta_boxes( $screen );
509
    	?>
510
    	<div id="side-sortables" class="accordion-container">
511
    	<?php
512
    	$i = 0;
513
    	$first_open = false;
514
    	do {
515
			if ( ! isset( $wp_meta_boxes ) || ! isset( $wp_meta_boxes[ $page ] ) || ! isset( $wp_meta_boxes[ $page ][ $context ] ) ) {
516
				break;
517
			}
518
519
    		foreach ( array( 'high', 'core', 'default', 'low' ) as $priority ) {
520
    			if ( isset( $wp_meta_boxes[ $page ][ $context ][ $priority ] ) ) {
521
    				foreach ( $wp_meta_boxes[ $page ][ $context ][ $priority ] as $box ) {
522
    					if ( false == $box || ! $box['title'] ) {
523
    						continue;
524
						}
525
526
    					$i++;
527
    					$hidden_class = in_array( $box['id'], $hidden ) ? 'hide-if-js' : '';
528
529
    					if ( ! $first_open && empty( $hidden_class ) ) {
530
    						$first_open = true;
531
    					}
532
533
    					?>
534
						<div class="postbox <?php echo esc_attr( $box['id'] ); ?>">
535
						<div class="handlediv" title="<?php esc_attr_e( 'Click to toggle', 'formidable' ) ?>"><br/></div>
536
                        <h3 class='hndle'><span><?php echo esc_html( $box['title'] ); ?></span></h3>
537
                            <div class="accordion-section-content <?php postbox_classes( $box['id'], $page ); ?>">
538
                                <div class="inside">
539
    								<?php call_user_func( $box['callback'], $object, $box ); ?>
540
    							</div><!-- .inside -->
541
    						</div><!-- .accordion-section-content -->
542
    					</div><!-- .postbox -->
543
    					<?php
544
    				}
545
    			}
546
    		}
547
    	} while ( 0 );
548
    	?>
549
    	</div><!-- .accordion-container -->
550
    	<?php
551
    	return $i;
552
    }
553
}
554