Completed
Push — develop ( 639326...ee7bce )
by Aristeides
04:48
created

Kirki_Modules_Webfonts_Async::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 4
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
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     http://opensource.org/licenses/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
	 * Constructor.
47
	 *
48
	 * @access public
49
	 * @since 3.0
50
	 * @param string $config_id   The config-ID.
51
	 * @param object $webfonts    The Kirki_Modules_Webfonts object.
52
	 * @param object $googlefonts The Kirki_Fonts_Google object.
53
	 * @param array  $args        Extra args we want to pass.
54
	 */
55
	public function __construct( $config_id, $webfonts, $googlefonts, $args = array() ) {
1 ignored issue
show
Unused Code introduced by
The parameter $args is not used and could be removed.

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

Loading history...
56
57
		$this->config_id   = $config_id;
58
		$this->webfonts    = $webfonts;
59
		$this->googlefonts = $googlefonts;
60
61
		add_action( 'wp_head', array( $this, 'webfont_loader' ) );
62
	}
63
64
	/**
65
	 * Webfont Loader for Google Fonts.
66
	 *
67
	 * @access public
68
	 * @since 3.0.0
69
	 */
70
	public function webfont_loader() {
71
72
		// Go through our fields and populate $this->fonts.
73
		$this->webfonts->loop_fields();
74
75
		$this->googlefonts->fonts = apply_filters( 'kirki/enqueue_google_fonts', $this->googlefonts->fonts );
76
77
		// Goes through $this->fonts and adds or removes things as needed.
78
		$this->googlefonts->process_fonts();
79
80
		$fonts_to_load = array();
81
		foreach ( $this->googlefonts->fonts as $font => $weights ) {
82
			foreach ( $weights as $key => $value ) {
83
				$weights[ $key ] = str_replace( array( 'regular', 'bold', 'italic' ), array( '400', '', 'i' ), $value );
84
				if ( 'i' === $value ) {
85
					$weights[ $key ] = '400i';
86
				}
87
			}
88
			$fonts_to_load[] = $font . ':' . join( ',', $weights );
89
			https://fonts.googleapis.com/css?family=Roboto:300,300i,400&amp;subset=cyrillic,greek
90
		}
91
		var_dump( $fonts_to_load );
0 ignored issues
show
Security Debugging Code introduced by
var_dump($fonts_to_load); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
introduced by
The use of function var_dump() is discouraged
Loading history...
92
		wp_enqueue_script( 'webfont-loader', 'https://ajax.googleapis.com/ajax/libs/webfont/1.6.26/webfont.js' );
93
		wp_add_inline_script(
94
			'webfont-loader',
95
			'WebFont.load({google:{families:[\'' . join( '\', \'', $fonts_to_load ) . '\']}});',
96
			'after'
97
		);
98
	}
99
}
100