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
← All changes | src/Packetery/Module/Upgrade.php +171 -113 1.5.42.1 View file →
@@ -11,8 +11,11 @@
11 11
12 12 use Packetery\Core;
13 13 use Packetery\Core\Log\ILogger;
14 14 use Packetery\Core\Log\Record;
15 +use Packetery\Module\Carrier\EntityRepository;
16 +use Packetery\Module\Options\OptionNames;
17 +use Packetery\Module\Options\OptionsProvider;
15 18 use Packetery\Module\Upgrade\Version_1_4_2;
16 19
17 20 /**
18 21 * Class Upgrade.
@@ -73,8 +76,15 @@
73 76 */
74 77 private $carrierRepository;
75 78
76 79 /**
80 + * Customs declaration repository.
81 + *
82 + * @var CustomsDeclaration\Repository
83 + */
84 + private $customsDeclarationRepository;
85 +
86 + /**
77 87 * WpdbAdapter.
78 88 *
79 89 * @var WpdbAdapter
80 90 */
@@ -80,17 +90,19 @@
80 90 */
81 91 private $wpdbAdapter;
82 92
83 93 /**
84 - * Constructor.
94 + * Options provider.
85 95 *
86 - * @param Order\Repository $orderRepository Order repository.
87 - * @param MessageManager $messageManager Message manager.
88 - * @param ILogger $logger Logger.
89 - * @param Log\Repository $logRepository Log repository.
90 - * @param WpdbAdapter $wpdbAdapter WpdbAdapter.
91 - * @param Carrier\Repository $carrierRepository Carrier repository.
96 + * @var OptionsProvider
92 97 */
98 + private $optionsProvider;
99 +
100 + /**
101 + * @var EntityRepository
102 + */
103 + private $carrierEntityRepository;
104 +
93 105 public function __construct(
94 106 Order\Repository $orderRepository,
95 107 MessageManager $messageManager,
96 108 ILogger $logger,
@@ -95,16 +107,22 @@
95 107 MessageManager $messageManager,
96 108 ILogger $logger,
97 109 Log\Repository $logRepository,
98 110 WpdbAdapter $wpdbAdapter,
99 - Carrier\Repository $carrierRepository
111 + Carrier\Repository $carrierRepository,
112 + CustomsDeclaration\Repository $customsDeclarationRepository,
113 + OptionsProvider $optionsProvider,
114 + EntityRepository $carrierEntityRepository
100 115 ) {
101 - $this->orderRepository = $orderRepository;
102 - $this->messageManager = $messageManager;
103 - $this->logger = $logger;
104 - $this->logRepository = $logRepository;
105 - $this->wpdbAdapter = $wpdbAdapter;
106 - $this->carrierRepository = $carrierRepository;
116 + $this->orderRepository = $orderRepository;
117 + $this->messageManager = $messageManager;
118 + $this->logger = $logger;
119 + $this->logRepository = $logRepository;
120 + $this->wpdbAdapter = $wpdbAdapter;
121 + $this->carrierRepository = $carrierRepository;
122 + $this->customsDeclarationRepository = $customsDeclarationRepository;
123 + $this->optionsProvider = $optionsProvider;
124 + $this->carrierEntityRepository = $carrierEntityRepository;
107 125 }
108 126
109 127 /**
110 128 * Checks previous plugin version and runs upgrade if needed.
@@ -112,63 +130,120 @@
112 130 *
113 131 * @return void
114 132 */
115 133 public function check(): void {
116 - $oldVersion = get_option( 'packetery_version' );
117 - if ( Plugin::VERSION === $oldVersion ) {
134 + $oldVersion = get_option( OptionNames::VERSION );
135 + if ( $oldVersion === Plugin::VERSION ) {
118 136 return;
119 137 }
120 138
121 - $this->createLogTable();
122 - $this->createCarrierTable();
123 - $this->createOrderTable();
139 + $this->runCreateTables();
124 140
125 141 // If no previous version detected, no upgrade will be run.
126 - if ( $oldVersion && version_compare( $oldVersion, '1.2.0', '<' ) ) {
127 - $logEntries = get_posts(
128 - [
129 - 'post_type' => 'packetery_log',
130 - 'post_status' => 'any',
131 - 'nopaging' => true,
132 - 'fields' => 'ids',
133 - ]
134 - );
135 - foreach ( $logEntries as $logEntryId ) {
136 - wp_delete_post( $logEntryId, true );
142 + if ( $oldVersion !== null && $oldVersion !== false ) {
143 + if ( version_compare( $oldVersion, '1.2.0', '<' ) ) {
144 + $logEntries = get_posts(
145 + [
146 + 'post_type' => 'packetery_log',
147 + 'post_status' => 'any',
148 + 'nopaging' => true,
149 + 'fields' => 'ids',
150 + ]
151 + );
152 + foreach ( $logEntries as $logEntryId ) {
153 + wp_delete_post( $logEntryId, true );
154 + }
155 +
156 + unregister_post_type( 'packetery_log' );
157 +
158 + $this->migrateWpOrderMetadata();
159 + $addressEntries = get_posts(
160 + [
161 + 'post_type' => self::POST_TYPE_VALIDATED_ADDRESS,
162 + 'post_status' => 'any',
163 + 'nopaging' => true,
164 + 'fields' => 'ids',
165 + ]
166 + );
167 + foreach ( $addressEntries as $addressEntryId ) {
168 + wp_delete_post( $addressEntryId, true );
169 + }
170 +
171 + unregister_post_type( self::POST_TYPE_VALIDATED_ADDRESS );
137 172 }
138 173
139 - unregister_post_type( 'packetery_log' );
174 + if ( version_compare( $oldVersion, '1.2.6', '<' ) ) {
175 + $this->orderRepository->deleteOrphans();
176 + }
140 177
141 - $this->migrateWpOrderMetadata();
142 - $addressEntries = get_posts(
143 - [
144 - 'post_type' => self::POST_TYPE_VALIDATED_ADDRESS,
145 - 'post_status' => 'any',
146 - 'nopaging' => true,
147 - 'fields' => 'ids',
148 - ]
149 - );
150 - foreach ( $addressEntries as $addressEntryId ) {
151 - wp_delete_post( $addressEntryId, true );
178 + if ( version_compare( $oldVersion, '1.4.2', '<' ) ) {
179 + $migration = new Version_1_4_2( $this->wpdbAdapter );
180 + $migration->run();
152 181 }
153 182
154 - unregister_post_type( self::POST_TYPE_VALIDATED_ADDRESS );
155 - }
183 + if ( version_compare( $oldVersion, '1.5', '<' ) ) {
184 + wp_clear_scheduled_hook( CronService::CRON_CARRIERS_HOOK );
185 + }
156 186
157 - if ( $oldVersion && version_compare( $oldVersion, '1.2.6', '<' ) ) {
158 - $this->orderRepository->deleteOrphans();
159 - }
187 + if ( version_compare( $oldVersion, '1.6.0', '<' ) ) {
188 + wp_clear_scheduled_hook( CronService::CRON_LOG_AUTO_DELETION_HOOK );
189 + wp_clear_scheduled_hook( CronService::CRON_PACKET_STATUS_SYNC_HOOK );
190 + }
160 191
161 - if ( $oldVersion && version_compare( $oldVersion, '1.4.2', '<' ) ) {
162 - $version_1_4_2 = new Version_1_4_2( $this->wpdbAdapter );
163 - $version_1_4_2->run();
192 + if ( version_compare( $oldVersion, '1.7.0', '<' ) ) {
193 + $orderStatusAutoChange = $this->optionsProvider->isOrderStatusAutoChangeEnabled();
194 + $autoOrderStatus = $this->optionsProvider->getAutoOrderStatus();
195 + $syncSettings = $this->optionsProvider->getOptionsByName( OptionNames::PACKETERY_SYNC );
196 + if ( $orderStatusAutoChange ) {
197 + $syncSettings['allow_order_status_change'] = true;
198 + }
199 + if ( $autoOrderStatus !== null ) {
200 + $syncSettings['order_status_change_packet_statuses'] = [
201 + Core\Entity\PacketStatus::RECEIVED_DATA => $autoOrderStatus,
202 + ];
203 + }
204 +
205 + update_option( OptionNames::PACKETERY_SYNC, $syncSettings );
206 + }
207 +
208 + if ( version_compare( $oldVersion, '1.7.1', '<' ) ) {
209 + $syncSettings = $this->optionsProvider->getOptionsByName( OptionNames::PACKETERY_SYNC );
210 + if (
211 + isset( $syncSettings['order_status_change_packet_statuses'][ Core\Entity\PacketStatus::RECEIVED_DATA ] ) &&
212 + $syncSettings['order_status_change_packet_statuses'][ Core\Entity\PacketStatus::RECEIVED_DATA ] === ''
213 + ) {
214 + unset( $syncSettings['order_status_change_packet_statuses'][ Core\Entity\PacketStatus::RECEIVED_DATA ] );
215 + update_option( OptionNames::PACKETERY_SYNC, $syncSettings );
216 + }
217 + }
218 +
219 + if ( version_compare( $oldVersion, '1.8.0', '<' ) ) {
220 + $generalSettings = $this->optionsProvider->getOptionsByName( OptionNames::PACKETERY );
221 + if ( isset( $generalSettings['cod_payment_method'] ) ) {
222 + $generalSettings['cod_payment_methods'] = [ $generalSettings['cod_payment_method'] ];
223 + unset( $generalSettings['cod_payment_method'] );
224 + }
225 +
226 + update_option( OptionNames::PACKETERY, $generalSettings );
227 + }
228 +
229 + if ( version_compare( $oldVersion, '2.1.0', '<' ) ) {
230 + add_option( OptionNames::PACKETERY_TUTORIAL_ORDER_DETAIL_EDIT_PACKET, 0 );
231 + add_option( OptionNames::PACKETERY_TUTORIAL_ORDER_GRID_EDIT_PACKET, 0 );
232 + }
233 + } else {
234 + add_option( OptionNames::PACKETERY_TUTORIAL_ORDER_DETAIL_EDIT_PACKET, 1 );
235 + add_option( OptionNames::PACKETERY_TUTORIAL_ORDER_GRID_EDIT_PACKET, 1 );
164 236 }
165 237
166 - if ( $oldVersion && version_compare( $oldVersion, '1.5', '<' ) ) {
167 - wp_clear_scheduled_hook( CronService::CRON_CARRIERS_HOOK );
168 - }
238 + update_option( OptionNames::VERSION, Plugin::VERSION );
239 + }
169 240
170 - update_option( 'packetery_version', Plugin::VERSION );
241 + public function runCreateTables(): void {
242 + $this->createLogTable();
243 + $this->createCarrierTable();
244 + $this->createOrderTable();
245 + $this->createCustomsDeclarationTables();
171 246 }
172 247
173 248 /**
174 249 * Migrates WP order metadata.
@@ -180,10 +255,21 @@
180 255
181 256 // Did not work when called from plugins_loaded hook.
182 257 $orders = wc_get_orders(
183 258 [
184 - 'packetery_all' => '1',
185 - 'nopaging' => true,
259 + 'nopaging' => true,
260 + // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
261 + 'meta_query' => [
262 + [
263 + 'key' => self::META_CARRIER_ID,
264 + 'compare' => 'EXISTS',
265 + ],
266 + [
267 + 'key' => self::META_CARRIER_ID,
268 + 'compare' => '!=',
269 + 'value' => '',
270 + ],
271 + ],
186 272 ]
187 273 );
188 274
189 275 foreach ( $orders as $order ) {
@@ -188,9 +274,9 @@
188 274
189 275 foreach ( $orders as $order ) {
190 276 $orderEntity = new Core\Entity\Order(
191 277 (string) $order->get_id(),
192 - $this->getMetaAsNullableString( $order, self::META_CARRIER_ID )
278 + $this->carrierEntityRepository->getAnyById( $this->getMetaAsNullableString( $order, self::META_CARRIER_ID ) )
193 279 );
194 280 $order->delete_meta_data( self::META_CARRIER_ID );
195 281
196 282 $orderEntity->setWeight( $this->getMetaAsNullableFloat( $order, self::META_WEIGHT ) );
@@ -221,9 +307,9 @@
221 307 $order->delete_meta_data( self::META_LENGTH );
222 308 $order->delete_meta_data( self::META_WIDTH );
223 309 $order->delete_meta_data( self::META_HEIGHT );
224 310
225 - if ( null !== $this->getMetaAsNullableString( $order, self::META_POINT_ID ) ) {
311 + if ( $this->getMetaAsNullableString( $order, self::META_POINT_ID ) !== null ) {
226 312 $orderEntity->setPickupPoint(
227 313 new Core\Entity\PickupPoint(
228 314 $this->getMetaAsNullableString( $order, self::META_POINT_ID ),
229 315 $this->getMetaAsNullableString( $order, self::META_POINT_NAME ),
@@ -241,9 +327,9 @@
241 327 $order->delete_meta_data( self::META_POINT_STREET );
242 328 $order->delete_meta_data( self::META_POINT_URL );
243 329
244 330 $validatedAddressId = $this->getValidatedAddressIdByOrderId( (int) $order->get_id() );
245 - if ( $validatedAddressId ) {
331 + if ( $validatedAddressId !== null ) {
246 332 $validatedAddress = $this->createAddressFromPostId( $validatedAddressId );
247 333 $orderEntity->setAddressValidated( true );
248 334 $orderEntity->setDeliveryAddress( $validatedAddress );
249 335 }
@@ -292,9 +378,9 @@
292 378 'post_parent' => $orderId,
293 379 ]
294 380 );
295 381
296 - if ( empty( $postIds ) ) {
382 + if ( count( $postIds ) === 0 ) {
297 383 return null;
298 384 }
299 385
300 386 return (int) array_shift( $postIds );
@@ -309,9 +395,10 @@
309 395 * @return string|null
310 396 */
311 397 private function getMetaAsNullableString( \WC_Order $order, string $key ): ?string {
312 398 $value = $order->get_meta( $key, true );
313 - return ( ( null !== $value && '' !== $value ) ? (string) $value : null );
399 +
400 + return ( ( $value !== null && $value !== '' ) ? (string) $value : null );
314 401 }
315 402
316 403 /**
317 404 * Gets meta property of order as float.
@@ -322,64 +409,22 @@
322 409 * @return float|null
323 410 */
324 411 private function getMetaAsNullableFloat( \WC_Order $order, string $key ): ?float {
325 412 $value = $order->get_meta( $key, true );
326 - return ( ( null !== $value && '' !== $value ) ? (float) $value : null );
327 - }
328 413
329 - /**
330 - * Transforms custom query variable to meta query.
331 - *
332 - * @param array $queryVars Query vars.
333 - * @param array $get Input values.
334 - *
335 - * @return array
336 - */
337 - public function handleCustomQueryVar( array $queryVars, array $get ): array {
338 - $metaQuery = $this->addQueryVars( ( $queryVars['meta_query'] ?? [] ), $get );
339 - if ( $metaQuery ) {
340 - // @codingStandardsIgnoreStart
341 - $queryVars['meta_query'] = $metaQuery;
342 - // @codingStandardsIgnoreEnd
343 - }
344 -
345 - return $queryVars;
414 + return ( ( $value !== null && $value !== '' ) ? (float) $value : null );
346 415 }
347 416
348 417 /**
349 - * Adds query vars to fetch order list.
350 - *
351 - * @param array $queryVars Query vars.
352 - * @param array $get Get parameters.
353 - *
354 - * @return array
355 - */
356 - private function addQueryVars( array $queryVars, array $get ): array {
357 - if ( ! empty( $get['packetery_all'] ) ) {
358 - $queryVars[] = [
359 - 'key' => self::META_CARRIER_ID,
360 - 'compare' => 'EXISTS',
361 - ];
362 - $queryVars[] = [
363 - 'key' => self::META_CARRIER_ID,
364 - 'value' => '',
365 - 'compare' => '!=',
366 - ];
367 - }
368 -
369 - return $queryVars;
370 - }
371 -
372 - /**
373 418 * Creates log table.
374 419 *
375 420 * @return void
376 421 */
377 422 private function createLogTable(): void {
378 - if ( false === $this->logRepository->createOrAlterTable() ) {
423 + if ( $this->logRepository->createOrAlterTable() === false ) {
379 424 $this->messageManager->flash_message(
380 425 // translators: %s: Short table name.
381 - sprintf( __( 'Database %s table could not be created, more information might be found in WooCommerce logs.', 'packeta' ), 'log' ),
426 + sprintf( __( 'Database %s table could not be created or altered, more information might be found in WooCommerce logs.', 'packeta' ), 'log' ),
382 427 MessageManager::TYPE_ERROR
383 428 );
384 429 }
385 430 }
@@ -389,9 +434,9 @@
389 434 *
390 435 * @return void
391 436 */
392 437 private function createCarrierTable(): void {
393 - if ( false === $this->carrierRepository->createOrAlterTable() ) {
438 + if ( $this->carrierRepository->createOrAlterTable() === false ) {
394 439 $this->flashAndLog( 'carrier', Record::ACTION_CARRIER_TABLE_NOT_CREATED );
395 440 }
396 441 }
397 442
@@ -400,25 +445,39 @@
400 445 *
401 446 * @return void
402 447 */
403 448 private function createOrderTable(): void {
404 - if ( false === $this->orderRepository->createOrAlterTable() ) {
449 + if ( $this->orderRepository->createOrAlterTable() === false ) {
405 450 $this->flashAndLog( 'order', Record::ACTION_ORDER_TABLE_NOT_CREATED );
406 451 }
407 452 }
408 453
409 454 /**
455 + * Creates order table.
456 + *
457 + * @return void
458 + */
459 + private function createCustomsDeclarationTables(): void {
460 + if ( $this->customsDeclarationRepository->createOrAlterTable() === false ) {
461 + $this->flashAndLog( 'customs_declaration', Record::ACTION_CUSTOMS_DECLARATION_TABLE_NOT_CREATED );
462 + }
463 +
464 + if ( $this->customsDeclarationRepository->createOrAlterItemTable() === false ) {
465 + $this->flashAndLog( 'customs_declaration_item', Record::ACTION_CUSTOMS_DECLARATION_ITEM_TABLE_NOT_CREATED );
466 + }
467 + }
468 +
469 + /**
410 470 * Flashes error and logs to Packeta log.
411 471 *
412 472 * @param string $table Short table name.
413 - * @param string $logMessage Message to log.
414 473 * @param string $action Log action.
415 474 *
416 475 * @return void
417 476 */
418 - private function flashAndLog( string $table, string $logMessage, string $action ): void {
477 + private function flashAndLog( string $table, string $action ): void {
419 478 // translators: %s: Short table name.
420 - $flashMessage = sprintf( __( 'Database %s table of Packeta plugin could not be created, you can find more information in Packeta log.', 'packeta' ), $table );
479 + $flashMessage = sprintf( __( 'Database %s table of Packeta plugin could not be created or altered, you can find more information in Packeta log.', 'packeta' ), $table );
421 480 $this->messageManager->flash_message( $flashMessage, MessageManager::TYPE_ERROR );
422 481
423 482 $record = new Record();
424 483 $record->action = $action;
@@ -423,9 +482,8 @@
423 482 $record = new Record();
424 483 $record->action = $action;
425 484 $record->status = Record::STATUS_ERROR;
426 485 // translators: %s: Short table name.
427 - $record->title = sprintf( __( 'Database %s table could not be created, more information might be found in WooCommerce logs.', 'packeta' ), $table );
486 + $record->title = sprintf( __( 'Database %s table could not be created or altered, more information might be found in WooCommerce logs.', 'packeta' ), $table );
428 487 $this->logger->add( $record );
429 488 }
430 -
431 489 }