Completed
Pull Request — master (#1728)
by Aristeides
03:23
created

Kirki_Modules_CSS::loop_controls()   C

Complexity

Conditions 17
Paths 25

Size

Total Lines 56
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 17
eloc 25
nc 25
nop 1
dl 0
loc 56
rs 6.5889
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
 * Handles the CSS Output of fields.
4
 *
5
 * @package     Kirki
6
 * @category    Modules
7
 * @author      Aristeides Stathopoulos
8
 * @copyright   Copyright (c) 2017, Aristeides Stathopoulos
9
 * @license     http://opensource.org/licenses/https://opensource.org/licenses/MIT
10
 * @since       3.0.0
11
 */
12
13
/**
14
 * The Kirki_Modules_CSS object.
15
 */
16
class Kirki_Modules_CSS {
17
18
	/**
19
	 * The object instance.
20
	 *
21
	 * @static
22
	 * @access private
23
	 * @since 3.0.0
24
	 * @var object
25
	 */
26
	private static $instance;
27
28
	/**
29
	 * Whether we've already processed this or not.
30
	 *
31
	 * @access public
32
	 * @var bool
33
	 */
34
	public $processed = false;
35
36
	/**
37
	 * The CSS array
38
	 *
39
	 * @access public
40
	 * @var array
41
	 */
42
	public static $css_array = array();
43
44
	/**
45
	 * Set to true if you want to use the AJAX method.
46
	 *
47
	 * @access public
48
	 * @var bool
49
	 */
50
	public static $ajax = false;
51
52
	/**
53
	 * The Kirki_CSS_To_File object.
54
	 *
55
	 * @access protected
56
	 * @since 3.0.0
57
	 * @var object
58
	 */
59
	protected $css_to_file;
60
61
	/**
62
	 * Constructor
63
	 *
64
	 * @access protected
65
	 */
66
	protected function __construct() {
67
68
		$class_files = array(
69
			'Kirki_CSS_To_File'                         => '/class-kirki-css-to-file.php',
70
			'Kirki_Modules_CSS_Generator'               => '/class-kirki-modules-css-generator.php',
71
			'Kirki_Output'                              => '/class-kirki-output.php',
72
			'Kirki_Output_Field_Background'             => '/field/class-kirki-output-field-background.php',
73
			'Kirki_Output_Field_Image'                  => '/field/class-kirki-output-field-image.php',
74
			'Kirki_Output_Field_Multicolor'             => '/field/class-kirki-output-field-multicolor.php',
75
			'Kirki_Output_Field_Dimensions'             => '/field/class-kirki-output-field-dimensions.php',
76
			'Kirki_Output_Field_Typography'             => '/field/class-kirki-output-field-typography.php',
77
			'Kirki_Output_Property'                     => '/property/class-kirki-output-property.php',
78
			'Kirki_Output_Property_Background_Image'    => '/property/class-kirki-output-property-background-image.php',
79
			'Kirki_Output_Property_Background_Position' => '/property/class-kirki-output-property-background-position.php',
80
			'Kirki_Output_Property_Font_Family'         => '/property/class-kirki-output-property-font-family.php',
81
		);
82
83
		foreach ( $class_files as $class_name => $file ) {
84
			if ( ! class_exists( $class_name ) ) {
85
				include_once wp_normalize_path( dirname( __FILE__ ) . $file );
1 ignored issue
show
Bug introduced by
The function wp_normalize_path was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

85
				include_once /** @scrutinizer ignore-call */ wp_normalize_path( dirname( __FILE__ ) . $file );
Loading history...
86
			}
87
		}
88
89
		add_action( 'init', array( $this, 'init' ) );
1 ignored issue
show
Bug introduced by
The function add_action was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

89
		/** @scrutinizer ignore-call */ 
90
  add_action( 'init', array( $this, 'init' ) );
Loading history...
90
91
	}
92
93
	/**
94
	 * Gets an instance of this object.
95
	 * Prevents duplicate instances which avoid artefacts and improves performance.
96
	 *
97
	 * @static
98
	 * @access public
99
	 * @since 3.0.0
100
	 * @return object
101
	 */
102
	public static function get_instance() {
103
		if ( ! self::$instance ) {
104
			self::$instance = new self();
105
		}
106
		return self::$instance;
107
	}
108
109
	/**
110
	 * Init.
111
	 *
112
	 * @access public
113
	 */
114
	public function init() {
115
116
		Kirki_Modules_Webfonts::get_instance();
117
118
		global $wp_customize;
119
120
		$config   = apply_filters( 'kirki_config', array() );
1 ignored issue
show
Bug introduced by
The function apply_filters was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

120
		$config   = /** @scrutinizer ignore-call */ apply_filters( 'kirki_config', array() );
Loading history...
121
		$priority = 999;
122
		if ( isset( $config['styles_priority'] ) ) {
123
			$priority = absint( $config['styles_priority'] );
1 ignored issue
show
Bug introduced by
The function absint was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

123
			$priority = /** @scrutinizer ignore-call */ absint( $config['styles_priority'] );
Loading history...
124
		}
125
126
		// Allow completely disabling Kirki CSS output.
127
		if ( ( defined( 'KIRKI_NO_OUTPUT' ) && true === KIRKI_NO_OUTPUT ) || ( isset( $config['disable_output'] ) && true === $config['disable_output'] ) ) {
1 ignored issue
show
Bug introduced by
The constant KIRKI_NO_OUTPUT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
128
			return;
129
		}
130
131
		$method = apply_filters( 'kirki_dynamic_css_method', 'inline' );
132
		if ( $wp_customize ) {
133
			// If we're in the customizer, load inline no matter what.
134
			add_action( 'wp_enqueue_scripts', array( $this, 'inline_dynamic_css' ), $priority );
1 ignored issue
show
Bug introduced by
The function add_action was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

134
			/** @scrutinizer ignore-call */ 
135
   add_action( 'wp_enqueue_scripts', array( $this, 'inline_dynamic_css' ), $priority );
Loading history...
135
136
			// If we're using file method, on save write the new styles.
137
			if ( 'file' === $method ) {
138
				$this->css_to_file = new Kirki_CSS_To_File();
139
				add_action( 'customize_save_after', array( $this->css_to_file, 'write_file' ) );
140
			}
141
			return;
142
		}
143
144
		if ( 'file' === $method ) {
145
			// Attempt to write the CSS to file.
146
			$this->css_to_file = new Kirki_CSS_To_File();
147
			// If we succesd, load this file.
148
			$failed = get_transient( 'kirki_css_write_to_file_failed' );
1 ignored issue
show
Bug introduced by
The function get_transient was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

148
			$failed = /** @scrutinizer ignore-call */ get_transient( 'kirki_css_write_to_file_failed' );
Loading history...
149
			// If writing CSS to file hasn't failed, just enqueue this file.
150
			if ( ! $failed ) {
151
				add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_compiled_file' ), $priority );
152
				return;
153
			}
154
		}
155
156
		// If we are in the customizer, load CSS using inline-styles.
157
		// If we are in the frontend AND self::$ajax is true, then load dynamic CSS using AJAX.
158
		if ( ( true === self::$ajax ) || ( isset( $config['inline_css'] ) && false === $config['inline_css'] ) ) {
159
			add_action( 'wp_enqueue_scripts', array( $this, 'frontend_styles' ), $priority );
160
			add_action( 'wp_ajax_kirki_dynamic_css', array( $this, 'ajax_dynamic_css' ) );
161
			add_action( 'wp_ajax_nopriv_kirki_dynamic_css', array( $this, 'ajax_dynamic_css' ) );
162
			return;
163
		}
164
165
		// If we got this far then add styles inline.
166
		add_action( 'wp_enqueue_scripts', array( $this, 'inline_dynamic_css' ), $priority );
167
	}
168
169
	/**
170
	 * Enqueues compiled CSS file.
171
	 *
172
	 * @access public
173
	 * @since 3.0.0
174
	 */
175
	public function enqueue_compiled_file() {
176
177
		wp_enqueue_style( 'kirki-styles', $this->css_to_file->get_url(), array(), $this->css_to_file->get_timestamp() );
1 ignored issue
show
Bug introduced by
The function wp_enqueue_style was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

177
		/** @scrutinizer ignore-call */ 
178
  wp_enqueue_style( 'kirki-styles', $this->css_to_file->get_url(), array(), $this->css_to_file->get_timestamp() );
Loading history...
178
179
	}
180
	/**
181
	 * Adds inline styles.
182
	 *
183
	 * @access public
184
	 */
185
	public function inline_dynamic_css() {
186
		$configs = Kirki::$config;
187
		if ( ! $this->processed ) {
188
			foreach ( $configs as $config_id => $args ) {
189
				if ( isset( $args['disable_output'] ) && true === $args['disable_output'] ) {
190
					continue;
191
				}
192
				$styles = self::loop_controls( $config_id );
193
				$styles = apply_filters( "kirki_{$config_id}_dynamic_css", $styles );
1 ignored issue
show
Bug introduced by
The function apply_filters was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

193
				$styles = /** @scrutinizer ignore-call */ apply_filters( "kirki_{$config_id}_dynamic_css", $styles );
Loading history...
194
				if ( ! empty( $styles ) ) {
195
					$stylesheet = apply_filters( "kirki_{$config_id}_stylesheet", false );
196
					if ( $stylesheet ) {
197
						wp_add_inline_style( $stylesheet, $styles );
1 ignored issue
show
Bug introduced by
The function wp_add_inline_style was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

197
						/** @scrutinizer ignore-call */ 
198
      wp_add_inline_style( $stylesheet, $styles );
Loading history...
198
						continue;
199
					}
200
					wp_enqueue_style( 'kirki-styles-' . $config_id, trailingslashit( Kirki::$url ) . 'assets/css/kirki-styles.css', array(), KIRKI_VERSION );
2 ignored issues
show
Bug introduced by
The function trailingslashit was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

200
					wp_enqueue_style( 'kirki-styles-' . $config_id, /** @scrutinizer ignore-call */ trailingslashit( Kirki::$url ) . 'assets/css/kirki-styles.css', array(), KIRKI_VERSION );
Loading history...
Bug introduced by
The function wp_enqueue_style was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

200
					/** @scrutinizer ignore-call */ 
201
     wp_enqueue_style( 'kirki-styles-' . $config_id, trailingslashit( Kirki::$url ) . 'assets/css/kirki-styles.css', array(), KIRKI_VERSION );
Loading history...
201
					wp_add_inline_style( 'kirki-styles-' . $config_id, $styles );
202
				}
203
			}
204
			$this->processed = true;
205
		}
206
		if ( apply_filters( 'kirki_load_fontawesome', true ) ) {
207
			wp_enqueue_script( 'kirki-fontawesome-font', 'https://use.fontawesome.com/30858dc40a.js', array(), '4.0.7', true );
1 ignored issue
show
Bug introduced by
The function wp_enqueue_script was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

207
			/** @scrutinizer ignore-call */ 
208
   wp_enqueue_script( 'kirki-fontawesome-font', 'https://use.fontawesome.com/30858dc40a.js', array(), '4.0.7', true );
Loading history...
208
		}
209
	}
210
211
	/**
212
	 * Get the dynamic-css.php file
213
	 *
214
	 * @access public
215
	 */
216
	public function ajax_dynamic_css() {
217
		require wp_normalize_path( Kirki::$path . '/modules/css/dynamic-css.php' );
1 ignored issue
show
Bug introduced by
The function wp_normalize_path was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

217
		require /** @scrutinizer ignore-call */ wp_normalize_path( Kirki::$path . '/modules/css/dynamic-css.php' );
Loading history...
218
		exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
219
	}
220
221
	/**
222
	 * Enqueues the ajax stylesheet.
223
	 *
224
	 * @access public
225
	 */
226
	public function frontend_styles() {
227
		wp_enqueue_style( 'kirki-styles-php', admin_url( 'admin-ajax.php' ) . '?action=kirki_dynamic_css', array(), KIRKI_VERSION );
2 ignored issues
show
Bug introduced by
The function admin_url was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

227
		wp_enqueue_style( 'kirki-styles-php', /** @scrutinizer ignore-call */ admin_url( 'admin-ajax.php' ) . '?action=kirki_dynamic_css', array(), KIRKI_VERSION );
Loading history...
Bug introduced by
The function wp_enqueue_style was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

227
		/** @scrutinizer ignore-call */ 
228
  wp_enqueue_style( 'kirki-styles-php', admin_url( 'admin-ajax.php' ) . '?action=kirki_dynamic_css', array(), KIRKI_VERSION );
Loading history...
228
	}
229
230
	/**
231
	 * Loop through all fields and create an array of style definitions.
232
	 *
233
	 * @static
234
	 * @access public
235
	 * @param string $config_id The configuration ID.
236
	 */
237
	public static function loop_controls( $config_id ) {
238
239
		// Get an instance of the Kirki_Modules_CSS_Generator class.
240
		// This will make sure google fonts and backup fonts are loaded.
241
		Kirki_Modules_CSS_Generator::get_instance();
242
243
		$fields = Kirki::$fields;
244
		$css    = array();
245
246
		// Early exit if no fields are found.
247
		if ( empty( $fields ) ) {
248
			return;
249
		}
250
251
		foreach ( $fields as $field ) {
252
253
			// Only process fields that belong to $config_id.
254
			if ( $config_id !== $field['kirki_config'] ) {
255
				continue;
256
			}
257
258
			if ( true === apply_filters( "kirki_{$config_id}_css_skip_hidden", true ) ) {
1 ignored issue
show
Bug introduced by
The function apply_filters was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

258
			if ( true === /** @scrutinizer ignore-call */ apply_filters( "kirki_{$config_id}_css_skip_hidden", true ) ) {
Loading history...
259
				// Only continue if field dependencies are met.
260
				if ( ! empty( $field['required'] ) ) {
261
					$valid = true;
262
263
					foreach ( $field['required'] as $requirement ) {
264
						if ( isset( $requirement['setting'] ) && isset( $requirement['value'] ) && isset( $requirement['operator'] ) ) {
265
							$controller_value = Kirki_Values::get_value( $config_id, $requirement['setting'] );
266
							if ( ! Kirki_Helper::compare_values( $controller_value, $requirement['value'], $requirement['operator'] ) ) {
267
								$valid = false;
268
							}
269
						}
270
					}
271
272
					if ( ! $valid ) {
273
						continue;
274
					}
275
				}
276
			}
277
278
			// Only continue if $field['output'] is set.
279
			if ( isset( $field['output'] ) && ! empty( $field['output'] ) ) {
280
				$css = Kirki_Helper::array_replace_recursive( $css, Kirki_Modules_CSS_Generator::css( $field ) );
281
282
				// Add the globals.
283
				if ( isset( self::$css_array[ $config_id ] ) && ! empty( self::$css_array[ $config_id ] ) ) {
284
					Kirki_Helper::array_replace_recursive( $css, self::$css_array[ $config_id ] );
285
				}
286
			}
287
		}
288
289
		$css = apply_filters( "kirki_{$config_id}_styles", $css );
290
291
		if ( is_array( $css ) ) {
292
			return Kirki_Modules_CSS_Generator::styles_parse( Kirki_Modules_CSS_Generator::add_prefixes( $css ) );
293
		}
294
	}
295
}
296