Total Complexity | 61 |
Total Lines | 532 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like WP_Font_Awesome_Settings often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use WP_Font_Awesome_Settings, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
35 | class WP_Font_Awesome_Settings { |
||
36 | |||
37 | /** |
||
38 | * Class version version. |
||
39 | * |
||
40 | * @var string |
||
41 | */ |
||
42 | public $version = '1.0.13'; |
||
43 | |||
44 | /** |
||
45 | * Class textdomain. |
||
46 | * |
||
47 | * @var string |
||
48 | */ |
||
49 | public $textdomain = 'font-awesome-settings'; |
||
50 | |||
51 | /** |
||
52 | * Latest version of Font Awesome at time of publish published. |
||
53 | * |
||
54 | * @var string |
||
55 | */ |
||
56 | public $latest = "5.8.2"; |
||
57 | |||
58 | /** |
||
59 | * The title. |
||
60 | * |
||
61 | * @var string |
||
62 | */ |
||
63 | public $name = 'Font Awesome'; |
||
64 | |||
65 | /** |
||
66 | * Holds the settings values. |
||
67 | * |
||
68 | * @var array |
||
69 | */ |
||
70 | private $settings; |
||
71 | |||
72 | /** |
||
73 | * WP_Font_Awesome_Settings instance. |
||
74 | * |
||
75 | * @access private |
||
76 | * @since 1.0.0 |
||
77 | * @var WP_Font_Awesome_Settings There can be only one! |
||
78 | */ |
||
79 | private static $instance = null; |
||
80 | |||
81 | /** |
||
82 | * Main WP_Font_Awesome_Settings Instance. |
||
83 | * |
||
84 | * Ensures only one instance of WP_Font_Awesome_Settings is loaded or can be loaded. |
||
85 | * |
||
86 | * @since 1.0.0 |
||
87 | * @static |
||
88 | * @return WP_Font_Awesome_Settings - Main instance. |
||
89 | */ |
||
90 | public static function instance() { |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * Initiate the settings and add the required action hooks. |
||
109 | * |
||
110 | * @since 1.0.8 Settings name wrong - FIXED |
||
111 | */ |
||
112 | public function init() { |
||
113 | $this->settings = $this->get_settings(); |
||
114 | |||
115 | if ( $this->settings['type'] == 'CSS' ) { |
||
116 | |||
117 | if ( $this->settings['enqueue'] == '' || $this->settings['enqueue'] == 'frontend' ) { |
||
118 | add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_style' ), 5000 ); |
||
119 | } |
||
120 | |||
121 | if ( $this->settings['enqueue'] == '' || $this->settings['enqueue'] == 'backend' ) { |
||
122 | add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_style' ), 5000 ); |
||
123 | } |
||
124 | |||
125 | } else { |
||
126 | |||
127 | if ( $this->settings['enqueue'] == '' || $this->settings['enqueue'] == 'frontend' ) { |
||
128 | add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ), 5000 ); |
||
129 | } |
||
130 | |||
131 | if ( $this->settings['enqueue'] == '' || $this->settings['enqueue'] == 'backend' ) { |
||
132 | add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ), 5000 ); |
||
133 | } |
||
134 | } |
||
135 | |||
136 | // remove font awesome if set to do so |
||
137 | if ( $this->settings['dequeue'] == '1' ) { |
||
138 | add_action( 'clean_url', array( $this, 'remove_font_awesome' ), 5000, 3 ); |
||
139 | } |
||
140 | |||
141 | } |
||
142 | |||
143 | /** |
||
144 | * Adds the Font Awesome styles. |
||
145 | */ |
||
146 | public function enqueue_style() { |
||
147 | // build url |
||
148 | $url = $this->get_url(); |
||
149 | |||
150 | wp_deregister_style( 'font-awesome' ); // deregister in case its already there |
||
151 | wp_register_style( 'font-awesome', $url, array(), null ); |
||
152 | wp_enqueue_style( 'font-awesome' ); |
||
153 | |||
154 | // RTL language support CSS. |
||
155 | if ( is_rtl() ) { |
||
156 | wp_add_inline_style( 'font-awesome', $this->rtl_inline_css() ); |
||
157 | } |
||
158 | |||
159 | if ( $this->settings['shims'] ) { |
||
160 | $url = $this->get_url( true ); |
||
161 | wp_deregister_style( 'font-awesome-shims' ); // deregister in case its already there |
||
162 | wp_register_style( 'font-awesome-shims', $url, array(), null ); |
||
163 | wp_enqueue_style( 'font-awesome-shims' ); |
||
164 | } |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * Adds the Font Awesome JS. |
||
169 | */ |
||
170 | public function enqueue_scripts() { |
||
171 | // build url |
||
172 | $url = $this->get_url(); |
||
173 | |||
174 | $deregister_function = 'wp' . '_' . 'deregister' . '_' . 'script'; |
||
175 | call_user_func( $deregister_function, 'font-awesome' ); // deregister in case its already there |
||
176 | wp_register_script( 'font-awesome', $url, array(), null ); |
||
177 | wp_enqueue_script( 'font-awesome' ); |
||
178 | |||
179 | if ( $this->settings['shims'] ) { |
||
180 | $url = $this->get_url( true ); |
||
181 | call_user_func( $deregister_function, 'font-awesome-shims' ); // deregister in case its already there |
||
182 | wp_register_script( 'font-awesome-shims', $url, array(), null ); |
||
183 | wp_enqueue_script( 'font-awesome-shims' ); |
||
184 | } |
||
185 | } |
||
186 | |||
187 | /** |
||
188 | * Get the url of the Font Awesome files. |
||
189 | * |
||
190 | * @param bool $shims If this is a shim file or not. |
||
191 | * |
||
192 | * @return string The url to the file. |
||
193 | */ |
||
194 | public function get_url( $shims = false ) { |
||
218 | } |
||
219 | |||
220 | /** |
||
221 | * Try and remove any other versions of Font Awesome added by other plugins/themes. |
||
222 | * |
||
223 | * 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. |
||
224 | * |
||
225 | * @param $url |
||
226 | * @param $original_url |
||
227 | * @param $_context |
||
228 | * |
||
229 | * @return string The filtered url. |
||
230 | */ |
||
231 | public function remove_font_awesome( $url, $original_url, $_context ) { |
||
253 | } |
||
254 | |||
255 | /** |
||
256 | * Register the database settings with WordPress. |
||
257 | */ |
||
258 | public function register_settings() { |
||
259 | register_setting( 'wp-font-awesome-settings', 'wp-font-awesome-settings' ); |
||
260 | } |
||
261 | |||
262 | /** |
||
263 | * Add the WordPress settings menu item. |
||
264 | * @since 1.0.10 Calling function name direct will fail theme check so we don't. |
||
265 | */ |
||
266 | public function menu_item() { |
||
267 | $menu_function = 'add' . '_' . 'options' . '_' . 'page'; // won't pass theme check if function name present in theme |
||
268 | call_user_func( $menu_function, $this->name, $this->name, 'manage_options', 'wp-font-awesome-settings', array( |
||
269 | $this, |
||
270 | 'settings_page' |
||
271 | ) ); |
||
272 | } |
||
273 | |||
274 | /** |
||
275 | * Get the current Font Awesome output settings. |
||
276 | * |
||
277 | * @return array The array of settings. |
||
278 | */ |
||
279 | public function get_settings() { |
||
280 | |||
281 | $db_settings = get_option( 'wp-font-awesome-settings' ); |
||
282 | |||
283 | $defaults = array( |
||
284 | 'type' => 'CSS', // type to use, CSS or JS or KIT |
||
285 | 'version' => '', // latest |
||
286 | 'enqueue' => '', // front and backend |
||
287 | 'shims' => '0', // default OFF now in 2020 |
||
288 | 'js-pseudo' => '0', // if the pseudo elements flag should be set (CPU intensive) |
||
289 | 'dequeue' => '0', // if we should try to remove other versions added by other plugins/themes |
||
290 | 'pro' => '0', // if pro CDN url should be used |
||
291 | 'kit-url' => '', // the kit url |
||
292 | ); |
||
293 | |||
294 | $settings = wp_parse_args( $db_settings, $defaults ); |
||
|
|||
295 | |||
296 | /** |
||
297 | * Filter the Font Awesome settings. |
||
298 | * |
||
299 | * @todo if we add this filer people might use it and then it defeates the purpose of this class :/ |
||
300 | */ |
||
301 | return $this->settings = apply_filters( 'wp-font-awesome-settings', $settings, $db_settings, $defaults ); |
||
302 | } |
||
303 | |||
304 | |||
305 | /** |
||
306 | * The settings page html output. |
||
307 | */ |
||
308 | public function settings_page() { |
||
309 | if ( ! current_user_can( 'manage_options' ) ) { |
||
310 | wp_die( __( 'You do not have sufficient permissions to access this page.', 'font-awesome-settings' ) ); |
||
311 | } |
||
312 | |||
313 | // a hidden way to force the update of the version number via api instead of waiting the 48 hours |
||
314 | if ( isset( $_REQUEST['force-version-check'] ) ) { |
||
315 | $this->get_latest_version( $force_api = true ); |
||
316 | } |
||
317 | ?> |
||
318 | <style> |
||
319 | .wpfas-kit-show { |
||
320 | display: none; |
||
321 | } |
||
322 | |||
323 | .wpfas-kit-set .wpfas-kit-hide { |
||
324 | display: none; |
||
325 | } |
||
326 | |||
327 | .wpfas-kit-set .wpfas-kit-show { |
||
328 | display: table-row; |
||
329 | } |
||
330 | </style> |
||
331 | <div class="wrap"> |
||
332 | <h1><?php echo $this->name; ?></h1> |
||
333 | <form method="post" action="options.php"> |
||
334 | <?php |
||
335 | settings_fields( 'wp-font-awesome-settings' ); |
||
336 | do_settings_sections( 'wp-font-awesome-settings' ); |
||
337 | $kit_set = $this->settings['type'] == 'KIT' ? 'wpfas-kit-set' : ''; |
||
338 | ?> |
||
339 | <table class="form-table wpfas-table-settings <?php echo esc_attr( $kit_set ); ?>"> |
||
340 | <tr valign="top"> |
||
341 | <th scope="row"><label |
||
342 | for="wpfas-type"><?php _e( 'Type', 'font-awesome-settings' ); ?></label></th> |
||
343 | <td> |
||
344 | <select name="wp-font-awesome-settings[type]" id="wpfas-type" |
||
345 | onchange="if(this.value=='KIT'){jQuery('.wpfas-table-settings').addClass('wpfas-kit-set');}else{jQuery('.wpfas-table-settings').removeClass('wpfas-kit-set');}"> |
||
346 | <option |
||
347 | value="CSS" <?php selected( $this->settings['type'], 'CSS' ); ?>><?php _e( 'CSS (default)', 'font-awesome-settings' ); ?></option> |
||
348 | <option value="JS" <?php selected( $this->settings['type'], 'JS' ); ?>>JS</option> |
||
349 | <option |
||
350 | value="KIT" <?php selected( $this->settings['type'], 'KIT' ); ?>><?php _e( 'Kits (settings managed on fontawesome.com)', 'font-awesome-settings' ); ?></option> |
||
351 | </select> |
||
352 | </td> |
||
353 | </tr> |
||
354 | |||
355 | <tr valign="top" class="wpfas-kit-show"> |
||
356 | <th scope="row"><label |
||
357 | for="wpfas-kit-url"><?php _e( 'Kit URL', 'font-awesome-settings' ); ?></label></th> |
||
358 | <td> |
||
359 | <input class="regular-text" id="wpfas-kit-url" type="url" |
||
360 | name="wp-font-awesome-settings[kit-url]" |
||
361 | value="<?php echo esc_attr( $this->settings['kit-url'] ); ?>" |
||
362 | placeholder="<?php echo 'https://kit.font';echo 'awesome.com/123abc.js'; // this won't pass theme check :(?>"/> |
||
363 | <span><?php |
||
364 | echo sprintf( |
||
365 | __( 'Requires a free account with Font Awesome. %sGet kit url%s', 'font-awesome-settings' ), |
||
366 | '<a rel="noopener noreferrer" target="_blank" href="https://fontawesome.com/kits"><i class="fas fa-external-link-alt"></i>', |
||
367 | '</a>' |
||
368 | ); |
||
369 | ?></span> |
||
370 | </td> |
||
371 | </tr> |
||
372 | |||
373 | <tr valign="top" class="wpfas-kit-hide"> |
||
374 | <th scope="row"><label |
||
375 | for="wpfas-version"><?php _e( 'Version', 'font-awesome-settings' ); ?></label></th> |
||
376 | <td> |
||
377 | <select name="wp-font-awesome-settings[version]" id="wpfas-version"> |
||
378 | <option |
||
379 | value="" <?php selected( $this->settings['version'], '' ); ?>><?php echo sprintf( __( 'Latest - %s (default)', 'font-awesome-settings' ), $this->get_latest_version() ); ?> |
||
380 | </option> |
||
381 | <option value="5.6.0" <?php selected( $this->settings['version'], '5.6.0' ); ?>> |
||
382 | 5.6.0 |
||
383 | </option> |
||
384 | <option value="5.5.0" <?php selected( $this->settings['version'], '5.5.0' ); ?>> |
||
385 | 5.5.0 |
||
386 | </option> |
||
387 | <option value="5.4.0" <?php selected( $this->settings['version'], '5.4.0' ); ?>> |
||
388 | 5.4.0 |
||
389 | </option> |
||
390 | <option value="5.3.0" <?php selected( $this->settings['version'], '5.3.0' ); ?>> |
||
391 | 5.3.0 |
||
392 | </option> |
||
393 | <option value="5.2.0" <?php selected( $this->settings['version'], '5.2.0' ); ?>> |
||
394 | 5.2.0 |
||
395 | </option> |
||
396 | <option value="5.1.0" <?php selected( $this->settings['version'], '5.1.0' ); ?>> |
||
397 | 5.1.0 |
||
398 | </option> |
||
399 | <option value="4.7.0" <?php selected( $this->settings['version'], '4.7.0' ); ?>> |
||
400 | 4.7.1 (CSS only) |
||
401 | </option> |
||
402 | </select> |
||
403 | </td> |
||
404 | </tr> |
||
405 | |||
406 | <tr valign="top"> |
||
407 | <th scope="row"><label |
||
408 | for="wpfas-enqueue"><?php _e( 'Enqueue', 'font-awesome-settings' ); ?></label></th> |
||
409 | <td> |
||
410 | <select name="wp-font-awesome-settings[enqueue]" id="wpfas-enqueue"> |
||
411 | <option |
||
412 | value="" <?php selected( $this->settings['enqueue'], '' ); ?>><?php _e( 'Frontend + Backend (default)', 'font-awesome-settings' ); ?></option> |
||
413 | <option |
||
414 | value="frontend" <?php selected( $this->settings['enqueue'], 'frontend' ); ?>><?php _e( 'Frontend', 'font-awesome-settings' ); ?></option> |
||
415 | <option |
||
416 | value="backend" <?php selected( $this->settings['enqueue'], 'backend' ); ?>><?php _e( 'Backend', 'font-awesome-settings' ); ?></option> |
||
417 | </select> |
||
418 | </td> |
||
419 | </tr> |
||
420 | |||
421 | <tr valign="top" class="wpfas-kit-hide"> |
||
422 | <th scope="row"><label |
||
423 | for="wpfas-pro"><?php _e( 'Enable pro', 'font-awesome-settings' ); ?></label></th> |
||
424 | <td> |
||
425 | <input type="hidden" name="wp-font-awesome-settings[pro]" value="0"/> |
||
426 | <input type="checkbox" name="wp-font-awesome-settings[pro]" |
||
427 | value="1" <?php checked( $this->settings['pro'], '1' ); ?> id="wpfas-pro"/> |
||
428 | <span><?php |
||
429 | echo sprintf( |
||
430 | __( 'Requires a subscription. %sLearn more%s %sManage my allowed domains%s', 'font-awesome-settings' ), |
||
431 | '<a rel="noopener noreferrer" target="_blank" href="https://fontawesome.com/pro"><i class="fas fa-external-link-alt"></i>', |
||
432 | '</a>', |
||
433 | '<a rel="noopener noreferrer" target="_blank" href="https://fontawesome.com/account/cdn"><i class="fas fa-external-link-alt"></i>', |
||
434 | '</a>' |
||
435 | ); |
||
436 | ?></span> |
||
437 | </td> |
||
438 | </tr> |
||
439 | |||
440 | <tr valign="top" class="wpfas-kit-hide"> |
||
441 | <th scope="row"><label |
||
442 | for="wpfas-shims"><?php _e( 'Enable v4 shims compatibility', 'font-awesome-settings' ); ?></label> |
||
443 | </th> |
||
444 | <td> |
||
445 | <input type="hidden" name="wp-font-awesome-settings[shims]" value="0"/> |
||
446 | <input type="checkbox" name="wp-font-awesome-settings[shims]" |
||
447 | value="1" <?php checked( $this->settings['shims'], '1' ); ?> id="wpfas-shims"/> |
||
448 | <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> |
||
449 | </td> |
||
450 | </tr> |
||
451 | |||
452 | <tr valign="top" class="wpfas-kit-hide"> |
||
453 | <th scope="row"><label |
||
454 | for="wpfas-js-pseudo"><?php _e( 'Enable JS pseudo elements (not recommended)', 'font-awesome-settings' ); ?></label> |
||
455 | </th> |
||
456 | <td> |
||
457 | <input type="hidden" name="wp-font-awesome-settings[js-pseudo]" value="0"/> |
||
458 | <input type="checkbox" name="wp-font-awesome-settings[js-pseudo]" |
||
459 | value="1" <?php checked( $this->settings['js-pseudo'], '1' ); ?> |
||
460 | id="wpfas-js-pseudo"/> |
||
461 | <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> |
||
462 | </td> |
||
463 | </tr> |
||
464 | |||
465 | <tr valign="top"> |
||
466 | <th scope="row"><label |
||
467 | for="wpfas-dequeue"><?php _e( 'Dequeue', 'font-awesome-settings' ); ?></label></th> |
||
468 | <td> |
||
469 | <input type="hidden" name="wp-font-awesome-settings[dequeue]" value="0"/> |
||
470 | <input type="checkbox" name="wp-font-awesome-settings[dequeue]" |
||
471 | value="1" <?php checked( $this->settings['dequeue'], '1' ); ?> |
||
472 | id="wpfas-dequeue"/> |
||
473 | <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> |
||
474 | </td> |
||
475 | </tr> |
||
476 | |||
477 | </table> |
||
478 | <?php |
||
479 | submit_button(); |
||
480 | ?> |
||
481 | </form> |
||
482 | |||
483 | <div id="wpfas-version"><?php echo $this->version; ?></div> |
||
484 | </div> |
||
485 | |||
486 | <?php |
||
487 | } |
||
488 | |||
489 | /** |
||
490 | * Check a version number is valid and if so return it or else return an empty string. |
||
491 | * |
||
492 | * @param $version string The version number to check. |
||
493 | * |
||
494 | * @since 1.0.6 |
||
495 | * |
||
496 | * @return string Either a valid version number or an empty string. |
||
497 | */ |
||
498 | public function validate_version_number( $version ) { |
||
499 | |||
500 | if ( version_compare( $version, '0.0.1', '>=' ) >= 0 ) { |
||
501 | // valid |
||
502 | } else { |
||
503 | $version = '';// not validated |
||
504 | } |
||
505 | |||
506 | return $version; |
||
507 | } |
||
508 | |||
509 | |||
510 | /** |
||
511 | * Get the latest version of Font Awesome. |
||
512 | * |
||
513 | * 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. |
||
514 | * |
||
515 | * @since 1.0.7 |
||
516 | * @return mixed|string The latest version number found. |
||
517 | */ |
||
518 | public function get_latest_version( $force_api = false ) { |
||
519 | $latest_version = $this->latest; |
||
520 | |||
521 | $cache = get_transient( 'wp-font-awesome-settings-version' ); |
||
522 | |||
523 | if ( $cache === false || $force_api ) { // its not set |
||
524 | $api_ver = $this->get_latest_version_from_api(); |
||
525 | if ( version_compare( $api_ver, $this->latest, '>=' ) >= 0 ) { |
||
526 | $latest_version = $api_ver; |
||
527 | set_transient( 'wp-font-awesome-settings-version', $api_ver, 48 * HOUR_IN_SECONDS ); |
||
528 | } |
||
529 | } elseif ( $this->validate_version_number( $cache ) ) { |
||
530 | if ( version_compare( $cache, $this->latest, '>=' ) >= 0 ) { |
||
531 | $latest_version = $cache; |
||
532 | } |
||
533 | } |
||
534 | |||
535 | return $latest_version; |
||
536 | } |
||
537 | |||
538 | /** |
||
539 | * Get the latest Font Awesome version from the github API. |
||
540 | * |
||
541 | * @since 1.0.7 |
||
542 | * @return string The latest version number or `0` on API fail. |
||
543 | */ |
||
544 | public function get_latest_version_from_api() { |
||
545 | $version = "0"; |
||
546 | $response = wp_remote_get( "https://api.github.com/repos/FortAwesome/Font-Awesome/releases/latest" ); |
||
547 | if ( ! is_wp_error( $response ) && is_array( $response ) ) { |
||
548 | $api_response = json_decode( wp_remote_retrieve_body( $response ), true ); |
||
549 | if ( isset( $api_response['tag_name'] ) && version_compare( $api_response['tag_name'], $this->latest, '>=' ) >= 0 && empty( $api_response['prerelease'] ) ) { |
||
550 | $version = $api_response['tag_name']; |
||
551 | } |
||
552 | } |
||
553 | |||
554 | return $version; |
||
555 | } |
||
556 | |||
557 | /** |
||
558 | * Inline CSS for RTL language support. |
||
559 | * |
||
560 | * @since 1.0.13 |
||
561 | * @return string Inline CSS. |
||
562 | */ |
||
563 | public function rtl_inline_css() { |
||
567 | } |
||
568 | |||
569 | } |
||
570 | |||
571 | /** |
||
572 | * Run the class if found. |
||
573 | */ |
||
574 | WP_Font_Awesome_Settings::instance(); |
||
575 | } |