Passed
Push — master ( 6a5a36...97fcb2 )
by Brian
04:18
created

WP_Font_Awesome_Settings::enqueue_editor_styles()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 8
rs 10
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 teh backend.
34
	 * @ver 1.0.15
35
	 * @todo decide how to implement textdomain
36
	 */
37
	class WP_Font_Awesome_Settings {
38
39
		/**
40
		 * Class version version.
41
		 *
42
		 * @var string
43
		 */
44
		public $version = '1.0.15';
45
46
		/**
47
		 * Class textdomain.
48
		 *
49
		 * @var string
50
		 */
51
		public $textdomain = 'font-awesome-settings';
52
53
		/**
54
		 * Latest version of Font Awesome at time of publish published.
55
		 *
56
		 * @var string
57
		 */
58
		public $latest = "5.8.2";
59
60
		/**
61
		 * The title.
62
		 *
63
		 * @var string
64
		 */
65
		public $name = 'Font Awesome';
66
67
		/**
68
		 * Holds the settings values.
69
		 *
70
		 * @var array
71
		 */
72
		private $settings;
73
74
		/**
75
		 * WP_Font_Awesome_Settings instance.
76
		 *
77
		 * @access private
78
		 * @since  1.0.0
79
		 * @var    WP_Font_Awesome_Settings There can be only one!
80
		 */
81
		private static $instance = null;
82
83
		/**
84
		 * Main WP_Font_Awesome_Settings Instance.
85
		 *
86
		 * Ensures only one instance of WP_Font_Awesome_Settings is loaded or can be loaded.
87
		 *
88
		 * @since 1.0.0
89
		 * @static
90
		 * @return WP_Font_Awesome_Settings - Main instance.
91
		 */
92
		public static function instance() {
93
			if ( ! isset( self::$instance ) && ! ( self::$instance instanceof WP_Font_Awesome_Settings ) ) {
94
				self::$instance = new WP_Font_Awesome_Settings;
95
96
				add_action( 'init', array( self::$instance, 'init' ) ); // set settings
97
98
				if ( is_admin() ) {
99
					add_action( 'admin_menu', array( self::$instance, 'menu_item' ) );
100
					add_action( 'admin_init', array( self::$instance, 'register_settings' ) );
101
					add_action( 'admin_notices', array( self::$instance, 'admin_notices' ) );
102
				}
103
104
				do_action( 'wp_font_awesome_settings_loaded' );
105
			}
106
107
			return self::$instance;
108
		}
109
110
		/**
111
		 * Initiate the settings and add the required action hooks.
112
		 *
113
		 * @since 1.0.8 Settings name wrong - FIXED
114
		 */
115
		public function init() {
116
			$this->settings = $this->get_settings();
117
118
			// check if the official plugin is active and use that instead if so.
119
			if ( ! defined( 'FONTAWESOME_PLUGIN_FILE' ) ) {
120
121
				if ( $this->settings['type'] == 'CSS' ) {
122
123
					if ( $this->settings['enqueue'] == '' || $this->settings['enqueue'] == 'frontend' ) {
124
						add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_style' ), 5000 );
125
					}
126
127
					if ( $this->settings['enqueue'] == '' || $this->settings['enqueue'] == 'backend' ) {
128
						add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_style' ), 5000 );
129
						add_filter( 'block_editor_settings_all', array( $this, 'enqueue_editor_styles' ), 10, 2 );
130
					}
131
132
				} else {
133
134
					if ( $this->settings['enqueue'] == '' || $this->settings['enqueue'] == 'frontend' ) {
135
						add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ), 5000 );
136
					}
137
138
					if ( $this->settings['enqueue'] == '' || $this->settings['enqueue'] == 'backend' ) {
139
						add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ), 5000 );
140
						add_filter( 'block_editor_settings_all', array( $this, 'enqueue_editor_scripts' ), 10, 2 );
141
					}
142
				}
143
144
				// remove font awesome if set to do so
145
				if ( $this->settings['dequeue'] == '1' ) {
146
					add_action( 'clean_url', array( $this, 'remove_font_awesome' ), 5000, 3 );
147
				}
148
			}
149
150
		}
151
152
		/**
153
		 * Add FA to the FSE.
154
		 *
155
		 * @param $editor_settings
156
		 * @param $block_editor_context
157
		 *
158
		 * @return array
159
		 */
160
		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

160
		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...
