Completed
Push — develop ( 9e3e4a...435cec )
by Aristeides
02:53
created

Kirki_Modules_Webfonts::get_method()   B

Complexity

Conditions 7
Paths 8

Size

Total Lines 20
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 7
nc 8
nop 1
dl 0
loc 20
rs 8.2222
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
	 * Which method to use when loading googlefonts.
25
	 * Available options: link, js, embed.
26
	 *
27
	 * @static
28
	 * @access private
29
	 * @since 3.0.0
30
	 * @var string
31
	 */
32
	private static $method = array(
33
		'global' => 'embed',
34
	);
35
36
	/**
37
	 * Whether we should fallback to the link method or not.
38
	 *
39
	 * @access private
40
	 * @since 3.0.0
41
	 * @var bool
42
	 */
43
	private $fallback_to_link = false;
44
45
	/**
46
	 * The Kirki_Fonts_Google object.
47
	 *
48
	 * @access protected
49
	 * @since 3.0.0
50
	 * @var object
51
	 */
52
	protected $fonts_google;
53
54
55
	/**
56
	 * The class constructor
57
	 *
58
	 * @access public
59
	 * @since 3.0.0
60
	 */
61
	public function __construct() {
62
63
		include_once wp_normalize_path( dirname( __FILE__ ) . '/class-kirki-fonts.php' );
64
		include_once wp_normalize_path( dirname( __FILE__ ) . '/class-kirki-fonts-google.php' );
65
66
		$this->fonts_google = Kirki_Fonts_Google::get_instance();
67
		$this->maybe_fallback_to_link();
68
		$this->init();
69
70
	}
71
72
	/**
73
	 * Init other objects depending on the method we'll be using.
74
	 *
75
	 * @access protected
76
	 * @since 3.0.0
77
	 */
78
	protected function init() {
79
80
		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...
81
82
			$method = $this->get_method( $config_id );
83
			$classname = 'Kirki_Modules_Webfonts_' . ucfirst( $method );
84
			new $classname( $config_id, $this, $this->fonts_google );
85
86
		}
87
	}
88
89
	/**
90
	 * Get the method we're going to use.
91
	 *
92
	 * @access public
93
	 * @since 3.0.0
94
	 * @param string $config_id The config-ID.
95
	 * @return string
96
	 */
97
	public function get_method( $config_id ) {
98
99
		// Figure out which method to use.
100
		$method = apply_filters( "kirki/{$config_id}/googlefonts_load_method", 'embed' );
101
102
		// Fallback to 'embed' if value is invalid.
103
		if ( 'async' !== $method || 'embed' !== $method || 'link' !== $method ) {
104
			$method = 'embed';
105
		}
106
107
		// Fallback to 'link' if embed was not possible.
108
		if ( 'embed' === $method && $this->fallback_to_link ) {
109
			$method = 'link';
110
		}
111
112
		// Force using the JS method while in the customizer.
113
		// This will help us work-out the live-previews for typography fields.
114
		// If we're not in the customizer use the defined method.
115
		return ( is_customize_preview() ) ? 'async' : $method;
116
	}
117
118
	/**
119
	 * Should we fallback to link method?
120
	 *
121
	 * @access protected
122
	 * @since 3.0.0
123
	 */
124
	protected function maybe_fallback_to_link() {
125
126
		// Get the $fallback_to_link value from transient.
127
		$fallback_to_link = get_transient( 'kirki_googlefonts_fallback_to_link' );
128
		if ( 'yes' === $fallback_to_link ) {
129
			$this->fallback_to_link = true;
130
		}
131
132
		// Use links when in the customizer.
133
		global $wp_customize;
134
		if ( $wp_customize ) {
135
			$this->fallback_to_link = true;
136
		}
137
	}
138
139
	/**
140
	 * Goes through all our fields and then populates the $this->fonts property.
141
	 *
142
	 * @access public
143
	 */
144
	public function loop_fields() {
145
		foreach ( Kirki::$fields as $field ) {
146
			$this->fonts_google->generate_google_font( $field );
147
		}
148
	}
149
}
150