Completed
Push — develop ( 38831a...4fffa2 )
by Aristeides
08:57 queued 03:28
created

Kirki_Modules_Webfonts_Link::create_link()   C

Complexity

Conditions 10
Paths 43

Size

Total Lines 49
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 24
nc 43
nop 0
dl 0
loc 49
rs 5.5471
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Adds a link for webfonts.
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_Link {
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
	 * The google link
47
	 *
48
	 * @access public
49
	 * @var string
50
	 */
51
	public $link = '';
52
53
	/**
54
	 * Constructor.
55
	 *
56
	 * @access public
57
	 * @since 3.0
58
	 */
59
	public function __construct( $config_id, $webfonts, $googlefonts, $args = array() ) {
60
61
		$this->config_id   = $config_id;
62
		$this->webfonts    = $webfonts;
63
		$this->googlefonts = $googlefonts;
64
65
		if ( ! isset( $args['enqueue'] ) || false !== $args['enqueue'] ) {
66
			add_action( 'wp_enqueue_scripts', array( $this, 'enqueue' ), 105 );
67
		}
68
	}
69
70
	/**
71
	 * Calls all the other necessary methods to populate and create the link.
72
	 */
73
	public function enqueue() {
74
75
		// Go through our fields and populate $this->fonts.
76
		$this->webfonts->loop_fields();
77
78
		$this->googlefonts->fonts = apply_filters( 'kirki/enqueue_google_fonts', $this->googlefonts->fonts );
79
80
		// Goes through $this->fonts and adds or removes things as needed.
81
		$this->googlefonts->process_fonts();
82
83
		// Go through $this->fonts and populate $this->link.
84
		$this->create_link();
85
86
		// If $this->link is not empty then enqueue it.
87
		if ( '' !== $this->link ) {
88
			wp_enqueue_style( 'kirki_google_fonts', $this->link, array(), null );
89
		}
90
	}
91
92
	/**
93
	 * Creates the google-fonts link.
94
	 */
95
	public function create_link() {
96
97
		// If we don't have any fonts then we can exit.
98
		if ( empty( $this->googlefonts->fonts ) ) {
99
			return;
100
		}
101
102
		// Add a fallback to Roboto.
103
		$font = 'Roboto';
104
105
		// Get font-family + subsets.
106
		$link_fonts = array();
107
		foreach ( $this->googlefonts->fonts as $font => $variants ) {
108
109
			// Are we force-loading all variants?
110
			if ( true === Kirki_Fonts_Google::$force_load_all_variants ) {
111
				if ( isset( $this->google_fonts[ $font ]['variants'] ) ) {
0 ignored issues
show
Bug introduced by
The property google_fonts does not seem to exist. Did you mean googlefonts?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
112
					$variants = $this->google_fonts[ $font ]['variants'];
0 ignored issues
show
Bug introduced by
The property google_fonts does not seem to exist. Did you mean googlefonts?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
113
				}
114
			}
115
			$variants = implode( ',', $variants );
116
117
			$link_font = str_replace( ' ', '+', $font );
118
			if ( ! empty( $variants ) ) {
119
				$link_font .= ':' . $variants;
120
			}
121
			$link_fonts[] = $link_font;
122
		}
123
124
		// Are we force-loading all subsets?
125
		if ( true === Kirki_Fonts_Google::$force_load_all_subsets ) {
126
127
			if ( isset( $this->google_fonts[ $font ]['subsets'] ) ) {
0 ignored issues
show
Bug introduced by
The property google_fonts does not seem to exist. Did you mean googlefonts?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
128
				foreach ( $this->google_fonts[ $font ]['subsets'] as $subset ) {
0 ignored issues
show
Bug introduced by
The property google_fonts does not seem to exist. Did you mean googlefonts?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
129
					$this->subsets[] = $subset;
0 ignored issues
show
Bug introduced by
The property subsets does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
130
				}
131
			}
132
		}
133
134
		if ( ! empty( $this->subsets ) ) {
135
			$this->subsets = array_unique( $this->subsets );
136
		}
137
138
		$this->link = add_query_arg( array(
139
			'family' => str_replace( '%2B', '+', urlencode( implode( '|', $link_fonts ) ) ),
140
			'subset' => urlencode( implode( ',', $this->googlefonts->subsets ) ),
141
		), 'https://fonts.googleapis.com/css' );
142
143
	}
144
}
145