161
162
			if ( ! empty( $editor_settings['__unstableResolvedAssets']['styles'] ) ) {
163
				$url = $this->get_url();
164
				$editor_settings['__unstableResolvedAssets']['styles'] .= "<link rel='stylesheet' id='font-awesome-css'  href='$url' media='all' />";
165
			}
166
167
			return $editor_settings;
168
		}
169
170
		/**
171
		 * Add FA to the FSE.
172
		 *
173
		 * @param $editor_settings
174
		 * @param $block_editor_context
175
		 *
176
		 * @return array
177
		 */
178
		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

178
		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...
179
180
			$url = $this->get_url();
181
			$editor_settings['__unstableResolvedAssets']['scripts'] .= "<script src='$url' id='font-awesome-js'></script>";
182
183
			return $editor_settings;
184
		}
185
186
		/**
187
		 * Adds the Font Awesome styles.
188
		 */
189
		public function enqueue_style() {
190
			// build url
191
			$url = $this->get_url();
192
193
			wp_deregister_style( 'font-awesome' ); // deregister in case its already there
194
			wp_register_style( 'font-awesome', $url, array(), null );
195
			wp_enqueue_style( 'font-awesome' );
196
197
			// RTL language support CSS.
198
			if ( is_rtl() ) {
199
				wp_add_inline_style( 'font-awesome', $this->rtl_inline_css() );
200
			}
201
202
			if ( $this->settings['shims'] ) {
203
				$url = $this->get_url( true );
204
				wp_deregister_style( 'font-awesome-shims' ); // deregister in case its already there
205
				wp_register_style( 'font-awesome-shims', $url, array(), null );
206
				wp_enqueue_style( 'font-awesome-shims' );
207
			}
208
		}
209
210
		/**
211
		 * Adds the Font Awesome JS.
212
		 */
213
		public function enqueue_scripts() {
214
			// build url
215
			$url = $this->get_url();
216
217
			$deregister_function = 'wp' . '_' . 'deregister' . '_' . 'script';
218
			call_user_func( $deregister_function, 'font-awesome' ); // deregister in case its already there
219
			wp_register_script( 'font-awesome', $url, array(), null );
220
			wp_enqueue_script( 'font-awesome' );
221
222
			if ( $this->settings['shims'] ) {
223
				$url = $this->get_url( true );
224
				call_user_func( $deregister_function, 'font-awesome-shims' ); // deregister in case its already there
225
				wp_register_script( 'font-awesome-shims', $url, array(), null );
226
				wp_enqueue_script( 'font-awesome-shims' );
227
			}
228
		}
229
230
		/**
231
		 * Get the url of the Font Awesome files.
232
		 *
233
		 * @param bool $shims If this is a shim file or not.
234
		 *
235
		 * @return string The url to the file.
236
		 */
237
		public function get_url( $shims = false ) {
238
			$script  = $shims ? 'v4-shims' : 'all';
239
			$sub     = $this->settings['pro'] ? 'pro' : 'use';
240
			$type    = $this->settings['type'];
241
			$version = $this->settings['version'];
242
			$kit_url = $this->settings['kit-url'] ? esc_url( $this->settings['kit-url'] ) : '';
243
			$url     = '';
244
245
			if ( $type == 'KIT' && $kit_url ) {
246
				if ( $shims ) {
247
					// if its a kit then we don't add shims here
248
					return '';
249
				}
250
				$url .= $kit_url; // CDN
251
				$url .= "?wpfas=true"; // set our var so our version is not removed
252
			} else {
253
				$url .= "https://$sub.fontawesome.com/releases/"; // CDN
254
				$url .= ! empty( $version ) ? "v" . $version . '/' : "v" . $this->get_latest_version() . '/'; // version
255
				$url .= $type == 'CSS' ? 'css/' : 'js/'; // type
256
				$url .= $type == 'CSS' ? $script . '.css' : $script . '.js'; // type
257
				$url .= "?wpfas=true"; // set our var so our version is not removed
258
			}
259
260
			return $url;
261
		}
262
263
		/**
264
		 * Try and remove any other versions of Font Awesome added by other plugins/themes.
265
		 *
266
		 * 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.
267
		 *
268
		 * @param $url
269
		 * @param $original_url
270
		 * @param $_context
271
		 *
272
		 * @return string The filtered url.
273
		 */
