Merge pull request #19321 from colemanw/profileGetFieldsFix
[civicrm-core.git] / CRM / Utils / Check / Component / Cms.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17 class CRM_Utils_Check_Component_Cms extends CRM_Utils_Check_Component {
18
19 /**
20 * For sites running in WordPress, make sure the configured base page exists.
21 *
22 * @return CRM_Utils_Check_Message[]
23 */
24 public static function checkWpBasePage() {
25 $config = CRM_Core_Config::singleton();
26 if ($config->userFramework != 'WordPress') {
27 return [];
28 }
29 if (is_multisite()) {
30 // There are a lot potential configurations in a multisite context where
31 // this could show a false positive. This completely skips multisite for
32 // now.
33 return [];
34 }
35
36 switch (self::pageExists($config->wpBasePage)) {
37 case 1:
38 // Page is here and published
39 return [];
40
41 case 0:
42 $messageText = [
43 ts(
44 'CiviCRM relies upon a <a href="%1%2">base page in WordPress</a>, but it is not published.',
45 [
46 1 => $config->userFrameworkBaseURL,
47 2 => $config->wpBasePage,
48 ]
49 ),
50 ];
51 break;
52
53 case -1:
54 // Page is missing, but let's look around to see if the default is there
55 // --either the default as modified by civicrm_basepage_slug or the
56 // default default, `civicrm`.
57 $cmsSettings = CRM_Utils_System::url(
58 'civicrm/admin/setting',
59 $query = ['reset' => 1],
60 FALSE,
61 NULL,
62 TRUE,
63 FALSE,
64 TRUE
65 );
66 $messageText = [
67 ts(
68 'CiviCRM relies upon a base page in WordPress at %1%2, but it is missing.',
69 [
70 1 => $config->userFrameworkBaseURL,
71 2 => $config->wpBasePage,
72 ]
73 ),
74 ];
75
76 $altSlugs = array_unique([
77 apply_filters('civicrm_basepage_slug', 'civicrm'),
78 'civicrm',
79 ]);
80
81 if (in_array($config->wpBasePage, $altSlugs)) {
82 $messageText[] = ts(
83 'If you have an alternative base page, it can be set in the <a href="%2">WordPress integration settings</a>.',
84 [
85 1 => $config->userFrameworkBaseURL,
86 2 => $cmsSettings,
87 ]
88 );
89 }
90 else {
91 foreach ($altSlugs as $slug) {
92 $exists = self::pageExists($slug);
93 if ($exists >= 0) {
94 // One of the possible defaults is here, published or not.
95 $messageText[] = ts(
96 'The default is %1%2, which <a href="%1%2">does exist on this site</a>.',
97 [
98 1 => $config->userFrameworkBaseURL,
99 2 => $slug,
100 ]
101 );
102 if ($exists == 0) {
103 $messageText[] = ts('However, it is not published.');
104 }
105 // We've found one, and if the `civicrm_basepage_slug` filter has
106 // modified the default, we should go with it.
107 break;
108 }
109 }
110 if ($exists == -1) {
111 // We went through the default(s) and couldn't find one. Defer to
112 // the one modified by the filter.
113 $messageText[] = ts(
114 'The default is %1%2, but that does not exist on this site either.',
115 [
116 1 => $config->userFrameworkBaseURL,
117 2 => $altSlugs[0],
118 ]
119 );
120 }
121
122 $messageText[] = ts(
123 'You can set the correct base page in the <a href="%1">WordPress integration settings</a>.',
124 [1 => $cmsSettings]
125 );
126 }
127 }
128
129 return [
130 new CRM_Utils_Check_Message(
131 __FUNCTION__,
132 implode(' ', $messageText),
133 ts('WordPress Base Page Missing'),
134 \Psr\Log\LogLevel::ERROR,
135 'fa-wordpress'
136 ),
137 ];
138 }
139
140 /**
141 * See if a page exists and is published.
142 *
143 * @param string $slug
144 * The page path.
145 * @return int
146 * -1 if it's missing
147 * 0 if it's present but not published
148 * 1 if it's present and published
149 */
150 private static function pageExists($slug) {
151 $basePage = get_page_by_path($slug);
152 if (!$basePage) {
153 return -1;
154 }
155
156 return (int) ($basePage->post_status == 'publish');
157 }
158
159 }