Completed
Push — develop ( 2d809e...4b580b )
by Aristeides
03:12
created

Kirki_Modules_Webfonts::get_instance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 6
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
		$this->fonts_google = Kirki_Fonts_Google::get_instance();
77
		$this->maybe_fallback_to_link();
78
		$this->init();
79
80
	}
81
82
	/**
83
	 * Gets an instance of this object.
84
	 * Prevents duplicate instances which avoid artefacts and improves performance.
85
	 *
86
	 * @static
87
	 * @access public
88
	 * @since 3.0.0
89
	 * @return object
90
	 */
91
	public static function get_instance() {
92
		if ( ! self::$instance ) {
93
			self::$instance = new self();
94
		}
95
		return self::$instance;
96
	}
97
98
	/**
99
	 * Init other objects depending on the method we'll be using.
100
	 *
101
	 * @access protected
102
	 * @since 3.0.0
103
	 */
104
	protected function init() {
105
106
		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...
107
108
			$method = $this->get_method( $config_id );
109
			$classname = 'Kirki_Modules_Webfonts_' . ucfirst( $method );
110
			new $classname( $config_id, $this, $this->fonts_google );
111
112
		}
113
	}
114
115
	/**
116
	 * Get the method we're going to use.
117
	 *
118
	 * @access public
119
	 * @since 3.0.0
120
	 * @param string $config_id The config-ID.
121
	 * @return string
122
	 */
123
	public function get_method( $config_id ) {
124
125
		// Figure out which method to use.
126
		$method = apply_filters( "kirki/{$config_id}/googlefonts_load_method", 'embed' );
127
128
		// Fallback to 'embed' if value is invalid.
129
		if ( 'async' !== $method || 'embed' !== $method || 'link' !== $method ) {
130
			$method = 'embed';
131
		}
132
133
		// Fallback to 'link' if embed was not possible.
134
		if ( 'embed' === $method && $this->fallback_to_link ) {
135
			$method = 'link';
136
		}
137
138
		// Force using the JS method while in the customizer.
139
		// This will help us work-out the live-previews for typography fields.
140
		// If we're not in the customizer use the defined method.
141
		return ( is_customize_preview() ) ? 'async' : $method;
142
	}
143
144
	/**
145
	 * Should we fallback to link method?
146
	 *
147
	 * @access protected
148
	 * @since 3.0.0
149
	 */
150
	protected function maybe_fallback_to_link() {
151
152
		// Get the $fallback_to_link value from transient.
153
		$fallback_to_link = get_transient( 'kirki_googlefonts_fallback_to_link' );
154
		if ( 'yes' === $fallback_to_link ) {
155
			$this->fallback_to_link = true;
156
		}
157
158
		// Use links when in the customizer.
159
		global $wp_customize;
160
		if ( $wp_customize ) {
161
			$this->fallback_to_link = true;
162
		}
163
	}
164
165
	/**
166
	 * Goes through all our fields and then populates the $this->fonts property.
167
	 *
168
	 * @access public
169
	 */
170
	public function loop_fields() {
171
		foreach ( Kirki::$fields as $field ) {
172
			$this->fonts_google->generate_google_font( $field );
173
		}
174
	}
175
}
176