Sync

Class for all syncing related functions

Description

Source

File: bp-integrations/learndash/buddypress/Sync.php

21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
class Sync
{
    // temporarily hold the synced learndash group id just before delete
    protected $deletingSyncedLdGroupId;
 
    /**
     * Constructor
     *
     * @since BuddyBoss 1.0.0
     */
    public function __construct()
    {
        add_action('bp_ld_sync/init', [$this, 'init']);
    }
 
    /**
     * Add actions once integration is ready
     *
     * @since BuddyBoss 1.0.0
     */
    public function init()
    {
        add_action('bp_ld_sync/buddypress_group_created', [$this, 'onGroupCreate']);
        add_action('bp_ld_sync/buddypress_group_updated', [$this, 'onGroupUpdate']);
        add_action('bp_ld_sync/buddypress_group_deleting', [$this, 'onGroupDeleting']);
        add_action('bp_ld_sync/buddypress_group_deleted', [$this, 'onGroupDeleted']);
 
        add_action('bp_ld_sync/buddypress_group_admin_added', [$this, 'onAdminAdded'], 10, 3);
        add_action('bp_ld_sync/buddypress_group_mod_added', [$this, 'onModAdded'], 10, 3);
        add_action('bp_ld_sync/buddypress_group_member_added', [$this, 'onMemberAdded'], 10, 3);
 
        add_action('bp_ld_sync/buddypress_group_admin_removed', [$this, 'onAdminRemoved'], 10, 3);
        add_action('bp_ld_sync/buddypress_group_mod_removed', [$this, 'onModRemoved'], 10, 3);
        add_action('bp_ld_sync/buddypress_group_member_removed', [$this, 'onMemberRemoved'], 10, 3);
        // add_action('bp_ld_sync/buddypress_group_member_banned', [$this, 'onMemberRemoved'], 10, 3);
    }
 
    /**
     * Get Sync generator object
     *
     * @since BuddyBoss 1.0.0
     */
    public function generator($bpGroupId = null, $ldGroupId = null)
    {
        return new SyncGenerator($bpGroupId, $ldGroupId);
    }
 
    /**
     * Run the sync when new group is created
     *
     * @since BuddyBoss 1.0.0
     */
    public function onGroupCreate($groupId)
    {
        if (! $this->preCheck()) {
            return;
        }
 
        $settings = bp_ld_sync('settings');
 
        // on the group creation first step, and create tab is enabled, we create sync group in later step
        if ('group-details' == bp_get_groups_current_create_step() && $settings->get('buddypress.show_in_bp_create')) {
            return false;
        }
 
        // if auto sync is turn off
        if (! $settings->get('buddypress.default_auto_sync')) {
            return false;
        }
 
        // admin is added BEFORE this hook is called, so we need to manually sync admin
        // src/bp-groups/bp-groups-functions.php:194
        $this->generator($groupId)->associateToLearndash()->syncBpAdmins();
    }
 
    /**
     * Run the sync when new group is updated
     *
     * @since BuddyBoss 1.0.0
     */
    public function onGroupUpdate($groupId)
    {
        if (! $this->preCheck()) {
            return;
        }
 
        $this->generator($groupId)->fullSyncToLearndash();
    }
 
    /**
     * Set the deleted gropu in temporarly variable for later use
     *
     * @since BuddyBoss 1.0.0
     */
    public function onGroupDeleting($groupId)
    {
        if (! $this->preCheck()) {
            return;
        }
 
        $this->deletingSyncedLdGroupId = $this->generator($groupId)->getLdGroupId();
    }
 
    /**
     * Desync when group is deleted
     *
     * @since BuddyBoss 1.0.0
     */
    public function onGroupDeleted($groupId)
    {
        if (! $this->enabled()) {
            return false;
        }
 
        if (! $ldGroupId = $this->deletingSyncedLdGroupId) {
            return;
        }
 
        $this->deletingSyncedLdGroupId = null;
 
        if (! bp_ld_sync('settings')->get('buddypress.delete_ld_on_delete')) {
            $this->generator(null, $ldGroupId)->desyncFromBuddypress();
            return;
        }
 
        $this->generator()->deleteLdGroup($ldGroupId);
    }
 
    /**
     * Sync when a admin is added to the group
     *
     * @since BuddyBoss 1.0.0
     */
    public function onAdminAdded($groupId, $memberId, $groupMemberObject)
    {
        if (! $generator = $this->groupUserEditCheck('admin', $groupId)) {
            return false;
        }
 
        $generator->syncBpAdmin($memberId);
    }
 
    /**
     * Sync when a mod is added to the group
     *
     * @since BuddyBoss 1.0.0
     */
    public function onModAdded($groupId, $memberId, $groupMemberObject)
    {
        if (! $generator = $this->groupUserEditCheck('mod', $groupId)) {
            return false;
        }
 
        $generator->syncBpMod($memberId);
    }
 
    /**
     * Sync when a member is added to the group
     *
     * @since BuddyBoss 1.0.0
     */
    public function onMemberAdded($groupId, $memberId, $groupMemberObject)
    {
        if (! $generator = $this->groupUserEditCheck('user', $groupId)) {
            return false;
        }
 
        $generator->syncBpMember($memberId);
    }
 
    /**
     * Sync when a admin is removed from the group
     *
     * @since BuddyBoss 1.0.0
     */
    public function onAdminRemoved($groupId, $memberId, $groupMemberObject)
    {
        if (! $generator = $this->groupUserEditCheck('admin', $groupId)) {
            return false;
        }
 
        $generator->syncBpAdmin($memberId, true);
    }
 
    /**
     * Sync when a mod is removed from the group
     *
     * @since BuddyBoss 1.0.0
     */
    public function onModRemoved($groupId, $memberId, $groupMemberObject)
    {
        if (! $generator = $this->groupUserEditCheck('mod', $groupId)) {
            return false;
        }
 
        $generator->syncBpMod($memberId, true);
    }
 
    /**
     * Sync when a members is removed from the group
     *
     * @since BuddyBoss 1.0.0
     */
    //public function onMemberRemoved($groupId, $memberId, $groupMemberObject)
    // {
    //  if (! $generator = $this->groupUserEditCheck('user', $groupId)) {
    //      return false;
    //  }
 
    //  $generator->syncBpMember($memberId, true);
    // }
 
    /**
     * Check if the user type need to be synced
     *
     * @since BuddyBoss 1.0.0
     */
    protected function groupUserEditCheck($role, $groupId)
    {
        if (! $this->preCheck()) {
            return;
        }
 
        $settings = bp_ld_sync('settings');
 
        if (! $settings->get('buddypress.enabled')) {
            return false;
        }
 
        if ('none' == $settings->get("buddypress.default_{$role}_sync_to")) {
            return false;
        }
 
        $generator = $this->generator($groupId);
 
        if (! $generator->hasLdGroup()) {
            return false;
        }
 
        return $generator;
    }
 
    /**
     * Standard pre check bore all sync happens
     *
     * @since BuddyBoss 1.0.0
     */
    protected function preCheck()
    {
        global $bp_ld_sync__syncing_to_buddypress;
 
        // if it's group is created from buddypress sync, don't need to sync back
        if ($bp_ld_sync__syncing_to_buddypress) {
            return false;
        }
 
        return $this->enabled();
    }
 
    /**
     * Returns if buddypress sync is enabled or not
     *
     * @since BuddyBoss 1.0.0
     */
    protected function enabled()
    {
        return bp_ld_sync('settings')->get('buddypress.enabled');
    }
}

Changelog

Changelog
Version Description
BuddyBoss 1.0.0 Introduced.

Methods

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.