PluginProbe
Yatra – Travel Booking & Tour Operator Software / 3.0.2.7
Yatra – Travel Booking & Tour Operator Software v3.0.2.7
3.0.16 3.0.15 3.0.14 3.0.14.1 3.0.14.2 3.0.12 3.0.13 3.0.11 3.0.10 3.0.9 3.0.8 3.0.7 3.0.6 3.0.5 3.0.5.1 3.0.4 3.0.3 3.0.2.9 3.0.2.7 3.0.2.8 3.0.2.6 trunk 1.0.0 2.0.0 2.0.1 All 84 releases
yatra / app / Migrations / ProFeaturesMigration.php

ProFeaturesMigration.php in Yatra – Travel Booking & Tour Operator Software 3.0.2.7, at app/Migrations/ProFeaturesMigration.php

161 lines 6.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 'dynamic_pricing',
51 ];
52
53 public function run(): array
54 {
55 $migrated = 0;
56 $skipped = 0;
57 $failed = 0;
58
59 $legacy = get_option('yatra_pro_features', []);
60 $total = (is_array($legacy) && array_filter($legacy)) ? 1 : 0;
61 if ($total === 0) {
62 return compact('migrated', 'skipped', 'failed');
63 }
64
65 $pro = ProMigrationReadiness::getState();
66 if (!$pro['ready']) {
67 // We cannot safely write module enablement without the new Pro conventions being present.
68 $skipped = 1;
69 Logger::warning('Skipping ProFeaturesMigration: Yatra Pro 3.0+ not ready', [
70 'source' => 'migration',
71 'pro_migration' => $pro,
72 ]);
73 $this->updateProgress('pro_features', 'running', $migrated, $skipped, $failed, $total, null, null);
74
75 return compact('migrated', 'skipped', 'failed');
76 }
77
78 try {
79 $legacy = is_array($legacy) ? $legacy : [];
80
81 $enabled = [];
82
83 if ($this->allLegacyProFeaturesWereEnabled($legacy)) {
84 $enabled = self::PRO_3_MODULE_SLUGS;
85 Logger::info('ProFeaturesMigration: all legacy Pro feature toggles on — enabling full Pro 3.x module set.', [
86 'source' => 'migration',
87 'modules' => $enabled,
88 ]);
89 } else {
90 // Map legacy feature keys to new Pro module slugs where there is an equivalent.
91 $map = [
92 'services' => 'additional_services',
93 'google_calendar' => 'google_calendar',
94 'partial_payment' => 'flexible_payments',
95 ];
96
97 foreach ($map as $legacyKey => $newSlug) {
98 if (!empty($legacy[$legacyKey])) {
99 $enabled[] = $newSlug;
100 }
101 }
102 $enabled = array_values(array_unique($enabled));
103 }
104
105 $existing = get_option('yatra_pro_modules_enabled', []);
106 if (!is_array($existing)) {
107 $existing = [];
108 }
109
110 $final = $this->isForceMigration()
111 ? array_values(array_unique(array_merge($existing, $enabled)))
112 : array_values(array_unique(array_merge($enabled, $existing)));
113
114 update_option('yatra_pro_modules_enabled', $final);
115
116 // Core 3.x reads module on/off from yatra_modules (ModuleManager). Legacy Pro only set
117 // yatra_pro_modules_enabled — without this, Additional Services and other Pro modules stay disabled in UI/runtime.
118 if (class_exists(ModuleManager::class)) {
119 $knownSlugs = array_column(ModuleManager::getDefaultModules(), 'slug');
120 foreach ($final as $slug) {
121 $slug = is_string($slug) ? trim($slug) : '';
122 if ($slug === '' || !in_array($slug, $knownSlugs, true)) {
123 continue;
124 }
125 ModuleManager::setModuleStatus($slug, true);
126 }
127 }
128
129 $migrated = 1;
130 $this->updateProgress('pro_features', 'running', $migrated, $skipped, $failed, $total, null, null);
131 } catch (\Throwable $e) {
132 $failed = 1;
133 Logger::error('ProFeaturesMigration failed', [
134 'source' => 'migration',
135 'error' => $e->getMessage(),
136 ]);
137 $this->updateProgress('pro_features', 'running', $migrated, $skipped, $failed, $total, null, null);
138 }
139
140 return compact('migrated', 'skipped', 'failed');
141 }
142
143 /**
144 * True when every legacy Pro 2.x feature we know about is enabled in the saved option.
145 * Uses a count so slightly different option shapes (missing keys) still match "all on" when appropriate.
146 */
147 private function allLegacyProFeaturesWereEnabled(array $legacy): bool
148 {
149 $required = count(self::LEGACY_PRO_FEATURE_KEYS);
150 $enabledCount = 0;
151 foreach (self::LEGACY_PRO_FEATURE_KEYS as $key) {
152 if (!empty($legacy[$key])) {
153 $enabledCount++;
154 }
155 }
156
157 return $enabledCount >= $required;
158 }
159 }
160
161