Merge pull request #11799 from eileenmcnaughton/enotice
[civicrm-core.git] / CRM / Utils / System / WordPress.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
8c9251b3 6 | Copyright CiviCRM LLC (c) 2004-2018 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
8c9251b3 31 * @copyright CiviCRM LLC (c) 2004-2018
6a488035
TO
32 * $Id$
33 *
34 */
35
36/**
37 * WordPress specific stuff goes here
38 */
39class CRM_Utils_System_WordPress extends CRM_Utils_System_Base {
bb3a214a 40 /**
bb3a214a 41 */
00be9182 42 public function __construct() {
4caaa696
EM
43 /**
44 * 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
45 * functions and leave the codebase oblivious to the type of CMS
46 * @deprecated
47 * @var bool
48 */
6a488035 49 $this->is_drupal = FALSE;
fe17e8d1 50 $this->is_wordpress = TRUE;
6a488035
TO
51 }
52
53 /**
17f443df 54 * @inheritDoc
6a488035 55 */
00be9182 56 public function setTitle($title, $pageTitle = NULL) {
6a488035
TO
57 if (!$pageTitle) {
58 $pageTitle = $title;
59 }
1beae3b1 60
17f443df
CW
61 // FIXME: Why is this global?
62 global $civicrm_wp_title;
63 $civicrm_wp_title = $title;
1beae3b1 64
17f443df
CW
65 // yes, set page title, depending on context
66 $context = civi_wp()->civicrm_context_get();
67 switch ($context) {
68 case 'admin':
69 case 'shortcode':
70 $template = CRM_Core_Smarty::singleton();
71 $template->assign('pageTitle', $pageTitle);
6a488035
TO
72 }
73 }
74
7ba2c8ad
KC
75 /**
76 * Moved from CRM_Utils_System_Base
77 */
78 public function getDefaultFileStorage() {
8ce9d9d6
TO
79 $config = CRM_Core_Config::singleton();
80 $cmsUrl = CRM_Utils_System::languageNegotiationURL($config->userFrameworkBaseURL, FALSE, TRUE);
81 $cmsPath = $this->cmsRootPath();
82 $filesPath = CRM_Utils_File::baseFilePath();
83 $filesRelPath = CRM_Utils_File::relativize($filesPath, $cmsPath);
84 $filesURL = rtrim($cmsUrl, '/') . '/' . ltrim($filesRelPath, ' /');
85 return array(
86 'url' => CRM_Utils_File::addTrailingSlash($filesURL, '/'),
87 'path' => CRM_Utils_File::addTrailingSlash($filesPath),
88 );
89 }
90
91 /**
92 * Determine the location of the CiviCRM source tree.
93 *
94 * @return array
95 * - url: string. ex: "http://example.com/sites/all/modules/civicrm"
96 * - path: string. ex: "/var/www/sites/all/modules/civicrm"
97 */
98 public function getCiviSourceStorage() {
7ba2c8ad 99 global $civicrm_root;
7ba2c8ad 100
8ce9d9d6
TO
101 // Don't use $config->userFrameworkBaseURL; it has garbage on it.
102 // More generally, we shouldn't be using $config here.
103 if (!defined('CIVICRM_UF_BASEURL')) {
104 throw new RuntimeException('Undefined constant: CIVICRM_UF_BASEURL');
7ba2c8ad 105 }
8ce9d9d6
TO
106
107 $cmsPath = $this->cmsRootPath();
108
109 // $config = CRM_Core_Config::singleton();
110 // overkill? // $cmsUrl = CRM_Utils_System::languageNegotiationURL($config->userFrameworkBaseURL, FALSE, TRUE);
111 $cmsUrl = CIVICRM_UF_BASEURL;
112 if (CRM_Utils_System::isSSL()) {
113 $cmsUrl = str_replace('http://', 'https://', $cmsUrl);
7ba2c8ad 114 }
d8182404 115 $civiRelPath = CRM_Utils_File::relativize(realpath($civicrm_root), realpath($cmsPath));
8ce9d9d6 116 $civiUrl = rtrim($cmsUrl, '/') . '/' . ltrim($civiRelPath, ' /');
7ba2c8ad 117 return array(
8ce9d9d6
TO
118 'url' => CRM_Utils_File::addTrailingSlash($civiUrl, '/'),
119 'path' => CRM_Utils_File::addTrailingSlash($civicrm_root),
7ba2c8ad
KC
120 );
121 }
122
6a488035 123 /**
17f443df 124 * @inheritDoc
6a488035 125 */
00be9182 126 public function appendBreadCrumb($breadCrumbs) {
6a488035
TO
127 $breadCrumb = wp_get_breadcrumb();
128
129 if (is_array($breadCrumbs)) {
130 foreach ($breadCrumbs as $crumbs) {
131 if (stripos($crumbs['url'], 'id%%')) {
132 $args = array('cid', 'mid');
133 foreach ($args as $a) {
134 $val = CRM_Utils_Request::retrieve($a, 'Positive', CRM_Core_DAO::$_nullObject,
135 FALSE, NULL, $_GET
136 );
137 if ($val) {
138 $crumbs['url'] = str_ireplace("%%{$a}%%", $val, $crumbs['url']);
139 }
140 }
141 }
142 $breadCrumb[] = "<a href=\"{$crumbs['url']}\">{$crumbs['title']}</a>";
143 }
144 }
145
146 $template = CRM_Core_Smarty::singleton();
147 $template->assign_by_ref('breadcrumb', $breadCrumb);
148 wp_set_breadcrumb($breadCrumb);
149 }
150
151 /**
17f443df 152 * @inheritDoc
6a488035 153 */
00be9182 154 public function resetBreadCrumb() {
6a488035
TO
155 $bc = array();
156 wp_set_breadcrumb($bc);
157 }
158
159 /**
17f443df 160 * @inheritDoc
6a488035 161 */
00be9182 162 public function addHTMLHead($head) {
6a488035
TO
163 static $registered = FALSE;
164 if (!$registered) {
165 // front-end view
166 add_action('wp_head', array(__CLASS__, '_showHTMLHead'));
167 // back-end views
168 add_action('admin_head', array(__CLASS__, '_showHTMLHead'));
169 }
170 CRM_Core_Region::instance('wp_head')->add(array(
171 'markup' => $head,
172 ));
173 }
174
17f443df 175 /**
fe482240 176 * WP action callback.
17f443df 177 */
00be9182 178 public static function _showHTMLHead() {
6a488035
TO
179 $region = CRM_Core_Region::instance('wp_head', FALSE);
180 if ($region) {
181 echo $region->render('');
182 }
183 }
184
185 /**
17f443df 186 * @inheritDoc
6a488035 187 */
00be9182 188 public function mapConfigToSSL() {
6a488035
TO
189 global $base_url;
190 $base_url = str_replace('http://', 'https://', $base_url);
191 }
192
193 /**
17f443df 194 * @inheritDoc
6a488035 195 */
408b79bf 196 public function url(
6a488035
TO
197 $path = NULL,
198 $query = NULL,
199 $absolute = FALSE,
200 $fragment = NULL,
6a488035
TO
201 $frontend = FALSE,
202 $forceBackend = FALSE
203 ) {
353ffa53
TO
204 $config = CRM_Core_Config::singleton();
205 $script = '';
c80e2dbf 206 $separator = '&';
353ffa53 207 $wpPageParam = '';
887f5d81 208 $fragment = isset($fragment) ? ('#' . $fragment) : '';
6a488035
TO
209
210 $path = CRM_Utils_String::stripPathChars($path);
211
212 //this means wp function we are trying to use is not available,
213 //so load bootStrap
d8182404 214 // FIXME: Why bootstrap in url()? Generally want to define 1-2 strategic places to put bootstrap
6a488035 215 if (!function_exists('get_option')) {
d8182404 216 $this->loadBootStrap();
6a488035 217 }
6a488035 218 if ($config->userFrameworkFrontend) {
887f5d81 219 if (get_option('permalink_structure') != '') {
6a488035
TO
220 global $post;
221 $script = get_permalink($post->ID);
222 }
223
01aca362 224 // when shortcode is included in page
6a488035
TO
225 // also make sure we have valid query object
226 global $wp_query;
481a74f4 227 if (method_exists($wp_query, 'get')) {
6a488035 228 if (get_query_var('page_id')) {
887f5d81 229 $wpPageParam = "page_id=" . get_query_var('page_id');
6a488035
TO
230 }
231 elseif (get_query_var('p')) {
232 // when shortcode is inserted in post
887f5d81 233 $wpPageParam = "p=" . get_query_var('p');
6a488035
TO
234 }
235 }
236 }
237
887f5d81
TO
238 $base = $this->getBaseUrl($absolute, $frontend, $forceBackend);
239
240 if (!isset($path) && !isset($query)) {
241 // FIXME: This short-circuited codepath is the same as the general one below, except
242 // in that it ignores "permlink_structure" / $wpPageParam / $script . I don't know
243 // why it's different (and I can only find two obvious use-cases for this codepath,
244 // of which at least one looks gratuitous). A more ambitious person would simply remove
245 // this code.
246 return $base . $fragment;
247 }
248
249 if (!$forceBackend && get_option('permalink_structure') != '' && ($wpPageParam || $script != '')) {
250 $base = $script;
6a488035
TO
251 }
252
887f5d81
TO
253 $queryParts = array();
254 if (isset($path)) {
255 $queryParts[] = 'page=CiviCRM';
256 $queryParts[] = "q={$path}";
6a488035 257 }
887f5d81
TO
258 if ($wpPageParam) {
259 $queryParts[] = $wpPageParam;
260 }
261 if (isset($query)) {
262 $queryParts[] = $query;
6a488035
TO
263 }
264
887f5d81
TO
265 return $base . '?' . implode($separator, $queryParts) . $fragment;
266 }
267
bb3a214a 268 /**
f553d1ea
KC
269 * 27-09-2016
270 * CRM-16421 CRM-17633 WIP Changes to support WP in it's own directory
271 * https://wiki.civicrm.org/confluence/display/CRM/WordPress+installed+in+its+own+directory+issues
272 * For now leave hard coded wp-admin references.
273 * TODO: remove wp-admin references and replace with admin_url() in the future. Look at best way to get path to admin_url
274 *
bb3a214a
EM
275 * @param $absolute
276 * @param $frontend
277 * @param $forceBackend
278 *
279 * @return mixed|null|string
280 */
887f5d81 281 private function getBaseUrl($absolute, $frontend, $forceBackend) {
353ffa53 282 $config = CRM_Core_Config::singleton();
6a488035 283 if ((is_admin() && !$frontend) || $forceBackend) {
f553d1ea 284 return Civi::paths()->getUrl('[wp.backend]/.', $absolute ? 'absolute' : 'relative');
6a488035 285 }
f553d1ea
KC
286 else {
287 return Civi::paths()->getUrl('[wp.frontend]/.', $absolute ? 'absolute' : 'relative');
36b820ae 288 }
01aca362 289 }
6a488035
TO
290
291 /**
17f443df 292 * @inheritDoc
6a488035 293 */
00be9182 294 public function authenticate($name, $password, $loadCMSBootstrap = FALSE, $realPath = NULL) {
6a488035
TO
295 $config = CRM_Core_Config::singleton();
296
297 if ($loadCMSBootstrap) {
b6c54d16 298 $config->userSystem->loadBootStrap($name, $password);
6a488035
TO
299 }
300
301 $user = wp_authenticate($name, $password);
302 if (is_a($user, 'WP_Error')) {
303 return FALSE;
304 }
305
17f443df 306 // TODO: need to change this to make sure we matched only one row
6a488035
TO
307
308 CRM_Core_BAO_UFMatch::synchronizeUFMatch($user->data, $user->data->ID, $user->data->user_email, 'WordPress');
309 $contactID = CRM_Core_BAO_UFMatch::getContactId($user->data->ID);
310 if (!$contactID) {
311 return FALSE;
312 }
313 return array($contactID, $user->data->ID, mt_rand());
314 }
315
316 /**
17f443df 317 * FIXME: Do something
ea3ddccf 318 *
319 * @param string $message
6a488035 320 */
00be9182 321 public function setMessage($message) {
6a488035
TO
322 }
323
bb3a214a 324 /**
b596c3e9 325 * @param \string $user
ea3ddccf 326 *
327 * @return bool
bb3a214a 328 */
e7292422 329 public function loadUser($user) {
b596c3e9 330 $userdata = get_user_by('login', $user);
331 if (!$userdata->data->ID) {
7ca9cd52 332 return FALSE;
b596c3e9 333 }
334
335 $uid = $userdata->data->ID;
336 wp_set_current_user($uid);
337 $contactID = CRM_Core_BAO_UFMatch::getContactId($uid);
338
339 // lets store contact id and user id in session
340 $session = CRM_Core_Session::singleton();
341 $session->set('ufID', $uid);
342 $session->set('userID', $contactID);
e7292422 343 return TRUE;
6a488035
TO
344 }
345
17f443df
CW
346 /**
347 * FIXME: Use CMS-native approach
348 */
00be9182 349 public function permissionDenied() {
0499b0ad 350 CRM_Core_Error::fatal(ts('You do not have permission to access this page.'));
6a488035
TO
351 }
352
8ee9bea9
SL
353 /**
354 * Determine the native ID of the CMS user.
355 *
356 * @param string $username
357 * @return int|NULL
358 */
359 public function getUfId($username) {
360 $userdata = get_user_by('login', $username);
361 if (!$userdata->data->ID) {
362 return NULL;
363 }
364 return $userdata->data->ID;
365 }
366
17f443df
CW
367 /**
368 * @inheritDoc
369 */
00be9182 370 public function logout() {
6a488035
TO
371 // destroy session
372 if (session_id()) {
373 session_destroy();
374 }
375 wp_logout();
376 wp_redirect(wp_login_url());
377 }
378
6a488035 379 /**
17f443df 380 * @inheritDoc
6a488035 381 */
00be9182 382 public function getUFLocale() {
cba2601a
SV
383 // Polylang plugin
384 if (function_exists('pll_current_language')) {
385 $language = pll_current_language();
386 }
19780d2b 387 // WPML plugin
cba2601a 388 elseif (defined('ICL_LANGUAGE_CODE')) {
19780d2b
DL
389 $language = ICL_LANGUAGE_CODE;
390 }
391
392 // TODO: set language variable for others WordPress plugin
393
5a44748c 394 if (!empty($language)) {
19780d2b 395 return CRM_Core_I18n_PseudoConstant::longForShort(substr($language, 0, 2));
0db6c3e1
TO
396 }
397 else {
19780d2b
DL
398 return NULL;
399 }
6a488035
TO
400 }
401
fd1f3a26
SV
402 /**
403 * @inheritDoc
404 */
405 public function setUFLocale($civicrm_language) {
406 // TODO (probably not possible with WPML?)
407 return TRUE;
408 }
409
6a488035 410 /**
fe482240 411 * Load wordpress bootstrap.
6a488035 412 *
5a4f6742
CW
413 * @param string $name
414 * optional username for login.
415 * @param string $pass
416 * optional password for login.
f4aaa82a
EM
417 *
418 * @return bool
6a488035 419 */
00be9182 420 public function loadBootStrap($name = NULL, $pass = NULL) {
05fcde76 421 global $wp, $wp_rewrite, $wp_the_query, $wp_query, $wpdb, $current_site, $current_blog, $current_user;
6a488035 422
7a44e49f 423 if (!defined('WP_USE_THEMES')) {
c5f77355 424 define('WP_USE_THEMES', FALSE);
7a44e49f 425 }
6a488035
TO
426
427 $cmsRootPath = $this->cmsRootPath();
428 if (!$cmsRootPath) {
429 CRM_Core_Error::fatal("Could not find the install directory for WordPress");
430 }
aaffa79f 431 $path = Civi::settings()->get('wpLoadPhp');
b299b1cc 432 if (!empty($path)) {
35da5d8d 433 require_once $path;
b299b1cc
KC
434 }
435 elseif (file_exists($cmsRootPath . DIRECTORY_SEPARATOR . 'wp-load.php')) {
35da5d8d 436 require_once $cmsRootPath . DIRECTORY_SEPARATOR . 'wp-load.php';
b299b1cc
KC
437 }
438 else {
439 CRM_Core_Error::fatal("Could not find the bootstrap file for WordPress");
35da5d8d 440 }
6491539b
DL
441 $wpUserTimezone = get_option('timezone_string');
442 if ($wpUserTimezone) {
443 date_default_timezone_set($wpUserTimezone);
444 CRM_Core_Config::singleton()->userSystem->setMySQLTimeZone();
445 }
e7292422 446 require_once $cmsRootPath . DIRECTORY_SEPARATOR . 'wp-includes/pluggable.php';
fe1e7958 447 $uid = CRM_Utils_Array::value('uid', $name);
17763922
WA
448 if (!$uid) {
449 $name = $name ? $name : trim(CRM_Utils_Array::value('name', $_REQUEST));
450 $pass = $pass ? $pass : trim(CRM_Utils_Array::value('pass', $_REQUEST));
451 if ($name) {
d8182404 452 $uid = wp_authenticate($name, $pass);
17763922
WA
453 if (!$uid) {
454 if ($throwError) {
455 echo '<br />Sorry, unrecognized username or password.';
456 exit();
457 }
458 return FALSE;
459 }
460 }
461 }
fe1e7958 462 if ($uid) {
a4111333
CW
463 if ($uid instanceof WP_User) {
464 $account = wp_set_current_user($uid->ID);
c5f77355
CW
465 }
466 else {
a4111333
CW
467 $account = wp_set_current_user($uid);
468 }
fe1e7958 469 if ($account && $account->data->ID) {
470 global $user;
471 $user = $account;
472 return TRUE;
473 }
474 }
e7292422 475 return TRUE;
6a488035
TO
476 }
477
bb3a214a
EM
478 /**
479 * @param $dir
480 *
481 * @return bool
482 */
00be9182 483 public function validInstallDir($dir) {
dfbcf0b7 484 $includePath = "$dir/wp-includes";
f81c7606 485 if (file_exists("$includePath/version.php")) {
dfbcf0b7
DL
486 return TRUE;
487 }
488 return FALSE;
489 }
490
bb3a214a
EM
491 /**
492 * Determine the location of the CMS root.
493 *
72b3a70c
CW
494 * @return string|NULL
495 * local file system path to CMS root, or NULL if it cannot be determined
bb3a214a 496 */
00be9182 497 public function cmsRootPath() {
a93a0366
TO
498 global $civicrm_paths;
499 if (!empty($civicrm_paths['cms.root']['path'])) {
500 return $civicrm_paths['cms.root']['path'];
501 }
502
6a488035 503 $cmsRoot = $valid = NULL;
dfbcf0b7
DL
504 if (defined('CIVICRM_CMSDIR')) {
505 if ($this->validInstallDir(CIVICRM_CMSDIR)) {
506 $cmsRoot = CIVICRM_CMSDIR;
507 $valid = TRUE;
508 }
6a488035 509 }
dfbcf0b7
DL
510 else {
511 $pathVars = explode('/', str_replace('\\', '/', $_SERVER['SCRIPT_FILENAME']));
6a488035 512
dfbcf0b7
DL
513 //might be windows installation.
514 $firstVar = array_shift($pathVars);
515 if ($firstVar) {
516 $cmsRoot = $firstVar;
517 }
518
519 //start w/ csm dir search.
520 foreach ($pathVars as $var) {
521 $cmsRoot .= "/$var";
522 if ($this->validInstallDir($cmsRoot)) {
523 //stop as we found bootstrap.
524 $valid = TRUE;
525 break;
526 }
6a488035
TO
527 }
528 }
529
530 return ($valid) ? $cmsRoot : NULL;
531 }
532
bb3a214a 533 /**
17f443df 534 * @inheritDoc
bb3a214a 535 */
00be9182 536 public function createUser(&$params, $mail) {
6a488035
TO
537 $user_data = array(
538 'ID' => '',
539 'user_pass' => $params['cms_pass'],
540 'user_login' => $params['cms_name'],
541 'user_email' => $params[$mail],
542 'nickname' => $params['cms_name'],
543 'role' => get_option('default_role'),
544 );
545 if (isset($params['contactID'])) {
546 $contactType = CRM_Contact_BAO_Contact::getContactType($params['contactID']);
547 if ($contactType == 'Individual') {
548 $user_data['first_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact',
549 $params['contactID'], 'first_name'
550 );
551 $user_data['last_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact',
552 $params['contactID'], 'last_name'
553 );
554 }
555 }
556
557 $uid = wp_insert_user($user_data);
558
559 $creds = array();
560 $creds['user_login'] = $params['cms_name'];
561 $creds['user_password'] = $params['cms_pass'];
562 $creds['remember'] = TRUE;
563 $user = wp_signon($creds, FALSE);
564
565 wp_new_user_notification($uid, $user_data['user_pass']);
566 return $uid;
567 }
568
f4aaa82a 569 /**
17f443df 570 * @inheritDoc
6a488035 571 */
00be9182 572 public function updateCMSName($ufID, $ufName) {
6a488035
TO
573 // CRM-10620
574 if (function_exists('wp_update_user')) {
353ffa53 575 $ufID = CRM_Utils_Type::escape($ufID, 'Integer');
6a488035
TO
576 $ufName = CRM_Utils_Type::escape($ufName, 'String');
577
e7292422 578 $values = array('ID' => $ufID, 'user_email' => $ufName);
481a74f4
TO
579 if ($ufID) {
580 wp_update_user($values);
6a488035
TO
581 }
582 }
583 }
584
bb3a214a 585 /**
c490a46a 586 * @param array $params
bb3a214a
EM
587 * @param $errors
588 * @param string $emailName
589 */
00be9182 590 public function checkUserNameEmailExists(&$params, &$errors, $emailName = 'email') {
6a488035
TO
591 $config = CRM_Core_Config::singleton();
592
353ffa53
TO
593 $dao = new CRM_Core_DAO();
594 $name = $dao->escape(CRM_Utils_Array::value('name', $params));
6a488035
TO
595 $email = $dao->escape(CRM_Utils_Array::value('mail', $params));
596
a7488080 597 if (!empty($params['name'])) {
6a488035
TO
598 if (!validate_username($params['name'])) {
599 $errors['cms_name'] = ts("Your username contains invalid characters");
600 }
601 elseif (username_exists(sanitize_user($params['name']))) {
602 $errors['cms_name'] = ts('The username %1 is already taken. Please select another username.', array(1 => $params['name']));
603 }
604 }
605
a7488080 606 if (!empty($params['mail'])) {
6a488035
TO
607 if (!is_email($params['mail'])) {
608 $errors[$emailName] = "Your email is invaid";
609 }
610 elseif (email_exists($params['mail'])) {
db18d815 611 $errors[$emailName] = ts('The email address %1 already has an account associated with it. <a href="%2">Have you forgotten your password?</a>',
153155d3 612 array(1 => $params['mail'], 2 => wp_lostpassword_url())
6a488035
TO
613 );
614 }
615 }
616 }
617
618 /**
17f443df 619 * @inheritDoc
6a488035
TO
620 */
621 public function isUserLoggedIn() {
622 $isloggedIn = FALSE;
623 if (function_exists('is_user_logged_in')) {
624 $isloggedIn = is_user_logged_in();
625 }
626
627 return $isloggedIn;
628 }
629
8caad0ce 630 /**
631 * @inheritDoc
632 */
633 public function isUserRegistrationPermitted() {
634 if (!get_option('users_can_register')) {
635 return FALSE;
636 }
637 return TRUE;
638 }
639
63df6889
HD
640 /**
641 * @inheritDoc
642 */
1a6630be 643 public function isPasswordUserGenerated() {
63df6889
HD
644 return TRUE;
645 }
646
bb3a214a
EM
647 /**
648 * @return mixed
649 */
00be9182 650 public function getLoggedInUserObject() {
2b617cb0 651 if (function_exists('is_user_logged_in') &&
353ffa53
TO
652 is_user_logged_in()
653 ) {
2b617cb0
EM
654 global $current_user;
655 }
656 return $current_user;
657 }
353ffa53 658
6a488035 659 /**
17f443df 660 * @inheritDoc
6a488035
TO
661 */
662 public function getLoggedInUfID() {
663 $ufID = NULL;
2b617cb0
EM
664 $current_user = $this->getLoggedInUserObject();
665 return isset($current_user->ID) ? $current_user->ID : NULL;
666 }
667
668 /**
17f443df 669 * @inheritDoc
2b617cb0 670 */
00be9182 671 public function getLoggedInUniqueIdentifier() {
2b617cb0
EM
672 $user = $this->getLoggedInUserObject();
673 return $this->getUniqueIdentifierFromUserObject($user);
6a488035
TO
674 }
675
32998c82
EM
676 /**
677 * Get User ID from UserFramework system (Joomla)
77855840
TO
678 * @param object $user
679 * Object as described by the CMS.
72b3a70c
CW
680 *
681 * @return int|null
32998c82 682 */
00be9182 683 public function getUserIDFromUserObject($user) {
32998c82
EM
684 return !empty($user->ID) ? $user->ID : NULL;
685 }
686
2b617cb0 687 /**
17f443df 688 * @inheritDoc
2b617cb0 689 */
00be9182 690 public function getUniqueIdentifierFromUserObject($user) {
2b617cb0
EM
691 return empty($user->user_email) ? NULL : $user->user_email;
692 }
693
6a488035 694 /**
17f443df 695 * @inheritDoc
6a488035
TO
696 */
697 public function getLoginURL($destination = '') {
698 $config = CRM_Core_Config::singleton();
153155d3 699 $loginURL = wp_login_url();
6a488035
TO
700 return $loginURL;
701 }
702
bb3a214a 703 /**
ad37ac8e 704 * FIXME: Do something.
705 *
706 * @param \CRM_Core_Form $form
707 *
708 * @return NULL|string
bb3a214a 709 */
6a488035 710 public function getLoginDestination(&$form) {
408b79bf 711 return NULL;
6a488035
TO
712 }
713
714 /**
17f443df 715 * @inheritDoc
6a488035 716 */
00be9182 717 public function getVersion() {
6a488035
TO
718 if (function_exists('get_bloginfo')) {
719 return get_bloginfo('version', 'display');
720 }
721 else {
722 return 'Unknown';
723 }
724 }
6491539b
DL
725
726 /**
17f443df 727 * @inheritDoc
6491539b 728 */
00be9182 729 public function getTimeZoneString() {
6491539b
DL
730 return get_option('timezone_string');
731 }
59f97da6
EM
732
733 /**
17f443df 734 * @inheritDoc
59f97da6 735 */
00be9182 736 public function getUserRecordUrl($contactID) {
59f97da6 737 $uid = CRM_Core_BAO_UFMatch::getUFId($contactID);
353ffa53
TO
738 if (CRM_Core_Session::singleton()
739 ->get('userID') == $contactID || CRM_Core_Permission::checkAnyPerm(array('cms:administer users'))
740 ) {
59f97da6
EM
741 return CRM_Core_Config::singleton()->userFrameworkBaseURL . "wp-admin/user-edit.php?user_id=" . $uid;
742 }
743 }
96025800 744
469d8dab
CW
745 /**
746 * Append WP js to coreResourcesList.
ad37ac8e 747 *
748 * @param array $list
469d8dab
CW
749 */
750 public function appendCoreResources(&$list) {
751 $list[] = 'js/crm.wordpress.js';
752 }
753
03d5592a
CW
754 /**
755 * @inheritDoc
756 */
757 public function synchronizeUsers() {
758 $config = CRM_Core_Config::singleton();
759 if (PHP_SAPI != 'cli') {
760 set_time_limit(300);
761 }
762 $id = 'ID';
763 $mail = 'user_email';
764
765 $uf = $config->userFramework;
766 $contactCount = 0;
767 $contactCreated = 0;
768 $contactMatching = 0;
769
770 global $wpdb;
771 $wpUserIds = $wpdb->get_col("SELECT $wpdb->users.ID FROM $wpdb->users");
772
773 foreach ($wpUserIds as $wpUserId) {
774 $wpUserData = get_userdata($wpUserId);
775 $contactCount++;
776 if ($match = CRM_Core_BAO_UFMatch::synchronizeUFMatch($wpUserData,
777 $wpUserData->$id,
778 $wpUserData->$mail,
779 $uf,
780 1,
781 'Individual',
782 TRUE
783 )
784 ) {
785 $contactCreated++;
786 }
787 else {
788 $contactMatching++;
789 }
790 if (is_object($match)) {
791 $match->free();
792 }
793 }
794
795 return array(
796 'contactCount' => $contactCount,
797 'contactMatching' => $contactMatching,
798 'contactCreated' => $contactCreated,
799 );
800 }
801
6a488035 802}