Merge pull request #4971 from kurund/batch-16
[civicrm-core.git] / CRM / Utils / System / WordPress.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 * @package CRM
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
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 /**
df7cbeeb 54 * Sets the title of the page
6a488035
TO
55 *
56 * @param string $title
f4aaa82a
EM
57 * @param null $pageTitle
58 *
6a488035 59 * @return void
6a488035 60 */
00be9182 61 public function setTitle($title, $pageTitle = NULL) {
6a488035
TO
62 if (!$pageTitle) {
63 $pageTitle = $title;
64 }
1beae3b1 65
0734e0e8
CW
66 // get civi-wordpress instance
67 $civi = civi_wp();
1beae3b1 68
0734e0e8
CW
69 // do we have functionality provided by plugin version 4.6+ present?
70 if (method_exists($civi, 'civicrm_context_get')) {
1beae3b1 71
0734e0e8
CW
72 global $civicrm_wp_title;
73 $civicrm_wp_title = $pageTitle;
1beae3b1 74
0734e0e8
CW
75 // yes, set page title, depending on context
76 $context = civi_wp()->civicrm_context_get();
481a74f4 77 switch ($context) {
0734e0e8
CW
78 case 'admin':
79 case 'shortcode':
80 $template = CRM_Core_Smarty::singleton();
81 $template->assign('pageTitle', $pageTitle);
82 }
1beae3b1 83
0db6c3e1
TO
84 }
85 elseif (civicrm_wp_in_civicrm()) {
1beae3b1 86
0734e0e8 87 // legacy pre-4.6 behaviour
6a488035
TO
88 global $civicrm_wp_title;
89 $civicrm_wp_title = $pageTitle;
90 $template = CRM_Core_Smarty::singleton();
91 $template->assign('pageTitle', $pageTitle);
1beae3b1 92
6a488035
TO
93 }
94 }
95
96 /**
97 * Append an additional breadcrumb tag to the existing breadcrumb
98 *
f4aaa82a
EM
99 * @param $breadCrumbs
100 *
101 * @internal param string $title
102 * @internal param string $url
6a488035
TO
103 *
104 * @return void
6a488035 105 */
00be9182 106 public function appendBreadCrumb($breadCrumbs) {
6a488035
TO
107 $breadCrumb = wp_get_breadcrumb();
108
109 if (is_array($breadCrumbs)) {
110 foreach ($breadCrumbs as $crumbs) {
111 if (stripos($crumbs['url'], 'id%%')) {
112 $args = array('cid', 'mid');
113 foreach ($args as $a) {
114 $val = CRM_Utils_Request::retrieve($a, 'Positive', CRM_Core_DAO::$_nullObject,
115 FALSE, NULL, $_GET
116 );
117 if ($val) {
118 $crumbs['url'] = str_ireplace("%%{$a}%%", $val, $crumbs['url']);
119 }
120 }
121 }
122 $breadCrumb[] = "<a href=\"{$crumbs['url']}\">{$crumbs['title']}</a>";
123 }
124 }
125
126 $template = CRM_Core_Smarty::singleton();
127 $template->assign_by_ref('breadcrumb', $breadCrumb);
128 wp_set_breadcrumb($breadCrumb);
129 }
130
131 /**
132 * Reset an additional breadcrumb tag to the existing breadcrumb
133 *
134 * @return void
6a488035 135 */
00be9182 136 public function resetBreadCrumb() {
6a488035
TO
137 $bc = array();
138 wp_set_breadcrumb($bc);
139 }
140
141 /**
142 * Append a string to the head of the html file
143 *
77855840
TO
144 * @param string $head
145 * The new string to be appended.
6a488035
TO
146 *
147 * @return void
6a488035 148 */
00be9182 149 public function addHTMLHead($head) {
6a488035
TO
150 static $registered = FALSE;
151 if (!$registered) {
152 // front-end view
153 add_action('wp_head', array(__CLASS__, '_showHTMLHead'));
154 // back-end views
155 add_action('admin_head', array(__CLASS__, '_showHTMLHead'));
156 }
157 CRM_Core_Region::instance('wp_head')->add(array(
158 'markup' => $head,
159 ));
160 }
161
00be9182 162 public static function _showHTMLHead() {
6a488035
TO
163 $region = CRM_Core_Region::instance('wp_head', FALSE);
164 if ($region) {
165 echo $region->render('');
166 }
167 }
168
169 /**
170 * Add a script file
171 *
353ffa53 172 * @param $url : string, absolute path to file
5a4f6742
CW
173 * @param string $region
174 * location within the document: 'html-header', 'page-header', 'page-footer'.
6a488035
TO
175 *
176 * Note: This function is not to be called directly
177 * @see CRM_Core_Region::render()
178 *
a6c01b45
CW
179 * @return bool
180 * TRUE if we support this operation in this CMS, FALSE otherwise
6a488035
TO
181 */
182 public function addScriptUrl($url, $region) {
183 return FALSE;
184 }
185
186 /**
187 * Add an inline script
188 *
353ffa53 189 * @param $code : string, javascript code
5a4f6742
CW
190 * @param string $region
191 * location within the document: 'html-header', 'page-header', 'page-footer'.
6a488035
TO
192 *
193 * Note: This function is not to be called directly
194 * @see CRM_Core_Region::render()
195 *
a6c01b45
CW
196 * @return bool
197 * TRUE if we support this operation in this CMS, FALSE otherwise
6a488035
TO
198 */
199 public function addScript($code, $region) {
200 return FALSE;
201 }
202
203 /**
204 * Add a css file
205 *
353ffa53 206 * @param $url : string, absolute path to file
5a4f6742
CW
207 * @param string $region
208 * location within the document: 'html-header', 'page-header', 'page-footer'.
6a488035
TO
209 *
210 * Note: This function is not to be called directly
211 * @see CRM_Core_Region::render()
212 *
a6c01b45
CW
213 * @return bool
214 * TRUE if we support this operation in this CMS, FALSE otherwise
6a488035
TO
215 */
216 public function addStyleUrl($url, $region) {
217 return FALSE;
218 }
219
220 /**
221 * Add an inline style
222 *
353ffa53 223 * @param $code : string, css code
5a4f6742
CW
224 * @param string $region
225 * location within the document: 'html-header', 'page-header', 'page-footer'.
6a488035
TO
226 *
227 * Note: This function is not to be called directly
228 * @see CRM_Core_Region::render()
229 *
a6c01b45
CW
230 * @return bool
231 * TRUE if we support this operation in this CMS, FALSE otherwise
6a488035
TO
232 */
233 public function addStyle($code, $region) {
234 return FALSE;
235 }
236
237 /**
100fef9d 238 * Rewrite various system urls to https
6a488035 239 *
6a488035 240 * @return void
6a488035 241 */
00be9182 242 public function mapConfigToSSL() {
6a488035
TO
243 global $base_url;
244 $base_url = str_replace('http://', 'https://', $base_url);
245 }
246
247 /**
100fef9d 248 * Figure out the post url for the form
6a488035 249 *
77855840
TO
250 * @param mix $action
251 * The default action if one is pre-specified.
6a488035 252 *
a6c01b45
CW
253 * @return string
254 * the url to post the form
6a488035 255 */
00be9182 256 public function postURL($action) {
6a488035
TO
257 if (!empty($action)) {
258 return $action;
259 }
260
261 return $this->url($_GET['q'], NULL, TRUE, NULL, FALSE);
262 }
263
264 /**
265 * Generate an internal CiviCRM URL (copied from DRUPAL/includes/common.inc#url)
266 *
5a4f6742
CW
267 * @param string $path
268 * The path being linked to, such as "civicrm/add".
269 * @param string $query
270 * A query string to append to the link.
271 * @param bool $absolute
272 * Whether to force the output to be an absolute link (beginning with http:).
6a488035
TO
273 * Useful for links that will be displayed outside the site, such as in an
274 * RSS feed.
5a4f6742
CW
275 * @param string $fragment
276 * A fragment identifier (named anchor) to append to the link.
277 * @param bool $htmlize
278 * whether to convert to html eqivalant.
279 * @param bool $frontend
280 * a gross joomla hack.
6a488035 281 *
f4aaa82a
EM
282 * @param bool $forceBackend
283 *
a6c01b45
CW
284 * @return string
285 * an HTML string containing a link to the given path.
6a488035 286 */
408b79bf 287 public function url(
6a488035
TO
288 $path = NULL,
289 $query = NULL,
290 $absolute = FALSE,
291 $fragment = NULL,
292 $htmlize = TRUE,
293 $frontend = FALSE,
294 $forceBackend = FALSE
295 ) {
353ffa53
TO
296 $config = CRM_Core_Config::singleton();
297 $script = '';
6a488035 298 $separator = $htmlize ? '&amp;' : '&';
353ffa53 299 $wpPageParam = '';
887f5d81 300 $fragment = isset($fragment) ? ('#' . $fragment) : '';
6a488035
TO
301
302 $path = CRM_Utils_String::stripPathChars($path);
303
304 //this means wp function we are trying to use is not available,
305 //so load bootStrap
306 if (!function_exists('get_option')) {
887f5d81 307 $this->loadBootStrap(); // FIXME: Why bootstrap in url()? Generally want to define 1-2 strategic places to put bootstrap
6a488035 308 }
6a488035 309 if ($config->userFrameworkFrontend) {
887f5d81 310 if (get_option('permalink_structure') != '') {
6a488035
TO
311 global $post;
312 $script = get_permalink($post->ID);
313 }
314
01aca362 315 // when shortcode is included in page
6a488035
TO
316 // also make sure we have valid query object
317 global $wp_query;
481a74f4 318 if (method_exists($wp_query, 'get')) {
6a488035 319 if (get_query_var('page_id')) {
887f5d81 320 $wpPageParam = "page_id=" . get_query_var('page_id');
6a488035
TO
321 }
322 elseif (get_query_var('p')) {
323 // when shortcode is inserted in post
887f5d81 324 $wpPageParam = "p=" . get_query_var('p');
6a488035
TO
325 }
326 }
327 }
328
887f5d81
TO
329 $base = $this->getBaseUrl($absolute, $frontend, $forceBackend);
330
331 if (!isset($path) && !isset($query)) {
332 // FIXME: This short-circuited codepath is the same as the general one below, except
333 // in that it ignores "permlink_structure" / $wpPageParam / $script . I don't know
334 // why it's different (and I can only find two obvious use-cases for this codepath,
335 // of which at least one looks gratuitous). A more ambitious person would simply remove
336 // this code.
337 return $base . $fragment;
338 }
339
340 if (!$forceBackend && get_option('permalink_structure') != '' && ($wpPageParam || $script != '')) {
341 $base = $script;
6a488035
TO
342 }
343
887f5d81
TO
344 $queryParts = array();
345 if (isset($path)) {
346 $queryParts[] = 'page=CiviCRM';
347 $queryParts[] = "q={$path}";
6a488035 348 }
887f5d81
TO
349 if ($wpPageParam) {
350 $queryParts[] = $wpPageParam;
351 }
352 if (isset($query)) {
353 $queryParts[] = $query;
6a488035
TO
354 }
355
887f5d81
TO
356 return $base . '?' . implode($separator, $queryParts) . $fragment;
357 }
358
bb3a214a
EM
359 /**
360 * @param $absolute
361 * @param $frontend
362 * @param $forceBackend
363 *
364 * @return mixed|null|string
365 */
887f5d81 366 private function getBaseUrl($absolute, $frontend, $forceBackend) {
353ffa53 367 $config = CRM_Core_Config::singleton();
6a488035
TO
368
369 if (!isset($config->useFrameworkRelativeBase)) {
370 $base = parse_url($config->userFrameworkBaseURL);
371 $config->useFrameworkRelativeBase = $base['path'];
372 }
373
374 $base = $absolute ? $config->userFrameworkBaseURL : $config->useFrameworkRelativeBase;
375
376 if ((is_admin() && !$frontend) || $forceBackend) {
377 $base .= 'wp-admin/admin.php';
887f5d81 378 return $base;
6a488035
TO
379 }
380 elseif (defined('CIVICRM_UF_WP_BASEPAGE')) {
381 $base .= CIVICRM_UF_WP_BASEPAGE;
887f5d81 382 return $base;
6a488035 383 }
36b820ae
DL
384 elseif (isset($config->wpBasePage)) {
385 $base .= $config->wpBasePage;
887f5d81 386 return $base;
36b820ae 387 }
887f5d81 388 return $base;
01aca362 389 }
6a488035
TO
390
391 /**
392 * Authenticate the user against the wordpress db
393 *
77855840
TO
394 * @param string $name
395 * The user name.
396 * @param string $password
397 * The password for the above user name.
6a488035 398 *
f4aaa82a
EM
399 * @param bool $loadCMSBootstrap
400 * @param null $realPath
401 *
72b3a70c
CW
402 * @return array|bool
403 * [contactID, ufID, uniqueString] if success else false if no auth
6a488035 404 */
00be9182 405 public function authenticate($name, $password, $loadCMSBootstrap = FALSE, $realPath = NULL) {
6a488035
TO
406 $config = CRM_Core_Config::singleton();
407
408 if ($loadCMSBootstrap) {
b6c54d16 409 $config->userSystem->loadBootStrap($name, $password);
6a488035
TO
410 }
411
412 $user = wp_authenticate($name, $password);
413 if (is_a($user, 'WP_Error')) {
414 return FALSE;
415 }
416
417 // need to change this to make sure we matched only one row
418
419 CRM_Core_BAO_UFMatch::synchronizeUFMatch($user->data, $user->data->ID, $user->data->user_email, 'WordPress');
420 $contactID = CRM_Core_BAO_UFMatch::getContactId($user->data->ID);
421 if (!$contactID) {
422 return FALSE;
423 }
424 return array($contactID, $user->data->ID, mt_rand());
425 }
426
427 /**
428 * Set a message in the UF to display to a user
429 *
77855840
TO
430 * @param string $message
431 * The message to set.
6a488035 432 *
6a488035 433 */
00be9182 434 public function setMessage($message) {
6a488035
TO
435 }
436
bb3a214a
EM
437 /**
438 * @param $user
439 *
440 * @return bool
441 */
e7292422
TO
442 public function loadUser($user) {
443 return TRUE;
6a488035
TO
444 }
445
00be9182 446 public function permissionDenied() {
0499b0ad 447 CRM_Core_Error::fatal(ts('You do not have permission to access this page.'));
6a488035
TO
448 }
449
00be9182 450 public function logout() {
6a488035
TO
451 // destroy session
452 if (session_id()) {
453 session_destroy();
454 }
455 wp_logout();
456 wp_redirect(wp_login_url());
457 }
458
e7292422
TO
459 public function updateCategories() {
460 }
6a488035
TO
461
462 /**
463 * Get the locale set in the hosting CMS
464 *
a6c01b45
CW
465 * @return string
466 * with the locale or null for none
6a488035 467 */
00be9182 468 public function getUFLocale() {
19780d2b
DL
469 // WPML plugin
470 if (defined('ICL_LANGUAGE_CODE')) {
471 $language = ICL_LANGUAGE_CODE;
472 }
473
474 // TODO: set language variable for others WordPress plugin
475
476 if (isset($language)) {
477 return CRM_Core_I18n_PseudoConstant::longForShort(substr($language, 0, 2));
0db6c3e1
TO
478 }
479 else {
19780d2b
DL
480 return NULL;
481 }
6a488035
TO
482 }
483
484 /**
100fef9d 485 * Load wordpress bootstrap
6a488035 486 *
5a4f6742
CW
487 * @param string $name
488 * optional username for login.
489 * @param string $pass
490 * optional password for login.
f4aaa82a
EM
491 *
492 * @return bool
6a488035 493 */
00be9182 494 public function loadBootStrap($name = NULL, $pass = NULL) {
6a488035
TO
495 global $wp, $wp_rewrite, $wp_the_query, $wp_query, $wpdb;
496
497 $cmsRootPath = $this->cmsRootPath();
498 if (!$cmsRootPath) {
499 CRM_Core_Error::fatal("Could not find the install directory for WordPress");
500 }
501
e7292422 502 require_once $cmsRootPath . DIRECTORY_SEPARATOR . 'wp-load.php';
6491539b
DL
503 $wpUserTimezone = get_option('timezone_string');
504 if ($wpUserTimezone) {
505 date_default_timezone_set($wpUserTimezone);
506 CRM_Core_Config::singleton()->userSystem->setMySQLTimeZone();
507 }
e7292422 508 require_once $cmsRootPath . DIRECTORY_SEPARATOR . 'wp-includes/pluggable.php';
fe1e7958 509 $uid = CRM_Utils_Array::value('uid', $name);
510 if ($uid) {
511 $account = wp_set_current_user($uid);
512 if ($account && $account->data->ID) {
513 global $user;
514 $user = $account;
515 return TRUE;
516 }
517 }
e7292422 518 return TRUE;
6a488035
TO
519 }
520
bb3a214a
EM
521 /**
522 * @param $dir
523 *
524 * @return bool
525 */
00be9182 526 public function validInstallDir($dir) {
dfbcf0b7 527 $includePath = "$dir/wp-includes";
f81c7606 528 if (file_exists("$includePath/version.php")) {
dfbcf0b7
DL
529 return TRUE;
530 }
531 return FALSE;
532 }
533
bb3a214a
EM
534 /**
535 * Determine the location of the CMS root.
536 *
72b3a70c
CW
537 * @return string|NULL
538 * local file system path to CMS root, or NULL if it cannot be determined
bb3a214a 539 */
00be9182 540 public function cmsRootPath() {
6a488035 541 $cmsRoot = $valid = NULL;
dfbcf0b7
DL
542 if (defined('CIVICRM_CMSDIR')) {
543 if ($this->validInstallDir(CIVICRM_CMSDIR)) {
544 $cmsRoot = CIVICRM_CMSDIR;
545 $valid = TRUE;
546 }
6a488035 547 }
dfbcf0b7
DL
548 else {
549 $pathVars = explode('/', str_replace('\\', '/', $_SERVER['SCRIPT_FILENAME']));
6a488035 550
dfbcf0b7
DL
551 //might be windows installation.
552 $firstVar = array_shift($pathVars);
553 if ($firstVar) {
554 $cmsRoot = $firstVar;
555 }
556
557 //start w/ csm dir search.
558 foreach ($pathVars as $var) {
559 $cmsRoot .= "/$var";
560 if ($this->validInstallDir($cmsRoot)) {
561 //stop as we found bootstrap.
562 $valid = TRUE;
563 break;
564 }
6a488035
TO
565 }
566 }
567
568 return ($valid) ? $cmsRoot : NULL;
569 }
570
bb3a214a 571 /**
c490a46a 572 * @param array $params
bb3a214a
EM
573 * @param $mail
574 *
575 * @return mixed
576 */
00be9182 577 public function createUser(&$params, $mail) {
6a488035
TO
578 $user_data = array(
579 'ID' => '',
580 'user_pass' => $params['cms_pass'],
581 'user_login' => $params['cms_name'],
582 'user_email' => $params[$mail],
583 'nickname' => $params['cms_name'],
584 'role' => get_option('default_role'),
585 );
586 if (isset($params['contactID'])) {
587 $contactType = CRM_Contact_BAO_Contact::getContactType($params['contactID']);
588 if ($contactType == 'Individual') {
589 $user_data['first_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact',
590 $params['contactID'], 'first_name'
591 );
592 $user_data['last_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact',
593 $params['contactID'], 'last_name'
594 );
595 }
596 }
597
598 $uid = wp_insert_user($user_data);
599
600 $creds = array();
601 $creds['user_login'] = $params['cms_name'];
602 $creds['user_password'] = $params['cms_pass'];
603 $creds['remember'] = TRUE;
604 $user = wp_signon($creds, FALSE);
605
606 wp_new_user_notification($uid, $user_data['user_pass']);
607 return $uid;
608 }
609
f4aaa82a 610 /**
6a488035
TO
611 * Change user name in host CMS
612 *
77855840
TO
613 * @param int $ufID
614 * User ID in CMS.
615 * @param string $ufName
616 * User name.
6a488035 617 */
00be9182 618 public function updateCMSName($ufID, $ufName) {
6a488035
TO
619 // CRM-10620
620 if (function_exists('wp_update_user')) {
353ffa53 621 $ufID = CRM_Utils_Type::escape($ufID, 'Integer');
6a488035
TO
622 $ufName = CRM_Utils_Type::escape($ufName, 'String');
623
e7292422 624 $values = array('ID' => $ufID, 'user_email' => $ufName);
481a74f4
TO
625 if ($ufID) {
626 wp_update_user($values);
6a488035
TO
627 }
628 }
629 }
630
bb3a214a 631 /**
c490a46a 632 * @param array $params
bb3a214a
EM
633 * @param $errors
634 * @param string $emailName
635 */
00be9182 636 public function checkUserNameEmailExists(&$params, &$errors, $emailName = 'email') {
6a488035
TO
637 $config = CRM_Core_Config::singleton();
638
353ffa53
TO
639 $dao = new CRM_Core_DAO();
640 $name = $dao->escape(CRM_Utils_Array::value('name', $params));
6a488035
TO
641 $email = $dao->escape(CRM_Utils_Array::value('mail', $params));
642
a7488080 643 if (!empty($params['name'])) {
6a488035
TO
644 if (!validate_username($params['name'])) {
645 $errors['cms_name'] = ts("Your username contains invalid characters");
646 }
647 elseif (username_exists(sanitize_user($params['name']))) {
648 $errors['cms_name'] = ts('The username %1 is already taken. Please select another username.', array(1 => $params['name']));
649 }
650 }
651
a7488080 652 if (!empty($params['mail'])) {
6a488035
TO
653 if (!is_email($params['mail'])) {
654 $errors[$emailName] = "Your email is invaid";
655 }
656 elseif (email_exists($params['mail'])) {
657 $resetUrl = $config->userFrameworkBaseURL . 'wp-login.php?action=lostpassword';
658 $errors[$emailName] = ts('The email address %1 is already registered. <a href="%2">Have you forgotten your password?</a>',
659 array(1 => $params['mail'], 2 => $resetUrl)
660 );
661 }
662 }
663 }
664
665 /**
100fef9d 666 * Check is user logged in.
6a488035 667 *
408b79bf 668 * @return bool
6a488035
TO
669 */
670 public function isUserLoggedIn() {
671 $isloggedIn = FALSE;
672 if (function_exists('is_user_logged_in')) {
673 $isloggedIn = is_user_logged_in();
674 }
675
676 return $isloggedIn;
677 }
678
bb3a214a
EM
679 /**
680 * @return mixed
681 */
00be9182 682 public function getLoggedInUserObject() {
2b617cb0 683 if (function_exists('is_user_logged_in') &&
353ffa53
TO
684 is_user_logged_in()
685 ) {
2b617cb0
EM
686 global $current_user;
687 }
688 return $current_user;
689 }
353ffa53 690
6a488035
TO
691 /**
692 * Get currently logged in user uf id.
693 *
a6c01b45
CW
694 * @return int
695 * $userID logged in user uf id.
6a488035
TO
696 */
697 public function getLoggedInUfID() {
698 $ufID = NULL;
2b617cb0
EM
699 $current_user = $this->getLoggedInUserObject();
700 return isset($current_user->ID) ? $current_user->ID : NULL;
701 }
702
703 /**
704 * Get currently logged in user unique identifier - this tends to be the email address or user name.
705 *
a6c01b45
CW
706 * @return string
707 * logged in user unique identifier
2b617cb0 708 */
00be9182 709 public function getLoggedInUniqueIdentifier() {
2b617cb0
EM
710 $user = $this->getLoggedInUserObject();
711 return $this->getUniqueIdentifierFromUserObject($user);
6a488035
TO
712 }
713
32998c82
EM
714 /**
715 * Get User ID from UserFramework system (Joomla)
77855840
TO
716 * @param object $user
717 * Object as described by the CMS.
72b3a70c
CW
718 *
719 * @return int|null
32998c82 720 */
00be9182 721 public function getUserIDFromUserObject($user) {
32998c82
EM
722 return !empty($user->ID) ? $user->ID : NULL;
723 }
724
2b617cb0
EM
725 /**
726 * Get Unique Identifier from UserFramework system (CMS)
77855840
TO
727 * @param object $user
728 * Object as described by the User Framework.
72b3a70c
CW
729 * @return int|null
730 * Unique identifier from the user Framework system
2b617cb0 731 */
00be9182 732 public function getUniqueIdentifierFromUserObject($user) {
2b617cb0
EM
733 return empty($user->user_email) ? NULL : $user->user_email;
734 }
735
6a488035
TO
736 /**
737 * Get user login URL for hosting CMS (method declared in each CMS system class)
738 *
77855840
TO
739 * @param string $destination
740 * If present, add destination to querystring (works for Drupal only).
6a488035 741 *
a6c01b45
CW
742 * @return string
743 * loginURL for the current CMS
6a488035
TO
744 */
745 public function getLoginURL($destination = '') {
746 $config = CRM_Core_Config::singleton();
747 $loginURL = $config->userFrameworkBaseURL;
748 $loginURL .= 'wp-login.php';
749 return $loginURL;
750 }
751
bb3a214a 752 /**
c490a46a 753 * @param CRM_Core_Form $form
bb3a214a 754 */
6a488035 755 public function getLoginDestination(&$form) {
408b79bf 756 return NULL;
6a488035
TO
757 }
758
759 /**
760 * Return the current WordPress version if relevant function exists
761 *
a6c01b45
CW
762 * @return string
763 * version number
6a488035 764 */
00be9182 765 public function getVersion() {
6a488035
TO
766 if (function_exists('get_bloginfo')) {
767 return get_bloginfo('version', 'display');
768 }
769 else {
770 return 'Unknown';
771 }
772 }
6491539b
DL
773
774 /**
100fef9d 775 * Get timezone as a string
a6c01b45
CW
776 * @return string
777 * Timezone e.g. 'America/Los_Angeles'
6491539b 778 */
00be9182 779 public function getTimeZoneString() {
6491539b
DL
780 return get_option('timezone_string');
781 }
59f97da6
EM
782
783 /**
784 * Get Url to view user record
77855840
TO
785 * @param int $contactID
786 * Contact ID.
59f97da6
EM
787 *
788 * @return string
789 */
00be9182 790 public function getUserRecordUrl($contactID) {
59f97da6 791 $uid = CRM_Core_BAO_UFMatch::getUFId($contactID);
353ffa53
TO
792 if (CRM_Core_Session::singleton()
793 ->get('userID') == $contactID || CRM_Core_Permission::checkAnyPerm(array('cms:administer users'))
794 ) {
59f97da6
EM
795 return CRM_Core_Config::singleton()->userFrameworkBaseURL . "wp-admin/user-edit.php?user_id=" . $uid;
796 }
797 }
6a488035 798}