-
-
Notifications
You must be signed in to change notification settings - Fork 84
/
civicrm.php
1614 lines (1340 loc) · 42 KB
/
civicrm.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
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
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Plugin Name: CiviCRM
* Description: CiviCRM - Growing and Sustaining Relationships
* Version: 4.7
* Requires at least: 4.9
* Requires PHP: 7.4
* Author: CiviCRM LLC
* Author URI: https://civicrm.org/
* Plugin URI: https://docs.civicrm.org/sysadmin/en/latest/install/wordpress/
* License: AGPL3
* Text Domain: civicrm
* Domain Path: /languages
*/
/*
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC. All rights reserved. |
| |
| This work is published under the GNU AGPLv3 license with some |
| permitted exceptions and without any warranty. For full license |
| and copyright information, see https://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* @package CRM
* @copyright CiviCRM LLC https://civicrm.org/licensing
*
*/
// This file must not accessed directly.
if (!defined('ABSPATH')) {
exit;
}
// Set version here: changing it forces Javascript and CSS to reload.
define('CIVICRM_PLUGIN_VERSION', '4.7');
// Store reference to this file.
if (!defined('CIVICRM_PLUGIN_FILE')) {
define('CIVICRM_PLUGIN_FILE', __FILE__);
}
// Store URL to this plugin's directory.
if (!defined('CIVICRM_PLUGIN_URL')) {
define('CIVICRM_PLUGIN_URL', plugin_dir_url(CIVICRM_PLUGIN_FILE));
}
// Store PATH to this plugin's directory.
if (!defined('CIVICRM_PLUGIN_DIR')) {
define('CIVICRM_PLUGIN_DIR', plugin_dir_path(CIVICRM_PLUGIN_FILE));
}
/*
* Minimum required PHP.
*
* Note: This duplicates `CRM_Upgrade_Incremental_General::MIN_INSTALL_PHP_VER`.
* The duplication helps avoid dependency issues. (Reading
* `CRM_Upgrade_Incremental_General::MIN_INSTALL_PHP_VER` requires loading
* `civicrm.settings.php`, but that triggers a parse-error on PHP 5.x.)
*
* @see CRM_Upgrade_Incremental_General::MIN_INSTALL_PHP_VER
* @see CiviWP\PhpVersionTest::testConstantMatch()
*/
if (!defined('CIVICRM_WP_PHP_MINIMUM')) {
define('CIVICRM_WP_PHP_MINIMUM', '7.4.0');
}
/*
* The constant `CIVICRM_SETTINGS_PATH` is also defined in `civicrm.config.php`
* and may already have been defined there - e.g. by cron or external scripts.
* These legacy routes should not be used because they try to bootstrap WordPress
* in unreliable ways. Use WP-CLI or WP-REST routes instead.
*/
if (!defined('CIVICRM_SETTINGS_PATH')) {
/*
* Test where the settings file exists.
*
* If the settings file is found in the 4.6 and prior location, use that as
* `CIVICRM_SETTINGS_PATH`, otherwise use the new location.
*/
$wp_civi_settings_deprecated = CIVICRM_PLUGIN_DIR . 'civicrm.settings.php';
if (file_exists($wp_civi_settings_deprecated)) {
define('CIVICRM_SETTINGS_PATH', $wp_civi_settings_deprecated);
}
else {
$upload_dir = wp_upload_dir();
$wp_civi_settings = implode(DIRECTORY_SEPARATOR, [$upload_dir['basedir'], 'civicrm', 'civicrm.settings.php']);
define('CIVICRM_SETTINGS_PATH', $wp_civi_settings);
}
}
// Test if CiviCRM is installed.
if (file_exists(CIVICRM_SETTINGS_PATH)) {
define('CIVICRM_INSTALLED', TRUE);
}
else {
define('CIVICRM_INSTALLED', FALSE);
}
// Prevent CiviCRM from rendering its own header.
define('CIVICRM_UF_HEAD', TRUE);
/**
* Setting this to 'TRUE' will replace all mailing URLs calls to 'extern/url.php'
* and 'extern/open.php' with their REST counterpart 'civicrm/v3/url' and
* 'civicrm/v3/open'.
*
* Use for test purposes, may affect mailing performance.
*
* @see CiviCRM_WP_REST\Plugin::replace_tracking_urls()
*/
if (!defined('CIVICRM_WP_REST_REPLACE_MAILING_TRACKING')) {
define('CIVICRM_WP_REST_REPLACE_MAILING_TRACKING', FALSE);
}
/**
* Define CiviCRM_For_WordPress Class.
*
* @since 4.4
*/
class CiviCRM_For_WordPress {
/**
* @var object
* Plugin instance.
* @since 4.4
* @access private
*/
private static $instance;
/**
* @var bool
* Plugin context (broad).
* @since 4.4
* @access public
*/
public static $in_wordpress;
/**
* @var string
* Plugin context (specific).
* @since 4.4
* @access public
*/
public static $context;
/**
* @var object
* Shortcodes management object.
* @since 4.4
* @access public
*/
public $shortcodes;
/**
* @var object
* Modal dialog management object.
* @since 4.4
* @access public
*/
public $modal;
/**
* @var object
* Base Page management object.
* @since 4.4
* @access public
*/
public $basepage;
/**
* @var object
* User management object.
* @since 4.4
* @access public
*/
public $users;
/**
* @var object
* The plugin compatibility object.
* @since 5.24
* @access public
*/
public $compat;
/**
* @var object
* Admin object.
* @since 5.33
* @access public
*/
public $admin;
/**
* @var array
* Reference to the original $_GET value.
* @since 4.6
* @access protected
*/
protected $wp_get;
/**
* @var array
* Reference to the original $_POST value.
* @since 4.6
* @access protected
*/
protected $wp_post;
/**
* @var array
* Reference to the original $_COOKIE value.
* @since 4.6
* @access protected
*/
protected $wp_cookie;
/**
* @var array
* Reference to the original $_REQUEST value.
* @since 4.6
* @access protected
*/
protected $wp_request;
// ---------------------------------------------------------------------------
// Setup
// ---------------------------------------------------------------------------
/**
* Getter method which returns the CiviCRM instance and optionally creates one
* if it does not already exist. Standard CiviCRM singleton pattern.
*
* @since 4.4
*
* @return object CiviCRM_For_WordPress The CiviCRM plugin instance.
*/
public static function singleton() {
// If instance doesn't already exist.
if (!isset(self::$instance)) {
// Create instance.
self::$instance = new CiviCRM_For_WordPress();
// Include global scope functions.
include_once CIVICRM_PLUGIN_DIR . 'includes/civicrm.functions.php';
// Add WP-CLI commands.
if (defined('WP_CLI') && WP_CLI) {
include_once CIVICRM_PLUGIN_DIR . 'wp-cli/wp-cli-civicrm.php';
}
// Delay setup until 'plugins_loaded' to allow other plugins to load as well.
add_action('plugins_loaded', [self::$instance, 'setup_instance']);
}
// Return instance.
return self::$instance;
}
/**
* Dummy instance constructor.
*
* @since 4.4
*/
public function __construct() {}
/**
* Dummy magic method to prevent CiviCRM_For_WordPress from being cloned.
*
* @since 4.4
*/
public function __clone() {
_doing_it_wrong(__FUNCTION__, __('Only one instance of CiviCRM_For_WordPress please', 'civicrm'), '4.4');
}
/**
* Dummy magic method to prevent CiviCRM_For_WordPress from being unserialized.
*
* @since 4.4
*/
public function __wakeup() {
_doing_it_wrong(__FUNCTION__, __('Please do not serialize CiviCRM_For_WordPress', 'civicrm'), '4.4');
}
/**
* Plugin activation.
*
* This method is called only when CiviCRM plugin is activated. Other plugins
* are able to interact with CiviCRM's activation because "plugins_loaded" has
* already fired.
*
* Since CiviCRM has an Installer UI when activated via the WordPress Plugins
* screen, this method sets an option that can be read on the next page load
* allowing `self::activation()` to redirect to it when possible.
*
* @since 4.4
*/
public function activate() {
// Set a one-time-only option.
add_option('civicrm_activation_in_progress', 'true');
// Include and init classes because "plugins_loaded" has already fired.
include_once CIVICRM_PLUGIN_DIR . 'includes/civicrm.users.php';
$this->users = new CiviCRM_For_WordPress_Users();
/**
* Broadcast that the CiviCRM plugin has been activated.
*
* Used internally by:
*
* - CiviCRM_For_WordPress_Users::activate()
*
* @since 5.44
*/
do_action('civicrm_activate');
}
/**
* Runs the CiviCRM activation procedure when activated via the WordPress UI.
*
* @since 4.4
*/
public function activation() {
// Bail if not activating.
if (get_option('civicrm_activation_in_progress') !== 'true') {
return;
}
// Bail if not in WordPress admin.
if (!is_admin()) {
return;
}
/**
* Broadcast that activation via the WordPress UI has happened.
*
* This fires on the admin page load that happens directly after the CiviCRM
* plugin has been activated via the WordPress UI.
*
* @since 5.6
*/
do_action('civicrm_activation');
// Change option so this action never fires again.
update_option('civicrm_activation_in_progress', 'false');
// When installed via the WordPress UI, try and redirect to the Installer page.
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$activate_multi = isset($_GET['activate-multi']) ? sanitize_text_field(wp_unslash($_GET['activate-multi'])) : '';
if (!is_multisite() && empty($activate_multi) && !CIVICRM_INSTALLED) {
wp_safe_redirect(admin_url('admin.php?page=civicrm-install'));
exit;
}
}
/**
* Plugin deactivation.
*
* This method is called only when CiviCRM plugin is deactivated. In order for
* other plugins to be able to interact with CiviCRM's activation, we need to
* remove any options that are set in activate() above.
*
* @since 4.4
*/
public function deactivate() {
// Delete any options we hay have set.
delete_option('civicrm_activation_in_progress');
/**
* Broadcast that deactivation actions need to happen now.
*
* Used internally by:
*
* - CiviCRM_For_WordPress_Users::deactivate()
*
* @since 5.6
*/
do_action('civicrm_deactivation');
}
// ---------------------------------------------------------------------------
// Plugin set up
// ---------------------------------------------------------------------------
/**
* Set up the CiviCRM plugin instance.
*
* @since 4.4
*/
public function setup_instance() {
// Kick out if another instance is being inited.
if (isset(self::$in_wordpress)) {
wp_die(__('Only one instance of CiviCRM_For_WordPress please', 'civicrm'));
}
// Maybe start session.
$this->maybe_start_session();
/*
* AJAX calls do not set the 'cms.root' item, so make sure it is set here so
* the CiviCRM doesn't fall back on flaky directory traversal code.
*/
global $civicrm_paths;
if (empty($civicrm_paths['cms.root']['path'])) {
$civicrm_paths['cms.root']['path'] = untrailingslashit(ABSPATH);
}
if (empty($civicrm_paths['cms.root']['url'])) {
$civicrm_paths['cms.root']['url'] = home_url();
}
// Include class files and instantiate.
$this->include_files();
$this->setup_objects();
// Do plugin activation when activated via the WordPress UI.
$this->activation();
// Use translation files.
$this->enable_translation();
// Register all hooks on init.
add_action('init', [$this, 'register_hooks']);
/**
* Broadcast that this plugin is now loaded.
*
* Used internally by:
*
* - CiviCRM_For_WordPress_Basepage::maybe_create_basepage()
*
* @since 4.4
*/
do_action('civicrm_instance_loaded');
}
/**
* Maybe start a session for CiviCRM.
*
* There is no session handling in WordPress so start it for CiviCRM pages.
*
* Not needed when running:
*
* - via WP-CLI
* - via wp-cron.php
* - via PHP on the command line
*
* none of which require sessions.
*
* @since 5.28
*/
public function maybe_start_session() {
// Get existing session ID.
$session_id = session_id();
// Check WordPress pseudo-cron.
$wp_cron = FALSE;
if (function_exists('wp_doing_cron') && wp_doing_cron()) {
$wp_cron = TRUE;
}
// Check WP-CLI.
$wp_cli = FALSE;
if (defined('WP_CLI') && WP_CLI) {
$wp_cli = TRUE;
}
// Check PHP on the command line - e.g. `cv`.
$php_cli = TRUE;
if (PHP_SAPI !== 'cli') {
$php_cli = FALSE;
}
// Maybe start session.
if (empty($session_id) && !$wp_cron && !$wp_cli && !$php_cli) {
session_start();
}
}
/**
* Include files.
*
* @since 4.4
*/
public function include_files() {
// Include class files.
include_once CIVICRM_PLUGIN_DIR . 'includes/civicrm.admin.php';
include_once CIVICRM_PLUGIN_DIR . 'includes/civicrm.users.php';
include_once CIVICRM_PLUGIN_DIR . 'includes/civicrm.shortcodes.php';
include_once CIVICRM_PLUGIN_DIR . 'includes/civicrm.shortcodes.modal.php';
include_once CIVICRM_PLUGIN_DIR . 'includes/civicrm.basepage.php';
include_once CIVICRM_PLUGIN_DIR . 'includes/civicrm.compat.php';
// Maybe include REST API autoloader class.
if (!class_exists('CiviCRM_WP_REST\Autoloader')) {
require_once CIVICRM_PLUGIN_DIR . 'wp-rest/Autoloader.php';
}
}
/**
* Instantiate objects.
*
* @since 5.33
*/
public function setup_objects() {
// Instantiate objects.
$this->admin = new CiviCRM_For_WordPress_Admin();
$this->users = new CiviCRM_For_WordPress_Users();
$this->shortcodes = new CiviCRM_For_WordPress_Shortcodes();
$this->modal = new CiviCRM_For_WordPress_Shortcodes_Modal();
$this->basepage = new CiviCRM_For_WordPress_Basepage();
$this->compat = new CiviCRM_For_WordPress_Compat();
}
/**
* Load translation files.
*
* A good reference on how to implement translation in WordPress:
* http://ottopress.com/2012/internationalization-youre-probably-doing-it-wrong/
*
* Also see:
* https://developer.wordpress.org/plugins/internationalization/
*
* @since 4.4
*/
public function enable_translation() {
// Load translations.
// phpcs:ignore WordPress.WP.DeprecatedParameters.Load_plugin_textdomainParam2Found
load_plugin_textdomain(
// Unique name.
'civicrm',
// Deprecated argument.
FALSE,
// Relative path to translation files.
dirname(plugin_basename(__FILE__)) . '/languages/'
);
}
// ---------------------------------------------------------------------------
// Context
// ---------------------------------------------------------------------------
/**
* Set broad CiviCRM context.
*
* Setter for determining if CiviCRM is currently being displayed in WordPress.
* This becomes true whe CiviCRM is called in the following contexts:
*
* (a) In the WordPress back-end.
* (b) When CiviCRM content is being displayed on the front-end via the Base Page.
* (c) When an AJAX request is made to CiviCRM.
*
* It is NOT true when CiviCRM is called via a Shortcode.
*
* @since 4.4
*/
public function civicrm_in_wordpress_set() {
// Store identifying query var.
$page = get_query_var('civiwp');
self::$in_wordpress = ($page === 'CiviCRM') ? TRUE : FALSE;
}
/**
* Getter for testing if CiviCRM is currently being displayed in WordPress.
*
* @see $this->civicrm_in_wordpress_set()
*
* @since 4.4
*
* @return bool $in_wordpress True if CiviCRM is displayed in WordPress, false otherwise.
*/
public function civicrm_in_wordpress() {
/**
* Allow broad context to be filtered.
*
* @since 4.4
*
* @param bool $in_wordpress True if CiviCRM is displayed in WordPress, false otherwise.
*/
return apply_filters('civicrm_in_wordpress', self::$in_wordpress);
}
/**
* Set specific CiviCRM context.
*
* Setter for determining how CiviCRM is currently being displayed in WordPress.
* This can be one of the following contexts:
*
* (a) In the WordPress back-end.
* (b) When CiviCRM content is being displayed on the front-end via the Base Page.
* (c) When a "non-page" request is made to CiviCRM.
* (d) When CiviCRM is called via a Shortcode.
*
* The following codes correspond to the different contexts:
*
* (a) 'admin'
* (b) 'basepage'
* (c) 'nonpage'
* (d) 'shortcode'
*
* @since 4.4
*
* @param string $context One of the four context codes above.
*/
public function civicrm_context_set($context) {
// Store.
self::$context = $context;
}
/**
* Get specific CiviCRM context.
*
* Getter for determining how CiviCRM is currently being displayed in WordPress.
*
* @see $this->civicrm_context_set()
*
* @since 4.4
*
* @return string The context in which CiviCRM is displayed in WordPress.
*/
public function civicrm_context_get() {
/**
* Allow specific context to be filtered.
*
* Used internally by:
*
* - CiviCRM_For_WordPress_Shortcodes::get_context()
*
* @since 4.4
*
* @param bool $context The existing context in which CiviCRM is displayed in WordPress.
*/
return apply_filters('civicrm_context', self::$context);
}
// ---------------------------------------------------------------------------
// Hooks
// ---------------------------------------------------------------------------
/**
* Register hooks on init.
*
* @since 4.4
*/
public function register_hooks() {
// Always add the common hooks.
$this->register_hooks_common();
// When in WordPress admin.
if (is_admin()) {
// Set context.
$this->civicrm_context_set('admin');
// Handle WordPress Admin context.
$this->admin->register_hooks();
// Enable Shortcode modal.
$this->modal->register_hooks();
return;
}
// Go no further if CiviCRM not installed yet.
if (!CIVICRM_INSTALLED) {
return;
}
// Attempt to replace 'page' query arg with 'civiwp'.
add_filter('request', [$this, 'maybe_replace_page_query_var']);
// Add our query vars.
add_filter('query_vars', [$this, 'query_vars']);
// Delay everything else until query has been parsed.
add_action('parse_query', [$this, 'register_hooks_front_end']);
}
/**
* Register hooks for the front end.
*
* @since 5.6
*
* @param WP_Query $query The WP_Query instance (passed by reference).
*/
public function register_hooks_front_end($query) {
// Bail if $query is not the main loop.
if (!$query->is_main_query()) {
return;
}
// Bail if filters are suppressed on this query.
if (TRUE === $query->get('suppress_filters')) {
return;
}
// Prevent multiple calls.
static $alreadyRegistered = FALSE;
if ($alreadyRegistered) {
return;
}
$alreadyRegistered = TRUE;
// Redirect if old query var is present.
if ('CiviCRM' === get_query_var('page') && 'CiviCRM' !== get_query_var('civiwp')) {
$redirect_url = remove_query_arg('page', FALSE);
$redirect_url = add_query_arg('civiwp', 'CiviCRM', $redirect_url);
wp_safe_redirect($redirect_url, 301);
exit();
}
// Store context.
$this->civicrm_in_wordpress_set();
// When the CiviCRM query var is detected.
if ($this->civicrm_in_wordpress()) {
/*
* Directly output CiviCRM html only in a few cases and skip WordPress
* templating:
*
* (a) when a snippet is set
* (b) when there is an AJAX call
* (c) for an iCal feed (unless 'html' is specified)
* (d) for file download URLs
*/
if (!$this->is_page_request()) {
// Set context.
$this->civicrm_context_set('nonpage');
// Add core resources for front end.
add_action('wp', [$this, 'front_end_page_load']);
// Echo all output when WordPress has been set up but nothing has been rendered.
add_action('wp', [$this, 'invoke']);
return;
}
}
// Let the classes decide how to handle other requests.
$this->basepage->register_hooks();
$this->shortcodes->register_hooks();
}
/**
* Register hooks that must always be present.
*
* @since 4.4
*/
public function register_hooks_common() {
// Register user hooks.
$this->users->register_hooks();
// Register hooks for clean URLs.
$this->register_hooks_clean_urls();
if (!class_exists('CiviCRM_WP_REST\Plugin')) {
// Set up REST API.
CiviCRM_WP_REST\Autoloader::add_source($source_path = trailingslashit(CIVICRM_PLUGIN_DIR . 'wp-rest'));
// Init REST API.
new CiviCRM_WP_REST\Plugin();
}
}
/**
* Register hooks to handle Clean URLs.
*
* @since 5.12
*/
public function register_hooks_clean_urls() {
// Bail if CiviCRM is not installed.
if (!CIVICRM_INSTALLED) {
return;
}
// Bail if we can't initialize CiviCRM.
if (!$this->initialize()) {
return;
}
// Bail if CiviCRM is not using Clean URLs.
if (!defined('CIVICRM_CLEANURL') || CIVICRM_CLEANURL !== 1) {
return;
}
// Have we flushed rewrite rules?
if (get_option('civicrm_rules_flushed') !== 'true') {
// Apply custom rewrite rules, then flush rules afterwards.
$this->rewrite_rules(TRUE);
// Set a one-time-only option to flag that this has been done.
add_option('civicrm_rules_flushed', 'true');
}
else {
// Apply custom rewrite rules normally.
$this->rewrite_rules();
}
}
// ---------------------------------------------------------------------------
// Construction of URLs
// ---------------------------------------------------------------------------
/**
* Add our rewrite rules.
*
* @since 5.7
*
* @param bool $flush_rewrite_rules True if rules should be flushed, false otherwise.
*/
public function rewrite_rules($flush_rewrite_rules = FALSE) {
// Kick out if not CiviCRM.
if (!$this->initialize()) {
return;
}
// Get config.
$config = CRM_Core_Config::singleton();
// Get Base Page object.
$basepage = get_page_by_path($config->wpBasePage);
// Sanity check.
if (!is_object($basepage)) {
return;
}
// Let's add Rewrite Rule when viewing the Base Page.
add_rewrite_rule(
'^' . $config->wpBasePage . '/([^?]*)?',
'index.php?page_id=' . $basepage->ID . '&civiwp=CiviCRM&q=civicrm%2F$matches[1]',
'top'
);
// Maybe force flush.
if ($flush_rewrite_rules) {
flush_rewrite_rules();
}
/**
* Broadcast the rewrite rules event.
*
* Used internally by:
*
* - CiviCRM_For_WordPress_Compat::rewrite_rules_polylang()
*
* @since 5.7
* @since 5.24 Added $basepage parameter.
*
* @param bool $flush_rewrite_rules True if rules flushed, false otherwise.
* @param WP_Post $basepage The Base Page post object.
*/
do_action('civicrm_after_rewrite_rules', $flush_rewrite_rules, $basepage);
}
/**
* Add our query vars.
*
* @since 5.7
*
* @param array $query_vars The existing query vars.
* @return array $query_vars The modified query vars.
*/
public function query_vars($query_vars) {
// Sanity check.
if (!is_array($query_vars)) {
$query_vars = [];
}
// Build our query vars.
$civicrm_query_vars = [
// URL query vars.
'civiwp', 'q', 'reset', 'id', 'html', 'snippet',
// Shortcode query vars.
'action', 'mode', 'cid', 'gid', 'sid', 'cs', 'force',
];
/**
* Filter the default CiviCRM query vars.
*
* Use in combination with `civicrm_query_vars_assigned` action to ensure
* that any other query vars are included in the assignment to the
* super-global arrays.
*
* @since 5.7
*
* @param array $civicrm_query_vars The default set of query vars.
*/
$civicrm_query_vars = apply_filters('civicrm_query_vars', $civicrm_query_vars);
// Now add them to WordPress.
foreach ($civicrm_query_vars as $civicrm_query_var) {
$query_vars[] = $civicrm_query_var;
}
return $query_vars;
}
/**
* Filters the request right after WordPress has parsed it and replaces the
* 'page' query variable with 'civiwp' if applicable.
*
* Prevents old URLs like example.org/civicrm/?page=CiviCRM&q=what/ever/path&reset=1
* being redirected to example.org/civicrm/?civiwp=CiviCRM&q=what/ever/path&reset=1
*
* @see https://lab.civicrm.org/dev/wordpress/-/issues/49
*
* @since 5.26
*
* @param array $query_vars The existing query vars.
* @return array $query_vars The modified query vars.
*/
public function maybe_replace_page_query_var($query_vars) {
$civi_query_arg = array_search('CiviCRM', $query_vars);
// Bail if the query var is not 'page'.
if (FALSE === $civi_query_arg || $civi_query_arg !== 'page') {
return $query_vars;
}
unset($query_vars['page']);
$query_vars['civiwp'] = 'CiviCRM';
return $query_vars;
}
// ---------------------------------------------------------------------------
// CiviCRM Initialisation
// ---------------------------------------------------------------------------
/**
* Initialize CiviCRM.
*
* This method has been moved to "includes/civicrm.admin.php"
*
* @since 4.4
* @since 5.33 Placeholder for backwards (and semantic) compatibility.
*
* @return bool True if CiviCRM is initialized, false otherwise.
*/
public function initialize() {