Completed
Pull Request — develop (#1682)
by Aristeides
02:08
created

Kirki_Modules_Webfonts   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 153
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 153
rs 10
c 0
b 0
f 0
wmc 20
lcom 2
cbo 2

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A run() 0 5 1
A get_instance() 0 6 2
A init() 0 8 2
B get_method() 0 20 7
A maybe_fallback_to_link() 0 14 3
A loop_fields() 0 8 4
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
	 * Whether we should fallback to the link method or not.
35
	 *
36
	 * @access private
37
	 * @since 3.0.0
38
	 * @var bool
39
	 */
40
	private $fallback_to_link = false;
41
42
	/**
43
	 * The Kirki_Fonts_Google object.
44
	 *
45
	 * @access protected
46
	 * @since 3.0.0
47
	 * @var object
48
	 */
49
	protected $fonts_google;
50
51
52
	/**
53
	 * The class constructor
54
	 *
55
	 * @access protected
56
	 * @since 3.0.0
57
	 */
58
	protected function __construct() {
59
60
		include_once wp_normalize_path( dirname( __FILE__ ) . '/class-kirki-fonts.php' );
61
		include_once wp_normalize_path( dirname( __FILE__ ) . '/class-kirki-fonts-google.php' );
62
63
		add_action( 'wp_loaded', array( $this, 'run' ) );
64
65
	}
66
67
	/**
68
	 * Run on after_setup_theme.
69
	 *
70
	 * @access public
71
	 * @since 3.0.0
72
	 */
73
	public function run() {
74
		$this->fonts_google = Kirki_Fonts_Google::get_instance();
75
		$this->maybe_fallback_to_link();
76
		$this->init();
77
	}
78
79
	/**
80
	 * Gets an instance of this object.
81
	 * Prevents duplicate instances which avoid artefacts and improves performance.
82
	 *
83
	 * @static
84
	 * @access public
85
	 * @since 3.0.0
86
	 * @return object
87
	 */
88
	public static function get_instance() {
89
		if ( ! self::$instance ) {
90
			self::$instance = new self();
91
		}
92
		return self::$instance;
93
	}
94
95
	/**
96
	 * Init other objects depending on the method we'll be using.
97
	 *
98
	 * @access protected
99
	 * @since 3.0.0
100
	 */
101
	protected function init() {
102
103
		foreach ( array_keys( Kirki::$config ) as $config_id ) {
104
			$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.

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.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
105
			$classname = 'Kirki_Modules_Webfonts_' . ucfirst( $method );
106
			new $classname( $config_id, $this, $this->fonts_google );
107
		}
108
	}
109
110
	/**
111
	 * Get the method we're going to use.
112
	 *
113
	 * @access public
114
	 * @since 3.0.0
115
	 * @return string
116
	 */
117
	public function get_method() {
118
119
		// Figure out which method to use.
120
		$method = apply_filters( 'kirki/googlefonts_load_method', 'async' );
121
122
		// Fallback to 'link' if value is invalid.
123
		if ( 'async' !== $method && 'embed' !== $method && 'link' !== $method ) {
124
			$method = 'async';
125
		}
126
127
		// Fallback to 'link' if embed was not possible.
128
		if ( 'embed' === $method && $this->fallback_to_link ) {
129
			$method = 'link';
130
		}
131
132
		// Force using the JS method while in the customizer.
133
		// This will help us work-out the live-previews for typography fields.
134
		// If we're not in the customizer use the defined method.
135
		return ( is_customize_preview() ) ? 'async' : $method;
136
	}
137
138
	/**
139
	 * Should we fallback to link method?
140
	 *
141
	 * @access protected
142
	 * @since 3.0.0
143
	 */
144
	protected function maybe_fallback_to_link() {
145
146
		// Get the $fallback_to_link value from transient.
147
		$fallback_to_link = get_transient( 'kirki_googlefonts_fallback_to_link' );
148
		if ( 'yes' === $fallback_to_link ) {
149
			$this->fallback_to_link = true;
150
		}
151
152
		// Use links when in the customizer.
153
		global $wp_customize;
154
		if ( $wp_customize ) {
155
			$this->fallback_to_link = true;
156
		}
157
	}
158
159
	/**
160
	 * Goes through all our fields and then populates the $this->fonts property.
161
	 *
162
	 * @access public
163
	 * @param string $config_id The config-ID.
164
	 */
165
	public function loop_fields( $config_id ) {
166
		foreach ( Kirki::$fields as $field ) {
167
			if ( isset( $field['kirki_config'] ) && $config_id !== $field['kirki_config'] ) {
168
				continue;
169
			}
170
			$this->fonts_google->generate_google_font( $field );
171
		}
172
	}
173
}
174