diff --git a/CRM/Utils/Check/Component/Cleanurls.php b/CRM/Utils/Check/Component/Cleanurls.php new file mode 100644 index 000000000000..b3b5293452a5 --- /dev/null +++ b/CRM/Utils/Check/Component/Cleanurls.php @@ -0,0 +1,28 @@ +userSystem->checkCleanurls(); + } + +} diff --git a/CRM/Utils/System/Base.php b/CRM/Utils/System/Base.php index d2e5fa93bbe6..9426bf28adee 100644 --- a/CRM/Utils/System/Base.php +++ b/CRM/Utils/System/Base.php @@ -1110,4 +1110,8 @@ public function shouldExitAfterFatal() { return TRUE; } + public function checkCleanurls() { + return []; + } + } diff --git a/CRM/Utils/System/WordPress.php b/CRM/Utils/System/WordPress.php index 928814f6d6d2..4a73712e7572 100644 --- a/CRM/Utils/System/WordPress.php +++ b/CRM/Utils/System/WordPress.php @@ -1509,4 +1509,108 @@ public function shouldExitAfterFatal() { 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 = 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 = '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.'; + + 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); + $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__, + 'Could not load a clean page to check', + ts('WordPress Permalinks cache needs to be refreshed '), + \Psr\Log\LogLevel::ERROR, + 'fa-wordpress' + ), + ]; + } + + if ($httpCode == 404) { + $warning = 'Go to Settings > Permalinks and click "Save".'; + + $message = new CRM_Utils_Check_Message( + __FUNCTION__, + $warning, + ts('Wordpress Permalinks cache needs to be refreshed.'), + \Psr\Log\LogLevel::WARNING, + 'fa-wordpress', + ); + $message->addAction( + 'Open WordPress settings', + FALSE, + 'href', + ['path' => get_admin_url(NULL, 'options-permalink.php')], + 'fa-wordpress', + ); + + return [$message]; + } + + //sanity + return []; + + } + }