Completed
Push — develop ( 19da19...deadf6 )
by Aristeides
05:59
created

Kirki_Modules_Webfonts_Local::add_styles()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 26
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 5
nop 0
dl 0
loc 26
rs 8.5806
c 0
b 0
f 0
1
<?php
2
/**
3
 * Handles adding to the footer the @font-face CSS for locally-hosted google-fonts.
4
 * Solves privacy concerns with Google's CDN and their sometimes less-than-transparent policies.
5
 *
6
 * @package     Kirki
7
 * @category    Core
8
 * @author      Aristeides Stathopoulos
9
 * @copyright   Copyright (c) 2017, Aristeides Stathopoulos
10
 * @license     http://opensource.org/licenses/https://opensource.org/licenses/MIT
11
 * @since       3.0.28
12
 */
13
14
/**
15
 * Manages the way Google Fonts are enqueued.
16
 */
17
final class Kirki_Modules_Webfonts_Local {
18
19
	/**
20
	 * The config ID.
21
	 *
22
	 * @access protected
23
	 * @since 3.0.28
24
	 * @var string
25
	 */
26
	protected $config_id;
27
28
	/**
29
	 * The Kirki_Modules_Webfonts object.
30
	 *
31
	 * @access protected
32
	 * @since 3.0.28
33
	 * @var object
34
	 */
35
	protected $webfonts;
36
37
	/**
38
	 * The Kirki_Fonts_Google object.
39
	 *
40
	 * @access protected
41
	 * @since 3.0.28
42
	 * @var object
43
	 */
44
	protected $googlefonts;
45
46
	/**
47
	 * Fonts to load.
48
	 *
49
	 * @access protected
50
	 * @since 3.0.28
51
	 * @var array
52
	 */
53
	protected $fonts_to_load = array();
54
55
	/**
56
	 * Constructor.
57
	 *
58
	 * @access public
59
	 * @since 3..28
60
	 * @param object $webfonts    The Kirki_Modules_Webfonts object.
61
	 * @param object $googlefonts The Kirki_Fonts_Google object.
62
	 */
63
	public function __construct( $webfonts, $googlefonts ) {
64
65
		$this->webfonts    = $webfonts;
66
		$this->googlefonts = $googlefonts;
67
68
		add_action( 'wp_footer', array( $this, 'add_styles' ) );
69
	}
70
71
	/**
72
	 * Webfont Loader for Google Fonts.
73
	 *
74
	 * @access public
75
	 * @since 3.0.28
76
	 */
77
	public function add_styles() {
78
79
		// Go through our fields and populate $this->fonts.
80
		$this->webfonts->loop_fields( $this->config_id );
81
		$this->googlefonts->process_fonts();
82
		$hosted_fonts = $this->googlefonts->get_hosted_fonts();
83
84
		// Early exit if we don't need to add any fonts.
85
		if ( empty( $hosted_fonts ) ) {
86
			return;
87
		}
88
89
		// Make sure we only do this once per font-family.
90
		$hosted_fonts = array_unique( $hosted_fonts );
91
92
		// Start CSS.
93
		$css = '';
94
		foreach ( $hosted_fonts as $family ) {
95
96
			// Add the @font-face CSS for this font-family.
97
			$css .= Kirki_Fonts_Google_Local::init( $family )->get_css();
98
		}
99
100
		// If we've got CSS, add to the footer.
101
		if ( $css ) {
102
			echo '<style id="kirki-local-webfonts-' . esc_attr( sanitize_key( $this->config_id ) ) . '">' . $css . '</style>'; // WPCS: XSS ok.
103
		}
104
	}
105
}
106