Passed
Pull Request — master (#183)
by
unknown
03:03
created

WP_Font_Awesome_Settings::enqueue_scripts()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15

Duplication

Lines 15
Ratio 100 %

Importance

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

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

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

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

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

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

Loading history...
154
			// build url
155
			$url = $this->get_url();
156
157
			wp_deregister_script( 'font-awesome' ); // deregister in case its already there
158
			wp_register_script( 'font-awesome', $url,array(), null );
159
			wp_enqueue_script( 'font-awesome' );
160
161
			if($this->settings['shims']){
162
				$url = $this->get_url(true);
163
				wp_deregister_script( 'font-awesome-shims' ); // deregister in case its already there
164
				wp_register_script( 'font-awesome-shims', $url, array(), null );
165
				wp_enqueue_script( 'font-awesome-shims' );
166
			}
167
		}
168
169
		/**
170
		 * Get the url of the Font Awesome files.
171
		 *
172
		 * @param bool $shims If this is a shim file or not.
173
		 *
174
		 * @return string The url to the file.
175
		 */
176
		public function get_url($shims = false){
177
			$script = $shims ? 'v4-shims' : 'all';
178
			$type = $this->settings['type'];
179
			$version = $this->settings['version'];
180
181
			$url = "https://use.fontawesome.com/releases/"; // CDN
182
			$url .= !empty($version) ? "v".$version.'/' : "v".$this->latest.'/'; // version
183
			$url .= $type=='CSS' ? 'css/' : 'js/'; // type
184
			$url .= $type=='CSS' ? $script.'.css' : $script.'.js'; // type
185
			$url .= "?wpfas=true"; // set our var so our version is not removed
186
187
			return $url;
188
		}
189
190
		/**
191
		 * Try and remove any other versions of Font Awesome added by other plugins/themes.
192
		 *
193
		 * 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.
194
		 *
195
		 * @param $url
196
		 * @param $original_url
197
		 * @param $_context
198
		 *
199
		 * @return string The filtered url.
200
		 */
201
		public function remove_font_awesome($url, $original_url, $_context){
202
203
			if ($_context=='display' &&  strstr( $url, "fontawesome" ) !== false || strstr( $url, "font-awesome" ) !== false ) {// it's a font-awesome-url (probably)
204
205
				if(strstr( $url, "wpfas=true" ) !== false){
206
					if($this->settings['type']=='JS'){
207
						if($this->settings['js-pseudo']){
208
							$url .= "' data-search-pseudo-elements defer='defer";
209
						}else{
210
							$url .= "' defer='defer";
211
						}
212
					}
213
				}
214
				else{
215
					$url = ''; // removing the url removes the file
216
				}
217
218
			}
219
220
			return $url;
221
		}
222
223
		/**
224
		 * Register the database settings with WordPress.
225
		 */
226
		public function register_settings() {
227
			register_setting( 'wp-font-awesome-settings', 'wp-font-awesome-settings' );
228
		}
229
230
		/**
231
		 * Add the WordPress settings menu item.
232
		 */
233
		public function menu_item(){
234
			add_options_page( $this->name, $this->name, 'manage_options', 'wp-font-awesome-settings', array($this,'settings_page') );
235
		}
236
237
		/**
238
		 * Get the current Font Awesome output settings.
239
		 *
240
		 * @return array The array of settings.
241
		 */
242
		public function get_settings(){
243
244
			$db_settings = get_option( 'wp-font-awesome-settings' );
245
246
			$defaults = array(
247
				'type'  => 'CSS', // type to use, CSS or JS
248
				'version'  => '', // latest
249
				'enqueue'  => '', // front and backend
250
				'shims'  => '1', // default on for now, @todo maybe change to off in 2020
251
				'js-pseudo'  => '0', // if the pseudo elements flag should be set (CPU intensive)
252
				'dequeue'  => '0', // if we should try to remove other versions added by other plugins/themes
253
			);
254
255
			$settings = wp_parse_args($db_settings,$defaults);
256
257
			/**
258
			 * Filter the Font Awesome settings.
259
			 *
260
			 * @todo if we add this filer people might use it and then it defeates the purpose of this class :/
261
			 */
262
			return $this->settings = apply_filters('wp-font-awesome-settings',$settings,$db_settings,$defaults);
263
		}
