| @@ -11,8 +11,12 @@ | ||
| 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; | |
| 18 | +use Packetery\Module\Upgrade\Version_1_4_2; | |
| 15 | 19 | |
| 16 | 20 | /** |
| 17 | 21 | * Class Upgrade. |
| 18 | 22 | */ |
| @@ -65,25 +69,60 @@ | ||
| 65 | 69 | */ |
| 66 | 70 | private $logRepository; |
| 67 | 71 | |
| 68 | 72 | /** |
| 69 | - * Constructor. | |
| 73 | + * Carrier repository. | |
| 70 | 74 | * |
| 71 | - * @param Order\Repository $orderRepository Order repository. | |
| 72 | - * @param MessageManager $messageManager Message manager. | |
| 73 | - * @param ILogger $logger Logger. | |
| 74 | - * @param Log\Repository $logRepository Log repository. | |
| 75 | + * @var Carrier\Repository | |
| 75 | 76 | */ |
| 77 | + private $carrierRepository; | |
| 78 | + | |
| 79 | + /** | |
| 80 | + * Customs declaration repository. | |
| 81 | + * | |
| 82 | + * @var CustomsDeclaration\Repository | |
| 83 | + */ | |
| 84 | + private $customsDeclarationRepository; | |
| 85 | + | |
| 86 | + /** | |
| 87 | + * WpdbAdapter. | |
| 88 | + * | |
| 89 | + * @var WpdbAdapter | |
| 90 | + */ | |
| 91 | + private $wpdbAdapter; | |
| 92 | + | |
| 93 | + /** | |
| 94 | + * Options provider. | |
| 95 | + * | |
| 96 | + * @var OptionsProvider | |
| 97 | + */ | |
| 98 | + private $optionsProvider; | |
| 99 | + | |
| 100 | + /** | |
| 101 | + * @var EntityRepository | |
| 102 | + */ | |
| 103 | + private $carrierEntityRepository; | |
| 104 | + | |
| 76 | 105 | public function __construct( |
| 77 | 106 | Order\Repository $orderRepository, |
| 78 | 107 | MessageManager $messageManager, |
| 79 | 108 | ILogger $logger, |
| 80 | - Log\Repository $logRepository | |
| 109 | + Log\Repository $logRepository, | |
| 110 | + WpdbAdapter $wpdbAdapter, | |
| 111 | + Carrier\Repository $carrierRepository, | |
| 112 | + CustomsDeclaration\Repository $customsDeclarationRepository, | |
| 113 | + OptionsProvider $optionsProvider, | |
| 114 | + EntityRepository $carrierEntityRepository | |
| 81 | 115 | ) { |
| 82 | - $this->orderRepository = $orderRepository; | |
| 83 | - $this->messageManager = $messageManager; | |
| 84 | - $this->logger = $logger; | |
| 85 | - $this->logRepository = $logRepository; | |
| 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; | |
| 86 | 125 | } |
| 87 | 126 | |
| 88 | 127 | /** |
| 89 | 128 | * Checks previous plugin version and runs upgrade if needed. |
| @@ -91,51 +130,120 @@ | ||
| 91 | 130 | * |
| 92 | 131 | * @return void |
| 93 | 132 | */ |
| 94 | 133 | public function check(): void { |
| 95 | - $oldVersion = get_option( 'packetery_version' ); | |
| 96 | - if ( Plugin::VERSION === $oldVersion ) { | |
| 134 | + $oldVersion = get_option( OptionNames::VERSION ); | |
| 135 | + if ( $oldVersion === Plugin::VERSION ) { | |
| 97 | 136 | return; |
| 98 | 137 | } |
| 99 | 138 | |
| 139 | + $this->runCreateTables(); | |
| 140 | + | |
| 100 | 141 | // If no previous version detected, no upgrade will be run. |
| 101 | - if ( $oldVersion && version_compare( $oldVersion, '1.2.0', '<' ) ) { | |
| 102 | - $this->logRepository->createTable(); | |
| 103 | - $logEntries = get_posts( | |
| 104 | - [ | |
| 105 | - 'post_type' => 'packetery_log', | |
| 106 | - 'post_status' => 'any', | |
| 107 | - 'nopaging' => true, | |
| 108 | - 'fields' => 'ids', | |
| 109 | - ] | |
| 110 | - ); | |
| 111 | - foreach ( $logEntries as $logEntryId ) { | |
| 112 | - 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 ); | |
| 113 | 172 | } |
| 114 | 173 | |
| 115 | - unregister_post_type( 'packetery_log' ); | |
| 174 | + if ( version_compare( $oldVersion, '1.2.6', '<' ) ) { | |
| 175 | + $this->orderRepository->deleteOrphans(); | |
| 176 | + } | |
| 116 | 177 | |
| 117 | - $this->migrateWpOrderMetadata(); | |
| 118 | - $addressEntries = get_posts( | |
| 119 | - [ | |
| 120 | - 'post_type' => self::POST_TYPE_VALIDATED_ADDRESS, | |
| 121 | - 'post_status' => 'any', | |
| 122 | - 'nopaging' => true, | |
| 123 | - 'fields' => 'ids', | |
| 124 | - ] | |
| 125 | - ); | |
| 126 | - foreach ( $addressEntries as $addressEntryId ) { | |
| 127 | - 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(); | |
| 128 | 181 | } |
| 129 | 182 | |
| 130 | - unregister_post_type( self::POST_TYPE_VALIDATED_ADDRESS ); | |
| 183 | + if ( version_compare( $oldVersion, '1.5', '<' ) ) { | |
| 184 | + wp_clear_scheduled_hook( CronService::CRON_CARRIERS_HOOK ); | |
| 185 | + } | |
| 186 | + | |
| 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 | + } | |
| 191 | + | |
| 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 ); | |
| 131 | 236 | } |
| 132 | 237 | |
| 133 | - if ( $oldVersion && version_compare( $oldVersion, '1.2.6', '<' ) ) { | |
| 134 | - $this->orderRepository->deleteOrphans(); | |
| 135 | - } | |
| 238 | + update_option( OptionNames::VERSION, Plugin::VERSION ); | |
| 239 | + } | |
| 136 | 240 | |
| 137 | - update_option( 'packetery_version', Plugin::VERSION ); | |
| 241 | + public function runCreateTables(): void { | |
| 242 | + $this->createLogTable(); | |
| 243 | + $this->createCarrierTable(); | |
| 244 | + $this->createOrderTable(); | |
| 245 | + $this->createCustomsDeclarationTables(); | |
| 138 | 246 | } |
| 139 | 247 | |
| 140 | 248 | /** |
| 141 | 249 | * Migrates WP order metadata. |
| @@ -142,30 +250,26 @@ | ||
| 142 | 250 | * |
| 143 | 251 | * @return void |
| 144 | 252 | */ |
| 145 | 253 | private function migrateWpOrderMetadata(): void { |
| 146 | - global $wpdb; | |
| 254 | + $this->createOrderTable(); | |
| 147 | 255 | |
| 148 | - $createResult = $this->orderRepository->createTable(); | |
| 149 | - if ( false === $createResult ) { | |
| 150 | - $lastError = $wpdb->last_error; | |
| 151 | - $this->messageManager->flash_message( __( 'Database order table was not created, you can find more information in Packeta log.', 'packeta' ), MessageManager::TYPE_ERROR ); | |
| 152 | - | |
| 153 | - $record = new Record(); | |
| 154 | - $record->action = Record::ACTION_ORDER_TABLE_NOT_CREATED; | |
| 155 | - $record->status = Record::STATUS_ERROR; | |
| 156 | - $record->title = __( 'Database order table was not created.', 'packeta' ); | |
| 157 | - $record->params = [ | |
| 158 | - 'errorMessage' => $lastError, | |
| 159 | - ]; | |
| 160 | - $this->logger->add( $record ); | |
| 161 | - } | |
| 162 | - | |
| 163 | 256 | // Did not work when called from plugins_loaded hook. |
| 164 | 257 | $orders = wc_get_orders( |
| 165 | 258 | [ |
| 166 | - 'packetery_all' => '1', | |
| 167 | - '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 | + ], | |
| 168 | 272 | ] |
| 169 | 273 | ); |
| 170 | 274 | |
| 171 | 275 | foreach ( $orders as $order ) { |
| @@ -170,9 +274,9 @@ | ||
| 170 | 274 | |
| 171 | 275 | foreach ( $orders as $order ) { |
| 172 | 276 | $orderEntity = new Core\Entity\Order( |
| 173 | 277 | (string) $order->get_id(), |
| 174 | - $this->getMetaAsNullableString( $order, self::META_CARRIER_ID ) | |
| 278 | + $this->carrierEntityRepository->getAnyById( $this->getMetaAsNullableString( $order, self::META_CARRIER_ID ) ) | |
| 175 | 279 | ); |
| 176 | 280 | $order->delete_meta_data( self::META_CARRIER_ID ); |
| 177 | 281 | |
| 178 | 282 | $orderEntity->setWeight( $this->getMetaAsNullableFloat( $order, self::META_WEIGHT ) ); |
| @@ -203,9 +307,9 @@ | ||
| 203 | 307 | $order->delete_meta_data( self::META_LENGTH ); |
| 204 | 308 | $order->delete_meta_data( self::META_WIDTH ); |
| 205 | 309 | $order->delete_meta_data( self::META_HEIGHT ); |
| 206 | 310 | |
| 207 | - if ( null !== $this->getMetaAsNullableString( $order, self::META_POINT_ID ) ) { | |
| 311 | + if ( $this->getMetaAsNullableString( $order, self::META_POINT_ID ) !== null ) { | |
| 208 | 312 | $orderEntity->setPickupPoint( |
| 209 | 313 | new Core\Entity\PickupPoint( |
| 210 | 314 | $this->getMetaAsNullableString( $order, self::META_POINT_ID ), |
| 211 | 315 | $this->getMetaAsNullableString( $order, self::META_POINT_NAME ), |
| @@ -223,9 +327,9 @@ | ||
| 223 | 327 | $order->delete_meta_data( self::META_POINT_STREET ); |
| 224 | 328 | $order->delete_meta_data( self::META_POINT_URL ); |
| 225 | 329 | |
| 226 | 330 | $validatedAddressId = $this->getValidatedAddressIdByOrderId( (int) $order->get_id() ); |
| 227 | - if ( $validatedAddressId ) { | |
| 331 | + if ( $validatedAddressId !== null ) { | |
| 228 | 332 | $validatedAddress = $this->createAddressFromPostId( $validatedAddressId ); |
| 229 | 333 | $orderEntity->setAddressValidated( true ); |
| 230 | 334 | $orderEntity->setDeliveryAddress( $validatedAddress ); |
| 231 | 335 | } |
| @@ -274,9 +378,9 @@ | ||
| 274 | 378 | 'post_parent' => $orderId, |
| 275 | 379 | ] |
| 276 | 380 | ); |
| 277 | 381 | |
| 278 | - if ( empty( $postIds ) ) { | |
| 382 | + if ( count( $postIds ) === 0 ) { | |
| 279 | 383 | return null; |
| 280 | 384 | } |
| 281 | 385 | |
| 282 | 386 | return (int) array_shift( $postIds ); |
| @@ -291,9 +395,10 @@ | ||
| 291 | 395 | * @return string|null |
| 292 | 396 | */ |
| 293 | 397 | private function getMetaAsNullableString( \WC_Order $order, string $key ): ?string { |
| 294 | 398 | $value = $order->get_meta( $key, true ); |
| 295 | - return ( ( null !== $value && '' !== $value ) ? (string) $value : null ); | |
| 399 | + | |
| 400 | + return ( ( $value !== null && $value !== '' ) ? (string) $value : null ); | |
| 296 | 401 | } |
| 297 | 402 | |
| 298 | 403 | /** |
| 299 | 404 | * Gets meta property of order as float. |
| @@ -304,51 +409,81 @@ | ||
| 304 | 409 | * @return float|null |
| 305 | 410 | */ |
| 306 | 411 | private function getMetaAsNullableFloat( \WC_Order $order, string $key ): ?float { |
| 307 | 412 | $value = $order->get_meta( $key, true ); |
| 308 | - return ( ( null !== $value && '' !== $value ) ? (float) $value : null ); | |
| 413 | + | |
| 414 | + return ( ( $value !== null && $value !== '' ) ? (float) $value : null ); | |
| 309 | 415 | } |
| 310 | 416 | |
| 311 | 417 | /** |
| 312 | - * Transforms custom query variable to meta query. | |
| 418 | + * Creates log table. | |
| 313 | 419 | * |
| 314 | - * @param array $queryVars Query vars. | |
| 315 | - * @param array $get Input values. | |
| 420 | + * @return void | |
| 421 | + */ | |
| 422 | + private function createLogTable(): void { | |
| 423 | + if ( $this->logRepository->createOrAlterTable() === false ) { | |
| 424 | + $this->messageManager->flash_message( | |
| 425 | + // translators: %s: Short table name. | |
| 426 | + sprintf( __( 'Database %s table could not be created or altered, more information might be found in WooCommerce logs.', 'packeta' ), 'log' ), | |
| 427 | + MessageManager::TYPE_ERROR | |
| 428 | + ); | |
| 429 | + } | |
| 430 | + } | |
| 431 | + | |
| 432 | + /** | |
| 433 | + * Creates carrier table. | |
| 316 | 434 | * |
| 317 | - * @return array | |
| 435 | + * @return void | |
| 318 | 436 | */ |
| 319 | - public function handleCustomQueryVar( array $queryVars, array $get ): array { | |
| 320 | - $metaQuery = $this->addQueryVars( ( $queryVars['meta_query'] ?? [] ), $get ); | |
| 321 | - if ( $metaQuery ) { | |
| 322 | - // @codingStandardsIgnoreStart | |
| 323 | - $queryVars['meta_query'] = $metaQuery; | |
| 324 | - // @codingStandardsIgnoreEnd | |
| 437 | + private function createCarrierTable(): void { | |
| 438 | + if ( $this->carrierRepository->createOrAlterTable() === false ) { | |
| 439 | + $this->flashAndLog( 'carrier', Record::ACTION_CARRIER_TABLE_NOT_CREATED ); | |
| 325 | 440 | } |
| 441 | + } | |
| 326 | 442 | |
| 327 | - return $queryVars; | |
| 443 | + /** | |
| 444 | + * Creates order table. | |
| 445 | + * | |
| 446 | + * @return void | |
| 447 | + */ | |
| 448 | + private function createOrderTable(): void { | |
| 449 | + if ( $this->orderRepository->createOrAlterTable() === false ) { | |
| 450 | + $this->flashAndLog( 'order', Record::ACTION_ORDER_TABLE_NOT_CREATED ); | |
| 451 | + } | |
| 328 | 452 | } |
| 329 | 453 | |
| 330 | 454 | /** |
| 331 | - * Adds query vars to fetch order list. | |
| 455 | + * Creates order table. | |
| 332 | 456 | * |
| 333 | - * @param array $queryVars Query vars. | |
| 334 | - * @param array $get Get parameters. | |
| 335 | - * | |
| 336 | - * @return array | |
| 457 | + * @return void | |
| 337 | 458 | */ |
| 338 | - private function addQueryVars( array $queryVars, array $get ): array { | |
| 339 | - if ( ! empty( $get['packetery_all'] ) ) { | |
| 340 | - $queryVars[] = [ | |
| 341 | - 'key' => self::META_CARRIER_ID, | |
| 342 | - 'compare' => 'EXISTS', | |
| 343 | - ]; | |
| 344 | - $queryVars[] = [ | |
| 345 | - 'key' => self::META_CARRIER_ID, | |
| 346 | - 'value' => '', | |
| 347 | - 'compare' => '!=', | |
| 348 | - ]; | |
| 459 | + private function createCustomsDeclarationTables(): void { | |
| 460 | + if ( $this->customsDeclarationRepository->createOrAlterTable() === false ) { | |
| 461 | + $this->flashAndLog( 'customs_declaration', Record::ACTION_CUSTOMS_DECLARATION_TABLE_NOT_CREATED ); | |
| 349 | 462 | } |
| 350 | 463 | |
| 351 | - return $queryVars; | |
| 464 | + if ( $this->customsDeclarationRepository->createOrAlterItemTable() === false ) { | |
| 465 | + $this->flashAndLog( 'customs_declaration_item', Record::ACTION_CUSTOMS_DECLARATION_ITEM_TABLE_NOT_CREATED ); | |
| 466 | + } | |
| 352 | 467 | } |
| 353 | 468 | |
| 469 | + /** | |
| 470 | + * Flashes error and logs to Packeta log. | |
| 471 | + * | |
| 472 | + * @param string $table Short table name. | |
| 473 | + * @param string $action Log action. | |
| 474 | + * | |
| 475 | + * @return void | |
| 476 | + */ | |
| 477 | + private function flashAndLog( string $table, string $action ): void { | |
| 478 | + // translators: %s: Short table name. | |
| 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 ); | |
| 480 | + $this->messageManager->flash_message( $flashMessage, MessageManager::TYPE_ERROR ); | |
| 481 | + | |
| 482 | + $record = new Record(); | |
| 483 | + $record->action = $action; | |
| 484 | + $record->status = Record::STATUS_ERROR; | |
| 485 | + // translators: %s: Short table name. | |
| 486 | + $record->title = sprintf( __( 'Database %s table could not be created or altered, more information might be found in WooCommerce logs.', 'packeta' ), $table ); | |
| 487 | + $this->logger->add( $record ); | |
| 488 | + } | |
| 354 | 489 | } |