PluginProbe
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments / 1.1.4
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments v1.1.4
4.7.2 4.7.1 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 All 281 releases
surecart / app / src / WordPress / Assets / ScriptsService.php
ScriptsService.php
244 lines 8.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace SureCart\WordPress\Assets;
4
5 /**
6 * Handles the component theme.
7 */
8 class ScriptsService {
9 /**
10 * Holds the service container
11 *
12 * @var \Pimple\Container
13 */
14 protected $container;
15
16 /**
17 * Make sure we change the script loader tag for esm loading.
18 *
19 * @param \Pimple\Container $container Service Container.
20 */
21 public function __construct( $container ) {
22 $this->container = $container;
23
24 // add module to components tag.
25 add_filter( 'script_loader_tag', [ $this, 'componentsTag' ], 10, 3 );
26 }
27
28 /**
29 * Add module to the components tag
30 *
31 * @param string $tag Tag output.
32 * @param string $handle Script handle.
33 * @param string $source Script source.
34 *
35 * @return string
36 */
37 public function componentsTag( $tag, $handle, $source ) {
38 if ( 'surecart-components' !== $handle || ! $source ) {
39 return $tag;
40 }
41 // don't use javascript module if we are not using esm loader.
42 if ( ! \SureCart::assets()->usesEsmLoader() ) {
43 return $tag;
44 }
45
46 // make sure our translations do not get stripped.
47 $translations = wp_scripts()->print_translations( $handle, false );
48 if ( $translations ) {
49 $translations = sprintf( "<script%s id='%s-js-translations'>\n%s\n</script>\n", " type='text/javascript'", esc_attr( $handle ), $translations );
50 }
51
52 // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript
53 return '<script src="' . esc_url_raw( $source ) . '" type="module"></script>' . $translations;
54 }
55
56 /**
57 * Register the component scripts and translations.
58 *
59 * @return void
60 */
61 public function register() {
62 // should we use the esm loader directly?
63 if ( \SureCart::assets()->usesEsmLoader() ) {
64 wp_register_script(
65 'surecart-components',
66 trailingslashit( \SureCart::core()->assets()->getUrl() ) . 'dist/components/surecart/surecart.esm.js',
67 [ 'wp-i18n' ],
68 filemtime( trailingslashit( $this->container[ SURECART_CONFIG_KEY ]['app_core']['path'] ) . 'dist/components/surecart/surecart.esm.js' ) . '-' . \SureCart::plugin()->version(),
69 false
70 );
71 } else {
72 // instead, use a static loader that injects the script at runtime.
73 $static_assets = include trailingslashit( $this->container[ SURECART_CONFIG_KEY ]['app_core']['path'] ) . 'dist/components/static-loader.asset.php';
74 wp_register_script(
75 'surecart-components',
76 trailingslashit( \SureCart::core()->assets()->getUrl() ) . 'dist/components/static-loader.js',
77 array_merge( [ 'wp-i18n' ], $static_assets['dependencies'] ),
78 $static_assets['version'] . '-' . \SureCart::plugin()->version(),
79 true
80 );
81 wp_localize_script(
82 'surecart-components',
83 'surecartComponents',
84 [
85 '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' ),
86 ]
87 );
88 }
89
90 wp_set_script_translations( 'surecart-components', 'surecart' );
91
92 // core-data.
93 $asset_file = include trailingslashit( $this->container[ SURECART_CONFIG_KEY ]['app_core']['path'] ) . 'dist/store/data.asset.php';
94 wp_register_script(
95 'sc-core-data',
96 trailingslashit( \SureCart::core()->assets()->getUrl() ) . 'dist/store/data.js',
97 array_merge( [ 'surecart-components' ], $asset_file['dependencies'] ),
98 $asset_file['version'] . '-' . \SureCart::plugin()->version(),
99 true
100 );
101
102 // ui.
103 $asset_file = include trailingslashit( $this->container[ SURECART_CONFIG_KEY ]['app_core']['path'] ) . 'dist/store/ui.asset.php';
104 wp_register_script(
105 'sc-ui-data',
106 trailingslashit( \SureCart::core()->assets()->getUrl() ) . 'dist/store/ui.js',
107 array_merge( [ 'surecart-components' ], $asset_file['dependencies'] ),
108 $asset_file['version'] . '-' . \SureCart::plugin()->version(),
109 true
110 );
111
112 // blocks.
113 $asset_file = include trailingslashit( $this->container[ SURECART_CONFIG_KEY ]['app_core']['path'] ) . 'dist/blocks/library.asset.php';
114 $deps = $asset_file['dependencies'];
115 // fix bug in deps array.
116 $deps[ array_search( 'wp-blockEditor', $deps ) ] = 'wp-block-editor';
117 wp_register_script(
118 'surecart-blocks',
119 trailingslashit( \SureCart::core()->assets()->getUrl() ) . 'dist/blocks/library.js',
120 array_merge( [ 'surecart-components' ], $deps ),
121 $asset_file['version'] . '-' . \SureCart::plugin()->version(),
122 true
123 );
124
125 wp_localize_script( 'surecart-cart-blocks', 'scIcons', [ 'path' => esc_url_raw( plugin_dir_url( SURECART_PLUGIN_FILE ) . 'dist/icon-assets' ) ] );
126 $asset_file = include trailingslashit( $this->container[ SURECART_CONFIG_KEY ]['app_core']['path'] ) . 'dist/blocks/cart.asset.php';
127 $deps = $asset_file['dependencies'];
128 // fix bug in deps array.
129 $deps[ array_search( 'wp-blockEditor', $deps ) ] = 'wp-block-editor';
130 wp_register_script(
131 'surecart-cart-blocks',
132 trailingslashit( \SureCart::core()->assets()->getUrl() ) . 'dist/blocks/cart.js',
133 array_merge( [ 'surecart-components' ], $deps ),
134 $asset_file['version'] . '-' . \SureCart::plugin()->version(),
135 true
136 );
137
138 wp_localize_script( 'surecart-cart-blocks', 'scIcons', [ 'path' => esc_url_raw( plugin_dir_url( SURECART_PLUGIN_FILE ) . 'dist/icon-assets' ) ] );
139
140 // cart.
141 $asset_file = include trailingslashit( $this->container[ SURECART_CONFIG_KEY ]['app_core']['path'] ) . 'dist/blocks/cart.asset.php';
142 $deps = $asset_file['dependencies'];
143 // fix bug in deps array.
144 $deps[ array_search( 'wp-blockEditor', $deps ) ] = 'wp-block-editor';
145 wp_register_script(
146 'surecart-cart-blocks',
147 trailingslashit( \SureCart::core()->assets()->getUrl() ) . 'dist/blocks/cart.js',
148 array_merge( [ 'surecart-components' ], $deps ),
149 $asset_file['version'] . '-' . \SureCart::plugin()->version(),
150 true
151 );
152
153 wp_localize_script( 'surecart-cart-blocks', 'scIcons', [ 'path' => esc_url_raw( plugin_dir_url( SURECART_PLUGIN_FILE ) . 'dist/icon-assets' ) ] );
154 }
155
156 /**
157 * Enqueue block front scripts.
158 *
159 * @return void
160 */
161 public function enqueueFront() {
162 // make sure it is registered.
163 $this->register();
164 // enqueue it.
165 wp_enqueue_script( 'surecart-components' );
166 wp_localize_script(
167 'surecart-components',
168 'scData',
169 [
170 'root_url' => esc_url_raw( get_rest_url() ),
171 'plugin_url' => \SureCart::core()->assets()->getUrl(),
172 'currency' => \SureCart::account()->currency,
173 'do_not_persist_cart' => is_admin(),
174 'pages' => [
175 'dashboard' => \SureCart::pages()->url( 'dashboard' ),
176 'checkout' => \SureCart::pages()->url( 'checkout' ),
177 ],
178 'page_id' => get_the_ID(),
179 'nonce' => ( wp_installing() && ! is_multisite() ) ? '' : wp_create_nonce( 'wp_rest' ),
180 'nonce_endpoint' => admin_url( 'admin-ajax.php?action=sc-rest-nonce' ),
181 ]
182 );
183
184 wp_localize_script( 'surecart-components', 'scIcons', [ 'path' => esc_url_raw( plugin_dir_url( SURECART_PLUGIN_FILE ) . 'dist/icon-assets' ) ] );
185 }
186
187 /**
188 * Enqueue editor scripts.
189 *
190 * @return void
191 */
192 public function enqueueEditor() {
193 $this->enqueueFront();
194 $this->enqueueBlocks();
195 $this->enqueueCartBlocks();
196 }
197
198 /**
199 * Enqueue Cart Blocks.
200 *
201 * @return void
202 */
203 public function enqueueCartBlocks() {
204 // not our post type.
205 if ( 'sc_cart' !== get_post_type() ) {
206 return;
207 }
208
209 wp_enqueue_script( 'surecart-cart-blocks' );
210 wp_localize_script(
211 'surecart-cart-blocks',
212 'scBlockData',
213 [
214 'currency' => \SureCart::account()->currency,
215 ]
216 );
217 }
218
219 /**
220 * Enqueue blocks scripts.
221 *
222 * @return void
223 */
224 public function enqueueBlocks() {
225 wp_enqueue_script( 'surecart-blocks' );
226 wp_localize_script(
227 'surecart-blocks',
228 'scBlockData',
229 [
230 'processors' => (array) \SureCart::account()->processors ?? [],
231 'plugin_url' => \SureCart::core()->assets()->getUrl(),
232 'currency' => \SureCart::account()->currency,
233 'beta' => [
234 'stripe_payment_element' => (bool) get_option( 'sc_stripe_payment_element', false ),
235 ],
236 'pages' => [
237 'dashboard' => \SureCart::pages()->url( 'dashboard' ),
238 'checkout' => \SureCart::pages()->url( 'checkout' ),
239 ],
240 ]
241 );
242 }
243 }
244