Completed
Push — master ( a92e59...0c6f8c )
by Stephanie
07:26
created

FrmStylesController::duplicate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

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