PluginProbe ʕ •ᴥ•ʔ
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments / 2.6.1
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments v2.6.1
4.5.1 4.5.0 4.4.2 4.4.1 4.4.0 4.3.3 4.3.2 4.3.1 4.3.0 4.2.3 4.2.2 4.2.1 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.10 1.1.11 1.1.12 1.1.13 1.1.14 1.1.15 1.1.16 1.1.17 1.1.18 1.1.19 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 1.1.9 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.11.0 1.11.1 1.11.2 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.7.0 1.7.1 1.7.2 1.8.0 1.8.1 1.8.2 1.8.3 1.8.4 1.8.5 1.9.0 1.9.1 1.9.2 1.9.3 1.9.4 1.9.5 2.0.0 2.0.1 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.10.0 2.10.1 2.11.0 2.11.1 2.11.2 2.11.3 2.11.4 2.12.0 2.13.0 2.14.0 2.14.1 2.15.0 2.15.1 2.16.0 2.16.1 2.16.2 2.16.3 2.17.0 2.17.1 2.17.2 2.18.0 2.19.0 2.19.2 2.19.3 2.19.4 2.2.0 2.2.1 2.20.0 2.20.1 2.20.2 2.20.3 2.20.4 2.20.5 2.20.6 2.21.0 2.22.0 2.22.1 2.23.0 2.24.0 2.25.0 2.25.1 2.25.2 2.26.0 2.27.0 2.27.1 2.28.0 2.29.0 2.29.1 2.29.2 2.29.3 2.29.4 2.3.0 2.3.1 2.30.0 2.31.0 2.31.1 2.31.2 2.31.3 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.40.0 2.40.1 2.5.0 2.5.1 2.5.2 2.6.0 2.6.1 2.6.2 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.8.0 2.8.1 2.8.2 2.8.3 2.8.4 2.9.0 3.0.0 3.0.0-RC1 3.0.0-RC2 3.0.0-beta1 3.0.0-beta2 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.10.0 3.10.1 3.11.0 3.12.0 3.13.0 3.13.1 3.13.2 3.13.3 3.13.4 3.14.0 3.15.0 3.15.1 3.15.2 3.15.3 3.15.4 3.15.5 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.16.5 3.16.6 3.16.7 3.16.8 3.17.0 3.17.1 3.17.2 3.17.3 3.17.4 3.17.5 3.17.6 3.18.0 3.19.0 3.19.1 3.19.2 3.2.0 3.2.1 3.2.2 3.20.0 3.20.1 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.4.3 3.5.0 3.5.1 3.5.2 3.5.3 3.6.0 3.6.1 3.6.2 3.7.0 3.7.1 3.7.2 3.7.3 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.9.0 4.0.0 4.0.1 4.0.2 4.0.3 trunk 4.1.0 0.2.19.1 4.1.1 1.0.0 4.2.0 1.0.1 1.0.2
surecart / app / src / WordPress / Assets / ScriptsService.php
surecart / app / src / WordPress / Assets Last commit date
AssetsService.php 3 years ago AssetsServiceProvider.php 3 years ago BlockAssetsLoadService.php 3 years ago PreloadService.php 3 years ago ScriptsService.php 2 years ago StylesService.php 3 years ago
ScriptsService.php
411 lines
1 <?php
2
3 namespace SureCart\WordPress\Assets;
4
5 use SureCart\Models\ManualPaymentMethod;
6 use SureCart\Models\Processor;
7
8 /**
9 * Handles the component theme.
10 */
11 class ScriptsService {
12 /**
13 * Holds the service container
14 *
15 * @var \Pimple\Container
16 */
17 protected $container;
18
19 /**
20 * Make sure we change the script loader tag for esm loading.
21 *
22 * @param \Pimple\Container $container Service Container.
23 */
24 public function __construct( $container ) {
25 $this->container = $container;
26
27 // add module to components tag.
28 add_filter( 'script_loader_tag', [ $this, 'componentsTag' ], 10, 3 );
29 }
30
31 /**
32 * Add module to the components tag
33 *
34 * @param string $tag Tag output.
35 * @param string $handle Script handle.
36 * @param string $source Script source.
37 *
38 * @return string
39 */
40 public function componentsTag( $tag, $handle, $source ) {
41 if ( 'surecart-components' !== $handle || ! $source ) {
42 return $tag;
43 }
44 // don't use javascript module if we are not using esm loader.
45 if ( ! \SureCart::assets()->usesEsmLoader() ) {
46 return $tag;
47 }
48
49 // make sure our translations do not get stripped.
50 $translations = wp_scripts()->print_translations( $handle, false );
51 if ( $translations ) {
52 $translations = sprintf( "<script%s id='%s-js-translations'>\n%s\n</script>\n", " type='text/javascript'", esc_attr( $handle ), $translations );
53 }
54
55 // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript
56 return '<script src="' . esc_url_raw( $source ) . '" type="module"></script>' . $translations;
57 }
58
59 /**
60 * Get the claim url.
61 *
62 * @return string
63 */
64 public function getAccountClaimUrl() {
65 return ! wp_doing_ajax() && is_admin() && ! \SureCart::account()->claimed ? \SureCart::routeUrl( 'account.claim' ) : '';
66 }
67
68 /**
69 * Register the component scripts and translations.
70 *
71 * @return void
72 */
73 public function register() {
74
75 // should we use the esm loader directly?
76 if ( ! is_admin() && \SureCart::assets()->usesEsmLoader() ) {
77 wp_register_script(
78 'surecart-components',
79 trailingslashit( \SureCart::core()->assets()->getUrl() ) . 'dist/components/surecart/surecart.esm.js',
80 [ 'wp-i18n' ],
81 filemtime( trailingslashit( $this->container[ SURECART_CONFIG_KEY ]['app_core']['path'] ) . 'dist/components/surecart/surecart.esm.js' ) . '-' . \SureCart::plugin()->version(),
82 false
83 );
84 } else {
85 // instead, use a static loader that injects the script at runtime.
86 $static_assets = include trailingslashit( $this->container[ SURECART_CONFIG_KEY ]['app_core']['path'] ) . 'dist/components/static-loader.asset.php';
87 wp_register_script(
88 'surecart-components',
89 trailingslashit( \SureCart::core()->assets()->getUrl() ) . 'dist/components/static-loader.js',
90 array_merge( [ 'wp-i18n' ], $static_assets['dependencies'] ?? [] ),
91 $static_assets['version'] . '-' . \SureCart::plugin()->version(),
92 true
93 );
94 wp_localize_script(
95 'surecart-components',
96 'surecartComponents',
97 [
98 'url' => trailingslashit( \SureCart::core()->assets()->getUrl() ) . 'dist/components/surecart/surecart.esm.js?ver=' . filemtime( trailingslashit( $this->container[ SURECART_CONFIG_KEY ]['app_core']['path'] ) . 'dist/components/surecart/surecart.esm.js' ),
99 ]
100 );
101 }
102
103 // wp_register_script(
104 // 'surecart-components',
105 // 'https://unpkg.com/@surecart/components@1.0.1-beta.1/dist/surecart/surecart.esm.js',
106 // [ 'wp-i18n' ],
107 // null,
108 // false
109 // );
110
111 wp_set_script_translations( 'surecart-components', 'surecart' );
112
113 wp_localize_script(
114 'surecart-components',
115 'scData',
116 apply_filters(
117 'surecart-components/scData',
118 [
119 'cdn_root' => SURECART_CDN_IMAGE_BASE,
120 'root_url' => esc_url_raw( get_rest_url() ),
121 'plugin_url' => \SureCart::core()->assets()->getUrl(),
122 'api_url' => \SureCart::requests()->getBaseUrl(),
123 'currency' => \SureCart::account()->currency,
124 'do_not_persist_cart' => is_admin(),
125 'theme' => get_option( 'surecart_theme', 'light' ),
126 'pages' => [
127 'dashboard' => \SureCart::pages()->url( 'dashboard' ),
128 'checkout' => \SureCart::pages()->url( 'checkout' ),
129 ],
130 'page_id' => get_the_ID(),
131 'nonce' => ( wp_installing() && ! is_multisite() ) ? '' : wp_create_nonce( 'wp_rest' ),
132 'nonce_endpoint' => admin_url( 'admin-ajax.php?action=sc-rest-nonce' ),
133 'recaptcha_site_key' => \SureCart::settings()->recaptcha()->getSiteKey(),
134 'claim_url' => $this->getAccountClaimUrl(),
135 'admin_url' => trailingslashit( admin_url() ),
136 'user_permissions' => array (
137 'manage_sc_shop_settings' => current_user_can( 'manage_sc_shop_settings' )
138 )
139 ]
140 )
141 );
142
143 wp_localize_script( 'surecart-components', 'scIcons', [ 'path' => esc_url_raw( plugin_dir_url( SURECART_PLUGIN_FILE ) . 'dist/icon-assets' ) ] );
144
145 // core-data.
146 $asset_file = include trailingslashit( $this->container[ SURECART_CONFIG_KEY ]['app_core']['path'] ) . 'dist/store/data.asset.php';
147 wp_register_script(
148 'sc-core-data',
149 trailingslashit( \SureCart::core()->assets()->getUrl() ) . 'dist/store/data.js',
150 array_merge( [ 'surecart-components' ], $asset_file['dependencies'] ?? [] ),
151 $asset_file['version'] . '-' . \SureCart::plugin()->version(),
152 true
153 );
154
155 // ui.
156 $asset_file = include trailingslashit( $this->container[ SURECART_CONFIG_KEY ]['app_core']['path'] ) . 'dist/store/ui.asset.php';
157 wp_register_script(
158 'sc-ui-data',
159 trailingslashit( \SureCart::core()->assets()->getUrl() ) . 'dist/store/ui.js',
160 array_merge( [ 'surecart-components' ], $asset_file['dependencies'] ?? [] ),
161 $asset_file['version'] . '-' . \SureCart::plugin()->version(),
162 true
163 );
164
165 $this->registerBlocks();
166
167 wp_localize_script( 'surecart-cart-blocks', 'scIcons', [ 'path' => esc_url_raw( plugin_dir_url( SURECART_PLUGIN_FILE ) . 'dist/icon-assets' ) ] );
168 $asset_file = include trailingslashit( $this->container[ SURECART_CONFIG_KEY ]['app_core']['path'] ) . 'dist/blocks/cart.asset.php';
169 $deps = $asset_file['dependencies'] ?? [];
170 // fix bug in deps array.
171 $deps[ array_search( 'wp-blockEditor', $deps ) ] = 'wp-block-editor';
172 wp_register_script(
173 'surecart-cart-blocks',
174 trailingslashit( \SureCart::core()->assets()->getUrl() ) . 'dist/blocks/cart.js',
175 array_merge( [ 'surecart-components' ], $deps ),
176 $asset_file['version'] . '-' . \SureCart::plugin()->version(),
177 true
178 );
179 wp_localize_script(
180 'surecart-cart-blocks',
181 'scBlockData',
182 [
183 'currency' => \SureCart::account()->currency,
184 'theme' => get_option( 'surecart_theme', 'light' ),
185 ]
186 );
187
188 wp_localize_script( 'surecart-cart-blocks', 'scIcons', [ 'path' => esc_url_raw( plugin_dir_url( SURECART_PLUGIN_FILE ) . 'dist/icon-assets' ) ] );
189
190 // cart.
191 $asset_file = include trailingslashit( $this->container[ SURECART_CONFIG_KEY ]['app_core']['path'] ) . 'dist/blocks/cart.asset.php';
192 $deps = $asset_file['dependencies'] ?? [];
193 // fix bug in deps array.
194 $deps[ array_search( 'wp-blockEditor', $deps ) ] = 'wp-block-editor';
195 wp_register_script(
196 'surecart-cart-blocks',
197 trailingslashit( \SureCart::core()->assets()->getUrl() ) . 'dist/blocks/cart.js',
198 array_merge( [ 'surecart-components' ], $deps ),
199 $asset_file['version'] . '-' . \SureCart::plugin()->version(),
200 true
201 );
202
203 wp_localize_script( 'surecart-cart-blocks', 'scIcons', [ 'path' => esc_url_raw( plugin_dir_url( SURECART_PLUGIN_FILE ) . 'dist/icon-assets' ) ] );
204
205 // regsiter recaptcha.
206 wp_register_script( 'surecart-google-recaptcha', 'https://www.google.com/recaptcha/api.js?render=' . \SureCart::settings()->recaptcha()->getSiteKey(), [], \SureCart::plugin()->version(), true );
207
208 // register stripe if enabled.
209 if ( get_option( 'surecart_load_stripe_js', false ) ) {
210 wp_enqueue_script( 'surecart-stripe-script', 'https://js.stripe.com/v3', [], \SureCart::plugin()->version(), false );
211 }
212
213 // templates.
214 $asset_file = include trailingslashit( $this->container[ SURECART_CONFIG_KEY ]['app_core']['path'] ) . 'dist/templates/admin.asset.php';
215 wp_register_script(
216 'surecart-templates-admin',
217 trailingslashit( \SureCart::core()->assets()->getUrl() ) . 'dist/templates/admin.js',
218 $asset_file['dependencies'],
219 $asset_file['version'],
220 true
221 );
222
223 // admin notices.
224 $asset_file = include trailingslashit( $this->container[ SURECART_CONFIG_KEY ]['app_core']['path'] ) . 'dist/styles/webhook-notice.asset.php';
225 wp_register_style(
226 'surecart-webhook-admin-notices',
227 trailingslashit( \SureCart::core()->assets()->getUrl() ) . 'dist/styles/webhook-notice.css',
228 $asset_file['dependencies'],
229 $asset_file['version']
230 );
231 }
232
233 /**
234 * Enqueue block front scripts.
235 *
236 * @return void
237 */
238 public function enqueueFront() {
239 // make sure it is registered.
240 $this->register();
241 // enqueue it.
242 wp_enqueue_script( 'surecart-components' );
243
244 // fix shitty jetpack issues key hijacking issues.
245 add_filter(
246 'wp_head',
247 function() {
248 wp_dequeue_script( 'wpcom-notes-common' );
249 wp_dequeue_script( 'wpcom-notes-admin-bar' );
250 wp_dequeue_style( 'wpcom-notes-admin-bar' );
251 wp_dequeue_style( 'noticons' );
252 },
253 200
254 );
255 }
256
257 /**
258 * Enqueue editor scripts.
259 *
260 * @return void
261 */
262 public function enqueueEditor() {
263 $this->enqueueBlocks();
264 $this->enqueuePageTemplateEditor();
265 $this->enqueueCartBlocks();
266 $this->enqueueProductBlocks();
267 }
268
269 /**
270 * Enqueue page templates.
271 *
272 * @return void
273 */
274 public function enqueuePageTemplateEditor() {
275 wp_enqueue_script( 'surecart-templates-admin' );
276 }
277
278 /**
279 * We only want these available in FSE.
280 *
281 * @return void
282 */
283 public function enqueueProductBlocks() {
284 global $pagenow;
285 if ( 'site-editor.php' !== $pagenow ) {
286 return;
287 }
288
289 wp_enqueue_script( 'surecart-product-blocks' );
290 }
291
292 /**
293 * Enqueue Cart Blocks.
294 *
295 * @return void
296 */
297 public function enqueueCartBlocks() {
298 // not our post type.
299 if ( 'sc_cart' !== get_post_type() ) {
300 return;
301 }
302 wp_enqueue_script( 'surecart-cart-blocks' );
303 }
304
305 /**
306 * Register block scripts.
307 *
308 * @return void
309 */
310 public function registerBlocks() {
311 $enabled_payment_processors = array_values(
312 array_filter(
313 (array) Processor::get() ?? [],
314 function( $payment_method ) {
315 return $payment_method->enabled ?? false;
316 }
317 )
318 );
319 // blocks.
320 $asset_file = include trailingslashit( $this->container[ SURECART_CONFIG_KEY ]['app_core']['path'] ) . 'dist/blocks/library.asset.php';
321 $deps = $asset_file['dependencies'] ?? [];
322 // fix bug in deps array.
323 $deps[ array_search( 'wp-blockEditor', $deps ) ] = 'wp-block-editor';
324 wp_register_script(
325 'surecart-blocks',
326 trailingslashit( \SureCart::core()->assets()->getUrl() ) . 'dist/blocks/library.js',
327 $deps,
328 $asset_file['version'] . '-' . \SureCart::plugin()->version(),
329 true
330 );
331
332 // only register
333 $asset_file = include trailingslashit( $this->container[ SURECART_CONFIG_KEY ]['app_core']['path'] ) . 'dist/blocks/product.asset.php';
334 $deps = $asset_file['dependencies'] ?? [];
335 // fix bug in deps array.
336 $deps[ array_search( 'wp-blockEditor', $deps ) ] = 'wp-block-editor';
337 wp_register_script(
338 'surecart-product-blocks',
339 trailingslashit( \SureCart::core()->assets()->getUrl() ) . 'dist/blocks/product.js',
340 $deps,
341 $asset_file['version'],
342 true
343 );
344
345 // localize.
346 wp_localize_script(
347 'surecart-blocks',
348 'scData',
349 apply_filters(
350 'surecart-components/scData',
351 [
352 'root_url' => esc_url_raw( get_rest_url() ),
353 'plugin_url' => \SureCart::core()->assets()->getUrl(),
354 'api_url' => \SureCart::requests()->getBaseUrl(),
355 'currency' => \SureCart::account()->currency,
356 'do_not_persist_cart' => is_admin(),
357 'theme' => get_option( 'surecart_theme', 'light' ),
358 'pages' => [
359 'dashboard' => \SureCart::pages()->url( 'dashboard' ),
360 'checkout' => \SureCart::pages()->url( 'checkout' ),
361 ],
362 'page_id' => get_the_ID(),
363 'nonce' => ( wp_installing() && ! is_multisite() ) ? '' : wp_create_nonce( 'wp_rest' ),
364 'nonce_endpoint' => admin_url( 'admin-ajax.php?action=sc-rest-nonce' ),
365 'recaptcha_site_key' => \SureCart::settings()->recaptcha()->getSiteKey(),
366 'claim_url' => $this->getAccountClaimUrl(),
367 'admin_url' => trailingslashit( admin_url() ),
368 'user_permissions' => array (
369 'manage_sc_shop_settings' => current_user_can( 'manage_sc_shop_settings' )
370 )
371 ]
372 )
373 );
374
375 wp_localize_script(
376 'surecart-blocks',
377 'scBlockData',
378 [
379 'root_url' => esc_url_raw( get_rest_url() ),
380 'nonce' => ( wp_installing() && ! is_multisite() ) ? '' : wp_create_nonce( 'wp_rest' ),
381 'nonce_endpoint' => admin_url( 'admin-ajax.php?action=sc-rest-nonce' ),
382 'processors' => $enabled_payment_processors,
383 'manualPaymentMethods' => (array) ManualPaymentMethod::get() ?? [],
384 'plugin_url' => \SureCart::core()->assets()->getUrl(),
385 'currency' => \SureCart::account()->currency,
386 'theme' => get_option( 'surecart_theme', 'light' ),
387 'entitlements' => \SureCart::account()->entitlements,
388 'upgrade_url' => \SureCart::config()->links->purchase,
389 'beta' => [
390 'stripe_payment_element' => (bool) get_option( 'sc_stripe_payment_element', false ),
391 ],
392 'pages' => [
393 'dashboard' => \SureCart::pages()->url( 'dashboard' ),
394 'checkout' => \SureCart::pages()->url( 'checkout' ),
395 ],
396 ]
397 );
398
399 wp_localize_script( 'surecart-blocks', 'scIcons', [ 'path' => esc_url_raw( plugin_dir_url( SURECART_PLUGIN_FILE ) . 'dist/icon-assets' ) ] );
400 }
401
402 /**
403 * Enqueue blocks scripts.
404 *
405 * @return void
406 */
407 public function enqueueBlocks() {
408 wp_enqueue_script( 'surecart-blocks' );
409 }
410 }
411