1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
<?php
namespace SIW;
class Assets {
const JSCOOKIE_VERSION = '2.2.1';
const SMOOTHSCROLL_VERSION = '1.4.10';
const BALLOON_VERSION = '1.0.4';
const POLYFILL_VERSION = '3.52.1';
protected $polyfill_features = [
'default'
];
public static function init() {
$self = new self();
add_action( 'wp_enqueue_scripts', [ $self, 'register_styles' ] );
add_action( 'wp_enqueue_scripts', [ $self, 'register_scripts' ] );
add_filter( 'script_loader_tag', [ $self, 'set_crossorigin' ], 10, 2 );
add_filter( 'rocket_minify_excluded_external_js', [ $self, 'add_polyfill_url' ] );
}
public function register_styles() {
wp_register_style( 'siw', SIW_ASSETS_URL . 'css/siw.css', [], SIW_PLUGIN_VERSION );
wp_enqueue_style( 'siw' );
wp_register_style( 'balloon', SIW_ASSETS_URL . 'modules/balloon/balloon.css', [], self::BALLOON_VERSION );
wp_enqueue_style( 'balloon' );
}
public function register_scripts() {
wp_register_script( 'js-cookie', SIW_ASSETS_URL . 'modules/js-cookie/js.cookie.js', [], self::JSCOOKIE_VERSION, true );
wp_register_script( 'siw-svg', SIW_ASSETS_URL . 'js/siw-svg.js', [], SIW_PLUGIN_VERSION, true );
$polyfill_url = add_query_arg(
[
'version' => self::POLYFILL_VERSION,
'features' => implode( ',', $this->polyfill_features ),
'flags' => 'gated'
],
'https://polyfill.io/v3/polyfill.min.js'
);
wp_register_script( 'polyfill', $polyfill_url, [], null, true );
wp_script_add_data( 'polyfill', 'crossorigin', 'anonymous' );
wp_register_script( 'smoothscroll', SIW_ASSETS_URL . 'modules/smoothscroll/smoothscroll.js', [], self::SMOOTHSCROLL_VERSION, true );
wp_enqueue_script( 'smoothscroll' );
}
public function set_crossorigin( string $tag, string $handle ) {
$crossorigin = wp_scripts()->get_data( $handle, 'crossorigin' );
if ( $crossorigin ) {
$tag = str_replace(
'></',
sprintf( ' crossorigin="%s"></', esc_attr( $crossorigin ) ),
$tag
);
}
return $tag;
}
public function add_polyfill_url( array $urls ) {
$urls[] = 'https://polyfill.io';
return $urls;
}
}