Passed
Push — master ( b5d6c7...020c1f )
by Stiofan
06:03
created

WP_Font_Awesome_Settings::add_generator()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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

211
		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...
212
213
			if ( ! empty( $editor_settings['__unstableResolvedAssets']['styles'] ) ) {
214
				$url = $this->get_url();
215
				$editor_settings['__unstableResolvedAssets']['styles'] .= "<link rel='stylesheet' id='font-awesome-css'  href='$url' media='all' />";
216
			}
217
218
			return $editor_settings;
219
		}
220
221
		/**
222
		 * Add FA to the FSE.
223
		 *
224
		 * @param $editor_settings
225
		 * @param $block_editor_context
226
		 *
227
		 * @return array
228
		 */
229
		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

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

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

836
				$creds = request_filesystem_credentials( trailingslashit( site_url() ) . 'wp-admin/', '', false, /** @scrutinizer ignore-type */ false, array() );
Loading history...
837
838
				/* Initialize the API */
839
				if ( ! WP_Filesystem( $creds ) ) {
840
					/* Any problems and we exit */
841
					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...
842
				}
843
844
				global $wp_filesystem;
845
846
				return $wp_filesystem;
847
				/* Do our file manipulations below */
848
			} else if ( defined( 'FTP_USER' ) ) {
849
				$creds = request_filesystem_credentials( trailingslashit( site_url() ) . 'wp-admin/', '', false, false, array() );
850
851
				/* Initialize the API */
852
				if ( ! WP_Filesystem( $creds ) ) {
853
					/* Any problems and we exit */
854
					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...
855
				}
856
857
				global $wp_filesystem;
858
859
				return $wp_filesystem;
860
			} else {
861
				/* Don't have direct write access. Prompt user with our notice */
862
				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...
863
			}
864
		}
865
866
		/**
867
		 * Download the fontawesome package file.
868
		 *
869
		 * @since 1.1.0
870
		 *
871
		 * @param mixed $version The font awesome.
872
		 * @param array $option Fontawesome settings.
873
		 * @return WP_ERROR|bool Error on fail and true on success.
874
		 */
875
		public function download_package( $version, $option = array() ) {
876
			$filename = 'fontawesome-free-' . $version . '-web';
877
			$url = 'https://use.fontawesome.com/releases/v' . $version . '/' . $filename . '.zip';
878
879
			if ( ! function_exists( 'wp_handle_upload' ) ) {
880
				require_once ABSPATH . 'wp-admin/includes/file.php';
881
			}
882
883
			$download_file = download_url( esc_url_raw( $url ) );
884
885
			if ( is_wp_error( $download_file ) ) {
886
				return new WP_Error( 'fontawesome_download_failed', __( $download_file->get_error_message(), 'ayecode-connect' ) );
887
			} else if ( empty( $download_file ) ) {
888
				return new WP_Error( 'fontawesome_download_failed', __( 'Something went wrong in downloading the font awesome to store locally.', 'ayecode-connect' ) );
889
			}
890
891
			$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

891
			$response = $this->extract_package( /** @scrutinizer ignore-type */ $download_file, $filename, true );
Loading history...
892
893
			// Update local version.
894
			if ( is_wp_error( $response ) ) {
895
				return $response;
896
			} else if ( $response ) {
897
				if ( empty( $option ) ) {
898
					$option = get_option( 'wp-font-awesome-settings' );
899
				}
900
901
				$option['local_version'] = $version;
902
903
				// Remove action to prevent looping.
904
				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

904
				/** @scrutinizer ignore-call */ 
905
    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...
905
906
				update_option( 'wp-font-awesome-settings', $option );
907
908
				return true;
909
			}
910
911
			return false;
912
		}
913
914
		/**
915
		 * Extract the fontawesome package file.
916
		 *
917
		 * @since 1.1.0
918
		 *
919
		 * @param string $package The package file path.
920
		 * @param string $dirname Package file name.
921
		 * @param bool   $delete_package Delete temp file or not.
922
		 * @return WP_Error|bool True on success WP_Error on fail.
923
		 */
