LSX_Theme_Customizer::customizer()   B
last analyzed

Complexity

Conditions 11
Paths 32

Size

Total Lines 56
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 30
nc 32
nop 1
dl 0
loc 56
rs 7.3166
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * LSX functions and definitions - Customizer.
4
 *
5
 * @package    lsx
6
 * @subpackage customizer
7
 */
8
9
if ( ! defined( 'ABSPATH' ) ) {
10
	exit;
11
}
12
13
if ( ! class_exists( 'LSX_Theme_Customizer' ) ) :
14
15
	/**
16
	 * Customizer Configuration File
17
	 *
18
	 * @package    lsx
19
	 * @subpackage customizer
20
	 */
21
	class LSX_Theme_Customizer {
22
23
		public $post_types = array();
0 ignored issues
show
Coding Style Documentation introduced by
Missing member variable doc comment
Loading history...
24
		private $controls  = array();
0 ignored issues
show
Coding Style Documentation introduced by
Missing member variable doc comment
Loading history...
25
26
		/**
0 ignored issues
show
Coding Style Documentation introduced by
Doc comment for parameter "$controls" missing
Loading history...
27
		 * Initialize the plugin by setting localization and loading public scripts and styles.
28
		 */
29
		public function __construct( $controls ) {
30
			require get_template_directory() . '/includes/classes/class-lsx-customize-core-control.php';
31
			require get_template_directory() . '/includes/classes/class-lsx-customize-layout-control.php';
32
			require get_template_directory() . '/includes/classes/class-lsx-customize-header-layout-control.php';
33
			require get_template_directory() . '/includes/classes/class-lsx-customize-mobile-header-layout-control.php';
34
35
			$this->controls = $controls;
36
37
			add_action( 'customize_preview_init', array( $this, 'customize_preview_js' ), 20 );
38
			add_action( 'customize_register', array( $this, 'customizer' ), 11 );
39
		}
40
41
		/**
42
		 * Binds JS handlers to make Theme Customizer preview reload changes asynchronously.
43
		 */
44
		public function customize_preview_js() {
45
			wp_enqueue_script( 'lsx_customizer', get_template_directory_uri() . '/assets/js/admin/customizer.js', array( 'customize-preview' ), LSX_VERSION, true );
46
47
			wp_localize_script(
48
				'lsx_customizer',
49
				'lsx_customizer_params',
50
				array(
51
					'template_directory' => get_template_directory_uri(),
52
				)
53
			);
54
		}
55
56
		/**
0 ignored issues
show
Coding Style Documentation introduced by
Doc comment for parameter "$wp_customize" missing
Loading history...
57
		 * Create customiser controls.
58
		 */
59
		public function customizer( $wp_customize ) {
60
			// Start panels.
61
			if ( ! empty( $this->controls['panels'] ) ) {
62
				foreach ( $this->controls['panels'] as $panel_slug => $args ) {
63
					$this->add_panel( $panel_slug, $args, $wp_customize );
64
				}
65
			}
66
67
			// Start sections.
68
			if ( ! empty( $this->controls['sections'] ) ) {
69
				foreach ( $this->controls['sections'] as $section_slug => $args ) {
70
					$this->add_section( $section_slug, $args, $wp_customize );
71
				}
72
			}
73
74
			// Start settings.
75
			if ( ! empty( $this->controls['settings'] ) ) {
76
				foreach ( $this->controls['settings'] as $settings_slug => $args ) {
77
					$this->add_setting( $settings_slug, $args, $wp_customize );
78
				}
79
			}
80
81
			// Start fields.
82
			if ( ! empty( $this->controls['fields'] ) ) {
83
				foreach ( $this->controls['fields'] as $field_slug => $args ) {
84
					$this->add_control( $field_slug, $args, $wp_customize );
85
				}
86
			}
87
88
			// Start selective refresh.
89
			if ( ! empty( $this->controls['selective_refresh'] ) ) {
90
				foreach ( $this->controls['selective_refresh'] as $field_slug => $args ) {
91
					$this->add_selective_refresh( $field_slug, $args, $wp_customize );
92
				}
93
			}
94
95
			$wp_customize->get_setting( 'blogname' )->transport         = 'postMessage';
96
			$wp_customize->get_setting( 'blogdescription' )->transport  = 'postMessage';
97
			$wp_customize->get_setting( 'background_color' )->transport = 'postMessage';
98
99
			$wp_customize->selective_refresh->add_partial(
100
				'blogname',
101
				array(
102
					'selector'        => 'h1.site-title a',
103
					'render_callback' => function() {
104
						bloginfo( 'name' );
105
					},
106
				)
107
			);
108
109
			$wp_customize->selective_refresh->add_partial(
110
				'blogdescription',
111
				array(
112
					'selector'        => '.site-description',
113
					'render_callback' => function() {
114
						bloginfo( 'description' );
115
					},
116
				)
117
			);
118
		}
119
120
		/**
0 ignored issues
show
Coding Style Documentation introduced by
Doc comment for parameter "$slug" missing
Loading history...
Coding Style Documentation introduced by
Doc comment for parameter "$args" missing
Loading history...
Coding Style Documentation introduced by
Doc comment for parameter "$wp_customize" missing
Loading history...
121
		 * Create a panel.
122
		 */
123
		private function add_panel( $slug, $args, $wp_customize ) {
124
			$default_args = array(
125
				'title'       => null,
126
				'description' => null,
127
			);
128
129
			$wp_customize->add_panel(
130
				$slug,
131
				array_merge( $default_args, $args )
132
			);
133
		}
134
135
		/**
0 ignored issues
show
Coding Style Documentation introduced by
Doc comment for parameter "$slug" missing
Loading history...
Coding Style Documentation introduced by
Doc comment for parameter "$args" missing
Loading history...
Coding Style Documentation introduced by
Doc comment for parameter "$wp_customize" missing
Loading history...
136
		 * Create a section.
137
		 */
138
		private function add_section( $slug, $args, $wp_customize ) {
139
			$default_args = array(
140
				'capability'  => 'edit_theme_options',
141
				'description' => null,
142
			);
143
144
			$wp_customize->add_section( $slug, array_merge( $default_args, $args ) );
145
		}
146
147
		/**
0 ignored issues
show
Coding Style Documentation introduced by
Doc comment for parameter "$slug" missing
Loading history...
Coding Style Documentation introduced by
Doc comment for parameter "$args" missing
Loading history...
Coding Style Documentation introduced by
Doc comment for parameter "$wp_customize" missing
Loading history...
148
		 * Create a setting.
149
		 */
150
		private function add_setting( $slug, $args, $wp_customize ) {
151
			$wp_customize->add_setting(
152
				$slug,
153
				array_merge(
154
					array(
155
						'default'           => null,
156
						'type'              => 'theme_mod',
157
						'capability'        => 'edit_theme_options',
158
						'transport'         => 'postMessage',
159
						'sanitize_callback' => 'lsx_sanitize_choices',
160
					),
161
					$args
162
				)
163
			);
164
		}
165
166
		/**
0 ignored issues
show
Coding Style Documentation introduced by
Doc comment for parameter "$slug" missing
Loading history...
Coding Style Documentation introduced by
Doc comment for parameter "$args" missing
Loading history...
Coding Style Documentation introduced by
Doc comment for parameter "$wp_customize" missing
Loading history...
167
		 * Create a control.
168
		 */
169
		private function add_control( $slug, $args, $wp_customize ) {
170
			$default_args = array();
171
172
			if ( isset( $args['control'] ) && class_exists( $args['control'] ) ) {
173
				$control_class = $args['control'];
174
				unset( $args['control'] );
175
176
				$control = new $control_class( $wp_customize, $slug, array_merge( $default_args, $args ) );
177
				$wp_customize->add_control( $control );
178
			} else {
179
				if ( isset( $args['control'] ) ) {
180
					unset( $args['control'] );
181
				}
182
183
				$wp_customize->add_control(
184
					$slug,
185
					array_merge( $default_args, $args )
186
				);
187
			}
188
		}
189
190
		/**
0 ignored issues
show
Coding Style Documentation introduced by
Doc comment for parameter "$slug" missing
Loading history...
Coding Style Documentation introduced by
Doc comment for parameter "$args" missing
Loading history...
Coding Style Documentation introduced by
Doc comment for parameter "$wp_customize" missing
Loading history...
191
		 * Create a selective refresh.
192
		 */
193
		private function add_selective_refresh( $slug, $args, $wp_customize ) {
194
			$default_args = array(
195
				'selector'        => null,
196
				'render_callback' => null,
197
			);
198
199
			$wp_customize->selective_refresh->add_partial(
200
				$slug,
201
				array_merge( $default_args, $args )
202
			);
203
		}
204
205
		/**
0 ignored issues
show
Coding Style Documentation introduced by
Doc comment for parameter "$id" missing
Loading history...
206
		 * Returns a registered field.
207
		 */
208
		public function get_control( $id ) {
209
			$field = $this->controls['fields'][ $id ];
210
			return $field;
211
		}
212
213
		/**
0 ignored issues
show
Coding Style Documentation introduced by
Doc comment for parameter "$id" missing
Loading history...
214
		 * Returns a registered setting.
215
		 */
216
		public function get_setting( $id ) {
217
			$setting = $this->controls['settings'][ $id ];
218
			return $setting;
219
		}
220
221
	}
222
223
endif;
224