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 +126 -66 1.6.22.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.
@@ -87,18 +90,19 @@
87 90 */
88 91 private $wpdbAdapter;
89 92
90 93 /**
91 - * Constructor.
94 + * Options provider.
92 95 *
93 - * @param Order\Repository $orderRepository Order repository.
94 - * @param MessageManager $messageManager Message manager.
95 - * @param ILogger $logger Logger.
96 - * @param Log\Repository $logRepository Log repository.
97 - * @param WpdbAdapter $wpdbAdapter WpdbAdapter.
98 - * @param Carrier\Repository $carrierRepository Carrier repository.
99 - * @param CustomsDeclaration\Repository $customsDeclarationRepository Customs declaration repository.
96 + * @var OptionsProvider
100 97 */
98 + private $optionsProvider;
99 +
100 + /**
101 + * @var EntityRepository
102 + */
103 + private $carrierEntityRepository;
104 +
101 105 public function __construct(
102 106 Order\Repository $orderRepository,
103 107 MessageManager $messageManager,
104 108 ILogger $logger,
@@ -104,9 +108,11 @@
104 108 ILogger $logger,
105 109 Log\Repository $logRepository,
106 110 WpdbAdapter $wpdbAdapter,
107 111 Carrier\Repository $carrierRepository,
108 - CustomsDeclaration\Repository $customsDeclarationRepository
112 + CustomsDeclaration\Repository $customsDeclarationRepository,
113 + OptionsProvider $optionsProvider,
114 + EntityRepository $carrierEntityRepository
109 115 ) {
110 116 $this->orderRepository = $orderRepository;
111 117 $this->messageManager = $messageManager;
112 118 $this->logger = $logger;
@@ -113,8 +119,10 @@
113 119 $this->logRepository = $logRepository;
114 120 $this->wpdbAdapter = $wpdbAdapter;
115 121 $this->carrierRepository = $carrierRepository;
116 122 $this->customsDeclarationRepository = $customsDeclarationRepository;
123 + $this->optionsProvider = $optionsProvider;
124 + $this->carrierEntityRepository = $carrierEntityRepository;
117 125 }
118 126
119 127 /**
120 128 * Checks previous plugin version and runs upgrade if needed.
@@ -122,69 +130,120 @@
122 130 *
123 131 * @return void
124 132 */
125 133 public function check(): void {
126 - $oldVersion = get_option( 'packetery_version' );
127 - if ( Plugin::VERSION === $oldVersion ) {
134 + $oldVersion = get_option( OptionNames::VERSION );
135 + if ( $oldVersion === Plugin::VERSION ) {
128 136 return;
129 137 }
130 138
131 - $this->createLogTable();
132 - $this->createCarrierTable();
133 - $this->createOrderTable();
134 - $this->createCustomsDeclarationTables();
139 + $this->runCreateTables();
135 140
136 141 // If no previous version detected, no upgrade will be run.
137 - if ( $oldVersion && version_compare( $oldVersion, '1.2.0', '<' ) ) {
138 - $logEntries = get_posts(
139 - [
140 - 'post_type' => 'packetery_log',
141 - 'post_status' => 'any',
142 - 'nopaging' => true,
143 - 'fields' => 'ids',
144 - ]
145 - );
146 - foreach ( $logEntries as $logEntryId ) {
147 - 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 );
148 172 }
149 173
150 - unregister_post_type( 'packetery_log' );
174 + if ( version_compare( $oldVersion, '1.2.6', '<' ) ) {
175 + $this->orderRepository->deleteOrphans();
176 + }
151 177
152 - $this->migrateWpOrderMetadata();
153 - $addressEntries = get_posts(
154 - [
155 - 'post_type' => self::POST_TYPE_VALIDATED_ADDRESS,
156 - 'post_status' => 'any',
157 - 'nopaging' => true,
158 - 'fields' => 'ids',
159 - ]
160 - );
161 - foreach ( $addressEntries as $addressEntryId ) {
162 - 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();
163 181 }
164 182
165 - unregister_post_type( self::POST_TYPE_VALIDATED_ADDRESS );
166 - }
183 + if ( version_compare( $oldVersion, '1.5', '<' ) ) {
184 + wp_clear_scheduled_hook( CronService::CRON_CARRIERS_HOOK );
185 + }
167 186
168 - if ( $oldVersion && version_compare( $oldVersion, '1.2.6', '<' ) ) {
169 - $this->orderRepository->deleteOrphans();
170 - }
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 + }
171 191
172 - if ( $oldVersion && version_compare( $oldVersion, '1.4.2', '<' ) ) {
173 - $version_1_4_2 = new Version_1_4_2( $this->wpdbAdapter );
174 - $version_1_4_2->run();
175 - }
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 + }
176 204
177 - if ( $oldVersion && version_compare( $oldVersion, '1.5', '<' ) ) {
178 - wp_clear_scheduled_hook( CronService::CRON_CARRIERS_HOOK );
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 );
179 236 }
180 237
181 - if ( $oldVersion && version_compare( $oldVersion, '1.6.0', '<' ) ) {
182 - wp_clear_scheduled_hook( CronService::CRON_LOG_AUTO_DELETION_HOOK );
183 - wp_clear_scheduled_hook( CronService::CRON_PACKET_STATUS_SYNC_HOOK );
184 - }
238 + update_option( OptionNames::VERSION, Plugin::VERSION );
239 + }
185 240
186 - update_option( 'packetery_version', Plugin::VERSION );
241 + public function runCreateTables(): void {
242 + $this->createLogTable();
243 + $this->createCarrierTable();
244 + $this->createOrderTable();
245 + $this->createCustomsDeclarationTables();
187 246 }
188 247
189 248 /**
190 249 * Migrates WP order metadata.
@@ -215,9 +274,9 @@
215 274
216 275 foreach ( $orders as $order ) {
217 276 $orderEntity = new Core\Entity\Order(
218 277 (string) $order->get_id(),
219 - $this->getMetaAsNullableString( $order, self::META_CARRIER_ID )
278 + $this->carrierEntityRepository->getAnyById( $this->getMetaAsNullableString( $order, self::META_CARRIER_ID ) )
220 279 );
221 280 $order->delete_meta_data( self::META_CARRIER_ID );
222 281
223 282 $orderEntity->setWeight( $this->getMetaAsNullableFloat( $order, self::META_WEIGHT ) );
@@ -248,9 +307,9 @@
248 307 $order->delete_meta_data( self::META_LENGTH );
249 308 $order->delete_meta_data( self::META_WIDTH );
250 309 $order->delete_meta_data( self::META_HEIGHT );
251 310
252 - if ( null !== $this->getMetaAsNullableString( $order, self::META_POINT_ID ) ) {
311 + if ( $this->getMetaAsNullableString( $order, self::META_POINT_ID ) !== null ) {
253 312 $orderEntity->setPickupPoint(
254 313 new Core\Entity\PickupPoint(
255 314 $this->getMetaAsNullableString( $order, self::META_POINT_ID ),
256 315 $this->getMetaAsNullableString( $order, self::META_POINT_NAME ),
@@ -268,9 +327,9 @@
268 327 $order->delete_meta_data( self::META_POINT_STREET );
269 328 $order->delete_meta_data( self::META_POINT_URL );
270 329
271 330 $validatedAddressId = $this->getValidatedAddressIdByOrderId( (int) $order->get_id() );
272 - if ( $validatedAddressId ) {
331 + if ( $validatedAddressId !== null ) {
273 332 $validatedAddress = $this->createAddressFromPostId( $validatedAddressId );
274 333 $orderEntity->setAddressValidated( true );
275 334 $orderEntity->setDeliveryAddress( $validatedAddress );
276 335 }
@@ -319,9 +378,9 @@
319 378 'post_parent' => $orderId,
320 379 ]
321 380 );
322 381
323 - if ( empty( $postIds ) ) {
382 + if ( count( $postIds ) === 0 ) {
324 383 return null;
325 384 }
326 385
327 386 return (int) array_shift( $postIds );
@@ -336,9 +395,10 @@
336 395 * @return string|null
337 396 */
338 397 private function getMetaAsNullableString( \WC_Order $order, string $key ): ?string {
339 398 $value = $order->get_meta( $key, true );
340 - return ( ( null !== $value && '' !== $value ) ? (string) $value : null );
399 +
400 + return ( ( $value !== null && $value !== '' ) ? (string) $value : null );
341 401 }
342 402
343 403 /**
344 404 * Gets meta property of order as float.
@@ -349,9 +409,10 @@
349 409 * @return float|null
350 410 */
351 411 private function getMetaAsNullableFloat( \WC_Order $order, string $key ): ?float {
352 412 $value = $order->get_meta( $key, true );
353 - return ( ( null !== $value && '' !== $value ) ? (float) $value : null );
413 +
414 + return ( ( $value !== null && $value !== '' ) ? (float) $value : null );
354 415 }
355 416
356 417 /**
357 418 * Creates log table.
@@ -358,9 +419,9 @@
358 419 *
359 420 * @return void
360 421 */
361 422 private function createLogTable(): void {
362 - if ( false === $this->logRepository->createOrAlterTable() ) {
423 + if ( $this->logRepository->createOrAlterTable() === false ) {
363 424 $this->messageManager->flash_message(
364 425 // translators: %s: Short table name.
365 426 sprintf( __( 'Database %s table could not be created or altered, more information might be found in WooCommerce logs.', 'packeta' ), 'log' ),
366 427 MessageManager::TYPE_ERROR
@@ -373,9 +434,9 @@
373 434 *
374 435 * @return void
375 436 */
376 437 private function createCarrierTable(): void {
377 - if ( false === $this->carrierRepository->createOrAlterTable() ) {
438 + if ( $this->carrierRepository->createOrAlterTable() === false ) {
378 439 $this->flashAndLog( 'carrier', Record::ACTION_CARRIER_TABLE_NOT_CREATED );
379 440 }
380 441 }
381 442
@@ -384,9 +445,9 @@
384 445 *
385 446 * @return void
386 447 */
387 448 private function createOrderTable(): void {
388 - if ( false === $this->orderRepository->createOrAlterTable() ) {
449 + if ( $this->orderRepository->createOrAlterTable() === false ) {
389 450 $this->flashAndLog( 'order', Record::ACTION_ORDER_TABLE_NOT_CREATED );
390 451 }
391 452 }
392 453
@@ -395,13 +456,13 @@
395 456 *
396 457 * @return void
397 458 */
398 459 private function createCustomsDeclarationTables(): void {
399 - if ( false === $this->customsDeclarationRepository->createOrAlterTable() ) {
460 + if ( $this->customsDeclarationRepository->createOrAlterTable() === false ) {
400 461 $this->flashAndLog( 'customs_declaration', Record::ACTION_CUSTOMS_DECLARATION_TABLE_NOT_CREATED );
401 462 }
402 463
403 - if ( false === $this->customsDeclarationRepository->createOrAlterItemTable() ) {
464 + if ( $this->customsDeclarationRepository->createOrAlterItemTable() === false ) {
404 465 $this->flashAndLog( 'customs_declaration_item', Record::ACTION_CUSTOMS_DECLARATION_ITEM_TABLE_NOT_CREATED );
405 466 }
406 467 }
407 468
@@ -424,6 +485,5 @@
424 485 // translators: %s: Short table name.
425 486 $record->title = sprintf( __( 'Database %s table could not be created or altered, more information might be found in WooCommerce logs.', 'packeta' ), $table );
426 487 $this->logger->add( $record );
427 488 }
428 -
429 489 }