Test Setup Failed
Pull Request — master (#1710)
by Aristeides
02:23
created

jQuery.extend.util.webfonts.getFontType   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
nc 3
nop 1
dl 0
loc 19
rs 8.8571
c 1
b 0
f 0
1
/* global ajaxurl */
2
var kirki = kirki || {};
0 ignored issues
show
Bug introduced by
The variable kirki seems to be never initialized.
Loading history...
Coding Style introduced by
As per coding-style, prefer block-scoped variables using let or const which have better semantics than var.

Since ECMAScript 6, you can create block-scoped vars or constants with the keywords let or const. These variables/constants are only valid in the code block where they have been declared.

Consider the following two pieces of code:

if (true)
 {
    var x = "Hello, Stonehenge!";
}

console.log(x); //prints Hello, Stonehenge! to the console

and

if (true)
 {
    let x = "Hello, Stonehenge!";
}

console.log(x); //ReferenceError: x is not defined

The variable is not defined otuside of its block. This limits bleeding of variables into other contexts.

To know more about this ECMA6 feature, look at the MDN pages on let and const.

Loading history...
3
kirki = jQuery.extend( kirki, {
4
	/**
5
	 * A collection of utility methods.
6
	 *
7
	 * @since 3.0.17
8
	 */
9
	util: {
10
11
		/**
12
		 * A collection of utility methods for webfonts.
13
		 *
14
		 * @since 3.0.17
15
		 */
16
		webfonts: {
17
18
			/**
19
			 * Google-fonts related methods.
20
			 *
21
			 * @since 3.0.17
22
			 */
23
			google: {
24
25
				/**
26
				 * An object containing all Google fonts.
27
				 *
28
				 * to set this call this.setFonts();
29
				 *
30
				 * @since 3.0.17
31
				 */
32
				fonts: {},
33
34
				/**
35
				 * Init for google-fonts.
36
				 *
37
				 * @since 3.0.17
38
				 * @returns {null}
39
				 */
40
				initialize: function() {
41
					var self = this;
0 ignored issues
show
Coding Style introduced by
As per coding-style, prefer block-scoped variables using let or const which have better semantics than var.

Since ECMAScript 6, you can create block-scoped vars or constants with the keywords let or const. These variables/constants are only valid in the code block where they have been declared.

Consider the following two pieces of code:

if (true)
 {
    var x = "Hello, Stonehenge!";
}

console.log(x); //prints Hello, Stonehenge! to the console

and

if (true)
 {
    let x = "Hello, Stonehenge!";
}

console.log(x); //ReferenceError: x is not defined

The variable is not defined otuside of its block. This limits bleeding of variables into other contexts.

To know more about this ECMA6 feature, look at the MDN pages on let and const.

Loading history...
42
43
					self.setFonts();
44
				},
45
46
				/**
47
				 * Set fonts in this.fonts
48
				 *
49
				 * @since 3.0.17
50
				 * @returns {null}
51
				 */
52
				setFonts: function() {
53
					var self = this;
0 ignored issues
show
Coding Style introduced by
As per coding-style, prefer block-scoped variables using let or const which have better semantics than var.

Since ECMAScript 6, you can create block-scoped vars or constants with the keywords let or const. These variables/constants are only valid in the code block where they have been declared.

Consider the following two pieces of code:

if (true)
 {
    var x = "Hello, Stonehenge!";
}

console.log(x); //prints Hello, Stonehenge! to the console

and

if (true)
 {
    let x = "Hello, Stonehenge!";
}

console.log(x); //ReferenceError: x is not defined

The variable is not defined otuside of its block. This limits bleeding of variables into other contexts.

To know more about this ECMA6 feature, look at the MDN pages on let and const.

Loading history...
54
55
					// No need to run if we already have the fonts.
56
					if ( ! _.isEmpty( self.fonts ) ) {
57
						return;
58
					}
59
60
					// Make an AJAX call to set the fonts object (alpha).
61
					jQuery.post( ajaxurl, { 'action': 'kirki_fonts_google_all_get' }, function( response ) {
62
63
						// Get fonts from the JSON array.
64
						self.fonts = JSON.parse( response );
65
					} );
66
				},
67
68
				/**
69
				 * Gets all properties of a font-family.
70
				 *
71
				 * @since 3.0.17
72
				 * @param {string} family - The font-family we're interested in.
73
				 * @returns {Object}
74
				 */
75
				getFont: function( family ) {
76
					var self = this,
0 ignored issues
show
Coding Style introduced by
As per coding-style, prefer block-scoped variables using let or const which have better semantics than var.

Since ECMAScript 6, you can create block-scoped vars or constants with the keywords let or const. These variables/constants are only valid in the code block where they have been declared.

Consider the following two pieces of code:

if (true)
 {
    var x = "Hello, Stonehenge!";
}

console.log(x); //prints Hello, Stonehenge! to the console

and

if (true)
 {
    let x = "Hello, Stonehenge!";
}

console.log(x); //ReferenceError: x is not defined

The variable is not defined otuside of its block. This limits bleeding of variables into other contexts.

To know more about this ECMA6 feature, look at the MDN pages on let and const.

Loading history...
77
					    fonts = self.getFonts();
78
79
					if ( 'undefined' === typeof fonts[ family ] ) {
80
						return false;
81
					}
82
					return fonts[ family ];
83
				},
84
85
				/**
86
				 * Gets all properties of a font-family.
87
				 *
88
				 * @since 3.0.17
89
				 * @param {string} order - How to order the fonts (alpha|popularity|trending).
90
				 * @param {int}    number - How many to get. 0 for all.
91
				 * @returns {Object}
92
				 */
93
				getFonts: function( order, number ) {
94
					var self    = this,
0 ignored issues
show
Coding Style introduced by
As per coding-style, prefer block-scoped variables using let or const which have better semantics than var.

Since ECMAScript 6, you can create block-scoped vars or constants with the keywords let or const. These variables/constants are only valid in the code block where they have been declared.

Consider the following two pieces of code:

if (true)
 {
    var x = "Hello, Stonehenge!";
}

console.log(x); //prints Hello, Stonehenge! to the console

and

if (true)
 {
    let x = "Hello, Stonehenge!";
}

console.log(x); //ReferenceError: x is not defined

The variable is not defined otuside of its block. This limits bleeding of variables into other contexts.

To know more about this ECMA6 feature, look at the MDN pages on let and const.

Loading history...
95
					    ordered = {},
96
					    partial = [];
0 ignored issues
show
Unused Code introduced by
The assignment to variable partial seems to be never used. Consider removing it.
Loading history...
97
98
					// Make sure order is correct.
99
					order  = order || 'alpha';
0 ignored issues
show
Comprehensibility Best Practice introduced by
This re-assigns to the parameter order. Re-assigning to parameters often makes code less readable, consider introducing a new variable instead.
Loading history...
100
					order  = ( 'alpha' !== order && 'popularity' !== order && 'trending' !== order ) ? 'alpha' : order;
101
102
					// Make sure number is correct.
103
					number = number || 0;
0 ignored issues
show
Comprehensibility Best Practice introduced by
This re-assigns to the parameter number. Re-assigning to parameters often makes code less readable, consider introducing a new variable instead.
Loading history...
104
					number = parseInt( number, 10 );
105
106
					if ( 'alpha' === order || 0 === number ) {
107
						ordered = self.fonts.items;
108
					} else {
109
						partial = _.first( self.fonts.order[ order ], number );
110
						_.each( partial, function( family ) {
111
							ordered[ family ] = self.fonts.items[ family ];
112
						} );
113
					}
114
115
					return ordered;
116
				},
117
118
				/**
119
				 * Gets the variants for a font-family.
120
				 *
121
				 * @since 3.0.17
122
				 * @param {string} family - The font-family we're interested in.
123
				 * @returns {Array}
124
				 */
125
				getVariants: function( family ) {
126
					var self = this,
0 ignored issues
show
Coding Style introduced by
As per coding-style, prefer block-scoped variables using let or const which have better semantics than var.

Since ECMAScript 6, you can create block-scoped vars or constants with the keywords let or const. These variables/constants are only valid in the code block where they have been declared.

Consider the following two pieces of code:

if (true)
 {
    var x = "Hello, Stonehenge!";
}

console.log(x); //prints Hello, Stonehenge! to the console

and

if (true)
 {
    let x = "Hello, Stonehenge!";
}

console.log(x); //ReferenceError: x is not defined

The variable is not defined otuside of its block. This limits bleeding of variables into other contexts.

To know more about this ECMA6 feature, look at the MDN pages on let and const.

Loading history...
127
					    font = self.getFont( family );
128
129
					// Early exit if font was not found.
130
					if ( ! font ) {
131
						return false;
132
					}
133
134
					// Early exit if font doesn't have variants.
135
					if ( _.isUndefined( font.variants ) ) {
136
						return false;
137
					}
138
139
					// Return the variants.
140
					return font.variants;
141
				},
142
143
				/**
144
				 * Get the subsets for a font-family.
145
				 *
146
				 * @since 3.0.17
147
				 * @param {string} family - The font-family we're interested in.
148
				 * @returns {Object}
149
				 */
150
				getSubsets: function( family ) {
151
					var self = this,
0 ignored issues
show
Coding Style introduced by
As per coding-style, prefer block-scoped variables using let or const which have better semantics than var.

Since ECMAScript 6, you can create block-scoped vars or constants with the keywords let or const. These variables/constants are only valid in the code block where they have been declared.

Consider the following two pieces of code:

if (true)
 {
    var x = "Hello, Stonehenge!";
}

console.log(x); //prints Hello, Stonehenge! to the console

and

if (true)
 {
    let x = "Hello, Stonehenge!";
}

console.log(x); //ReferenceError: x is not defined

The variable is not defined otuside of its block. This limits bleeding of variables into other contexts.

To know more about this ECMA6 feature, look at the MDN pages on let and const.

Loading history...
152
					    font = self.getFont( family );
153
154
					// Early exit if font was not found.
155
					if ( ! font ) {
156
						return false;
157
					}
158
159
					// Early exit if font doesn't have subsets.
160
					if ( _.isUndefined( font.subsets ) ) {
161
						return false;
162
					}
163
164
					// Return the variants.
165
					return font.subsets;
166
				}
167
			},
168
169
			/**
170
			 * Standard fonts related methods.
171
			 *
172
			 * @since 3.0.17
173
			 */
174
			standard: {
175
176
				/**
177
				 * An object containing all Standard fonts.
178
				 *
179
				 * to set this call this.setFonts();
180
				 *
181
				 * @since 3.0.17
182
				 */
183
				fonts: {},
184
185
				/**
186
				 * Init for google-fonts.
187
				 *
188
				 * @since 3.0.17
189
				 * @returns {null}
190
				 */
191
				initialize: function() {
192
					var self = this;
0 ignored issues
show
Coding Style introduced by
As per coding-style, prefer block-scoped variables using let or const which have better semantics than var.

Since ECMAScript 6, you can create block-scoped vars or constants with the keywords let or const. These variables/constants are only valid in the code block where they have been declared.

Consider the following two pieces of code:

if (true)
 {
    var x = "Hello, Stonehenge!";
}

console.log(x); //prints Hello, Stonehenge! to the console

and

if (true)
 {
    let x = "Hello, Stonehenge!";
}

console.log(x); //ReferenceError: x is not defined

The variable is not defined otuside of its block. This limits bleeding of variables into other contexts.

To know more about this ECMA6 feature, look at the MDN pages on let and const.

Loading history...
193
194
					self.setFonts();
195
				},
196
197
				/**
198
				 * Set fonts in this.fonts
199
				 *
200
				 * @since 3.0.17
201
				 * @returns {null}
202
				 */
203
				setFonts: function() {
204
					var self = this;
0 ignored issues
show
Coding Style introduced by
As per coding-style, prefer block-scoped variables using let or const which have better semantics than var.

Since ECMAScript 6, you can create block-scoped vars or constants with the keywords let or const. These variables/constants are only valid in the code block where they have been declared.

Consider the following two pieces of code:

if (true)
 {
    var x = "Hello, Stonehenge!";
}

console.log(x); //prints Hello, Stonehenge! to the console

and

if (true)
 {
    let x = "Hello, Stonehenge!";
}

console.log(x); //ReferenceError: x is not defined

The variable is not defined otuside of its block. This limits bleeding of variables into other contexts.

To know more about this ECMA6 feature, look at the MDN pages on let and const.

Loading history...
205
206
					// No need to run if we already have the fonts.
207
					if ( ! _.isEmpty( self.fonts ) ) {
208
						return;
209
					}
210
211
					// Make an AJAX call to set the fonts object.
212
					jQuery.post( ajaxurl, { 'action': 'kirki_fonts_standard_all_get' }, function( response ) {
213
214
						// Get fonts from the JSON array.
215
						self.fonts = JSON.parse( response );
216
					} );
217
				},
218
219
				/**
220
				 * Gets the variants for a font-family.
221
				 *
222
				 * @since 3.0.17
223
				 * @returns {Array}
224
				 */
225
				getVariants: function( family ) { // jshint ignore: line
0 ignored issues
show
Unused Code introduced by
The parameter family is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
226
					return ['regular', 'italic', '700', '700italic'];
227
				}
228
			},
229
230
			/**
231
			 * Figure out what this font-family is (google/standard)
232
			 *
233
			 * @since 3.0.20
234
			 * @param {string} family - The font-family.
235
			 * @returns {string|false} - Returns string if found (google|standard)
236
			 *                           and false in case the font-family is an arbitrary value
237
			 *                           not found anywhere in our font definitions.
238
			 */
239
			getFontType: function( family ) {
240
				var self = this;
0 ignored issues
show
Coding Style introduced by
As per coding-style, prefer block-scoped variables using let or const which have better semantics than var.

Since ECMAScript 6, you can create block-scoped vars or constants with the keywords let or const. These variables/constants are only valid in the code block where they have been declared.

Consider the following two pieces of code:

if (true)
 {
    var x = "Hello, Stonehenge!";
}

console.log(x); //prints Hello, Stonehenge! to the console

and

if (true)
 {
    let x = "Hello, Stonehenge!";
}

console.log(x); //ReferenceError: x is not defined

The variable is not defined otuside of its block. This limits bleeding of variables into other contexts.

To know more about this ECMA6 feature, look at the MDN pages on let and const.

Loading history...
241
242
				// Check for standard fonts first.
243
				if (
244
					'undefined' !== typeof self.standard.fonts[ family ] || (
245
						'undefined' !== typeof self.standard.fonts.stack &&
246
						'undefined' !== typeof self.standard.fonts.stack[ family ]
247
					)
248
				) {
249
					return 'standard';
250
				}
251
252
				// Check in googlefonts.
253
				if ( 'undefined' !== typeof self.google.fonts.items[ family ] ) {
254
					return 'google';
255
				}
256
				return false;
257
			}
258
		}
259
	}
260
} );
261