-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcivi-wp-ms-schedule.php
executable file
·192 lines (126 loc) · 3.33 KB
/
civi-wp-ms-schedule.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
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
<?php
/**
* CiviCRM WordPress Member Sync Schedule class.
*
* Class for encapsulating WordPress scheduling functionality.
*
* @since 0.1
*
* @package Civi_WP_Member_Sync
*/
class Civi_WP_Member_Sync_Schedule {
/**
* Plugin (calling) object.
*
* @since 0.1
* @access public
* @var object $plugin The plugin object.
*/
public $plugin;
/**
* Constructor.
*
* @since 0.1
*
* @param object $plugin The plugin object.
*/
public function __construct( $plugin ) {
// Store reference to plugin.
$this->plugin = $plugin;
// Initialise early.
add_action( 'civi_wp_member_sync_initialised', array( $this, 'initialise' ), 5 );
}
/**
* Initialise this object.
*
* @since 0.1
*/
public function initialise() {
// Get our schedule sync setting.
$schedule = absint( $this->plugin->admin->setting_get( 'schedule' ) );
// Add schedule if set.
if ( $schedule === 1 ) {
// Get our interval setting.
$interval = $this->plugin->admin->setting_get( 'interval' );
// Sanity check.
if ( ! empty( $interval ) ) {
// Set schedule.
$this->schedule( $interval );
}
// Add schedule callback action.
add_action( 'civi_wp_member_sync_refresh', array( $this, 'schedule_callback' ) );
}
}
//##########################################################################
/**
* Set up our scheduled event.
*
* @since 0.1
*
* @param string $interval One of the WordPress-defined intervals.
*/
public function schedule( $interval ) {
// If not already present.
if ( ! wp_next_scheduled( 'civi_wp_member_sync_refresh' ) ) {
// Add schedule.
wp_schedule_event(
time(), // Time when event fires.
$interval, // Event interval.
'civi_wp_member_sync_refresh' // Hook to fire.
);
}
}
/**
* Clear our scheduled event.
*
* @since 0.1
*/
public function unschedule() {
// Get next scheduled event.
$timestamp = wp_next_scheduled( 'civi_wp_member_sync_refresh' );
// Unschedule it if we get one.
if ( $timestamp !== false ) {
wp_unschedule_event( $timestamp, 'civi_wp_member_sync_refresh' );
}
// It's not clear whether wp_unschedule_event() clears everything,
// so let's remove existing scheduled hook as well.
wp_clear_scheduled_hook( 'civi_wp_member_sync_refresh' );
}
/**
* Called when a scheduled event is triggered.
*
* @since 0.1
*/
public function schedule_callback() {
// Call sync all method.
$this->plugin->members->sync_all_wp_user_memberships();
}
//##########################################################################
/**
* Get schedule intervals.
*
* @since 0.1
*
* @return array $intervals Array of schedule interval arrays, keyed by interval slug.
*/
public function intervals_get() {
// Just a wrapper.
return wp_get_schedules();
}
//##########################################################################
/**
* Clear our legacy_scheduled event.
*
* @since 0.1
*/
public function legacy_unschedule() {
// Get next scheduled event.
$timestamp = wp_next_scheduled( 'civi_member_sync_refresh' );
// Unschedule it if we get one.
if ( $timestamp !== false ) {
wp_unschedule_event( $timestamp, 'civi_member_sync_refresh' );
}
// Remove existing scheduled hook as well.
wp_clear_scheduled_hook( 'civi_member_sync_refresh' );
}
} // Class ends.