Issues (377)

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

1
<?php
2
/**
3
 * Adds the Webfont Loader to load fonts asyncronously.
4
 *
5
 * @package     Kirki
6
 * @category    Core
7
 * @author      Aristeides Stathopoulos
8
 * @copyright   Copyright (c) 2017, Aristeides Stathopoulos
9
 * @license    https://opensource.org/licenses/MIT
10
 * @since       3.0
11
 */
12
13
/**
14
 * Manages the way Google Fonts are enqueued.
15
 */
16
final class Kirki_Modules_Webfonts_Async {
17
18
	/**
19
	 * The config ID.
20
	 *
21
	 * @access protected
22
	 * @since 3.0.0
23
	 * @var string
24
	 */
25
	protected $config_id;
26
27
	/**
28
	 * The Kirki_Modules_Webfonts object.
29
	 *
30
	 * @access protected
31
	 * @since 3.0.0
32
	 * @var object
33
	 */
34
	protected $webfonts;
35
36
	/**
37
	 * The Kirki_Fonts_Google object.
38
	 *
39
	 * @access protected
40
	 * @since 3.0.0
41
	 * @var object
42
	 */
43
	protected $googlefonts;
44
45
	/**
46
	 * Fonts to load.
47
	 *
48
	 * @access protected
49
	 * @since 3.0.26
50
	 * @var array
51
	 */
52
	protected $fonts_to_load = array();
53
54
	/**
55
	 * Constructor.
56
	 *
57
	 * @access public
58
	 * @since 3.0
59
	 * @param string $config_id   The config-ID.
60
	 * @param object $webfonts    The Kirki_Modules_Webfonts object.
61
	 * @param object $googlefonts The Kirki_Fonts_Google object.
62
	 * @param array  $args        Extra args we want to pass.
63
	 */
64
	public function __construct( $config_id, $webfonts, $googlefonts, $args = array() ) {
0 ignored issues
show
The parameter $args is not used and could be removed. ( Ignorable by Annotation )

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

64
	public function __construct( $config_id, $webfonts, $googlefonts, /** @scrutinizer ignore-unused */ $args = array() ) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
65
		$this->config_id   = $config_id;
66
		$this->webfonts    = $webfonts;
67
		$this->googlefonts = $googlefonts;
68
69
		add_action( 'wp_head', array( $this, 'webfont_loader' ) );
70
		add_action( 'wp_head', array( $this, 'webfont_loader_script' ), 30 );
71
72
		// Add these in the dashboard to support editor-styles.
73
		add_action( 'admin_enqueue_scripts', array( $this, 'webfont_loader' ) );
74
		add_action( 'admin_enqueue_scripts', array( $this, 'webfont_loader_script' ), 30 );
75
76
		add_filter( 'wp_resource_hints', array( $this, 'resource_hints' ), 10, 2 );
77
	}
78
79
	/**
80
	 * Add preconnect for Google Fonts.
81
	 *
82
	 * @access public
83
	 * @param array  $urls           URLs to print for resource hints.
84
	 * @param string $relation_type  The relation type the URLs are printed.
85
	 * @return array $urls           URLs to print for resource hints.
86
	 */
87
	public function resource_hints( $urls, $relation_type ) {
88
		$fonts_to_load = $this->googlefonts->fonts;
89
90
		if ( ! empty( $fonts_to_load ) && 'preconnect' === $relation_type ) {
91
			$urls[] = array(
92
				'href' => 'https://fonts.gstatic.com',
93
				'crossorigin',
94
			);
95
		}
96
		return $urls;
97
	}
98
99
	/**
100
	 * Webfont Loader for Google Fonts.
101
	 *
102
	 * @access public
103
	 * @since 3.0.0
104
	 */
105
	public function webfont_loader() {
106
107
		// Go through our fields and populate $this->fonts.
108
		$this->webfonts->loop_fields( $this->config_id );
109
110
		$this->googlefonts->fonts = apply_filters( 'kirki_enqueue_google_fonts', $this->googlefonts->fonts );
111
112
		// Goes through $this->fonts and adds or removes things as needed.
113
		$this->googlefonts->process_fonts();
114
115
		foreach ( $this->googlefonts->fonts as $font => $weights ) {
116
			foreach ( $weights as $key => $value ) {
117
				if ( 'italic' === $value ) {
118
					$weights[ $key ] = '400i';
119
				} else {
120
					$weights[ $key ] = str_replace( array( 'regular', 'bold', 'italic' ), array( '400', '', 'i' ), $value );
121
				}
122
			}
123
			$this->fonts_to_load[] = $font . ':' . join( ',', $weights ) . ':cyrillic,cyrillic-ext,devanagari,greek,greek-ext,khmer,latin,latin-ext,vietnamese,hebrew,arabic,bengali,gujarati,tamil,telugu,thai';
124
		}
125
		if ( ! empty( $this->fonts_to_load ) ) {
126
			Kirki_Modules_Webfont_Loader::$load = true;
127
		}
128
	}
129
130
	/**
131
	 * Webfont Loader script for Google Fonts.
132
	 *
133
	 * @access public
134
	 * @since 3.0.0
135
	 */
136
	public function webfont_loader_script() {
137
		if ( ! empty( $this->fonts_to_load ) ) {
138
			wp_add_inline_script(
139
				'webfont-loader',
140
				'WebFont.load({google:{families:[\'' . join( '\', \'', $this->fonts_to_load ) . '\']}});',
141
				'after'
142
			);
143
		}
144
	}
145
}
146