From b07855e9eea851ee6294867da32ccb9145f82cb2 Mon Sep 17 00:00:00 2001 From: Christian Wach Date: Sat, 18 Jul 2020 21:08:50 +0100 Subject: [PATCH] Prevent session from starting during WordPress pseudo-cron procedures --- CRM/Utils/System/WordPress.php | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/CRM/Utils/System/WordPress.php b/CRM/Utils/System/WordPress.php index a4c79c7193..7010dafa50 100644 --- a/CRM/Utils/System/WordPress.php +++ b/CRM/Utils/System/WordPress.php @@ -1016,4 +1016,36 @@ class CRM_Utils_System_WordPress extends CRM_Utils_System_Base { CRM_Utils_System::civiExit(); } + /** + * Start a new session if there's no existing session ID. + * + * Checks are needed to prevent sessions being started when not necessary. + */ + public function sessionStart() { + $session_id = session_id(); + + // Check WordPress pseudo-cron. + $wp_cron = FALSE; + if (function_exists('wp_doing_cron') && wp_doing_cron()) { + $wp_cron = TRUE; + } + + // Check WP-CLI. + $wp_cli = FALSE; + if (defined('WP_CLI') && WP_CLI) { + $wp_cli = TRUE; + } + + // Check PHP on the command line - e.g. `cv`. + $php_cli = TRUE; + if (PHP_SAPI !== 'cli') { + $php_cli = FALSE; + } + + // Maybe start session. + if (empty($session_id) && !$wp_cron && !$wp_cli && !$php_cli) { + session_start(); + } + } + } -- 2.25.1