| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yatra\Migration; |
| 4 |
|
| 5 |
use Yatra\Core\Modules\ModuleManager; |
| 6 |
use Yatra\Utils\Logger; |
| 7 |
|
| 8 |
/** |
| 9 |
* Migrate legacy Pro feature toggles to the new Pro module enablement option. |
| 10 |
* |
| 11 |
* Legacy: wp_options.yatra_pro_features (array of feature => bool) |
| 12 |
* New Pro: wp_options.yatra_pro_modules_enabled (list of module slugs) |
| 13 |
* |
| 14 |
* Old Pro 2.x feature keys (see yatra-pro-old Admin\Features::get_available_features) and outcome: |
| 15 |
* - services → additional_services (module exists in Pro 3.x) |
| 16 |
* - google_calendar → google_calendar |
| 17 |
* - partial_payment → flexible_payments |
| 18 |
* - payment_gateways → no module slug (gateways live in core SettingsMigration + ProGatewayRegistration) |
| 19 |
* - review_ratings → no module (reviews: ReviewMigration + ProReviewCptMigration; settings in SettingsMigration) |
| 20 |
* - downloads → no module (data: ProDownloadsMigration) |
| 21 |
* - available_conditions → no module (data: AvailabilityConditionsMigration in core) |
| 22 |
* |
| 23 |
* When every legacy Pro 2.x feature flag in yatra_pro_features is on, we enable all Pro 3.x modules |
| 24 |
* that exist in the new plugin (extras like trip_consent were not in 2.x but match "everything on"). |
| 25 |
*/ |
| 26 |
class ProFeaturesMigration extends BaseMigration |
| 27 |
{ |
| 28 |
/** Keys from old yatra-pro Admin\Features::get_available_features(). */ |
| 29 |
private const LEGACY_PRO_FEATURE_KEYS = [ |
| 30 |
'payment_gateways', |
| 31 |
'services', |
| 32 |
'partial_payment', |
| 33 |
'review_ratings', |
| 34 |
'google_calendar', |
| 35 |
'downloads', |
| 36 |
'available_conditions', |
| 37 |
]; |
| 38 |
|
| 39 |
/** Pro 3.x module slugs (see yatra-pro ProModuleRepository::$modules). */ |
| 40 |
private const PRO_3_MODULE_SLUGS = [ |
| 41 |
'google_calendar', |
| 42 |
'additional_services', |
| 43 |
'trip_consent', |
| 44 |
'email_automation', |
| 45 |
'dynamic_form_field', |
| 46 |
'advanced_discount', |
| 47 |
'mailchimp', |
| 48 |
'facebook_pixel', |
| 49 |
'flexible_payments', |
| 50 |
'scheduled_payments', |
| 51 |
'dynamic_pricing', |
| 52 |
]; |
| 53 |
|
| 54 |
public function run(): array |
| 55 |
{ |
| 56 |
$migrated = 0; |
| 57 |
$skipped = 0; |
| 58 |
$failed = 0; |
| 59 |
|
| 60 |
$legacy = get_option('yatra_pro_features', []); |
| 61 |
$total = (is_array($legacy) && array_filter($legacy)) ? 1 : 0; |
| 62 |
if ($total === 0) { |
| 63 |
return compact('migrated', 'skipped', 'failed'); |
| 64 |
} |
| 65 |
|
| 66 |
$pro = ProMigrationReadiness::getState(); |
| 67 |
if (!$pro['ready']) { |
| 68 |
// We cannot safely write module enablement without the new Pro conventions being present. |
| 69 |
$skipped = 1; |
| 70 |
Logger::warning('Skipping ProFeaturesMigration: Yatra Pro 3.0+ not ready', [ |
| 71 |
'source' => 'migration', |
| 72 |
'pro_migration' => $pro, |
| 73 |
]); |
| 74 |
$this->updateProgress('pro_features', 'running', $migrated, $skipped, $failed, $total, null, null); |
| 75 |
|
| 76 |
return compact('migrated', 'skipped', 'failed'); |
| 77 |
} |
| 78 |
|
| 79 |
try { |
| 80 |
$legacy = is_array($legacy) ? $legacy : []; |
| 81 |
|
| 82 |
$enabled = []; |
| 83 |
|
| 84 |
if ($this->allLegacyProFeaturesWereEnabled($legacy)) { |
| 85 |
$enabled = self::PRO_3_MODULE_SLUGS; |
| 86 |
Logger::info('ProFeaturesMigration: all legacy Pro feature toggles on — enabling full Pro 3.x module set.', [ |
| 87 |
'source' => 'migration', |
| 88 |
'modules' => $enabled, |
| 89 |
]); |
| 90 |
} else { |
| 91 |
// Map legacy feature keys to new Pro module slugs where there is an equivalent. |
| 92 |
$map = [ |
| 93 |
'services' => 'additional_services', |
| 94 |
'google_calendar' => 'google_calendar', |
| 95 |
'partial_payment' => 'flexible_payments', |
| 96 |
]; |
| 97 |
|
| 98 |
foreach ($map as $legacyKey => $newSlug) { |
| 99 |
if (!empty($legacy[$legacyKey])) { |
| 100 |
$enabled[] = $newSlug; |
| 101 |
} |
| 102 |
} |
| 103 |
$enabled = array_values(array_unique($enabled)); |
| 104 |
} |
| 105 |
|
| 106 |
$existing = get_option('yatra_pro_modules_enabled', []); |
| 107 |
if (!is_array($existing)) { |
| 108 |
$existing = []; |
| 109 |
} |
| 110 |
|
| 111 |
$final = $this->isForceMigration() |
| 112 |
? array_values(array_unique(array_merge($existing, $enabled))) |
| 113 |
: array_values(array_unique(array_merge($enabled, $existing))); |
| 114 |
|
| 115 |
update_option('yatra_pro_modules_enabled', $final); |
| 116 |
|
| 117 |
// Core 3.x reads module on/off from yatra_modules (ModuleManager). Legacy Pro only set |
| 118 |
// yatra_pro_modules_enabled — without this, Additional Services and other Pro modules stay disabled in UI/runtime. |
| 119 |
if (class_exists(ModuleManager::class)) { |
| 120 |
$knownSlugs = array_column(ModuleManager::getDefaultModules(), 'slug'); |
| 121 |
foreach ($final as $slug) { |
| 122 |
$slug = is_string($slug) ? trim($slug) : ''; |
| 123 |
if ($slug === '' || !in_array($slug, $knownSlugs, true)) { |
| 124 |
continue; |
| 125 |
} |
| 126 |
ModuleManager::setModuleStatus($slug, true); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
$migrated = 1; |
| 131 |
$this->updateProgress('pro_features', 'running', $migrated, $skipped, $failed, $total, null, null); |
| 132 |
} catch (\Throwable $e) { |
| 133 |
$failed = 1; |
| 134 |
Logger::error('ProFeaturesMigration failed', [ |
| 135 |
'source' => 'migration', |
| 136 |
'error' => $e->getMessage(), |
| 137 |
]); |
| 138 |
$this->updateProgress('pro_features', 'running', $migrated, $skipped, $failed, $total, null, null); |
| 139 |
} |
| 140 |
|
| 141 |
return compact('migrated', 'skipped', 'failed'); |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* True when every legacy Pro 2.x feature we know about is enabled in the saved option. |
| 146 |
* Uses a count so slightly different option shapes (missing keys) still match "all on" when appropriate. |
| 147 |
*/ |
| 148 |
private function allLegacyProFeaturesWereEnabled(array $legacy): bool |
| 149 |
{ |
| 150 |
$required = count(self::LEGACY_PRO_FEATURE_KEYS); |
| 151 |
$enabledCount = 0; |
| 152 |
foreach (self::LEGACY_PRO_FEATURE_KEYS as $key) { |
| 153 |
if (!empty($legacy[$key])) { |
| 154 |
$enabledCount++; |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
return $enabledCount >= $required; |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
|