PluginProbe
Yatra – Travel Booking & Tour Operator Software / trunk
Yatra – Travel Booking & Tour Operator Software vtrunk
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 2.0.10 2.0.11 All 82 releases
yatra / app / Migrations / ReviewMigration.php

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

129 lines 4.7 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\Database\Tables\ReviewsTable;
6 use Yatra\Utils\Logger;
7
8 /**
9 * Review Migration - Migrate reviews from WordPress comments on old 'tour' posts
10 * to the new ReviewsTable.
11 */
12 class ReviewMigration extends BaseMigration
13 {
14 public function __construct(MigrationProgress $service)
15 {
16 parent::__construct($service);
17 }
18
19 public function run(): array
20 {
21 $migrated = 0;
22 $skipped = 0;
23 $failed = 0;
24
25 $reviewsTable = ReviewsTable::getTableName();
26
27 // Old reviews are WordPress comments on 'tour' post type
28 $oldReviews = $this->wpdb->get_results(
29 "SELECT c.*, cm_rating.meta_value AS rating
30 FROM {$this->wpdb->comments} c
31 INNER JOIN {$this->wpdb->posts} p ON c.comment_post_ID = p.ID
32 LEFT JOIN {$this->wpdb->commentmeta} cm_rating
33 ON c.comment_ID = cm_rating.comment_id AND cm_rating.meta_key = 'rating'
34 WHERE p.post_type = 'tour'
35 AND c.comment_type IN ('', 'comment', 'review', 'yatra_review')
36 AND c.comment_approved != 'trash'"
37 );
38
39 $total = count($oldReviews);
40
41 if ($total === 0) {
42 return compact('migrated', 'skipped', 'failed');
43 }
44
45 foreach ($oldReviews as $review) {
46 try {
47 // Get the migrated trip ID
48 $newTripId = $this->getRawPostMeta((int) $review->comment_post_ID, '_migrated_to_trip_id');
49
50 if (!$newTripId) {
51 $skipped++;
52 continue;
53 }
54
55 $newTripId = (int) $newTripId;
56 $rating = !empty($review->rating) ? min(5, max(1, (int) $review->rating)) : 5;
57
58 // Check if already migrated (by matching trip_id + author_email + content)
59 if (!$this->isForceMigration()) {
60 $exists = $this->wpdb->get_var($this->wpdb->prepare(
61 "SELECT id FROM {$reviewsTable}
62 WHERE trip_id = %d AND author_email = %s AND content = %s LIMIT 1",
63 $newTripId,
64 $review->comment_author_email,
65 $review->comment_content
66 ));
67
68 if ($exists) {
69 $skipped++;
70 continue;
71 }
72 }
73
74 // Map comment_approved to review status
75 $status = 'pending';
76 if ($review->comment_approved === '1') {
77 $status = 'approved';
78 } elseif ($review->comment_approved === 'spam' || $review->comment_approved === '0') {
79 $status = 'pending';
80 }
81
82 $inserted = $this->wpdb->insert(
83 $reviewsTable,
84 [
85 'trip_id' => $newTripId,
86 'user_id' => (int) $review->user_id ?: 0,
87 'rating' => $rating,
88 'title' => null,
89 'content' => $review->comment_content,
90 'author_name' => $review->comment_author ?: 'Anonymous',
91 'author_email' => $review->comment_author_email ?: null,
92 'author_location' => null,
93 'status' => $status,
94 'helpful_count' => 0,
95 'created_at' => $review->comment_date,
96 'updated_at' => $review->comment_date,
97 'created_by' => (int) $review->user_id ?: null,
98 'updated_by' => null,
99 ]
100 );
101
102 if ($inserted) {
103 $migrated++;
104 } else {
105 $failed++;
106 Logger::error("Failed to insert review", [
107 'source' => 'migration',
108 'comment_id' => $review->comment_ID,
109 'error' => $this->wpdb->last_error
110 ]);
111 }
112
113 $this->updateProgress('reviews', 'running', $migrated, $skipped, $failed, $total, null, null);
114
115 } catch (\Exception $e) {
116 $failed++;
117 Logger::error("Review migration exception", [
118 'source' => 'migration',
119 'comment_id' => $review->comment_ID,
120 'error' => $e->getMessage()
121 ]);
122 $this->updateProgress('reviews', 'running', $migrated, $skipped, $failed, $total, null, null);
123 }
124 }
125
126 return compact('migrated', 'skipped', 'failed');
127 }
128 }
129