Completed
Push — master ( dd9a96...48f7d9 )
by Stephanie
03:45
created

FrmStylesController::admin_init()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

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