Merge pull request #13653 from MegaphoneJon/reporting-8
[civicrm-core.git] / CRM / Utils / System / WordPress.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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
31 * @copyright CiviCRM LLC (c) 2004-2019
32 * $Id$
33 *
34 */
35
36 /**
37 * WordPress specific stuff goes here
38 */
39 class CRM_Utils_System_WordPress extends CRM_Utils_System_Base {
40 /**
41 */
42 public function __construct() {
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 */
49 $this->is_drupal = FALSE;
50 $this->is_wordpress = TRUE;
51 }
52
53 /**
54 * @inheritDoc
55 */
56 public function setTitle($title, $pageTitle = NULL) {
57 if (!$pageTitle) {
58 $pageTitle = $title;
59 }
60
61 // FIXME: Why is this global?
62 global $civicrm_wp_title;
63 $civicrm_wp_title = $title;
64
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);
72 }
73 }
74
75 /**
76 * Moved from CRM_Utils_System_Base
77 */
78 public function getDefaultFileStorage() {
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() {
99 global $civicrm_root;
100
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');
105 }
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);
114 }
115 $civiRelPath = CRM_Utils_File::relativize(realpath($civicrm_root), realpath($cmsPath));
116 $civiUrl = rtrim($cmsUrl, '/') . '/' . ltrim($civiRelPath, ' /');
117 return array(
118 'url' => CRM_Utils_File::addTrailingSlash($civiUrl, '/'),
119 'path' => CRM_Utils_File::addTrailingSlash($civicrm_root),
120 );
121 }
122
123 /**
124 * @inheritDoc
125 */
126 public function appendBreadCrumb($breadCrumbs) {
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 /**
152 * @inheritDoc
153 */
154 public function resetBreadCrumb() {
155 $bc = array();
156 wp_set_breadcrumb($bc);
157 }
158
159 /**
160 * @inheritDoc
161 */
162 public function addHTMLHead($head) {
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
175 /**
176 * WP action callback.
177 */
178 public static function _showHTMLHead() {
179 $region = CRM_Core_Region::instance('wp_head', FALSE);
180 if ($region) {
181 echo $region->render('');
182 }
183 }
184
185 /**
186 * @inheritDoc
187 */
188 public function mapConfigToSSL() {
189 global $base_url;
190 $base_url = str_replace('http://', 'https://', $base_url);
191 }
192
193 /**
194 * @inheritDoc
195 */
196 public function url(
197 $path = NULL,
198 $query = NULL,
199 $absolute = FALSE,
200 $fragment = NULL,
201 $frontend = FALSE,
202 $forceBackend = FALSE
203 ) {
204 $config = CRM_Core_Config::singleton();
205 $script = '';
206 $separator = '&';
207 $wpPageParam = '';
208 $fragment = isset($fragment) ? ('#' . $fragment) : '';
209
210 $path = CRM_Utils_String::stripPathChars($path);
211 $basepage = FALSE;
212
213 //this means wp function we are trying to use is not available,
214 //so load bootStrap
215 // FIXME: Why bootstrap in url()? Generally want to define 1-2 strategic places to put bootstrap
216 if (!function_exists('get_option')) {
217 $this->loadBootStrap();
218 }
219
220 if ($config->userFrameworkFrontend) {
221 global $post;
222 if (get_option('permalink_structure') != '') {
223 $script = get_permalink($post->ID);
224 }
225 if ($config->wpBasePage == $post->post_name) {
226 $basepage = TRUE;
227 }
228 // when shortcode is included in page
229 // also make sure we have valid query object
230 // FIXME: $wpPageParam has no effect and is only set on the *basepage*
231 global $wp_query;
232 if (get_option('permalink_structure') == '' && method_exists($wp_query, 'get')) {
233 if (get_query_var('page_id')) {
234 $wpPageParam = "page_id=" . get_query_var('page_id');
235 }
236 elseif (get_query_var('p')) {
237 // when shortcode is inserted in post
238 $wpPageParam = "p=" . get_query_var('p');
239 }
240 }
241 }
242
243 $base = $this->getBaseUrl($absolute, $frontend, $forceBackend);
244
245 if (!isset($path) && !isset($query)) {
246 // FIXME: This short-circuited codepath is the same as the general one below, except
247 // in that it ignores "permlink_structure" / $wpPageParam / $script . I don't know
248 // why it's different (and I can only find two obvious use-cases for this codepath,
249 // of which at least one looks gratuitous). A more ambitious person would simply remove
250 // this code.
251 return $base . $fragment;
252 }
253
254 if (!$forceBackend && get_option('permalink_structure') != '' && ($wpPageParam || $script != '')) {
255 $base = $script;
256 }
257
258 $queryParts = array();
259
260 if (
261 // not using clean URLs
262 !$config->cleanURL
263 // requesting an admin URL
264 || ((is_admin() && !$frontend) || $forceBackend)
265 // is shortcode
266 || (!$basepage && $script != '')
267 ) {
268
269 // pre-existing logic
270 if (isset($path)) {
271 $queryParts[] = 'page=CiviCRM';
272 // Encode all but the *path* placeholder
273 if ($path !== '*path*') {
274 $path = rawurlencode($path);
275 }
276 $queryParts[] = "q={$path}";
277 }
278 if ($wpPageParam) {
279 $queryParts[] = $wpPageParam;
280 }
281 if (!empty($query)) {
282 $queryParts[] = $query;
283 }
284
285 $final = $base . '?' . implode($separator, $queryParts) . $fragment;
286
287 }
288 else {
289
290 // clean URLs
291 if (isset($path)) {
292 $base = trailingslashit($base) . str_replace('civicrm/', '', $path) . '/';
293 }
294 if (isset($query)) {
295 $query = ltrim($query, '=?&');
296 $queryParts[] = $query;
297 }
298
299 if (!empty($queryParts)) {
300 $final = $base . '?' . implode($separator, $queryParts) . $fragment;
301 }
302 else {
303 $final = $base . $fragment;
304 }
305
306 }
307
308 return $final;
309 }
310
311 /**
312 * 27-09-2016
313 * CRM-16421 CRM-17633 WIP Changes to support WP in it's own directory
314 * https://wiki.civicrm.org/confluence/display/CRM/WordPress+installed+in+its+own+directory+issues
315 * For now leave hard coded wp-admin references.
316 * TODO: remove wp-admin references and replace with admin_url() in the future. Look at best way to get path to admin_url
317 *
318 * @param $absolute
319 * @param $frontend
320 * @param $forceBackend
321 *
322 * @return mixed|null|string
323 */
324 private function getBaseUrl($absolute, $frontend, $forceBackend) {
325 $config = CRM_Core_Config::singleton();
326 if ((is_admin() && !$frontend) || $forceBackend) {
327 return Civi::paths()->getUrl('[wp.backend]/.', $absolute ? 'absolute' : 'relative');
328 }
329 else {
330 return Civi::paths()->getUrl('[wp.frontend]/.', $absolute ? 'absolute' : 'relative');
331 }
332 }
333
334 /**
335 * @inheritDoc
336 */
337 public function authenticate($name, $password, $loadCMSBootstrap = FALSE, $realPath = NULL) {
338 $config = CRM_Core_Config::singleton();
339
340 if ($loadCMSBootstrap) {
341 $config->userSystem->loadBootStrap($name, $password);
342 }
343
344 $user = wp_authenticate($name, $password);
345 if (is_a($user, 'WP_Error')) {
346 return FALSE;
347 }
348
349 // TODO: need to change this to make sure we matched only one row
350
351 CRM_Core_BAO_UFMatch::synchronizeUFMatch($user->data, $user->data->ID, $user->data->user_email, 'WordPress');
352 $contactID = CRM_Core_BAO_UFMatch::getContactId($user->data->ID);
353 if (!$contactID) {
354 return FALSE;
355 }
356 return array($contactID, $user->data->ID, mt_rand());
357 }
358
359 /**
360 * FIXME: Do something
361 *
362 * @param string $message
363 */
364 public function setMessage($message) {
365 }
366
367 /**
368 * @param \string $user
369 *
370 * @return bool
371 */
372 public function loadUser($user) {
373 $userdata = get_user_by('login', $user);
374 if (!$userdata->data->ID) {
375 return FALSE;
376 }
377
378 $uid = $userdata->data->ID;
379 wp_set_current_user($uid);
380 $contactID = CRM_Core_BAO_UFMatch::getContactId($uid);
381
382 // lets store contact id and user id in session
383 $session = CRM_Core_Session::singleton();
384 $session->set('ufID', $uid);
385 $session->set('userID', $contactID);
386 return TRUE;
387 }
388
389 /**
390 * FIXME: Use CMS-native approach
391 */
392 public function permissionDenied() {
393 CRM_Core_Error::fatal(ts('You do not have permission to access this page.'));
394 }
395
396 /**
397 * Determine the native ID of the CMS user.
398 *
399 * @param string $username
400 * @return int|NULL
401 */
402 public function getUfId($username) {
403 $userdata = get_user_by('login', $username);
404 if (!$userdata->data->ID) {
405 return NULL;
406 }
407 return $userdata->data->ID;
408 }
409
410 /**
411 * @inheritDoc
412 */
413 public function logout() {
414 // destroy session
415 if (session_id()) {
416 session_destroy();
417 }
418 wp_logout();
419 wp_redirect(wp_login_url());
420 }
421
422 /**
423 * @inheritDoc
424 */
425 public function getUFLocale() {
426 // Polylang plugin
427 if (function_exists('pll_current_language')) {
428 $language = pll_current_language();
429 }
430 // WPML plugin
431 elseif (defined('ICL_LANGUAGE_CODE')) {
432 $language = ICL_LANGUAGE_CODE;
433 }
434
435 // TODO: set language variable for others WordPress plugin
436
437 if (!empty($language)) {
438 return CRM_Core_I18n_PseudoConstant::longForShort(substr($language, 0, 2));
439 }
440 else {
441 return NULL;
442 }
443 }
444
445 /**
446 * @inheritDoc
447 */
448 public function setUFLocale($civicrm_language) {
449 // TODO (probably not possible with WPML?)
450 return TRUE;
451 }
452
453 /**
454 * Load wordpress bootstrap.
455 *
456 * @param string $name
457 * optional username for login.
458 * @param string $pass
459 * optional password for login.
460 *
461 * @return bool
462 */
463 public function loadBootStrap($name = NULL, $pass = NULL) {
464 global $wp, $wp_rewrite, $wp_the_query, $wp_query, $wpdb, $current_site, $current_blog, $current_user;
465
466 if (!defined('WP_USE_THEMES')) {
467 define('WP_USE_THEMES', FALSE);
468 }
469
470 $cmsRootPath = $this->cmsRootPath();
471 if (!$cmsRootPath) {
472 CRM_Core_Error::fatal("Could not find the install directory for WordPress");
473 }
474 $path = Civi::settings()->get('wpLoadPhp');
475 if (!empty($path)) {
476 require_once $path;
477 }
478 elseif (file_exists($cmsRootPath . DIRECTORY_SEPARATOR . 'wp-load.php')) {
479 require_once $cmsRootPath . DIRECTORY_SEPARATOR . 'wp-load.php';
480 }
481 else {
482 CRM_Core_Error::fatal("Could not find the bootstrap file for WordPress");
483 }
484 $wpUserTimezone = get_option('timezone_string');
485 if ($wpUserTimezone) {
486 date_default_timezone_set($wpUserTimezone);
487 CRM_Core_Config::singleton()->userSystem->setMySQLTimeZone();
488 }
489 require_once $cmsRootPath . DIRECTORY_SEPARATOR . 'wp-includes/pluggable.php';
490 $uid = CRM_Utils_Array::value('uid', $name);
491 if (!$uid) {
492 $name = $name ? $name : trim(CRM_Utils_Array::value('name', $_REQUEST));
493 $pass = $pass ? $pass : trim(CRM_Utils_Array::value('pass', $_REQUEST));
494 if ($name) {
495 $uid = wp_authenticate($name, $pass);
496 if (!$uid) {
497 if ($throwError) {
498 echo '<br />Sorry, unrecognized username or password.';
499 exit();
500 }
501 return FALSE;
502 }
503 }
504 }
505 if ($uid) {
506 if ($uid instanceof WP_User) {
507 $account = wp_set_current_user($uid->ID);
508 }
509 else {
510 $account = wp_set_current_user($uid);
511 }
512 if ($account && $account->data->ID) {
513 global $user;
514 $user = $account;
515 return TRUE;
516 }
517 }
518 return TRUE;
519 }
520
521 /**
522 * @param $dir
523 *
524 * @return bool
525 */
526 public function validInstallDir($dir) {
527 $includePath = "$dir/wp-includes";
528 if (@file_exists("$includePath/version.php")) {
529 return TRUE;
530 }
531 return FALSE;
532 }
533
534 /**
535 * Determine the location of the CMS root.
536 *
537 * @return string|NULL
538 * local file system path to CMS root, or NULL if it cannot be determined
539 */
540 public function cmsRootPath() {
541 global $civicrm_paths;
542 if (!empty($civicrm_paths['cms.root']['path'])) {
543 return $civicrm_paths['cms.root']['path'];
544 }
545
546 $cmsRoot = $valid = NULL;
547 if (defined('CIVICRM_CMSDIR')) {
548 if ($this->validInstallDir(CIVICRM_CMSDIR)) {
549 $cmsRoot = CIVICRM_CMSDIR;
550 $valid = TRUE;
551 }
552 }
553 else {
554 $pathVars = explode('/', str_replace('\\', '/', $_SERVER['SCRIPT_FILENAME']));
555
556 //might be windows installation.
557 $firstVar = array_shift($pathVars);
558 if ($firstVar) {
559 $cmsRoot = $firstVar;
560 }
561
562 //start w/ csm dir search.
563 foreach ($pathVars as $var) {
564 $cmsRoot .= "/$var";
565 if ($this->validInstallDir($cmsRoot)) {
566 //stop as we found bootstrap.
567 $valid = TRUE;
568 break;
569 }
570 }
571 }
572
573 return ($valid) ? $cmsRoot : NULL;
574 }
575
576 /**
577 * @inheritDoc
578 */
579 public 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
612 /**
613 * @inheritDoc
614 */
615 public function updateCMSName($ufID, $ufName) {
616 // CRM-10620
617 if (function_exists('wp_update_user')) {
618 $ufID = CRM_Utils_Type::escape($ufID, 'Integer');
619 $ufName = CRM_Utils_Type::escape($ufName, 'String');
620
621 $values = array('ID' => $ufID, 'user_email' => $ufName);
622 if ($ufID) {
623 wp_update_user($values);
624 }
625 }
626 }
627
628 /**
629 * @param array $params
630 * @param $errors
631 * @param string $emailName
632 */
633 public function checkUserNameEmailExists(&$params, &$errors, $emailName = 'email') {
634 $config = CRM_Core_Config::singleton();
635
636 $dao = new CRM_Core_DAO();
637 $name = $dao->escape(CRM_Utils_Array::value('name', $params));
638 $email = $dao->escape(CRM_Utils_Array::value('mail', $params));
639
640 if (!empty($params['name'])) {
641 if (!validate_username($params['name'])) {
642 $errors['cms_name'] = ts("Your username contains invalid characters");
643 }
644 elseif (username_exists(sanitize_user($params['name']))) {
645 $errors['cms_name'] = ts('The username %1 is already taken. Please select another username.', array(1 => $params['name']));
646 }
647 }
648
649 if (!empty($params['mail'])) {
650 if (!is_email($params['mail'])) {
651 $errors[$emailName] = "Your email is invaid";
652 }
653 elseif (email_exists($params['mail'])) {
654 $errors[$emailName] = ts('The email address %1 already has an account associated with it. <a href="%2">Have you forgotten your password?</a>',
655 array(1 => $params['mail'], 2 => wp_lostpassword_url())
656 );
657 }
658 }
659 }
660
661 /**
662 * @inheritDoc
663 */
664 public function isUserLoggedIn() {
665 $isloggedIn = FALSE;
666 if (function_exists('is_user_logged_in')) {
667 $isloggedIn = is_user_logged_in();
668 }
669
670 return $isloggedIn;
671 }
672
673 /**
674 * @inheritDoc
675 */
676 public function isUserRegistrationPermitted() {
677 if (!get_option('users_can_register')) {
678 return FALSE;
679 }
680 return TRUE;
681 }
682
683 /**
684 * @inheritDoc
685 */
686 public function isPasswordUserGenerated() {
687 return TRUE;
688 }
689
690 /**
691 * @return mixed
692 */
693 public function getLoggedInUserObject() {
694 if (function_exists('is_user_logged_in') &&
695 is_user_logged_in()
696 ) {
697 global $current_user;
698 }
699 return $current_user;
700 }
701
702 /**
703 * @inheritDoc
704 */
705 public function getLoggedInUfID() {
706 $ufID = NULL;
707 $current_user = $this->getLoggedInUserObject();
708 return isset($current_user->ID) ? $current_user->ID : NULL;
709 }
710
711 /**
712 * @inheritDoc
713 */
714 public function getLoggedInUniqueIdentifier() {
715 $user = $this->getLoggedInUserObject();
716 return $this->getUniqueIdentifierFromUserObject($user);
717 }
718
719 /**
720 * Get User ID from UserFramework system (Joomla)
721 * @param object $user
722 * Object as described by the CMS.
723 *
724 * @return int|null
725 */
726 public function getUserIDFromUserObject($user) {
727 return !empty($user->ID) ? $user->ID : NULL;
728 }
729
730 /**
731 * @inheritDoc
732 */
733 public function getUniqueIdentifierFromUserObject($user) {
734 return empty($user->user_email) ? NULL : $user->user_email;
735 }
736
737 /**
738 * @inheritDoc
739 */
740 public function getLoginURL($destination = '') {
741 $config = CRM_Core_Config::singleton();
742 $loginURL = wp_login_url();
743 return $loginURL;
744 }
745
746 /**
747 * FIXME: Do something.
748 *
749 * @param \CRM_Core_Form $form
750 *
751 * @return NULL|string
752 */
753 public function getLoginDestination(&$form) {
754 return NULL;
755 }
756
757 /**
758 * @inheritDoc
759 */
760 public function getVersion() {
761 if (function_exists('get_bloginfo')) {
762 return get_bloginfo('version', 'display');
763 }
764 else {
765 return 'Unknown';
766 }
767 }
768
769 /**
770 * @inheritDoc
771 */
772 public function getTimeZoneString() {
773 return get_option('timezone_string');
774 }
775
776 /**
777 * @inheritDoc
778 */
779 public function getUserRecordUrl($contactID) {
780 $uid = CRM_Core_BAO_UFMatch::getUFId($contactID);
781 if (CRM_Core_Session::singleton()
782 ->get('userID') == $contactID || CRM_Core_Permission::checkAnyPerm(array('cms:administer users'))
783 ) {
784 return CRM_Core_Config::singleton()->userFrameworkBaseURL . "wp-admin/user-edit.php?user_id=" . $uid;
785 }
786 }
787
788 /**
789 * Append WP js to coreResourcesList.
790 *
791 * @param array $list
792 */
793 public function appendCoreResources(&$list) {
794 $list[] = 'js/crm.wordpress.js';
795 }
796
797 /**
798 * @inheritDoc
799 */
800 public function synchronizeUsers() {
801 $config = CRM_Core_Config::singleton();
802 if (PHP_SAPI != 'cli') {
803 set_time_limit(300);
804 }
805 $id = 'ID';
806 $mail = 'user_email';
807
808 $uf = $config->userFramework;
809 $contactCount = 0;
810 $contactCreated = 0;
811 $contactMatching = 0;
812
813 global $wpdb;
814 $wpUserIds = $wpdb->get_col("SELECT $wpdb->users.ID FROM $wpdb->users");
815
816 foreach ($wpUserIds as $wpUserId) {
817 $wpUserData = get_userdata($wpUserId);
818 $contactCount++;
819 if ($match = CRM_Core_BAO_UFMatch::synchronizeUFMatch($wpUserData,
820 $wpUserData->$id,
821 $wpUserData->$mail,
822 $uf,
823 1,
824 'Individual',
825 TRUE
826 )
827 ) {
828 $contactCreated++;
829 }
830 else {
831 $contactMatching++;
832 }
833 if (is_object($match)) {
834 $match->free();
835 }
836 }
837
838 return array(
839 'contactCount' => $contactCount,
840 'contactMatching' => $contactMatching,
841 'contactCreated' => $contactCreated,
842 );
843 }
844
845 }