274
		public function remove_font_awesome( $url, $original_url, $_context ) {
275
276
			if ( $_context == 'display'
277
			     && ( strstr( $url, "fontawesome" ) !== false || strstr( $url, "font-awesome" ) !== false )
278
			     && ( strstr( $url, ".js" ) !== false || strstr( $url, ".css" ) !== false )
279
			) {// it's a font-awesome-url (probably)
280
281
				if ( strstr( $url, "wpfas=true" ) !== false ) {
282
					if ( $this->settings['type'] == 'JS' ) {
283
						if ( $this->settings['js-pseudo'] ) {
284
							$url .= "' data-search-pseudo-elements defer='defer";
285
						} else {
286
							$url .= "' defer='defer";
287
						}
288
					}
289
				} else {
290
					$url = ''; // removing the url removes the file
291
				}
292
293
			}
294
295
			return $url;
296
		}
297
298
		/**
299
		 * Register the database settings with WordPress.
300
		 */
301
		public function register_settings() {
302
			register_setting( 'wp-font-awesome-settings', 'wp-font-awesome-settings' );
303
		}
304
305
		/**
306
		 * Add the WordPress settings menu item.
307
		 * @since 1.0.10 Calling function name direct will fail theme check so we don't.
308
		 */
309
		public function menu_item() {
310
			$menu_function = 'add' . '_' . 'options' . '_' . 'page'; // won't pass theme check if function name present in theme
311
			call_user_func( $menu_function, $this->name, $this->name, 'manage_options', 'wp-font-awesome-settings', array(
312
				$this,
313
				'settings_page'
314
			) );
315
		}
316
317
		/**
318
		 * Get the current Font Awesome output settings.
319
		 *
320
		 * @return array The array of settings.
321
		 */
322
		public function get_settings() {
323
324
			$db_settings = get_option( 'wp-font-awesome-settings' );
325
326
			$defaults = array(
327
				'type'      => 'CSS', // type to use, CSS or JS or KIT
328
				'version'   => '', // latest
329
				'enqueue'   => '', // front and backend
330
				'shims'     => '0', // default OFF now in 2020
331
				'js-pseudo' => '0', // if the pseudo elements flag should be set (CPU intensive)
332
				'dequeue'   => '0', // if we should try to remove other versions added by other plugins/themes
333
				'pro'       => '0', // if pro CDN url should be used
334
				'kit-url'   => '', // the kit url
335
			);
336
337
			$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

337
			$settings = wp_parse_args( /** @scrutinizer ignore-type */ $db_settings, $defaults );
Loading history...
338
339
			/**
340
			 * Filter the Font Awesome settings.
341
			 *
342
			 * @todo if we add this filer people might use it and then it defeates the purpose of this class :/
343
			 */
344
			return $this->settings = apply_filters( 'wp-font-awesome-settings', $settings, $db_settings, $defaults );
345
		}
346
347
348
		/**
349
		 * The settings page html output.
350
		 */
