PluginProbe ʕ •ᴥ•ʔ
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments / trunk
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments vtrunk
4.7.0 4.6.6 4.6.5 4.6.4 4.6.3 4.6.2 4.6.1 4.6.0 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 1 year ago AssetsServiceProvider.php 3 years ago BlockAssetsLoadService.php 1 year ago PreloadService.php 1 year ago ScriptsService.php 1 week ago StylesService.php 2 months ago
ScriptsService.php
455 lines
1 <?php
2
3 namespace SureCart\WordPress\Assets;
4
5 use SureCart\Models\ManualPaymentMethod;
6 use SureCart\Models\Processor;
7 use SureCart\Support\Currency;
8
9 /**
10 * Handles the component theme.
11 */
12 class ScriptsService {
13 /**
14 * Holds the service container
15 *
16 * @var \Pimple\Container
17 */
18 protected $container;
19
20 /**
21 * Make sure we change the script loader tag for esm loading.
22 *
23 * @param \Pimple\Container $container Service Container.
24 */
25 public function __construct( $container ) {
26 $this->container = $container;
27
28 // add module to components tag.
29 add_filter( 'script_loader_tag', [ $this, 'componentsTag' ], 10, 3 );
30 }
31
32 /**
33 * Add module to the components tag
34 *
35 * @param string $tag Tag output.
36 * @param string $handle Script handle.
37 * @param string $source Script source.
38 *
39 * @return string
40 */
41 public function componentsTag( $tag, $handle, $source ) {
42 if ( 'surecart-components' !== $handle || ! $source ) {
43 return $tag;
44 }
45 // don't use javascript module if we are not using esm loader.
46 if ( ! \SureCart::assets()->usesEsmLoader() ) {
47 return $tag;
48 }
49
50 // make sure our translations do not get stripped.
51 $translations = wp_scripts()->print_translations( $handle, false );
52 if ( $translations ) {
53 $translations = sprintf( "<script%s id='%s-js-translations'>\n%s\n</script>\n", " type='text/javascript'", esc_attr( $handle ), $translations );
54 }
55
56 // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript
57 return '<script src="' . esc_url_raw( $source ) . '" type="module"></script>' . $translations;
58 }
59
60 /**
61 * Get the claim url.
62 *
63 * @return string
64 */
65 public function getAccountClaimUrl() {
66 return ! wp_doing_ajax() && is_admin() && ! \SureCart::account()->claimed ? \SureCart::routeUrl( 'account.claim' ) : '';
67 }
68
69 /**
70 * Whether SureCart should automatically send Facebook Pixel (fbq) events.
71 *
72 * Stores running their own Pixel/CAPI stack can opt out to avoid double-tracking.
73 *
74 * @return bool
75 */
76 public function isFacebookTrackingEnabled() {
77 /**
78 * Filter whether SureCart automatically sends Facebook Pixel (fbq) events.
79 *
80 * @param bool $enabled Whether Facebook tracking is enabled. Default true.
81 */
82 return (bool) apply_filters( 'surecart/tracking/facebook/enabled', true );
83 }
84
85 /**
86 * Whether SureCart should automatically send Google Analytics (gtag/dataLayer) events.
87 *
88 * Stores running their own GA4/GTM stack can opt out to avoid double-tracking.
89 *
90 * @return bool
91 */
92 public function isGoogleTrackingEnabled() {
93 /**
94 * Filter whether SureCart automatically sends Google Analytics (gtag/dataLayer) events.
95 *
96 * @param bool $enabled Whether Google tracking is enabled. Default true.
97 */
98 return (bool) apply_filters( 'surecart/tracking/google/enabled', true );
99 }
100
101 /**
102 * Register the component scripts and translations.
103 *
104 * @return void
105 */
106 public function register() {
107 // should we use the esm loader directly?
108 if ( ! is_admin() && \SureCart::assets()->usesEsmLoader() ) {
109 wp_register_script(
110 'surecart-components',
111 trailingslashit( \SureCart::core()->assets()->getUrl() ) . 'dist/components/surecart/surecart.esm.js',
112 [ 'wp-i18n', 'regenerator-runtime' ],
113 filemtime( trailingslashit( $this->container[ SURECART_CONFIG_KEY ]['app_core']['path'] ) . 'dist/components/surecart/surecart.esm.js' ) . '-' . \SureCart::plugin()->version(),
114 true
115 );
116 } else {
117 // instead, use a static loader that injects the script at runtime.
118 $static_assets = include trailingslashit( $this->container[ SURECART_CONFIG_KEY ]['app_core']['path'] ) . 'dist/components/static-loader.asset.php';
119 wp_register_script(
120 'surecart-components',
121 trailingslashit( \SureCart::core()->assets()->getUrl() ) . 'dist/components/static-loader.js',
122 array_merge( [ 'wp-i18n', 'regenerator-runtime' ], $static_assets['dependencies'] ?? [] ),
123 $static_assets['version'] . '-' . \SureCart::plugin()->version(),
124 true
125 );
126 wp_localize_script(
127 'surecart-components',
128 'surecartComponents',
129 [
130 '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' ),
131 ]
132 );
133 }
134
135 wp_set_script_translations( 'surecart-components', 'surecart' );
136
137 wp_localize_script(
138 'surecart-components',
139 'scData',
140 apply_filters(
141 'surecart-components/scData', // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores,WordPress.NamingConventions.ValidHookName.NotLowercase
142 [
143 'cdn_root' => SURECART_CDN_IMAGE_BASE,
144 'root_url' => esc_url_raw( get_rest_url() ),
145 'plugin_url' => \SureCart::core()->assets()->getUrl(),
146 'home_url' => esc_url_raw( home_url() ),
147 'api_url' => \SureCart::requests()->getBaseUrl(),
148 'locale' => str_replace( '_', '-', determine_locale() ),
149 'currency' => \SureCart::account()->currency,
150 'currency_symbol' => html_entity_decode( Currency::getCurrencySymbol( \SureCart::account()->currency ) ),
151 'theme' => \SureCart::theme()->mode(),
152 'pages' => [
153 'dashboard' => \SureCart::pages()->url( 'dashboard' ),
154 'checkout' => \SureCart::pages()->url( 'checkout' ),
155 ],
156 'page_id' => get_the_ID(),
157 'nonce' => ( wp_installing() && ! is_multisite() ) ? '' : wp_create_nonce( 'wp_rest' ),
158 'nonce_endpoint' => admin_url( 'admin-ajax.php?action=sc-rest-nonce' ),
159 'recaptcha_site_key' => \SureCart::settings()->recaptcha()->getSiteKey(),
160 'claim_url' => $this->getAccountClaimUrl(),
161 'admin_url' => trailingslashit( admin_url() ),
162 'getting_started_url' => untrailingslashit( admin_url( 'admin.php?page=sc-getting-started' ) ),
163 'user_permissions' => array(
164 'manage_sc_shop_settings' => current_user_can( 'manage_sc_shop_settings' ),
165 ),
166 'is_account_connected' => \SureCart::account()->isConnected(),
167 'modern_order_bump' => get_option( 'surecart_order_bump_design', 'modern' ) === 'modern',
168 'google_map_api_key' => \SureCart::googleMaps()->getApiKey(),
169 // Nested so wp_localize_script() keeps the booleans — it string-casts top-level scalars.
170 'tracking' => array(
171 'facebook' => $this->isFacebookTrackingEnabled(),
172 'google' => $this->isGoogleTrackingEnabled(),
173 ),
174 ]
175 )
176 );
177
178 wp_localize_script( 'surecart-components', 'scIcons', [ 'path' => esc_url_raw( plugin_dir_url( SURECART_PLUGIN_FILE ) . 'dist/icon-assets' ) ] );
179
180 // core-data.
181 $asset_file = include trailingslashit( $this->container[ SURECART_CONFIG_KEY ]['app_core']['path'] ) . 'dist/store/data.asset.php';
182 $asset_file['dependencies'][] = 'regenerator-runtime';
183 wp_register_script(
184 'sc-core-data',
185 trailingslashit( \SureCart::core()->assets()->getUrl() ) . 'dist/store/data.js',
186 array_merge( [ 'surecart-components' ], $asset_file['dependencies'] ?? [] ),
187 $asset_file['version'] . '-' . \SureCart::plugin()->version(),
188 true
189 );
190
191 // ui.
192 $asset_file = include trailingslashit( $this->container[ SURECART_CONFIG_KEY ]['app_core']['path'] ) . 'dist/store/ui.asset.php';
193 $asset_file['dependencies'][] = 'regenerator-runtime';
194 wp_register_script(
195 'sc-ui-data',
196 trailingslashit( \SureCart::core()->assets()->getUrl() ) . 'dist/store/ui.js',
197 array_merge( [ 'surecart-components' ], $asset_file['dependencies'] ?? [] ),
198 $asset_file['version'] . '-' . \SureCart::plugin()->version(),
199 true
200 );
201
202 $this->registerBlocks();
203
204 // regsiter recaptcha.
205 wp_register_script( 'surecart-google-recaptcha', 'https://www.google.com/recaptcha/api.js?render=' . \SureCart::settings()->recaptcha()->getSiteKey(), [], \SureCart::plugin()->version(), true );
206
207 // register stripe if enabled.
208 if ( get_option( 'surecart_load_stripe_js', false ) ) {
209 wp_enqueue_script( 'surecart-stripe-script', 'https://js.stripe.com/v3', [], \SureCart::plugin()->version(), false );
210 }
211
212 // templates.
213 $asset_file = include trailingslashit( $this->container[ SURECART_CONFIG_KEY ]['app_core']['path'] ) . 'dist/templates/admin.asset.php';
214 $asset_file['dependencies'][] = 'regenerator-runtime';
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 wp_register_script(
233 'surecart-affiliate-tracking',
234 esc_url_raw( untrailingslashit( SURECART_JS_URL ) . '/v1/affiliates' ),
235 [],
236 '1.1',
237 [
238 'strategy' => 'defer',
239 ]
240 );
241
242 wp_add_inline_script(
243 'surecart-affiliate-tracking',
244 'window.SureCartAffiliatesConfig = {
245 "publicToken": "' . \SureCart::account()->public_token . '",
246 "baseURL":"' . esc_url_raw( untrailingslashit( SURECART_API_URL ) ) . '/v1"
247 };',
248 'before'
249 );
250 }
251
252 /**
253 * Enqueue block front scripts.
254 *
255 * @return void
256 */
257 public function enqueueFront() {
258 // make sure it is registered.
259 $this->register();
260 // enqueue it.
261 wp_enqueue_script( 'surecart-components' );
262
263 // Fix Jetpack script key hijacking issues.
264 add_filter(
265 'wp_head',
266 function () {
267 wp_dequeue_script( 'wpcom-notes-common' );
268 wp_dequeue_script( 'wpcom-notes-admin-bar' );
269 wp_dequeue_style( 'wpcom-notes-admin-bar' );
270 wp_dequeue_style( 'noticons' );
271 },
272 200
273 );
274 }
275
276 /**
277 * Enqueue editor scripts.
278 *
279 * @return void
280 */
281 public function enqueueEditor() {
282 $this->enqueueBlocks();
283 $this->enqueuePageTemplateEditor();
284 $this->enqueueProductBlocks();
285 $this->enqueueProductCollectionBlocks();
286 }
287
288 /**
289 * Enqueue page templates.
290 *
291 * @return void
292 */
293 public function enqueuePageTemplateEditor() {
294 wp_enqueue_script( 'surecart-templates-admin' );
295 }
296
297 /**
298 * We only want these available in FSE.
299 *
300 * @return void
301 */
302 public function enqueueProductBlocks() {
303 global $pagenow;
304 if ( 'site-editor.php' !== $pagenow ) {
305 return;
306 }
307
308 wp_enqueue_script( 'surecart-product-blocks' );
309 }
310
311 /**
312 * We only want these available in FSE.
313 *
314 * @return void
315 */
316 public function enqueueProductCollectionBlocks() {
317 global $pagenow;
318 if ( 'site-editor.php' !== $pagenow ) {
319 return;
320 }
321
322 wp_enqueue_script( 'surecart-product-collection-blocks' );
323 }
324
325 /**
326 * Register block scripts.
327 *
328 * @return void
329 */
330 public function registerBlocks() {
331 $enabled_payment_processors = is_admin() ? array_values(
332 array_filter(
333 (array) Processor::get() ?? [],
334 function ( $payment_method ) {
335 return $payment_method->enabled ?? false;
336 }
337 )
338 ) : [];
339 // blocks.
340 $asset_file = include trailingslashit( $this->container[ SURECART_CONFIG_KEY ]['app_core']['path'] ) . 'dist/blocks/library.asset.php';
341 $deps = $asset_file['dependencies'] ?? [];
342 // fix bug in deps array.
343 $deps[ array_search( 'wp-blockEditor', $deps ) ] = 'wp-block-editor';
344 wp_register_script(
345 'surecart-blocks',
346 trailingslashit( \SureCart::core()->assets()->getUrl() ) . 'dist/blocks/library.js',
347 $deps,
348 $asset_file['version'] . '-' . \SureCart::plugin()->version(),
349 true
350 );
351
352 // only register.
353 $asset_file = include trailingslashit( $this->container[ SURECART_CONFIG_KEY ]['app_core']['path'] ) . 'dist/blocks/product.asset.php';
354 $deps = $asset_file['dependencies'] ?? [];
355 // fix bug in deps array.
356 $deps[ array_search( 'wp-blockEditor', $deps ) ] = 'wp-block-editor';
357 wp_register_script(
358 'surecart-product-blocks',
359 trailingslashit( \SureCart::core()->assets()->getUrl() ) . 'dist/blocks/product.js',
360 $deps,
361 $asset_file['version'],
362 true
363 );
364
365 // Register product collection blocks.
366 $asset_file = include trailingslashit( $this->container[ SURECART_CONFIG_KEY ]['app_core']['path'] ) . 'dist/blocks/product_collection.asset.php';
367 $deps = $asset_file['dependencies'] ?? [];
368 $deps[ array_search( 'wp-blockEditor', $deps ) ] = 'wp-block-editor';
369 wp_register_script(
370 'surecart-product-collection-blocks',
371 trailingslashit( \SureCart::core()->assets()->getUrl() ) . 'dist/blocks/product_collection.js',
372 $deps,
373 $asset_file['version'],
374 true
375 );
376
377 // localize.
378 wp_localize_script(
379 'surecart-blocks',
380 'scData',
381 apply_filters(
382 'surecart-components/scData', // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores,WordPress.NamingConventions.ValidHookName.NotLowercase
383 [
384 'root_url' => esc_url_raw( get_rest_url() ),
385 'plugin_url' => \SureCart::core()->assets()->getUrl(),
386 'api_url' => \SureCart::requests()->getBaseUrl(),
387 'currency' => \SureCart::account()->currency,
388 'locale' => str_replace( '_', '-', determine_locale() ),
389 'currency_symbol' => html_entity_decode( Currency::getCurrencySymbol( \SureCart::account()->currency ) ),
390 'theme' => \SureCart::theme()->mode(),
391 'pages' => [
392 'dashboard' => \SureCart::pages()->url( 'dashboard' ),
393 'checkout' => \SureCart::pages()->url( 'checkout' ),
394 ],
395 'default_checkout_id' => (int) \SureCart::forms()->getDefaultId(),
396 'page_id' => get_the_ID(),
397 'nonce' => ( wp_installing() && ! is_multisite() ) ? '' : wp_create_nonce( 'wp_rest' ),
398 'nonce_endpoint' => admin_url( 'admin-ajax.php?action=sc-rest-nonce' ),
399 'recaptcha_site_key' => \SureCart::settings()->recaptcha()->getSiteKey(),
400 'claim_url' => $this->getAccountClaimUrl(),
401 'admin_url' => trailingslashit( admin_url() ),
402 'getting_started_url' => untrailingslashit( admin_url( 'admin.php?page=sc-getting-started' ) ),
403 'user_permissions' => array(
404 'manage_sc_shop_settings' => current_user_can( 'manage_sc_shop_settings' ),
405 ),
406 'is_account_connected' => \SureCart::account()->isConnected(),
407 'modern_order_bump' => get_option( 'surecart_order_bump_design', 'modern' ) === 'modern',
408 'google_map_api_key' => \SureCart::googleMaps()->getApiKey(),
409 // Nested so wp_localize_script() keeps the booleans — it string-casts top-level scalars.
410 'tracking' => array(
411 'facebook' => $this->isFacebookTrackingEnabled(),
412 'google' => $this->isGoogleTrackingEnabled(),
413 ),
414 ]
415 )
416 );
417
418 wp_localize_script(
419 'surecart-blocks',
420 'scBlockData',
421 [
422 'root_url' => esc_url_raw( get_rest_url() ),
423 'nonce' => ( wp_installing() && ! is_multisite() ) ? '' : wp_create_nonce( 'wp_rest' ),
424 'nonce_endpoint' => admin_url( 'admin-ajax.php?action=sc-rest-nonce' ),
425 'processors' => $enabled_payment_processors,
426 'manualPaymentMethods' => is_admin() ? ( (array) ManualPaymentMethod::get() ?? [] ) : [],
427 'plugin_url' => \SureCart::core()->assets()->getUrl(),
428 'currency' => \SureCart::account()->currency,
429 'theme' => \SureCart::theme()->mode(),
430 'entitlements' => \SureCart::account()->entitlements,
431 'upgrade_url' => \SureCart::config()->links->purchase,
432 'beta' => [
433 'stripe_payment_element' => (bool) get_option( 'sc_stripe_payment_element', true ),
434 ],
435 'pages' => [
436 'dashboard' => \SureCart::pages()->url( 'dashboard' ),
437 'checkout' => \SureCart::pages()->url( 'checkout' ),
438 ],
439 'google_map_api_key' => \SureCart::googleMaps()->getApiKey(),
440 ]
441 );
442
443 wp_localize_script( 'surecart-blocks', 'scIcons', [ 'path' => esc_url_raw( plugin_dir_url( SURECART_PLUGIN_FILE ) . 'dist/icon-assets' ) ] );
444 }
445
446 /**
447 * Enqueue blocks scripts.
448 *
449 * @return void
450 */
451 public function enqueueBlocks() {
452 wp_enqueue_script( 'surecart-blocks' );
453 }
454 }
455