264
265
266
		public function settings_page() {
267
			if ( !current_user_can( 'manage_options' ) )  {
268
				wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
269
			}
270
			?>
271
			<div class="wrap">
272
				<h1><?php echo $this->name; ?></h1>
273
				<form method="post" action="options.php">
274
					<?php
275
					settings_fields( 'wp-font-awesome-settings' );
276
					do_settings_sections( 'wp-font-awesome-settings' );
277
					?>
278
					<table class="form-table">
279
						<tr valign="top">
280
							<th scope="row"><label for="wpfas-type"><?php _e('Type');?></label></th>
281
							<td>
282
								<select name="wp-font-awesome-settings[type]" id="wpfas-type">
283
									<option value="CSS" <?php selected( $this->settings['type'], 'CSS' ); ?>><?php _e('CSS (default)');?></option>
284
									<option value="JS" <?php selected( $this->settings['type'], 'JS' ); ?>>JS</option>
285
								</select>
286
							</td>
287
						</tr>
288
289
						<tr valign="top">
290
							<th scope="row"><label for="wpfas-version"><?php _e('Version');?></label></th>
291
							<td>
292
								<select name="wp-font-awesome-settings[version]" id="wpfas-version">
293
									<option value="" <?php selected( $this->settings['version'], '' ); ?>><?php _e('Latest (default)');?></option>
294
									<option value="5.5.0" <?php selected( $this->settings['version'], '5.5.0' ); ?>>5.5.0</option>
295
									<option value="5.4.0" <?php selected( $this->settings['version'], '5.4.0' ); ?>>5.4.0</option>
296
									<option value="5.3.0" <?php selected( $this->settings['version'], '5.3.0' ); ?>>5.3.0</option>
297
									<option value="5.2.0" <?php selected( $this->settings['version'], '5.2.0' ); ?>>5.2.0</option>
298
									<option value="5.1.0" <?php selected( $this->settings['version'], '5.1.0' ); ?>>5.1.0</option>
299
									<option value="4.7.0" <?php selected( $this->settings['version'], '4.7.0' ); ?>>4.7.1 (CSS only)</option>
300
								</select>
301
							</td>
302
						</tr>
303
304
						<tr valign="top">
305
							<th scope="row"><label for="wpfas-enqueue"><?php _e('Enqueue');?></label></th>
306
							<td>
307
								<select name="wp-font-awesome-settings[enqueue]" id="wpfas-enqueue">
308
									<option value="" <?php selected( $this->settings['enqueue'], '' ); ?>><?php _e('Frontend + Backend (default)');?></option>
309
									<option value="frontend" <?php selected( $this->settings['enqueue'], 'frontend' ); ?>><?php _e('Frontend');?></option>
310
									<option value="backend" <?php selected( $this->settings['enqueue'], 'backend' ); ?>><?php _e('Backend');?></option>
311
								</select>
312
							</td>
313
						</tr>
314
315
						<tr valign="top">
316
							<th scope="row"><label for="wpfas-shims"><?php _e('Enable v4 shims compatibility');?></label></th>
317
							<td>
318
								<input type="hidden" name="wp-font-awesome-settings[shims]" value="0" />
319
								<input type="checkbox" name="wp-font-awesome-settings[shims]" value="1" <?php checked( $this->settings['shims'], '1' ); ?> id="wpfas-shims" />
320
								<span><?php _e('This enables v4 classes to work with v5, sort of like a band-aid until everyone has updated everything to v5.');?></span>
321
							</td>
322
						</tr>
323
324
						<tr valign="top">
325
							<th scope="row"><label for="wpfas-js-pseudo"><?php _e('Enable JS pseudo elements (not recommended)');?></label></th>
326
							<td>
327
								<input type="hidden" name="wp-font-awesome-settings[js-pseudo]" value="0" />
328
								<input type="checkbox" name="wp-font-awesome-settings[js-pseudo]" value="1" <?php checked( $this->settings['js-pseudo'], '1' ); ?> id="wpfas-js-pseudo" />
329
								<span><?php _e('Used only with the JS version, this will make pseudo-elements work but can be CPU intensive on some sites.');?></span>
330
							</td>
331
						</tr>
332
333
						<tr valign="top">
334
							<th scope="row"><label for="wpfas-dequeue"><?php _e('Dequeue');?></label></th>
335
							<td>
336
								<input type="hidden" name="wp-font-awesome-settings[dequeue]" value="0" />
337
								<input type="checkbox" name="wp-font-awesome-settings[dequeue]" value="1" <?php checked( $this->settings['dequeue'], '1' ); ?> id="wpfas-dequeue" />
338
								<span><?php _e('This will try to dequeue any other Font Awesome versions loaded by other sources if they are added with `font-awesome` or `fontawesome` in the name.');?></span>
339
							</td>
340
						</tr>
341
342
343
					</table>
344
					<?php
345
					submit_button();
346
					?>
347
				</form>
348
349
				<div id="wpfas-version"><?php echo $this->version;?></div>
350
			</div>
351
352
			<?php
353
		}
354
355
356
	}
357
358
	/**
359
	 * Run the class if found.
360
	 */
361
	WP_Font_Awesome_Settings::instance();
362
}