PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / 2.7.7
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses v2.7.7
2.11.0 2.10.0 2.10.01 2.9.1 2.9.0 2.8.1 2.8.0 2.7.7 2.7.5 2.7.0 2.6.01 2.6.0 2.5.0 2.4.01 trunk 1.0.90 1.0.91 1.0.92 1.0.93 1.0.94 1.0.95 1.0.96 1.0.97 1.0.98 1.0.99 All 78 releases
fluent-community / app / Hooks / CLI / BuddyPressMigrator.php

BuddyPressMigrator.php in FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses 2.7.7, at app/Hooks/CLI/BuddyPressMigrator.php

227 lines 6.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCommunity\App\Hooks\CLI;
4
5 use FluentCommunity\App\Models\User;
6 use FluentCommunity\App\Services\Helper;
7 use FluentCommunity\Framework\Support\Arr;
8 use FluentCommunity\Modules\Migrations\Helpers\BPMigratorHelper;
9 use FluentCommunity\Modules\Migrations\Helpers\PostMigrator;
10
11 class BuddyPressMigrator
12 {
13 public function getStats()
14 {
15 $stats = BPMigratorHelper::getBbDataStats();
16 $formattedStats = [];
17
18 foreach ($stats as $key => $stat) {
19 $formattedStats[] = [
20 'key' => $key,
21 'count' => $stat,
22 ];
23 }
24
25 return $formattedStats;
26 }
27
28 public function migrateGroups()
29 {
30 $hasGroups = bp_is_active('groups');
31
32 if (!$hasGroups) {
33 \WP_CLI::line('BuddyPress Groups component is not active. Please activate it before running the migration.');
34 return;
35 }
36
37 $groups = fluentCommunityApp('db')->table('bp_groups')->get();
38
39 $createdMaps = [];
40
41 foreach ($groups as $group) {
42 $createdSpace = BPMigratorHelper::migrateGroupData($group);
43 if ($createdSpace) {
44 $createdMaps[$group->id] = $createdSpace->id;
45 }
46 }
47
48 \WP_CLI::line(count($createdMaps) . ' BuddyPress Groups migrated successfully.');
49
50 update_option('_bp_fcom_group_maps', $createdMaps);
51
52 return $createdMaps;
53 }
54
55 public function migratePosts($lastPostId = 0)
56 {
57 if (!$lastPostId) {
58 $lastPostId = get_option('_bp_fcom_last_post_id', 0);
59 }
60
61 $isBuddyBoss = BPMigratorHelper::isBuddyBoss();
62
63 $posts = fluentCommunityApp('db')->table('bp_activity')
64 ->where('type', 'activity_update')
65 ->when($isBuddyBoss, function ($q) {
66 $q->whereNotIn('privacy', [ 'media', 'onlyme', 'document' ]);
67 })
68 ->when($lastPostId, function ($q) use ($lastPostId) {
69 $q->where('id', '>', $lastPostId);
70 })
71 ->where(function ($q) {
72 $q->where('secondary_item_id', '=', '0')
73 ->orWhereNull('secondary_item_id');
74 })
75 ->orderBy('id', 'ASC')
76 ->limit(100)
77 ->get();
78
79 $migrated = 0;
80
81 foreach ($posts as $post) {
82 ++$migrated;
83 $lastPostId = $post->id;
84
85 if (fluentCommunityApp('db')->table('bp_activity_meta')->where('activity_id', $post->id)->where('meta_key', '_fcom_feed_id')->exists()) {
86 continue;
87 }
88
89 $feed = (new PostMigrator($post))->migrate();
90
91 if (!$feed) {
92 continue;
93 }
94
95 fluentCommunityApp('db')->table('bp_activity_meta')
96 ->insert([
97 'activity_id' => $post->id,
98 'meta_key' => '_fcom_feed_id', // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
99 'meta_value' => $feed->id, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
100 ]);
101 }
102
103 update_option('_bp_fcom_last_post_id', $lastPostId);
104
105 if (!$migrated) {
106 \WP_CLI::line('No more posts to migrate.');
107 return true;
108 }
109
110 \WP_CLI::line($migrated . ' posts migrated successfully. Last Post ID: ' . $lastPostId . ' at ' . gmdate('Y-m-d H:i:s'));
111
112 return $this->migratePosts($lastPostId);
113 }
114
115 public function syncUsers()
116 {
117 $lastUserId = get_option('_bp_fcom_last_user_id', 0);
118
119 if (!$lastUserId) {
120 $lastUserId = 0;
121 }
122
123 $usersIds = fluentCommunityApp('db')->table('bp_xprofile_data')
124 ->groupBy('user_id')
125 ->select([ 'user_id' ])
126 ->when($lastUserId, function ($q) use ($lastUserId) {
127 $q->where('user_id', '>', $lastUserId);
128 })
129 ->orderBy('user_id', 'ASC')
130 ->limit(100)
131 ->get()
132 ->pluck('user_id')
133 ->toArray();
134
135 if (!count($usersIds)) {
136 \WP_CLI::line('No more users to sync.');
137 return true;
138 }
139
140 $users = User::whereIn('id', $usersIds)->get();
141
142 if ($users->isEmpty()) {
143 \WP_CLI::line('No users found to sync.');
144 return true;
145 }
146
147
148 foreach ($users as $user) {
149 $lastUserId = $user->ID;
150 BPMigratorHelper::syncUser($user);
151 }
152
153 do_action('fluent_community/after_sync_bp_users', $users);
154
155 update_option('_bp_fcom_last_user_id', $lastUserId);
156
157 \WP_CLI::line('Synced ' . count($users) . ' users successfully. Last User ID: ' . $lastUserId . ' at ' . gmdate('Y-m-d H:i:s'));
158
159 return $this->syncUsers();
160 }
161
162 public function syncGroupMembers()
163 {
164 $lastMemberId = get_option('_bp_fcom_last_migrated_member_id', 0);
165
166 $groupMemberEntries = fluentCommunityApp('db')->table('bp_groups_members')
167 ->when($lastMemberId, function ($q) use ($lastMemberId) {
168 $q->where('id', '>', $lastMemberId);
169 })
170 ->orderBy('id', 'ASC')
171 ->limit(100)
172 ->get();
173
174 $spaceMaps = get_option('_bp_fcom_group_maps', []);
175
176 $hadItem = false;
177
178 foreach ($groupMemberEntries as $entry) {
179 $hadItem = true;
180 $spaceId = Arr::get($spaceMaps, $entry->group_id);
181
182 $lastMemberId = $entry->id;
183
184 if (!$spaceId) {
185 continue;
186 }
187
188 $role = 'member';
189 if ($entry->is_admin == 1) {
190 $role = 'admin';
191 } elseif ($entry->is_mod == 1) {
192 $role = 'moderator';
193 }
194
195 $entryData = [
196 'space_id' => $spaceId,
197 'user_id' => $entry->user_id,
198 'status' => 'active',
199 'role' => $role,
200 'created_at' => $entry->date_modified,
201 ];
202
203 if (!Helper::isUserInSpace($entryData['user_id'], $entryData['space_id'])) {
204 fluentCommunityApp('db')->table('fcom_space_user')->insert($entryData);
205 }
206 }
207
208 update_option('_bp_fcom_last_migrated_member_id', $lastMemberId);
209
210 if (!$hadItem) {
211 \WP_CLI::line('No more group members to sync.');
212 return true;
213 }
214
215 \WP_CLI::line('Synced ' . count($groupMemberEntries) . ' group members successfully. Last Member ID: ' . $lastMemberId . ' at ' . gmdate('Y-m-d H:i:s'));
216
217 return $this->syncGroupMembers();
218
219 }
220
221 public function finishingUp()
222 {
223
224 }
225
226 }
227