351
		public function settings_page() {
352
			if ( ! current_user_can( 'manage_options' ) ) {
353
				wp_die( __( 'You do not have sufficient permissions to access this page.', 'font-awesome-settings' ) );
354
			}
355
356
			// a hidden way to force the update of the version number via api instead of waiting the 48 hours
357
			if ( isset( $_REQUEST['force-version-check'] ) ) {
358
				$this->get_latest_version( $force_api = true );
359
			}
360
361
			if ( ! defined( 'FONTAWESOME_PLUGIN_FILE' ) ) {
362
				?>
363
                <style>
364
                    .wpfas-kit-show {
365
                        display: none;
366
                    }
367
368
                    .wpfas-kit-set .wpfas-kit-hide {
369
                        display: none;
370
                    }
371
372
                    .wpfas-kit-set .wpfas-kit-show {
373
                        display: table-row;
374
                    }
375
                </style>
376
                <div class="wrap">
377
                    <h1><?php echo $this->name; ?></h1>
378
                    <form method="post" action="options.php" class="fas-settings-form">
379
						<?php
380
						settings_fields( 'wp-font-awesome-settings' );
381
						do_settings_sections( 'wp-font-awesome-settings' );
382
						$kit_set = $this->settings['type'] == 'KIT' ? 'wpfas-kit-set' : '';
383
						?>
384
                        <table class="form-table wpfas-table-settings <?php echo esc_attr( $kit_set ); ?>">
385
                            <tr valign="top">
386
                                <th scope="row"><label
387
                                            for="wpfas-type"><?php _e( 'Type', 'font-awesome-settings' ); ?></label></th>
388
                                <td>
389
                                    <select name="wp-font-awesome-settings[type]" id="wpfas-type"
390
                                            onchange="if(this.value=='KIT'){jQuery('.wpfas-table-settings').addClass('wpfas-kit-set');}else{jQuery('.wpfas-table-settings').removeClass('wpfas-kit-set');}">
391
                                        <option
392
                                                value="CSS" <?php selected( $this->settings['type'], 'CSS' ); ?>><?php _e( 'CSS (default)', 'font-awesome-settings' ); ?></option>
393
                                        <option value="JS" <?php selected( $this->settings['type'], 'JS' ); ?>>JS</option>
394
                                        <option
395
                                                value="KIT" <?php selected( $this->settings['type'], 'KIT' ); ?>><?php _e( 'Kits (settings managed on fontawesome.com)', 'font-awesome-settings' ); ?></option>
396
                                    </select>
397
                                </td>
398
                            </tr>
399
400
                            <tr valign="top" class="wpfas-kit-show">
401
                                <th scope="row"><label
402
                                            for="wpfas-kit-url"><?php _e( 'Kit URL', 'font-awesome-settings' ); ?></label></th>
403
                                <td>
404
                                    <input class="regular-text" id="wpfas-kit-url" type="url"
405
                                           name="wp-font-awesome-settings[kit-url]"
406
                                           value="<?php echo esc_attr( $this->settings['kit-url'] ); ?>"
407
                                           placeholder="<?php echo 'https://kit.font';echo 'awesome.com/123abc.js'; // this won't pass theme check :(?>"/>
408
                                    <span><?php
409
										echo sprintf(
410
											__( 'Requires a free account with Font Awesome. %sGet kit url%s', 'font-awesome-settings' ),
411
											'<a rel="noopener noreferrer" target="_blank" href="https://fontawesome.com/kits"><i class="fas fa-external-link-alt"></i>',
412
											'</a>'
413
										);
414
										?></span>
415
                                </td>
416
                            </tr>
417
418
                            <tr valign="top" class="wpfas-kit-hide">
419
                                <th scope="row"><label
420
                                            for="wpfas-version"><?php _e( 'Version', 'font-awesome-settings' ); ?></label></th>
421
                                <td>
422
                                    <select name="wp-font-awesome-settings[version]" id="wpfas-version">
423
                                        <option
424
                                                value="" <?php selected( $this->settings['version'], '' ); ?>><?php echo sprintf( __( 'Latest - %s (default)', 'font-awesome-settings' ), $this->get_latest_version() ); ?>
425
                                        </option>
426
                                        <option value="6.1.0" <?php selected( $this->settings['version'], '6.1.0' ); ?>>
427
                                            6.1.0
428
                                        </option>
429
                                        <option value="6.0.0" <?php selected( $this->settings['version'], '6.0.0' ); ?>>
430
                                            6.0.0
431
                                        </option>
432
                                        <option value="5.15.4" <?php selected( $this->settings['version'], '5.15.4' ); ?>>
433
                                            5.15.4
434
                                        </option>
435
                                        <option value="5.6.0" <?php selected( $this->settings['version'], '5.6.0' ); ?>>
436
                                            5.6.0
437
                                        </option>
438
                                        <option value="5.5.0" <?php selected( $this->settings['version'], '5.5.0' ); ?>>
439
                                            5.5.0
440
                                        </option>
441
                                        <option value="5.4.0" <?php selected( $this->settings['version'], '5.4.0' ); ?>>
442
                                            5.4.0
443
                                        </option>
444
                                        <option value="5.3.0" <?php selected( $this->settings['version'], '5.3.0' ); ?>>
445
                                            5.3.0
446
                                        </option>
447
                                        <option value="5.2.0" <?php selected( $this->settings['version'], '5.2.0' ); ?>>
448
                                            5.2.0
449
                                        </option>
450
                                        <option value="5.1.0" <?php selected( $this->settings['version'], '5.1.0' ); ?>>
451
                                            5.1.0
452
                                        </option>
453
                                        <option value="4.7.0" <?php selected( $this->settings['version'], '4.7.0' ); ?>>
454
                                            4.7.1 (CSS only)
455
                                        </option>
456
                                    </select>
457
                                </td>
458
                            </tr>
459
460
                            <tr valign="top">
461
                                <th scope="row"><label
462
                                            for="wpfas-enqueue"><?php _e( 'Enqueue', 'font-awesome-settings' ); ?></label></th>
463
                                <td>
464
                                    <select name="wp-font-awesome-settings[enqueue]" id="wpfas-enqueue">
465
                                        <option
466
                                                value="" <?php selected( $this->settings['enqueue'], '' ); ?>><?php _e( 'Frontend + Backend (default)', 'font-awesome-settings' ); ?></option>
467
                                        <option
468
                                                value="frontend" <?php selected( $this->settings['enqueue'], 'frontend' ); ?>><?php _e( 'Frontend', 'font-awesome-settings' ); ?></option>
469
                                        <option
470
                                                value="backend" <?php selected( $this->settings['enqueue'], 'backend' ); ?>><?php _e( 'Backend', 'font-awesome-settings' ); ?></option>
471
                                    </select>
472
                                </td>
473
                            </tr>
474
475
                            <tr valign="top" class="wpfas-kit-hide">
476
                                <th scope="row"><label
477
                                            for="wpfas-pro"><?php _e( 'Enable pro', 'font-awesome-settings' ); ?></label></th>
478
                                <td>
479
                                    <input type="hidden" name="wp-font-awesome-settings[pro]" value="0"/>
480
                                    <input type="checkbox" name="wp-font-awesome-settings[pro]"
481
                                           value="1" <?php checked( $this->settings['pro'], '1' ); ?> id="wpfas-pro"/>
482
                                    <span><?php
483
										echo sprintf(
484
											__( 'Requires a subscription. %sLearn more%s  %sManage my allowed domains%s', 'font-awesome-settings' ),
485
											'<a rel="noopener noreferrer" target="_blank" href="https://fontawesome.com/referral?a=c9b89e1418">',
486
											' <i class="fas fa-external-link-alt"></i></a>',
487
											'<a rel="noopener noreferrer" target="_blank" href="https://fontawesome.com/account/cdn">',
488
											' <i class="fas fa-external-link-alt"></i></a>'
489
										);
490
										?></span>
491
                                </td>
492
                            </tr>
493
494
                            <tr valign="top" class="wpfas-kit-hide">
495
                                <th scope="row"><label
496
                                            for="wpfas-shims"><?php _e( 'Enable v4 shims compatibility', 'font-awesome-settings' ); ?></label>
497
                                </th>
498
                                <td>
499
                                    <input type="hidden" name="wp-font-awesome-settings[shims]" value="0"/>
500
                                    <input type="checkbox" name="wp-font-awesome-settings[shims]"
501
                                           value="1" <?php checked( $this->settings['shims'], '1' ); ?> id="wpfas-shims"/>
502
                                    <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>
503
                                </td>
504
                            </tr>
505
506
                            <tr valign="top" class="wpfas-kit-hide">
507
                                <th scope="row"><label
508
                                            for="wpfas-js-pseudo"><?php _e( 'Enable JS pseudo elements (not recommended)', 'font-awesome-settings' ); ?></label>
509
                                </th>
510
                                <td>
511
                                    <input type="hidden" name="wp-font-awesome-settings[js-pseudo]" value="0"/>
512
                                    <input type="checkbox" name="wp-font-awesome-settings[js-pseudo]"
513
                                           value="1" <?php checked( $this->settings['js-pseudo'], '1' ); ?>
514
                                           id="wpfas-js-pseudo"/>
515
                                    <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>
516
                                </td>
517
                            </tr>
518
519
                            <tr valign="top">
520
                                <th scope="row"><label
521
                                            for="wpfas-dequeue"><?php _e( 'Dequeue', 'font-awesome-settings' ); ?></label></th>
522
                                <td>
523
                                    <input type="hidden" name="wp-font-awesome-settings[dequeue]" value="0"/>
524
                                    <input type="checkbox" name="wp-font-awesome-settings[dequeue]"
525
                                           value="1" <?php checked( $this->settings['dequeue'], '1' ); ?>
526
                                           id="wpfas-dequeue"/>
527
                                    <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>
528
                                </td>
529
                            </tr>
530
531
                        </table>
532
                        <div class="fas-buttons">
533
							<?php
534
							submit_button();
535
							?>
536
                            <p class="submit"><a href="https://fontawesome.com/referral?a=c9b89e1418" class="button button-secondary"><?php _e('Get 14,000+ more icons with Font Awesome Pro','font-awesome-settings'); ?> <i class="fas fa-external-link-alt"></i></a></p>
537
538
                        </div>
539
                    </form>
540
541
                    <div id="wpfas-version"><?php echo sprintf(__( 'Version: %s (affiliate links provided)', 'font-awesome-settings' ), $this->version ); ?></div>
542
                </div>
543
544
                <style>
545
                    .fas-settings-form .submit{
546
                        display: inline;
547
                        padding-right: 5px;
548
                    }
549
550
                    .fas-settings-form .fas-buttons{
551
                        margin: 15px 0;
552
                    }
553
                    #wpfas-version{
554
                        margin-top: 30px;
555
                        color: #646970;
556
                    }
557
                </style>
558
				<?php
559
			}
