Skip to content

Commit

Permalink
Reinstate traversal as "method of last resort" to find WordPress
Browse files Browse the repository at this point in the history
  • Loading branch information
christianwach committed Nov 22, 2019
1 parent f28f315 commit 02bfbc7
Showing 1 changed file with 41 additions and 6 deletions.
47 changes: 41 additions & 6 deletions CRM/Utils/System/WordPress.php
Original file line number Diff line number Diff line change
Expand Up @@ -545,23 +545,58 @@ public function validInstallDir($dir) {
* local file system path to CMS root, or NULL if it cannot be determined
*/
public function cmsRootPath() {

// Return early if the path is already set.
global $civicrm_paths;
if (!empty($civicrm_paths['cms.root']['path'])) {
return $civicrm_paths['cms.root']['path'];
}

$cmsRoot = $valid = NULL;
// Return early if constant has been defined.
if (defined('CIVICRM_CMSDIR')) {
if ($this->validInstallDir(CIVICRM_CMSDIR)) {
$cmsRoot = CIVICRM_CMSDIR;
$valid = TRUE;
return CIVICRM_CMSDIR;
}
}
else {
$setting = Civi::settings()->get('wpLoadPhp');

// Return early if path to wp-load.php can be retrieved from settings.
$setting = Civi::settings()->get('wpLoadPhp');
if (!empty($setting)) {
$path = str_replace('wp-load.php', '', $setting);
$cmsRoot = rtrim($path, '/\\');
$valid = TRUE;
if ($this->validInstallDir($cmsRoot)) {
return $cmsRoot;
}
}

/*
* Keep previous logic as fallback of last resort.
*
* At some point, it would be good to remove this because there are serious
* problems in correctly locating WordPress in this manner. In summary, it
* is impossible to do so reliably.
*
* @see https://github.com/civicrm/civicrm-wordpress/pull/63#issuecomment-61792328
* @see https://github.com/civicrm/civicrm-core/pull/11086#issuecomment-335454992
*/
$cmsRoot = $valid = NULL;

$pathVars = explode('/', str_replace('\\', '/', $_SERVER['SCRIPT_FILENAME']));

// Might be Windows installation.
$firstVar = array_shift($pathVars);
if ($firstVar) {
$cmsRoot = $firstVar;
}

// Start with CMS dir search.
foreach ($pathVars as $var) {
$cmsRoot .= "/$var";
if ($this->validInstallDir($cmsRoot)) {
// Stop as we found bootstrap.
$valid = TRUE;
break;
}
}

return ($valid) ? $cmsRoot : NULL;
Expand Down

0 comments on commit 02bfbc7

Please sign in to comment.