Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent session from starting during WordPress pseudo-cron procedures #210

Merged
merged 3 commits into from
Jul 18, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 49 additions & 10 deletions civicrm.php
Original file line number Diff line number Diff line change
Expand Up @@ -360,16 +360,8 @@ public function setup_instance() {
wp_die( __( 'Only one instance of CiviCRM_For_WordPress please', 'civicrm' ) );
}

// Get existing session ID
$session_id = session_id();

/*
* There is no session handling in WP - hence we start it for CiviCRM pages
* except when running via WP-CLI which does not require sessions.
*/
if ( empty( $session_id ) && ! ( defined( 'WP_CLI' ) && WP_CLI ) ) {
session_start();
}
// Maybe start session.
$this->maybe_start_session();

/*
* AJAX calls do not set the 'cms.root' item, so make sure it is set here so
Expand Down Expand Up @@ -405,6 +397,53 @@ public function setup_instance() {
}


/**
* Maybe start a session for CiviCRM.
*
* There is no session handling in WordPress so start it for CiviCRM pages.
*
* Not needed when running:
*
* - via WP-CLI
* - via wp-cron.php
* - via PHP on the command line
*
* none of which require sessions.
*
*
* @since 5.28
*/
public function maybe_start_session() {

// Get existing session ID
$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();
}

}


/**
* Set broad CiviCRM context.
*
Expand Down