Skip to content

Commit

Permalink
Add system check for clean URL configuration
Browse files Browse the repository at this point in the history
Move per demerit's feedback

Add action button, link to WordPress Permalinks page.

resolve style warnings
  • Loading branch information
elisseck committed Sep 19, 2022
1 parent 0217f12 commit d04cf40
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 0 deletions.
28 changes: 28 additions & 0 deletions CRM/Utils/Check/Component/Cleanurls.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/*
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC. All rights reserved. |
| |
| This work is published under the GNU AGPLv3 license with some |
| permitted exceptions and without any warranty. For full license |
| and copyright information, see https://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/

/**
*
* @package CRM
* @copyright CiviCRM LLC https://civicrm.org/licensing
*/
class CRM_Utils_Check_Component_Cleanurls 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 checkWpCleanurls() {
return CRM_Core_Config::singleton()->userSystem->checkCleanurls();
}

}
4 changes: 4 additions & 0 deletions CRM/Utils/System/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -1110,4 +1110,8 @@ public function shouldExitAfterFatal() {
return TRUE;
}

public function checkCleanurls() {
return [];
}

}
104 changes: 104 additions & 0 deletions CRM/Utils/System/WordPress.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <a href="https://docs.civicrm.org/sysadmin/en/latest/integration/wordpress/clean-urls/">the documentation</a> 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 [];

}

}

0 comments on commit d04cf40

Please sign in to comment.