diff --git a/CRM/Utils/System/WordPress.php b/CRM/Utils/System/WordPress.php index 873020ce537cd96b2fb6c4a5252ef89872be99c0..d844e98b45aa880c8cf8c244224ff6da5698ed93 100644 --- a/CRM/Utils/System/WordPress.php +++ b/CRM/Utils/System/WordPress.php @@ -35,6 +35,87 @@ class CRM_Utils_System_WordPress extends CRM_Utils_System_Base { $this->is_wordpress = TRUE; } + public function initialize() { + parent::initialize(); + $this->registerPathVars(); + } + + /** + * Specify the default computation for various paths/URLs. + */ + protected function registerPathVars():void { + $isNormalBoot = function_exists('get_option'); + if ($isNormalBoot) { + // Normal mode - CMS boots first, then calls Civi. "Normal" web pages and newer extern routes. + // To simplify the code-paths, some items are re-registered with WP-specific functions. + $cmsRoot = function() { + return [ + 'path' => untrailingslashit(ABSPATH), + 'url' => home_url(), + ]; + }; + Civi::paths()->register('cms', $cmsRoot); + Civi::paths()->register('cms.root', $cmsRoot); + Civi::paths()->register('civicrm.files', function () { + $upload_dir = wp_get_upload_dir(); + return [ + 'path' => $upload_dir['basedir'] . DIRECTORY_SEPARATOR . 'civicrm' . DIRECTORY_SEPARATOR, + 'url' => $upload_dir['baseurl'] . '/civicrm/', + ]; + }); + Civi::paths()->register('civicrm.root', function () { + return [ + 'path' => CIVICRM_PLUGIN_DIR . 'civicrm' . DIRECTORY_SEPARATOR, + 'url' => CIVICRM_PLUGIN_URL . 'civicrm/', + ]; + }); + Civi::paths()->register('wp.frontend.base', function () { + return [ + 'url' => home_url('/'), + ]; + }); + Civi::paths()->register('wp.frontend', function () { + $config = CRM_Core_Config::singleton(); + $basepage = get_page_by_path($config->wpBasePage); + return [ + 'url' => get_permalink($basepage->ID), + ]; + }); + Civi::paths()->register('wp.backend.base', function () { + return [ + 'url' => admin_url(), + ]; + }); + Civi::paths()->register('wp.backend', function() { + return [ + 'url' => admin_url('admin.php'), + ]; + }); + } + else { + // Legacy support - only relevant for older extern routes. + Civi::paths() + ->register('wp.frontend.base', function () { + return ['url' => rtrim(CIVICRM_UF_BASEURL, '/') . '/']; + }) + ->register('wp.frontend', function () { + $config = \CRM_Core_Config::singleton(); + $suffix = defined('CIVICRM_UF_WP_BASEPAGE') ? CIVICRM_UF_WP_BASEPAGE : $config->wpBasePage; + return [ + 'url' => Civi::paths()->getVariable('wp.frontend.base', 'url') . $suffix, + ]; + }) + ->register('wp.backend.base', function () { + return ['url' => rtrim(CIVICRM_UF_BASEURL, '/') . '/wp-admin/']; + }) + ->register('wp.backend', function () { + return [ + 'url' => Civi::paths()->getVariable('wp.backend.base', 'url') . 'admin.php', + ]; + }); + } + } + /** * @inheritDoc */ diff --git a/Civi/Core/Paths.php b/Civi/Core/Paths.php index d0d15814b3671b2cf202d741ae920c083b611b05..89b433679a90b42125d156ac864be853b0415b07 100644 --- a/Civi/Core/Paths.php +++ b/Civi/Core/Paths.php @@ -30,7 +30,10 @@ class Paths { * Class constructor. */ public function __construct() { - $paths = $this; + // Below is a *default* set of functions to calculate paths/URLs. + // Some variables may be overridden as follow: + // - The global `$civicrm_paths` may be preset before Civi boots. (Ex: via `civicrm.settings.php`, `settings.php`, or `vendor/autoload.php`) + // - Variables may be re-registered. (Ex: via `CRM_Utils_System_WordPress`) $this ->register('civicrm.root', function () { return \CRM_Core_Config::singleton()->userSystem->getCiviSourceStorage(); @@ -84,24 +87,6 @@ class Paths { 'path' => is_dir($dir) ? $dir : \Civi::paths()->getPath('[civicrm.root]/l10n'), ]; }) - ->register('wp.frontend.base', function () { - return ['url' => rtrim(CIVICRM_UF_BASEURL, '/') . '/']; - }) - ->register('wp.frontend', function () use ($paths) { - $config = \CRM_Core_Config::singleton(); - $suffix = defined('CIVICRM_UF_WP_BASEPAGE') ? CIVICRM_UF_WP_BASEPAGE : $config->wpBasePage; - return [ - 'url' => $paths->getVariable('wp.frontend.base', 'url') . $suffix, - ]; - }) - ->register('wp.backend.base', function () { - return ['url' => rtrim(CIVICRM_UF_BASEURL, '/') . '/wp-admin/']; - }) - ->register('wp.backend', function () use ($paths) { - return [ - 'url' => $paths->getVariable('wp.backend.base', 'url') . 'admin.php', - ]; - }) ->register('cms', function () { return [ 'path' => \CRM_Core_Config::singleton()->userSystem->cmsRootPath(),