560
		}
561
562
		/**
563
		 * Check a version number is valid and if so return it or else return an empty string.
564
		 *
565
		 * @param $version string The version number to check.
566
		 *
567
		 * @since 1.0.6
568
		 *
569
		 * @return string Either a valid version number or an empty string.
570
		 */
571
		public function validate_version_number( $version ) {
572
573
			if ( version_compare( $version, '0.0.1', '>=' ) >= 0 ) {
574
				// valid
575
			} else {
576
				$version = '';// not validated
577
			}
578
579
			return $version;
580
		}
581
582
583
		/**
584
		 * Get the latest version of Font Awesome.
585
		 *
586
		 * 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.
587
		 *
588
		 * @since 1.0.7
589
		 * @return mixed|string The latest version number found.
590
		 */
591
		public function get_latest_version( $force_api = false ) {
592
			$latest_version = $this->latest;
593
594
			$cache = get_transient( 'wp-font-awesome-settings-version' );
595
596
			if ( $cache === false || $force_api ) { // its not set
597
				$api_ver = $this->get_latest_version_from_api();
598
				if ( version_compare( $api_ver, $this->latest, '>=' ) >= 0 ) {
599
					$latest_version = $api_ver;
600
					set_transient( 'wp-font-awesome-settings-version', $api_ver, 48 * HOUR_IN_SECONDS );
601
				}
602
			} elseif ( $this->validate_version_number( $cache ) ) {
603
				if ( version_compare( $cache, $this->latest, '>=' ) >= 0 ) {
604
					$latest_version = $cache;
605
				}
606
			}
607
608
			return $latest_version;
609
		}
