-
-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathcivicrm.basepage.php
1092 lines (894 loc) · 28.8 KB
/
civicrm.basepage.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
/*
+--------------------------------------------------------------------+
| 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;
}
/**
* Define CiviCRM_For_WordPress_Basepage Class.
*
* @since 4.6
*/
class CiviCRM_For_WordPress_Basepage {
/**
* @var object
* Plugin object reference.
* @since 4.6
* @access public
*/
public $civi;
/**
* @var bool
* Base Page parsed flag.
* @since 4.6
* @access public
*/
public $basepage_parsed = FALSE;
/**
* @var string
* Base Page title.
* @since 4.6
* @access public
*/
public $basepage_title = '';
/**
* @var string
* Base Page markup.
* @since 4.6
* @access public
*/
public $basepage_markup = '';
/**
* Instance constructor.
*
* @since 4.6
*/
public function __construct() {
// Store reference to CiviCRM plugin object.
$this->civi = civi_wp();
// Always listen for activation action.
add_action('civicrm_activation', [$this, 'activate']);
// Always listen for deactivation action.
add_action('civicrm_deactivation', [$this, 'deactivate']);
// Always check if the Base Page needs to be created.
add_action('civicrm_instance_loaded', [$this, 'maybe_create_basepage']);
}
/**
* Register hooks to handle CiviCRM in a WordPress Base Page context.
*
* @since 4.6
*/
public function register_hooks() {
// Kick out if not CiviCRM.
if (!$this->civi->initialize()) {
return;
}
// Cache CiviCRM Base Page markup.
add_action('wp', [$this, 'basepage_handler'], 10, 1);
}
/**
* Triggers the process whereby the WordPress Base Page is created.
*
* Sets a one-time-only option to flag that we need to create a Base Page -
* it will not update the option once it has been set to another value nor
* create a new option with the same name.
*
* As a result of doing this, we know that a Base Page needs to be created,
* but the moment to do so is once CiviCRM has been successfully installed.
*
* @see self::maybe_create_basepage()
*
* @since 5.6
*/
public function activate() {
// Save option.
add_option('civicrm_activation_create_basepage', 'true');
}
/**
* Plugin deactivation.
*
* @since 5.6
*/
public function deactivate() {
// Delete option.
delete_option('civicrm_activation_create_basepage');
}
/**
* Auto-creates the WordPress Base Page if necessary.
*
* Changes the one-time-only option so that the Base Page can only be created
* once. Thereafter, we're on our own until there's a 'delete_post' callback
* to prevent the Base Page from being deleted.
*
* @since 5.6
*/
public function maybe_create_basepage() {
// Bail if CiviCRM not installed.
if (!CIVICRM_INSTALLED) {
return;
}
// Bail if not installing.
if (get_option('civicrm_activation_create_basepage') !== 'true') {
return;
}
// Create the Base Page.
add_action('wp_loaded', [$this, 'create_wp_basepage']);
// Change option so the callback above never runs again.
update_option('civicrm_activation_create_basepage', 'done');
}
/**
* Creates the WordPress Base Page and saves the CiviCRM "wpBasePage" setting.
*
* @since 4.6
* @since 5.6 Relocated from CiviCRM_For_WordPress to here.
* @since 5.44 Returns success or failure.
*
* @return bool TRUE if successful, FALSE otherwise.
*/
public function create_wp_basepage() {
if (!$this->civi->initialize()) {
return FALSE;
}
if (version_compare(CRM_Core_BAO_Domain::getDomain()->version, '4.7.0', '<')) {
return FALSE;
}
// Bail if we already have a Base Page setting.
$config = CRM_Core_Config::singleton();
if (!empty($config->wpBasePage)) {
return TRUE;
}
/**
* Filter the default Base Page slug.
*
* @since 4.6
*
* @param str The default Base Page slug.
*/
$slug = apply_filters('civicrm_basepage_slug', 'civicrm');
// Get existing Page with that slug.
$page = get_page_by_path($slug);
// Get the ID if the Base Page already exists.
$result = 0;
if ($page instanceof WP_Post) {
$result = $page->ID;
}
// Create the Base Page if it's missing.
if ($result === 0) {
$result = $this->create_basepage($slug);
}
// Save the Page slug as the setting if we have one.
if ($result !== 0 && !is_wp_error($result)) {
$post = get_post($result);
civicrm_api3('Setting', 'create', [
'wpBasePage' => $post->post_name,
]);
return TRUE;
}
return FALSE;
}
/**
* Create a WordPress page to act as the CiviCRM Base Page.
*
* @since 4.6
* @since 5.6 Relocated from CiviCRM_For_WordPress to here.
*
* @param string $slug The unique slug for the page - same as wpBasePage setting.
* @return int|WP_Error The page ID on success. The value 0 or WP_Error on failure.
*/
private function create_basepage($slug) {
// If multisite, switch to main site.
if (is_multisite() && !is_main_site()) {
/**
* Allow plugins to override the switch to the main site.
*
* This filter changes the default behaviour on WordPress Multisite so
* that the Base Page *is* created on every site on which CiviCRM is
* activated. This is a more sensible and inclusive default, since the
* absence of the Base Page on a sub-site often leads to confusion.
*
* To restore the previous functionality, return boolean TRUE.
*
* The previous functionality may be the desired behaviour when the
* WordPress Multisite instance in question is one where sub-sites aren't
* truly "separate" e.g. sites built on frameworks such as "Commons in
* a Box" or "MultilingualPress".
*
* @since 5.44
*
* @param bool False by default prevents the switch to the main site.
*/
$switch = apply_filters('civicrm/basepage/main_site_only', FALSE);
if ($switch !== FALSE) {
// Store this site.
$original_site = get_current_blog_id();
// Switch to main site.
switch_to_blog(get_main_site_id());
}
}
// Define Base Page.
$page = [
'post_status' => 'publish',
'post_type' => 'page',
'post_parent' => 0,
'comment_status' => 'closed',
'ping_status' => 'closed',
// Quick fix for Windows.
'to_ping' => '',
// Quick fix for Windows.
'pinged' => '',
// Quick fix for Windows.
'post_content_filtered' => '',
// Quick fix for Windows.
'post_excerpt' => '',
'menu_order' => 0,
'post_name' => $slug,
];
/**
* Filter the default Base Page title.
*
* @since 4.6
*
* @param str The default Base Page title.
*/
$page['post_title'] = apply_filters('civicrm_basepage_title', __('CiviCRM', 'civicrm'));
// Default content.
$content = __('Do not delete this page. Page content is generated by CiviCRM.', 'civicrm');
/**
* Filter the default Base Page content.
*
* @since 4.6
*
* @param str $content The default Base Page content.
* @return str $content The modified Base Page content.
*/
$page['post_content'] = apply_filters('civicrm_basepage_content', $content);
// Insert the post into the database.
$page_id = wp_insert_post($page);
// Switch back if we've switched.
if (isset($original_site)) {
restore_current_blog();
}
// Make sure Rewrite Rules are flushed.
delete_option('civicrm_rules_flushed');
return $page_id;
}
/**
* Build CiviCRM Base Page content.
*
* Callback method for 'wp' hook, always called from WordPress front-end.
*
* @since 4.6
*
* @param object $wp The WordPress object, present but not used.
*/
public function basepage_handler($wp) {
/*
* At this point, all conditional tags are available.
* @see https://codex.wordpress.org/Conditional_Tags
*/
// Bail if this is a 404.
if (is_404()) {
return;
}
// Bail if this is a Favicon request.
if (function_exists('is_favicon') && is_favicon()) {
return;
}
// Check for the Base Page query conditions.
$is_basepage_query = FALSE;
if ($this->civi->civicrm_in_wordpress() && $this->civi->is_page_request()) {
$is_basepage_query = TRUE;
}
// Do not proceed without them.
if (!$is_basepage_query) {
return;
}
// Kick out if not CiviCRM.
if (!$this->civi->initialize()) {
return;
}
/**
* Fires before the Base Page is processed.
*
* @since 5.66
*/
do_action('civicrm/basepage/handler/pre');
// Set a "found" flag.
$basepage_found = FALSE;
// Check permission.
$denied = TRUE;
$argdata = $this->civi->get_request_args();
if ($this->civi->users->check_permission($argdata['args'])) {
$denied = FALSE;
}
// Get the Shortcode Mode setting.
$shortcode_mode = $this->civi->admin->get_shortcode_mode();
/*
* Let's do the_loop.
* This has the effect of bypassing the logic in:
* @see https://github.com/civicrm/civicrm-wordpress/pull/36
*/
if (have_posts()) {
while (have_posts()) {
the_post();
global $post;
/**
* Allow "Base Page mode" to be forced.
*
* Return TRUE to force CiviCRM to render a Post/Page as if on the Base Page.
*
* @since 5.44
*
* @param bool By default "Base Page mode" should not be triggered.
* @param WP_Post $post The current WordPress Post object.
*/
$basepage_mode = (bool) apply_filters('civicrm_force_basepage_mode', FALSE, $post);
// Determine if the current Post is the Base Page.
$is_basepage = $this->is_match($post->ID);
// Skip when this is not the Base Page or when "Base Page mode" is not forced or not in "legacy mode".
if ($is_basepage || $basepage_mode || $shortcode_mode === 'legacy') {
// Set context.
$this->civi->civicrm_context_set('basepage');
// Start buffering.
ob_start();
// Now, instead of echoing, Base Page output ends up in buffer.
$this->civi->invoke();
// Save the output and flush the buffer.
$this->basepage_markup = ob_get_clean();
/*
* The following logic is in response to some of the complexities of how
* titles are handled in WordPress, particularly when there are SEO
* plugins present that modify the title for Open Graph purposes. There
* have also been issues with the default WordPress themes, which modify
* the title using the 'wp_title' filter.
*
* First, we try and set the title of the page object, which will work
* if the loop is not run subsequently and if there are no additional
* filters on the title.
*
* Second, we store the CiviCRM title so that we can construct the base
* page title if other plugins modify it.
*/
// Override post title.
global $civicrm_wp_title;
$post->post_title = $civicrm_wp_title;
// Because the above seems unreliable, store title for later use.
$this->basepage_title = $civicrm_wp_title;
// Disallow commenting.
$post->comment_status = 'closed';
// Put CiviCRM into "Base Page mode".
$basepage_found = TRUE;
}
}
}
// Reset loop.
rewind_posts();
/**
* Fires after the Base Page may have been processed.
*
* @since 5.66
*
* @param bool $basepage_found TRUE if the CiviCRM Base Page was found, FALSE otherwise.
*/
do_action('civicrm/basepage/handler/post', $basepage_found);
// Bail if the Base Page has not been processed.
if (!$basepage_found) {
return;
}
// Hide the edit link.
add_action('edit_post_link', [$this, 'clear_edit_post_link']);
// Tweak admin bar.
add_action('wp_before_admin_bar_render', [$this, 'clear_edit_post_menu_item']);
// Add body classes for easier styling.
add_filter('body_class', [$this, 'add_body_classes']);
// In WordPress 4.6.0+, tell it URL params are part of canonical URL.
add_filter('get_canonical_url', [$this, 'basepage_canonical_url'], 999);
// Yoast SEO has separate way of establishing canonical URL.
add_filter('wpseo_canonical', [$this, 'basepage_canonical_url'], 999);
// And also for All in One SEO to handle canonical URL.
add_filter('aioseop_canonical_url', [$this, 'basepage_canonical_url'], 999);
// Override page title with high priority.
add_filter('wp_title', [$this, 'wp_page_title'], 100, 3);
add_filter('document_title_parts', [$this, 'wp_page_title_parts'], 100, 1);
// Regardless of URL, load page template.
add_filter('template_include', [$this, 'basepage_template'], 999);
// Show content based on permission.
if ($denied) {
// Do not show content.
add_filter('the_content', [$this->civi->users, 'get_permission_denied']);
}
else {
// Add core resources for front end.
add_action('wp', [$this->civi, 'front_end_page_load'], 100);
// Include this content when Base Page is rendered.
add_filter('the_content', [$this, 'basepage_render'], 21);
}
// Flag that we have parsed the Base Page.
$this->basepage_parsed = TRUE;
/**
* Broadcast that the Base Page is parsed.
*
* @since 4.4
*/
do_action('civicrm_basepage_parsed');
}
/**
* Get CiviCRM Base Page title for <title> element.
*
* Callback method for 'wp_title' hook, called at the end of function wp_title.
*
* @since 4.6
*
* @param string $title Title that might have already been set.
* @param string $separator Separator determined in theme (but defaults to WordPress default).
* @param string $separator_location Whether the separator should be left or right.
*/
public function wp_page_title($title, $separator = '»', $separator_location = '') {
// If feed, return just the title.
if (is_feed()) {
return $this->basepage_title;
}
// Set default separator location, if it isn't defined.
if ('' === trim($separator_location)) {
$separator_location = (is_rtl()) ? 'left' : 'right';
}
// If we have WP SEO present, use its separator.
if (class_exists('WPSEO_Options')) {
$separator_code = WPSEO_Options::get_default('wpseo_titles', 'separator');
$separator_array = WPSEO_Option_Titles::get_instance()->get_separator_options();
if (array_key_exists($separator_code, $separator_array)) {
$separator = $separator_array[$separator_code];
}
}
// Construct title depending on separator location.
if ($separator_location === 'right') {
$title = $this->basepage_title . " $separator " . get_bloginfo('name', 'display');
}
else {
$title = get_bloginfo('name', 'display') . " $separator " . $this->basepage_title;
}
// Return modified title.
return $title;
}
/**
* Get CiviCRM Base Page title for <title> element.
*
* Callback method for 'document_title_parts' hook. This filter was introduced
* in WordPress 3.8 but it depends on whether the theme has implemented that
* method for generating the title or not.
*
* @since 5.14
*
* @param array $parts The existing title parts.
* @return array $parts The modified title parts.
*/
public function wp_page_title_parts($parts) {
// Override with CiviCRM's title.
if (isset($parts['title'])) {
$parts['title'] = $this->basepage_title;
}
// Return modified title parts.
return $parts;
}
/**
* Get CiviCRM Base Page content.
*
* Callback method for 'the_content' hook, always called from WordPress
* front-end.
*
* @since 4.6
*
* @return str $basepage_markup The Base Page markup.
*/
public function basepage_render() {
// Hand back our Base Page markup.
return $this->basepage_markup;
}
/**
* Provide the canonical URL for a page accessed through a Base Page.
*
* WordPress will default to saying the canonical URL is the URL of the base
* page itself, but we need to indicate that in this case, the whole thing
* matters.
*
* Note: this function is used for three different but similar hooks:
* - `get_canonical_url` (WordPress 4.6.0+)
* - `aioseop_canonical_url` (All in One SEO)
* - `wpseo_canonical` (Yoast WordPress SEO)
*
* The native WordPress one passes the page object, while the other two do
* not. We don't actually need the page object, so the argument is omitted
* here.
*
* @since 4.6
*
* @param string $canonical The canonical URL.
* @return string The complete URL to the page as it should be accessed.
*/
public function basepage_canonical_url($canonical) {
// Access CiviCRM config object.
$config = CRM_Core_Config::singleton();
// None of the following needs a nonce check.
// phpcs:disable WordPress.Security.NonceVerification.Recommended
// Retain old logic when not using clean URLs.
if (!$config->cleanURL) {
$civiwp = empty($_GET['civiwp']) ? '' : sanitize_text_field(wp_unslash($_GET['civiwp']));
$q = empty($_GET['q']) ? '' : sanitize_text_field(wp_unslash($_GET['q']));
/*
* It would be better to specify which params are okay to accept as the
* canonical URLs, but this will work for the time being.
*/
if (empty($civiwp)
|| 'CiviCRM' !== $civiwp
|| empty($q)) {
return $canonical;
}
$path = $q;
unset($q, $_GET['q'], $civiwp, $_GET['civiwp']);
$query = http_build_query($_GET);
}
else {
$argdata = $this->civi->get_request_args();
$path = $argdata['argString'];
$query = http_build_query($_GET);
}
// phpcs:enable WordPress.Security.NonceVerification.Recommended
/*
* We should, however, build the URL the way that CiviCRM expects it to be
* (rather than through some other funny Base Page).
*/
return CRM_Utils_System::url($path, $query);
}
/**
* Get CiviCRM Base Page template.
*
* Callback method for 'template_include' hook, always called from WordPress
* front-end.
*
* @since 4.6
*
* @param string $template The path to the existing template.
* @return string $template The modified path to the desired template.
*/
public function basepage_template($template) {
// Get template path relative to the theme's root directory.
$template_name = str_replace(trailingslashit(get_stylesheet_directory()), '', $template);
// If the above fails, try parent theme.
if ($template_name === $template) {
$template_name = str_replace(trailingslashit(get_template_directory()), '', $template);
}
// Bail in the unlikely event that the template name has not been found.
if ($template_name === $template) {
return $template;
}
/**
* Allow Base Page template to be overridden.
*
* In most cases, the logic will not progress beyond here. Shortcodes in
* posts and pages will have a template set, so we leave them alone unless
* specifically overridden by the filter.
*
* @since 4.6
*
* @param string $template_name The provided template name.
*/
$basepage_template = apply_filters('civicrm_basepage_template', $template_name);
// Find the Base Page template.
$page_template = locate_template([$basepage_template]);
// If not homepage and template is found.
if (!is_front_page() && !empty($page_template)) {
return $page_template;
}
/**
* Override the template, but allow plugins to amend.
*
* This filter handles the scenario where no Base Page has been set, in
* which case CiviCRM will try to load its content in the site's homepage.
* Many themes, however, do not have a call to "the_content()" on the
* homepage - it is often used as a gateway page to display widgets,
* archives and so forth.
*
* Be aware that if the homepage is set to show latest posts, then this
* template override will not have the desired effect. A Base Page *must*
* be set if this is the case.
*
* @since 4.6
*
* @param string The template name (set to the default page template).
*/
$home_template_name = apply_filters('civicrm_basepage_home_template', 'page.php');
// Find the homepage template.
$home_template = locate_template([$home_template_name]);
// Use it if found.
if (!empty($home_template)) {
return $home_template;
}
// Fall back to provided template.
return $template;
}
/**
* Add classes to body element when on Base Page.
*
* This allows selectors to be written for particular CiviCRM "pages" despite
* them all being rendered on the one WordPress Base Page.
*
* @since 4.7.18
*
* @param array $classes The existing body classes.
* @return array $classes The modified body classes.
*/
public function add_body_classes($classes) {
$args = $this->civi->get_request_args();
// Bail if we don't have any.
if (is_null($args['argString'])) {
return $classes;
}
// Check for top level - it can be assumed this always 'civicrm'.
if (isset($args['args'][0]) && !empty($args['args'][0])) {
$classes[] = $args['args'][0];
}
// Check for second level - the component.
if (isset($args['args'][1]) && !empty($args['args'][1])) {
$classes[] = $args['args'][0] . '-' . $args['args'][1];
}
// Check for third level - the component's configuration.
if (isset($args['args'][2]) && !empty($args['args'][2])) {
$classes[] = $args['args'][0] . '-' . $args['args'][1] . '-' . $args['args'][2];
}
// Check for fourth level - because well, why not?
if (isset($args['args'][3]) && !empty($args['args'][3])) {
$classes[] = $args['args'][0] . '-' . $args['args'][1] . '-' . $args['args'][2] . '-' . $args['args'][3];
}
return $classes;
}
/**
* Remove edit link from page content.
*
* Callback from 'edit_post_link' hook.
*
* @since 4.6
* @since 5.33 Moved to this class.
*
* @return string Always empty.
*/
public function clear_edit_post_link() {
return '';
}
/**
* Remove edit link in WordPress Admin Bar.
*
* Callback from 'wp_before_admin_bar_render' hook.
*
* @since 4.6
*/
public function clear_edit_post_menu_item() {
// Access object.
global $wp_admin_bar;
// Bail if in admin.
if (is_admin()) {
return;
}
// Remove the menu item from front end.
$wp_admin_bar->remove_menu('edit');
}
/**
* Gets the current Base Page object.
*
* @since 5.44
*
* @return WP_Post|bool The Base Page object or FALSE on failure.
*/
public function basepage_get() {
// Bail if CiviCRM not bootstrapped.
if (!$this->civi->initialize()) {
return FALSE;
}
// Get config.
$config = CRM_Core_Config::singleton();
// Get Base Page object.
$basepage = get_page_by_path($config->wpBasePage);
if (is_null($basepage) || !($basepage instanceof WP_Post)) {
return FALSE;
}
/**
* Filters the CiviCRM Base Page object.
*
* @since 5.66
*
* @param WP_Post $basepage The CiviCRM Base Page object.
*/
return apply_filters('civicrm/basepage', $basepage);
}
/**
* Gets a URL that points to the CiviCRM Base Page.
*
* There can be situations where `CRM_Utils_System::url` does not return
* a link to the Base Page, e.g. in a page template where the content
* contains a Shortcode. This utility method will always return a URL
* that points to the CiviCRM Base Page.
*
* @see https://lab.civicrm.org/dev/wordpress/-/issues/144
*
* @since 5.69
*
* @param string $path The path being linked to, such as "civicrm/add".
* @param array|string $query A query string to append to the link, or an array of key-value pairs.
* @param bool $absolute Whether to force the output to be an absolute link.
* @param string $fragment A fragment identifier (named anchor) to append to the link.
* @param bool $htmlize Whether to encode special html characters such as &.
* @return string $link An HTML string containing a link to the given path.
*/
public function url(
$path = '',
$query = '',
$absolute = TRUE,
$fragment = NULL,
$htmlize = TRUE
) {
// Return early if no CiviCRM.
$link = '';
if (!$this->civi->initialize()) {
return $link;
}
// Add modifying callbacks prior to multi-lingual compat.
add_filter('civicrm/basepage/match', [$this, 'ensure_match'], 9);
add_filter('civicrm/core/url/base', [$this, 'ensure_url'], 9, 2);
// Pass to CiviCRM to construct front-end URL.
$link = CRM_Utils_System::url(
$path,
$query,
TRUE,
$fragment,
$htmlize,
TRUE,
FALSE
);
// Remove callbacks.
remove_filter('civicrm/basepage/match', [$this, 'ensure_match'], 9);
remove_filter('civicrm/core/url/base', [$this, 'ensure_url'], 9);
return $link;
}
/**
* Callback to ensure CiviCRM returns a Base Page URL.
*
* @since 5.69
*
* @return bool
*/
public function ensure_match() {
return TRUE;
}
/**
* Callback to ensure CiviCRM builds a Base Page URL.
*
* @since 5.69
*
* @param str $url The "base" URL as built by CiviCRM.
* @param bool $admin_request True if building an admin URL, false otherwise.
* @return str $url The Base Page URL.
*/
public function ensure_url($url, $admin_request) {
// Skip when not defined.
if (empty($url) || $admin_request) {
return $url;
}
// Return the Base Page URL.
return $this->url_get();
}
/**
* Gets the current Base Page ID.
*
* @since 5.66
*
* @return int|bool The Base Page ID or FALSE on failure.
*/
public function id_get() {
// Get the Base Page object.
$basepage = $this->basepage_get();
if (!($basepage instanceof WP_Post)) {
return FALSE;
}
return $basepage->ID;
}
/**
* Gets the current Base Page URL.
*
* @since 5.66
*
* @return str The Base Page URL or empty on failure.
*/
public function url_get() {
// Get the Base Page object.
$basepage = $this->basepage_get();
if (!($basepage instanceof WP_Post)) {
return '';
}