-
Notifications
You must be signed in to change notification settings - Fork 17
/
civicrm-wp-profile-sync.php
563 lines (459 loc) · 12.2 KB
/
civicrm-wp-profile-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
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
<?php
/**
* CiviCRM Profile Sync
*
* Plugin Name: CiviCRM Profile Sync
* Description: Keeps a WordPress User profile in sync with a CiviCRM Contact and integrates WordPress and CiviCRM Entities with data synced via Advanced Custom Fields.
* Plugin URI: https://github.com/christianwach/civicrm-wp-profile-sync
* GitHub Plugin URI: https://github.com/christianwach/civicrm-wp-profile-sync
* Version: 0.7.0a
* Author: Christian Wach
* Author URI: https://haystack.co.uk
* License: GPLv2 or later
* License URI: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* Requires at least: 5.7
* Requires PHP: 7.4
* Text Domain: civicrm-wp-profile-sync
* Domain Path: /languages
*
* @package CiviCRM_WP_Profile_Sync
* @link https://github.com/christianwach/civicrm-wp-profile-sync
* @license GPL v2 or later
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;
// Set plugin version here.
define( 'CIVICRM_WP_PROFILE_SYNC_VERSION', '0.7.0a' );
// Set our bulk operations flag here.
if ( ! defined( 'CIVICRM_WP_PROFILE_SYNC_BULK' ) ) {
define( 'CIVICRM_WP_PROFILE_SYNC_BULK', false );
}
// Store reference to this file.
if ( ! defined( 'CIVICRM_WP_PROFILE_SYNC_FILE' ) ) {
define( 'CIVICRM_WP_PROFILE_SYNC_FILE', __FILE__ );
}
// Store URL to this plugin's directory.
if ( ! defined( 'CIVICRM_WP_PROFILE_SYNC_URL' ) ) {
define( 'CIVICRM_WP_PROFILE_SYNC_URL', plugin_dir_url( CIVICRM_WP_PROFILE_SYNC_FILE ) );
}
// Store PATH to this plugin's directory.
if ( ! defined( 'CIVICRM_WP_PROFILE_SYNC_PATH' ) ) {
define( 'CIVICRM_WP_PROFILE_SYNC_PATH', plugin_dir_path( CIVICRM_WP_PROFILE_SYNC_FILE ) );
}
// Set BuddyPress development environment.
if ( ! defined( 'CIVICRM_WP_PROFILE_SYNC_BUDDYPRESS' ) ) {
define( 'CIVICRM_WP_PROFILE_SYNC_BUDDYPRESS', false );
}
// Set plugin debugging state.
if ( ! defined( 'CIVICRM_WP_PROFILE_SYNC_DEBUG' ) ) {
define( 'CIVICRM_WP_PROFILE_SYNC_DEBUG', false );
}
/**
* CiviCRM Profile Sync Class.
*
* A class that encapsulates this plugin's functionality.
*
* @since 0.1
*/
class CiviCRM_WP_Profile_Sync {
/**
* Admin object.
*
* @since 0.4
* @access public
* @var object
*/
public $admin;
/**
* Mapper object.
*
* @since 0.4
* @access public
* @var object
*/
public $mapper;
/**
* WordPress compatibility object.
*
* @since 0.4
* @access public
* @var object
*/
public $wp;
/**
* CiviCRM compatibility object.
*
* @since 0.4
* @access public
* @var object
*/
public $civicrm;
/**
* BuddyPress compatibility object.
*
* @since 0.4
* @access public
* @var object
*/
public $bp;
/**
* ACF compatibility object.
*
* @since 0.4
* @access public
* @var object
*/
public $acf;
/**
* CiviCRM ACF Integration compatibility object.
*
* @since 0.4
* @access public
* @var object
*/
public $cai;
/**
* Initialises this object.
*
* @since 0.1
*/
public function __construct() {
// Initialise this plugin.
$this->initialise();
}
/**
* Initialise this plugin.
*
* @since 0.5
*/
public function initialise() {
// Only do this once.
static $done;
if ( isset( $done ) && true === $done ) {
return;
}
// Include files.
$this->include_files();
// Set up objects and references.
$this->setup_objects();
/**
* Broadcast that this plugin is active.
*
* @since 0.2.4
*/
do_action( 'civicrm_wp_profile_sync_init' );
// We're done.
$done = true;
}
/**
* Include files.
*
* @since 0.4
*/
public function include_files() {
// Load our class files.
require CIVICRM_WP_PROFILE_SYNC_PATH . 'includes/admin/cwps-admin.php';
require CIVICRM_WP_PROFILE_SYNC_PATH . 'includes/wordpress/cwps-wp.php';
require CIVICRM_WP_PROFILE_SYNC_PATH . 'includes/civicrm/cwps-civicrm.php';
require CIVICRM_WP_PROFILE_SYNC_PATH . 'includes/buddypress/cwps-bp.php';
require CIVICRM_WP_PROFILE_SYNC_PATH . 'includes/cai/cwps-cai.php';
require CIVICRM_WP_PROFILE_SYNC_PATH . 'includes/acf/cwps-acf-loader.php';
require CIVICRM_WP_PROFILE_SYNC_PATH . 'includes/mapper/cwps-mapper.php';
}
/**
* Set up this plugin's objects.
*
* @since 0.4
*/
public function setup_objects() {
// Initialise objects.
$this->admin = new CiviCRM_WP_Profile_Sync_Admin( $this );
$this->wp = new CiviCRM_WP_Profile_Sync_WordPress( $this );
$this->civicrm = new CiviCRM_WP_Profile_Sync_CiviCRM( $this );
$this->bp = new CiviCRM_WP_Profile_Sync_BuddyPress( $this );
$this->cai = new CiviCRM_WP_Profile_Sync_CAI( $this );
$this->acf = new CiviCRM_WP_Profile_Sync_ACF_Loader( $this );
$this->mapper = new CiviCRM_WP_Profile_Sync_Mapper( $this );
}
// -------------------------------------------------------------------------
/**
* Add BuddyPress sync hooks.
*
* @since 0.1
*/
public function hooks_bp_add() {
/**
* Broadcast that BuddyPress hooks should be added.
*
* @since 0.5.2
*/
do_action( 'cwps/plugin/hooks/bp/add' );
}
/**
* Remove BuddyPress sync hooks.
*
* @since 0.1
*/
public function hooks_bp_remove() {
/**
* Broadcast that BuddyPress hooks should be removed.
*
* @since 0.5.2
*/
do_action( 'cwps/plugin/hooks/bp/remove' );
}
/**
* Add WordPress sync hooks.
*
* Post-processes a CiviCRM Contact when a WordPress User is updated.
* Hooked in late to let other plugins go first.
*
* @since 0.1
*/
public function hooks_wp_add() {
/**
* Broadcast that WordPress hooks should be added.
*
* @since 0.5.2
*/
do_action( 'cwps/plugin/hooks/wp/add' );
}
/**
* Remove WordPress sync hooks.
*
* @since 0.1
*/
public function hooks_wp_remove() {
/**
* Broadcast that WordPress hooks should be removed.
*
* @since 0.5.2
*/
do_action( 'cwps/plugin/hooks/wp/remove' );
}
/**
* Add CiviCRM sync hooks.
*
* Syncs data to a WordPress User when a CiviCRM Contact is updated.
*
* @since 0.1
*/
public function hooks_civicrm_add() {
/**
* Broadcast that CiviCRM hooks should be added.
*
* @since 0.5.2
*/
do_action( 'cwps/plugin/hooks/civicrm/add' );
}
/**
* Remove CiviCRM sync hooks.
*
* @since 0.1
*/
public function hooks_civicrm_remove() {
/**
* Broadcast that CiviCRM hooks should be removed.
*
* @since 0.5.2
*/
do_action( 'cwps/plugin/hooks/civicrm/remove' );
}
// -------------------------------------------------------------------------
/**
* Check if this plugin is network activated.
*
* @since 0.4
*
* @return bool $is_network_active True if network activated, false otherwise.
*/
public function is_network_activated() {
// Only need to test once.
static $is_network_active;
// Have we done this already?
if ( isset( $is_network_active ) ) {
return $is_network_active;
}
// If not multisite, it cannot be.
if ( ! is_multisite() ) {
$is_network_active = false;
return $is_network_active;
}
// Make sure plugin file is included when outside admin.
if ( ! function_exists( 'is_plugin_active_for_network' ) ) {
require_once ABSPATH . '/wp-admin/includes/plugin.php';
}
// Get path from 'plugins' directory to this plugin.
$this_plugin = plugin_basename( CIVICRM_WP_PROFILE_SYNC_FILE );
// Test if network active.
$is_network_active = is_plugin_active_for_network( $this_plugin );
// --<
return $is_network_active;
}
/**
* Check if CiviCRM is network activated.
*
* @since 0.4
*
* @return bool $civicrm_network_active True if network activated, false otherwise.
*/
public function is_civicrm_network_activated() {
// Only need to test once.
static $civicrm_network_active;
// Have we done this already?
if ( isset( $civicrm_network_active ) ) {
return $civicrm_network_active;
}
// If not multisite, it cannot be.
if ( ! is_multisite() ) {
$civicrm_network_active = false;
return $civicrm_network_active;
}
// If CiviCRM's constant is not defined, we'll never know.
if ( ! defined( 'CIVICRM_PLUGIN_FILE' ) ) {
$civicrm_network_active = false;
return $civicrm_network_active;
}
// Make sure plugin file is included when outside admin.
if ( ! function_exists( 'is_plugin_active_for_network' ) ) {
require_once ABSPATH . '/wp-admin/includes/plugin.php';
}
// Get path from 'plugins' directory to CiviCRM's directory.
$civicrm = plugin_basename( CIVICRM_PLUGIN_FILE );
// Test if network active.
$civicrm_network_active = is_plugin_active_for_network( $civicrm );
// --<
return $civicrm_network_active;
}
/**
* Check if CiviCRM Admin Utilities is hiding CiviCRM except on main site.
*
* @since 0.4
*
* @return bool $civicrm_hidden True if CAU is hiding CiviCRM, false otherwise.
*/
public function is_civicrm_main_site_only() {
// Only need to test once.
static $civicrm_hidden;
// Have we done this already?
if ( isset( $civicrm_hidden ) ) {
return $civicrm_hidden;
}
// If not multisite, it cannot be.
if ( ! is_multisite() ) {
$civicrm_hidden = false;
return $civicrm_hidden;
}
// Bail if CiviCRM is not network-activated.
if ( ! $this->is_civicrm_network_activated() ) {
$civicrm_hidden = false;
return $civicrm_hidden;
}
// If CAU's constant is not defined, we'll never know.
if ( ! defined( 'CIVICRM_ADMIN_UTILITIES_VERSION' ) ) {
$civicrm_hidden = false;
return $civicrm_hidden;
}
// Grab the CAU plugin reference.
$cau = civicrm_au();
// Bail if CAU's multisite object is not defined.
if ( empty( $cau->multisite ) ) {
$civicrm_hidden = false;
return $civicrm_hidden;
}
// Bail if not hidden.
if ( $cau->multisite->setting_get( 'main_site_only', '0' ) == '0' ) {
$civicrm_hidden = false;
return $civicrm_hidden;
}
// CAU is hiding CiviCRM.
$civicrm_hidden = true;
return $civicrm_hidden;
}
/**
* Write to the error log.
*
* @since 0.5.8
*
* @param array $data The data to write to the log file.
*/
public function log_error( $data = [] ) {
// Skip if not debugging.
if ( CIVICRM_WP_PROFILE_SYNC_DEBUG === false ) {
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 );
}
}
/**
* Load plugin if not yet loaded and return reference.
*
* @since 0.1
*
* @return CiviCRM_WP_Profile_Sync $civicrm_wp_profile_sync The plugin reference.
*/
function civicrm_wp_profile_sync() {
// Declare as global.
global $civicrm_wp_profile_sync;
// Instantiate plugin if not yet instantiated.
if ( ! isset( $civicrm_wp_profile_sync ) ) {
$civicrm_wp_profile_sync = new CiviCRM_WP_Profile_Sync();
}
// --<
return $civicrm_wp_profile_sync;
}
// Load only when CiviCRM has loaded on the "plugins_loaded" hook.
add_action( 'civicrm_instance_loaded', 'civicrm_wp_profile_sync' );
/**
* Performs plugin activation tasks.
*
* @since 0.5
*/
function civicrm_wp_profile_sync_activate() {
/**
* Broadcast that this plugin has been activated.
*
* @since 0.5
*/
do_action( 'cwps/activated' );
}
// Activation.
register_activation_hook( __FILE__, 'civicrm_wp_profile_sync_activate' );
/**
* Performs plugin deactivation tasks.
*
* @since 0.5
*/
function civicrm_wp_profile_sync_deactivated() {
/**
* Broadcast that this plugin has been deactivated.
*
* @since 0.5
*/
do_action( 'cwps/deactivated' );
}
// Deactivation.
register_deactivation_hook( __FILE__, 'civicrm_wp_profile_sync_deactivated' );
/*
* Uninstall uses the 'uninstall.php' method.
* @see https://developer.wordpress.org/reference/functions/register_uninstall_hook/
*/