3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
21 * WordPress specific stuff goes here
23 class CRM_Utils_System_WordPress
extends CRM_Utils_System_Base
{
27 public function __construct() {
29 * 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
30 * functions and leave the codebase oblivious to the type of CMS
34 $this->is_drupal
= FALSE;
35 $this->is_wordpress
= TRUE;
38 public function initialize() {
40 $this->registerPathVars();
44 * Specify the default computation for various paths/URLs.
46 protected function registerPathVars():void
{
47 $isNormalBoot = function_exists('get_option');
49 // Normal mode - CMS boots first, then calls Civi. "Normal" web pages and newer extern routes.
50 // To simplify the code-paths, some items are re-registered with WP-specific functions.
51 $cmsRoot = function() {
53 'path' => untrailingslashit(ABSPATH
),
57 Civi
::paths()->register('cms', $cmsRoot);
58 Civi
::paths()->register('cms.root', $cmsRoot);
59 Civi
::paths()->register('civicrm.root', function () {
61 'path' => CIVICRM_PLUGIN_DIR
. 'civicrm' . DIRECTORY_SEPARATOR
,
62 'url' => CIVICRM_PLUGIN_URL
. 'civicrm/',
65 Civi
::paths()->register('wp.frontend.base', function () {
67 'url' => home_url('/'),
70 Civi
::paths()->register('wp.frontend', function () {
71 $config = CRM_Core_Config
::singleton();
72 $basepage = get_page_by_path($config->wpBasePage
);
74 'url' => get_permalink($basepage->ID
),
77 Civi
::paths()->register('wp.backend.base', function () {
82 Civi
::paths()->register('wp.backend', function() {
84 'url' => admin_url('admin.php'),
89 // Legacy support - only relevant for older extern routes.
91 ->register('wp.frontend.base', function () {
92 return ['url' => rtrim(CIVICRM_UF_BASEURL
, '/') . '/'];
94 ->register('wp.frontend', function () {
95 $config = \CRM_Core_Config
::singleton();
96 $suffix = defined('CIVICRM_UF_WP_BASEPAGE') ? CIVICRM_UF_WP_BASEPAGE
: $config->wpBasePage
;
98 'url' => Civi
::paths()->getVariable('wp.frontend.base', 'url') . $suffix,
101 ->register('wp.backend.base', function () {
102 return ['url' => rtrim(CIVICRM_UF_BASEURL
, '/') . '/wp-admin/'];
104 ->register('wp.backend', function () {
106 'url' => Civi
::paths()->getVariable('wp.backend.base', 'url') . 'admin.php',
115 public function setTitle($title, $pageTitle = NULL) {
120 // FIXME: Why is this global?
121 global $civicrm_wp_title;
122 $civicrm_wp_title = $title;
124 // yes, set page title, depending on context
125 $context = civi_wp()->civicrm_context_get();
129 $template = CRM_Core_Smarty
::singleton();
130 $template->assign('pageTitle', $pageTitle);
135 * Moved from CRM_Utils_System_Base
137 public function getDefaultFileStorage() {
138 $config = CRM_Core_Config
::singleton();
139 $cmsUrl = CRM_Utils_System
::languageNegotiationURL($config->userFrameworkBaseURL
, FALSE, TRUE);
140 $cmsPath = $this->cmsRootPath();
141 $filesPath = CRM_Utils_File
::baseFilePath();
142 $filesRelPath = CRM_Utils_File
::relativize($filesPath, $cmsPath);
143 $filesURL = rtrim($cmsUrl, '/') . '/' . ltrim($filesRelPath, ' /');
145 'url' => CRM_Utils_File
::addTrailingSlash($filesURL, '/'),
146 'path' => CRM_Utils_File
::addTrailingSlash($filesPath),
151 * Determine the location of the CiviCRM source tree.
154 * - url: string. ex: "http://example.com/sites/all/modules/civicrm"
155 * - path: string. ex: "/var/www/sites/all/modules/civicrm"
157 public function getCiviSourceStorage() {
158 global $civicrm_root;
160 // Don't use $config->userFrameworkBaseURL; it has garbage on it.
161 // More generally, we shouldn't be using $config here.
162 if (!defined('CIVICRM_UF_BASEURL')) {
163 throw new RuntimeException('Undefined constant: CIVICRM_UF_BASEURL');
166 $cmsPath = $this->cmsRootPath();
168 // $config = CRM_Core_Config::singleton();
169 // overkill? // $cmsUrl = CRM_Utils_System::languageNegotiationURL($config->userFrameworkBaseURL, FALSE, TRUE);
170 $cmsUrl = CIVICRM_UF_BASEURL
;
171 if (CRM_Utils_System
::isSSL()) {
172 $cmsUrl = str_replace('http://', 'https://', $cmsUrl);
174 $civiRelPath = CRM_Utils_File
::relativize(realpath($civicrm_root), realpath($cmsPath));
175 $civiUrl = rtrim($cmsUrl, '/') . '/' . ltrim($civiRelPath, ' /');
177 'url' => CRM_Utils_File
::addTrailingSlash($civiUrl, '/'),
178 'path' => CRM_Utils_File
::addTrailingSlash($civicrm_root),
185 public function appendBreadCrumb($breadCrumbs) {
186 $breadCrumb = wp_get_breadcrumb();
188 if (is_array($breadCrumbs)) {
189 foreach ($breadCrumbs as $crumbs) {
190 if (stripos($crumbs['url'], 'id%%')) {
191 $args = ['cid', 'mid'];
192 foreach ($args as $a) {
193 $val = CRM_Utils_Request
::retrieve($a, 'Positive', CRM_Core_DAO
::$_nullObject,
197 $crumbs['url'] = str_ireplace("%%{$a}%%", $val, $crumbs['url']);
201 $breadCrumb[] = "<a href=\"{$crumbs['url']}\">{$crumbs['title']}</a>";
205 $template = CRM_Core_Smarty
::singleton();
206 $template->assign_by_ref('breadcrumb', $breadCrumb);
207 wp_set_breadcrumb($breadCrumb);
213 public function resetBreadCrumb() {
215 wp_set_breadcrumb($bc);
221 public function addHTMLHead($head) {
222 static $registered = FALSE;
225 add_action('wp_head', [__CLASS__
, '_showHTMLHead']);
227 add_action('admin_head', [__CLASS__
, '_showHTMLHead']);
229 CRM_Core_Region
::instance('wp_head')->add([
235 * WP action callback.
237 public static function _showHTMLHead() {
238 $region = CRM_Core_Region
::instance('wp_head', FALSE);
240 echo $region->render('');
247 public function mapConfigToSSL() {
249 $base_url = str_replace('http://', 'https://', $base_url);
261 $forceBackend = FALSE,
264 $config = CRM_Core_Config
::singleton();
268 $fragment = isset($fragment) ?
('#' . $fragment) : '';
270 $path = CRM_Utils_String
::stripPathChars($path);
273 //this means wp function we are trying to use is not available,
275 // FIXME: Why bootstrap in url()? Generally want to define 1-2 strategic places to put bootstrap
276 if (!function_exists('get_option')) {
277 $this->loadBootStrap();
280 if ($config->userFrameworkFrontend
) {
282 if (get_option('permalink_structure') != '') {
283 $script = get_permalink($post->ID
);
285 if ($config->wpBasePage
== $post->post_name
) {
288 // when shortcode is included in page
289 // also make sure we have valid query object
290 // FIXME: $wpPageParam has no effect and is only set on the *basepage*
292 if (get_option('permalink_structure') == '' && method_exists($wp_query, 'get')) {
293 if (get_query_var('page_id')) {
294 $wpPageParam = "page_id=" . get_query_var('page_id');
296 elseif (get_query_var('p')) {
297 // when shortcode is inserted in post
298 $wpPageParam = "p=" . get_query_var('p');
303 $base = $this->getBaseUrl($absolute, $frontend, $forceBackend);
305 if (!isset($path) && !isset($query)) {
306 // FIXME: This short-circuited codepath is the same as the general one below, except
307 // in that it ignores "permlink_structure" / $wpPageParam / $script . I don't know
308 // why it's different (and I can only find two obvious use-cases for this codepath,
309 // of which at least one looks gratuitous). A more ambitious person would simply remove
311 return $base . $fragment;
314 if (!$forceBackend && get_option('permalink_structure') != '' && ($wpPageParam ||
$script != '')) {
321 // not using clean URLs
323 // requesting an admin URL
324 ||
((is_admin() && !$frontend) ||
$forceBackend)
326 ||
(!$basepage && $script != '')
329 // pre-existing logic
331 // Admin URLs still need "page=CiviCRM", front-end URLs do not.
332 if ((is_admin() && !$frontend) ||
$forceBackend) {
333 $queryParts[] = 'page=CiviCRM';
336 $queryParts[] = 'civiwp=CiviCRM';
338 $queryParts[] = 'q=' . rawurlencode($path);
341 $queryParts[] = $wpPageParam;
343 if (!empty($query)) {
344 $queryParts[] = $query;
347 $final = $base . '?' . implode($separator, $queryParts) . $fragment;
354 $base = trailingslashit($base) . str_replace('civicrm/', '', $path) . '/';
357 $query = ltrim($query, '=?&');
358 $queryParts[] = $query;
361 if (!empty($queryParts)) {
362 $final = $base . '?' . implode($separator, $queryParts) . $fragment;
365 $final = $base . $fragment;
375 * CRM-16421 CRM-17633 WIP Changes to support WP in it's own directory
376 * https://wiki.civicrm.org/confluence/display/CRM/WordPress+installed+in+its+own+directory+issues
377 * For now leave hard coded wp-admin references.
378 * TODO: remove wp-admin references and replace with admin_url() in the future. Look at best way to get path to admin_url
382 * @param $forceBackend
384 * @return mixed|null|string
386 private function getBaseUrl($absolute, $frontend, $forceBackend) {
387 $config = CRM_Core_Config
::singleton();
388 if ((is_admin() && !$frontend) ||
$forceBackend) {
389 return Civi
::paths()->getUrl('[wp.backend]/.', $absolute ?
'absolute' : 'relative');
392 return Civi
::paths()->getUrl('[wp.frontend]/.', $absolute ?
'absolute' : 'relative');
399 public function authenticate($name, $password, $loadCMSBootstrap = FALSE, $realPath = NULL) {
400 $config = CRM_Core_Config
::singleton();
402 if ($loadCMSBootstrap) {
403 $config->userSystem
->loadBootStrap([
409 $user = wp_authenticate($name, $password);
410 if (is_a($user, 'WP_Error')) {
414 // TODO: need to change this to make sure we matched only one row
416 CRM_Core_BAO_UFMatch
::synchronizeUFMatch($user->data
, $user->data
->ID
, $user->data
->user_email
, 'WordPress');
417 $contactID = CRM_Core_BAO_UFMatch
::getContactId($user->data
->ID
);
421 return [$contactID, $user->data
->ID
, mt_rand()];
425 * FIXME: Do something
427 * @param string $message
429 public function setMessage($message) {
433 * @param \string $user
437 public function loadUser($user) {
438 $userdata = get_user_by('login', $user);
439 if (!$userdata->data
->ID
) {
443 $uid = $userdata->data
->ID
;
444 wp_set_current_user($uid);
445 $contactID = CRM_Core_BAO_UFMatch
::getContactId($uid);
447 // lets store contact id and user id in session
448 $session = CRM_Core_Session
::singleton();
449 $session->set('ufID', $uid);
450 $session->set('userID', $contactID);
455 * FIXME: Use CMS-native approach
456 * @throws \CRM_Core_Exception
458 public function permissionDenied() {
459 throw new CRM_Core_Exception(ts('You do not have permission to access this page.'));
463 * Determine the native ID of the CMS user.
465 * @param string $username
469 public function getUfId($username) {
470 $userdata = get_user_by('login', $username);
471 if (!$userdata->data
->ID
) {
474 return $userdata->data
->ID
;
480 public function logout() {
486 wp_redirect(wp_login_url());
492 public function getUFLocale() {
493 // Bail early if method is called when WordPress isn't bootstrapped.
494 // Additionally, the function checked here is located in pluggable.php
495 // and is required by wp_get_referer() - so this also bails early if it is
496 // called too early in the request lifecycle.
497 // @see https://core.trac.wordpress.org/ticket/25294
498 if (!function_exists('wp_validate_redirect')) {
502 // Default to WordPress User locale.
503 $locale = get_user_locale();
505 // Is this a "back-end" AJAX call?
507 if (wp_doing_ajax() && FALSE !== strpos(wp_get_referer(), admin_url())) {
511 // Ignore when in WordPress admin or it's a "back-end" AJAX call.
512 if (!(is_admin() ||
$is_backend)) {
514 // Reaching here means it is very likely to be a front-end context.
516 // Default to WordPress locale.
517 $locale = get_locale();
519 // Maybe override with the locale that Polylang reports.
520 if (function_exists('pll_current_language')) {
521 $pll_locale = pll_current_language('locale');
522 if (!empty($pll_locale)) {
523 $locale = $pll_locale;
527 // Maybe override with the locale that WPML reports.
528 elseif (defined('ICL_LANGUAGE_CODE')) {
529 $languages = apply_filters('wpml_active_languages', NULL);
530 foreach ($languages as $language) {
531 if ($language['active']) {
532 $locale = $language['default_locale'];
538 // TODO: Set locale for other WordPress plugins.
539 // @see https://wordpress.org/plugins/tags/multilingual/
540 // A hook would be nice here.
544 if (!empty($locale)) {
545 // If for some reason only we get a language code, convert it to a locale.
546 if (2 === strlen($locale)) {
547 $locale = CRM_Core_I18n_PseudoConstant
::longForShort($locale);
559 public function setUFLocale($civicrm_language) {
560 // TODO (probably not possible with WPML?)
565 * Load wordpress bootstrap.
567 * @param array $params
568 * Optional credentials
569 * - name: string, cms username
570 * - pass: string, cms password
571 * @param bool $loadUser
572 * @param bool $throwError
573 * @param mixed $realPath
576 * @throws \CRM_Core_Exception
578 public function loadBootStrap($params = [], $loadUser = TRUE, $throwError = TRUE, $realPath = NULL) {
579 global $wp, $wp_rewrite, $wp_the_query, $wp_query, $wpdb, $current_site, $current_blog, $current_user;
581 $name = $params['name'] ??
NULL;
582 $pass = $params['pass'] ??
NULL;
584 if (!defined('WP_USE_THEMES')) {
585 define('WP_USE_THEMES', FALSE);
588 $cmsRootPath = $this->cmsRootPath();
590 throw new CRM_Core_Exception("Could not find the install directory for WordPress");
592 $path = Civi
::settings()->get('wpLoadPhp');
596 elseif (file_exists($cmsRootPath . DIRECTORY_SEPARATOR
. 'wp-load.php')) {
597 require_once $cmsRootPath . DIRECTORY_SEPARATOR
. 'wp-load.php';
600 throw new CRM_Core_Exception("Could not find the bootstrap file for WordPress");
602 $wpUserTimezone = get_option('timezone_string');
603 if ($wpUserTimezone) {
604 date_default_timezone_set($wpUserTimezone);
605 CRM_Core_Config
::singleton()->userSystem
->setMySQLTimeZone();
607 require_once $cmsRootPath . DIRECTORY_SEPARATOR
. 'wp-includes/pluggable.php';
608 $uid = $params['uid'] ??
NULL;
610 $name = $name ?
$name : trim(CRM_Utils_Array
::value('name', $_REQUEST));
611 $pass = $pass ?
$pass : trim(CRM_Utils_Array
::value('pass', $_REQUEST));
613 $uid = wp_authenticate($name, $pass);
616 echo '<br />Sorry, unrecognized username or password.';
624 if ($uid instanceof WP_User
) {
625 $account = wp_set_current_user($uid->ID
);
628 $account = wp_set_current_user($uid);
630 if ($account && $account->data
->ID
) {
644 public function validInstallDir($dir) {
645 $includePath = "$dir/wp-includes";
646 if (@file_exists
("$includePath/version.php")) {
653 * Determine the location of the CMS root.
655 * @return string|NULL
656 * local file system path to CMS root, or NULL if it cannot be determined
658 public function cmsRootPath() {
660 // Return early if the path is already set.
661 global $civicrm_paths;
662 if (!empty($civicrm_paths['cms.root']['path'])) {
663 return $civicrm_paths['cms.root']['path'];
666 // Return early if constant has been defined.
667 if (defined('CIVICRM_CMSDIR')) {
668 if ($this->validInstallDir(CIVICRM_CMSDIR
)) {
669 return CIVICRM_CMSDIR
;
673 // Return early if path to wp-load.php can be retrieved from settings.
674 $setting = Civi
::settings()->get('wpLoadPhp');
675 if (!empty($setting)) {
676 $path = str_replace('wp-load.php', '', $setting);
677 $cmsRoot = rtrim($path, '/\\');
678 if ($this->validInstallDir($cmsRoot)) {
684 * Keep previous logic as fallback of last resort.
686 * At some point, it would be good to remove this because there are serious
687 * problems in correctly locating WordPress in this manner. In summary, it
688 * is impossible to do so reliably.
690 * @see https://github.com/civicrm/civicrm-wordpress/pull/63#issuecomment-61792328
691 * @see https://github.com/civicrm/civicrm-core/pull/11086#issuecomment-335454992
693 $cmsRoot = $valid = NULL;
695 $pathVars = explode('/', str_replace('\\', '/', $_SERVER['SCRIPT_FILENAME']));
697 // Might be Windows installation.
698 $firstVar = array_shift($pathVars);
700 $cmsRoot = $firstVar;
703 // Start with CMS dir search.
704 foreach ($pathVars as $var) {
706 if ($this->validInstallDir($cmsRoot)) {
707 // Stop as we found bootstrap.
713 return ($valid) ?
$cmsRoot : NULL;
719 public function createUser(&$params, $mail) {
722 'user_pass' => $params['cms_pass'],
723 'user_login' => $params['cms_name'],
724 'user_email' => $params[$mail],
725 'nickname' => $params['cms_name'],
726 'role' => get_option('default_role'),
728 if (isset($params['contactID'])) {
729 $contactType = CRM_Contact_BAO_Contact
::getContactType($params['contactID']);
730 if ($contactType == 'Individual') {
731 $user_data['first_name'] = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact',
732 $params['contactID'], 'first_name'
734 $user_data['last_name'] = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact',
735 $params['contactID'], 'last_name'
740 $uid = wp_insert_user($user_data);
743 $creds['user_login'] = $params['cms_name'];
744 $creds['user_password'] = $params['cms_pass'];
745 $creds['remember'] = TRUE;
746 $user = wp_signon($creds, FALSE);
748 wp_new_user_notification($uid, $user_data['user_pass']);
755 public function updateCMSName($ufID, $ufName) {
757 if (function_exists('wp_update_user')) {
758 $ufID = CRM_Utils_Type
::escape($ufID, 'Integer');
759 $ufName = CRM_Utils_Type
::escape($ufName, 'String');
761 $values = ['ID' => $ufID, 'user_email' => $ufName];
763 wp_update_user($values);
769 * @param array $params
771 * @param string $emailName
773 public function checkUserNameEmailExists(&$params, &$errors, $emailName = 'email') {
774 $config = CRM_Core_Config
::singleton();
776 $dao = new CRM_Core_DAO();
777 $name = $dao->escape(CRM_Utils_Array
::value('name', $params));
778 $email = $dao->escape(CRM_Utils_Array
::value('mail', $params));
780 if (!empty($params['name'])) {
781 if (!validate_username($params['name'])) {
782 $errors['cms_name'] = ts("Your username contains invalid characters");
784 elseif (username_exists(sanitize_user($params['name']))) {
785 $errors['cms_name'] = ts('The username %1 is already taken. Please select another username.', [1 => $params['name']]);
789 if (!empty($params['mail'])) {
790 if (!is_email($params['mail'])) {
791 $errors[$emailName] = "Your email is invaid";
793 elseif (email_exists($params['mail'])) {
794 $errors[$emailName] = ts('The email address %1 already has an account associated with it. <a href="%2">Have you forgotten your password?</a>',
795 [1 => $params['mail'], 2 => wp_lostpassword_url()]
804 public function isUserLoggedIn() {
806 if (function_exists('is_user_logged_in')) {
807 $isloggedIn = is_user_logged_in();
816 public function isUserRegistrationPermitted() {
817 if (!get_option('users_can_register')) {
826 public function isPasswordUserGenerated() {
833 public function getLoggedInUserObject() {
834 if (function_exists('is_user_logged_in') &&
837 global $current_user;
839 return $current_user;
845 public function getLoggedInUfID() {
847 $current_user = $this->getLoggedInUserObject();
848 return $current_user->ID ??
NULL;
854 public function getLoggedInUniqueIdentifier() {
855 $user = $this->getLoggedInUserObject();
856 return $this->getUniqueIdentifierFromUserObject($user);
860 * Get User ID from UserFramework system (Joomla)
861 * @param object $user
862 * Object as described by the CMS.
866 public function getUserIDFromUserObject($user) {
867 return !empty($user->ID
) ?
$user->ID
: NULL;
873 public function getUniqueIdentifierFromUserObject($user) {
874 return empty($user->user_email
) ?
NULL : $user->user_email
;
880 public function getLoginURL($destination = '') {
881 $config = CRM_Core_Config
::singleton();
882 $loginURL = wp_login_url();
887 * FIXME: Do something.
889 * @param \CRM_Core_Form $form
891 * @return NULL|string
893 public function getLoginDestination(&$form) {
900 public function getVersion() {
901 if (function_exists('get_bloginfo')) {
902 return get_bloginfo('version', 'display');
912 public function getTimeZoneString() {
913 return get_option('timezone_string');
919 public function getUserRecordUrl($contactID) {
920 $uid = CRM_Core_BAO_UFMatch
::getUFId($contactID);
921 if (CRM_Core_Session
::singleton()
922 ->get('userID') == $contactID || CRM_Core_Permission
::checkAnyPerm(['cms:administer users'])
924 return CRM_Core_Config
::singleton()->userFrameworkBaseURL
. "wp-admin/user-edit.php?user_id=" . $uid;
929 * Append WP js to coreResourcesList.
931 * @param \Civi\Core\Event\GenericHookEvent $e
933 public function appendCoreResources(\Civi\Core\Event\GenericHookEvent
$e) {
934 $e->list[] = 'js/crm.wordpress.js';
940 public function alterAssetUrl(\Civi\Core\Event\GenericHookEvent
$e) {
941 // Set menubar breakpoint to match WP admin theme
942 if ($e->asset
== 'crm-menubar.css') {
943 $e->params
['breakpoint'] = 783;
950 public function synchronizeUsers() {
951 $config = CRM_Core_Config
::singleton();
952 if (PHP_SAPI
!= 'cli') {
956 $mail = 'user_email';
958 $uf = $config->userFramework
;
961 $contactMatching = 0;
963 // Previously used the $wpdb global - which means WordPress *must* be bootstrapped.
964 $wpUsers = get_users(array(
965 'blog_id' => get_current_blog_id(),
969 foreach ($wpUsers as $wpUserData) {
971 if ($match = CRM_Core_BAO_UFMatch
::synchronizeUFMatch($wpUserData,
985 if (is_object($match)) {
991 'contactCount' => $contactCount,
992 'contactMatching' => $contactMatching,
993 'contactCreated' => $contactCreated,
998 * Send an HTTP Response base on PSR HTTP RespnseInterface response.
1000 * @param \Psr\Http\Message\ResponseInterface $response
1002 public function sendResponse(\Psr\Http\Message\ResponseInterface
$response) {
1003 // use WordPress function status_header to ensure 404 response is sent
1004 status_header($response->getStatusCode());
1005 foreach ($response->getHeaders() as $name => $values) {
1006 CRM_Utils_System
::setHttpHeader($name, implode(', ', (array) $values));
1008 echo $response->getBody();
1009 CRM_Utils_System
::civiExit();
1013 * Start a new session if there's no existing session ID.
1015 * Checks are needed to prevent sessions being started when not necessary.
1017 public function sessionStart() {
1018 $session_id = session_id();
1020 // Check WordPress pseudo-cron.
1022 if (function_exists('wp_doing_cron') && wp_doing_cron()) {
1028 if (defined('WP_CLI') && WP_CLI
) {
1032 // Check PHP on the command line - e.g. `cv`.
1034 if (PHP_SAPI
!== 'cli') {
1038 // Maybe start session.
1039 if (empty($session_id) && !$wp_cron && !$wp_cli && !$php_cli) {