924
		public function extract_package( $package, $dirname = '', $delete_package = false ) {
925
			global $wp_filesystem;
926
927
			$wp_filesystem = $this->get_wp_filesystem();
928
929
			if ( empty( $wp_filesystem ) && isset( $wp_filesystem->errors ) && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->get_error_code() ) {
930
				return new WP_Error( 'fontawesome_filesystem_error', __( $wp_filesystem->errors->get_error_message(), 'ayecode-connect' ) );
931
			} else if ( empty( $wp_filesystem ) ) {
932
				return new WP_Error( 'fontawesome_filesystem_error', __( 'Failed to initialise WP_Filesystem while trying to download the Font Awesome package.', 'ayecode-connect' ) );
933
			}
934
935
			$fonts_dir = $this->get_fonts_dir();
936
			$fonts_tmp_dir = dirname( $fonts_dir ) . DIRECTORY_SEPARATOR . 'fa-tmp' . DIRECTORY_SEPARATOR;
937
938
			if ( $wp_filesystem->is_dir( $fonts_tmp_dir ) ) {
939
				$wp_filesystem->delete( $fonts_tmp_dir, true );
940
			}
941
942
			// Unzip package to working directory.
943
			$result = unzip_file( $package, $fonts_tmp_dir );
944
945
			if ( is_wp_error( $result ) ) {
946
				$wp_filesystem->delete( $fonts_tmp_dir, true );
947
948
				if ( 'incompatible_archive' === $result->get_error_code() ) {
949
					return new WP_Error( 'fontawesome_incompatible_archive', __( $result->get_error_message(), 'ayecode-connect' ) );
950
				}
951
952
				return $result;
953
			}
954
955
			if ( $wp_filesystem->is_dir( $fonts_dir ) ) {
956
				$wp_filesystem->delete( $fonts_dir, true );
957
			}
958
959
			$extract_dir = $fonts_tmp_dir;
960
961
			if ( $dirname && $wp_filesystem->is_dir( $extract_dir . $dirname . DIRECTORY_SEPARATOR ) ) {
962
				$extract_dir .= $dirname . DIRECTORY_SEPARATOR;
963
			}
964
965
			try {
966
				$return = $wp_filesystem->move( $extract_dir, $fonts_dir, true );
967
			} catch ( Exception $e ) {
968
				$return = new WP_Error( 'fontawesome_move_package', __( 'Fail to move font awesome package!', 'ayecode-connect' ) );
969
			}
970
971
			if ( $wp_filesystem->is_dir( $fonts_tmp_dir ) ) {
972
				$wp_filesystem->delete( $fonts_tmp_dir, true );
973
			}
974
975
			// Once extracted, delete the package if required.
976
			if ( $delete_package ) {
977
				unlink( $package );
978
			}
979
980
			return $return;
981
		}
982
983
		/**
984
		 * Output the version in the header.
985
		 */
986
		public function add_generator() {
987
			$file = str_replace( array( "/", "\\" ), "/", realpath( __FILE__ ) );
988
			$plugins_dir = str_replace( array( "/", "\\" ), "/", realpath( WP_PLUGIN_DIR ) );
989
990
			// Find source plugin/theme.
991
			$source = array();
992
			if ( strpos( $file, $plugins_dir ) !== false ) {
993
				$source = explode( "/", plugin_basename( $file ) );
994
			} else if ( function_exists( 'get_theme_root' ) ) {
995
				$themes_dir = str_replace( array( "/", "\\" ), "/", realpath( get_theme_root() ) );
996
997
				if ( strpos( $file, $themes_dir ) !== false ) {
998
					$source = explode( "/", ltrim( str_replace( $themes_dir, "", $file ), "/" ) );
999
				}
1000
			}
1001
1002
			echo '<meta name="generator" content="WP Font Awesome Settings v' . esc_attr( $this->version ) . '"' . ( ! empty( $source[0] ) ? ' data-ac-source="' . esc_attr( $source[0] ) . '"' : '' ) . ' />';
1003
		}
1004
	}
1005
1006
	/**
1007
	 * Run the class if found.
1008
	 */
1009
	WP_Font_Awesome_Settings::instance();
1010
}
1011