Completed
Push — develop ( 38831a...4fffa2 )
by Aristeides
08:57 queued 03:28
created

Kirki_Modules_Webfonts::get_method()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 4
nop 1
dl 0
loc 14
rs 9.2
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
	 * Parses fields and if any tooltips are found, they are added to the
91
	 * object's $tooltips_content property.
92
	 *
93
	 * @access private
94
	 * @since 3.0.0
95
	 */
96
	private function parse_fields() {
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
97
98
		$fields = Kirki::$fields;
99
		foreach ( $fields as $field ) {
0 ignored issues
show
Unused Code introduced by
This foreach statement is empty and can be removed.

This check looks for foreach loops that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

Consider removing the loop.

Loading history...
100
		}
101
	}
102
103
	/**
104
	 * Get the method we're going to use.
105
	 *
106
	 * @access public
107
	 * @since 3.0.0
108
	 * @return string
109
	 */
110
	public function get_method( $config_id ) {
111
112
		// Figure out which method to use.
113
		$method = apply_filters( "kirki/{$config_id}/googlefonts_load_method", 'link' );
114
		if ( 'embed' === $method && true !== $this->fallback_to_link ) {
115
			$method = 'embed';
116
		}
117
		// Force using the JS method while in the customizer.
118
		// This will help us work-out the live-previews for typography fields.
119
		if ( is_customize_preview() ) {
120
			$method = 'async';
121
		}
122
		return $method;
123
	}
124
125
	/**
126
	 * Should we fallback to link method?
127
	 *
128
	 * @access protected
129
	 * @since 3.0.0
130
	 */
131
	protected function maybe_fallback_to_link() {
132
133
		// Get the $fallback_to_link value from transient.
134
		$fallback_to_link = get_transient( 'kirki_googlefonts_fallback_to_link' );
135
		if ( 'yes' === $fallback_to_link ) {
136
			$this->fallback_to_link = true;
137
		}
138
139
		// Use links when in the customizer.
140
		global $wp_customize;
141
		if ( $wp_customize ) {
142
			$this->fallback_to_link = true;
143
		}
144
	}
145
146
	/**
147
	 * Goes through all our fields and then populates the $this->fonts property.
148
	 *
149
	 * @access public
150
	 */
151
	public function loop_fields() {
152
		foreach ( Kirki::$fields as $field ) {
153
			$this->fonts_google->generate_google_font( $field );
154
		}
155
	}
156
}
157