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