PluginProbe
Packeta / trunk
Packeta vtrunk
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 trunk, at src/Packetery/Module/Views/AssetManager.php

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