Migration_20221028_105818.php
3 months ago
Migration_20221110_151621.php
3 years ago
Migration_20230111_120000.php
2 years ago
Migration_20230111_130000.php
3 years ago
Migration_20230215_050813.php
3 months ago
Migration_20230221_200520.php
3 years ago
Migration_20230421_135915.php
3 years ago
Migration_20230503_210945.php
3 years ago
Migration_20230605_174836.php
3 years ago
Migration_20230703_105957.php
3 years ago
Migration_20230716_130221_Db.php
2 years ago
Migration_20230824_054259_Db.php
2 years ago
Migration_20230831_124214_Db.php
2 years ago
Migration_20230831_143755_Db.php
1 year ago
Migration_20240119_113943_Db.php
2 years ago
Migration_20240617_122847_Db.php
2 years ago
Migration_20240725_182318_Db.php
2 years ago
Migration_20241007_170437_Db.php
1 year ago
Migration_20241108_103249_Db.php
3 months ago
Migration_20250903_151331_Db.php
11 months ago
Migration_20250926_153050_Db.php
10 months ago
Migration_20260415_090055_Db.php
3 months ago
Migration_20260427_100000.php
3 months ago
Migration_20260428_120000.php
3 months ago
Migration_20260430_103000_Db.php
3 months ago
Migration_20260430_120000.php
3 months ago
Migration_20260504_120000_Db.php
3 months ago
Migration_20260514_120000_Db.php
3 months ago
Migration_20260609_120000_Db.php
2 months ago
Migration_20260610_120000_Db.php
1 month ago
Migration_20260622_120000_Db.php
1 month ago
Migration_20260709_120000_Db.php
1 month ago
Migration_20260715_100000_Db.php
1 month ago
index.php
3 years ago
Migration_20241108_103249_Db.php
102 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Migrations\Db; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Automation\Engine\Utils\Json; |
| 9 | use MailPoet\Migrator\DbMigration; |
| 10 | |
| 11 | /** |
| 12 | * Fixes WooCommerce Subscriptions status filter in automations (e.g. trigger filters or if/else conditions) |
| 13 | * - Changes `field_type` from `enum_array` to `enum` |
| 14 | * - Changes condition from `matches-*` to `is-*` |
| 15 | * - Strips `wc-` prefix from values |
| 16 | */ |
| 17 | class Migration_20241108_103249_Db extends DbMigration { |
| 18 | public function run(): void { |
| 19 | global $wpdb; |
| 20 | $automationVersionsTable = esc_sql($wpdb->prefix . 'mailpoet_automation_versions'); |
| 21 | |
| 22 | $wooSubscriptionAutomations = $wpdb->get_results( |
| 23 | $wpdb->prepare( |
| 24 | "SELECT `id`, `steps` FROM %i WHERE `steps` LIKE %s OR `steps` LIKE %s", |
| 25 | $automationVersionsTable, |
| 26 | '%woocommerce-subscriptions:subscription:status%', |
| 27 | '%woocommerce-subscriptions:subscription:billing-period%' |
| 28 | ), |
| 29 | ARRAY_A |
| 30 | ); |
| 31 | |
| 32 | foreach ($wooSubscriptionAutomations as $automation) { |
| 33 | // Parse JSON encoded automation steps |
| 34 | try { |
| 35 | $jsonData = Json::decode($automation['steps']); |
| 36 | } catch (\Exception $e) { |
| 37 | continue; |
| 38 | } |
| 39 | |
| 40 | // Iterate over each automation step |
| 41 | foreach ($jsonData as $key => &$item) { |
| 42 | if (!is_array($item) || !isset($item['filters']) || !is_array($item['filters'])) { |
| 43 | continue; |
| 44 | } |
| 45 | if (!isset($item['filters']['groups']) || !is_array($item['filters']['groups'])) { |
| 46 | continue; |
| 47 | } |
| 48 | |
| 49 | // Iterate over each step filter group |
| 50 | // This can be e.g. a trigger filter, or if/else condition |
| 51 | foreach ($item['filters']['groups'] as &$group) { |
| 52 | if (!is_array($group) || !isset($group['filters']) || !is_array($group['filters'])) { |
| 53 | continue; |
| 54 | } |
| 55 | foreach ($group['filters'] as &$filter_item) { |
| 56 | $this->fixWooSubscriptionFilters($filter_item); |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | $updatedJson = json_encode($jsonData); |
| 61 | $wpdb->update($automationVersionsTable, ['steps' => $updatedJson], ['id' => $automation['id']]); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | private function fixWooSubscriptionFilters(&$filter_item): void { |
| 66 | // Only fix WooCommerce Subscriptions status and billing period filters |
| 67 | if (!isset($filter_item['field_key'])) { |
| 68 | return; |
| 69 | } |
| 70 | $isStatusField = $filter_item['field_key'] === 'woocommerce-subscriptions:subscription:status'; |
| 71 | $isBillingPeriodField = $filter_item['field_key'] === 'woocommerce-subscriptions:subscription:billing-period'; |
| 72 | if (!$isStatusField && !$isBillingPeriodField) { |
| 73 | return; |
| 74 | } |
| 75 | |
| 76 | // Fix field_type from enum_array to enum |
| 77 | if (isset($filter_item['field_type']) && $filter_item['field_type'] === 'enum_array') { |
| 78 | $filter_item['field_type'] = 'enum'; |
| 79 | } |
| 80 | |
| 81 | // Fix condition |
| 82 | if (isset($filter_item['condition'])) { |
| 83 | $conditionMap = [ |
| 84 | 'matches-any-of' => 'is-any-of', |
| 85 | 'matches-all-of' => 'is-any-of', |
| 86 | 'matches-none-of' => 'is-none-of', |
| 87 | ]; |
| 88 | $filter_item['condition'] = $conditionMap[$filter_item['condition']] ?? $filter_item['condition']; |
| 89 | } |
| 90 | |
| 91 | // Strip "wc-" prefix from values but only in status field |
| 92 | if (!$isStatusField) { |
| 93 | return; |
| 94 | } |
| 95 | if (isset($filter_item['args']['value']) && is_array($filter_item['args']['value'])) { |
| 96 | $filter_item['args']['value'] = array_map(function($value) { |
| 97 | return is_string($value) ? preg_replace('/^wc-/', '', $value) : $value; |
| 98 | }, $filter_item['args']['value']); |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 |