PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / 2.4.01
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses v2.4.01
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.4.01, at app/Hooks/CLI/BuddyPressMigrator.php

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