Passed
Push — master ( ae49aa...a584b2 )
by Brian
16:32
created

WP_Font_Awesome_Settings::get_path_url()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 2
nop 0
dl 0
loc 14
rs 10
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
	 * @since 1.0.10 Now able to pass wp.org theme check.
29
	 * @since 1.0.11 Font Awesome Pro now supported.
30
	 * @since 1.0.11 Font Awesome Kits now supported.
31
	 * @since 1.0.13 RTL language support added.
32
	 * @since 1.0.14 Warning added for v6 pro requires kit and will now not work if official FA plugin installed.
33
	 * @since 1.0.15 Font Awesome will now load in the FSE if enable din the backend.
34
	 * @since 1.1.0 Option added to load FontAwesome locally.
35
	 * @since 1.1.1 Requires to re-save settings to load locally when option does not exists - FIXED.
36
	 * @since 1.1.2 Bumped the latest version to 6.3.0 - CHANGED.
37
     * @since 1.1.3 Added JS files for iconpicker and added constant for URL for AyeCode-UI - ADDED.
38
     * @since 1.1.5 Added constant for when pro enabled - ADDED.
39
     * @since 1.1.6 Calling FA in wp_footer can cause issues on frontend - REVERTED
40
	 * @ver 1.1.6
41
	 * @todo decide how to implement textdomain
42
	 */
43
	class WP_Font_Awesome_Settings {
44
45
		/**
46
		 * Class version version.
47
		 *
48
		 * @var string
49
		 */
50
		public $version = '1.1.6';
51
52
		/**
53
		 * Class textdomain.
54
		 *
55
		 * @var string
56
		 */
57
		public $textdomain = 'font-awesome-settings';
58
59
		/**
60
		 * Latest version of Font Awesome at time of publish published.
61
		 *
62
		 * @var string
63
		 */
64
		public $latest = "6.4.2";
65
66
		/**
67
		 * The title.
68
		 *
69
		 * @var string
70
		 */
71
		public $name = 'Font Awesome';
72
73
		/**
74
		 * Holds the settings values.
75
		 *
76
		 * @var array
77
		 */
78
		private $settings;
79
80
		/**
81
		 * WP_Font_Awesome_Settings instance.
82
		 *
83
		 * @access private
84
		 * @since  1.0.0
85
		 * @var    WP_Font_Awesome_Settings There can be only one!
86
		 */
87
		private static $instance = null;
88
89
		/**
90
		 * Main WP_Font_Awesome_Settings Instance.
91
		 *
92
		 * Ensures only one instance of WP_Font_Awesome_Settings is loaded or can be loaded.
93
		 *
94
		 * @since 1.0.0
95
		 * @static
96
		 * @return WP_Font_Awesome_Settings - Main instance.
97
		 */
98
		public static function instance() {
99
			if ( ! isset( self::$instance ) && ! ( self::$instance instanceof WP_Font_Awesome_Settings ) ) {
100
				self::$instance = new WP_Font_Awesome_Settings;
101
102
				add_action( 'init', array( self::$instance, 'init' ) ); // set settings
103
104
				if ( is_admin() ) {
105
					add_action( 'admin_menu', array( self::$instance, 'menu_item' ) );
106
					add_action( 'admin_init', array( self::$instance, 'register_settings' ) );
107
					add_action( 'admin_init', array( self::$instance, 'constants' ) );
108
					add_action( 'admin_notices', array( self::$instance, 'admin_notices' ) );
109
				}
110
111
				do_action( 'wp_font_awesome_settings_loaded' );
112
			}
113
114
			return self::$instance;
115
		}
116
117
		/**
118
         * Define any constants that may be needed by other packages.
119
         *
120
		 * @return void
121
		 */
122
		public function constants(){
123
124
			// register iconpicker constant
125
			if ( ! defined( 'FAS_ICONPICKER_JS_URL' ) ) {
126
				$url = $this->get_path_url();
127
				$version = $this->settings['version'];
128
129
				if( !$version || version_compare($version,'5.999','>')){
130
					$url .= 'assets/js/fa-iconpicker-v6.min.js';
131
				}else{
132
					$url .= 'assets/js/fa-iconpicker-v5.min.js';
133
				}
134
135
				define( 'FAS_ICONPICKER_JS_URL', $url );
136
137
			}
138
139
            // Set a constant if pro enbaled
140
			if ( ! defined( 'FAS_PRO' ) && $this->settings['pro'] ) {
141
				define( 'FAS_PRO', true );
142
			}
143
		}
144
145
		/**
146
		 * Get the url path to the current folder.
147
		 *
148
		 * @return string
149
		 */
150
		public function get_path_url() {
151
			$content_dir = wp_normalize_path( untrailingslashit( WP_CONTENT_DIR ) );
152
			$content_url = untrailingslashit( WP_CONTENT_URL );
153
154
			// Replace http:// to https://.
155
			if ( strpos( $content_url, 'http://' ) === 0 && strpos( plugins_url(), 'https://' ) === 0 ) {
156
				$content_url = str_replace( 'http://', 'https://', $content_url );
157
			}
158
159
			// Check if we are inside a plugin
160
			$file_dir = str_replace( "/includes", "", wp_normalize_path( dirname( __FILE__ ) ) );
161
			$url = str_replace( $content_dir, $content_url, $file_dir );
162
163
			return trailingslashit( $url );
164
		}
165
166
		/**
167
		 * Initiate the settings and add the required action hooks.
168
		 *
169
		 * @since 1.0.8 Settings name wrong - FIXED
170
		 */
171
		public function init() {
172
			// Download fontawesome locally.
173
			add_action( 'add_option_wp-font-awesome-settings', array( $this, 'add_option_wp_font_awesome_settings' ), 10, 2 );
174
			add_action( 'update_option_wp-font-awesome-settings', array( $this, 'update_option_wp_font_awesome_settings' ), 10, 2 );
175
176
			$this->settings = $this->get_settings();
177
178
			// check if the official plugin is active and use that instead if so.
179
			if ( ! defined( 'FONTAWESOME_PLUGIN_FILE' ) ) {
180
181
				if ( $this->settings['type'] == 'CSS' ) {
182
183
					if ( $this->settings['enqueue'] == '' || $this->settings['enqueue'] == 'frontend' ) {
184
						add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_style' ), 5000 );
185
//						add_action( 'wp_footer', array( $this, 'enqueue_style' ), 5000 ); // not sure why this was added, seems to break frontend
186
					}
187
188
					if ( $this->settings['enqueue'] == '' || $this->settings['enqueue'] == 'backend' ) {
189
						add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_style' ), 5000 );
190
						add_filter( 'block_editor_settings_all', array( $this, 'enqueue_editor_styles' ), 10, 2 );
191
					}
192
193
				} else {
194
195
					if ( $this->settings['enqueue'] == '' || $this->settings['enqueue'] == 'frontend' ) {
196
						add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ), 5000 );
197
					}
198
199
					if ( $this->settings['enqueue'] == '' || $this->settings['enqueue'] == 'backend' ) {
200
						add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ), 5000 );
201
						add_filter( 'block_editor_settings_all', array( $this, 'enqueue_editor_scripts' ), 10, 2 );
202
					}
