Test Failed
Push — master ( ed3296...209135 )
by Stiofan
11:26
created

WP_Font_Awesome_Settings::instance()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 3
nop 0
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
1
<?php
2
/**
3
 * A class for adjusting font awesome settings on WordPress
4
 *
5
 * This class can be added to any plugin or theme and will add a settings screen to WordPress to control Font Awesome settings.
6
 *
7
 * @link https://github.com/AyeCode/wp-font-awesome-settings
8
 *
9
 * @internal This file should not be edited directly but pulled from the github repo above.
10
 */
11
12
/**
13
 * Bail if we are not in WP.
14
 */
15
if ( ! defined( 'ABSPATH' ) ) {
16
	exit;
17
}
18
19
/**
20
 * Only add if the class does not already exist.
21
 */
22
if ( ! class_exists( 'WP_Font_Awesome_Settings' ) ) {
23
24
	/**
25
	 * A Class to be able to change settings for Font Awesome.
26
	 *
27
	 * Class WP_Font_Awesome_Settings
28
	 * @ver 1.0.8
29
	 * @todo decide how to implement textdomain
30
	 */
31
	class WP_Font_Awesome_Settings {
32
33
		/**
34
		 * Class version version.
35
		 *
36
		 * @var string
37
		 */
38
		public $version = '1.0.8';
39
40
		/**
41
		 * Latest version of Font Awesome at time of publish published.
42
		 *
43
		 * @var string
44
		 */
45
		public $latest = "5.6.0";
46
47
		/**
48
		 * The title.
49
		 *
50
		 * @var string
51
		 */
52
		public $name = 'Font Awesome';
53
54
		/**
55
		 * Holds the settings values.
56
		 *
57
		 * @var array
58
		 */
59
		private $settings;
60
61
		/**
62
		 * WP_Font_Awesome_Settings instance.
63
		 *
64
		 * @access private
65
		 * @since  1.0.0
66
		 * @var    WP_Font_Awesome_Settings There can be only one!
67
		 */
68
		private static $instance = null;
69
70
		/**
71
		 * Main WP_Font_Awesome_Settings Instance.
72
		 *
73
		 * Ensures only one instance of WP_Font_Awesome_Settings is loaded or can be loaded.
74
		 *
75
		 * @since 1.0.0
76
		 * @static
77
		 * @return WP_Font_Awesome_Settings - Main instance.
78
		 */
79
		public static function instance() {
80
			if ( ! isset( self::$instance ) && ! ( self::$instance instanceof WP_Font_Awesome_Settings ) ) {
81
				self::$instance = new WP_Font_Awesome_Settings;
82
83
				add_action( 'init', array( self::$instance, 'init' ) ); // set settings
84
85
				if ( is_admin() ) {
86
					add_action( 'admin_menu', array( self::$instance, 'menu_item' ) );
87
					add_action( 'admin_init', array( self::$instance, 'register_settings' ) );
88
				}
89
90
				do_action( 'wp_font_awesome_settings_loaded' );
91
			}
92
93
			return self::$instance;
94
		}
95
96
		/**
97
		 * Initiate the settings and add the required action hooks.
98
		 *
99
		 * @since 1.0.8 Settings name wrong - FIXED
100
		 */
101
		public function init() {
102
			$this->settings = $this->get_settings();
103
104
			if ( $this->settings['type'] == 'CSS' ) {
105
106
				if ( $this->settings['enqueue'] == '' || $this->settings['enqueue'] == 'frontend' ) {
107
					add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_style' ), 5000 );//echo '###';exit;
108
				}
109
110 View Code Duplication
				if ( $this->settings['enqueue'] == '' || $this->settings['enqueue'] == 'backend' ) {
111
					add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_style' ), 5000 );
112
				}
113
114
			} else {
115
116 View Code Duplication
				if ( $this->settings['enqueue'] == '' || $this->settings['enqueue'] == 'frontend' ) {
117
					add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ), 5000 );//echo '###';exit;
118
				}
119
120 View Code Duplication
				if ( $this->settings['enqueue'] == '' || $this->settings['enqueue'] == 'backend' ) {
121
					add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ), 5000 );
122
				}
123
			}
124
125
			// remove font awesome if set to do so
126
			if ( $this->settings['dequeue'] == '1' ) {
127
				add_action( 'clean_url', array( $this, 'remove_font_awesome' ), 5000, 3 );
128
			}
129
130
		}
131
132
		/**
133
		 * Adds the Font Awesome styles.
134
		 */
135 View Code Duplication
		public function enqueue_style() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
136
			// build url
137
			$url = $this->get_url();
