PluginProbe
Cookie Consent – GDPR & CCPA Cookie Banner & Consent Manager / trunk
Cookie Consent – GDPR & CCPA Cookie Banner & Consent Manager vtrunk
0.0.11 0.0.10 0.0.9 0.0.8 0.0.7 0.0.6 trunk 0.0.1 0.0.2 0.0.3 0.0.4 0.0.5
cookiez / classes / utils / assets.php

assets.php in Cookie Consent – GDPR & CCPA Cookie Banner & Consent Manager trunk, at classes/utils/assets.php

202 lines 5.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Cookiez\Classes\Utils;
4
5 if ( ! defined( 'ABSPATH' ) ) {
6 exit; // Exit if accessed directly
7 }
8
9 /**
10 * Class Assets
11 */
12 class Assets {
13 private static ?array $chunks_manifest = null;
14
15 /**
16 * enqueue_scripts
17 *
18 * @param string $handle
19 * @param string $script_name
20 * @param array $dependencies
21 * @param string $version
22 * @param bool $footer
23 */
24 public static function enqueue_scripts( string $handle, string $script_name, array $dependencies = [], string $version = '', bool $footer = false ): void {
25 $asset_data = self::get_asset_version_and_suffix( $version );
26 wp_enqueue_script(
27 $handle,
28 self::get_assets_path( $script_name, 'js', $asset_data['suffix'] ),
29 $dependencies,
30 $asset_data['version'],
31 $footer
32 );
33 }
34
35 /**
36 * enqueue_styles
37 *
38 * @param string $handle
39 * @param string $style_name
40 * @param array $dependencies
41 * @param string $version
42 */
43 public static function enqueue_styles( string $handle, string $style_name, array $dependencies = [], string $version = '' ) {
44 $asset_data = self::get_asset_version_and_suffix( $version );
45 wp_enqueue_style(
46 $handle,
47 self::get_assets_path( $style_name, 'css', $asset_data['suffix'] ),
48 $dependencies,
49 $asset_data['version']
50 );
51 }
52
53 /**
54 * get_assets_version
55 *
56 * @param string $version
57 *
58 * @return string
59 */
60 private static function get_assets_version( string $version = '' ): string {
61 return empty( $version ) ? \COOKIEZ_VERSION : $version;
62 }
63
64 /**
65 * get_assets_suffix
66 * @return string
67 */
68 private static function get_assets_suffix(): string {
69 return defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
70 }
71
72 /**
73 * get_asset_version_and_suffix
74 *
75 * @param string $version
76 *
77 * @return array
78 */
79 private static function get_asset_version_and_suffix( string $version = '' ): array {
80 return [
81 'version' => self::get_assets_version( $version ),
82 'suffix' => self::get_assets_suffix(),
83 ];
84 }
85
86 /**
87 * get_assets_path
88 *
89 * @param string $asset_name
90 * @param string $asset_type
91 * @param string $suffix
92 *
93 * @return string
94 */
95 private static function get_assets_path( string $asset_name, string $asset_type, string $suffix = '' ): string {
96 return \COOKIEZ_ASSETS_URL . $asset_type . '/' . ( '' === $suffix ? 'dev/' : '' ) . $asset_name . $suffix . '.' . $asset_type;
97 }
98
99 /**
100 * enqueue_app_assets
101 *
102 * @param string $handle
103 * @param bool $with_css
104 */
105 public static function enqueue_app_assets( string $handle = '', bool $with_css = true ): void {
106 $dir = \COOKIEZ_ASSETS_PATH . 'build/';
107 $url = \COOKIEZ_ASSETS_URL . 'build/';
108
109 $script_asset_path = $dir . $handle . '.asset.php';
110 if ( ! file_exists( $script_asset_path ) ) {
111 throw new \Error(
112 'You need to run `npm start` or `npm run build` for the "' . esc_html( $handle ) . '" script first.'
113 );
114 }
115
116 // enqueue js
117 $script_asset = require $script_asset_path;
118 $chunk_handles = self::enqueue_vendor_chunks( $handle, $dir, $url, $script_asset['version'] );
119
120 wp_enqueue_script(
121 $handle,
122 $url . $handle . '.js',
123 array_merge( $script_asset['dependencies'], [ 'wp-util' ], $chunk_handles ),
124 $script_asset['version'],
125 true,
126 );
127
128 // add translation support
129 wp_set_script_translations( $handle, 'cookiez' );
130
131 if ( ! $with_css ) {
132 return;
133 }
134
135 // enqueue css
136 $css_file_name = $handle . '.css';
137 $css_version = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? filemtime( $dir . $css_file_name ) : \COOKIEZ_VERSION;
138 wp_enqueue_style(
139 $handle,
140 $url . $css_file_name,
141 [ 'wp-components' ],
142 $css_version
143 );
144 }
145
146 private static function enqueue_vendor_chunks( string $entry_handle, string $dir, string $url, string $version ): array {
147 $manifest = self::load_chunks_manifest( $dir );
148
149 if ( empty( $manifest[ $entry_handle ] ) ) {
150 return [];
151 }
152
153 $entry_file = $entry_handle . '.js';
154 $chunk_handles = [];
155
156 foreach ( $manifest[ $entry_handle ] as $chunk_file ) {
157 if ( $chunk_file === $entry_file ) {
158 continue;
159 }
160
161 $slug = basename( $chunk_file, '.js' );
162 $chunk_handle = 'cookiez-' . $entry_handle . '-chunk-' . $slug;
163
164 if ( wp_script_is( $chunk_handle, 'enqueued' ) ) {
165 $chunk_handles[] = $chunk_handle;
166 continue;
167 }
168
169 $chunk_deps = [];
170 $chunk_asset_path = $dir . $slug . '.asset.php';
171 if ( file_exists( $chunk_asset_path ) ) {
172 $chunk_asset = require $chunk_asset_path;
173 $chunk_deps = $chunk_asset['dependencies'] ?? [];
174 }
175
176 wp_enqueue_script( $chunk_handle, $url . $chunk_file, $chunk_deps, $version, true );
177 $chunk_handles[] = $chunk_handle;
178 }
179
180 return $chunk_handles;
181 }
182
183 private static function load_chunks_manifest( string $dir ): array {
184 if ( null !== self::$chunks_manifest ) {
185 return self::$chunks_manifest;
186 }
187
188 $manifest_path = $dir . 'chunks-manifest.json';
189 if ( ! file_exists( $manifest_path ) ) {
190 // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
191 error_log( 'Cookiez: chunks-manifest.json not found in build directory. Run `npm run build`.' );
192 self::$chunks_manifest = [];
193 return self::$chunks_manifest;
194 }
195
196 $manifest = json_decode( (string) file_get_contents( $manifest_path ), true );
197 self::$chunks_manifest = is_array( $manifest ) ? $manifest : [];
198
199 return self::$chunks_manifest;
200 }
201 }
202