| 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.12'; |
| 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 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 enabled |
| 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 |
$enqueue = false; |
| 186 |
|
| 187 |
if ( $this->settings['enqueue'] == '' || $this->settings['enqueue'] == 'frontend' ) { |
| 188 |
$enqueue = true; |
| 189 |
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ), 5000 ); |
| 190 |
} |
| 191 |
|
| 192 |
if ( $this->settings['enqueue'] == '' || $this->settings['enqueue'] == 'backend' ) { |
| 193 |
$enqueue = true; |
| 194 |
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ), 5000 ); |
| 195 |
add_filter( 'block_editor_settings_all', array( $this, 'enqueue_editor_scripts' ), 10, 2 ); |
| 196 |
} |
| 197 |
|
| 198 |
if ( $enqueue ) { |
| 199 |
add_filter( 'script_loader_tag', array( $this, 'script_loader_tag' ), 20, 3 ); |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
// remove font awesome if set to do so |
| 204 |
if ( $this->settings['dequeue'] == '1' ) { |
| 205 |
add_action( 'clean_url', array( $this, 'remove_font_awesome' ), 5000, 3 ); |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
} |
| 210 |
|
| 211 |
/** |
| 212 |
* Add FA to the FSE. |
| 213 |
* |
| 214 |
* @param $editor_settings |
| 215 |
* @param $block_editor_context |
| 216 |
* |
| 217 |
* @return array |
| 218 |
*/ |
| 219 |
public function enqueue_editor_styles( $editor_settings, $block_editor_context ){ |
| 220 |
|
| 221 |
if ( ! empty( $editor_settings['__unstableResolvedAssets']['styles'] ) ) { |
| 222 |
$url = $this->get_url(); |
| 223 |
$editor_settings['__unstableResolvedAssets']['styles'] .= "<link rel='stylesheet' id='font-awesome-css' href='$url' media='all' />"; |
| 224 |
} |
| 225 |
|
| 226 |
return $editor_settings; |
| 227 |
} |
| 228 |
|
| 229 |
/** |
| 230 |
* Add FA to the FSE. |
| 231 |
* |
| 232 |
* @param $editor_settings |
| 233 |
* @param $block_editor_context |
| 234 |
* |
| 235 |
* @return array |
| 236 |
*/ |
| 237 |
public function enqueue_editor_scripts( $editor_settings, $block_editor_context ) { |
| 238 |
$url = $this->get_url(); |
| 239 |
|
| 240 |
$editor_settings['__unstableResolvedAssets']['scripts'] .= "<script src='$url' id='font-awesome-js' defer crossorigin='anonymous'></script>"; |
| 241 |
|
| 242 |
return $editor_settings; |
| 243 |
} |
| 244 |
|
| 245 |
/** |
| 246 |
* Adds the Font Awesome styles. |
| 247 |
*/ |
| 248 |
public function enqueue_style() { |
| 249 |
// build url |
| 250 |
$url = $this->get_url(); |
| 251 |
$version = ! empty( $this->settings['local'] ) && empty( $this->settings['pro'] ) ? strip_tags( $this->settings['local_version'] ) : null; |
| 252 |
|
| 253 |
wp_deregister_style( 'font-awesome' ); // deregister in case its already there |
| 254 |
wp_register_style( 'font-awesome', $url, array(), $version ); |
| 255 |
wp_enqueue_style( 'font-awesome' ); |
| 256 |
|
| 257 |
// RTL language support CSS. |
| 258 |
if ( is_rtl() ) { |
| 259 |
wp_add_inline_style( 'font-awesome', $this->rtl_inline_css() ); |
| 260 |
} |
| 261 |
|
| 262 |
if ( $this->settings['shims'] ) { |
| 263 |
$url = $this->get_url( true ); |
| 264 |
wp_deregister_style( 'font-awesome-shims' ); // deregister in case its already there |
| 265 |
wp_register_style( 'font-awesome-shims', $url, array(), $version ); |
| 266 |
wp_enqueue_style( 'font-awesome-shims' ); |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
/** |
| 271 |
* Adds the Font Awesome JS. |
| 272 |
*/ |
| 273 |
public function enqueue_scripts() { |
| 274 |
// build url |
| 275 |
$url = $this->get_url(); |
| 276 |
|
| 277 |
$deregister_function = 'wp' . '_' . 'deregister' . '_' . 'script'; |
| 278 |
call_user_func( $deregister_function, 'font-awesome' ); // deregister in case its already there |
| 279 |
wp_register_script( 'font-awesome', $url, array(), null ); |
| 280 |
wp_enqueue_script( 'font-awesome' ); |
| 281 |
|
| 282 |
if ( $this->settings['shims'] ) { |
| 283 |
$url = $this->get_url( true ); |
| 284 |
call_user_func( $deregister_function, 'font-awesome-shims' ); // deregister in case its already there |
| 285 |
wp_register_script( 'font-awesome-shims', $url, array(), null ); |
| 286 |
wp_enqueue_script( 'font-awesome-shims' ); |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
/** |
| 291 |
* Get the url of the Font Awesome files. |
| 292 |
* |
| 293 |
* @param bool $shims If this is a shim file or not. |
| 294 |
* @param bool $local Load locally if allowed. |
| 295 |
* |
| 296 |
* @return string The url to the file. |
| 297 |
*/ |
| 298 |
public function get_url( $shims = false, $local = true ) { |
| 299 |
$script = $shims ? 'v4-shims' : 'all'; |
| 300 |
$sub = $this->settings['pro'] ? 'pro' : 'use'; |
| 301 |
$type = $this->settings['type']; |
| 302 |
$version = $this->settings['version']; |
| 303 |
$kit_url = $this->settings['kit-url'] ? sanitize_text_field( $this->settings['kit-url'] ) : ''; |
| 304 |
$url = ''; |
| 305 |
|
| 306 |
if ( $type == 'KIT' && $kit_url ) { |
| 307 |
if ( $shims ) { |
| 308 |
// if its a kit then we don't add shims here |
| 309 |
return ''; |
| 310 |
} |
| 311 |
$url .= $kit_url; // CDN |
| 312 |
$url .= "?wpfas=true"; // set our var so our version is not removed |
| 313 |
} else { |
| 314 |
$v = ''; |
| 315 |
// Check and load locally. |
| 316 |
if ( $local && $this->has_local() ) { |
| 317 |
$script .= ".min"; |
| 318 |
$v .= '&ver=' . strip_tags( $this->settings['local_version'] ); |
| 319 |
$url .= $this->get_fonts_url(); // Local fonts url. |
| 320 |
} else { |
| 321 |
$url .= "https://$sub.fontawesome.com/releases/"; // CDN |
| 322 |
$url .= ! empty( $version ) ? "v" . $version . '/' : "v" . $this->get_latest_version() . '/'; // version |
| 323 |
} |
| 324 |
$url .= $type == 'CSS' ? 'css/' : 'js/'; // type |
| 325 |
$url .= $type == 'CSS' ? $script . '.css' : $script . '.js'; // type |
| 326 |
$url .= "?wpfas=true" . $v; // set our var so our version is not removed |
| 327 |
} |
| 328 |
|
| 329 |
return $url; |
| 330 |
} |
| 331 |
|
| 332 |
/** |
| 333 |
* Try and remove any other versions of Font Awesome added by other plugins/themes. |
| 334 |
* |
| 335 |
* 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. |
| 336 |
* |
| 337 |
* @param $url |
| 338 |
* @param $original_url |
| 339 |
* @param $_context |
| 340 |
* |
| 341 |
* @return string The filtered url. |
| 342 |
*/ |
| 343 |
public function remove_font_awesome( $url, $original_url, $_context ) { |
| 344 |
if ( $_context == 'display' |
| 345 |
&& ( strstr( $url, "fontawesome" ) !== false || strstr( $url, "font-awesome" ) !== false ) |
| 346 |
&& ( strstr( $url, ".js" ) !== false || strstr( $url, ".css" ) !== false ) |
| 347 |
&& ( strpos( $url, "iconpicker" ) === false ) |
| 348 |
) {// it's a font-awesome-url (probably) |
| 349 |
|
| 350 |
if ( strstr( $url, "wpfas=true" ) !== false ) { |
| 351 |
if ( $this->settings['type'] == 'JS' ) { |
| 352 |
if ( $this->settings['js-pseudo'] ) { |
| 353 |
$url .= "' data-search-pseudo-elements defer='defer"; |
| 354 |
} else { |
| 355 |
$url .= "' defer='defer"; |
| 356 |
} |
| 357 |
} |
| 358 |
} else { |
| 359 |
$url = ''; // removing the url removes the file |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
return $url; |
| 364 |
} |
| 365 |
|
| 366 |
/** |
| 367 |
* Register the database settings with WordPress. |
| 368 |
*/ |
| 369 |
public function register_settings() { |
| 370 |
register_setting( 'wp-font-awesome-settings', 'wp-font-awesome-settings' ); |
| 371 |
} |
| 372 |
|
| 373 |
/** |
| 374 |
* Add the WordPress settings menu item. |
| 375 |
* @since 1.0.10 Calling function name direct will fail theme check so we don't. |
| 376 |
*/ |
| 377 |
public function menu_item() { |
| 378 |
$menu_function = 'add' . '_' . 'options' . '_' . 'page'; // won't pass theme check if function name present in theme |
| 379 |
call_user_func( $menu_function, $this->name, $this->name, 'manage_options', 'wp-font-awesome-settings', array( |
| 380 |
$this, |
| 381 |
'settings_page' |
| 382 |
) ); |
| 383 |
} |
| 384 |
|
| 385 |
/** |
| 386 |
* Get the current Font Awesome output settings. |
| 387 |
* |
| 388 |
* @return array The array of settings. |
| 389 |
*/ |
| 390 |
public function get_settings() { |
| 391 |
$db_settings = get_option( 'wp-font-awesome-settings' ); |
| 392 |
|
| 393 |
$defaults = array( |
| 394 |
'type' => 'CSS', // type to use, CSS or JS or KIT |
| 395 |
'version' => '', // latest |
| 396 |
'enqueue' => '', // front and backend |
| 397 |
'shims' => '0', // default OFF now in 2020 |
| 398 |
'js-pseudo' => '0', // if the pseudo elements flag should be set (CPU intensive) |
| 399 |
'dequeue' => '0', // if we should try to remove other versions added by other plugins/themes |
| 400 |
'pro' => '0', // if pro CDN url should be used |
| 401 |
'local' => '0', // Store fonts locally. |
| 402 |
'local_version' => '', // Local fonts version. |
| 403 |
'kit-url' => '', // the kit url |
| 404 |
); |
| 405 |
|
| 406 |
$settings = wp_parse_args( $db_settings, $defaults ); |
| 407 |
|
| 408 |
/** |
| 409 |
* Filter the Font Awesome settings. |
| 410 |
* |
| 411 |
* @todo if we add this filer people might use it and then it defeates the purpose of this class :/ |
| 412 |
*/ |
| 413 |
return $this->settings = apply_filters( 'wp-font-awesome-settings', $settings, $db_settings, $defaults ); |
| 414 |
} |
| 415 |
|
| 416 |
/** |
| 417 |
* The settings page html output. |
| 418 |
*/ |
| 419 |
public function settings_page() { |
| 420 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 421 |
wp_die( __( 'You do not have sufficient permissions to access this page.', 'ayecode-connect' ) ); |
| 422 |
} |
| 423 |
|
| 424 |
// a hidden way to force the update of the version number via api instead of waiting the 48 hours |
| 425 |
if ( isset( $_REQUEST['force-version-check'] ) ) { |
| 426 |
$this->get_latest_version( $force_api = true ); |
| 427 |
} |
| 428 |
|
| 429 |
if ( ! defined( 'FONTAWESOME_PLUGIN_FILE' ) ) { |
| 430 |
?> |
| 431 |
<style> |
| 432 |
.wpfas-kit-show { |
| 433 |
display: none; |
| 434 |
} |
| 435 |
.wpfas-kit-set .wpfas-kit-hide,.wpfas-has-pro .wpfas-hide-pro { |
| 436 |
display: none; |
| 437 |
} |
| 438 |
.wpfas-kit-set .wpfas-kit-show { |
| 439 |
display: table-row; |
| 440 |
} |
| 441 |
.fas-settings-form .submit{ |
| 442 |
display: inline; |
| 443 |
padding-right: 5px; |
| 444 |
} |
| 445 |
.fas-settings-form .fas-buttons{ |
| 446 |
margin: 15px 0; |
| 447 |
} |
| 448 |
#wpfas-version{ |
| 449 |
color: #646970; |
| 450 |
} |
| 451 |
</style> |
| 452 |
<div class="wrap"> |
| 453 |
<h1><?php echo $this->name; ?></h1> |
| 454 |
<form method="post" action="options.php" class="fas-settings-form"> |
| 455 |
<?php |
| 456 |
settings_fields( 'wp-font-awesome-settings' ); |
| 457 |
do_settings_sections( 'wp-font-awesome-settings' ); |
| 458 |
$table_class = ''; |
| 459 |
if ( $this->settings['type'] ) { |
| 460 |
$table_class .= 'wpfas-' . sanitize_html_class( strtolower( $this->settings['type'] ) ) . '-set'; |
| 461 |
} |
| 462 |
if ( ! empty( $this->settings['pro'] ) ) { |
| 463 |
$table_class .= ' wpfas-has-pro'; |
| 464 |
} |
| 465 |
?> |
| 466 |
<?php if ( $this->settings['type'] != 'KIT' && ! empty( $this->settings['local'] ) && empty( $this->settings['pro'] ) ) { ?> |
| 467 |
<?php if ( $this->has_local() ) { ?> |
| 468 |
<div class="notice notice-info"><p><strong><?php _e( 'Font Awesome fonts are loading locally.', 'ayecode-connect' ); ?></strong></p></div> |
| 469 |
<?php } else { ?> |
| 470 |
<div class="notice notice-error"><p><strong><?php _e( 'Font Awesome fonts are not loading locally!', 'ayecode-connect' ); ?></strong></p></div> |
| 471 |
<?php } ?> |
| 472 |
<?php } ?> |
| 473 |
<table class="form-table wpfas-table-settings <?php echo esc_attr( $table_class ); ?>"> |
| 474 |
<tr valign="top"> |
| 475 |
<th scope="row"><label for="wpfas-type"><?php _e( 'Type', 'ayecode-connect' ); ?></label></th> |
| 476 |
<td> |
| 477 |
<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');}"> |
| 478 |
<option value="CSS" <?php selected( $this->settings['type'], 'CSS' ); ?>><?php _e( 'CSS (default)', 'ayecode-connect' ); ?></option> |
| 479 |
<option value="JS" <?php selected( $this->settings['type'], 'JS' ); ?>>JS</option> |
| 480 |
<option value="KIT" <?php selected( $this->settings['type'], 'KIT' ); ?>><?php _e( 'Kits (settings managed on fontawesome.com)', 'ayecode-connect' ); ?></option> |
| 481 |
</select> |
| 482 |
</td> |
| 483 |
</tr> |
| 484 |
|
| 485 |
<tr valign="top" class="wpfas-kit-show"> |
| 486 |
<th scope="row"><label for="wpfas-kit-url"><?php _e( 'Kit URL', 'ayecode-connect' ); ?></label></th> |
| 487 |
<td> |
| 488 |
<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 :(?>"/> |
| 489 |
<span><?php |
| 490 |
echo wp_sprintf( |
| 491 |
__( 'Requires a free account with Font Awesome. %sGet kit url%s', 'ayecode-connect' ), |
| 492 |
'<a rel="noopener noreferrer" target="_blank" href="https://fontawesome.com/kits"><i class="fas fa-external-link-alt"></i> ', |
| 493 |
'</a>' |
| 494 |
); |
| 495 |
?></span> |
| 496 |
</td> |
| 497 |
</tr> |
| 498 |
<tr valign="top" class="wpfas-kit-hide"> |
| 499 |
<th scope="row"><label for="wpfas-version"><?php _e( 'Version', 'ayecode-connect' ); ?></label></th> |
| 500 |
<td> |
| 501 |
<select name="wp-font-awesome-settings[version]" id="wpfas-version"> |
| 502 |
<?php /* @todo Remove after FA7 compatibility */ ?> |
| 503 |
<option value="" <?php selected( $this->settings['version'], '' ); ?>><?php echo wp_sprintf( __( '%s (default)', 'ayecode-connect' ), '6.7.2' ); ?></option> |
| 504 |
<?php $latest_version = $this->get_latest_version( false, true, true ); if ( $latest_version && version_compare( $latest_version, '7.0.0', '>' ) ) { ?> |
| 505 |
<option value="<?php echo esc_attr( $latest_version ); ?>" <?php selected( $this->settings['version'], $latest_version ); ?>><?php echo esc_html( $latest_version ); ?></option> |
| 506 |
<?php } ?> |
| 507 |
<?php /* @todo Remove after after FA7 compatibility */ ?> |
| 508 |
|
| 509 |
<?php /* @todo Un-comment after FA7 compatibility */ ?> |
| 510 |
<?php /* ?><option value="" <?php selected( $this->settings['version'], '' ); ?>><?php echo wp_sprintf( __( 'Latest - %s (default)', 'ayecode-connect' ), $this->get_latest_version() ); ?></option><?php */ ?> |
| 511 |
<option value="7.3.0" <?php selected( $this->settings['version'], '7.3.0' ); ?>>7.3.0</option> |
| 512 |
<option value="7.2.0" <?php selected( $this->settings['version'], '7.2.0' ); ?>>7.2.0</option> |
| 513 |
<option value="7.1.0" <?php selected( $this->settings['version'], '7.1.0' ); ?>>7.1.0</option> |
| 514 |
<option value="7.0.0" <?php selected( $this->settings['version'], '7.0.0' ); ?>>7.0.0</option> |
| 515 |
<option value="6.7.1" <?php selected( $this->settings['version'], '6.7.1' ); ?>>6.7.1</option> |
| 516 |
<option value="6.7.0" <?php selected( $this->settings['version'], '6.7.0' ); ?>>6.7.0</option> |
| 517 |
<option value="6.6.0" <?php selected( $this->settings['version'], '6.6.0' ); ?>>6.6.0</option> |
| 518 |
<option value="6.5.2" <?php selected( $this->settings['version'], '6.5.2' ); ?>>6.5.2</option> |
| 519 |
<option value="6.5.1" <?php selected( $this->settings['version'], '6.5.1' ); ?>>6.5.1</option> |
| 520 |
<option value="6.5.0" <?php selected( $this->settings['version'], '6.5.0' ); ?>>6.5.0</option> |
| 521 |
<option value="6.4.2" <?php selected( $this->settings['version'], '6.4.2' ); ?>>6.4.2</option> |
| 522 |
<option value="6.1.0" <?php selected( $this->settings['version'], '6.1.0' ); ?>>6.1.0</option> |
| 523 |
<option value="6.0.0" <?php selected( $this->settings['version'], '6.0.0' ); ?>>6.0.0</option> |
| 524 |
<option value="5.15.4" <?php selected( $this->settings['version'], '5.15.4' ); ?>>5.15.4</option> |
| 525 |
<option value="5.6.0" <?php selected( $this->settings['version'], '5.6.0' ); ?>>5.6.0</option> |
| 526 |
<option value="5.5.0" <?php selected( $this->settings['version'], '5.5.0' ); ?>>5.5.0</option> |
| 527 |
<option value="5.4.0" <?php selected( $this->settings['version'], '5.4.0' ); ?>>5.4.0</option> |
| 528 |
<option value="5.3.0" <?php selected( $this->settings['version'], '5.3.0' ); ?>>5.3.0</option> |
| 529 |
<option value="5.2.0" <?php selected( $this->settings['version'], '5.2.0' ); ?>>5.2.0</option> |
| 530 |
<option value="5.1.0" <?php selected( $this->settings['version'], '5.1.0' ); ?>>5.1.0</option> |
| 531 |
<option value="4.7.0" <?php selected( $this->settings['version'], '4.7.0' ); ?>>4.7.1 (CSS only)</option> |
| 532 |
</select> |
| 533 |
</td> |
| 534 |
</tr> |
| 535 |
|
| 536 |
<tr valign="top"> |
| 537 |
<th scope="row"><label for="wpfas-enqueue"><?php _e( 'Enqueue', 'ayecode-connect' ); ?></label></th> |
| 538 |
<td> |
| 539 |
<select name="wp-font-awesome-settings[enqueue]" id="wpfas-enqueue"> |
| 540 |
<option value="" <?php selected( $this->settings['enqueue'], '' ); ?>><?php _e( 'Frontend + Backend (default)', 'ayecode-connect' ); ?></option> |
| 541 |
<option value="frontend" <?php selected( $this->settings['enqueue'], 'frontend' ); ?>><?php _e( 'Frontend', 'ayecode-connect' ); ?></option> |
| 542 |
<option value="backend" <?php selected( $this->settings['enqueue'], 'backend' ); ?>><?php _e( 'Backend', 'ayecode-connect' ); ?></option> |
| 543 |
</select> |
| 544 |
</td> |
| 545 |
</tr> |
| 546 |
|
| 547 |
<tr valign="top" class="wpfas-kit-hide"> |
| 548 |
<th scope="row"><label |
| 549 |
for="wpfas-pro"><?php _e( 'Enable pro', 'ayecode-connect' ); ?></label></th> |
| 550 |
<td> |
| 551 |
<input type="hidden" name="wp-font-awesome-settings[pro]" value="0"/> |
| 552 |
<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')}"/> |
| 553 |
<span><?php |
| 554 |
echo wp_sprintf( |
| 555 |
__( 'Requires a subscription. %sLearn more%s %sManage my allowed domains%s', 'ayecode-connect' ), |
| 556 |
'<a rel="noopener noreferrer" target="_blank" href="https://fontawesome.com/referral?a=c9b89e1418">', |
| 557 |
' <i class="fas fa-external-link-alt"></i></a>', |
| 558 |
'<a rel="noopener noreferrer" target="_blank" href="https://fontawesome.com/account/cdn">', |
| 559 |
' <i class="fas fa-external-link-alt"></i></a>' |
| 560 |
); |
| 561 |
?></span> |
| 562 |
</td> |
| 563 |
</tr> |
| 564 |
|
| 565 |
<tr valign="top" class="wpfas-kit-hide wpfas-hide-pro"> |
| 566 |
<th scope="row"><label for="wpfas-local"><?php _e( 'Load Fonts Locally', 'ayecode-connect' ); ?></label></th> |
| 567 |
<td> |
| 568 |
<input type="hidden" name="wp-font-awesome-settings[local]" value="0"/> |
| 569 |
<input type="hidden" name="wp-font-awesome-settings[local_version]" value="<?php echo esc_attr( $this->settings['local_version'] ); ?>"/> |
| 570 |
<input type="checkbox" name="wp-font-awesome-settings[local]" value="1" <?php checked( $this->settings['local'], '1' ); ?> id="wpfas-local"/> |
| 571 |
<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> |
| 572 |
</td> |
| 573 |
</tr> |
| 574 |
|
| 575 |
<tr valign="top" class="wpfas-kit-hide"> |
| 576 |
<th scope="row"><label |
| 577 |
for="wpfas-shims"><?php _e( 'Enable v4 shims compatibility', 'ayecode-connect' ); ?></label> |
| 578 |
</th> |
| 579 |
<td> |
| 580 |
<input type="hidden" name="wp-font-awesome-settings[shims]" value="0"/> |
| 581 |
<input type="checkbox" name="wp-font-awesome-settings[shims]" |
| 582 |
value="1" <?php checked( $this->settings['shims'], '1' ); ?> id="wpfas-shims"/> |
| 583 |
<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> |
| 584 |
</td> |
| 585 |
</tr> |
| 586 |
|
| 587 |
<tr valign="top" class="wpfas-kit-hide"> |
| 588 |
<th scope="row"><label |
| 589 |
for="wpfas-js-pseudo"><?php _e( 'Enable JS pseudo elements (not recommended)', 'ayecode-connect' ); ?></label> |
| 590 |
</th> |
| 591 |
<td> |
| 592 |
<input type="hidden" name="wp-font-awesome-settings[js-pseudo]" value="0"/> |
| 593 |
<input type="checkbox" name="wp-font-awesome-settings[js-pseudo]" |
| 594 |
value="1" <?php checked( $this->settings['js-pseudo'], '1' ); ?> |
| 595 |
id="wpfas-js-pseudo"/> |
| 596 |
<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> |
| 597 |
</td> |
| 598 |
</tr> |
| 599 |
|
| 600 |
<tr valign="top"> |
| 601 |
<th scope="row"><label |
| 602 |
for="wpfas-dequeue"><?php _e( 'Dequeue', 'ayecode-connect' ); ?></label></th> |
| 603 |
<td> |
| 604 |
<input type="hidden" name="wp-font-awesome-settings[dequeue]" value="0"/> |
| 605 |
<input type="checkbox" name="wp-font-awesome-settings[dequeue]" |
| 606 |
value="1" <?php checked( $this->settings['dequeue'], '1' ); ?> |
| 607 |
id="wpfas-dequeue"/> |
| 608 |
<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> |
| 609 |
</td> |
| 610 |
</tr> |
| 611 |
|
| 612 |
</table> |
| 613 |
<div class="fas-buttons"> |
| 614 |
<?php |
| 615 |
submit_button(); |
| 616 |
?> |
| 617 |
<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> |
| 618 |
|
| 619 |
</div> |
| 620 |
</form> |
| 621 |
|
| 622 |
<div id="wpfas-version"><?php echo wp_sprintf(__( 'Version: %s (affiliate links provided)', 'ayecode-connect' ), $this->version ); ?></div> |
| 623 |
</div> |
| 624 |
<?php |
| 625 |
} |
| 626 |
} |
| 627 |
|
| 628 |
/** |
| 629 |
* Check a version number is valid and if so return it or else return an empty string. |
| 630 |
* |
| 631 |
* @param $version string The version number to check. |
| 632 |
* |
| 633 |
* @since 1.0.6 |
| 634 |
* |
| 635 |
* @return string Either a valid version number or an empty string. |
| 636 |
*/ |
| 637 |
public function validate_version_number( $version ) { |
| 638 |
|
| 639 |
if ( version_compare( $version, '0.0.1', '>=' ) >= 0 ) { |
| 640 |
// valid |
| 641 |
} else { |
| 642 |
$version = '';// not validated |
| 643 |
} |
| 644 |
|
| 645 |
return $version; |
| 646 |
} |
| 647 |
|
| 648 |
|
| 649 |
/** |
| 650 |
* Get the latest version of Font Awesome. |
| 651 |
* |
| 652 |
* 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. |
| 653 |
* |
| 654 |
* @since 1.0.7 |
| 655 |
* @return mixed|string The latest version number found. |
| 656 |
*/ |
| 657 |
public function get_latest_version( $force_api = false, $force_latest = false, $skip_download = false ) { |
| 658 |
$latest_version = $this->latest; |
| 659 |
|
| 660 |
$cache = get_transient( 'wp-font-awesome-settings-version' ); |
| 661 |
|
| 662 |
if ( $cache === false || $force_api ) { // its not set |
| 663 |
$api_ver = $this->get_latest_version_from_api(); |
| 664 |
if ( version_compare( $api_ver, $this->latest, '>=' ) >= 0 ) { |
| 665 |
$latest_version = $api_ver; |
| 666 |
set_transient( 'wp-font-awesome-settings-version', $api_ver, 48 * HOUR_IN_SECONDS ); |
| 667 |
} |
| 668 |
} elseif ( $this->validate_version_number( $cache ) ) { |
| 669 |
if ( version_compare( $cache, $this->latest, '>=' ) >= 0 ) { |
| 670 |
$latest_version = $cache; |
| 671 |
} |
| 672 |
} |
| 673 |
|
| 674 |
// @todo remove after FA7 compatibility |
| 675 |
if ( ! $force_latest && version_compare( $cache, '7.0.0', '>=' ) >= 0 ) { |
| 676 |
$latest_version = '6.7.2'; |
| 677 |
} |
| 678 |
|
| 679 |
// Check and auto download fonts locally. |
| 680 |
if ( ! $skip_download && empty( $this->settings['pro'] ) && empty( $this->settings['version'] ) && $this->settings['type'] != 'KIT' && ! empty( $this->settings['local'] ) && ! empty( $this->settings['local_version'] ) && ! empty( $latest_version ) ) { |
| 681 |
if ( version_compare( $latest_version, $this->settings['local_version'], '>' ) && is_admin() && ! wp_doing_ajax() ) { |
| 682 |
$this->download_package( $latest_version ); |
| 683 |
} |
| 684 |
} |
| 685 |
|
| 686 |
return $latest_version; |
| 687 |
} |
| 688 |
|
| 689 |
/** |
| 690 |
* Get the latest Font Awesome version from the github API. |
| 691 |
* |
| 692 |
* @since 1.0.7 |
| 693 |
* @return string The latest version number or `0` on API fail. |
| 694 |
*/ |
| 695 |
public function get_latest_version_from_api() { |
| 696 |
$version = "0"; |
| 697 |
$response = wp_remote_get( "https://api.github.com/repos/FortAwesome/Font-Awesome/releases/latest" ); |
| 698 |
if ( ! is_wp_error( $response ) && is_array( $response ) ) { |
| 699 |
$api_response = json_decode( wp_remote_retrieve_body( $response ), true ); |
| 700 |
if ( isset( $api_response['tag_name'] ) && version_compare( $api_response['tag_name'], $this->latest, '>=' ) >= 0 && empty( $api_response['prerelease'] ) ) { |
| 701 |
$version = $api_response['tag_name']; |
| 702 |
} |
| 703 |
} |
| 704 |
|
| 705 |
return $version; |
| 706 |
} |
| 707 |
|
| 708 |
/** |
| 709 |
* Inline CSS for RTL language support. |
| 710 |
* |
| 711 |
* @since 1.0.13 |
| 712 |
* @return string Inline CSS. |
| 713 |
*/ |
| 714 |
public function rtl_inline_css() { |
| 715 |
$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}'; |
| 716 |
|
| 717 |
return $inline_css; |
| 718 |
} |
| 719 |
|
| 720 |
/** |
| 721 |
* Show any warnings as an admin notice. |
| 722 |
* |
| 723 |
* @return void |
| 724 |
*/ |
| 725 |
public function admin_notices() { |
| 726 |
$settings = $this->settings; |
| 727 |
|
| 728 |
if ( defined( 'FONTAWESOME_PLUGIN_FILE' ) ) { |
| 729 |
if ( ! empty( $_REQUEST['page'] ) && $_REQUEST['page'] == 'wp-font-awesome-settings' ) { |
| 730 |
?> |
| 731 |
<div class="notice notice-error is-dismissible"> |
| 732 |
<p><?php _e( 'The Official Font Awesome Plugin is active, please adjust your settings there.', 'ayecode-connect' ); ?></p> |
| 733 |
</div> |
| 734 |
<?php |
| 735 |
} |
| 736 |
} else { |
| 737 |
if ( ! empty( $settings ) ) { |
| 738 |
if ( $settings['type'] != 'KIT' && $settings['pro'] && ( $settings['version'] == '' || version_compare( $settings['version'], '6', '>=' ) ) ) { |
| 739 |
$link = admin_url('options-general.php?page=wp-font-awesome-settings'); |
| 740 |
?> |
| 741 |
<div class="notice notice-error is-dismissible"> |
| 742 |
<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> |
| 743 |
</div> |
| 744 |
<?php |
| 745 |
} |
| 746 |
} |
| 747 |
} |
| 748 |
} |
| 749 |
|
| 750 |
/** |
| 751 |
* Handle fontawesome add settings to download fontawesome to store locally. |
| 752 |
* |
| 753 |
* @since 1.1.1 |
| 754 |
* |
| 755 |
* @param string $option The option name. |
| 756 |
* @param mixed $value The option value. |
| 757 |
*/ |
| 758 |
public function add_option_wp_font_awesome_settings( $option, $value ) { |
| 759 |
// Do nothing if WordPress is being installed. |
| 760 |
if ( wp_installing() ) { |
| 761 |
return; |
| 762 |
} |
| 763 |
|
| 764 |
if ( ! empty( $value['local'] ) && empty( $value['pro'] ) && ! ( ! empty( $value['type'] ) && $value['type'] == 'KIT' ) ) { |
| 765 |
$version = isset( $value['version'] ) && $value['version'] ? $value['version'] : $this->get_latest_version(); |
| 766 |
|
| 767 |
if ( ! empty( $version ) ) { |
| 768 |
$response = $this->download_package( $version, $value ); |
| 769 |
|
| 770 |
if ( is_wp_error( $response ) ) { |
| 771 |
add_settings_error( 'general', 'fontawesome_download', __( 'ERROR:', 'ayecode-connect' ) . ' ' . $response->get_error_message(), 'error' ); |
| 772 |
} |
| 773 |
} |
| 774 |
} |
| 775 |
} |
| 776 |
|
| 777 |
/** |
| 778 |
* Handle fontawesome update settings to download fontawesome to store locally. |
| 779 |
* |
| 780 |
* @since 1.1.0 |
| 781 |
* |
| 782 |
* @param mixed $old_value The old option value. |
| 783 |
* @param mixed $value The new option value. |
| 784 |
*/ |
| 785 |
public function update_option_wp_font_awesome_settings( $old_value, $new_value ) { |
| 786 |
// Do nothing if WordPress is being installed. |
| 787 |
if ( wp_installing() ) { |
| 788 |
return; |
| 789 |
} |
| 790 |
|
| 791 |
if ( ! empty( $new_value['local'] ) && empty( $new_value['pro'] ) && ! ( ! empty( $new_value['type'] ) && $new_value['type'] == 'KIT' ) ) { |
| 792 |
// Old values |
| 793 |
$old_version = isset( $old_value['version'] ) && $old_value['version'] ? $old_value['version'] : ( isset( $old_value['local_version'] ) ? $old_value['local_version'] : '' ); |
| 794 |
$old_local = isset( $old_value['local'] ) ? (int) $old_value['local'] : 0; |
| 795 |
|
| 796 |
// New values |
| 797 |
$new_version = isset( $new_value['version'] ) && $new_value['version'] ? $new_value['version'] : $this->get_latest_version(); |
| 798 |
|
| 799 |
if ( empty( $old_local ) || $old_version !== $new_version || ! file_exists( $this->get_fonts_dir() . 'css' . DIRECTORY_SEPARATOR . 'all.css' ) ) { |
| 800 |
$response = $this->download_package( $new_version, $new_value ); |
| 801 |
|
| 802 |
if ( is_wp_error( $response ) ) { |
| 803 |
add_settings_error( 'general', 'fontawesome_download', __( 'ERROR:', 'ayecode-connect' ) . ' ' . $response->get_error_message(), 'error' ); |
| 804 |
} |
| 805 |
} |
| 806 |
} |
| 807 |
} |
| 808 |
|
| 809 |
/** |
| 810 |
* Get the fonts directory local path. |
| 811 |
* |
| 812 |
* @since 1.1.0 |
| 813 |
* |
| 814 |
* @param string Fonts directory local path. |
| 815 |
*/ |
| 816 |
public function get_fonts_dir() { |
| 817 |
$upload_dir = wp_upload_dir( null, false ); |
| 818 |
|
| 819 |
return $upload_dir['basedir'] . DIRECTORY_SEPARATOR . 'ayefonts' . DIRECTORY_SEPARATOR . 'fa' . DIRECTORY_SEPARATOR; |
| 820 |
} |
| 821 |
|
| 822 |
/** |
| 823 |
* Get the fonts directory local url. |
| 824 |
* |
| 825 |
* @since 1.1.0 |
| 826 |
* |
| 827 |
* @param string Fonts directory local url. |
| 828 |
*/ |
| 829 |
public function get_fonts_url() { |
| 830 |
$upload_dir = wp_upload_dir( null, false ); |
| 831 |
|
| 832 |
return $upload_dir['baseurl'] . '/ayefonts/fa/'; |
| 833 |
} |
| 834 |
|
| 835 |
/** |
| 836 |
* Check whether load locally active. |
| 837 |
* |
| 838 |
* @since 1.1.0 |
| 839 |
* |
| 840 |
* @return bool True if active else false. |
| 841 |
*/ |
| 842 |
public function has_local() { |
| 843 |
if ( ! empty( $this->settings['local'] ) && empty( $this->settings['pro'] ) && file_exists( $this->get_fonts_dir() . 'css' . DIRECTORY_SEPARATOR . 'all.css' ) ) { |
| 844 |
return true; |
| 845 |
} |
| 846 |
|
| 847 |
return false; |
| 848 |
} |
| 849 |
|
| 850 |
/** |
| 851 |
* Get the WP Filesystem access. |
| 852 |
* |
| 853 |
* @since 1.1.0 |
| 854 |
* |
| 855 |
* @return object The WP Filesystem. |
| 856 |
*/ |
| 857 |
public function get_wp_filesystem() { |
| 858 |
if ( ! function_exists( 'get_filesystem_method' ) ) { |
| 859 |
require_once( ABSPATH . "/wp-admin/includes/file.php" ); |
| 860 |
} |
| 861 |
|
| 862 |
$access_type = get_filesystem_method(); |
| 863 |
|
| 864 |
if ( $access_type === 'direct' ) { |
| 865 |
/* You can safely run request_filesystem_credentials() without any issues and don't need to worry about passing in a URL */ |
| 866 |
$creds = request_filesystem_credentials( trailingslashit( site_url() ) . 'wp-admin/', '', false, false, array() ); |
| 867 |
|
| 868 |
/* Initialize the API */ |
| 869 |
if ( ! WP_Filesystem( $creds ) ) { |
| 870 |
/* Any problems and we exit */ |
| 871 |
return false; |
| 872 |
} |
| 873 |
|
| 874 |
global $wp_filesystem; |
| 875 |
|
| 876 |
return $wp_filesystem; |
| 877 |
/* Do our file manipulations below */ |
| 878 |
} else if ( defined( 'FTP_USER' ) ) { |
| 879 |
$creds = request_filesystem_credentials( trailingslashit( site_url() ) . 'wp-admin/', '', false, false, array() ); |
| 880 |
|
| 881 |
/* Initialize the API */ |
| 882 |
if ( ! WP_Filesystem( $creds ) ) { |
| 883 |
/* Any problems and we exit */ |
| 884 |
return false; |
| 885 |
} |
| 886 |
|
| 887 |
global $wp_filesystem; |
| 888 |
|
| 889 |
return $wp_filesystem; |
| 890 |
} else { |
| 891 |
/* Don't have direct write access. Prompt user with our notice */ |
| 892 |
return false; |
| 893 |
} |
| 894 |
} |
| 895 |
|
| 896 |
/** |
| 897 |
* Download the fontawesome package file. |
| 898 |
* |
| 899 |
* @since 1.1.0 |
| 900 |
* |
| 901 |
* @param mixed $version The font awesome. |
| 902 |
* @param array $option Fontawesome settings. |
| 903 |
* @return WP_ERROR|bool Error on fail and true on success. |
| 904 |
*/ |
| 905 |
public function download_package( $version, $option = array() ) { |
| 906 |
$filename = 'fontawesome-free-' . $version . '-web'; |
| 907 |
$url = 'https://use.fontawesome.com/releases/v' . $version . '/' . $filename . '.zip'; |
| 908 |
|
| 909 |
if ( ! function_exists( 'wp_handle_upload' ) ) { |
| 910 |
require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 911 |
} |
| 912 |
|
| 913 |
$download_file = download_url( esc_url_raw( $url ) ); |
| 914 |
|
| 915 |
if ( is_wp_error( $download_file ) ) { |
| 916 |
return new WP_Error( 'fontawesome_download_failed', __( $download_file->get_error_message(), 'ayecode-connect' ) ); |
| 917 |
} else if ( empty( $download_file ) ) { |
| 918 |
return new WP_Error( 'fontawesome_download_failed', __( 'Something went wrong in downloading the font awesome to store locally.', 'ayecode-connect' ) ); |
| 919 |
} |
| 920 |
|
| 921 |
$response = $this->extract_package( $download_file, $filename, true ); |
| 922 |
|
| 923 |
// Update local version. |
| 924 |
if ( is_wp_error( $response ) ) { |
| 925 |
return $response; |
| 926 |
} else if ( $response ) { |
| 927 |
if ( empty( $option ) ) { |
| 928 |
$option = get_option( 'wp-font-awesome-settings' ); |
| 929 |
} |
| 930 |
|
| 931 |
$option['local_version'] = $version; |
| 932 |
|
| 933 |
// Remove action to prevent looping. |
| 934 |
remove_action( 'update_option_wp-font-awesome-settings', array( $this, 'update_option_wp_font_awesome_settings' ), 10, 2 ); |
| 935 |
|
| 936 |
update_option( 'wp-font-awesome-settings', $option ); |
| 937 |
|
| 938 |
return true; |
| 939 |
} |
| 940 |
|
| 941 |
return false; |
| 942 |
} |
| 943 |
|
| 944 |
/** |
| 945 |
* Extract the fontawesome package file. |
| 946 |
* |
| 947 |
* @since 1.1.0 |
| 948 |
* |
| 949 |
* @param string $package The package file path. |
| 950 |
* @param string $dirname Package file name. |
| 951 |
* @param bool $delete_package Delete temp file or not. |
| 952 |
* @return WP_Error|bool True on success WP_Error on fail. |
| 953 |
*/ |
| 954 |
public function extract_package( $package, $dirname = '', $delete_package = false ) { |
| 955 |
global $wp_filesystem; |
| 956 |
|
| 957 |
$wp_filesystem = $this->get_wp_filesystem(); |
| 958 |
|
| 959 |
if ( empty( $wp_filesystem ) && isset( $wp_filesystem->errors ) && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->get_error_code() ) { |
| 960 |
return new WP_Error( 'fontawesome_filesystem_error', __( $wp_filesystem->errors->get_error_message(), 'ayecode-connect' ) ); |
| 961 |
} else if ( empty( $wp_filesystem ) ) { |
| 962 |
return new WP_Error( 'fontawesome_filesystem_error', __( 'Failed to initialise WP_Filesystem while trying to download the Font Awesome package.', 'ayecode-connect' ) ); |
| 963 |
} |
| 964 |
|
| 965 |
$fonts_dir = $this->get_fonts_dir(); |
| 966 |
$fonts_tmp_dir = dirname( $fonts_dir ) . DIRECTORY_SEPARATOR . 'fa-tmp' . DIRECTORY_SEPARATOR; |
| 967 |
|
| 968 |
if ( $wp_filesystem->is_dir( $fonts_tmp_dir ) ) { |
| 969 |
$wp_filesystem->delete( $fonts_tmp_dir, true ); |
| 970 |
} |
| 971 |
|
| 972 |
// Unzip package to working directory. |
| 973 |
$result = unzip_file( $package, $fonts_tmp_dir ); |
| 974 |
|
| 975 |
if ( is_wp_error( $result ) ) { |
| 976 |
$wp_filesystem->delete( $fonts_tmp_dir, true ); |
| 977 |
|
| 978 |
if ( 'incompatible_archive' === $result->get_error_code() ) { |
| 979 |
return new WP_Error( 'fontawesome_incompatible_archive', __( $result->get_error_message(), 'ayecode-connect' ) ); |
| 980 |
} |
| 981 |
|
| 982 |
return $result; |
| 983 |
} |
| 984 |
|
| 985 |
if ( $wp_filesystem->is_dir( $fonts_dir ) ) { |
| 986 |
$wp_filesystem->delete( $fonts_dir, true ); |
| 987 |
} |
| 988 |
|
| 989 |
$extract_dir = $fonts_tmp_dir; |
| 990 |
|
| 991 |
if ( $dirname && $wp_filesystem->is_dir( $extract_dir . $dirname . DIRECTORY_SEPARATOR ) ) { |
| 992 |
$extract_dir .= $dirname . DIRECTORY_SEPARATOR; |
| 993 |
} |
| 994 |
|
| 995 |
try { |
| 996 |
$return = $wp_filesystem->move( $extract_dir, $fonts_dir, true ); |
| 997 |
} catch ( Exception $e ) { |
| 998 |
$return = new WP_Error( 'fontawesome_move_package', __( 'Fail to move font awesome package!', 'ayecode-connect' ) ); |
| 999 |
} |
| 1000 |
|
| 1001 |
if ( $wp_filesystem->is_dir( $fonts_tmp_dir ) ) { |
| 1002 |
$wp_filesystem->delete( $fonts_tmp_dir, true ); |
| 1003 |
} |
| 1004 |
|
| 1005 |
// Once extracted, delete the package if required. |
| 1006 |
if ( $delete_package ) { |
| 1007 |
unlink( $package ); |
| 1008 |
} |
| 1009 |
|
| 1010 |
return $return; |
| 1011 |
} |
| 1012 |
|
| 1013 |
/** |
| 1014 |
* Output the version in the header. |
| 1015 |
*/ |
| 1016 |
public function add_generator() { |
| 1017 |
$file = str_replace( array( "/", "\\" ), "/", realpath( __FILE__ ) ); |
| 1018 |
$plugins_dir = str_replace( array( "/", "\\" ), "/", realpath( WP_PLUGIN_DIR ) ); |
| 1019 |
|
| 1020 |
// Find source plugin/theme. |
| 1021 |
$source = array(); |
| 1022 |
if ( strpos( $file, $plugins_dir ) !== false ) { |
| 1023 |
$source = explode( "/", plugin_basename( $file ) ); |
| 1024 |
} else if ( function_exists( 'get_theme_root' ) ) { |
| 1025 |
$themes_dir = str_replace( array( "/", "\\" ), "/", realpath( get_theme_root() ) ); |
| 1026 |
|
| 1027 |
if ( strpos( $file, $themes_dir ) !== false ) { |
| 1028 |
$source = explode( "/", ltrim( str_replace( $themes_dir, "", $file ), "/" ) ); |
| 1029 |
} |
| 1030 |
} |
| 1031 |
|
| 1032 |
echo '<meta name="generator" content="WP Font Awesome Settings v' . esc_attr( $this->version ) . '"' . ( ! empty( $source[0] ) ? ' data-ac-source="' . esc_attr( $source[0] ) . '"' : '' ) . ' />'; |
| 1033 |
} |
| 1034 |
|
| 1035 |
/** |
| 1036 |
* Add extra parameters to the script tag. |
| 1037 |
* |
| 1038 |
* Add crossorigin="anonymous" to prevent OpaqueResponseBlocking |
| 1039 |
* (NS_BINDING_ABORTED) http error. |
| 1040 |
* |
| 1041 |
* @since 1.1.8 |
| 1042 |
* |
| 1043 |
* @param string $tag The script tag. |
| 1044 |
* @param string $handle The script handle. |
| 1045 |
* @param string $src The script url. |
| 1046 |
* @return string The script tag. |
| 1047 |
*/ |
| 1048 |
public function script_loader_tag( $tag, $handle, $src ) { |
| 1049 |
if ( ( $handle == 'font-awesome' || $handle == 'font-awesome-shims' ) && ( strpos( $src, "kit.fontawesome.com" ) !== false || strpos( $src, ".fontawesome.com/releases/" ) !== false ) ) { |
| 1050 |
$tag = preg_replace( |
| 1051 |
'/<script[\s]+(.*?)>/', |
| 1052 |
'<script defer crossorigin="anonymous" \1>', |
| 1053 |
$tag |
| 1054 |
); |
| 1055 |
} |
| 1056 |
|
| 1057 |
return $tag; |
| 1058 |
} |
| 1059 |
} |
| 1060 |
|
| 1061 |
/** |
| 1062 |
* Run the class if found. |
| 1063 |
*/ |
| 1064 |
WP_Font_Awesome_Settings::instance(); |
| 1065 |
} |
| 1066 |
|