PluginProbe ʕ •ᴥ•ʔ
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments / 2.13.0
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments v2.13.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 2 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
452 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 // should we use the esm loader directly?
75 if ( ! is_admin() && \SureCart::assets()->usesEsmLoader() ) {
76 wp_register_script(
77 'surecart-components',
78 trailingslashit( \SureCart::core()->assets()->getUrl() ) . 'dist/components/surecart/surecart.esm.js',
79 [ 'wp-i18n' ],
80 filemtime( trailingslashit( $this->container[ SURECART_CONFIG_KEY ]['app_core']['path'] ) . 'dist/components/surecart/surecart.esm.js' ) . '-' . \SureCart::plugin()->version(),
81 false
82 );
83 } else {
84 // instead, use a static loader that injects the script at runtime.
85 $static_assets = include trailingslashit( $this->container[ SURECART_CONFIG_KEY ]['app_core']['path'] ) . 'dist/components/static-loader.asset.php';
86 wp_register_script(
87 'surecart-components',
88 trailingslashit( \SureCart::core()->assets()->getUrl() ) . 'dist/components/static-loader.js',
89 array_merge( [ 'wp-i18n' ], $static_assets['dependencies'] ?? [] ),
90 $static_assets['version'] . '-' . \SureCart::plugin()->version(),
91 true
92 );
93 wp_localize_script(
94 'surecart-components',
95 'surecartComponents',
96 [
97 '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' ),
98 ]
99 );
100 }
101
102 wp_set_script_translations( 'surecart-components', 'surecart' );
103
104 wp_localize_script(
105 'surecart-components',
106 'scData',
107 apply_filters(
108 'surecart-components/scData', // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores,WordPress.NamingConventions.ValidHookName.NotLowercase
109 [
110 'cdn_root' => SURECART_CDN_IMAGE_BASE,
111 'root_url' => esc_url_raw( get_rest_url() ),
112 'plugin_url' => \SureCart::core()->assets()->getUrl(),
113 'api_url' => \SureCart::requests()->getBaseUrl(),
114 'currency' => \SureCart::account()->currency,
115 'do_not_persist_cart' => is_admin(),
116 'theme' => get_option( 'surecart_theme', 'light' ),
117 'pages' => [
118 'dashboard' => \SureCart::pages()->url( 'dashboard' ),
119 'checkout' => \SureCart::pages()->url( 'checkout' ),
120 ],
121 'page_id' => get_the_ID(),
122 'nonce' => ( wp_installing() && ! is_multisite() ) ? '' : wp_create_nonce( 'wp_rest' ),
123 'nonce_endpoint' => admin_url( 'admin-ajax.php?action=sc-rest-nonce' ),
124 'recaptcha_site_key' => \SureCart::settings()->recaptcha()->getSiteKey(),
125 'claim_url' => $this->getAccountClaimUrl(),
126 'admin_url' => trailingslashit( admin_url() ),
127 'getting_started_url' => untrailingslashit( admin_url( 'admin.php?page=sc-getting-started' ) ),
128 'user_permissions' => array(
129 'manage_sc_shop_settings' => current_user_can( 'manage_sc_shop_settings' ),
130 ),
131 'is_account_connected' => \SureCart::account()->isConnected(),
132 ]
133 )
134 );
135
136 wp_localize_script( 'surecart-components', 'scIcons', [ 'path' => esc_url_raw( plugin_dir_url( SURECART_PLUGIN_FILE ) . 'dist/icon-assets' ) ] );
137
138 // core-data.
139 $asset_file = include trailingslashit( $this->container[ SURECART_CONFIG_KEY ]['app_core']['path'] ) . 'dist/store/data.asset.php';
140 wp_register_script(
141 'sc-core-data',
142 trailingslashit( \SureCart::core()->assets()->getUrl() ) . 'dist/store/data.js',
143 array_merge( [ 'surecart-components' ], $asset_file['dependencies'] ?? [] ),
144 $asset_file['version'] . '-' . \SureCart::plugin()->version(),
145 true
146 );
147
148 // ui.
149 $asset_file = include trailingslashit( $this->container[ SURECART_CONFIG_KEY ]['app_core']['path'] ) . 'dist/store/ui.asset.php';
150 wp_register_script(
151 'sc-ui-data',
152 trailingslashit( \SureCart::core()->assets()->getUrl() ) . 'dist/store/ui.js',
153 array_merge( [ 'surecart-components' ], $asset_file['dependencies'] ?? [] ),
154 $asset_file['version'] . '-' . \SureCart::plugin()->version(),
155 true
156 );
157
158 $this->registerBlocks();
159
160 wp_localize_script( 'surecart-cart-blocks', 'scIcons', [ 'path' => esc_url_raw( plugin_dir_url( SURECART_PLUGIN_FILE ) . 'dist/icon-assets' ) ] );
161 $asset_file = include trailingslashit( $this->container[ SURECART_CONFIG_KEY ]['app_core']['path'] ) . 'dist/blocks/cart.asset.php';
162 $deps = $asset_file['dependencies'] ?? [];
163 // fix bug in deps array.
164 $deps[ array_search( 'wp-blockEditor', $deps ) ] = 'wp-block-editor';
165 wp_register_script(
166 'surecart-cart-blocks',
167 trailingslashit( \SureCart::core()->assets()->getUrl() ) . 'dist/blocks/cart.js',
168 array_merge( [ 'surecart-components' ], $deps ),
169 $asset_file['version'] . '-' . \SureCart::plugin()->version(),
170 true
171 );
172 wp_localize_script(
173 'surecart-cart-blocks',
174 'scBlockData',
175 [
176 'currency' => \SureCart::account()->currency,
177 'theme' => get_option( 'surecart_theme', 'light' ),
178 ]
179 );
180
181 wp_localize_script( 'surecart-cart-blocks', 'scIcons', [ 'path' => esc_url_raw( plugin_dir_url( SURECART_PLUGIN_FILE ) . 'dist/icon-assets' ) ] );
182
183 // cart.
184 $asset_file = include trailingslashit( $this->container[ SURECART_CONFIG_KEY ]['app_core']['path'] ) . 'dist/blocks/cart.asset.php';
185 $deps = $asset_file['dependencies'] ?? [];
186 // fix bug in deps array.
187 $deps[ array_search( 'wp-blockEditor', $deps ) ] = 'wp-block-editor';
188 wp_register_script(
189 'surecart-cart-blocks',
190 trailingslashit( \SureCart::core()->assets()->getUrl() ) . 'dist/blocks/cart.js',
191 array_merge( [ 'surecart-components' ], $deps ),
192 $asset_file['version'] . '-' . \SureCart::plugin()->version(),
193 true
194 );
195
196 wp_localize_script( 'surecart-cart-blocks', 'scIcons', [ 'path' => esc_url_raw( plugin_dir_url( SURECART_PLUGIN_FILE ) . 'dist/icon-assets' ) ] );
197
198 // regsiter recaptcha.
199 wp_register_script( 'surecart-google-recaptcha', 'https://www.google.com/recaptcha/api.js?render=' . \SureCart::settings()->recaptcha()->getSiteKey(), [], \SureCart::plugin()->version(), true );
200
201 // register stripe if enabled.
202 if ( get_option( 'surecart_load_stripe_js', false ) ) {
203 wp_enqueue_script( 'surecart-stripe-script', 'https://js.stripe.com/v3', [], \SureCart::plugin()->version(), false );
204 }
205
206 // templates.
207 $asset_file = include trailingslashit( $this->container[ SURECART_CONFIG_KEY ]['app_core']['path'] ) . 'dist/templates/admin.asset.php';
208 wp_register_script(
209 'surecart-templates-admin',
210 trailingslashit( \SureCart::core()->assets()->getUrl() ) . 'dist/templates/admin.js',
211 $asset_file['dependencies'],
212 $asset_file['version'],
213 true
214 );
215
216 // admin notices.
217 $asset_file = include trailingslashit( $this->container[ SURECART_CONFIG_KEY ]['app_core']['path'] ) . 'dist/styles/webhook-notice.asset.php';
218 wp_register_style(
219 'surecart-webhook-admin-notices',
220 trailingslashit( \SureCart::core()->assets()->getUrl() ) . 'dist/styles/webhook-notice.css',
221 $asset_file['dependencies'],
222 $asset_file['version']
223 );
224
225 wp_register_script(
226 'surecart-affiliate-tracking',
227 esc_url_raw( untrailingslashit( SURECART_JS_URL ) . '/v1/affiliates' ),
228 [],
229 '1.1',
230 [
231 'strategy' => 'defer',
232 ]
233 );
234
235 wp_add_inline_script(
236 'surecart-affiliate-tracking',
237 'window.SureCartAffiliatesConfig = {
238 "publicToken": "' . \SureCart::account()->public_token . '",
239 "baseURL":"' . esc_url_raw( untrailingslashit( SURECART_API_URL ) ) . '/v1"
240 };',
241 'before'
242 );
243 }
244
245 /**
246 * Enqueue block front scripts.
247 *
248 * @return void
249 */
250 public function enqueueFront() {
251 // make sure it is registered.
252 $this->register();
253 // enqueue it.
254 wp_enqueue_script( 'surecart-components' );
255
256 // fix shitty jetpack issues key hijacking issues.
257 add_filter(
258 'wp_head',
259 function() {
260 wp_dequeue_script( 'wpcom-notes-common' );
261 wp_dequeue_script( 'wpcom-notes-admin-bar' );
262 wp_dequeue_style( 'wpcom-notes-admin-bar' );
263 wp_dequeue_style( 'noticons' );
264 },
265 200
266 );
267 }
268
269 /**
270 * Enqueue editor scripts.
271 *
272 * @return void
273 */
274 public function enqueueEditor() {
275 $this->enqueueBlocks();
276 $this->enqueuePageTemplateEditor();
277 $this->enqueueCartBlocks();
278 $this->enqueueProductBlocks();
279 $this->enqueueProductCollectionBlocks();
280 }
281
282 /**
283 * Enqueue page templates.
284 *
285 * @return void
286 */
287 public function enqueuePageTemplateEditor() {
288 wp_enqueue_script( 'surecart-templates-admin' );
289 }
290
291 /**
292 * We only want these available in FSE.
293 *
294 * @return void
295 */
296 public function enqueueProductBlocks() {
297 global $pagenow;
298 if ( 'site-editor.php' !== $pagenow ) {
299 return;
300 }
301
302 wp_enqueue_script( 'surecart-product-blocks' );
303 }
304
305 /**
306 * We only want these available in FSE.
307 *
308 * @return void
309 */
310 public function enqueueProductCollectionBlocks() {
311 global $pagenow;
312 if ( 'site-editor.php' !== $pagenow ) {
313 return;
314 }
315
316 wp_enqueue_script( 'surecart-product-collection-blocks' );
317 }
318
319 /**
320 * Enqueue Cart Blocks.
321 *
322 * @return void
323 */
324 public function enqueueCartBlocks() {
325 // not our post type.
326 if ( 'sc_cart' !== get_post_type() ) {
327 return;
328 }
329 wp_enqueue_script( 'surecart-cart-blocks' );
330 }
331
332 /**
333 * Register block scripts.
334 *
335 * @return void
336 */
337 public function registerBlocks() {
338 $enabled_payment_processors = array_values(
339 array_filter(
340 (array) Processor::get() ?? [],
341 function( $payment_method ) {
342 return $payment_method->enabled ?? false;
343 }
344 )
345 );
346 // blocks.
347 $asset_file = include trailingslashit( $this->container[ SURECART_CONFIG_KEY ]['app_core']['path'] ) . 'dist/blocks/library.asset.php';
348 $deps = $asset_file['dependencies'] ?? [];
349 // fix bug in deps array.
350 $deps[ array_search( 'wp-blockEditor', $deps ) ] = 'wp-block-editor';
351 wp_register_script(
352 'surecart-blocks',
353 trailingslashit( \SureCart::core()->assets()->getUrl() ) . 'dist/blocks/library.js',
354 $deps,
355 $asset_file['version'] . '-' . \SureCart::plugin()->version(),
356 true
357 );
358
359 // only register.
360 $asset_file = include trailingslashit( $this->container[ SURECART_CONFIG_KEY ]['app_core']['path'] ) . 'dist/blocks/product.asset.php';
361 $deps = $asset_file['dependencies'] ?? [];
362 // fix bug in deps array.
363 $deps[ array_search( 'wp-blockEditor', $deps ) ] = 'wp-block-editor';
364 wp_register_script(
365 'surecart-product-blocks',
366 trailingslashit( \SureCart::core()->assets()->getUrl() ) . 'dist/blocks/product.js',
367 $deps,
368 $asset_file['version'],
369 true
370 );
371
372 // Register product collection blocks.
373 $asset_file = include trailingslashit( $this->container[ SURECART_CONFIG_KEY ]['app_core']['path'] ) . 'dist/blocks/product_collection.asset.php';
374 $deps = $asset_file['dependencies'] ?? [];
375 $deps[ array_search( 'wp-blockEditor', $deps ) ] = 'wp-block-editor';
376 wp_register_script(
377 'surecart-product-collection-blocks',
378 trailingslashit( \SureCart::core()->assets()->getUrl() ) . 'dist/blocks/product_collection.js',
379 $deps,
380 $asset_file['version'],
381 true
382 );
383
384 // localize.
385 wp_localize_script(
386 'surecart-blocks',
387 'scData',
388 apply_filters(
389 'surecart-components/scData', // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores,WordPress.NamingConventions.ValidHookName.NotLowercase
390 [
391 'root_url' => esc_url_raw( get_rest_url() ),
392 'plugin_url' => \SureCart::core()->assets()->getUrl(),
393 'api_url' => \SureCart::requests()->getBaseUrl(),
394 'currency' => \SureCart::account()->currency,
395 'do_not_persist_cart' => is_admin(),
396 'theme' => get_option( 'surecart_theme', 'light' ),
397 'pages' => [
398 'dashboard' => \SureCart::pages()->url( 'dashboard' ),
399 'checkout' => \SureCart::pages()->url( 'checkout' ),
400 ],
401 'page_id' => get_the_ID(),
402 'nonce' => ( wp_installing() && ! is_multisite() ) ? '' : wp_create_nonce( 'wp_rest' ),
403 'nonce_endpoint' => admin_url( 'admin-ajax.php?action=sc-rest-nonce' ),
404 'recaptcha_site_key' => \SureCart::settings()->recaptcha()->getSiteKey(),
405 'claim_url' => $this->getAccountClaimUrl(),
406 'admin_url' => trailingslashit( admin_url() ),
407 'getting_started_url' => untrailingslashit( admin_url( 'admin.php?page=sc-getting-started' ) ),
408 'user_permissions' => array(
409 'manage_sc_shop_settings' => current_user_can( 'manage_sc_shop_settings' ),
410 ),
411 'is_account_connected' => \SureCart::account()->isConnected(),
412 ]
413 )
414 );
415
416 wp_localize_script(
417 'surecart-blocks',
418 'scBlockData',
419 [
420 'root_url' => esc_url_raw( get_rest_url() ),
421 'nonce' => ( wp_installing() && ! is_multisite() ) ? '' : wp_create_nonce( 'wp_rest' ),
422 'nonce_endpoint' => admin_url( 'admin-ajax.php?action=sc-rest-nonce' ),
423 'processors' => $enabled_payment_processors,
424 'manualPaymentMethods' => (array) ManualPaymentMethod::get() ?? [],
425 'plugin_url' => \SureCart::core()->assets()->getUrl(),
426 'currency' => \SureCart::account()->currency,
427 'theme' => get_option( 'surecart_theme', 'light' ),
428 'entitlements' => \SureCart::account()->entitlements,
429 'upgrade_url' => \SureCart::config()->links->purchase,
430 'beta' => [
431 'stripe_payment_element' => (bool) get_option( 'sc_stripe_payment_element', true ),
432 ],
433 'pages' => [
434 'dashboard' => \SureCart::pages()->url( 'dashboard' ),
435 'checkout' => \SureCart::pages()->url( 'checkout' ),
436 ],
437 ]
438 );
439
440 wp_localize_script( 'surecart-blocks', 'scIcons', [ 'path' => esc_url_raw( plugin_dir_url( SURECART_PLUGIN_FILE ) . 'dist/icon-assets' ) ] );
441 }
442
443 /**
444 * Enqueue blocks scripts.
445 *
446 * @return void
447 */
448 public function enqueueBlocks() {
449 wp_enqueue_script( 'surecart-blocks' );
450 }
451 }
452