Issues (377)

modules/webfonts/class-kirki-modules-webfonts.php (1 issue)

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    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
	 * The Kirki_Fonts_Google object.
35
	 *
36
	 * @access protected
37
	 * @since 3.0.0
38
	 * @var object
39
	 */
40
	protected $fonts_google;
41
42
	/**
43
	 * The class constructor
44
	 *
45
	 * @access protected
46
	 * @since 3.0.0
47
	 */
48
	protected function __construct() {
49
50
		include_once wp_normalize_path( dirname( __FILE__ ) . '/class-kirki-fonts.php' );
51
		include_once wp_normalize_path( dirname( __FILE__ ) . '/class-kirki-fonts-google.php' );
52
		include_once wp_normalize_path( dirname( __FILE__ ) . '/class-kirki-fonts-google-local.php' );
53
54
		add_action( 'wp_loaded', array( $this, 'run' ) );
55
56
	}
57
58
	/**
59
	 * Run on after_setup_theme.
60
	 *
61
	 * @access public
62
	 * @since 3.0.0
63
	 */
64
	public function run() {
65
		$this->fonts_google = Kirki_Fonts_Google::get_instance();
66
		$this->init();
67
	}
68
69
	/**
70
	 * Gets an instance of this object.
71
	 * Prevents duplicate instances which avoid artefacts and improves performance.
72
	 *
73
	 * @static
74
	 * @access public
75
	 * @since 3.0.0
76
	 * @return object
77
	 */
78
	public static function get_instance() {
79
		if ( ! self::$instance ) {
80
			self::$instance = new self();
81
		}
82
		return self::$instance;
83
	}
84
85
	/**
86
	 * Init other objects depending on the method we'll be using.
87
	 *
88
	 * @access protected
89
	 * @since 3.0.0
90
	 */
91
	protected function init() {
92
		foreach ( array_keys( Kirki::$config ) as $config_id ) {
93
			$method    = $this->get_method( $config_id );
0 ignored issues
show
The call to Kirki_Modules_Webfonts::get_method() has too many arguments starting with $config_id. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

93
			/** @scrutinizer ignore-call */ 
94
   $method    = $this->get_method( $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. Please note the @ignore annotation hint above.

Loading history...
94
			$classname = 'Kirki_Modules_Webfonts_' . ucfirst( $method );
95
			new $classname( $config_id, $this, $this->fonts_google );
96
		}
97
		new Kirki_Modules_Webfonts_Local( $this, $this->fonts_google );
98
	}
99
100
	/**
101
	 * Get the method we're going to use.
102
	 *
103
	 * @access public
104
	 * @since 3.0.0
105
	 * @return string
106
	 */
107
	public function get_method() {
108
109
		// Figure out which method to use.
110
		$method = apply_filters( 'kirki_googlefonts_load_method', 'async' );
111
112
		// Fallback to 'async' if value is invalid.
113
		if ( 'async' !== $method && 'embed' !== $method && 'link' !== $method ) {
114
			$method = 'async';
115
		}
116
117
		$classname = 'Kirki_Modules_Webfonts_' . ucfirst( $method );
118
		if ( ! class_exists( $classname ) ) {
119
			$method = 'async';
120
		}
121
122
		// Force using the JS method while in the customizer.
123
		// This will help us work-out the live-previews for typography fields.
124
		// If we're not in the customizer use the defined method.
125
		return ( is_customize_preview() ) ? 'async' : $method;
126
	}
127
128
	/**
129
	 * Goes through all our fields and then populates the $this->fonts property.
130
	 *
131
	 * @access public
132
	 * @param string $config_id The config-ID.
133
	 */
134
	public function loop_fields( $config_id ) {
135
		foreach ( Kirki::$fields as $field ) {
136
			if ( isset( $field['kirki_config'] ) && $config_id !== $field['kirki_config'] ) {
137
				continue;
138
			}
139
			if ( true === apply_filters( "kirki_{$config_id}_webfonts_skip_hidden", true ) ) {
140
				// Only continue if field dependencies are met.
141
				if ( ! empty( $field['required'] ) ) {
142
					$valid = true;
143
144
					foreach ( $field['required'] as $requirement ) {
145
						if ( isset( $requirement['setting'] ) && isset( $requirement['value'] ) && isset( $requirement['operator'] ) ) {
146
							$controller_value = Kirki_Values::get_value( $config_id, $requirement['setting'] );
147
							if ( ! Kirki_Helper::compare_values( $controller_value, $requirement['value'], $requirement['operator'] ) ) {
148
								$valid = false;
149
							}
150
						}
151
					}
152
153
					if ( ! $valid ) {
154
						continue;
155
					}
156
				}
157
			}
158
			$this->fonts_google->generate_google_font( $field );
159
		}
160
	}
161
}
162