138
139
			wp_deregister_style( 'font-awesome' ); // deregister in case its already there
140
			wp_register_style( 'font-awesome', $url, array(), null );
141
			wp_enqueue_style( 'font-awesome' );
142
143
			if ( $this->settings['shims'] ) {
144
				$url = $this->get_url( true );
145
				wp_deregister_style( 'font-awesome-shims' ); // deregister in case its already there
146
				wp_register_style( 'font-awesome-shims', $url, array(), null );
147
				wp_enqueue_style( 'font-awesome-shims' );
148
			}
149
		}
150
151
		/**
152
		 * Adds the Font Awesome JS.
153
		 */
154 View Code Duplication
		public function enqueue_scripts() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
155
			// build url
156
			$url = $this->get_url();
157
158
			wp_deregister_script( 'font-awesome' ); // deregister in case its already there
159
			wp_register_script( 'font-awesome', $url, array(), null );
160
			wp_enqueue_script( 'font-awesome' );
161
162
			if ( $this->settings['shims'] ) {
163
				$url = $this->get_url( true );
164
				wp_deregister_script( 'font-awesome-shims' ); // deregister in case its already there
165
				wp_register_script( 'font-awesome-shims', $url, array(), null );
166
				wp_enqueue_script( 'font-awesome-shims' );
167
			}
168
		}
169
170
		/**
171
		 * Get the url of the Font Awesome files.
172
		 *
173
		 * @param bool $shims If this is a shim file or not.
174
		 *
175
		 * @return string The url to the file.
176
		 */
177
		public function get_url( $shims = false ) {
178
			$script  = $shims ? 'v4-shims' : 'all';
179
			$type    = $this->settings['type'];
180
			$version = $this->settings['version'];
181
182
			$url = "https://use.fontawesome.com/releases/"; // CDN
183
			$url .= ! empty( $version ) ? "v" . $version . '/' : "v" . $this->get_latest_version() . '/'; // version
184
			$url .= $type == 'CSS' ? 'css/' : 'js/'; // type
185
			$url .= $type == 'CSS' ? $script . '.css' : $script . '.js'; // type
186
			$url .= "?wpfas=true"; // set our var so our version is not removed
187
188
			return $url;
189
		}
190
191
		/**
192
		 * Try and remove any other versions of Font Awesome added by other plugins/themes.
193
		 *
194
		 * Uses the clean_url filter to try and remove any other Font Awesome files added, it can also add pseudo-elements flag for the JS version.
195
		 *
196
		 * @param $url
197
		 * @param $original_url
198
		 * @param $_context
199
		 *
200
		 * @return string The filtered url.
201
		 */
202
		public function remove_font_awesome( $url, $original_url, $_context ) {
203
204
			if ( $_context == 'display'
205
			     && ( strstr( $url, "fontawesome" ) !== false || strstr( $url, "font-awesome" ) !== false )
206
			     && ( strstr( $url, ".js" ) !== false || strstr( $url, ".css" ) !== false )
207
			) {// it's a font-awesome-url (probably)
208
209
				if ( strstr( $url, "wpfas=true" ) !== false ) {
210
					if ( $this->settings['type'] == 'JS' ) {
211
						if ( $this->settings['js-pseudo'] ) {
212
							$url .= "' data-search-pseudo-elements defer='defer";
213
						} else {
214
							$url .= "' defer='defer";
215
						}
216
					}
217
				} else {
218
					$url = ''; // removing the url removes the file
219
				}
220
221
			}
222
223
			return $url;
224
		}
225
226
		/**
227
		 * Register the database settings with WordPress.
228
		 */
229
		public function register_settings() {
230
			register_setting( 'wp-font-awesome-settings', 'wp-font-awesome-settings' );
231
		}
232
233
		/**
234
		 * Add the WordPress settings menu item.
235
		 */
236
		public function menu_item() {
237
			add_options_page( $this->name, $this->name, 'manage_options', 'wp-font-awesome-settings', array(
238
				$this,
239
				'settings_page'
240
			) );
241
		}
242
243
		/**
244
		 * Get the current Font Awesome output settings.
245
		 *
246
		 * @return array The array of settings.
247
		 */
