Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
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 | */ | |
39 | class CRM_Utils_System_WordPress extends CRM_Utils_System_Base { | |
bb3a214a EM |
40 | /** |
41 | * | |
42 | */ | |
6a488035 | 43 | function __construct() { |
4caaa696 EM |
44 | /** |
45 | * 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 | |
46 | * functions and leave the codebase oblivious to the type of CMS | |
47 | * @deprecated | |
48 | * @var bool | |
49 | */ | |
6a488035 | 50 | $this->is_drupal = FALSE; |
fe17e8d1 | 51 | $this->is_wordpress = TRUE; |
6a488035 TO |
52 | } |
53 | ||
54 | /** | |
df7cbeeb | 55 | * Sets the title of the page |
6a488035 TO |
56 | * |
57 | * @param string $title | |
f4aaa82a EM |
58 | * @param null $pageTitle |
59 | * | |
6a488035 TO |
60 | * @return void |
61 | * @access public | |
62 | */ | |
63 | function setTitle($title, $pageTitle = NULL) { | |
64 | if (!$pageTitle) { | |
65 | $pageTitle = $title; | |
66 | } | |
1beae3b1 | 67 | |
0734e0e8 CW |
68 | // get civi-wordpress instance |
69 | $civi = civi_wp(); | |
1beae3b1 | 70 | |
0734e0e8 CW |
71 | // do we have functionality provided by plugin version 4.6+ present? |
72 | if (method_exists($civi, 'civicrm_context_get')) { | |
1beae3b1 | 73 | |
0734e0e8 CW |
74 | global $civicrm_wp_title; |
75 | $civicrm_wp_title = $pageTitle; | |
1beae3b1 | 76 | |
0734e0e8 CW |
77 | // yes, set page title, depending on context |
78 | $context = civi_wp()->civicrm_context_get(); | |
79 | switch ( $context ) { | |
80 | case 'admin': | |
81 | case 'shortcode': | |
82 | $template = CRM_Core_Smarty::singleton(); | |
83 | $template->assign('pageTitle', $pageTitle); | |
84 | } | |
1beae3b1 | 85 | |
0734e0e8 | 86 | } elseif (civicrm_wp_in_civicrm()) { |
1beae3b1 | 87 | |
0734e0e8 | 88 | // legacy pre-4.6 behaviour |
6a488035 TO |
89 | global $civicrm_wp_title; |
90 | $civicrm_wp_title = $pageTitle; | |
91 | $template = CRM_Core_Smarty::singleton(); | |
92 | $template->assign('pageTitle', $pageTitle); | |
1beae3b1 | 93 | |
6a488035 TO |
94 | } |
95 | } | |
96 | ||
97 | /** | |
98 | * Append an additional breadcrumb tag to the existing breadcrumb | |
99 | * | |
f4aaa82a EM |
100 | * @param $breadCrumbs |
101 | * | |
102 | * @internal param string $title | |
103 | * @internal param string $url | |
6a488035 TO |
104 | * |
105 | * @return void | |
106 | * @access public | |
107 | * @static | |
108 | */ | |
109 | function appendBreadCrumb($breadCrumbs) { | |
110 | $breadCrumb = wp_get_breadcrumb(); | |
111 | ||
112 | if (is_array($breadCrumbs)) { | |
113 | foreach ($breadCrumbs as $crumbs) { | |
114 | if (stripos($crumbs['url'], 'id%%')) { | |
115 | $args = array('cid', 'mid'); | |
116 | foreach ($args as $a) { | |
117 | $val = CRM_Utils_Request::retrieve($a, 'Positive', CRM_Core_DAO::$_nullObject, | |
118 | FALSE, NULL, $_GET | |
119 | ); | |
120 | if ($val) { | |
121 | $crumbs['url'] = str_ireplace("%%{$a}%%", $val, $crumbs['url']); | |
122 | } | |
123 | } | |
124 | } | |
125 | $breadCrumb[] = "<a href=\"{$crumbs['url']}\">{$crumbs['title']}</a>"; | |
126 | } | |
127 | } | |
128 | ||
129 | $template = CRM_Core_Smarty::singleton(); | |
130 | $template->assign_by_ref('breadcrumb', $breadCrumb); | |
131 | wp_set_breadcrumb($breadCrumb); | |
132 | } | |
133 | ||
134 | /** | |
135 | * Reset an additional breadcrumb tag to the existing breadcrumb | |
136 | * | |
137 | * @return void | |
138 | * @access public | |
139 | * @static | |
140 | */ | |
141 | function resetBreadCrumb() { | |
142 | $bc = array(); | |
143 | wp_set_breadcrumb($bc); | |
144 | } | |
145 | ||
146 | /** | |
147 | * Append a string to the head of the html file | |
148 | * | |
149 | * @param string $head the new string to be appended | |
150 | * | |
151 | * @return void | |
152 | * @access public | |
153 | * @static | |
154 | */ | |
155 | function addHTMLHead($head) { | |
156 | static $registered = FALSE; | |
157 | if (!$registered) { | |
158 | // front-end view | |
159 | add_action('wp_head', array(__CLASS__, '_showHTMLHead')); | |
160 | // back-end views | |
161 | add_action('admin_head', array(__CLASS__, '_showHTMLHead')); | |
162 | } | |
163 | CRM_Core_Region::instance('wp_head')->add(array( | |
164 | 'markup' => $head, | |
165 | )); | |
166 | } | |
167 | ||
168 | static function _showHTMLHead() { | |
169 | $region = CRM_Core_Region::instance('wp_head', FALSE); | |
170 | if ($region) { | |
171 | echo $region->render(''); | |
172 | } | |
173 | } | |
174 | ||
175 | /** | |
176 | * Add a script file | |
177 | * | |
178 | * @param $url: string, absolute path to file | |
179 | * @param $region string, location within the document: 'html-header', 'page-header', 'page-footer' | |
180 | * | |
181 | * Note: This function is not to be called directly | |
182 | * @see CRM_Core_Region::render() | |
183 | * | |
184 | * @return bool TRUE if we support this operation in this CMS, FALSE otherwise | |
185 | * @access public | |
186 | */ | |
187 | public function addScriptUrl($url, $region) { | |
188 | return FALSE; | |
189 | } | |
190 | ||
191 | /** | |
192 | * Add an inline script | |
193 | * | |
194 | * @param $code: string, javascript code | |
195 | * @param $region string, location within the document: 'html-header', 'page-header', 'page-footer' | |
196 | * | |
197 | * Note: This function is not to be called directly | |
198 | * @see CRM_Core_Region::render() | |
199 | * | |
200 | * @return bool TRUE if we support this operation in this CMS, FALSE otherwise | |
201 | * @access public | |
202 | */ | |
203 | public function addScript($code, $region) { | |
204 | return FALSE; | |
205 | } | |
206 | ||
207 | /** | |
208 | * Add a css file | |
209 | * | |
210 | * @param $url: string, absolute path to file | |
211 | * @param $region string, location within the document: 'html-header', 'page-header', 'page-footer' | |
212 | * | |
213 | * Note: This function is not to be called directly | |
214 | * @see CRM_Core_Region::render() | |
215 | * | |
216 | * @return bool TRUE if we support this operation in this CMS, FALSE otherwise | |
217 | * @access public | |
218 | */ | |
219 | public function addStyleUrl($url, $region) { | |
220 | return FALSE; | |
221 | } | |
222 | ||
223 | /** | |
224 | * Add an inline style | |
225 | * | |
226 | * @param $code: string, css code | |
227 | * @param $region string, location within the document: 'html-header', 'page-header', 'page-footer' | |
228 | * | |
229 | * Note: This function is not to be called directly | |
230 | * @see CRM_Core_Region::render() | |
231 | * | |
232 | * @return bool TRUE if we support this operation in this CMS, FALSE otherwise | |
233 | * @access public | |
234 | */ | |
235 | public function addStyle($code, $region) { | |
236 | return FALSE; | |
237 | } | |
238 | ||
239 | /** | |
240 | * rewrite various system urls to https | |
241 | * | |
242 | * @param null | |
243 | * | |
244 | * @return void | |
245 | * @access public | |
246 | * @static | |
247 | */ | |
248 | function mapConfigToSSL() { | |
249 | global $base_url; | |
250 | $base_url = str_replace('http://', 'https://', $base_url); | |
251 | } | |
252 | ||
253 | /** | |
254 | * figure out the post url for the form | |
255 | * | |
256 | * @param mix $action the default action if one is pre-specified | |
257 | * | |
258 | * @return string the url to post the form | |
259 | * @access public | |
260 | * @static | |
261 | */ | |
262 | function postURL($action) { | |
263 | if (!empty($action)) { | |
264 | return $action; | |
265 | } | |
266 | ||
267 | return $this->url($_GET['q'], NULL, TRUE, NULL, FALSE); | |
268 | } | |
269 | ||
270 | /** | |
271 | * Generate an internal CiviCRM URL (copied from DRUPAL/includes/common.inc#url) | |
272 | * | |
273 | * @param $path string The path being linked to, such as "civicrm/add" | |
274 | * @param $query string A query string to append to the link. | |
275 | * @param $absolute boolean Whether to force the output to be an absolute link (beginning with http:). | |
276 | * Useful for links that will be displayed outside the site, such as in an | |
277 | * RSS feed. | |
278 | * @param $fragment string A fragment identifier (named anchor) to append to the link. | |
279 | * @param $htmlize boolean whether to convert to html eqivalant | |
280 | * @param $frontend boolean a gross joomla hack | |
281 | * | |
f4aaa82a EM |
282 | * @param bool $forceBackend |
283 | * | |
6a488035 TO |
284 | * @return string an HTML string containing a link to the given path. |
285 | * @access public | |
6a488035 TO |
286 | */ |
287 | function url( | |
288 | $path = NULL, | |
289 | $query = NULL, | |
290 | $absolute = FALSE, | |
291 | $fragment = NULL, | |
292 | $htmlize = TRUE, | |
293 | $frontend = FALSE, | |
294 | $forceBackend = FALSE | |
295 | ) { | |
296 | $config = CRM_Core_Config::singleton(); | |
297 | $script = ''; | |
298 | $separator = $htmlize ? '&' : '&'; | |
887f5d81 TO |
299 | $wpPageParam = ''; |
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; | |
318 | if ( method_exists( $wp_query, 'get' ) ) { | |
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 TO |
366 | private function getBaseUrl($absolute, $frontend, $forceBackend) { |
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 | * | |
f4aaa82a | 394 | * @param string $name the user name |
6a488035 TO |
395 | * @param string $password the password for the above user name |
396 | * | |
f4aaa82a EM |
397 | * @param bool $loadCMSBootstrap |
398 | * @param null $realPath | |
399 | * | |
6a488035 TO |
400 | * @return mixed false if no auth |
401 | * array( | |
f4aaa82a | 402 | * contactID, ufID, unique string ) if success |
6a488035 TO |
403 | * @access public |
404 | * @static | |
405 | */ | |
406 | function authenticate($name, $password, $loadCMSBootstrap = FALSE, $realPath = NULL) { | |
407 | $config = CRM_Core_Config::singleton(); | |
408 | ||
409 | if ($loadCMSBootstrap) { | |
b6c54d16 | 410 | $config->userSystem->loadBootStrap($name, $password); |
6a488035 TO |
411 | } |
412 | ||
413 | $user = wp_authenticate($name, $password); | |
414 | if (is_a($user, 'WP_Error')) { | |
415 | return FALSE; | |
416 | } | |
417 | ||
418 | // need to change this to make sure we matched only one row | |
419 | ||
420 | CRM_Core_BAO_UFMatch::synchronizeUFMatch($user->data, $user->data->ID, $user->data->user_email, 'WordPress'); | |
421 | $contactID = CRM_Core_BAO_UFMatch::getContactId($user->data->ID); | |
422 | if (!$contactID) { | |
423 | return FALSE; | |
424 | } | |
425 | return array($contactID, $user->data->ID, mt_rand()); | |
426 | } | |
427 | ||
428 | /** | |
429 | * Set a message in the UF to display to a user | |
430 | * | |
431 | * @param string $message the message to set | |
432 | * | |
433 | * @access public | |
434 | * @static | |
435 | */ | |
436 | function setMessage($message) { | |
437 | } | |
438 | ||
bb3a214a EM |
439 | /** |
440 | * @param $user | |
441 | * | |
442 | * @return bool | |
443 | */ | |
6a488035 TO |
444 | function loadUser( $user ) { |
445 | return true; | |
446 | } | |
447 | ||
448 | function permissionDenied() { | |
449 | CRM_Core_Error::fatal(ts('You do not have permission to access this page')); | |
450 | } | |
451 | ||
452 | function logout() { | |
453 | // destroy session | |
454 | if (session_id()) { | |
455 | session_destroy(); | |
456 | } | |
457 | wp_logout(); | |
458 | wp_redirect(wp_login_url()); | |
459 | } | |
460 | ||
461 | function updateCategories() {} | |
462 | ||
463 | /** | |
464 | * Get the locale set in the hosting CMS | |
465 | * | |
466 | * @return string with the locale or null for none | |
467 | */ | |
468 | 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)); | |
478 | } else { | |
479 | return NULL; | |
480 | } | |
6a488035 TO |
481 | } |
482 | ||
483 | /** | |
484 | * load wordpress bootstrap | |
485 | * | |
486 | * @param $name string optional username for login | |
487 | * @param $pass string optional password for login | |
f4aaa82a EM |
488 | * |
489 | * @return bool | |
6a488035 TO |
490 | */ |
491 | function loadBootStrap($name = NULL, $pass = NULL) { | |
492 | global $wp, $wp_rewrite, $wp_the_query, $wp_query, $wpdb; | |
493 | ||
494 | $cmsRootPath = $this->cmsRootPath(); | |
495 | if (!$cmsRootPath) { | |
496 | CRM_Core_Error::fatal("Could not find the install directory for WordPress"); | |
497 | } | |
498 | ||
499 | require_once ($cmsRootPath . DIRECTORY_SEPARATOR . 'wp-load.php'); | |
6491539b DL |
500 | $wpUserTimezone = get_option('timezone_string'); |
501 | if ($wpUserTimezone) { | |
502 | date_default_timezone_set($wpUserTimezone); | |
503 | CRM_Core_Config::singleton()->userSystem->setMySQLTimeZone(); | |
504 | } | |
fe1e7958 | 505 | require_once ($cmsRootPath . DIRECTORY_SEPARATOR . 'wp-includes/pluggable.php'); |
506 | $uid = CRM_Utils_Array::value('uid', $name); | |
507 | if ($uid) { | |
508 | $account = wp_set_current_user($uid); | |
509 | if ($account && $account->data->ID) { | |
510 | global $user; | |
511 | $user = $account; | |
512 | return TRUE; | |
513 | } | |
514 | } | |
6a488035 TO |
515 | return true; |
516 | } | |
517 | ||
bb3a214a EM |
518 | /** |
519 | * @param $dir | |
520 | * | |
521 | * @return bool | |
522 | */ | |
dfbcf0b7 DL |
523 | function validInstallDir($dir) { |
524 | $includePath = "$dir/wp-includes"; | |
525 | if ( | |
526 | @opendir($includePath) && | |
527 | file_exists("$includePath/version.php") | |
528 | ) { | |
529 | return TRUE; | |
530 | } | |
531 | return FALSE; | |
532 | } | |
533 | ||
bb3a214a EM |
534 | /** |
535 | * Determine the location of the CMS root. | |
536 | * | |
537 | * @return string|NULL local file system path to CMS root, or NULL if it cannot be determined | |
538 | */ | |
539 | /** | |
540 | * @return NULL|string | |
541 | */ | |
6a488035 TO |
542 | function cmsRootPath() { |
543 | $cmsRoot = $valid = NULL; | |
dfbcf0b7 DL |
544 | if (defined('CIVICRM_CMSDIR')) { |
545 | if ($this->validInstallDir(CIVICRM_CMSDIR)) { | |
546 | $cmsRoot = CIVICRM_CMSDIR; | |
547 | $valid = TRUE; | |
548 | } | |
6a488035 | 549 | } |
dfbcf0b7 DL |
550 | else { |
551 | $pathVars = explode('/', str_replace('\\', '/', $_SERVER['SCRIPT_FILENAME'])); | |
6a488035 | 552 | |
dfbcf0b7 DL |
553 | //might be windows installation. |
554 | $firstVar = array_shift($pathVars); | |
555 | if ($firstVar) { | |
556 | $cmsRoot = $firstVar; | |
557 | } | |
558 | ||
559 | //start w/ csm dir search. | |
560 | foreach ($pathVars as $var) { | |
561 | $cmsRoot .= "/$var"; | |
562 | if ($this->validInstallDir($cmsRoot)) { | |
563 | //stop as we found bootstrap. | |
564 | $valid = TRUE; | |
565 | break; | |
566 | } | |
6a488035 TO |
567 | } |
568 | } | |
569 | ||
570 | return ($valid) ? $cmsRoot : NULL; | |
571 | } | |
572 | ||
bb3a214a EM |
573 | /** |
574 | * @param $params | |
575 | * @param $mail | |
576 | * | |
577 | * @return mixed | |
578 | */ | |
6a488035 TO |
579 | function createUser(&$params, $mail) { |
580 | $user_data = array( | |
581 | 'ID' => '', | |
582 | 'user_pass' => $params['cms_pass'], | |
583 | 'user_login' => $params['cms_name'], | |
584 | 'user_email' => $params[$mail], | |
585 | 'nickname' => $params['cms_name'], | |
586 | 'role' => get_option('default_role'), | |
587 | ); | |
588 | if (isset($params['contactID'])) { | |
589 | $contactType = CRM_Contact_BAO_Contact::getContactType($params['contactID']); | |
590 | if ($contactType == 'Individual') { | |
591 | $user_data['first_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', | |
592 | $params['contactID'], 'first_name' | |
593 | ); | |
594 | $user_data['last_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', | |
595 | $params['contactID'], 'last_name' | |
596 | ); | |
597 | } | |
598 | } | |
599 | ||
600 | $uid = wp_insert_user($user_data); | |
601 | ||
602 | $creds = array(); | |
603 | $creds['user_login'] = $params['cms_name']; | |
604 | $creds['user_password'] = $params['cms_pass']; | |
605 | $creds['remember'] = TRUE; | |
606 | $user = wp_signon($creds, FALSE); | |
607 | ||
608 | wp_new_user_notification($uid, $user_data['user_pass']); | |
609 | return $uid; | |
610 | } | |
611 | ||
f4aaa82a | 612 | /** |
6a488035 TO |
613 | * Change user name in host CMS |
614 | * | |
615 | * @param integer $ufID User ID in CMS | |
616 | * @param string $ufName User name | |
617 | */ | |
618 | function updateCMSName($ufID, $ufName) { | |
619 | // CRM-10620 | |
620 | if (function_exists('wp_update_user')) { | |
621 | $ufID = CRM_Utils_Type::escape($ufID, 'Integer'); | |
622 | $ufName = CRM_Utils_Type::escape($ufName, 'String'); | |
623 | ||
624 | $values = array ('ID' => $ufID, 'user_email' => $ufName); | |
625 | if( $ufID ) { | |
626 | wp_update_user( $values ) ; | |
627 | } | |
628 | } | |
629 | } | |
630 | ||
bb3a214a EM |
631 | /** |
632 | * @param $params | |
633 | * @param $errors | |
634 | * @param string $emailName | |
635 | */ | |
6a488035 TO |
636 | function checkUserNameEmailExists(&$params, &$errors, $emailName = 'email') { |
637 | $config = CRM_Core_Config::singleton(); | |
638 | ||
639 | $dao = new CRM_Core_DAO(); | |
640 | $name = $dao->escape(CRM_Utils_Array::value('name', $params)); | |
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 | /** | |
666 | * check is user logged in. | |
667 | * | |
668 | * @return boolean true/false. | |
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 | */ | |
2b617cb0 EM |
682 | function getLoggedInUserObject() { |
683 | if (function_exists('is_user_logged_in') && | |
684 | is_user_logged_in()) { | |
685 | global $current_user; | |
686 | } | |
687 | return $current_user; | |
688 | } | |
6a488035 TO |
689 | /** |
690 | * Get currently logged in user uf id. | |
691 | * | |
692 | * @return int $userID logged in user uf id. | |
693 | */ | |
694 | public function getLoggedInUfID() { | |
695 | $ufID = NULL; | |
2b617cb0 EM |
696 | $current_user = $this->getLoggedInUserObject(); |
697 | return isset($current_user->ID) ? $current_user->ID : NULL; | |
698 | } | |
699 | ||
700 | /** | |
701 | * Get currently logged in user unique identifier - this tends to be the email address or user name. | |
702 | * | |
703 | * @return string $userID logged in user unique identifier | |
704 | */ | |
705 | function getLoggedInUniqueIdentifier() { | |
706 | $user = $this->getLoggedInUserObject(); | |
707 | return $this->getUniqueIdentifierFromUserObject($user); | |
6a488035 TO |
708 | } |
709 | ||
32998c82 EM |
710 | /** |
711 | * Get User ID from UserFramework system (Joomla) | |
712 | * @param object $user object as described by the CMS | |
713 | * @return mixed <NULL, number> | |
714 | */ | |
715 | function getUserIDFromUserObject($user) { | |
716 | return !empty($user->ID) ? $user->ID : NULL; | |
717 | } | |
718 | ||
2b617cb0 EM |
719 | /** |
720 | * Get Unique Identifier from UserFramework system (CMS) | |
721 | * @param object $user object as described by the User Framework | |
722 | * @return mixed $uniqueIdentifer Unique identifier from the user Framework system | |
723 | * | |
724 | */ | |
725 | function getUniqueIdentifierFromUserObject($user) { | |
726 | return empty($user->user_email) ? NULL : $user->user_email; | |
727 | } | |
728 | ||
6a488035 TO |
729 | /** |
730 | * Get user login URL for hosting CMS (method declared in each CMS system class) | |
731 | * | |
732 | * @param string $destination - if present, add destination to querystring (works for Drupal only) | |
733 | * | |
734 | * @return string - loginURL for the current CMS | |
735 | * | |
736 | */ | |
737 | public function getLoginURL($destination = '') { | |
738 | $config = CRM_Core_Config::singleton(); | |
739 | $loginURL = $config->userFrameworkBaseURL; | |
740 | $loginURL .= 'wp-login.php'; | |
741 | return $loginURL; | |
742 | } | |
743 | ||
bb3a214a EM |
744 | /** |
745 | * @param $form | |
746 | */ | |
6a488035 TO |
747 | public function getLoginDestination(&$form) { |
748 | return; | |
749 | } | |
750 | ||
751 | /** | |
752 | * Return the current WordPress version if relevant function exists | |
753 | * | |
754 | * @return string - version number | |
755 | * | |
756 | */ | |
757 | function getVersion() { | |
758 | if (function_exists('get_bloginfo')) { | |
759 | return get_bloginfo('version', 'display'); | |
760 | } | |
761 | else { | |
762 | return 'Unknown'; | |
763 | } | |
764 | } | |
6491539b DL |
765 | |
766 | /** | |
767 | * get timezone as a string | |
768 | * @return string Timezone e.g. 'America/Los_Angeles' | |
769 | */ | |
770 | function getTimeZoneString() { | |
771 | return get_option('timezone_string'); | |
772 | } | |
59f97da6 EM |
773 | |
774 | /** | |
775 | * Get Url to view user record | |
776 | * @param integer $contactID Contact ID | |
777 | * | |
778 | * @return string | |
779 | */ | |
780 | function getUserRecordUrl($contactID) { | |
781 | $uid = CRM_Core_BAO_UFMatch::getUFId($contactID); | |
782 | if (CRM_Core_Session::singleton()->get('userID') == $contactID || CRM_Core_Permission::checkAnyPerm(array('cms:administer users'))) { | |
783 | return CRM_Core_Config::singleton()->userFrameworkBaseURL . "wp-admin/user-edit.php?user_id=" . $uid; | |
784 | } | |
785 | } | |
6a488035 TO |
786 | } |
787 |