203
				}
204
205
				// remove font awesome if set to do so
206
				if ( $this->settings['dequeue'] == '1' ) {
207
					add_action( 'clean_url', array( $this, 'remove_font_awesome' ), 5000, 3 );
208
				}
209
			}
210
211
		}
212
213
		/**
214
		 * Add FA to the FSE.
215
		 *
216
		 * @param $editor_settings
217
		 * @param $block_editor_context
218
		 *
219
		 * @return array
220
		 */
221
		public function enqueue_editor_styles( $editor_settings, $block_editor_context ){
0 ignored issues
show
Unused Code introduced by
The parameter $block_editor_context is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

221
		public function enqueue_editor_styles( $editor_settings, /** @scrutinizer ignore-unused */ $block_editor_context ){

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
222
223
			if ( ! empty( $editor_settings['__unstableResolvedAssets']['styles'] ) ) {
224
				$url = $this->get_url();
225
				$editor_settings['__unstableResolvedAssets']['styles'] .= "<link rel='stylesheet' id='font-awesome-css'  href='$url' media='all' />";
226
			}
227
228
			return $editor_settings;
229
		}
230
231
		/**
232
		 * Add FA to the FSE.
233
		 *
234
		 * @param $editor_settings
235
		 * @param $block_editor_context
236
		 *
237
		 * @return array
238
		 */
239
		public function enqueue_editor_scripts( $editor_settings, $block_editor_context ){
0 ignored issues
show
Unused Code introduced by
The parameter $block_editor_context is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

239
		public function enqueue_editor_scripts( $editor_settings, /** @scrutinizer ignore-unused */ $block_editor_context ){

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
240
241
			$url = $this->get_url();
242
			$editor_settings['__unstableResolvedAssets']['scripts'] .= "<script src='$url' id='font-awesome-js'></script>";
243
244
			return $editor_settings;
245
		}
246
247
		/**
248
		 * Adds the Font Awesome styles.
249
		 */
250
		public function enqueue_style() {
251
			// build url
252
			$url = $this->get_url();
253
			$version = ! empty( $this->settings['local'] ) && empty( $this->settings['pro'] ) ? strip_tags( $this->settings['local_version'] ) : null;
254
255
			wp_deregister_style( 'font-awesome' ); // deregister in case its already there
256
			wp_register_style( 'font-awesome', $url, array(), $version );
257
			wp_enqueue_style( 'font-awesome' );
258
259
			// RTL language support CSS.
260
			if ( is_rtl() ) {
261
				wp_add_inline_style( 'font-awesome', $this->rtl_inline_css() );
262
			}
263
264
			if ( $this->settings['shims'] ) {
265
				$url = $this->get_url( true );
266
				wp_deregister_style( 'font-awesome-shims' ); // deregister in case its already there
267
				wp_register_style( 'font-awesome-shims', $url, array(), $version );
268
				wp_enqueue_style( 'font-awesome-shims' );
269
			}
270
		}
271
272
		/**
273
		 * Adds the Font Awesome JS.
274
		 */
275
		public function enqueue_scripts() {
276
			// build url
277
			$url = $this->get_url();
278
279
			$deregister_function = 'wp' . '_' . 'deregister' . '_' . 'script';
280
			call_user_func( $deregister_function, 'font-awesome' ); // deregister in case its already there
281
			wp_register_script( 'font-awesome', $url, array(), null );
282
			wp_enqueue_script( 'font-awesome' );
283
284
			if ( $this->settings['shims'] ) {
285
				$url = $this->get_url( true );
286
				call_user_func( $deregister_function, 'font-awesome-shims' ); // deregister in case its already there
287
				wp_register_script( 'font-awesome-shims', $url, array(), null );
288
				wp_enqueue_script( 'font-awesome-shims' );
289
			}
290
		}
291
292
		/**
293
		 * Get the url of the Font Awesome files.
294
		 *
295
		 * @param bool $shims If this is a shim file or not.
296
		 * @param bool $local Load locally if allowed.
297
		 *
298
		 * @return string The url to the file.
299
		 */
300
		public function get_url( $shims = false, $local = true ) {
301
			$script  = $shims ? 'v4-shims' : 'all';
302
			$sub     = $this->settings['pro'] ? 'pro' : 'use';
303
			$type    = $this->settings['type'];
304
			$version = $this->settings['version'];
305
			$kit_url = $this->settings['kit-url'] ? esc_url( $this->settings['kit-url'] ) : '';
306
			$url     = '';
307
308
			if ( $type == 'KIT' && $kit_url ) {
309
				if ( $shims ) {
310
					// if its a kit then we don't add shims here
311
					return '';
312
				}
313
				$url .= $kit_url; // CDN
314
				$url .= "?wpfas=true"; // set our var so our version is not removed
315
			} else {
316
				$v = '';
317
				// Check and load locally.
318
				if ( $local && $this->has_local() ) {
319
					$script .= ".min";
320
					$v .= '&ver=' . strip_tags( $this->settings['local_version'] );
321
					$url .= $this->get_fonts_url(); // Local fonts url.
322
				} else {
323
					$url .= "https://$sub.fontawesome.com/releases/"; // CDN
324
					$url .= ! empty( $version ) ? "v" . $version . '/' : "v" . $this->get_latest_version() . '/'; // version
325
				}
326
				$url .= $type == 'CSS' ? 'css/' : 'js/'; // type
327
				$url .= $type == 'CSS' ? $script . '.css' : $script . '.js'; // type
328
				$url .= "?wpfas=true" . $v; // set our var so our version is not removed
329
			}
330
331
			return $url;
332
		}
333
334
		/**
335
		 * Try and remove any other versions of Font Awesome added by other plugins/themes.
336
		 *
337
		 * 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.
338
		 *
339
		 * @param $url
340
		 * @param $original_url
341
		 * @param $_context
342
		 *
343
		 * @return string The filtered url.
344
		 */
345
		public function remove_font_awesome( $url, $original_url, $_context ) {
346
347
			if ( $_context == 'display'
348
			     && ( strstr( $url, "fontawesome" ) !== false || strstr( $url, "font-awesome" ) !== false )
349
			     && ( strstr( $url, ".js" ) !== false || strstr( $url, ".css" ) !== false )
350
			) {// it's a font-awesome-url (probably)
351
352
				if ( strstr( $url, "wpfas=true" ) !== false ) {
353
					if ( $this->settings['type'] == 'JS' ) {
354
						if ( $this->settings['js-pseudo'] ) {
355
							$url .= "' data-search-pseudo-elements defer='defer";
356
						} else {
357
							$url .= "' defer='defer";
358
						}
359
					}
360
				} else {
361
					$url = ''; // removing the url removes the file
362
				}
363
364
			}
365
366
			return $url;
367
		}
368
369
		/**
370
		 * Register the database settings with WordPress.
371
		 */
372
		public function register_settings() {
373
			register_setting( 'wp-font-awesome-settings', 'wp-font-awesome-settings' );
374
		}
375
376
		/**
377
		 * Add the WordPress settings menu item.
378
		 * @since 1.0.10 Calling function name direct will fail theme check so we don't.
379
		 */
380
		public function menu_item() {
381
			$menu_function = 'add' . '_' . 'options' . '_' . 'page'; // won't pass theme check if function name present in theme
382
			call_user_func( $menu_function, $this->name, $this->name, 'manage_options', 'wp-font-awesome-settings', array(
383
				$this,
384
				'settings_page'
385
			) );
386
		}
387
388
		/**
389
		 * Get the current Font Awesome output settings.
390
		 *
391
		 * @return array The array of settings.
392
		 */
393
		public function get_settings() {
394
			$db_settings = get_option( 'wp-font-awesome-settings' );
395
396
			$defaults = array(
397
				'type'      => 'CSS', // type to use, CSS or JS or KIT
398
				'version'   => '', // latest
399
				'enqueue'   => '', // front and backend
400
				'shims'     => '0', // default OFF now in 2020
401
				'js-pseudo' => '0', // if the pseudo elements flag should be set (CPU intensive)
402
				'dequeue'   => '0', // if we should try to remove other versions added by other plugins/themes
403
				'pro'       => '0', // if pro CDN url should be used
404
				'local'     => '0', // Store fonts locally.
405
				'local_version' => '', // Local fonts version.
406
				'kit-url'   => '', // the kit url
407
			);
408
409
			$settings = wp_parse_args( $db_settings, $defaults );
0 ignored issues
show
Bug introduced by
It seems like $db_settings can also be of type false; however, parameter $args of wp_parse_args() does only seem to accept array|object|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

409
			$settings = wp_parse_args( /** @scrutinizer ignore-type */ $db_settings, $defaults );
Loading history...
410
411
			/**
412
			 * Filter the Font Awesome settings.
413
			 *
414
			 * @todo if we add this filer people might use it and then it defeates the purpose of this class :/
415
			 */
416
			return $this->settings = apply_filters( 'wp-font-awesome-settings', $settings, $db_settings, $defaults );
417
		}
418
419
		/**
420
		 * The settings page html output.
421
		 */
422
		public function settings_page() {
423
			if ( ! current_user_can( 'manage_options' ) ) {
424
				wp_die( __( 'You do not have sufficient permissions to access this page.', 'font-awesome-settings' ) );
425
			}
426
427
			// a hidden way to force the update of the version number via api instead of waiting the 48 hours
428
			if ( isset( $_REQUEST['force-version-check'] ) ) {
429
				$this->get_latest_version( $force_api = true );
430
			}
431
432
			if ( ! defined( 'FONTAWESOME_PLUGIN_FILE' ) ) {
433
				?>
434
                <style>
435
                    .wpfas-kit-show {
436
                        display: none;
437
                    }
438
                    .wpfas-kit-set .wpfas-kit-hide,.wpfas-has-pro .wpfas-hide-pro {
439
                        display: none;
440
                    }
441
                    .wpfas-kit-set .wpfas-kit-show {
442
                        display: table-row;
443
                    }
444
                    .fas-settings-form .submit{
445
                        display: inline;
446
                        padding-right: 5px;
447
                    }
448
                    .fas-settings-form .fas-buttons{
449
                        margin: 15px 0;
450
                    }
451
                    #wpfas-version{
452
                        color: #646970;
453
                    }
454
                </style>
455
                <div class="wrap">
456
                    <h1><?php echo $this->name; ?></h1>
457
                    <form method="post" action="options.php" class="fas-settings-form">
458
						<?php
459
						settings_fields( 'wp-font-awesome-settings' );
460
						do_settings_sections( 'wp-font-awesome-settings' );
461
						$table_class = '';
462
						if ( $this->settings['type'] ) {
463
							$table_class .= 'wpfas-' . sanitize_html_class( strtolower( $this->settings['type'] ) ) . '-set';
464
						}
465
						if ( ! empty( $this->settings['pro'] ) ) {
466
							$table_class .= ' wpfas-has-pro';
467
						}
468
						?>
469
						<?php if ( $this->settings['type'] != 'KIT' && ! empty( $this->settings['local'] ) && empty( $this->settings['pro'] ) ) { ?>
470
							<?php if ( $this->has_local() ) { ?>
471
                                <div class="notice notice-info"><p><strong><?php _e( 'Font Awesome fonts are loading locally.', 'font-awesome-settings' ); ?></strong></p></div>
472
							<?php } else { ?>
473
                                <div class="notice notice-error"><p><strong><?php _e( 'Font Awesome fonts are not loading locally!', 'font-awesome-settings' ); ?></strong></p></div>
474
							<?php } ?>
475
						<?php } ?>
476
                        <table class="form-table wpfas-table-settings <?php echo esc_attr( $table_class ); ?>">
477
                            <tr valign="top">
478
                                <th scope="row"><label for="wpfas-type"><?php _e( 'Type', 'font-awesome-settings' ); ?></label></th>
479
                                <td>
480
                                    <select name="wp-font-awesome-settings[type]" id="wpfas-type" onchange="if(this.value=='KIT'){jQuery('.wpfas-table-settings').addClass('wpfas-kit-set');}else{jQuery('.wpfas-table-settings').removeClass('wpfas-kit-set');}">
481
                                        <option value="CSS" <?php selected( $this->settings['type'], 'CSS' ); ?>><?php _e( 'CSS (default)', 'font-awesome-settings' ); ?></option>
482
                                        <option value="JS" <?php selected( $this->settings['type'], 'JS' ); ?>>JS</option>
483
                                        <option value="KIT" <?php selected( $this->settings['type'], 'KIT' ); ?>><?php _e( 'Kits (settings managed on fontawesome.com)', 'font-awesome-settings' ); ?></option>
484
                                    </select>
485
                                </td>
486
                            </tr>
487
488
                            <tr valign="top" class="wpfas-kit-show">
489
                                <th scope="row"><label for="wpfas-kit-url"><?php _e( 'Kit URL', 'font-awesome-settings' ); ?></label></th>
490
                                <td>
491
                                    <input class="regular-text" id="wpfas-kit-url" type="url" name="wp-font-awesome-settings[kit-url]" value="<?php echo esc_attr( $this->settings['kit-url'] ); ?>" placeholder="<?php echo 'https://kit.font';echo 'awesome.com/123abc.js'; // this won't pass theme check :(?>"/>
492
                                    <span><?php
493
										echo sprintf(
494
											__( 'Requires a free account with Font Awesome. %sGet kit url%s', 'font-awesome-settings' ),
495
											'<a rel="noopener noreferrer" target="_blank" href="https://fontawesome.com/kits"><i class="fas fa-external-link-alt"></i>',
496
											'</a>'
497
										);
498
										?></span>
499
                                </td>
500
                            </tr>
501
502
                            <tr valign="top" class="wpfas-kit-hide">
503
                                <th scope="row"><label for="wpfas-version"><?php _e( 'Version', 'font-awesome-settings' ); ?></label></th>
504
                                <td>
505
                                    <select name="wp-font-awesome-settings[version]" id="wpfas-version">
506
                                        <option value="" <?php selected( $this->settings['version'], '' ); ?>><?php echo sprintf( __( 'Latest - %s (default)', 'font-awesome-settings' ), $this->get_latest_version() ); ?></option>
507
                                        <option value="6.1.0" <?php selected( $this->settings['version'], '6.1.0' ); ?>>6.1.0</option>
508
                                        <option value="6.0.0" <?php selected( $this->settings['version'], '6.0.0' ); ?>>6.0.0</option>
509
                                        <option value="5.15.4" <?php selected( $this->settings['version'], '5.15.4' ); ?>>5.15.4</option>
510
                                        <option value="5.6.0" <?php selected( $this->settings['version'], '5.6.0' ); ?>>5.6.0</option>
511
                                        <option value="5.5.0" <?php selected( $this->settings['version'], '5.5.0' ); ?>>5.5.0</option>
512
                                        <option value="5.4.0" <?php selected( $this->settings['version'], '5.4.0' ); ?>>5.4.0</option>
513
                                        <option value="5.3.0" <?php selected( $this->settings['version'], '5.3.0' ); ?>>5.3.0</option>
514
                                        <option value="5.2.0" <?php selected( $this->settings['version'], '5.2.0' ); ?>>5.2.0</option>
515
                                        <option value="5.1.0" <?php selected( $this->settings['version'], '5.1.0' ); ?>>5.1.0</option>
516
                                        <option value="4.7.0" <?php selected( $this->settings['version'], '4.7.0' ); ?>>4.7.1 (CSS only)</option>
517
                                    </select>
518
                                </td>
519
                            </tr>
520
521
                            <tr valign="top">
522
                                <th scope="row"><label for="wpfas-enqueue"><?php _e( 'Enqueue', 'font-awesome-settings' ); ?></label></th>
523
                                <td>
524
                                    <select name="wp-font-awesome-settings[enqueue]" id="wpfas-enqueue">
525
                                        <option value="" <?php selected( $this->settings['enqueue'], '' ); ?>><?php _e( 'Frontend + Backend (default)', 'font-awesome-settings' ); ?></option>
526
                                        <option value="frontend" <?php selected( $this->settings['enqueue'], 'frontend' ); ?>><?php _e( 'Frontend', 'font-awesome-settings' ); ?></option>
527
                                        <option value="backend" <?php selected( $this->settings['enqueue'], 'backend' ); ?>><?php _e( 'Backend', 'font-awesome-settings' ); ?></option>
528
                                    </select>
529
                                </td>
530
                            </tr>
531
532
                            <tr valign="top" class="wpfas-kit-hide">
533
                                <th scope="row"><label
534
                                            for="wpfas-pro"><?php _e( 'Enable pro', 'font-awesome-settings' ); ?></label></th>
535
                                <td>
536
                                    <input type="hidden" name="wp-font-awesome-settings[pro]" value="0"/>
537
                                    <input type="checkbox" name="wp-font-awesome-settings[pro]" value="1" <?php checked( $this->settings['pro'], '1' ); ?> id="wpfas-pro" onchange="if(jQuery(this).is(':checked')){jQuery('.wpfas-table-settings').addClass('wpfas-has-pro')}else{jQuery('.wpfas-table-settings').removeClass('wpfas-has-pro')}"/>
538
                                    <span><?php
539
										echo wp_sprintf(
540
											__( 'Requires a subscription. %sLearn more%s  %sManage my allowed domains%s', 'font-awesome-settings' ),
541
											'<a rel="noopener noreferrer" target="_blank" href="https://fontawesome.com/referral?a=c9b89e1418">',
542
											' <i class="fas fa-external-link-alt"></i></a>',
543
											'<a rel="noopener noreferrer" target="_blank" href="https://fontawesome.com/account/cdn">',
544
											' <i class="fas fa-external-link-alt"></i></a>'
545
										);
546
										?></span>
547
                                </td>
548
                            </tr>
549
550
                            <tr valign="top" class="wpfas-kit-hide wpfas-hide-pro">
551
                                <th scope="row"><label for="wpfas-local"><?php _e( 'Load Fonts Locally', 'font-awesome-settings' ); ?></label></th>
552
                                <td>
553
                                    <input type="hidden" name="wp-font-awesome-settings[local]" value="0"/>
554
                                    <input type="hidden" name="wp-font-awesome-settings[local_version]" value="<?php echo esc_attr( $this->settings['local_version'] ); ?>"/>
555
                                    <input type="checkbox" name="wp-font-awesome-settings[local]" value="1" <?php checked( $this->settings['local'], '1' ); ?> id="wpfas-local"/>
556
                                    <span><?php _e( '(For free version only) Load FontAwesome fonts from locally. This downloads FontAwesome fonts from fontawesome.com & stores at the local site.', 'font-awesome-settings' ); ?></span>
557
                                </td>
558
                            </tr>
559
560
                            <tr valign="top" class="wpfas-kit-hide">
561
                                <th scope="row"><label
562
                                            for="wpfas-shims"><?php _e( 'Enable v4 shims compatibility', 'font-awesome-settings' ); ?></label>
563
                                </th>
564
                                <td>
565
                                    <input type="hidden" name="wp-font-awesome-settings[shims]" value="0"/>
566
                                    <input type="checkbox" name="wp-font-awesome-settings[shims]"
567
                                           value="1" <?php checked( $this->settings['shims'], '1' ); ?> id="wpfas-shims"/>
568
                                    <span><?php _e( 'This enables v4 classes to work with v5, sort of like a band-aid until everyone has updated everything to v5.', 'font-awesome-settings' ); ?></span>
569
                                </td>
570
                            </tr>
571
572
                            <tr valign="top" class="wpfas-kit-hide">
573
                                <th scope="row"><label
574
                                            for="wpfas-js-pseudo"><?php _e( 'Enable JS pseudo elements (not recommended)', 'font-awesome-settings' ); ?></label>
575
                                </th>
576
                                <td>
577
                                    <input type="hidden" name="wp-font-awesome-settings[js-pseudo]" value="0"/>
578
                                    <input type="checkbox" name="wp-font-awesome-settings[js-pseudo]"
579
                                           value="1" <?php checked( $this->settings['js-pseudo'], '1' ); ?>
580
                                           id="wpfas-js-pseudo"/>
581
                                    <span><?php _e( 'Used only with the JS version, this will make pseudo-elements work but can be CPU intensive on some sites.', 'font-awesome-settings' ); ?></span>
582
                                </td>
583
                            </tr>
584
585
                            <tr valign="top">
586
                                <th scope="row"><label
587
                                            for="wpfas-dequeue"><?php _e( 'Dequeue', 'font-awesome-settings' ); ?></label></th>
588
                                <td>
589
                                    <input type="hidden" name="wp-font-awesome-settings[dequeue]" value="0"/>
590
                                    <input type="checkbox" name="wp-font-awesome-settings[dequeue]"
591
                                           value="1" <?php checked( $this->settings['dequeue'], '1' ); ?>
592
                                           id="wpfas-dequeue"/>
593
                                    <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.', 'font-awesome-settings' ); ?></span>
594
                                </td>
595
                            </tr>
596
597
                        </table>
598
                        <div class="fas-buttons">
599
							<?php
600
							submit_button();
601
							?>
602
                            <p class="submit"><a href="https://fontawesome.com/referral?a=c9b89e1418" class="button button-secondary"><?php _e('Get 24,000+ more icons with Font Awesome Pro','font-awesome-settings'); ?> <i class="fas fa-external-link-alt"></i></a></p>
603
604
                        </div>
605
                    </form>
606
607
                    <div id="wpfas-version"><?php echo sprintf(__( 'Version: %s (affiliate links provided)', 'font-awesome-settings' ), $this->version ); ?></div>
608
                </div>
609
				<?php
610
			}
611
		}
612
613
		/**
614
		 * Check a version number is valid and if so return it or else return an empty string.
615
		 *
616
		 * @param $version string The version number to check.
617
		 *
618
		 * @since 1.0.6
619
		 *
620
		 * @return string Either a valid version number or an empty string.
621
		 */
622
		public function validate_version_number( $version ) {
623
624
			if ( version_compare( $version, '0.0.1', '>=' ) >= 0 ) {
625
				// valid
626
			} else {
627
				$version = '';// not validated
628
			}
629
630
			return $version;
631
		}
632
633
634
		/**
635
		 * Get the latest version of Font Awesome.
636
		 *
637
		 * We check for a cached version and if none we will check for a live version via API and then cache it for 48 hours.
638
		 *
639
		 * @since 1.0.7
640
		 * @return mixed|string The latest version number found.
641
		 */
642
		public function get_latest_version( $force_api = false ) {
643
			$latest_version = $this->latest;
644
645
			$cache = get_transient( 'wp-font-awesome-settings-version' );
646
647
			if ( $cache === false || $force_api ) { // its not set
648
				$api_ver = $this->get_latest_version_from_api();
649
				if ( version_compare( $api_ver, $this->latest, '>=' ) >= 0 ) {
650
					$latest_version = $api_ver;
651
					set_transient( 'wp-font-awesome-settings-version', $api_ver, 48 * HOUR_IN_SECONDS );
652
				}
653
			} elseif ( $this->validate_version_number( $cache ) ) {
654
				if ( version_compare( $cache, $this->latest, '>=' ) >= 0 ) {
655
					$latest_version = $cache;
656
				}
657
			}
658
659
			// Check and auto download fonts locally.
660
			if ( empty( $this->settings['pro'] ) && empty( $this->settings['version'] ) && $this->settings['type'] != 'KIT' && ! empty( $this->settings['local'] ) && ! empty( $this->settings['local_version'] ) && ! empty( $latest_version ) ) {
661
				if ( version_compare( $latest_version, $this->settings['local_version'], '>' ) && is_admin() && ! wp_doing_ajax() ) {
662
					$this->download_package( $latest_version );
663
				}
664
			}
665
666
			return $latest_version;
667
		}
668
669
		/**
670
		 * Get the latest Font Awesome version from the github API.
671
		 *
672
		 * @since 1.0.7
673
		 * @return string The latest version number or `0` on API fail.
674
		 */
675
		public function get_latest_version_from_api() {
676
			$version  = "0";
677
			$response = wp_remote_get( "https://api.github.com/repos/FortAwesome/Font-Awesome/releases/latest" );
678
			if ( ! is_wp_error( $response ) && is_array( $response ) ) {
679
				$api_response = json_decode( wp_remote_retrieve_body( $response ), true );
680
				if ( isset( $api_response['tag_name'] ) && version_compare( $api_response['tag_name'], $this->latest, '>=' ) >= 0 && empty( $api_response['prerelease'] ) ) {
681
					$version = $api_response['tag_name'];
682
				}
683
			}
684
685
			return $version;
686
		}
687
688
		/**
689
		 * Inline CSS for RTL language support.
690
		 *
691
		 * @since 1.0.13
692
		 * @return string Inline CSS.
693
		 */
694
		public function rtl_inline_css() {
695
			$inline_css = '[dir=rtl] .fa-address,[dir=rtl] .fa-address-card,[dir=rtl] .fa-adjust,[dir=rtl] .fa-alarm-clock,[dir=rtl] .fa-align-left,[dir=rtl] .fa-align-right,[dir=rtl] .fa-analytics,[dir=rtl] .fa-angle-double-left,[dir=rtl] .fa-angle-double-right,[dir=rtl] .fa-angle-left,[dir=rtl] .fa-angle-right,[dir=rtl] .fa-arrow-alt-circle-left,[dir=rtl] .fa-arrow-alt-circle-right,[dir=rtl] .fa-arrow-alt-from-left,[dir=rtl] .fa-arrow-alt-from-right,[dir=rtl] .fa-arrow-alt-left,[dir=rtl] .fa-arrow-alt-right,[dir=rtl] .fa-arrow-alt-square-left,[dir=rtl] .fa-arrow-alt-square-right,[dir=rtl] .fa-arrow-alt-to-left,[dir=rtl] .fa-arrow-alt-to-right,[dir=rtl] .fa-arrow-circle-left,[dir=rtl] .fa-arrow-circle-right,[dir=rtl] .fa-arrow-from-left,[dir=rtl] .fa-arrow-from-right,[dir=rtl] .fa-arrow-left,[dir=rtl] .fa-arrow-right,[dir=rtl] .fa-arrow-square-left,[dir=rtl] .fa-arrow-square-right,[dir=rtl] .fa-arrow-to-left,[dir=rtl] .fa-arrow-to-right,[dir=rtl] .fa-balance-scale-left,[dir=rtl] .fa-balance-scale-right,[dir=rtl] .fa-bed,[dir=rtl] .fa-bed-bunk,[dir=rtl] .fa-bed-empty,[dir=rtl] .fa-border-left,[dir=rtl] .fa-border-right,[dir=rtl] .fa-calendar-check,[dir=rtl] .fa-caret-circle-left,[dir=rtl] .fa-caret-circle-right,[dir=rtl] .fa-caret-left,[dir=rtl] .fa-caret-right,[dir=rtl] .fa-caret-square-left,[dir=rtl] .fa-caret-square-right,[dir=rtl] .fa-cart-arrow-down,[dir=rtl] .fa-cart-plus,[dir=rtl] .fa-chart-area,[dir=rtl] .fa-chart-bar,[dir=rtl] .fa-chart-line,[dir=rtl] .fa-chart-line-down,[dir=rtl] .fa-chart-network,[dir=rtl] .fa-chart-pie,[dir=rtl] .fa-chart-pie-alt,[dir=rtl] .fa-chart-scatter,[dir=rtl] .fa-check-circle,[dir=rtl] .fa-check-square,[dir=rtl] .fa-chevron-circle-left,[dir=rtl] .fa-chevron-circle-right,[dir=rtl] .fa-chevron-double-left,[dir=rtl] .fa-chevron-double-right,[dir=rtl] .fa-chevron-left,[dir=rtl] .fa-chevron-right,[dir=rtl] .fa-chevron-square-left,[dir=rtl] .fa-chevron-square-right,[dir=rtl] .fa-clock,[dir=rtl] .fa-file,[dir=rtl] .fa-file-alt,[dir=rtl] .fa-file-archive,[dir=rtl] .fa-file-audio,[dir=rtl] .fa-file-chart-line,[dir=rtl] .fa-file-chart-pie,[dir=rtl] .fa-file-code,[dir=rtl] .fa-file-excel,[dir=rtl] .fa-file-image,[dir=rtl] .fa-file-pdf,[dir=rtl] .fa-file-powerpoint,[dir=rtl] .fa-file-video,[dir=rtl] .fa-file-word,[dir=rtl] .fa-flag,[dir=rtl] .fa-folder,[dir=rtl] .fa-folder-open,[dir=rtl] .fa-hand-lizard,[dir=rtl] .fa-hand-point-down,[dir=rtl] .fa-hand-point-left,[dir=rtl] .fa-hand-point-right,[dir=rtl] .fa-hand-point-up,[dir=rtl] .fa-hand-scissors,[dir=rtl] .fa-image,[dir=rtl] .fa-long-arrow-alt-left,[dir=rtl] .fa-long-arrow-alt-right,[dir=rtl] .fa-long-arrow-left,[dir=rtl] .fa-long-arrow-right,[dir=rtl] .fa-luggage-cart,[dir=rtl] .fa-moon,[dir=rtl] .fa-pencil,[dir=rtl] .fa-pencil-alt,[dir=rtl] .fa-play-circle,[dir=rtl] .fa-project-diagram,[dir=rtl] .fa-quote-left,[dir=rtl] .fa-quote-right,[dir=rtl] .fa-shopping-cart,[dir=rtl] .fa-thumbs-down,[dir=rtl] .fa-thumbs-up,[dir=rtl] .fa-user-chart{filter: progid:DXImageTransform.Microsoft.BasicImage(rotation=0, mirror=1);transform:scale(-1,1)}[dir=rtl] .fa-spin{animation-direction:reverse}';
696
697
			return $inline_css;
698
		}
699
700
		/**
701
		 * Show any warnings as an admin notice.
702
		 *
703
		 * @return void
704
		 */
705
		public function admin_notices() {
706
			$settings = $this->settings;
707
708
			if ( defined( 'FONTAWESOME_PLUGIN_FILE' ) ) {
709
				if ( ! empty( $_REQUEST['page'] ) && $_REQUEST['page'] == 'wp-font-awesome-settings' ) {
710
					?>
711
                    <div class="notice  notice-error is-dismissible">
712
                        <p><?php _e( 'The Official Font Awesome Plugin is active, please adjust your settings there.', 'font-awesome-settings' ); ?></p>
713
                    </div>
714
					<?php
715
				}
716
			} else {
717
				if ( ! empty( $settings ) ) {
718
					if ( $settings['type'] != 'KIT' && $settings['pro'] && ( $settings['version'] == '' || version_compare( $settings['version'], '6', '>=' ) ) ) {
719
						$link = admin_url('options-general.php?page=wp-font-awesome-settings');
720
						?>
721
                        <div class="notice  notice-error is-dismissible">
722
                            <p><?php echo sprintf( __( 'Font Awesome Pro v6 requires the use of a kit, please setup your kit in %ssettings.%s', 'font-awesome-settings' ),"<a href='". esc_url_raw( $link )."'>","</a>" ); ?></p>
723
                        </div>
724
						<?php
725
					}
726
				}
727
			}
728
		}
729
730
		/**
731
		 * Handle fontawesome add settings to download fontawesome to store locally.
732
		 *
733
		 * @since 1.1.1
734
		 *
735
		 * @param string $option The option name.
736
		 * @param mixed  $value  The option value.
737
		 */
738
		public function add_option_wp_font_awesome_settings( $option, $value ) {
739
			// Do nothing if WordPress is being installed.
740
			if ( wp_installing() ) {
741
				return;
742
			}
743
744
			if ( ! empty( $value['local'] ) && empty( $value['pro'] ) && ! ( ! empty( $value['type'] ) && $value['type'] == 'KIT' ) ) {
745
				$version = isset( $value['version'] ) && $value['version'] ? $value['version'] : $this->get_latest_version();
746
747
				if ( ! empty( $version ) ) {
748
					$response = $this->download_package( $version, $value );
749
750
					if ( is_wp_error( $response ) ) {
751
						add_settings_error( 'general', 'fontawesome_download', __( 'ERROR:', 'font-awesome-settings' ) . ' ' . $response->get_error_message(), 'error' );
752
					}
753
				}
754
			}
755
		}
756
757
		/**
758
		 * Handle fontawesome update settings to download fontawesome to store locally.
759
		 *
760
		 * @since 1.1.0
761
		 *
762
		 * @param mixed $old_value The old option value.
763
		 * @param mixed $value     The new option value.
764
		 */
765
		public function update_option_wp_font_awesome_settings( $old_value, $new_value ) {
766
			// Do nothing if WordPress is being installed.
767
			if ( wp_installing() ) {
768
				return;
769
			}
770
771
			if ( ! empty( $new_value['local'] ) && empty( $new_value['pro'] ) && ! ( ! empty( $new_value['type'] ) && $new_value['type'] == 'KIT' ) ) {
772
				// Old values
773
				$old_version = isset( $old_value['version'] ) && $old_value['version'] ? $old_value['version'] : ( isset( $old_value['local_version'] ) ? $old_value['local_version'] : '' );
774
				$old_local = isset( $old_value['local'] ) ? (int) $old_value['local'] : 0;
775
776
				// New values
777
				$new_version = isset( $new_value['version'] ) && $new_value['version'] ? $new_value['version'] : $this->get_latest_version();
778
779
				if ( empty( $old_local ) || $old_version !== $new_version || ! file_exists( $this->get_fonts_dir() . 'css' . DIRECTORY_SEPARATOR . 'all.css' ) ) {
780
					$response = $this->download_package( $new_version, $new_value );
781
782
					if ( is_wp_error( $response ) ) {
783
						add_settings_error( 'general', 'fontawesome_download', __( 'ERROR:', 'font-awesome-settings' ) . ' ' . $response->get_error_message(), 'error' );
784
					}
785
				}
786
			}
787
		}
788
789
		/**
790
		 * Get the fonts directory local path.
791
		 *
792
		 * @since 1.1.0
793
		 *
794
		 * @param string Fonts directory local path.
0 ignored issues
show
Bug introduced by
The type Fonts was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
795
		 */
796
		public function get_fonts_dir() {
797
			$upload_dir = wp_upload_dir( null, false );
798
799
			return $upload_dir['basedir'] . DIRECTORY_SEPARATOR .  'ayefonts' . DIRECTORY_SEPARATOR . 'fa' . DIRECTORY_SEPARATOR;
800
		}
801
802
		/**
803
		 * Get the fonts directory local url.
804
		 *
805
		 * @since 1.1.0
806
		 *
807
		 * @param string Fonts directory local url.
808
		 */
809
		public function get_fonts_url() {
810
			$upload_dir = wp_upload_dir( null, false );
811
812
			return $upload_dir['baseurl'] .  '/ayefonts/fa/';
813
		}
814
815
		/**
816
		 * Check whether load locally active.
817
		 *
818
		 * @since 1.1.0
819
		 *
820
		 * @return bool True if active else false.
821
		 */
822
		public function has_local() {
823
			if ( ! empty( $this->settings['local'] ) && empty( $this->settings['pro'] ) && file_exists( $this->get_fonts_dir() . 'css' . DIRECTORY_SEPARATOR . 'all.css' ) ) {
824
				return true;
825
			}
826
827
			return false;
828
		}
829
830
		/**
831
		 * Get the WP Filesystem access.
832
		 *
833
		 * @since 1.1.0
834
		 *
835
		 * @return object The WP Filesystem.
836
		 */
837
		public function get_wp_filesystem() {
838
			if ( ! function_exists( 'get_filesystem_method' ) ) {
839
				require_once( ABSPATH . "/wp-admin/includes/file.php" );
840
			}
841
842
			$access_type = get_filesystem_method();
843
844
			if ( $access_type === 'direct' ) {
845
				/* You can safely run request_filesystem_credentials() without any issues and don't need to worry about passing in a URL */
846
				$creds = request_filesystem_credentials( trailingslashit( site_url() ) . 'wp-admin/', '', false, false, array() );
0 ignored issues
show
Bug introduced by
false of type false is incompatible with the type string expected by parameter $context of request_filesystem_credentials(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

846
				$creds = request_filesystem_credentials( trailingslashit( site_url() ) . 'wp-admin/', '', false, /** @scrutinizer ignore-type */ false, array() );
Loading history...
847
848
				/* Initialize the API */
849
				if ( ! WP_Filesystem( $creds ) ) {
850
					/* Any problems and we exit */
851
					return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type object.
Loading history...
852
				}
853
854
				global $wp_filesystem;
855
856
				return $wp_filesystem;
857
				/* Do our file manipulations below */
858
			} else if ( defined( 'FTP_USER' ) ) {
859
				$creds = request_filesystem_credentials( trailingslashit( site_url() ) . 'wp-admin/', '', false, false, array() );
860
861
				/* Initialize the API */
862
				if ( ! WP_Filesystem( $creds ) ) {
863
					/* Any problems and we exit */
864
					return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type object.
Loading history...
865
				}
866
867
				global $wp_filesystem;
868
869
				return $wp_filesystem;
870
			} else {
871
				/* Don't have direct write access. Prompt user with our notice */
872
				return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type object.
Loading history...
873
			}
874
		}
875
876
		/**
877
		 * Download the fontawesome package file.
878
		 *
879
		 * @since 1.1.0
880
		 *
881
		 * @param mixed $version The font awesome.
882
		 * @param array $option Fontawesome settings.
883
		 * @return WP_ERROR|bool Error on fail and true on success.
884
		 */
885
		public function download_package( $version, $option = array() ) {
886
			$filename = 'fontawesome-free-' . $version . '-web';
887
			$url = 'https://use.fontawesome.com/releases/v' . $version . '/' . $filename . '.zip';
888
889
			if ( ! function_exists( 'wp_handle_upload' ) ) {
890
				require_once ABSPATH . 'wp-admin/includes/file.php';
891
			}
892
893
			$download_file = download_url( esc_url_raw( $url ) );
894
895
			if ( is_wp_error( $download_file ) ) {
896
				return new WP_Error( 'fontawesome_download_failed', __( $download_file->get_error_message(), 'font-awesome-settings' ) );
897
			} else if ( empty( $download_file ) ) {
898
				return new WP_Error( 'fontawesome_download_failed', __( 'Something went wrong in downloading the font awesome to store locally.', 'font-awesome-settings' ) );
899
			}
900
901
			$response = $this->extract_package( $download_file, $filename, true );
0 ignored issues
show
Bug introduced by
$download_file of type WP_Error is incompatible with the type string expected by parameter $package of WP_Font_Awesome_Settings::extract_package(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

901
			$response = $this->extract_package( /** @scrutinizer ignore-type */ $download_file, $filename, true );
Loading history...
902
903
			// Update local version.
904
			if ( is_wp_error( $response ) ) {
905
				return $response;
906
			} else if ( $response ) {
907
				if ( empty( $option ) ) {
908
					$option = get_option( 'wp-font-awesome-settings' );
909
				}
910
911
				$option['local_version'] = $version;
912
913
				// Remove action to prevent looping.
914
				remove_action( 'update_option_wp-font-awesome-settings', array( $this, 'update_option_wp_font_awesome_settings' ), 10, 2 );
0 ignored issues
show
Unused Code introduced by
The call to remove_action() has too many arguments starting with 2. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

914
				/** @scrutinizer ignore-call */ 
915
    remove_action( 'update_option_wp-font-awesome-settings', array( $this, 'update_option_wp_font_awesome_settings' ), 10, 2 );

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
915
916
				update_option( 'wp-font-awesome-settings', $option );
917
918
				return true;
919
			}
920
921
			return false;
922
		}
923
924
		/**
925
		 * Extract the fontawesome package file.
926
		 *
927
		 * @since 1.1.0
928
		 *
929
		 * @param string $package The package file path.
930
		 * @param string $dirname Package file name.
931
		 * @param bool   $delete_package Delete temp file or not.
932
		 * @return WP_Error|bool True on success WP_Error on fail.
933
		 */
934
		public function extract_package( $package, $dirname = '', $delete_package = false ) {
935
			global $wp_filesystem;
936
937
			$wp_filesystem = $this->get_wp_filesystem();
938
939
			if ( empty( $wp_filesystem ) && isset( $wp_filesystem->errors ) && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->get_error_code() ) {
940
				return new WP_Error( 'fontawesome_filesystem_error', __( $wp_filesystem->errors->get_error_message(), 'font-awesome-settings' ) );
941
			} else if ( empty( $wp_filesystem ) ) {
942
				return new WP_Error( 'fontawesome_filesystem_error', __( 'Failed to initialise WP_Filesystem while trying to download the Font Awesome package.', 'font-awesome-settings' ) );
943
			}
944
945
			$fonts_dir = $this->get_fonts_dir();
946
			$fonts_tmp_dir = dirname( $fonts_dir ) . DIRECTORY_SEPARATOR . 'fa-tmp' . DIRECTORY_SEPARATOR;
947
948
			if ( $wp_filesystem->is_dir( $fonts_tmp_dir ) ) {
949
				$wp_filesystem->delete( $fonts_tmp_dir, true );
950
			}
951
952
			// Unzip package to working directory.
953
			$result = unzip_file( $package, $fonts_tmp_dir );
954
955
			if ( is_wp_error( $result ) ) {
956
				$wp_filesystem->delete( $fonts_tmp_dir, true );
957
958
				if ( 'incompatible_archive' === $result->get_error_code() ) {
959
					return new WP_Error( 'fontawesome_incompatible_archive', __( $result->get_error_message(), 'font-awesome-settings' ) );
960
				}
961
962
				return $result;
963
			}
964
965
			if ( $wp_filesystem->is_dir( $fonts_dir ) ) {
966
				$wp_filesystem->delete( $fonts_dir, true );
967
			}
968
969
			$extract_dir = $fonts_tmp_dir;
970
971
			if ( $dirname && $wp_filesystem->is_dir( $extract_dir . $dirname . DIRECTORY_SEPARATOR ) ) {
972
				$extract_dir .= $dirname . DIRECTORY_SEPARATOR;
973
			}
974
975
			try {
976
				$return = $wp_filesystem->move( $extract_dir, $fonts_dir, true );
977
			} catch ( Exception $e ) {
978
				$return = new WP_Error( 'fontawesome_move_package', __( 'Fail to move font awesome package!', 'font-awesome-settings' ) );
979
			}
980
981
			if ( $wp_filesystem->is_dir( $fonts_tmp_dir ) ) {
982
				$wp_filesystem->delete( $fonts_tmp_dir, true );
983
			}
984
985
			// Once extracted, delete the package if required.
986
			if ( $delete_package ) {
987
				unlink( $package );
988
			}
989
990
			return $return;
991
		}
992
	}
993
994
	/**
995
	 * Run the class if found.
996
	 */
997
	WP_Font_Awesome_Settings::instance();
998
}
999