Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
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 | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | * $Id$ |
17 | * | |
18 | */ | |
19 | ||
20 | /** | |
21 | * WordPress specific stuff goes here | |
22 | */ | |
23 | class CRM_Utils_System_WordPress extends CRM_Utils_System_Base { | |
6714d8d2 | 24 | |
bb3a214a | 25 | /** |
bb3a214a | 26 | */ |
00be9182 | 27 | public function __construct() { |
4caaa696 EM |
28 | /** |
29 | * deprecated property to check if this is a drupal install. The correct method is to have functions on the UF classes for all UF specific | |
30 | * functions and leave the codebase oblivious to the type of CMS | |
31 | * @deprecated | |
32 | * @var bool | |
33 | */ | |
6a488035 | 34 | $this->is_drupal = FALSE; |
fe17e8d1 | 35 | $this->is_wordpress = TRUE; |
6a488035 TO |
36 | } |
37 | ||
9509977f TO |
38 | public function initialize() { |
39 | parent::initialize(); | |
40 | $this->registerPathVars(); | |
41 | } | |
42 | ||
43 | /** | |
44 | * Specify the default computation for various paths/URLs. | |
45 | */ | |
46 | protected function registerPathVars():void { | |
716bdc94 TO |
47 | $isNormalBoot = function_exists('get_option'); |
48 | if ($isNormalBoot) { | |
49 | // Normal mode - CMS boots first, then calls Civi. "Normal" web pages and newer extern routes. | |
50 | // To simplify the code-paths, some items are re-registered with WP-specific functions. | |
51 | $cmsRoot = function() { | |
9509977f | 52 | return [ |
716bdc94 TO |
53 | 'path' => untrailingslashit(ABSPATH), |
54 | 'url' => home_url(), | |
9509977f | 55 | ]; |
716bdc94 TO |
56 | }; |
57 | Civi::paths()->register('cms', $cmsRoot); | |
58 | Civi::paths()->register('cms.root', $cmsRoot); | |
59 | Civi::paths()->register('civicrm.files', function () { | |
60 | $upload_dir = wp_get_upload_dir(); | |
9509977f | 61 | return [ |
716bdc94 TO |
62 | 'path' => $upload_dir['basedir'] . DIRECTORY_SEPARATOR . 'civicrm' . DIRECTORY_SEPARATOR, |
63 | 'url' => $upload_dir['baseurl'] . '/civicrm/', | |
9509977f TO |
64 | ]; |
65 | }); | |
716bdc94 TO |
66 | Civi::paths()->register('civicrm.root', function () { |
67 | return [ | |
68 | 'path' => CIVICRM_PLUGIN_DIR . 'civicrm' . DIRECTORY_SEPARATOR, | |
69 | 'url' => CIVICRM_PLUGIN_URL . 'civicrm/', | |
70 | ]; | |
71 | }); | |
72 | Civi::paths()->register('wp.frontend.base', function () { | |
73 | return [ | |
74 | 'url' => home_url('/'), | |
75 | ]; | |
76 | }); | |
77 | Civi::paths()->register('wp.frontend', function () { | |
78 | $config = CRM_Core_Config::singleton(); | |
79 | $basepage = get_page_by_path($config->wpBasePage); | |
80 | return [ | |
81 | 'url' => get_permalink($basepage->ID), | |
82 | ]; | |
83 | }); | |
84 | Civi::paths()->register('wp.backend.base', function () { | |
85 | return [ | |
86 | 'url' => admin_url(), | |
87 | ]; | |
88 | }); | |
89 | Civi::paths()->register('wp.backend', function() { | |
90 | return [ | |
91 | 'url' => admin_url('admin.php'), | |
92 | ]; | |
93 | }); | |
94 | } | |
95 | else { | |
96 | // Legacy support - only relevant for older extern routes. | |
97 | Civi::paths() | |
98 | ->register('wp.frontend.base', function () { | |
99 | return ['url' => rtrim(CIVICRM_UF_BASEURL, '/') . '/']; | |
100 | }) | |
101 | ->register('wp.frontend', function () { | |
102 | $config = \CRM_Core_Config::singleton(); | |
103 | $suffix = defined('CIVICRM_UF_WP_BASEPAGE') ? CIVICRM_UF_WP_BASEPAGE : $config->wpBasePage; | |
104 | return [ | |
105 | 'url' => Civi::paths()->getVariable('wp.frontend.base', 'url') . $suffix, | |
106 | ]; | |
107 | }) | |
108 | ->register('wp.backend.base', function () { | |
109 | return ['url' => rtrim(CIVICRM_UF_BASEURL, '/') . '/wp-admin/']; | |
110 | }) | |
111 | ->register('wp.backend', function () { | |
112 | return [ | |
113 | 'url' => Civi::paths()->getVariable('wp.backend.base', 'url') . 'admin.php', | |
114 | ]; | |
115 | }); | |
116 | } | |
9509977f TO |
117 | } |
118 | ||
6a488035 | 119 | /** |
17f443df | 120 | * @inheritDoc |
6a488035 | 121 | */ |
00be9182 | 122 | public function setTitle($title, $pageTitle = NULL) { |
6a488035 TO |
123 | if (!$pageTitle) { |
124 | $pageTitle = $title; | |
125 | } | |
1beae3b1 | 126 | |
17f443df CW |
127 | // FIXME: Why is this global? |
128 | global $civicrm_wp_title; | |
129 | $civicrm_wp_title = $title; | |
1beae3b1 | 130 | |
17f443df CW |
131 | // yes, set page title, depending on context |
132 | $context = civi_wp()->civicrm_context_get(); | |
133 | switch ($context) { | |
134 | case 'admin': | |
135 | case 'shortcode': | |
136 | $template = CRM_Core_Smarty::singleton(); | |
137 | $template->assign('pageTitle', $pageTitle); | |
6a488035 TO |
138 | } |
139 | } | |
140 | ||
7ba2c8ad KC |
141 | /** |
142 | * Moved from CRM_Utils_System_Base | |
143 | */ | |
144 | public function getDefaultFileStorage() { | |
8ce9d9d6 TO |
145 | $config = CRM_Core_Config::singleton(); |
146 | $cmsUrl = CRM_Utils_System::languageNegotiationURL($config->userFrameworkBaseURL, FALSE, TRUE); | |
147 | $cmsPath = $this->cmsRootPath(); | |
148 | $filesPath = CRM_Utils_File::baseFilePath(); | |
149 | $filesRelPath = CRM_Utils_File::relativize($filesPath, $cmsPath); | |
150 | $filesURL = rtrim($cmsUrl, '/') . '/' . ltrim($filesRelPath, ' /'); | |
be2fb01f | 151 | return [ |
8ce9d9d6 TO |
152 | 'url' => CRM_Utils_File::addTrailingSlash($filesURL, '/'), |
153 | 'path' => CRM_Utils_File::addTrailingSlash($filesPath), | |
be2fb01f | 154 | ]; |
8ce9d9d6 TO |
155 | } |
156 | ||
157 | /** | |
158 | * Determine the location of the CiviCRM source tree. | |
159 | * | |
160 | * @return array | |
161 | * - url: string. ex: "http://example.com/sites/all/modules/civicrm" | |
162 | * - path: string. ex: "/var/www/sites/all/modules/civicrm" | |
163 | */ | |
164 | public function getCiviSourceStorage() { | |
7ba2c8ad | 165 | global $civicrm_root; |
7ba2c8ad | 166 | |
8ce9d9d6 TO |
167 | // Don't use $config->userFrameworkBaseURL; it has garbage on it. |
168 | // More generally, we shouldn't be using $config here. | |
169 | if (!defined('CIVICRM_UF_BASEURL')) { | |
170 | throw new RuntimeException('Undefined constant: CIVICRM_UF_BASEURL'); | |
7ba2c8ad | 171 | } |
8ce9d9d6 TO |
172 | |
173 | $cmsPath = $this->cmsRootPath(); | |
174 | ||
175 | // $config = CRM_Core_Config::singleton(); | |
176 | // overkill? // $cmsUrl = CRM_Utils_System::languageNegotiationURL($config->userFrameworkBaseURL, FALSE, TRUE); | |
177 | $cmsUrl = CIVICRM_UF_BASEURL; | |
178 | if (CRM_Utils_System::isSSL()) { | |
179 | $cmsUrl = str_replace('http://', 'https://', $cmsUrl); | |
7ba2c8ad | 180 | } |
d8182404 | 181 | $civiRelPath = CRM_Utils_File::relativize(realpath($civicrm_root), realpath($cmsPath)); |
8ce9d9d6 | 182 | $civiUrl = rtrim($cmsUrl, '/') . '/' . ltrim($civiRelPath, ' /'); |
be2fb01f | 183 | return [ |
8ce9d9d6 TO |
184 | 'url' => CRM_Utils_File::addTrailingSlash($civiUrl, '/'), |
185 | 'path' => CRM_Utils_File::addTrailingSlash($civicrm_root), | |
be2fb01f | 186 | ]; |
7ba2c8ad KC |
187 | } |
188 | ||
6a488035 | 189 | /** |
17f443df | 190 | * @inheritDoc |
6a488035 | 191 | */ |
00be9182 | 192 | public function appendBreadCrumb($breadCrumbs) { |
6a488035 TO |
193 | $breadCrumb = wp_get_breadcrumb(); |
194 | ||
195 | if (is_array($breadCrumbs)) { | |
196 | foreach ($breadCrumbs as $crumbs) { | |
197 | if (stripos($crumbs['url'], 'id%%')) { | |
be2fb01f | 198 | $args = ['cid', 'mid']; |
6a488035 TO |
199 | foreach ($args as $a) { |
200 | $val = CRM_Utils_Request::retrieve($a, 'Positive', CRM_Core_DAO::$_nullObject, | |
201 | FALSE, NULL, $_GET | |
202 | ); | |
203 | if ($val) { | |
204 | $crumbs['url'] = str_ireplace("%%{$a}%%", $val, $crumbs['url']); | |
205 | } | |
206 | } | |
207 | } | |
208 | $breadCrumb[] = "<a href=\"{$crumbs['url']}\">{$crumbs['title']}</a>"; | |
209 | } | |
210 | } | |
211 | ||
212 | $template = CRM_Core_Smarty::singleton(); | |
213 | $template->assign_by_ref('breadcrumb', $breadCrumb); | |
214 | wp_set_breadcrumb($breadCrumb); | |
215 | } | |
216 | ||
217 | /** | |
17f443df | 218 | * @inheritDoc |
6a488035 | 219 | */ |
00be9182 | 220 | public function resetBreadCrumb() { |
be2fb01f | 221 | $bc = []; |
6a488035 TO |
222 | wp_set_breadcrumb($bc); |
223 | } | |
224 | ||
225 | /** | |
17f443df | 226 | * @inheritDoc |
6a488035 | 227 | */ |
00be9182 | 228 | public function addHTMLHead($head) { |
6a488035 TO |
229 | static $registered = FALSE; |
230 | if (!$registered) { | |
231 | // front-end view | |
be2fb01f | 232 | add_action('wp_head', [__CLASS__, '_showHTMLHead']); |
6a488035 | 233 | // back-end views |
be2fb01f | 234 | add_action('admin_head', [__CLASS__, '_showHTMLHead']); |
6a488035 | 235 | } |
be2fb01f | 236 | CRM_Core_Region::instance('wp_head')->add([ |
6a488035 | 237 | 'markup' => $head, |
be2fb01f | 238 | ]); |
6a488035 TO |
239 | } |
240 | ||
17f443df | 241 | /** |
fe482240 | 242 | * WP action callback. |
17f443df | 243 | */ |
00be9182 | 244 | public static function _showHTMLHead() { |
6a488035 TO |
245 | $region = CRM_Core_Region::instance('wp_head', FALSE); |
246 | if ($region) { | |
247 | echo $region->render(''); | |
248 | } | |
249 | } | |
250 | ||
251 | /** | |
17f443df | 252 | * @inheritDoc |
6a488035 | 253 | */ |
00be9182 | 254 | public function mapConfigToSSL() { |
6a488035 TO |
255 | global $base_url; |
256 | $base_url = str_replace('http://', 'https://', $base_url); | |
257 | } | |
258 | ||
259 | /** | |
17f443df | 260 | * @inheritDoc |
6a488035 | 261 | */ |
408b79bf | 262 | public function url( |
6a488035 TO |
263 | $path = NULL, |
264 | $query = NULL, | |
265 | $absolute = FALSE, | |
266 | $fragment = NULL, | |
6a488035 | 267 | $frontend = FALSE, |
8de2a34e SL |
268 | $forceBackend = FALSE, |
269 | $htmlize = TRUE | |
6a488035 | 270 | ) { |
353ffa53 TO |
271 | $config = CRM_Core_Config::singleton(); |
272 | $script = ''; | |
c80e2dbf | 273 | $separator = '&'; |
353ffa53 | 274 | $wpPageParam = ''; |
887f5d81 | 275 | $fragment = isset($fragment) ? ('#' . $fragment) : ''; |
6a488035 TO |
276 | |
277 | $path = CRM_Utils_String::stripPathChars($path); | |
df17aa21 | 278 | $basepage = FALSE; |
6a488035 TO |
279 | |
280 | //this means wp function we are trying to use is not available, | |
281 | //so load bootStrap | |
d8182404 | 282 | // FIXME: Why bootstrap in url()? Generally want to define 1-2 strategic places to put bootstrap |
6a488035 | 283 | if (!function_exists('get_option')) { |
d8182404 | 284 | $this->loadBootStrap(); |
6a488035 | 285 | } |
df17aa21 | 286 | |
6a488035 | 287 | if ($config->userFrameworkFrontend) { |
df17aa21 | 288 | global $post; |
887f5d81 | 289 | if (get_option('permalink_structure') != '') { |
6a488035 TO |
290 | $script = get_permalink($post->ID); |
291 | } | |
df17aa21 CW |
292 | if ($config->wpBasePage == $post->post_name) { |
293 | $basepage = TRUE; | |
294 | } | |
01aca362 | 295 | // when shortcode is included in page |
6a488035 | 296 | // also make sure we have valid query object |
df17aa21 | 297 | // FIXME: $wpPageParam has no effect and is only set on the *basepage* |
6a488035 | 298 | global $wp_query; |
df17aa21 | 299 | if (get_option('permalink_structure') == '' && method_exists($wp_query, 'get')) { |
6a488035 | 300 | if (get_query_var('page_id')) { |
887f5d81 | 301 | $wpPageParam = "page_id=" . get_query_var('page_id'); |
6a488035 TO |
302 | } |
303 | elseif (get_query_var('p')) { | |
304 | // when shortcode is inserted in post | |
887f5d81 | 305 | $wpPageParam = "p=" . get_query_var('p'); |
6a488035 TO |
306 | } |
307 | } | |
308 | } | |
309 | ||
887f5d81 TO |
310 | $base = $this->getBaseUrl($absolute, $frontend, $forceBackend); |
311 | ||
312 | if (!isset($path) && !isset($query)) { | |
313 | // FIXME: This short-circuited codepath is the same as the general one below, except | |
314 | // in that it ignores "permlink_structure" / $wpPageParam / $script . I don't know | |
315 | // why it's different (and I can only find two obvious use-cases for this codepath, | |
316 | // of which at least one looks gratuitous). A more ambitious person would simply remove | |
317 | // this code. | |
318 | return $base . $fragment; | |
319 | } | |
320 | ||
321 | if (!$forceBackend && get_option('permalink_structure') != '' && ($wpPageParam || $script != '')) { | |
322 | $base = $script; | |
6a488035 TO |
323 | } |
324 | ||
be2fb01f | 325 | $queryParts = []; |
df17aa21 | 326 | |
df17aa21 CW |
327 | if ( |
328 | // not using clean URLs | |
329 | !$config->cleanURL | |
330 | // requesting an admin URL | |
331 | || ((is_admin() && !$frontend) || $forceBackend) | |
332 | // is shortcode | |
333 | || (!$basepage && $script != '') | |
334 | ) { | |
335 | ||
336 | // pre-existing logic | |
337 | if (isset($path)) { | |
25bd5735 CW |
338 | // Admin URLs still need "page=CiviCRM", front-end URLs do not. |
339 | if ((is_admin() && !$frontend) || $forceBackend) { | |
340 | $queryParts[] = 'page=CiviCRM'; | |
341 | } | |
342 | else { | |
343 | $queryParts[] = 'civiwp=CiviCRM'; | |
344 | } | |
cdef34e0 | 345 | $queryParts[] = 'q=' . rawurlencode($path); |
df17aa21 CW |
346 | } |
347 | if ($wpPageParam) { | |
348 | $queryParts[] = $wpPageParam; | |
349 | } | |
ffa62912 | 350 | if (!empty($query)) { |
df17aa21 CW |
351 | $queryParts[] = $query; |
352 | } | |
353 | ||
354 | $final = $base . '?' . implode($separator, $queryParts) . $fragment; | |
355 | ||
887f5d81 | 356 | } |
df17aa21 CW |
357 | else { |
358 | ||
359 | // clean URLs | |
360 | if (isset($path)) { | |
361 | $base = trailingslashit($base) . str_replace('civicrm/', '', $path) . '/'; | |
362 | } | |
363 | if (isset($query)) { | |
364 | $query = ltrim($query, '=?&'); | |
365 | $queryParts[] = $query; | |
366 | } | |
367 | ||
368 | if (!empty($queryParts)) { | |
369 | $final = $base . '?' . implode($separator, $queryParts) . $fragment; | |
370 | } | |
371 | else { | |
372 | $final = $base . $fragment; | |
373 | } | |
374 | ||
6a488035 TO |
375 | } |
376 | ||
df17aa21 | 377 | return $final; |
887f5d81 TO |
378 | } |
379 | ||
bb3a214a | 380 | /** |
f553d1ea KC |
381 | * 27-09-2016 |
382 | * CRM-16421 CRM-17633 WIP Changes to support WP in it's own directory | |
383 | * https://wiki.civicrm.org/confluence/display/CRM/WordPress+installed+in+its+own+directory+issues | |
384 | * For now leave hard coded wp-admin references. | |
385 | * TODO: remove wp-admin references and replace with admin_url() in the future. Look at best way to get path to admin_url | |
386 | * | |
bb3a214a EM |
387 | * @param $absolute |
388 | * @param $frontend | |
389 | * @param $forceBackend | |
390 | * | |
391 | * @return mixed|null|string | |
392 | */ | |
887f5d81 | 393 | private function getBaseUrl($absolute, $frontend, $forceBackend) { |
353ffa53 | 394 | $config = CRM_Core_Config::singleton(); |
6a488035 | 395 | if ((is_admin() && !$frontend) || $forceBackend) { |
f553d1ea | 396 | return Civi::paths()->getUrl('[wp.backend]/.', $absolute ? 'absolute' : 'relative'); |
6a488035 | 397 | } |
f553d1ea KC |
398 | else { |
399 | return Civi::paths()->getUrl('[wp.frontend]/.', $absolute ? 'absolute' : 'relative'); | |
36b820ae | 400 | } |
01aca362 | 401 | } |
6a488035 TO |
402 | |
403 | /** | |
17f443df | 404 | * @inheritDoc |
6a488035 | 405 | */ |
00be9182 | 406 | public function authenticate($name, $password, $loadCMSBootstrap = FALSE, $realPath = NULL) { |
6a488035 TO |
407 | $config = CRM_Core_Config::singleton(); |
408 | ||
409 | if ($loadCMSBootstrap) { | |
9ba02e3e TO |
410 | $config->userSystem->loadBootStrap([ |
411 | 'name' => $name, | |
412 | 'pass' => $password, | |
413 | ]); | |
6a488035 TO |
414 | } |
415 | ||
416 | $user = wp_authenticate($name, $password); | |
417 | if (is_a($user, 'WP_Error')) { | |
418 | return FALSE; | |
419 | } | |
420 | ||
17f443df | 421 | // TODO: need to change this to make sure we matched only one row |
6a488035 TO |
422 | |
423 | CRM_Core_BAO_UFMatch::synchronizeUFMatch($user->data, $user->data->ID, $user->data->user_email, 'WordPress'); | |
424 | $contactID = CRM_Core_BAO_UFMatch::getContactId($user->data->ID); | |
425 | if (!$contactID) { | |
426 | return FALSE; | |
427 | } | |
be2fb01f | 428 | return [$contactID, $user->data->ID, mt_rand()]; |
6a488035 TO |
429 | } |
430 | ||
431 | /** | |
17f443df | 432 | * FIXME: Do something |
ea3ddccf | 433 | * |
434 | * @param string $message | |
6a488035 | 435 | */ |
00be9182 | 436 | public function setMessage($message) { |
6a488035 TO |
437 | } |
438 | ||
bb3a214a | 439 | /** |
b596c3e9 | 440 | * @param \string $user |
ea3ddccf | 441 | * |
442 | * @return bool | |
bb3a214a | 443 | */ |
e7292422 | 444 | public function loadUser($user) { |
b596c3e9 | 445 | $userdata = get_user_by('login', $user); |
446 | if (!$userdata->data->ID) { | |
7ca9cd52 | 447 | return FALSE; |
b596c3e9 | 448 | } |
449 | ||
450 | $uid = $userdata->data->ID; | |
451 | wp_set_current_user($uid); | |
452 | $contactID = CRM_Core_BAO_UFMatch::getContactId($uid); | |
453 | ||
454 | // lets store contact id and user id in session | |
455 | $session = CRM_Core_Session::singleton(); | |
456 | $session->set('ufID', $uid); | |
457 | $session->set('userID', $contactID); | |
e7292422 | 458 | return TRUE; |
6a488035 TO |
459 | } |
460 | ||
17f443df CW |
461 | /** |
462 | * FIXME: Use CMS-native approach | |
309310bf | 463 | * @throws \CRM_Core_Exception |
17f443df | 464 | */ |
00be9182 | 465 | public function permissionDenied() { |
309310bf | 466 | throw new CRM_Core_Exception(ts('You do not have permission to access this page.')); |
6a488035 TO |
467 | } |
468 | ||
8ee9bea9 SL |
469 | /** |
470 | * Determine the native ID of the CMS user. | |
471 | * | |
472 | * @param string $username | |
e97c66ff | 473 | * |
474 | * @return int|null | |
8ee9bea9 SL |
475 | */ |
476 | public function getUfId($username) { | |
477 | $userdata = get_user_by('login', $username); | |
478 | if (!$userdata->data->ID) { | |
479 | return NULL; | |
480 | } | |
481 | return $userdata->data->ID; | |
482 | } | |
483 | ||
17f443df CW |
484 | /** |
485 | * @inheritDoc | |
486 | */ | |
00be9182 | 487 | public function logout() { |
6a488035 TO |
488 | // destroy session |
489 | if (session_id()) { | |
490 | session_destroy(); | |
491 | } | |
492 | wp_logout(); | |
493 | wp_redirect(wp_login_url()); | |
494 | } | |
495 | ||
6a488035 | 496 | /** |
17f443df | 497 | * @inheritDoc |
6a488035 | 498 | */ |
00be9182 | 499 | public function getUFLocale() { |
742eb5c6 CW |
500 | // Bail early if method is called when WordPress isn't bootstrapped. |
501 | // Additionally, the function checked here is located in pluggable.php | |
502 | // and is required by wp_get_referer() - so this also bails early if it is | |
503 | // called too early in the request lifecycle. | |
504 | // @see https://core.trac.wordpress.org/ticket/25294 | |
505 | if (!function_exists('wp_validate_redirect')) { | |
506 | return NULL; | |
cba2601a | 507 | } |
742eb5c6 CW |
508 | |
509 | // Default to WordPress User locale. | |
510 | $locale = get_user_locale(); | |
511 | ||
512 | // Is this a "back-end" AJAX call? | |
513 | $is_backend = FALSE; | |
514 | if (wp_doing_ajax() && FALSE !== strpos(wp_get_referer(), admin_url())) { | |
515 | $is_backend = TRUE; | |
19780d2b | 516 | } |
742eb5c6 CW |
517 | |
518 | // Ignore when in WordPress admin or it's a "back-end" AJAX call. | |
519 | if (!(is_admin() || $is_backend)) { | |
520 | ||
521 | // Reaching here means it is very likely to be a front-end context. | |
522 | ||
523 | // Default to WordPress locale. | |
524 | $locale = get_locale(); | |
525 | ||
526 | // Maybe override with the locale that Polylang reports. | |
527 | if (function_exists('pll_current_language')) { | |
528 | $pll_locale = pll_current_language('locale'); | |
529 | if (!empty($pll_locale)) { | |
530 | $locale = $pll_locale; | |
531 | } | |
532 | } | |
533 | ||
534 | // Maybe override with the locale that WPML reports. | |
535 | elseif (defined('ICL_LANGUAGE_CODE')) { | |
536 | $languages = apply_filters('wpml_active_languages', NULL); | |
537 | foreach ($languages as $language) { | |
538 | if ($language['active']) { | |
539 | $locale = $language['default_locale']; | |
540 | break; | |
541 | } | |
542 | } | |
543 | } | |
544 | ||
545 | // TODO: Set locale for other WordPress plugins. | |
546 | // @see https://wordpress.org/plugins/tags/multilingual/ | |
547 | // A hook would be nice here. | |
548 | ||
2c3d151b | 549 | } |
19780d2b | 550 | |
742eb5c6 CW |
551 | if (!empty($locale)) { |
552 | // If for some reason only we get a language code, convert it to a locale. | |
553 | if (2 === strlen($locale)) { | |
554 | $locale = CRM_Core_I18n_PseudoConstant::longForShort($locale); | |
555 | } | |
556 | return $locale; | |
0db6c3e1 TO |
557 | } |
558 | else { | |
19780d2b DL |
559 | return NULL; |
560 | } | |
6a488035 TO |
561 | } |
562 | ||
fd1f3a26 SV |
563 | /** |
564 | * @inheritDoc | |
565 | */ | |
566 | public function setUFLocale($civicrm_language) { | |
567 | // TODO (probably not possible with WPML?) | |
568 | return TRUE; | |
569 | } | |
570 | ||
6a488035 | 571 | /** |
fe482240 | 572 | * Load wordpress bootstrap. |
6a488035 | 573 | * |
9ba02e3e TO |
574 | * @param array $params |
575 | * Optional credentials | |
576 | * - name: string, cms username | |
577 | * - pass: string, cms password | |
6714d8d2 SL |
578 | * @param bool $loadUser |
579 | * @param bool $throwError | |
580 | * @param mixed $realPath | |
f4aaa82a EM |
581 | * |
582 | * @return bool | |
309310bf | 583 | * @throws \CRM_Core_Exception |
6a488035 | 584 | */ |
be2fb01f | 585 | public function loadBootStrap($params = [], $loadUser = TRUE, $throwError = TRUE, $realPath = NULL) { |
05fcde76 | 586 | global $wp, $wp_rewrite, $wp_the_query, $wp_query, $wpdb, $current_site, $current_blog, $current_user; |
6a488035 | 587 | |
9c1bc317 CW |
588 | $name = $params['name'] ?? NULL; |
589 | $pass = $params['pass'] ?? NULL; | |
9ba02e3e | 590 | |
7a44e49f | 591 | if (!defined('WP_USE_THEMES')) { |
c5f77355 | 592 | define('WP_USE_THEMES', FALSE); |
7a44e49f | 593 | } |
6a488035 TO |
594 | |
595 | $cmsRootPath = $this->cmsRootPath(); | |
596 | if (!$cmsRootPath) { | |
309310bf | 597 | throw new CRM_Core_Exception("Could not find the install directory for WordPress"); |
6a488035 | 598 | } |
aaffa79f | 599 | $path = Civi::settings()->get('wpLoadPhp'); |
b299b1cc | 600 | if (!empty($path)) { |
35da5d8d | 601 | require_once $path; |
b299b1cc KC |
602 | } |
603 | elseif (file_exists($cmsRootPath . DIRECTORY_SEPARATOR . 'wp-load.php')) { | |
35da5d8d | 604 | require_once $cmsRootPath . DIRECTORY_SEPARATOR . 'wp-load.php'; |
b299b1cc KC |
605 | } |
606 | else { | |
309310bf | 607 | throw new CRM_Core_Exception("Could not find the bootstrap file for WordPress"); |
35da5d8d | 608 | } |
6491539b DL |
609 | $wpUserTimezone = get_option('timezone_string'); |
610 | if ($wpUserTimezone) { | |
611 | date_default_timezone_set($wpUserTimezone); | |
612 | CRM_Core_Config::singleton()->userSystem->setMySQLTimeZone(); | |
613 | } | |
e7292422 | 614 | require_once $cmsRootPath . DIRECTORY_SEPARATOR . 'wp-includes/pluggable.php'; |
9c1bc317 | 615 | $uid = $params['uid'] ?? NULL; |
17763922 WA |
616 | if (!$uid) { |
617 | $name = $name ? $name : trim(CRM_Utils_Array::value('name', $_REQUEST)); | |
618 | $pass = $pass ? $pass : trim(CRM_Utils_Array::value('pass', $_REQUEST)); | |
619 | if ($name) { | |
d8182404 | 620 | $uid = wp_authenticate($name, $pass); |
17763922 WA |
621 | if (!$uid) { |
622 | if ($throwError) { | |
623 | echo '<br />Sorry, unrecognized username or password.'; | |
624 | exit(); | |
625 | } | |
626 | return FALSE; | |
627 | } | |
628 | } | |
629 | } | |
fe1e7958 | 630 | if ($uid) { |
a4111333 CW |
631 | if ($uid instanceof WP_User) { |
632 | $account = wp_set_current_user($uid->ID); | |
c5f77355 CW |
633 | } |
634 | else { | |
a4111333 CW |
635 | $account = wp_set_current_user($uid); |
636 | } | |
fe1e7958 | 637 | if ($account && $account->data->ID) { |
638 | global $user; | |
639 | $user = $account; | |
640 | return TRUE; | |
641 | } | |
642 | } | |
e7292422 | 643 | return TRUE; |
6a488035 TO |
644 | } |
645 | ||
bb3a214a EM |
646 | /** |
647 | * @param $dir | |
648 | * | |
649 | * @return bool | |
650 | */ | |
00be9182 | 651 | public function validInstallDir($dir) { |
dfbcf0b7 | 652 | $includePath = "$dir/wp-includes"; |
468176f6 | 653 | if (@file_exists("$includePath/version.php")) { |
dfbcf0b7 DL |
654 | return TRUE; |
655 | } | |
656 | return FALSE; | |
657 | } | |
658 | ||
bb3a214a EM |
659 | /** |
660 | * Determine the location of the CMS root. | |
661 | * | |
72b3a70c CW |
662 | * @return string|NULL |
663 | * local file system path to CMS root, or NULL if it cannot be determined | |
bb3a214a | 664 | */ |
00be9182 | 665 | public function cmsRootPath() { |
02bfbc78 CW |
666 | |
667 | // Return early if the path is already set. | |
a93a0366 TO |
668 | global $civicrm_paths; |
669 | if (!empty($civicrm_paths['cms.root']['path'])) { | |
670 | return $civicrm_paths['cms.root']['path']; | |
671 | } | |
672 | ||
02bfbc78 | 673 | // Return early if constant has been defined. |
dfbcf0b7 DL |
674 | if (defined('CIVICRM_CMSDIR')) { |
675 | if ($this->validInstallDir(CIVICRM_CMSDIR)) { | |
02bfbc78 | 676 | return CIVICRM_CMSDIR; |
dfbcf0b7 | 677 | } |
6a488035 | 678 | } |
02bfbc78 CW |
679 | |
680 | // Return early if path to wp-load.php can be retrieved from settings. | |
681 | $setting = Civi::settings()->get('wpLoadPhp'); | |
682 | if (!empty($setting)) { | |
57811df8 KC |
683 | $path = str_replace('wp-load.php', '', $setting); |
684 | $cmsRoot = rtrim($path, '/\\'); | |
02bfbc78 CW |
685 | if ($this->validInstallDir($cmsRoot)) { |
686 | return $cmsRoot; | |
687 | } | |
688 | } | |
689 | ||
690 | /* | |
691 | * Keep previous logic as fallback of last resort. | |
692 | * | |
693 | * At some point, it would be good to remove this because there are serious | |
694 | * problems in correctly locating WordPress in this manner. In summary, it | |
695 | * is impossible to do so reliably. | |
696 | * | |
697 | * @see https://github.com/civicrm/civicrm-wordpress/pull/63#issuecomment-61792328 | |
698 | * @see https://github.com/civicrm/civicrm-core/pull/11086#issuecomment-335454992 | |
699 | */ | |
700 | $cmsRoot = $valid = NULL; | |
701 | ||
702 | $pathVars = explode('/', str_replace('\\', '/', $_SERVER['SCRIPT_FILENAME'])); | |
703 | ||
704 | // Might be Windows installation. | |
705 | $firstVar = array_shift($pathVars); | |
706 | if ($firstVar) { | |
707 | $cmsRoot = $firstVar; | |
708 | } | |
709 | ||
710 | // Start with CMS dir search. | |
711 | foreach ($pathVars as $var) { | |
712 | $cmsRoot .= "/$var"; | |
713 | if ($this->validInstallDir($cmsRoot)) { | |
714 | // Stop as we found bootstrap. | |
715 | $valid = TRUE; | |
716 | break; | |
717 | } | |
6a488035 TO |
718 | } |
719 | ||
720 | return ($valid) ? $cmsRoot : NULL; | |
721 | } | |
722 | ||
bb3a214a | 723 | /** |
17f443df | 724 | * @inheritDoc |
bb3a214a | 725 | */ |
00be9182 | 726 | public function createUser(&$params, $mail) { |
be2fb01f | 727 | $user_data = [ |
6a488035 TO |
728 | 'ID' => '', |
729 | 'user_pass' => $params['cms_pass'], | |
730 | 'user_login' => $params['cms_name'], | |
731 | 'user_email' => $params[$mail], | |
732 | 'nickname' => $params['cms_name'], | |
733 | 'role' => get_option('default_role'), | |
be2fb01f | 734 | ]; |
6a488035 TO |
735 | if (isset($params['contactID'])) { |
736 | $contactType = CRM_Contact_BAO_Contact::getContactType($params['contactID']); | |
737 | if ($contactType == 'Individual') { | |
738 | $user_data['first_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', | |
739 | $params['contactID'], 'first_name' | |
740 | ); | |
741 | $user_data['last_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', | |
742 | $params['contactID'], 'last_name' | |
743 | ); | |
744 | } | |
745 | } | |
746 | ||
747 | $uid = wp_insert_user($user_data); | |
748 | ||
be2fb01f | 749 | $creds = []; |
6a488035 TO |
750 | $creds['user_login'] = $params['cms_name']; |
751 | $creds['user_password'] = $params['cms_pass']; | |
752 | $creds['remember'] = TRUE; | |
753 | $user = wp_signon($creds, FALSE); | |
754 | ||
755 | wp_new_user_notification($uid, $user_data['user_pass']); | |
756 | return $uid; | |
757 | } | |
758 | ||
f4aaa82a | 759 | /** |
17f443df | 760 | * @inheritDoc |
6a488035 | 761 | */ |
00be9182 | 762 | public function updateCMSName($ufID, $ufName) { |
6a488035 TO |
763 | // CRM-10620 |
764 | if (function_exists('wp_update_user')) { | |
353ffa53 | 765 | $ufID = CRM_Utils_Type::escape($ufID, 'Integer'); |
6a488035 TO |
766 | $ufName = CRM_Utils_Type::escape($ufName, 'String'); |
767 | ||
be2fb01f | 768 | $values = ['ID' => $ufID, 'user_email' => $ufName]; |
481a74f4 TO |
769 | if ($ufID) { |
770 | wp_update_user($values); | |
6a488035 TO |
771 | } |
772 | } | |
773 | } | |
774 | ||
bb3a214a | 775 | /** |
c490a46a | 776 | * @param array $params |
bb3a214a EM |
777 | * @param $errors |
778 | * @param string $emailName | |
779 | */ | |
00be9182 | 780 | public function checkUserNameEmailExists(&$params, &$errors, $emailName = 'email') { |
6a488035 TO |
781 | $config = CRM_Core_Config::singleton(); |
782 | ||
353ffa53 TO |
783 | $dao = new CRM_Core_DAO(); |
784 | $name = $dao->escape(CRM_Utils_Array::value('name', $params)); | |
6a488035 TO |
785 | $email = $dao->escape(CRM_Utils_Array::value('mail', $params)); |
786 | ||
a7488080 | 787 | if (!empty($params['name'])) { |
6a488035 TO |
788 | if (!validate_username($params['name'])) { |
789 | $errors['cms_name'] = ts("Your username contains invalid characters"); | |
790 | } | |
791 | elseif (username_exists(sanitize_user($params['name']))) { | |
be2fb01f | 792 | $errors['cms_name'] = ts('The username %1 is already taken. Please select another username.', [1 => $params['name']]); |
6a488035 TO |
793 | } |
794 | } | |
795 | ||
a7488080 | 796 | if (!empty($params['mail'])) { |
6a488035 TO |
797 | if (!is_email($params['mail'])) { |
798 | $errors[$emailName] = "Your email is invaid"; | |
799 | } | |
800 | elseif (email_exists($params['mail'])) { | |
db18d815 | 801 | $errors[$emailName] = ts('The email address %1 already has an account associated with it. <a href="%2">Have you forgotten your password?</a>', |
be2fb01f | 802 | [1 => $params['mail'], 2 => wp_lostpassword_url()] |
6a488035 TO |
803 | ); |
804 | } | |
805 | } | |
806 | } | |
807 | ||
808 | /** | |
17f443df | 809 | * @inheritDoc |
6a488035 TO |
810 | */ |
811 | public function isUserLoggedIn() { | |
812 | $isloggedIn = FALSE; | |
813 | if (function_exists('is_user_logged_in')) { | |
814 | $isloggedIn = is_user_logged_in(); | |
815 | } | |
816 | ||
817 | return $isloggedIn; | |
818 | } | |
819 | ||
8caad0ce | 820 | /** |
821 | * @inheritDoc | |
822 | */ | |
823 | public function isUserRegistrationPermitted() { | |
824 | if (!get_option('users_can_register')) { | |
825 | return FALSE; | |
826 | } | |
827 | return TRUE; | |
828 | } | |
829 | ||
63df6889 HD |
830 | /** |
831 | * @inheritDoc | |
832 | */ | |
1a6630be | 833 | public function isPasswordUserGenerated() { |
63df6889 HD |
834 | return TRUE; |
835 | } | |
836 | ||
bb3a214a EM |
837 | /** |
838 | * @return mixed | |
839 | */ | |
00be9182 | 840 | public function getLoggedInUserObject() { |
2b617cb0 | 841 | if (function_exists('is_user_logged_in') && |
353ffa53 TO |
842 | is_user_logged_in() |
843 | ) { | |
2b617cb0 EM |
844 | global $current_user; |
845 | } | |
846 | return $current_user; | |
847 | } | |
353ffa53 | 848 | |
6a488035 | 849 | /** |
17f443df | 850 | * @inheritDoc |
6a488035 TO |
851 | */ |
852 | public function getLoggedInUfID() { | |
853 | $ufID = NULL; | |
2b617cb0 | 854 | $current_user = $this->getLoggedInUserObject(); |
2e1f50d6 | 855 | return $current_user->ID ?? NULL; |
2b617cb0 EM |
856 | } |
857 | ||
858 | /** | |
17f443df | 859 | * @inheritDoc |
2b617cb0 | 860 | */ |
00be9182 | 861 | public function getLoggedInUniqueIdentifier() { |
2b617cb0 EM |
862 | $user = $this->getLoggedInUserObject(); |
863 | return $this->getUniqueIdentifierFromUserObject($user); | |
6a488035 TO |
864 | } |
865 | ||
32998c82 EM |
866 | /** |
867 | * Get User ID from UserFramework system (Joomla) | |
77855840 TO |
868 | * @param object $user |
869 | * Object as described by the CMS. | |
72b3a70c CW |
870 | * |
871 | * @return int|null | |
32998c82 | 872 | */ |
00be9182 | 873 | public function getUserIDFromUserObject($user) { |
32998c82 EM |
874 | return !empty($user->ID) ? $user->ID : NULL; |
875 | } | |
876 | ||
2b617cb0 | 877 | /** |
17f443df | 878 | * @inheritDoc |
2b617cb0 | 879 | */ |
00be9182 | 880 | public function getUniqueIdentifierFromUserObject($user) { |
2b617cb0 EM |
881 | return empty($user->user_email) ? NULL : $user->user_email; |
882 | } | |
883 | ||
6a488035 | 884 | /** |
17f443df | 885 | * @inheritDoc |
6a488035 TO |
886 | */ |
887 | public function getLoginURL($destination = '') { | |
888 | $config = CRM_Core_Config::singleton(); | |
153155d3 | 889 | $loginURL = wp_login_url(); |
6a488035 TO |
890 | return $loginURL; |
891 | } | |
892 | ||
bb3a214a | 893 | /** |
ad37ac8e | 894 | * FIXME: Do something. |
895 | * | |
896 | * @param \CRM_Core_Form $form | |
897 | * | |
898 | * @return NULL|string | |
bb3a214a | 899 | */ |
6a488035 | 900 | public function getLoginDestination(&$form) { |
408b79bf | 901 | return NULL; |
6a488035 TO |
902 | } |
903 | ||
904 | /** | |
17f443df | 905 | * @inheritDoc |
6a488035 | 906 | */ |
00be9182 | 907 | public function getVersion() { |
6a488035 TO |
908 | if (function_exists('get_bloginfo')) { |
909 | return get_bloginfo('version', 'display'); | |
910 | } | |
911 | else { | |
912 | return 'Unknown'; | |
913 | } | |
914 | } | |
6491539b DL |
915 | |
916 | /** | |
17f443df | 917 | * @inheritDoc |
6491539b | 918 | */ |
00be9182 | 919 | public function getTimeZoneString() { |
6491539b DL |
920 | return get_option('timezone_string'); |
921 | } | |
59f97da6 EM |
922 | |
923 | /** | |
17f443df | 924 | * @inheritDoc |
59f97da6 | 925 | */ |
00be9182 | 926 | public function getUserRecordUrl($contactID) { |
59f97da6 | 927 | $uid = CRM_Core_BAO_UFMatch::getUFId($contactID); |
353ffa53 | 928 | if (CRM_Core_Session::singleton() |
6714d8d2 | 929 | ->get('userID') == $contactID || CRM_Core_Permission::checkAnyPerm(['cms:administer users']) |
353ffa53 | 930 | ) { |
59f97da6 EM |
931 | return CRM_Core_Config::singleton()->userFrameworkBaseURL . "wp-admin/user-edit.php?user_id=" . $uid; |
932 | } | |
933 | } | |
96025800 | 934 | |
469d8dab CW |
935 | /** |
936 | * Append WP js to coreResourcesList. | |
ad37ac8e | 937 | * |
303017a1 | 938 | * @param \Civi\Core\Event\GenericHookEvent $e |
469d8dab | 939 | */ |
303017a1 CW |
940 | public function appendCoreResources(\Civi\Core\Event\GenericHookEvent $e) { |
941 | $e->list[] = 'js/crm.wordpress.js'; | |
469d8dab CW |
942 | } |
943 | ||
03d5592a CW |
944 | /** |
945 | * @inheritDoc | |
62c20d1e CW |
946 | */ |
947 | public function alterAssetUrl(\Civi\Core\Event\GenericHookEvent $e) { | |
948 | // Set menubar breakpoint to match WP admin theme | |
949 | if ($e->asset == 'crm-menubar.css') { | |
950 | $e->params['breakpoint'] = 783; | |
951 | } | |
952 | } | |
953 | ||
954 | /** | |
955 | * @inheritDoc | |
03d5592a CW |
956 | */ |
957 | public function synchronizeUsers() { | |
958 | $config = CRM_Core_Config::singleton(); | |
959 | if (PHP_SAPI != 'cli') { | |
960 | set_time_limit(300); | |
961 | } | |
962 | $id = 'ID'; | |
963 | $mail = 'user_email'; | |
964 | ||
965 | $uf = $config->userFramework; | |
966 | $contactCount = 0; | |
967 | $contactCreated = 0; | |
968 | $contactMatching = 0; | |
969 | ||
5b4ee130 CW |
970 | // Previously used the $wpdb global - which means WordPress *must* be bootstrapped. |
971 | $wpUsers = get_users(array( | |
972 | 'blog_id' => get_current_blog_id(), | |
973 | 'number' => -1, | |
974 | )); | |
03d5592a | 975 | |
5b4ee130 | 976 | foreach ($wpUsers as $wpUserData) { |
03d5592a CW |
977 | $contactCount++; |
978 | if ($match = CRM_Core_BAO_UFMatch::synchronizeUFMatch($wpUserData, | |
979 | $wpUserData->$id, | |
980 | $wpUserData->$mail, | |
981 | $uf, | |
982 | 1, | |
983 | 'Individual', | |
984 | TRUE | |
985 | ) | |
986 | ) { | |
987 | $contactCreated++; | |
988 | } | |
989 | else { | |
990 | $contactMatching++; | |
991 | } | |
5b4ee130 CW |
992 | if (is_object($match)) { |
993 | $match->free(); | |
994 | } | |
03d5592a CW |
995 | } |
996 | ||
be2fb01f | 997 | return [ |
03d5592a CW |
998 | 'contactCount' => $contactCount, |
999 | 'contactMatching' => $contactMatching, | |
1000 | 'contactCreated' => $contactCreated, | |
be2fb01f | 1001 | ]; |
03d5592a CW |
1002 | } |
1003 | ||
79dd7fe9 | 1004 | /** |
46dddc5c SL |
1005 | * Send an HTTP Response base on PSR HTTP RespnseInterface response. |
1006 | * | |
1007 | * @param \Psr\Http\Message\ResponseInterface $response | |
79dd7fe9 | 1008 | */ |
46dddc5c SL |
1009 | public function sendResponse(\Psr\Http\Message\ResponseInterface $response) { |
1010 | // use WordPress function status_header to ensure 404 response is sent | |
1011 | status_header($response->getStatusCode()); | |
90f6c8df SL |
1012 | foreach ($response->getHeaders() as $name => $values) { |
1013 | CRM_Utils_System::setHttpHeader($name, implode(', ', (array) $values)); | |
79dd7fe9 | 1014 | } |
46dddc5c SL |
1015 | echo $response->getBody(); |
1016 | CRM_Utils_System::civiExit(); | |
79dd7fe9 SL |
1017 | } |
1018 | ||
6a488035 | 1019 | } |