Migration_20221028_105818_App.php
3 years ago
Migration_20230109_144830.php
3 years ago
Migration_20230131_121621.php
3 years ago
Migration_20230419_080000.php
3 years ago
Migration_20230425_211517.php
3 years ago
Migration_20230712_180341.php
3 years ago
Migration_20230803_200413_App.php
2 years ago
Migration_20230825_093531_App.php
1 year ago
Migration_20231128_120355_App.php
1 year ago
Migration_20240202_130053_App.php
2 years ago
Migration_20240207_105912_App.php
1 year ago
Migration_20240322_110443_App.php
2 years ago
Migration_20240730_212419_App.php
2 years ago
Migration_20241015_105511_App.php
1 year ago
Migration_20241128_114257_App.php
1 year ago
Migration_20250120_094614_App.php
1 year ago
Migration_20250501_114655_App.php
11 months ago
Migration_20260421_155908_App.php
3 months ago
Migration_20260515_120000_App.php
3 months ago
Migration_20260623_120000_App.php
1 month ago
Migration_20260805_120000_App.php
1 week ago
index.php
3 years ago
Migration_20230825_093531_App.php
72 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Migrations\App; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Doctrine\WPDB\Connection; |
| 9 | use MailPoet\Entities\StatisticsWooCommercePurchaseEntity; |
| 10 | use MailPoet\Migrations\Db\Migration_20230824_054259_Db; |
| 11 | use MailPoet\Migrator\AppMigration; |
| 12 | use MailPoet\WooCommerce\Helper; |
| 13 | |
| 14 | class Migration_20230825_093531_App extends AppMigration { |
| 15 | const DEFAULT_STATUS = Migration_20230824_054259_Db::DEFAULT_STATUS; |
| 16 | |
| 17 | public function run(): void { |
| 18 | |
| 19 | $wooCommerceHelper = $this->container->get(Helper::class); |
| 20 | |
| 21 | // If Woo is not active and the table doesn't exist, we can skip this migration |
| 22 | if (!$wooCommerceHelper->isWooCommerceActive() && !$this->tableExists()) { |
| 23 | return; |
| 24 | } |
| 25 | |
| 26 | // Temporarily skip the queries in WP Playground. |
| 27 | // The SQLite integration doesn't seem to support them yet. |
| 28 | if (Connection::isSQLite()) { |
| 29 | return; |
| 30 | } |
| 31 | |
| 32 | $wooCommerceHelper->isWooCommerceCustomOrdersTableEnabled() ? |
| 33 | $this->populateStatusColumnUsingHpos() : $this->populateStatusColumnUsingPost(); |
| 34 | } |
| 35 | |
| 36 | private function populateStatusColumnUsingHpos(): void { |
| 37 | global $wpdb; |
| 38 | |
| 39 | $revenueTable = esc_sql($this->getTableName()); |
| 40 | $ordersTable = esc_sql($wpdb->prefix . 'wc_orders'); |
| 41 | $wpdb->query($wpdb->prepare( |
| 42 | "UPDATE %i AS rev, %i AS wc SET rev.status=TRIM(Leading 'wc-' FROM wc.status) WHERE wc.id = rev.order_id AND rev.status= %s", |
| 43 | $revenueTable, |
| 44 | $ordersTable, |
| 45 | self::DEFAULT_STATUS |
| 46 | )); |
| 47 | } |
| 48 | |
| 49 | private function populateStatusColumnUsingPost(): void { |
| 50 | global $wpdb; |
| 51 | |
| 52 | $revenueTable = esc_sql($this->getTableName()); |
| 53 | $wpdb->query($wpdb->prepare( |
| 54 | "UPDATE %i AS rev, %i AS wc SET rev.status=TRIM(Leading 'wc-' FROM wc.post_status) WHERE wc.id = rev.order_id AND rev.status= %s", |
| 55 | $revenueTable, |
| 56 | $wpdb->posts, |
| 57 | self::DEFAULT_STATUS |
| 58 | )); |
| 59 | } |
| 60 | |
| 61 | private function getTableName(): string { |
| 62 | return $this->entityManager->getClassMetadata(StatisticsWooCommercePurchaseEntity::class)->getTableName(); |
| 63 | } |
| 64 | |
| 65 | private function tableExists(): bool { |
| 66 | global $wpdb; |
| 67 | |
| 68 | $revenueTable = $this->getTableName(); |
| 69 | return $wpdb->get_var($wpdb->prepare('SHOW TABLES LIKE %s', $wpdb->esc_like($revenueTable))) === $revenueTable; |
| 70 | } |
| 71 | } |
| 72 |