Completed
Branch master (3e1132)
by Aristeides
03:12
created

Kirki_GoogleFonts_Loader::link()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
rs 9.2
cc 4
eloc 7
nc 3
nop 0
1
<?php
2
3
class Kirki_GoogleFonts_Loader extends Kirki_GoogleFonts_Manager {
4
5
	/**
6
	 * The generated script
7
	 */
8
	private static $script;
0 ignored issues
show
Unused Code introduced by
The property $script is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
9
10
	private static $script_fonts = array();
11
12
	private static $link = '';
13
14
	public function __construct() {
15
		if ( 'script' == Kirki_Fonts::$mode ) {
16
			// Add script in <head>
17
			add_action( 'wp_head', array( $this, 'add_script' ) );
18
		} else {
19
			// enqueue link
20
			add_action( 'wp_enqueue_scripts', array( $this, 'link' ), 105 );
21
		}
22
	}
23
24
	private function loop_fields() {
25
		foreach ( Kirki::$fields as $field_id => $args ) {
26
			Kirki_GoogleFonts_Field_Processor::generate_google_fonts( $args );
27
		}
28
	}
29
30
	private function set_properties() {
31
32
		$this->loop_fields();
33
34
		// If we don't have any fonts then we can exit.
35
		if ( empty( parent::$fonts ) ) {
36
			return;
37
		}
38
		$all_subsets  = array();
39
		$link_fonts   = array();
40
		$script_fonts = array();
41
		foreach ( parent::$fonts as $font => $properties ) {
42
			$variants = implode( ',', $properties['variants'] );
43
			$subsets  = implode( ',', $properties['subsets'] );
44
45
			$link_font = str_replace( ' ', '+', $font );
46
			if ( ! empty( $variants ) ) {
47
				$link_font .= ':' . $variants;
48
			}
49
			$link_fonts[] = $link_font;
50
			$all_subsets = array_merge( $all_subsets, $properties['subsets'] );
51
52
			$script_fonts[] = str_replace( ' ', '+', $font ) . ':' . $variants . ':' . $subsets;
53
		}
54
55
		$all_subsets = array_unique( $all_subsets );
56
		self::$link  = 'https://fonts.googleapis.com/css?family=';
57
		self::$link .= implode( '|', $link_fonts );
58
		if ( ! empty( $all_subsets ) ) {
59
			self::$link  .= '&subset=' . implode( ',', $all_subsets );
60
		}
61
62
	}
63
64
65
	/**
66
	 * Enqueue Google fonts if necessary
67
	 */
68
	public function link() {
69
		$this->set_properties();
70
		$config = apply_filters( 'kirki/config', array() );
71
		/**
72
		 * If we have set $config['disable_google_fonts'] to true
73
		 * then do not proceed any further.
74
		 */
75
		if ( isset( $config['disable_google_fonts'] ) && true == $config['disable_google_fonts'] ) {
76
			return;
77
		}
78
		if ( ! empty( self::$link ) ) {
79
			wp_enqueue_style( 'kirki_google_fonts', self::$link, array(), null );
80
		}
81
	}
82
83
	public function add_script() {
84
		$this->set_properties();
85
		?>
86
		<script type="text/javascript" id="kirki-googlefonts">
87
			WebFontConfig = {
88
				google: { families: [ '<?php echo implode( '\', \'', self::$script_fonts ); ?>' ] },
89
				timeout: 2500 // Set the timeout to 2.5 seconds
90
			};
91
			(function() {
92
				var wf   = document.createElement('script');
93
				wf.src   = 'https://ajax.googleapis.com/ajax/libs/webfont/1.6.16/webfont.js';
94
				wf.type  = 'text/javascript';
95
				wf.async = 'true';
96
				var s    = document.getElementsByTagName('script')[0];
97
				s.parentNode.insertBefore(wf, s);
98
			})();
99
		</script>
100
		<?php
101
102
	}
103
104
}
105
106