248
		public function get_settings() {
249
250
			$db_settings = get_option( 'wp-font-awesome-settings' );
251
252
			$defaults = array(
253
				'type'      => 'CSS', // type to use, CSS or JS
254
				'version'   => '', // latest
255
				'enqueue'   => '', // front and backend
256
				'shims'     => '1', // default on for now, @todo maybe change to off in 2020
257
				'js-pseudo' => '0', // if the pseudo elements flag should be set (CPU intensive)
258
				'dequeue'   => '0', // if we should try to remove other versions added by other plugins/themes
259
			);
260
261
			$settings = wp_parse_args( $db_settings, $defaults );
262
263
			/**
264
			 * Filter the Font Awesome settings.
265
			 *
266
			 * @todo if we add this filer people might use it and then it defeates the purpose of this class :/
267
			 */
268
			return $this->settings = apply_filters( 'wp-font-awesome-settings', $settings, $db_settings, $defaults );
269
		}
270
271
272
		/**
273
		 * The settings page html output.
274
		 */
275
		public function settings_page() {
276
			if ( ! current_user_can( 'manage_options' ) ) {
277
				wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
278
			}
279
280
			// a hidden way to force the update of the verison number vai api instead of waiting the 48 hours
281
			if(isset($_REQUEST['force-version-check'])){
282
				$this->get_latest_version($force_api = true);
283
			}
284
			?>
285
			<div class="wrap">
286
				<h1><?php echo $this->name; ?></h1>
287
				<form method="post" action="options.php">
288
					<?php
289
					settings_fields( 'wp-font-awesome-settings' );
290
					do_settings_sections( 'wp-font-awesome-settings' );
291
					?>
292
					<table class="form-table">
293
						<tr valign="top">
294
							<th scope="row"><label for="wpfas-type"><?php _e( 'Type' ); ?></label></th>
295
							<td>
296
								<select name="wp-font-awesome-settings[type]" id="wpfas-type">
297
									<option
298
										value="CSS" <?php selected( $this->settings['type'], 'CSS' ); ?>><?php _e( 'CSS (default)' ); ?></option>
299
									<option value="JS" <?php selected( $this->settings['type'], 'JS' ); ?>>JS</option>
300
								</select>
301
							</td>
302
						</tr>
303
304
						<tr valign="top">
305
							<th scope="row"><label for="wpfas-version"><?php _e( 'Version' ); ?></label></th>
306
							<td>
307
								<select name="wp-font-awesome-settings[version]" id="wpfas-version">
308
									<option
309
										value="" <?php selected( $this->settings['version'], '' ); ?>><?php echo sprintf( __( 'Latest - %s (default)' ), $this->get_latest_version() ); ?></option>
310
									<option value="5.5.0" <?php selected( $this->settings['version'], '5.6.0' ); ?>>
311
										5.6.0
312
									</option>
313
									<option value="5.5.0" <?php selected( $this->settings['version'], '5.5.0' ); ?>>
314
										5.5.0
315
									</option>
316
									<option value="5.4.0" <?php selected( $this->settings['version'], '5.4.0' ); ?>>
317
										5.4.0
318
									</option>
319
									<option value="5.3.0" <?php selected( $this->settings['version'], '5.3.0' ); ?>>
320
										5.3.0
321
									</option>
322
									<option value="5.2.0" <?php selected( $this->settings['version'], '5.2.0' ); ?>>
323
										5.2.0
324
									</option>
325
									<option value="5.1.0" <?php selected( $this->settings['version'], '5.1.0' ); ?>>
326
										5.1.0
327
									</option>
328
									<option value="4.7.0" <?php selected( $this->settings['version'], '4.7.0' ); ?>>
329
										4.7.1 (CSS only)
330
									</option>
331
								</select>
332
							</td>
333
						</tr>
334
335
						<tr valign="top">
336
							<th scope="row"><label for="wpfas-enqueue"><?php _e( 'Enqueue' ); ?></label></th>
337
							<td>
338
								<select name="wp-font-awesome-settings[enqueue]" id="wpfas-enqueue">
339
									<option
340
										value="" <?php selected( $this->settings['enqueue'], '' ); ?>><?php _e( 'Frontend + Backend (default)' ); ?></option>
341
									<option
342
										value="frontend" <?php selected( $this->settings['enqueue'], 'frontend' ); ?>><?php _e( 'Frontend' ); ?></option>
343
									<option
344
										value="backend" <?php selected( $this->settings['enqueue'], 'backend' ); ?>><?php _e( 'Backend' ); ?></option>
345
								</select>
346
							</td>
347
						</tr>
348
349
						<tr valign="top">
350
							<th scope="row"><label
351
									for="wpfas-shims"><?php _e( 'Enable v4 shims compatibility' ); ?></label></th>
352
							<td>
353
								<input type="hidden" name="wp-font-awesome-settings[shims]" value="0"/>
354
								<input type="checkbox" name="wp-font-awesome-settings[shims]"
355
								       value="1" <?php checked( $this->settings['shims'], '1' ); ?> id="wpfas-shims"/>
356
								<span><?php _e( 'This enables v4 classes to work with v5, sort of like a band-aid until everyone has updated everything to v5.' ); ?></span>
357
							</td>
358
						</tr>
359
360
						<tr valign="top">
361
							<th scope="row"><label
362
									for="wpfas-js-pseudo"><?php _e( 'Enable JS pseudo elements (not recommended)' ); ?></label>
363
							</th>
364
							<td>
365
								<input type="hidden" name="wp-font-awesome-settings[js-pseudo]" value="0"/>
366
								<input type="checkbox" name="wp-font-awesome-settings[js-pseudo]"
367
								       value="1" <?php checked( $this->settings['js-pseudo'], '1' ); ?>
368
								       id="wpfas-js-pseudo"/>
369
								<span><?php _e( 'Used only with the JS version, this will make pseudo-elements work but can be CPU intensive on some sites.' ); ?></span>
370
							</td>
371
						</tr>
372
373
						<tr valign="top">
374
							<th scope="row"><label for="wpfas-dequeue"><?php _e( 'Dequeue' ); ?></label></th>
375
							<td>
376
								<input type="hidden" name="wp-font-awesome-settings[dequeue]" value="0"/>
377
								<input type="checkbox" name="wp-font-awesome-settings[dequeue]"
378
								       value="1" <?php checked( $this->settings['dequeue'], '1' ); ?>
379
								       id="wpfas-dequeue"/>
380
								<span><?php _e( 'This will try to dequeue any other Font Awesome versions loaded by other sources if they are added with `font-awesome` or `fontawesome` in the name.' ); ?></span>
381
							</td>
382
						</tr>
383
384
385
					</table>
386
					<?php
387
					submit_button();
388
					?>
389
				</form>
390
391
				<div id="wpfas-version"><?php echo $this->version; ?></div>
392
			</div>
393
394
			<?php
395
		}
