From cbcea42e177b397ccec4ab6e3df583b3ed39ce0a Mon Sep 17 00:00:00 2001 From: Eli Lisseck Date: Wed, 7 Sep 2022 11:44:21 -0700 Subject: [PATCH] Add system check for clean URL configuration Move per demerit's feedback Add action button, link to WordPress Permalinks page. resolve style warnings --- CRM/Utils/Check/Component/Cms.php | 9 +++ CRM/Utils/System/Base.php | 4 ++ CRM/Utils/System/WordPress.php | 99 +++++++++++++++++++++++++++++++ 3 files changed, 112 insertions(+) diff --git a/CRM/Utils/Check/Component/Cms.php b/CRM/Utils/Check/Component/Cms.php index 397d956f21..1c4c7a41dd 100644 --- a/CRM/Utils/Check/Component/Cms.php +++ b/CRM/Utils/Check/Component/Cms.php @@ -133,6 +133,15 @@ class CRM_Utils_Check_Component_Cms extends CRM_Utils_Check_Component { ]; } + /** + * For sites running in WordPress, make sure clean URLs are properly set in settings file. + * + * @return CRM_Utils_Check_Message[] + */ + public static function checkCleanurls() { + return CRM_Core_Config::singleton()->userSystem->checkCleanurls(); + } + /** * See if a page exists and is published. * diff --git a/CRM/Utils/System/Base.php b/CRM/Utils/System/Base.php index d2e5fa93bb..9426bf28ad 100644 --- a/CRM/Utils/System/Base.php +++ b/CRM/Utils/System/Base.php @@ -1110,4 +1110,8 @@ abstract class CRM_Utils_System_Base { return TRUE; } + public function checkCleanurls() { + return []; + } + } diff --git a/CRM/Utils/System/WordPress.php b/CRM/Utils/System/WordPress.php index 928814f6d6..e905bb003e 100644 --- a/CRM/Utils/System/WordPress.php +++ b/CRM/Utils/System/WordPress.php @@ -1509,4 +1509,103 @@ class CRM_Utils_System_WordPress extends CRM_Utils_System_Base { return apply_filters('civicrm_exit_after_fatal', $ret); } + /** + * Make sure clean URLs are properly set in settings file. + * + * @return CRM_Utils_Check_Message[] + */ + public function checkCleanurls() { + $config = CRM_Core_Config::singleton(); + $clean = 0; + if (defined('CIVICRM_CLEANURL')) { + $clean = CIVICRM_CLEANURL; + } + if ($clean == 1) { + //cleanURLs are enabled in CiviCRM, let's make sure the wordpress permalink settings and cache are actually correct by checking the first active contribution page + $contributionPages = \Civi\Api4\ContributionPage::get(FALSE) + ->addSelect('id') + ->addWhere('is_active', '=', TRUE) + ->setLimit(1) + ->execute(); + if (count($contributionPages) > 0) { + $activePageId = $contributionPages[0]['id']; + $message = self::checkCleanPage('/contribute/transact/?reset=1&id=', $activePageId, $config); + + return $message; + } + else { + //no active contribution pages, we can check an event page. This probably won't ever happen. + $eventPages = \Civi\Api4\Event::get(FALSE) + ->addSelect('id') + ->addWhere('is_active', '=', TRUE) + ->setLimit(1) + ->execute(); + if (count($eventPages) > 0) { + $activePageId = $eventPages[0]['id']; + $message = self::checkCleanPage('/event/info/?reset=1&id=', $activePageId, $config); + + return $message; + } + else { + //If there are no active event or contribution pages, we'll skip this check for now. + + return []; + } + } + } + else { + //cleanURLs aren't enabled or aren't defined correctly in CiviCRM, admin should check civicrm.settings.php + $warning = ts('Clean URLs are not enabled correctly in CiviCRM. This can lead to "valid id" errors for users registering for events or making donations. Check civicrm.settings.php and review the documentation for more information.', [1 => 'href="' . CRM_Utils_System::docURL2('sysadmin/integration/wordpress/clean-urls/', TRUE) . '"']); + + return [ + new CRM_Utils_Check_Message( + __FUNCTION__, + $warning, + ts('Clean URLs Not Enabled'), + \Psr\Log\LogLevel::WARNING, + 'fa-wordpress' + ), + ]; + } + } + + private static function checkCleanPage($slug, $id, $config) { + $page = $config->userFrameworkBaseURL . $config->wpBasePage . $slug . $id; + try { + $client = new \GuzzleHttp\Client(); + $res = $client->head($page, ['http_errors' => FALSE]); + $httpCode = $res->getStatusCode(); + } + catch (Exception $e) { + Civi::log()->error("Could not run " . __FUNCTION__ . " on $page. GuzzleHttp\Client returned " . $e->getMessage()); + + return [ + new CRM_Utils_Check_Message( + __FUNCTION__, + ts('Could not load a clean page to check'), + ts('Guzzle client error'), + \Psr\Log\LogLevel::ERROR, + 'fa-wordpress' + ), + ]; + } + + if ($httpCode == 404) { + $warning = ts('Click here to go to Settings > Permalinks, then click "Save" to refresh the cache.', [1 => 'href="' . get_admin_url(NULL, 'options-permalink.php') . '"']); + $message = new CRM_Utils_Check_Message( + __FUNCTION__, + $warning, + ts('Wordpress Permalinks cache needs to be refreshed.'), + \Psr\Log\LogLevel::WARNING, + 'fa-wordpress' + ); + + return [$message]; + } + + //sanity + return []; + + } + } -- 2.25.1