610
611
		/**
612
		 * Get the latest Font Awesome version from the github API.
613
		 *
614
		 * @since 1.0.7
615
		 * @return string The latest version number or `0` on API fail.
616
		 */
617
		public function get_latest_version_from_api() {
618
			$version  = "0";
619
			$response = wp_remote_get( "https://api.github.com/repos/FortAwesome/Font-Awesome/releases/latest" );
620
			if ( ! is_wp_error( $response ) && is_array( $response ) ) {
621
				$api_response = json_decode( wp_remote_retrieve_body( $response ), true );
622
				if ( isset( $api_response['tag_name'] ) && version_compare( $api_response['tag_name'], $this->latest, '>=' ) >= 0 && empty( $api_response['prerelease'] ) ) {
623
					$version = $api_response['tag_name'];
624
				}
625
			}
626
627
			return $version;
628
		}
629
630
		/**
631
		 * Inline CSS for RTL language support.
632
		 *
633
		 * @since 1.0.13
634
		 * @return string Inline CSS.
635
		 */
636
		public function rtl_inline_css() {
637
			$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}';
638
639
			return $inline_css;
640
		}
641
642
		/**
643
		 * Show any warnings as an admin notice.
644
		 *
645
		 * @return void
646
		 */
647
		public function admin_notices(){
648
			$settings = $this->settings;
649
650
			if ( defined( 'FONTAWESOME_PLUGIN_FILE' ) ) {
651
652
				if ( ! empty( $_REQUEST['page'] ) && $_REQUEST['page'] == 'wp-font-awesome-settings' ) {
653
					?>
654
                    <div class="notice  notice-error is-dismissible">
655
                        <p><?php _e( 'The Official Font Awesome Plugin is active, please adjust your settings there.', 'font-awesome-settings' ); ?></p>
656
                    </div>
657
					<?php
658
				}
659
660
			}else{
661
				if ( ! empty( $settings ) ) {
662
					if ( $settings['type'] != 'KIT' && $settings['pro'] && ( $settings['version'] == '' || version_compare( $settings['version'], '6', '>=' ) ) ) {
663
						$link = admin_url('options-general.php?page=wp-font-awesome-settings');
664
						?>
665
                        <div class="notice  notice-error is-dismissible">
666
                            <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>
667
                        </div>
668
						<?php
669
					}
670
				}
671
			}
672
673
		}
674
675
	}
676
677
	/**
678
	 * Run the class if found.
679
	 */
680
	WP_Font_Awesome_Settings::instance();
681
}
682