SyncGenerator
Class for controlling gorup syncing
Description
Source
File: bp-integrations/learndash/library/SyncGenerator.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 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 | class SyncGenerator { protected $syncingToLearndash = false; protected $syncingToBuddypress = false; protected $bpGroupId ; protected $ldGroupId ; protected $syncMetaKey = '_sync_group_id' ; /** * Constructor * * @since BuddyBoss 1.0.0 */ public function __construct( $bpGroupId = null, $ldGroupId = null) { $this ->bpGroupId = $bpGroupId ; $this ->ldGroupId = $ldGroupId ; $this ->populateData(); $this ->verifyInputs(); } /** * Check if there's a ld group * * @since BuddyBoss 1.0.0 */ public function hasLdGroup() { return !! $this ->ldGroupId; } /** * Check if there's a bp group * * @since BuddyBoss 1.0.0 */ public function hasBpGroup() { return !! $this ->bpGroupId; } /** * Get the ld group id * * @since BuddyBoss 1.0.0 */ public function getLdGroupId() { return $this ->ldGroupId; } /** * Get the bp group id * * @since BuddyBoss 1.0.0 */ public function getBpGroupId() { return $this ->bpGroupId; } /** * Associate current bp group to a ld group * * @since BuddyBoss 1.0.0 */ public function associateToLearndash( $ldGroupId = null) { if ( $this ->ldGroupId && ! $ldGroupId ) { return $this ; } $this ->syncingToLearndash( function () use ( $ldGroupId ) { $ldGroup = get_post( $ldGroupId ); if (! $ldGroupId || ! $ldGroup ) { $this ->createLearndashGroup(); } else { $this ->unsetBpGroupMeta(false)->unsetLdGroupMeta(false); $this ->ldGroupId = $ldGroupId ; } $this ->setSyncGropuIds(); }); return $this ; } /** * Un-associate the current bp group from ld group * * @since BuddyBoss 1.0.0 */ public function desyncFromLearndash() { if (! $this ->ldGroupId) { return $this ; } $this ->unsetSyncGropuIds(); return $this ; } /** * delete the bp group without trigging sync * * @since BuddyBoss 1.0.0 */ public function deleteBpGroup( $bpGroupId ) { $this ->syncingToBuddypress( function () use ( $bpGroupId ) { groups_delete_group( $bpGroupId ); }); } /** * delete the ld group without trigging sync * * @since BuddyBoss 1.0.0 */ public function deleteLdGroup( $ldGroupId ) { $this ->syncingToLearndash( function () use ( $ldGroupId ) { wp_delete_post( $ldGroupId , true); }); } /** * Associate current ld group to bp group * * @since BuddyBoss 1.0.0 */ public function associateToBuddypress( $bpGroupId = null) { if ( $this ->bpGroupId && ! $bpGroupId ) { return $this ; } $this ->syncingToBuddypress( function () use ( $bpGroupId ) { $bpGroup = groups_get_group( $bpGroupId ); if (! $bpGroupId || ! $bpGroup ->id) { $this ->createBuddypressGroup(); } else { $this ->unsetBpGroupMeta(false)->unsetLdGroupMeta(false); $this ->bpGroupId = $bpGroupId ; } $this ->setSyncGropuIds(); }); return $this ; } /** * Un associate current ld group from bp group * * @since BuddyBoss 1.0.0 */ public function desyncFromBuddypress() { if (! $this ->bpGroupId) { return $this ; } $this ->unsetSyncGropuIds(); return $this ; } /** * Run a full users sync up bp group to ld group * * @since BuddyBoss 1.0.0 */ public function fullSyncToLearndash() { $lastSynced = groups_get_groupmeta( $this ->bpGroupId, '_last_sync' , true) ?: 0; if ( $lastSynced > $this ->getLastSyncTimestamp( 'bp' )) { return ; } $this ->syncBpUsers()->syncBpMods()->syncBpAdmins(); groups_update_groupmeta( $this ->bpGroupId, '_last_sync' , time()); } /** * Run a full users sync up ld group to bp group * * @since BuddyBoss 1.0.0 */ public function fullSyncToBuddypress() { $lastSynced = groups_get_groupmeta( $this ->ldGroupId, '_last_sync' , true) ?: 0; if ( $lastSynced > $this ->getLastSyncTimestamp( 'ld' )) { return ; } $this ->syncLdAdmins()->syncLdUsers(); update_post_meta( $this ->ldGroupId, '_last_sync' , time()); } /** * Sync the bp admins to ld * * @since BuddyBoss 1.0.0 */ public function syncBpAdmins() { $this ->syncingToLearndash( function () { $adminIds = groups_get_group_admins( $this ->bpGroupId); foreach ( $adminIds as $admin ) { $this ->syncBpAdmin( $admin ->user_id, false, false); } }); $this ->clearLdGroupCache(); return $this ; } /** * Sync the bp mods to ld * * @since BuddyBoss 1.0.0 */ public function syncBpMods() { $this ->syncingToLearndash( function () { $modIds = groups_get_group_mods( $this ->bpGroupId); foreach ( $modIds as $mod ) { $this ->syncBpMod( $mod ->user_id, false, false); } }); $this ->clearLdGroupCache(); return $this ; } /** * Sync the bp members to ld * * @since BuddyBoss 1.0.0 */ public function syncBpUsers() { $this ->syncingToLearndash( function () { $members = groups_get_group_members([ 'group_id' => $this ->bpGroupId ]); $members = $members [ 'members' ]; foreach ( $members as $member ) { $this ->syncBpMember( $member ->ID, false, false); } }); $this ->clearLdGroupCache(); return $this ; } /** * Sync the ld admins to bp * * @since BuddyBoss 1.0.0 */ public function syncLdAdmins() { $this ->syncingToBuddypress( function () { $adminIds = learndash_get_groups_administrator_ids( $this ->ldGroupId); foreach ( $adminIds as $adminId ) { $this ->syncLdAdmin( $adminId ); } }); return $this ; } /** * Sync the ld students to bp * * @since BuddyBoss 1.0.0 */ public function syncLdUsers() { $this ->syncingToBuddypress( function () { $userIds = learndash_get_groups_user_ids( $this ->ldGroupId); foreach ( $userIds as $userId ) { $this ->syncLdUser( $userId ); } }); return $this ; } /** * Sync a bp admin to ld * * @since BuddyBoss 1.0.0 */ public function syncBpAdmin( $userId , $remove = false, $clearCache = true) { $this ->syncingToLearndash( function () use ( $userId , $remove ) { call_user_func_array( $this ->getBpSyncFunction( 'admin' ), [ $userId , $this ->ldGroupId, $remove ]); $this ->maybeRemoveAsLdUser( 'admin' , $userId ); }); if ( $clearCache ) { $this ->clearLdGroupCache(); } return $this ; } /** * Sync a bp mod to ld * * @since BuddyBoss 1.0.0 */ public function syncBpMod( $userId , $remove = false, $clearCache = true) { $this ->syncingToLearndash( function () use ( $userId , $remove ) { call_user_func_array( $this ->getBpSyncFunction( 'mod' ), [ $userId , $this ->ldGroupId, $remove ]); $this ->maybeRemoveAsLdUser( 'mod' , $userId ); }); if ( $clearCache ) { $this ->clearLdGroupCache(); } return $this ; } /** * Sync a bp member to ld * * @since BuddyBoss 1.0.0 */ public function syncBpMember( $userId , $remove = false, $clearCache = true) { $this ->syncingToLearndash( function () use ( $userId , $remove ) { call_user_func_array( $this ->getBpSyncFunction( 'user' ), [ $userId , $this ->ldGroupId, $remove ]); // if sync to user, we need to remove previous admin if ( 'user' == $this ->getBpSyncToRole( 'user' )) { call_user_func_array( 'ld_update_leader_group_access' , [ $userId , $this ->ldGroupId, true]); } }); if ( $clearCache ) { $this ->clearLdGroupCache(); } return $this ; } /** * Sync a ld admin to bp * * @since BuddyBoss 1.0.0 */ public function syncLdAdmin( $userId , $remove = false) { $this ->syncingToBuddypress( function () use ( $userId , $remove ) { $this ->addUserToBpGroup( $userId , 'admin' , $remove ); }); return $this ; } /** * Sync a ld student to bp * * @since BuddyBoss 1.0.0 */ public function syncLdUser( $userId , $remove = false) { $ldGroupAdmins = learndash_get_groups_administrator_ids( $this ->ldGroupid); // if this user is learndash leader, we don't want to downgrad them (bp only allow 1 user) if (in_array( $userId , $ldGroupAdmins )) { return $this ; } $this ->syncingToBuddypress( function () use ( $userId , $remove ) { $this ->addUserToBpGroup( $userId , 'user' , $remove ); }); return $this ; } /** * Verify the givent group ids still exists in db * * @since BuddyBoss 1.0.0 */ protected function verifyInputs() { if ( $this ->bpGroupId && ! groups_get_group( $this ->bpGroupId)->id) { $this ->unsetBpGroupMeta(); } if ( $this ->ldGroupId && ! get_post( $this ->ldGroupId)) { $this ->unsetLdGroupMeta(); } } /** * Populate the class data based on given input * * @since BuddyBoss 1.0.0 */ protected function populateData() { if (! $this ->bpGroupId) { $this ->bpGroupId = $this ->loadBpGroupId(); } if (! $this ->ldGroupId) { $this ->ldGroupId = $this ->loadLdGroupId(); } } /** * Find the bp group id on current ld group * * @since BuddyBoss 1.0.0 */ protected function loadBpGroupId() { return get_post_meta( $this ->ldGroupId, $this ->syncMetaKey, true) ?: null; } /** * Find the ld group id on current bp group * * @since BuddyBoss 1.0.0 */ protected function loadLdGroupId() { return groups_get_groupmeta( $this ->bpGroupId, $this ->syncMetaKey, true) ?: null; } /** * Sasve bp group id to current ld group * * @since BuddyBoss 1.0.0 */ protected function setBpGroupId() { update_post_meta( $this ->ldGroupId, $this ->syncMetaKey, $this ->bpGroupId); return $this ; } /** * Sasve ld group id to current bp group * * @since BuddyBoss 1.0.0 */ protected function setLdGroupId() { groups_update_groupmeta( $this ->bpGroupId, $this ->syncMetaKey, $this ->ldGroupId); return $this ; } /** * Force id sync * * @since BuddyBoss 1.0.0 */ protected function setSyncGropuIds() { return $this ->setLdGroupId()->setBpGroupId(); } /** * Remove bp group id from current ld group * * @since BuddyBoss 1.0.0 */ protected function unsetBpGroupMeta( $removeProp = true) { if ( $removeProp ) { $this ->bpGroupId = null; } delete_post_meta( $this ->ldGroupId, $this ->syncMetaKey); return $this ; } /** * Remove ld group id from current bp group * * @since BuddyBoss 1.0.0 */ protected function unsetLdGroupMeta( $removeProp = true) { if ( $removeProp ) { $this ->ldGroupId = null; } groups_delete_groupmeta( $this ->bpGroupId, $this ->syncMetaKey); return $this ; } /** * Force unsync group ids * * @since BuddyBoss 1.0.0 */ protected function unsetSyncGropuIds() { $this ->unsetBpGroupMeta(false)->unsetLdGroupMeta(false); $this ->bpGroupId = $this ->ldGroupId = null; return $this ; } /** * Greate a ld group based on current bp group * * @since BuddyBoss 1.0.0 */ protected function createLearndashGroup() { $bpGroup = groups_get_group( $this ->bpGroupId); $this ->ldGroupId = wp_insert_post([ 'post_title' => $bpGroup ->name, 'post_author' => $bpGroup ->creator_id, 'post_content' => $bpGroup ->description, 'post_status' => 'publish' , 'post_type' => learndash_get_post_type_slug( 'group' ) ]); } /** * Create bp group based on current ld group * * @since BuddyBoss 1.0.0 */ protected function createBuddypressGroup() { $ldGroup = get_post( $this ->ldGroupId); $settings = bp_ld_sync( 'settings' ); $this ->bpGroupId = groups_create_group([ 'name' => $ldGroup ->post_title ?: "For Social Group: {$this->ldGroupId}" , 'status' => $settings ->get( 'learndash.default_bp_privacy' ), ]); groups_update_groupmeta( $this ->bpGroupId, 'invite_status' , $settings ->get( 'learndash.default_bp_invite_status' )); $this ->setSyncGropuIds(); } /** * Maybe remove ld user if user is promote or demote from bp * * @since BuddyBoss 1.0.0 */ protected function maybeRemoveAsLdUser( $type , $userId ) { if ( 'user' == $this ->getBpSyncToRole( $type )) { return ; } // remove them as user, cause they are leader now ld_update_group_access( $userId , $this ->ldGroupId, true); } /** * Get the bp role to sync to * * @since BuddyBoss 1.0.0 */ protected function getBpSyncToRole( $type ) { return bp_ld_sync( 'settings' )->get( "buddypress.default_{$type}_sync_to" ); } /** * Get the function that update ld group role * * @since BuddyBoss 1.0.0 */ protected function getBpSyncFunction( $type ) { switch ( $this ->getBpSyncToRole( $type )) { case 'admin' : return 'ld_update_leader_group_access' ; default : return 'ld_update_group_access' ; } } /** * Get the ld role to sync to * * @since BuddyBoss 1.0.0 */ protected function getLdSyncToRole( $type ) { return bp_ld_sync( 'settings' )->get( "learndash.default_{$type}_sync_to" ); } /** * Add a user to bp group by role * * @since BuddyBoss 1.0.0 */ protected function addUserToBpGroup( $userId , $type , $remove ) { $groupMember = new BP_Groups_Member( $userId , $this ->bpGroupId); $syncTo = $this ->getLdSyncToRole( $type ); if ( $remove ) { return $groupMember ->remove(); } $groupMember ->group_id = $this ->bpGroupId; $groupMember ->user_id = $userId ; $groupMember ->is_admin = 0; $groupMember ->is_mod = 0; $groupMember ->is_confirmed = 1; if ( 'user' != $syncTo ) { $var = "is_{$syncTo}" ; $groupMember -> $var = 1; } $groupMember ->save(); } /** * Clear the ld cache after sync * * @since BuddyBoss 1.0.0 */ protected function clearLdGroupCache() { delete_transient( "learndash_group_leaders_{$this->ldGroupId}" ); delete_transient( "learndash_group_users_{$this->ldGroupId}" ); } /** * Wrapper to prevent infinite 2 way sync when syncing to learndash * * @since BuddyBoss 1.0.0 */ protected function syncingToLearndash( $callback ) { global $bp_ld_sync__syncing_to_learndash ; $bp_ld_sync__syncing_to_learndash = true; $callback (); $bp_ld_sync__syncing_to_learndash = false; return $this ; } /** * Wrapper to prevent infinite 2 way sync when syncing to buddypress * * @since BuddyBoss 1.0.0 */ protected function syncingToBuddypress( $callback ) { global $bp_ld_sync__syncing_to_buddypress ; $bp_ld_sync__syncing_to_buddypress = true; $callback (); $bp_ld_sync__syncing_to_buddypress = false; return $this ; } /** * Get the timestamp when the group is last synced * * @since BuddyBoss 1.0.0 */ protected function getLastSyncTimestamp( $type = 'bp' ) { if (! $lastSync = bp_get_option( "bp_ld_sync/{$type}_last_synced" )) { $lastSync = time(); bp_update_option( "bp_ld_sync/{$type}_last_synced" , $lastSync ); } return $lastSync ; } } |
Changelog
Version | Description |
---|---|
BuddyBoss 1.0.0 | Introduced. |
Methods
- __construct — Constructor
- addUserToBpGroup — Add a user to bp group by role
- associateToBuddypress — Associate current ld group to bp group
- associateToLearndash — Associate current bp group to a ld group
- clearLdGroupCache — Clear the ld cache after sync
- createBuddypressGroup — Create bp group based on current ld group
- createLearndashGroup — Greate a ld group based on current bp group
- deleteBpGroup — delete the bp group without trigging sync
- deleteLdGroup — delete the ld group without trigging sync
- desyncFromBuddypress — Un associate current ld group from bp group
- desyncFromLearndash — Un-associate the current bp group from ld group
- fullSyncToBuddypress — Run a full users sync up ld group to bp group
- fullSyncToLearndash — Run a full users sync up bp group to ld group
- getBpGroupId — Get the bp group id
- getBpSyncFunction — Get the function that update ld group role
- getBpSyncToRole — Get the bp role to sync to
- getLastSyncTimestamp — Get the timestamp when the group is last synced
- getLdGroupId — Get the ld group id
- getLdSyncToRole — Get the ld role to sync to
- hasBpGroup — Check if there's a bp group
- hasLdGroup — Check if there's a ld group
- loadBpGroupId — Find the bp group id on current ld group
- loadLdGroupId — Find the ld group id on current bp group
- maybeRemoveAsLdUser — Maybe remove ld user if user is promote or demote from bp
- populateData — Populate the class data based on given input
- setBpGroupId — Sasve bp group id to current ld group
- setLdGroupId — Sasve ld group id to current bp group
- setSyncGropuIds — Force id sync
- syncBeforeBpMember — Check before Sync a bp member to ld
- syncBpAdmin — Sync a bp admin to ld
- syncBpAdmins — Sync the bp admins to ld
- syncBpMember — Sync a bp member to ld
- syncBpMod — Sync a bp mod to ld
- syncBpMods — Sync the bp mods to ld
- syncBpUsers — Sync the bp members to ld
- syncingToBuddypress — Wrapper to prevent infinite 2 way sync when syncing to buddypress
- syncingToLearndash — Wrapper to prevent infinite 2 way sync when syncing to learndash
- syncLdAdmin — Sync a ld admin to bp
- syncLdAdmins — Sync the ld admins to bp
- syncLdUser — Sync a ld student to bp
- syncLdUsers — Sync the ld students to bp
- unsetBpGroupMeta — Remove bp group id from current ld group
- unsetLdGroupMeta — Remove ld group id from current bp group
- unsetSyncGropuIds — Force unsync group ids
- verifyInputs — Verify the givent group ids still exists in db
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.