diff --git a/civicrm.php b/civicrm.php index b96edad..244d04c 100644 --- a/civicrm.php +++ b/civicrm.php @@ -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. @@ -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 '

' . + sprintf( + __( 'CiviCRM requires PHP version %s or greater. You are running PHP version %s', 'civicrm' ), + CIVICRM_WP_PHP_MINIMUM, + PHP_VERSION + ) . + '

'; + exit(); + } + } /** * Initialize CiviCRM. @@ -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 '

' . - sprintf( - __( 'CiviCRM requires PHP Version %s or greater. You are running PHP Version %s', 'civicrm' ), - $minPhpVersion, - PHP_VERSION - ) . - '

'; - exit(); - } + $this->assertPhpSupport(); // Check for settings if ( ! CIVICRM_INSTALLED ) { @@ -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( diff --git a/tests/phpunit/CiviWP/PhpVersionTest.php b/tests/phpunit/CiviWP/PhpVersionTest.php new file mode 100644 index 0000000..42eab3e --- /dev/null +++ b/tests/phpunit/CiviWP/PhpVersionTest.php @@ -0,0 +1,34 @@ +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__))); + } + +}