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

Fail more gracefully when upgrading on PHP 5.x - 5.16 backport #162

Merged
merged 2 commits into from
Aug 21, 2019
Merged
Show file tree
Hide file tree
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
42 changes: 30 additions & 12 deletions civicrm.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,21 @@
define( 'CIVICRM_PLUGIN_DIR', plugin_dir_path(CIVICRM_PLUGIN_FILE) );
}

if ( !defined( 'CIVICRM_WP_PHP_MINIMUM' ) ) {
/**
* Minimum required PHP
*
* Note: This duplicates CRM_Upgrade_Form::MINIMUM_PHP_VERSION. The
* duplication helps avoid dependency issues. (Reading `Form::MINIMUM_PHP_VERSION`
* requires loading `civicrm.settings.php`, but that triggers a parse-error
* on PHP 5.x.)
*
* @see CRM_Upgrade_Form::MINIMUM_PHP_VERSION
* @see CiviWP\PhpVersionTest::testConstantMatch()
*/
define( 'CIVICRM_WP_PHP_MINIMUM', '7.0.0' );
}

/*
* The constant CIVICRM_SETTINGS_PATH is also defined in civicrm.config.php and
* may already have been defined there - e.g. by cron or external scripts.
Expand Down Expand Up @@ -817,6 +832,19 @@ public function query_vars( $query_vars ) {
// CiviCRM Initialisation
// ---------------------------------------------------------------------------

protected function assertPhpSupport() {
// Need to check this before bootstrapping - once we start bootstrapping, the error messages will become ugly.
if ( version_compare( PHP_VERSION, CIVICRM_WP_PHP_MINIMUM ) < 0 ) {
echo '<p>' .
sprintf(
__( 'CiviCRM requires PHP version %s or greater. You are running PHP version %s', 'civicrm' ),
CIVICRM_WP_PHP_MINIMUM,
PHP_VERSION
) .
'<p>';
exit();
}
}

/**
* Initialize CiviCRM.
Expand All @@ -836,18 +864,7 @@ public function initialize() {

if ( ! $initialized ) {

// Check for php version and ensure its greater than minPhpVersion
$minPhpVersion = '5.3.4';
if ( version_compare( PHP_VERSION, $minPhpVersion ) < 0 ) {
echo '<p>' .
sprintf(
__( 'CiviCRM requires PHP Version %s or greater. You are running PHP Version %s', 'civicrm' ),
$minPhpVersion,
PHP_VERSION
) .
'<p>';
exit();
}
$this->assertPhpSupport();

// Check for settings
if ( ! CIVICRM_INSTALLED ) {
Expand Down Expand Up @@ -1059,6 +1076,7 @@ public function add_menu_items() {
* @since 4.4
*/
public function run_installer() {
$this->assertPhpSupport();
$civicrmCore = CIVICRM_PLUGIN_DIR . 'civicrm';

$setupPaths = array(
Expand Down
34 changes: 34 additions & 0 deletions tests/phpunit/CiviWP/PhpVersionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace CiviWP;

use Civi\Test\EndToEndInterface;

class PhpVersionTest extends \PHPUnit_Framework_TestCase implements EndToEndInterface {

/**
* CIVICRM_WP_PHP_MINIMUM (civicrm.module) should match MINIMUM_PHP_VERSION (CRM/Upgrade/Form.php).
*
* The literal value should be duplicated in the define() to prevent dependency issues.
*/
public function testConstantMatch() {
$constantFile = $this->getModulePath() . '/civicrm.php';
$this->assertFileExists($constantFile);
$content = file_get_contents($constantFile);
if (preg_match(";define\\(\\s*'CIVICRM_WP_PHP_MINIMUM'\\s*,\\s*'(.*)'\\s*\\);", $content, $m)) {
$this->assertEquals(\CRM_Upgrade_Form::MINIMUM_PHP_VERSION, $m[1]);
}
else {
$this->fail('Failed to find CIVICRM_WP_PHP_MINIMUM in ' . $constantFile);
}
}

/**
* @return string
* Ex: '/var/www/wp-content/plugins/civicrm'
*/
protected function getModulePath() {
return dirname(dirname(dirname(__DIR__)));
}

}