| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yatra\Migration; |
| 4 |
|
| 5 |
use Yatra\Database\Tables\ReviewsTable; |
| 6 |
use Yatra\Utils\Logger; |
| 7 |
|
| 8 |
/** |
| 9 |
* Migrate legacy Pro reviews stored as CPT `yatra-review` into the new ReviewsTable. |
| 10 |
* |
| 11 |
* Legacy Pro stored review fields as post meta: |
| 12 |
* - tour_id, rating, fullname, email, url |
| 13 |
* Review content is in post_content; title in post_title. |
| 14 |
*/ |
| 15 |
class ProReviewCptMigration extends BaseMigration |
| 16 |
{ |
| 17 |
public function run(): array |
| 18 |
{ |
| 19 |
$migrated = 0; |
| 20 |
$skipped = 0; |
| 21 |
$failed = 0; |
| 22 |
|
| 23 |
$reviewsTable = ReviewsTable::getTableName(); |
| 24 |
|
| 25 |
$old = $this->wpdb->get_results( |
| 26 |
"SELECT ID, post_title, post_content, post_date, post_status |
| 27 |
FROM {$this->wpdb->posts} |
| 28 |
WHERE post_type = 'yatra-review' |
| 29 |
AND post_status NOT IN ('trash','auto-draft')" |
| 30 |
); |
| 31 |
|
| 32 |
$total = is_array($old) ? count($old) : 0; |
| 33 |
if ($total === 0) { |
| 34 |
return compact('migrated', 'skipped', 'failed'); |
| 35 |
} |
| 36 |
|
| 37 |
foreach ($old as $post) { |
| 38 |
try { |
| 39 |
$meta = $this->getPostMeta((int) $post->ID); |
| 40 |
|
| 41 |
$tourId = (int) ($meta['tour_id'] ?? 0); |
| 42 |
if ($tourId <= 0) { |
| 43 |
$skipped++; |
| 44 |
$this->updateProgress('pro_reviews_cpt', 'running', $migrated, $skipped, $failed, $total, null, null); |
| 45 |
continue; |
| 46 |
} |
| 47 |
|
| 48 |
$newTripId = $this->getMigratedTripId($tourId); |
| 49 |
if (!$newTripId) { |
| 50 |
$skipped++; |
| 51 |
$this->updateProgress('pro_reviews_cpt', 'running', $migrated, $skipped, $failed, $total, null, null); |
| 52 |
continue; |
| 53 |
} |
| 54 |
|
| 55 |
$rating = isset($meta['rating']) ? (int) $meta['rating'] : 5; |
| 56 |
$rating = min(5, max(1, $rating ?: 5)); |
| 57 |
|
| 58 |
$authorName = (string) ($meta['fullname'] ?? 'Anonymous'); |
| 59 |
$authorEmail = isset($meta['email']) ? (string) $meta['email'] : null; |
| 60 |
$authorLocation = null; |
| 61 |
|
| 62 |
$status = 'pending'; |
| 63 |
if ((string) $post->post_status === 'publish') { |
| 64 |
$status = 'approved'; |
| 65 |
} |
| 66 |
|
| 67 |
// De-dupe (best-effort) unless forced: match trip_id + author_email + content. |
| 68 |
if (!$this->isForceMigration() && $authorEmail) { |
| 69 |
$exists = $this->wpdb->get_var($this->wpdb->prepare( |
| 70 |
"SELECT id FROM {$reviewsTable} |
| 71 |
WHERE trip_id = %d AND author_email = %s AND content = %s LIMIT 1", |
| 72 |
(int) $newTripId, |
| 73 |
$authorEmail, |
| 74 |
(string) $post->post_content |
| 75 |
)); |
| 76 |
if ($exists) { |
| 77 |
$skipped++; |
| 78 |
$this->updateProgress('pro_reviews_cpt', 'running', $migrated, $skipped, $failed, $total, null, null); |
| 79 |
continue; |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
$inserted = $this->wpdb->insert( |
| 84 |
$reviewsTable, |
| 85 |
[ |
| 86 |
'trip_id' => (int) $newTripId, |
| 87 |
'user_id' => 0, |
| 88 |
'rating' => $rating, |
| 89 |
'title' => $post->post_title !== '' ? (string) $post->post_title : null, |
| 90 |
'content' => (string) $post->post_content, |
| 91 |
'author_name' => $authorName !== '' ? $authorName : 'Anonymous', |
| 92 |
'author_email' => $authorEmail !== '' ? $authorEmail : null, |
| 93 |
'author_location' => $authorLocation, |
| 94 |
'status' => $status, |
| 95 |
'helpful_count' => 0, |
| 96 |
'created_at' => (string) $post->post_date, |
| 97 |
'updated_at' => (string) $post->post_date, |
| 98 |
'created_by' => null, |
| 99 |
'updated_by' => null, |
| 100 |
] |
| 101 |
); |
| 102 |
|
| 103 |
if ($inserted) { |
| 104 |
$migrated++; |
| 105 |
} else { |
| 106 |
$failed++; |
| 107 |
Logger::error('ProReviewCptMigration: insert failed', [ |
| 108 |
'source' => 'migration', |
| 109 |
'post_id' => (int) $post->ID, |
| 110 |
'error' => $this->wpdb->last_error, |
| 111 |
]); |
| 112 |
} |
| 113 |
|
| 114 |
$this->updateProgress('pro_reviews_cpt', 'running', $migrated, $skipped, $failed, $total, null, null); |
| 115 |
} catch (\Throwable $e) { |
| 116 |
$failed++; |
| 117 |
Logger::error('ProReviewCptMigration exception', [ |
| 118 |
'source' => 'migration', |
| 119 |
'post_id' => (int) $post->ID, |
| 120 |
'error' => $e->getMessage(), |
| 121 |
]); |
| 122 |
$this->updateProgress('pro_reviews_cpt', 'running', $migrated, $skipped, $failed, $total, null, null); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
return compact('migrated', 'skipped', 'failed'); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
|