-
Notifications
You must be signed in to change notification settings - Fork 10
/
civicrm-wp-member-sync.php
executable file
·352 lines (289 loc) · 8.53 KB
/
civicrm-wp-member-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
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
<?php
/**
* CiviCRM Member Sync
*
* Plugin Name: CiviCRM Member Sync
* Description: Synchronize CiviCRM Memberships with WordPress User Roles or Capabilities.
* Plugin URI: https://github.com/christianwach/civicrm-wp-member-sync
* GitHub Plugin URI: https://github.com/christianwach/civicrm-wp-member-sync
* Version: 0.6.4a
* Author: Christian Wach
* Author URI: https://haystack.co.uk
* Requires at least: 4.9
* Requires PHP: 7.4
* Text Domain: civicrm-wp-member-sync
* Domain Path: /languages
*
* Thanks to:
*
* Jag Kandasamy <https://github.com/jeevajoy> for:
* "Wordpress CiviMember Role Sync Plugin" <https://github.com/jeevajoy/Wordpress-CiviCRM-Member-Role-Sync>
*
* Tadpole Collective <https://tadpole.cc> for their fork:
* "Tadpole CiviMember Role Synchronize" <https://github.com/tadpolecc/civi_member_sync>
*
* @package Civi_WP_Member_Sync
*/
// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;
// Define Capability prefix.
if ( ! defined( 'CIVI_WP_MEMBER_SYNC_CAP_PREFIX' ) ) {
define( 'CIVI_WP_MEMBER_SYNC_CAP_PREFIX', 'civimember_' );
}
// Define plugin version - bumping this will also refresh CSS and JS.
define( 'CIVI_WP_MEMBER_SYNC_VERSION', '0.6.4a' );
// Store reference to this file.
define( 'CIVI_WP_MEMBER_SYNC_PLUGIN_FILE', __FILE__ );
// Store URL to this plugin's directory.
if ( ! defined( 'CIVI_WP_MEMBER_SYNC_PLUGIN_URL' ) ) {
define( 'CIVI_WP_MEMBER_SYNC_PLUGIN_URL', plugin_dir_url( CIVI_WP_MEMBER_SYNC_PLUGIN_FILE ) );
}
// Store PATH to this plugin's directory.
if ( ! defined( 'CIVI_WP_MEMBER_SYNC_PLUGIN_PATH' ) ) {
define( 'CIVI_WP_MEMBER_SYNC_PLUGIN_PATH', plugin_dir_path( CIVI_WP_MEMBER_SYNC_PLUGIN_FILE ) );
}
/**
* Plugin class.
*
* A class for encapsulating plugin functionality.
*
* @since 0.1
*/
class Civi_WP_Member_Sync {
/**
* WordPress Users utilities object.
*
* @since 0.1
* @access public
* @var Civi_WP_Member_Sync_Users
*/
public $users;
/**
* WordPress Scheduled Events utilities object.
*
* @since 0.1
* @access public
* @var Civi_WP_Member_Sync_Schedule
*/
public $schedule;
/**
* Admin object.
*
* @since 0.1
* @access public
* @var Civi_WP_Member_Sync_Admin
*/
public $admin;
/**
* CiviCRM Membership object.
*
* @since 0.1
* @access public
* @var Civi_WP_Member_Sync_Members
*/
public $members;
/**
* "Groups" compatibility object.
*
* @since 0.3.9
* @access public
* @var Civi_WP_Member_Sync_Groups
*/
public $groups;
/**
* BuddyPress compatibility object.
*
* @since 0.4.7
* @access public
* @var Civi_WP_Member_Sync_BuddyPress
*/
public $buddypress;
/**
* Initialise this object.
*
* @since 0.1
*/
public function __construct() {
// Bootstrap plugin.
$this->include_files();
$this->setup_objects();
$this->register_hooks();
}
/**
* Include files.
*
* @since 0.3.7
*/
public function include_files() {
// Load our class files.
require CIVI_WP_MEMBER_SYNC_PLUGIN_PATH . 'includes/civi-wp-ms-users.php';
require CIVI_WP_MEMBER_SYNC_PLUGIN_PATH . 'includes/civi-wp-ms-schedule.php';
require CIVI_WP_MEMBER_SYNC_PLUGIN_PATH . 'includes/civi-wp-ms-admin.php';
require CIVI_WP_MEMBER_SYNC_PLUGIN_PATH . 'includes/civi-wp-ms-members.php';
require CIVI_WP_MEMBER_SYNC_PLUGIN_PATH . 'includes/civi-wp-ms-groups.php';
require CIVI_WP_MEMBER_SYNC_PLUGIN_PATH . 'includes/civi-wp-ms-buddypress.php';
}
/**
* Set up this plugin's objects.
*
* @since 0.3.7
*/
public function setup_objects() {
// Instantiate our objects.
$this->users = new Civi_WP_Member_Sync_Users( $this );
$this->schedule = new Civi_WP_Member_Sync_Schedule( $this );
$this->admin = new Civi_WP_Member_Sync_Admin( $this );
$this->members = new Civi_WP_Member_Sync_Members( $this );
$this->groups = new Civi_WP_Member_Sync_Groups( $this );
$this->buddypress = new Civi_WP_Member_Sync_BuddyPress( $this );
}
/**
* Register hooks.
*
* @since 0.6.2
*/
public function register_hooks() {
// Initialise plugin when CiviCRM initialises during "plugins_loaded".
add_action( 'civicrm_instance_loaded', [ $this, 'initialise' ] );
// Use translation.
add_action( 'plugins_loaded', [ $this, 'translation' ] );
// Add settings link.
add_filter( 'network_admin_plugin_action_links', [ $this, 'plugin_action_links' ], 10, 2 );
add_filter( 'plugin_action_links', [ $this, 'plugin_action_links' ], 10, 2 );
}
/**
* Initialise objects when CiviCRM initialises.
*
* @since 0.1
*/
public function initialise() {
/**
* Bootstraps this plugin.
*
* This action is used internally in order to trigger initialisation.
* There is a specific order to the callbacks:
*
* * Civi_WP_Member_Sync_Admin - Priority 1
* * Civi_WP_Member_Sync_Users - Priority 3
* * Civi_WP_Member_Sync_Schedule - Priority 5
* * Civi_WP_Member_Sync_Members - Priority 7
* * Civi_WP_Member_Sync_Groups - Priority 10
* * Civi_WP_Member_Sync_BuddyPress - Priority 20
*
* @since 0.1
* @since 0.3.9 All CWMS classes hook into this to trigger initialisation.
*/
do_action( 'civi_wp_member_sync_initialised' );
}
// -----------------------------------------------------------------------------------
/**
* Perform plugin activation tasks.
*
* @since 0.1
*/
public function activate() {
// Setup plugin admin.
$this->admin->activate();
}
/**
* Perform plugin deactivation tasks.
*
* @since 0.1
*/
public function deactivate() {
// Remove scheduled hook.
$this->schedule->unschedule();
}
// -----------------------------------------------------------------------------------
/**
* Load translations.
*
* @since 0.1
*/
public function translation() {
// Load translations.
// phpcs:ignore WordPress.WP.DeprecatedParameters.Load_plugin_textdomainParam2Found
load_plugin_textdomain(
'civicrm-wp-member-sync', // Unique name.
false, // Deprecated argument.
dirname( plugin_basename( CIVI_WP_MEMBER_SYNC_PLUGIN_FILE ) ) . '/languages/' // Relative path to files.
);
}
/**
* Utility to add link to settings page.
*
* @since 0.6.2
*
* @param array $links The existing links array.
* @param str $file The name of the plugin file.
* @return array $links The modified links array.
*/
public function plugin_action_links( $links, $file ) {
// Maybe add settings link.
if ( plugin_basename( dirname( __FILE__ ) . '/civicrm-wp-member-sync.php' ) !== $file ) {
return $links;
}
// Is this Network Admin? Also check sub-site listings (since WordPress 4.4) and show for network admins.
if ( is_network_admin() || ( is_super_admin() && civicrm_wpms()->admin->is_network_activated() ) ) {
$link = add_query_arg( [ 'page' => 'civi_wp_member_sync_parent' ], network_admin_url( 'settings.php' ) );
} else {
$link = add_query_arg( [ 'page' => 'civi_wp_member_sync_parent' ], admin_url( 'admin.php' ) );
}
// Add settings link.
$links[] = '<a href="' . esc_url( $link ) . '">' . esc_html__( 'Settings', 'civicrm-wp-member-sync' ) . '</a>';
// Add Paypal link.
$paypal = 'https://www.paypal.me/interactivist';
$links[] = '<a href="' . esc_url( $paypal ) . '" target="_blank">' . esc_html__( 'Donate!', 'civicrm-wp-member-sync' ) . '</a>';
// --<
return $links;
}
/**
* Write to the error log.
*
* @since 0.6.2
*
* @param array $data The data to write to the log file.
*/
public function log_error( $data = [] ) {
// Skip if not debugging.
if ( ! defined( 'WP_DEBUG' ) || ! WP_DEBUG ) {
return;
}
// Skip if empty.
if ( empty( $data ) ) {
return;
}
// Format data.
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_print_r
$error = print_r( $data, true );
// Write to log file.
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
error_log( $error );
}
}
/**
* Utility for retrieving a reference to this plugin.
*
* @since 0.2.7
*
* @return object $civi_wp_member_sync The plugin reference.
*/
function civicrm_wpms() {
// Maybe bootstrap plugin.
global $civi_wp_member_sync;
if ( ! isset( $civi_wp_member_sync ) ) {
$civi_wp_member_sync = new Civi_WP_Member_Sync();
}
// --<
return $civi_wp_member_sync;
}
// Init plugin.
civicrm_wpms();
// Plugin activation.
register_activation_hook( __FILE__, [ civicrm_wpms(), 'activate' ] );
// Plugin deactivation.
register_deactivation_hook( __FILE__, [ civicrm_wpms(), 'deactivate' ] );
/*
* Uninstall uses the 'uninstall.php' method.
* @see https://developer.wordpress.org/reference/functions/register_uninstall_hook/
*/