396
397
		/**
398
		 * Check a version number is valid and if so return it or else return an empty string.
399
		 *
400
		 * @param $version string The version number to check.
401
		 * @since 1.0.6
402
		 *
403
		 * @return string Either a valid version number or an empty string.
404
		 */
405
		public function validate_version_number( $version ) {
406
407
			if ( version_compare( $version, '0.0.1', '>=' ) >= 0 ) {
408
				// valid
409
			} else {
410
				$version = '';// not validated
411
			}
412
413
			return $version;
414
		}
415
416
417
		/**
418
		 * Get the latest version of Font Awesome.
419
		 *
420
		 * We check for a cached bersion and if none we will check for a live version via API and then cache it for 48 hours.
421
		 *
422
		 * @since 1.0.7
423
		 * @return mixed|string The latest version number found.
424
		 */
425
		public function get_latest_version($force_api = false) {
426
			$latest_version = $this->latest;
427
428
			$cache = get_transient( 'wp-font-awesome-settings-version' );
429
430
			if ( $cache === false || $force_api) { // its not set
431
				$api_ver = $this->get_latest_version_from_api();
432
				if ( version_compare( $api_ver, $this->latest, '>=' ) >= 0 ) {
433
					$latest_version = $api_ver;
434
					set_transient( 'wp-font-awesome-settings-version', $api_ver, 48 * HOUR_IN_SECONDS );
435
				}
436
			} elseif ( $this->validate_version_number( $cache ) ) {
437
				if ( version_compare( $cache, $this->latest, '>=' ) >= 0 ) {
438
					$latest_version = $cache;
439
				}
440
			}
441
442
			return $latest_version;
443
		}
444
445
		/**
446
		 * Get the latest Font Awesome version from the github API.
447
		 *
448
		 * @since 1.0.7
449
		 * @return string The latest version number or `0` on API fail.
450
		 */
451
		public function get_latest_version_from_api() {
452
			$version  = "0";
453
			$response = wp_remote_get( "https://api.github.com/repos/FortAwesome/Font-Awesome/releases/latest" );
454
			if ( ! is_wp_error( $response ) && is_array( $response ) ) {
455
				$api_response = json_decode( wp_remote_retrieve_body( $response ), true );
456
				if ( isset( $api_response['tag_name'] ) && version_compare( $api_response['tag_name'], $this->latest, '>=' ) >= 0 && empty( $api_response['prerelease'] ) ) {
457
					$version = $api_response['tag_name'];
458
				}
459
			}
460
461
			return $version;
462
		}
463
464
	}
465
466
	/**
467
	 * Run the class if found.
468
	 */
469
	WP_Font_Awesome_Settings::instance();
470
}