-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuddypress-auto-join-groups-sync.php
103 lines (82 loc) · 2 KB
/
buddypress-auto-join-groups-sync.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
<?php
/**
* Plugin Name: BuddyPress Auto Join Groups Sync
* Description: This plugin basically setup a cron job for sync users groups using BuddyPress Auto Join Groups plugin.
* Author: BuddyDev
* Version: 1.0.0
* License: GPLv2 or later (license.txt)
*/
// Do not allow direct access over web.
defined( 'ABSPATH' ) || exit;
/**
* Syncs users groups
*/
class BuddyPress_Auto_Join_Groups_Sync {
/**
* Singleton.
*
* @var BuddyPress_Auto_Join_Groups_Sync
*/
private static $instance = null;
/**
* Constructor.
*/
private function __construct() {
$this->setup();
}
/**
* Boots the class.
*
* @return BuddyPress_Auto_Join_Groups_Sync
*/
public static function boot() {
if ( is_null( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Sets up hooks.
*/
private function setup() {
register_activation_hook( __FILE__, array( $this, 'activate' ) );
register_deactivation_hook( __FILE__, array( $this, 'deactivate' ) );
// hook runs daily.
add_action( 'bpajg_sync_users_groups', array( $this, 'sync' ) );
}
/**
* Schedule cron job.
*/
public function activate() {
if ( ! function_exists( 'buddypress' ) ) {
return;
}
if ( ! wp_next_scheduled( 'bpajg_sync_users_groups' ) ) {
wp_schedule_event( time(), 'daily', 'bpajg_sync_users_groups' );
}
}
/**
* Disable cron job.
*/
public function deactivate() {
wp_unschedule_event( wp_next_scheduled( 'bpajg_sync_users_groups' ), 'bpajg_sync_users_groups' );
}
/**
* Deletes user periodically.
*/
public function sync() {
if ( ! function_exists( 'buddypress' ) || ! function_exists( 'bp_auto_join_groups' ) ) {
return;
}
$user_ids = get_users( array( 'fields' => 'ID' ) );
if ( ! $user_ids ) {
return;
}
$auto_joiner = \BuddyPress_Auto_Join_Groups\Core\Groups_Auto_Joiner::boot();
foreach ( $user_ids as $user_id ) {
$auto_joiner->sync_user_groups( $user_id );
}
}
}
// init.
BuddyPress_Auto_Join_Groups_Sync::boot();