Merge pull request #10405 from agh1/crm-19612
[civicrm-core.git] / CRM / Utils / System / WordPress.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
0f03f337 6 | Copyright CiviCRM LLC (c) 2004-2017 |
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
0f03f337 31 * @copyright CiviCRM LLC (c) 2004-2017
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
EM
268 /**
269 * @param $absolute
270 * @param $frontend
271 * @param $forceBackend
272 *
273 * @return mixed|null|string
274 */
887f5d81 275 private function getBaseUrl($absolute, $frontend, $forceBackend) {
353ffa53 276 $config = CRM_Core_Config::singleton();
6a488035 277
6a488035
TO
278 $base = $absolute ? $config->userFrameworkBaseURL : $config->useFrameworkRelativeBase;
279
280 if ((is_admin() && !$frontend) || $forceBackend) {
281 $base .= 'wp-admin/admin.php';
887f5d81 282 return $base;
6a488035
TO
283 }
284 elseif (defined('CIVICRM_UF_WP_BASEPAGE')) {
285 $base .= CIVICRM_UF_WP_BASEPAGE;
887f5d81 286 return $base;
6a488035 287 }
36b820ae
DL
288 elseif (isset($config->wpBasePage)) {
289 $base .= $config->wpBasePage;
887f5d81 290 return $base;
36b820ae 291 }
887f5d81 292 return $base;
01aca362 293 }
6a488035
TO
294
295 /**
17f443df 296 * @inheritDoc
6a488035 297 */
00be9182 298 public function authenticate($name, $password, $loadCMSBootstrap = FALSE, $realPath = NULL) {
6a488035
TO
299 $config = CRM_Core_Config::singleton();
300
301 if ($loadCMSBootstrap) {
b6c54d16 302 $config->userSystem->loadBootStrap($name, $password);
6a488035
TO
303 }
304
305 $user = wp_authenticate($name, $password);
306 if (is_a($user, 'WP_Error')) {
307 return FALSE;
308 }
309
17f443df 310 // TODO: need to change this to make sure we matched only one row
6a488035
TO
311
312 CRM_Core_BAO_UFMatch::synchronizeUFMatch($user->data, $user->data->ID, $user->data->user_email, 'WordPress');
313 $contactID = CRM_Core_BAO_UFMatch::getContactId($user->data->ID);
314 if (!$contactID) {
315 return FALSE;
316 }
317 return array($contactID, $user->data->ID, mt_rand());
318 }
319
320 /**
17f443df 321 * FIXME: Do something
ea3ddccf 322 *
323 * @param string $message
6a488035 324 */
00be9182 325 public function setMessage($message) {
6a488035
TO
326 }
327
bb3a214a 328 /**
b596c3e9 329 * @param \string $user
ea3ddccf 330 *
331 * @return bool
bb3a214a 332 */
e7292422 333 public function loadUser($user) {
b596c3e9 334 $userdata = get_user_by('login', $user);
335 if (!$userdata->data->ID) {
7ca9cd52 336 return FALSE;
b596c3e9 337 }
338
339 $uid = $userdata->data->ID;
340 wp_set_current_user($uid);
341 $contactID = CRM_Core_BAO_UFMatch::getContactId($uid);
342
343 // lets store contact id and user id in session
344 $session = CRM_Core_Session::singleton();
345 $session->set('ufID', $uid);
346 $session->set('userID', $contactID);
e7292422 347 return TRUE;
6a488035
TO
348 }
349
17f443df
CW
350 /**
351 * FIXME: Use CMS-native approach
352 */
00be9182 353 public function permissionDenied() {
0499b0ad 354 CRM_Core_Error::fatal(ts('You do not have permission to access this page.'));
6a488035
TO
355 }
356
17f443df
CW
357 /**
358 * @inheritDoc
359 */
00be9182 360 public function logout() {
6a488035
TO
361 // destroy session
362 if (session_id()) {
363 session_destroy();
364 }
365 wp_logout();
366 wp_redirect(wp_login_url());
367 }
368
6a488035 369 /**
17f443df 370 * @inheritDoc
6a488035 371 */
00be9182 372 public function getUFLocale() {
19780d2b
DL
373 // WPML plugin
374 if (defined('ICL_LANGUAGE_CODE')) {
375 $language = ICL_LANGUAGE_CODE;
376 }
377
378 // TODO: set language variable for others WordPress plugin
379
380 if (isset($language)) {
381 return CRM_Core_I18n_PseudoConstant::longForShort(substr($language, 0, 2));
0db6c3e1
TO
382 }
383 else {
19780d2b
DL
384 return NULL;
385 }
6a488035
TO
386 }
387
fd1f3a26
SV
388 /**
389 * @inheritDoc
390 */
391 public function setUFLocale($civicrm_language) {
392 // TODO (probably not possible with WPML?)
393 return TRUE;
394 }
395
6a488035 396 /**
fe482240 397 * Load wordpress bootstrap.
6a488035 398 *
5a4f6742
CW
399 * @param string $name
400 * optional username for login.
401 * @param string $pass
402 * optional password for login.
f4aaa82a
EM
403 *
404 * @return bool
6a488035 405 */
00be9182 406 public function loadBootStrap($name = NULL, $pass = NULL) {
05fcde76 407 global $wp, $wp_rewrite, $wp_the_query, $wp_query, $wpdb, $current_site, $current_blog, $current_user;
6a488035 408
7a44e49f 409 if (!defined('WP_USE_THEMES')) {
c5f77355 410 define('WP_USE_THEMES', FALSE);
7a44e49f 411 }
6a488035
TO
412
413 $cmsRootPath = $this->cmsRootPath();
414 if (!$cmsRootPath) {
415 CRM_Core_Error::fatal("Could not find the install directory for WordPress");
416 }
aaffa79f 417 $path = Civi::settings()->get('wpLoadPhp');
b299b1cc 418 if (!empty($path)) {
35da5d8d 419 require_once $path;
b299b1cc
KC
420 }
421 elseif (file_exists($cmsRootPath . DIRECTORY_SEPARATOR . 'wp-load.php')) {
35da5d8d 422 require_once $cmsRootPath . DIRECTORY_SEPARATOR . 'wp-load.php';
b299b1cc
KC
423 }
424 else {
425 CRM_Core_Error::fatal("Could not find the bootstrap file for WordPress");
35da5d8d 426 }
6491539b
DL
427 $wpUserTimezone = get_option('timezone_string');
428 if ($wpUserTimezone) {
429 date_default_timezone_set($wpUserTimezone);
430 CRM_Core_Config::singleton()->userSystem->setMySQLTimeZone();
431 }
e7292422 432 require_once $cmsRootPath . DIRECTORY_SEPARATOR . 'wp-includes/pluggable.php';
fe1e7958 433 $uid = CRM_Utils_Array::value('uid', $name);
17763922
WA
434 if (!$uid) {
435 $name = $name ? $name : trim(CRM_Utils_Array::value('name', $_REQUEST));
436 $pass = $pass ? $pass : trim(CRM_Utils_Array::value('pass', $_REQUEST));
437 if ($name) {
d8182404 438 $uid = wp_authenticate($name, $pass);
17763922
WA
439 if (!$uid) {
440 if ($throwError) {
441 echo '<br />Sorry, unrecognized username or password.';
442 exit();
443 }
444 return FALSE;
445 }
446 }
447 }
fe1e7958 448 if ($uid) {
a4111333
CW
449 if ($uid instanceof WP_User) {
450 $account = wp_set_current_user($uid->ID);
c5f77355
CW
451 }
452 else {
a4111333
CW
453 $account = wp_set_current_user($uid);
454 }
fe1e7958 455 if ($account && $account->data->ID) {
456 global $user;
457 $user = $account;
458 return TRUE;
459 }
460 }
e7292422 461 return TRUE;
6a488035
TO
462 }
463
bb3a214a
EM
464 /**
465 * @param $dir
466 *
467 * @return bool
468 */
00be9182 469 public function validInstallDir($dir) {
dfbcf0b7 470 $includePath = "$dir/wp-includes";
f81c7606 471 if (file_exists("$includePath/version.php")) {
dfbcf0b7
DL
472 return TRUE;
473 }
474 return FALSE;
475 }
476
bb3a214a
EM
477 /**
478 * Determine the location of the CMS root.
479 *
72b3a70c
CW
480 * @return string|NULL
481 * local file system path to CMS root, or NULL if it cannot be determined
bb3a214a 482 */
00be9182 483 public function cmsRootPath() {
a93a0366
TO
484 global $civicrm_paths;
485 if (!empty($civicrm_paths['cms.root']['path'])) {
486 return $civicrm_paths['cms.root']['path'];
487 }
488
6a488035 489 $cmsRoot = $valid = NULL;
dfbcf0b7
DL
490 if (defined('CIVICRM_CMSDIR')) {
491 if ($this->validInstallDir(CIVICRM_CMSDIR)) {
492 $cmsRoot = CIVICRM_CMSDIR;
493 $valid = TRUE;
494 }
6a488035 495 }
dfbcf0b7
DL
496 else {
497 $pathVars = explode('/', str_replace('\\', '/', $_SERVER['SCRIPT_FILENAME']));
6a488035 498
dfbcf0b7
DL
499 //might be windows installation.
500 $firstVar = array_shift($pathVars);
501 if ($firstVar) {
502 $cmsRoot = $firstVar;
503 }
504
505 //start w/ csm dir search.
506 foreach ($pathVars as $var) {
507 $cmsRoot .= "/$var";
508 if ($this->validInstallDir($cmsRoot)) {
509 //stop as we found bootstrap.
510 $valid = TRUE;
511 break;
512 }
6a488035
TO
513 }
514 }
515
516 return ($valid) ? $cmsRoot : NULL;
517 }
518
bb3a214a 519 /**
17f443df 520 * @inheritDoc
bb3a214a 521 */
00be9182 522 public function createUser(&$params, $mail) {
6a488035
TO
523 $user_data = array(
524 'ID' => '',
525 'user_pass' => $params['cms_pass'],
526 'user_login' => $params['cms_name'],
527 'user_email' => $params[$mail],
528 'nickname' => $params['cms_name'],
529 'role' => get_option('default_role'),
530 );
531 if (isset($params['contactID'])) {
532 $contactType = CRM_Contact_BAO_Contact::getContactType($params['contactID']);
533 if ($contactType == 'Individual') {
534 $user_data['first_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact',
535 $params['contactID'], 'first_name'
536 );
537 $user_data['last_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact',
538 $params['contactID'], 'last_name'
539 );
540 }
541 }
542
543 $uid = wp_insert_user($user_data);
544
545 $creds = array();
546 $creds['user_login'] = $params['cms_name'];
547 $creds['user_password'] = $params['cms_pass'];
548 $creds['remember'] = TRUE;
549 $user = wp_signon($creds, FALSE);
550
551 wp_new_user_notification($uid, $user_data['user_pass']);
552 return $uid;
553 }
554
f4aaa82a 555 /**
17f443df 556 * @inheritDoc
6a488035 557 */
00be9182 558 public function updateCMSName($ufID, $ufName) {
6a488035
TO
559 // CRM-10620
560 if (function_exists('wp_update_user')) {
353ffa53 561 $ufID = CRM_Utils_Type::escape($ufID, 'Integer');
6a488035
TO
562 $ufName = CRM_Utils_Type::escape($ufName, 'String');
563
e7292422 564 $values = array('ID' => $ufID, 'user_email' => $ufName);
481a74f4
TO
565 if ($ufID) {
566 wp_update_user($values);
6a488035
TO
567 }
568 }
569 }
570
bb3a214a 571 /**
c490a46a 572 * @param array $params
bb3a214a
EM
573 * @param $errors
574 * @param string $emailName
575 */
00be9182 576 public function checkUserNameEmailExists(&$params, &$errors, $emailName = 'email') {
6a488035
TO
577 $config = CRM_Core_Config::singleton();
578
353ffa53
TO
579 $dao = new CRM_Core_DAO();
580 $name = $dao->escape(CRM_Utils_Array::value('name', $params));
6a488035
TO
581 $email = $dao->escape(CRM_Utils_Array::value('mail', $params));
582
a7488080 583 if (!empty($params['name'])) {
6a488035
TO
584 if (!validate_username($params['name'])) {
585 $errors['cms_name'] = ts("Your username contains invalid characters");
586 }
587 elseif (username_exists(sanitize_user($params['name']))) {
588 $errors['cms_name'] = ts('The username %1 is already taken. Please select another username.', array(1 => $params['name']));
589 }
590 }
591
a7488080 592 if (!empty($params['mail'])) {
6a488035
TO
593 if (!is_email($params['mail'])) {
594 $errors[$emailName] = "Your email is invaid";
595 }
596 elseif (email_exists($params['mail'])) {
db18d815 597 $errors[$emailName] = ts('The email address %1 already has an account associated with it. <a href="%2">Have you forgotten your password?</a>',
153155d3 598 array(1 => $params['mail'], 2 => wp_lostpassword_url())
6a488035
TO
599 );
600 }
601 }
602 }
603
604 /**
17f443df 605 * @inheritDoc
6a488035
TO
606 */
607 public function isUserLoggedIn() {
608 $isloggedIn = FALSE;
609 if (function_exists('is_user_logged_in')) {
610 $isloggedIn = is_user_logged_in();
611 }
612
613 return $isloggedIn;
614 }
615
bb3a214a
EM
616 /**
617 * @return mixed
618 */
00be9182 619 public function getLoggedInUserObject() {
2b617cb0 620 if (function_exists('is_user_logged_in') &&
353ffa53
TO
621 is_user_logged_in()
622 ) {
2b617cb0
EM
623 global $current_user;
624 }
625 return $current_user;
626 }
353ffa53 627
6a488035 628 /**
17f443df 629 * @inheritDoc
6a488035
TO
630 */
631 public function getLoggedInUfID() {
632 $ufID = NULL;
2b617cb0
EM
633 $current_user = $this->getLoggedInUserObject();
634 return isset($current_user->ID) ? $current_user->ID : NULL;
635 }
636
637 /**
17f443df 638 * @inheritDoc
2b617cb0 639 */
00be9182 640 public function getLoggedInUniqueIdentifier() {
2b617cb0
EM
641 $user = $this->getLoggedInUserObject();
642 return $this->getUniqueIdentifierFromUserObject($user);
6a488035
TO
643 }
644
32998c82
EM
645 /**
646 * Get User ID from UserFramework system (Joomla)
77855840
TO
647 * @param object $user
648 * Object as described by the CMS.
72b3a70c
CW
649 *
650 * @return int|null
32998c82 651 */
00be9182 652 public function getUserIDFromUserObject($user) {
32998c82
EM
653 return !empty($user->ID) ? $user->ID : NULL;
654 }
655
2b617cb0 656 /**
17f443df 657 * @inheritDoc
2b617cb0 658 */
00be9182 659 public function getUniqueIdentifierFromUserObject($user) {
2b617cb0
EM
660 return empty($user->user_email) ? NULL : $user->user_email;
661 }
662
6a488035 663 /**
17f443df 664 * @inheritDoc
6a488035
TO
665 */
666 public function getLoginURL($destination = '') {
667 $config = CRM_Core_Config::singleton();
153155d3 668 $loginURL = wp_login_url();
6a488035
TO
669 return $loginURL;
670 }
671
bb3a214a 672 /**
ad37ac8e 673 * FIXME: Do something.
674 *
675 * @param \CRM_Core_Form $form
676 *
677 * @return NULL|string
bb3a214a 678 */
6a488035 679 public function getLoginDestination(&$form) {
408b79bf 680 return NULL;
6a488035
TO
681 }
682
683 /**
17f443df 684 * @inheritDoc
6a488035 685 */
00be9182 686 public function getVersion() {
6a488035
TO
687 if (function_exists('get_bloginfo')) {
688 return get_bloginfo('version', 'display');
689 }
690 else {
691 return 'Unknown';
692 }
693 }
6491539b
DL
694
695 /**
17f443df 696 * @inheritDoc
6491539b 697 */
00be9182 698 public function getTimeZoneString() {
6491539b
DL
699 return get_option('timezone_string');
700 }
59f97da6
EM
701
702 /**
17f443df 703 * @inheritDoc
59f97da6 704 */
00be9182 705 public function getUserRecordUrl($contactID) {
59f97da6 706 $uid = CRM_Core_BAO_UFMatch::getUFId($contactID);
353ffa53
TO
707 if (CRM_Core_Session::singleton()
708 ->get('userID') == $contactID || CRM_Core_Permission::checkAnyPerm(array('cms:administer users'))
709 ) {
59f97da6
EM
710 return CRM_Core_Config::singleton()->userFrameworkBaseURL . "wp-admin/user-edit.php?user_id=" . $uid;
711 }
712 }
96025800 713
469d8dab
CW
714 /**
715 * Append WP js to coreResourcesList.
ad37ac8e 716 *
717 * @param array $list
469d8dab
CW
718 */
719 public function appendCoreResources(&$list) {
720 $list[] = 'js/crm.wordpress.js';
721 }
722
03d5592a
CW
723 /**
724 * @inheritDoc
725 */
726 public function synchronizeUsers() {
727 $config = CRM_Core_Config::singleton();
728 if (PHP_SAPI != 'cli') {
729 set_time_limit(300);
730 }
731 $id = 'ID';
732 $mail = 'user_email';
733
734 $uf = $config->userFramework;
735 $contactCount = 0;
736 $contactCreated = 0;
737 $contactMatching = 0;
738
739 global $wpdb;
740 $wpUserIds = $wpdb->get_col("SELECT $wpdb->users.ID FROM $wpdb->users");
741
742 foreach ($wpUserIds as $wpUserId) {
743 $wpUserData = get_userdata($wpUserId);
744 $contactCount++;
745 if ($match = CRM_Core_BAO_UFMatch::synchronizeUFMatch($wpUserData,
746 $wpUserData->$id,
747 $wpUserData->$mail,
748 $uf,
749 1,
750 'Individual',
751 TRUE
752 )
753 ) {
754 $contactCreated++;
755 }
756 else {
757 $contactMatching++;
758 }
759 if (is_object($match)) {
760 $match->free();
761 }
762 }
763
764 return array(
765 'contactCount' => $contactCount,
766 'contactMatching' => $contactMatching,
767 'contactCreated' => $contactCreated,
768 );
769 }
770
6a488035 771}