| @@ -6,9 +6,9 @@ | ||
| 6 | 6 | |
| 7 | 7 | use WPCoder\Dashboard\DBManager; |
| 8 | 8 | use WPCoder\Dashboard\FolderManager; |
| 9 | 9 | use WPCoder\Optimization\Obfuscator; |
| 10 | -use WPCoder\WOW_Plugin; | |
| 10 | +use WPCoder\WPCoder; | |
| 11 | 11 | |
| 12 | 12 | class EnqueueScript { |
| 13 | 13 | |
| 14 | 14 | public static function init( $result ): void { |
| @@ -14,18 +14,16 @@ | ||
| 14 | 14 | public static function init( $result ): void { |
| 15 | 15 | $param = maybe_unserialize( $result->param ); |
| 16 | 16 | $js = wp_specialchars_decode( $result->js_code, ENT_QUOTES ); |
| 17 | 17 | |
| 18 | - if ( ! empty( $js ) && ! empty( $param['inline_js'] ) ) { | |
| 19 | - self::print_style( $js, $param, $result ); | |
| 20 | - } | |
| 18 | + self::include_scripts( $result ); | |
| 21 | 19 | |
| 22 | 20 | if ( ! empty( $js ) && empty( $param['inline_js'] ) ) { |
| 23 | 21 | self::include_script( $result, $param ); |
| 24 | 22 | } |
| 25 | 23 | |
| 26 | - self::include_scripts( $result ); | |
| 27 | 24 | |
| 25 | + | |
| 28 | 26 | add_filter( 'script_loader_tag', [ __CLASS__, 'add_attribute' ], 20, 2 ); |
| 29 | 27 | } |
| 30 | 28 | |
| 31 | 29 | public static function add_attribute( $tag, $handle ) { |
| @@ -37,13 +35,26 @@ | ||
| 37 | 35 | |
| 38 | 36 | return $tag; |
| 39 | 37 | } |
| 40 | 38 | |
| 41 | - private static function print_style( $js, $param, $result ): void { | |
| 39 | + public static function inline( $result ): void { | |
| 40 | + | |
| 42 | 41 | if ( is_admin() ) { |
| 43 | 42 | return; |
| 44 | 43 | } |
| 45 | 44 | |
| 45 | + $param = maybe_unserialize( $result->param ); | |
| 46 | + $js = wp_specialchars_decode( $result->js_code, ENT_QUOTES ); | |
| 47 | + | |
| 48 | + if ( empty( $param['inline_js'] ) ) { | |
| 49 | + return; | |
| 50 | + } | |
| 51 | + | |
| 52 | + if ( empty( $js ) ) { | |
| 53 | + return; | |
| 54 | + } | |
| 55 | + | |
| 56 | + | |
| 46 | 57 | $minified = ! empty( $param['minified_js'] ) ? $param['minified_js'] : 'obfuscate'; |
| 47 | 58 | |
| 48 | 59 | if ( $minified === 'obfuscate' ) { |
| 49 | 60 | $packer = new Obfuscator( $js, 'Normal', true, false ); |
| @@ -53,9 +64,9 @@ | ||
| 53 | 64 | $packer = new Obfuscator( $js, 'None', true, false ); |
| 54 | 65 | $js = $packer->pack(); |
| 55 | 66 | } |
| 56 | 67 | |
| 57 | - $slug = WOW_Plugin::SLUG . '-js-' . $result->id . '-inline'; | |
| 68 | + $slug = WPCoder::SLUG . '-js-' . $result->id . '-inline'; | |
| 58 | 69 | echo '<script id="' . esc_attr( $slug ) . '">' . $js . '</script>'; |
| 59 | 70 | } |
| 60 | 71 | |
| 61 | 72 | private static function include_script( $result, $param ): void { |
| @@ -61,10 +72,10 @@ | ||
| 61 | 72 | private static function include_script( $result, $param ): void { |
| 62 | 73 | $url = FolderManager::get_js_url( $result ); |
| 63 | 74 | $time = ! empty( $param['time'] ) ? $param['time'] : time(); |
| 64 | 75 | $dependency = ! empty( $param['jquery_dependency'] ) ? [] : [ 'jquery' ]; |
| 65 | - wp_enqueue_script( WOW_Plugin::SLUG . '-script-' . $result->id, $url, $dependency, | |
| 66 | - WOW_Plugin::VERSION . '_' . $time, true ); | |
| 76 | + wp_enqueue_script( WPCoder::SLUG . '-script-' . $result->id, $url, $dependency, | |
| 77 | + WPCoder::info('version') . '_' . $time, true ); | |
| 67 | 78 | } |
| 68 | 79 | |
| 69 | 80 | private static function include_scripts( $result ): void { |
| 70 | 81 | $scripts = self::get_scripts( $result ); |
| @@ -92,10 +103,10 @@ | ||
| 92 | 103 | for ( $i = 0; $i < $count; $i ++ ) { |
| 93 | 104 | if ( $param['include'][ $i ] === 'js' && ! empty( $param['include_file'][ $i ] ) ) { |
| 94 | 105 | $scripts[] = [ |
| 95 | 106 | 'url' => $param['include_file'][ $i ], |
| 96 | - 'slug' => WOW_Plugin::SLUG . '-' . $result->id . '-js-' . $i, | |
| 97 | - 'ver' => WOW_Plugin::VERSION . '_' . $time, | |
| 107 | + 'slug' => WPCoder::SLUG . '-' . $result->id . '-js-' . $i, | |
| 108 | + 'ver' => WPCoder::info('version') . '_' . $time, | |
| 98 | 109 | ]; |
| 99 | 110 | } |
| 100 | 111 | } |
| 101 | 112 | |
| @@ -115,9 +126,9 @@ | ||
| 115 | 126 | $id = $result->id; |
| 116 | 127 | $param = maybe_unserialize( $result->param ); |
| 117 | 128 | |
| 118 | 129 | if ( ! empty( $param['js_attributes'] ) && ! empty( $result->js_code ) ) { |
| 119 | - $scripts[ WOW_Plugin::SLUG . '-script-' . $id ] = $param['js_attributes']; | |
| 130 | + $scripts[ WPCoder::SLUG . '-script-' . $id ] = $param['js_attributes']; | |
| 120 | 131 | } |
| 121 | 132 | |
| 122 | 133 | |
| 123 | 134 | $count = ! empty( $param['include'] ) ? count( $param['include'] ) : 0; |
| @@ -129,9 +140,9 @@ | ||
| 129 | 140 | for ( $i = 0; $i < $count; $i ++ ) { |
| 130 | 141 | if ( $param['include'][ $i ] === 'js' && ! empty( $param['include_file'][ $i ] ) ) { |
| 131 | 142 | $attr = ! empty( $param['file_js_att'][ $i ] ) ? $param['file_js_att'][ $i ] : ''; |
| 132 | 143 | |
| 133 | - $scripts[ WOW_Plugin::SLUG . '-' . $id . '-js-' . $i ] = $attr; | |
| 144 | + $scripts[ WPCoder::SLUG . '-' . $id . '-js-' . $i ] = $attr; | |
| 134 | 145 | } |
| 135 | 146 | } |
| 136 | 147 | } |
| 137 | 148 | |