PluginProbe
Packeta / 2.1
Packeta v2.1
2.3.2 2.3.1 trunk 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3.0 1.3.1 1.3.2 1.4 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 All 56 releases
packeta / src / Packetery / Module / Views / AssetManager.php

AssetManager.php in Packeta 2.1, at src/Packetery/Module/Views/AssetManager.php

405 lines 11.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare( strict_types=1 );
4
5 namespace Packetery\Module\Views;
6
7 use Packetery\Core\CoreHelper;
8 use Packetery\Module\Carrier;
9 use Packetery\Module\Checkout\CheckoutService;
10 use Packetery\Module\Checkout\CheckoutSettings;
11 use Packetery\Module\ContextResolver;
12 use Packetery\Module\Dashboard\DashboardPage;
13 use Packetery\Module\Framework\WcAdapter;
14 use Packetery\Module\Framework\WpAdapter;
15 use Packetery\Module\Log;
16 use Packetery\Module\ModuleHelper;
17 use Packetery\Module\Options;
18 use Packetery\Module\Options\FlagManager\FeatureFlagNotice;
19 use Packetery\Module\Options\FlagManager\FeatureFlagProvider;
20 use Packetery\Module\Order;
21 use Packetery\Module\Order\Metabox;
22 use Packetery\Module\Plugin;
23 use Packetery\Module\WidgetUrlResolver;
24 use Packetery\Nette\Http\Request;
25
26 class AssetManager {
27
28 /**
29 * @var ContextResolver
30 */
31 private $contextResolver;
32
33 /**
34 * @var FeatureFlagProvider
35 */
36 private $featureFlagProvider;
37
38 /**
39 * @var FeatureFlagNotice
40 */
41 private $featureFlagNotice;
42
43 /**
44 * @var Metabox
45 */
46 private $orderMetabox;
47
48 /**
49 * @var Request
50 */
51 private $request;
52
53 /**
54 * @var CheckoutSettings
55 */
56 private $checkoutSettings;
57
58 /**
59 * @var WpAdapter
60 */
61 private $wpAdapter;
62
63 /**
64 * @var WcAdapter
65 */
66 private $wcAdapter;
67
68 /**
69 * @var CheckoutService
70 */
71 private $checkoutService;
72
73 /**
74 * @var WidgetUrlResolver
75 */
76 private $widgetUrlResolver;
77
78 public function __construct(
79 ContextResolver $contextResolver,
80 FeatureFlagProvider $featureFlagProvider,
81 FeatureFlagNotice $featureFlagNotice,
82 Metabox $orderMetabox,
83 Request $request,
84 CheckoutSettings $checkoutSettings,
85 WpAdapter $wpAdapter,
86 WcAdapter $wcAdapter,
87 CheckoutService $checkoutService,
88 WidgetUrlResolver $widgetUrlResolver
89 ) {
90
91 $this->contextResolver = $contextResolver;
92 $this->featureFlagProvider = $featureFlagProvider;
93 $this->featureFlagNotice = $featureFlagNotice;
94 $this->orderMetabox = $orderMetabox;
95 $this->request = $request;
96 $this->checkoutSettings = $checkoutSettings;
97 $this->wpAdapter = $wpAdapter;
98 $this->wcAdapter = $wcAdapter;
99 $this->checkoutService = $checkoutService;
100 $this->widgetUrlResolver = $widgetUrlResolver;
101 }
102
103 /**
104 * Enqueues admin JS file.
105 *
106 * @param string $name Name of script.
107 * @param string $file Relative file path.
108 * @param bool $inFooter Tells where to include script.
109 * @param array $deps Script dependencies.
110 */
111 public function enqueueScript( string $name, string $file, bool $inFooter, array $deps = [] ): void {
112 $this->wpAdapter->enqueueScript(
113 $name,
114 $this->wpAdapter->pluginDirUrl( ModuleHelper::getPluginMainFilePath() ) . $file,
115 $deps,
116 md5( (string) filemtime( PACKETERY_PLUGIN_DIR . '/' . $file ) ),
117 $inFooter
118 );
119 }
120
121 /**
122 * Enqueues CSS file.
123 *
124 * @param string $name Name of script.
125 * @param string $file Relative file path.
126 */
127 public function enqueueStyle( string $name, string $file ): void {
128 $this->wpAdapter->enqueueStyle(
129 $name,
130 $this->wpAdapter->pluginDirUrl( ModuleHelper::getPluginMainFilePath() ) . $file,
131 [],
132 md5( (string) filemtime( PACKETERY_PLUGIN_DIR . '/' . $file ) )
133 );
134 }
135
136 /**
137 * Enqueues javascript files and stylesheets for checkout.
138 */
139 public function enqueueFrontAssets(): void {
140 if ( $this->wcAdapter->isCheckout() ) {
141 if ( $this->wpAdapter->doingAjax() === false ) {
142 $this->enqueueStyle( 'packetery-front-styles', 'public/css/front.css' );
143
144 $customFrontCssPath = WP_CONTENT_DIR . '/packeta-custom-front.css';
145 if ( file_exists( $customFrontCssPath ) ) {
146 $this->wpAdapter->enqueueStyle(
147 'packetery-custom-front-styles',
148 $customFrontCssPath,
149 [],
150 md5( (string) filemtime( $customFrontCssPath ) )
151 );
152 }
153 }
154 if ( $this->checkoutService->areBlocksUsedInCheckout() ) {
155 $this->wpAdapter->enqueueScript(
156 'packetery-widget-library',
157 $this->widgetUrlResolver->getUrl(),
158 [],
159 Plugin::VERSION,
160 false
161 );
162 } elseif ( $this->wpAdapter->doingAjax() === false ) {
163 $this->enqueueScript( 'packetery-checkout', 'public/js/checkout.js', true, [ 'jquery' ] );
164 $this->wpAdapter->localizeScript( 'packetery-checkout', 'packeteryCheckoutSettings', $this->checkoutSettings->createSettings() );
165 }
166 }
167 }
168
169 /**
170 * Enqueues javascript files and stylesheets for administration.
171 */
172 public function enqueueAdminAssets(): void {
173 $page = $this->request->getQuery( 'page' );
174 $isOrderGridPage = $this->contextResolver->isOrderGridPage();
175 $isOrderDetailPage = $this->contextResolver->isOrderDetailPage();
176 $isProductCategoryPage = $this->contextResolver->isProductCategoryDetailPage() || $this->contextResolver->isProductCategoryGridPage();
177 $datePickerSettings = [
178 'deliverOnMinDate' => $this->wpAdapter->date( CoreHelper::DATEPICKER_FORMAT, strtotime( 'tomorrow' ) ),
179 'dateFormat' => CoreHelper::DATEPICKER_FORMAT_JS,
180 ];
181
182 if ( $isOrderGridPage || $isOrderDetailPage || in_array(
183 $page,
184 [
185 Carrier\OptionsPage::SLUG,
186 Options\Page::SLUG,
187 ],
188 true
189 ) ) {
190 $this->enqueueScript( 'live-form-validation-options', 'public/js/live-form-validation-options.js', false );
191 $this->enqueueScript( 'live-form-validation', 'public/libs/live-form-validation/live-form-validation.js', false, [ 'live-form-validation-options' ] );
192 $this->enqueueScript( 'live-form-validation-extension', 'public/js/live-form-validation-extension.js', false, [ 'live-form-validation' ] );
193 }
194
195 if ( in_array( $page, [ Carrier\OptionsPage::SLUG, Options\Page::SLUG ], true ) ) {
196 $this->enqueueStyle( 'packetery-select2-css', 'public/libs/select2-4.0.13/dist.min.css' );
197 $this->enqueueScript( 'packetery-select2', 'public/libs/select2-4.0.13/dist.min.js', true, [ 'jquery' ] );
198 }
199
200 if ( $page === Carrier\OptionsPage::SLUG ) {
201 $this->enqueueScript(
202 'packetery-multiplier',
203 'public/js/multiplier.js',
204 true,
205 [
206 'jquery',
207 'live-form-validation-extension',
208 ]
209 );
210 $this->enqueueScript(
211 'packetery-admin-country-carrier',
212 'public/js/admin-country-carrier.js',
213 true,
214 [
215 'jquery',
216 'packetery-multiplier',
217 'packetery-select2',
218 ]
219 );
220 }
221
222 if ( $page === Options\Page::SLUG ) {
223 $this->enqueueScript(
224 'packetery-admin-options',
225 'public/js/admin-options.js',
226 true,
227 [
228 'jquery',
229 'packetery-select2',
230 ]
231 );
232 }
233
234 $isProductPage = $this->contextResolver->isProductPage();
235 $isPageDetail = $this->contextResolver->isPageDetail();
236
237 $screen = $this->wpAdapter->getCurrentScree();
238 $isDashboard = ( $screen !== null && $screen->id === 'dashboard' );
239
240 if (
241 $isOrderGridPage || $isOrderDetailPage || $isProductPage || $isProductCategoryPage || $isDashboard || $isPageDetail ||
242 in_array(
243 $page,
244 [
245 Options\Page::SLUG,
246 Carrier\OptionsPage::SLUG,
247 Log\Page::SLUG,
248 Order\LabelPrint::MENU_SLUG,
249 DashboardPage::SLUG,
250 ],
251 true
252 )
253 ) {
254 $this->enqueueStyle( 'packetery-admin-styles', 'public/css/admin.css' );
255 // It is placed here so that typenow in contextResolver works and there is no need to repeat the conditions.
256 if ( $this->featureFlagProvider->shouldShowSplitActivationNotice() ) {
257 $this->wpAdapter->addAction( 'admin_notices', [ $this->featureFlagNotice, 'renderSplitActivationNotice' ] );
258 }
259 }
260
261 if ( $isOrderGridPage ) {
262 $orderGridPageSettings = [
263 'translations' => [
264 'hasToFillCustomsDeclaration' => $this->wpAdapter->__( 'Customs declaration has to be filled in order detail.', 'packeta' ),
265 'packetSubmissionNotPossible' => $this->wpAdapter->__( 'It is not possible to submit the shipment because all the information required for this shipment is not filled.', 'packeta' ),
266 ],
267 ];
268 $this->enqueueScript(
269 'packetery-admin-grid-order-edit-js',
270 'public/js/admin-grid-order-edit.js',
271 true,
272 [
273 'jquery',
274 'wp-util',
275 'backbone',
276 ]
277 );
278
279 $this->wpAdapter->localizeScript( 'packetery-admin-grid-order-edit-js', 'datePickerSettings', $datePickerSettings );
280 $this->wpAdapter->localizeScript( 'packetery-admin-grid-order-edit-js', 'settings', $orderGridPageSettings );
281
282 $this->enqueueScript(
283 'packetery-admin-stored-until-modal-js',
284 'public/js/admin-stored-until-modal.js',
285 true,
286 [
287 'jquery',
288 'wp-util',
289 'backbone',
290 ]
291 );
292 $this->wpAdapter->localizeScript( 'packetery-admin-stored-until-modal-js', 'datePickerSettings', $datePickerSettings );
293 $this->wpAdapter->localizeScript( 'packetery-admin-stored-until-modal-js', 'settings', $orderGridPageSettings );
294 }
295
296 $pickupPointPickerSettings = null;
297 $addressPickerSettings = null;
298
299 if ( $isOrderDetailPage ) {
300 $this->enqueueScript(
301 'packetery-multiplier',
302 'public/js/multiplier.js',
303 true,
304 [
305 'jquery',
306 'live-form-validation-extension',
307 ]
308 );
309 $this->enqueueScript(
310 'admin-order-detail',
311 'public/js/admin-order-detail.js',
312 true,
313 [
314 'jquery',
315 'packetery-multiplier',
316 'live-form-validation-extension',
317 ]
318 );
319
320 $this->wpAdapter->localizeScript( 'admin-order-detail', 'datePickerSettings', $datePickerSettings );
321 $pickupPointPickerSettings = $this->orderMetabox->getPickupPointWidgetSettings();
322 $addressPickerSettings = $this->orderMetabox->getAddressWidgetSettings();
323
324 $this->enqueueScript(
325 'packetery-admin-stored-until-modal-js',
326 'public/js/admin-stored-until-modal.js',
327 true,
328 [
329 'jquery',
330 'wp-util',
331 'backbone',
332 ]
333 );
334
335 }
336
337 if ( $pickupPointPickerSettings !== null || $addressPickerSettings !== null ) {
338 $this->wpAdapter->enqueueScript(
339 'packetery-widget-library',
340 $this->widgetUrlResolver->getUrl(),
341 [],
342 Plugin::VERSION,
343 true
344 );
345 }
346
347 if ( $pickupPointPickerSettings !== null ) {
348 $this->enqueueScript(
349 'packetery-admin-pickup-point-picker',
350 'public/js/admin-pickup-point-picker.js',
351 true,
352 [
353 'jquery',
354 'packetery-widget-library',
355 ]
356 );
357
358 $this->wpAdapter->localizeScript(
359 'packetery-admin-pickup-point-picker',
360 'packeteryPickupPointPickerSettings',
361 $pickupPointPickerSettings
362 );
363 }
364
365 if ( $addressPickerSettings !== null ) {
366 $this->enqueueScript(
367 'packetery-admin-address-picker',
368 'public/js/admin-address-picker.js',
369 true,
370 [
371 'jquery',
372 'packetery-widget-library',
373 ]
374 );
375 $this->wpAdapter->localizeScript(
376 'packetery-admin-address-picker',
377 'packeteryAddressPickerSettings',
378 $addressPickerSettings
379 );
380 }
381
382 if ( $this->contextResolver->isConfirmModalPage() ) {
383 $this->enqueueScript( 'packetery-confirm', 'public/js/confirm.js', true, [ 'jquery', 'backbone' ] );
384 }
385 if ( $this->contextResolver->isPluginsOverviewPage() ) {
386 $this->enqueueScript(
387 'packetery-admin-confirm-deactivation',
388 'public/js/admin-confirm-deactivation.js',
389 true
390 );
391
392 $this->wpAdapter->localizeScript(
393 'packetery-admin-confirm-deactivation',
394 'translationsDeactivation',
395 [
396 'confirmDeactivation' => $this->wpAdapter->__(
397 'When uninstalling the plugin, ALL data and settings will be removed only if the PACKETERY_REMOVE_ALL_DATA constant is defined in wp-config.php and is set to true. This is to prevent data loss when removing the plugin from the backend and to ensure that only the site owner can perform this action.',
398 'packeta'
399 ),
400 ]
401 );
402 }
403 }
404 }
405