Completed
Push — develop ( 17becd...cd1fca )
by Aristeides
03:20
created

Kirki_Modules_Webfonts::loop_fields()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
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
	 * Which method to use when loading googlefonts.
35
	 * Available options: link, js, embed.
36
	 *
37
	 * @static
38
	 * @access private
39
	 * @since 3.0.0
40
	 * @var string
41
	 */
42
	private static $method = array(
43
		'global' => 'embed',
44
	);
45
46
	/**
47
	 * Whether we should fallback to the link method or not.
48
	 *
49
	 * @access private
50
	 * @since 3.0.0
51
	 * @var bool
52
	 */
53
	private $fallback_to_link = false;
54
55
	/**
56
	 * The Kirki_Fonts_Google object.
57
	 *
58
	 * @access protected
59
	 * @since 3.0.0
60
	 * @var object
61
	 */
62
	protected $fonts_google;
63
64
65
	/**
66
	 * The class constructor
67
	 *
68
	 * @access protected
69
	 * @since 3.0.0
70
	 */
71
	protected function __construct() {
72
73
		include_once wp_normalize_path( dirname( __FILE__ ) . '/class-kirki-fonts.php' );
74
		include_once wp_normalize_path( dirname( __FILE__ ) . '/class-kirki-fonts-google.php' );
75
76
		add_action( 'after_setup_theme', array( $this, 'run' ) );
77
78
	}
79
80
	/**
81
	 * Run on after_setup_theme.
82
	 *
83
	 * @access public
84
	 * @since 3.0.0
85
	 */
86
	public function run() {
87
		$this->fonts_google = Kirki_Fonts_Google::get_instance();
88
		$this->maybe_fallback_to_link();
89
		$this->init();
90
	}
91
92
	/**
93
	 * Gets an instance of this object.
94
	 * Prevents duplicate instances which avoid artefacts and improves performance.
95
	 *
96
	 * @static
97
	 * @access public
98
	 * @since 3.0.0
99
	 * @return object
100
	 */
101
	public static function get_instance() {
102
		if ( ! self::$instance ) {
103
			self::$instance = new self();
104
		}
105
		return self::$instance;
106
	}
107
108
	/**
109
	 * Init other objects depending on the method we'll be using.
110
	 *
111
	 * @access protected
112
	 * @since 3.0.0
113
	 */
114
	protected function init() {
115
116
		foreach ( self::$method as $config_id => $config_method ) {
0 ignored issues
show
Bug introduced by
The expression self::$method of type string is not traversable.
Loading history...
117
118
			$method = $this->get_method( $config_id );
119
			$classname = 'Kirki_Modules_Webfonts_' . ucfirst( $method );
120
			new $classname( $config_id, $this, $this->fonts_google );
121
122
		}
123
	}
124
125
	/**
126
	 * Get the method we're going to use.
127
	 *
128
	 * @access public
129
	 * @since 3.0.0
130
	 * @param string $config_id The config-ID.
131
	 * @return string
132
	 */
133
	public function get_method( $config_id ) {
134
135
		// Figure out which method to use.
136
		$method = apply_filters( "kirki/{$config_id}/googlefonts_load_method", 'embed' );
137
138
		// Fallback to 'embed' if value is invalid.
139
		if ( 'async' !== $method || 'embed' !== $method || 'link' !== $method ) {
140
			$method = 'embed';
141
		}
142
143
		// Fallback to 'link' if embed was not possible.
144
		if ( 'embed' === $method && $this->fallback_to_link ) {
145
			$method = 'link';
146
		}
147
148
		// Force using the JS method while in the customizer.
149
		// This will help us work-out the live-previews for typography fields.
150
		// If we're not in the customizer use the defined method.
151
		return ( is_customize_preview() ) ? 'async' : $method;
152
	}
153
154
	/**
155
	 * Should we fallback to link method?
156
	 *
157
	 * @access protected
158
	 * @since 3.0.0
159
	 */
160
	protected function maybe_fallback_to_link() {
161
162
		// Get the $fallback_to_link value from transient.
163
		$fallback_to_link = get_transient( 'kirki_googlefonts_fallback_to_link' );
164
		if ( 'yes' === $fallback_to_link ) {
165
			$this->fallback_to_link = true;
166
		}
167
168
		// Use links when in the customizer.
169
		global $wp_customize;
170
		if ( $wp_customize ) {
171
			$this->fallback_to_link = true;
172
		}
173
	}
174
175
	/**
176
	 * Goes through all our fields and then populates the $this->fonts property.
177
	 *
178
	 * @access public
179
	 */
180
	public function loop_fields() {
181
		foreach ( Kirki::$fields as $field ) {
182
			$this->fonts_google->generate_google_font( $field );
183
		}
184
	}
185
}
186