Completed
Pull Request — master (#1930)
by Aristeides
08:20 queued 04:16
created

Kirki_Modules_Webfonts::loop_fields()   C

Complexity

Conditions 12
Paths 6

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 2
Metric Value
cc 12
nc 6
nop 1
dl 0
loc 25
rs 6.9666
c 4
b 0
f 2

How to fix   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 webfonts.
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
// Exit if accessed directly.
14
if ( ! defined( 'ABSPATH' ) ) {
15
	exit;
16
}
17
18
/**
19
 * Adds script for tooltips.
20
 */
21
class Kirki_Modules_Webfonts {
22
23
	/**
24
	 * The object instance.
25
	 *
26
	 * @static
27
	 * @access private
28
	 * @since 3.0.0
29
	 * @var object
30
	 */
31
	private static $instance;
32
33
	/**
34
	 * The Kirki_Fonts_Google object.
35
	 *
36
	 * @access protected
37
	 * @since 3.0.0
38
	 * @var object
39
	 */
40
	protected $fonts_google;
41
42
	/**
43
	 * The class constructor
44
	 *
45
	 * @access protected
46
	 * @since 3.0.0
47
	 */
48
	protected function __construct() {
49
50
		include_once wp_normalize_path( dirname( __FILE__ ) . '/class-kirki-fonts.php' );
51
		include_once wp_normalize_path( dirname( __FILE__ ) . '/class-kirki-fonts-google.php' );
52
		include_once wp_normalize_path( dirname( __FILE__ ) . '/class-kirki-fonts-google-local.php' );
53
54
		add_action( 'wp_loaded', array( $this, 'run' ) );
55
56
	}
57
58
	/**
59
	 * Run on after_setup_theme.
60
	 *
61
	 * @access public
62
	 * @since 3.0.0
63
	 */
64
	public function run() {
65
		$this->fonts_google = Kirki_Fonts_Google::get_instance();
66
		$this->init();
67
	}
68
69
	/**
70
	 * Gets an instance of this object.
71
	 * Prevents duplicate instances which avoid artefacts and improves performance.
72
	 *
73
	 * @static
74
	 * @access public
75
	 * @since 3.0.0
76
	 * @return object
77
	 */
78
	public static function get_instance() {
79
		if ( ! self::$instance ) {
80
			self::$instance = new self();
81
		}
82
		return self::$instance;
83
	}
84
85
	/**
86
	 * Init other objects depending on the method we'll be using.
87
	 *
88
	 * @access protected
89
	 * @since 3.0.0
90
	 */
91
	protected function init() {
92
93
		foreach ( array_keys( Kirki::$config ) as $config_id ) {
94
			$method    = $this->get_method( $config_id );
0 ignored issues
show
Unused Code introduced by
The call to Kirki_Modules_Webfonts::get_method() has too many arguments starting with $config_id. ( Ignorable by Annotation )

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

94
			/** @scrutinizer ignore-call */ 
95
   $method    = $this->get_method( $config_id );

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
95
			$classname = 'Kirki_Modules_Webfonts_' . ucfirst( $method );
96
			new $classname( $config_id, $this, $this->fonts_google );
97
		}
98
		new Kirki_Modules_Webfonts_Local( $this, $this->fonts_google );
99
100
	}
101
102
	/**
103
	 * Get the method we're going to use.
104
	 *
105
	 * @access public
106
	 * @since 3.0.0
107
	 * @return string
108
	 */
109
	public function get_method() {
110
111
		// Figure out which method to use.
112
		$method = apply_filters( 'kirki_googlefonts_load_method', 'async' );
113
114
		// Fallback to 'async' if value is invalid.
115
		if ( 'async' !== $method && 'embed' !== $method && 'link' !== $method ) {
116
			$method = 'async';
117
		}
118
119
		$classname = 'Kirki_Modules_Webfonts_' . ucfirst( $method );
120
		if ( ! class_exists( $classname ) ) {
121
			$method = 'async';
122
		}
123
124
		// Force using the JS method while in the customizer.
125
		// This will help us work-out the live-previews for typography fields.
126
		// If we're not in the customizer use the defined method.
127
		return ( is_customize_preview() ) ? 'async' : $method;
128
	}
129
130
	/**
131
	 * Goes through all our fields and then populates the $this->fonts property.
132
	 *
133
	 * @access public
134
	 * @param string $config_id The config-ID.
135
	 */
136
	public function loop_fields( $config_id ) {
137
		foreach ( Kirki::$fields as $field ) {
138
			if ( isset( $field['kirki_config'] ) && $config_id !== $field['kirki_config'] ) {
139
				continue;
140
			}
141
			if ( true === apply_filters( "kirki_{$config_id}_webfonts_skip_hidden", true ) ) {
142
				// Only continue if field dependencies are met.
143
				if ( ! empty( $field['required'] ) ) {
144
					$valid = true;
145
146
					foreach ( $field['required'] as $requirement ) {
147
						if ( isset( $requirement['setting'] ) && isset( $requirement['value'] ) && isset( $requirement['operator'] ) ) {
148
							$controller_value = Kirki_Values::get_value( $config_id, $requirement['setting'] );
149
							if ( ! Kirki_Helper::compare_values( $controller_value, $requirement['value'], $requirement['operator'] ) ) {
150
								$valid = false;
151
							}
152
						}
153
					}
154
155
					if ( ! $valid ) {
156
						continue;
157
					}
158
				}
159
			}
160
			$this->fonts_google->generate_google_font( $field );
161
		}
162
	}
163
}
164