Add system check for clean URL configuration
authorEli Lisseck <eli@aghstrategies.com>
Wed, 7 Sep 2022 18:44:21 +0000 (11:44 -0700)
committerEli Lisseck <eli@aghstrategies.com>
Tue, 20 Sep 2022 22:37:20 +0000 (15:37 -0700)
Move per demerit's feedback

Add action button, link to WordPress Permalinks page.

resolve style warnings

CRM/Utils/Check/Component/Cms.php
CRM/Utils/System/Base.php
CRM/Utils/System/WordPress.php

index 397d956f21c66119bbac7d57f0ccac50427e29e4..1c4c7a41dd237ee0b001bfe395d3590dff7116eb 100644 (file)
@@ -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.
    *
index d2e5fa93bbe66fa9d5e0306cf99e829529a8558c..9426bf28adee68d0bd05862bd9a8e3fc9d4f2889 100644 (file)
@@ -1110,4 +1110,8 @@ abstract class CRM_Utils_System_Base {
     return TRUE;
   }
 
+  public function checkCleanurls() {
+    return [];
+  }
+
 }
index 928814f6d6d2900f59728dbb9eef1216a5462da4..e905bb003efcd6c045798725737060ceaca4768b 100644 (file)
@@ -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 <a %1>the documentation</a> 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('<a %1>Click here to go to Settings > Permalinks, then click "Save" to refresh the cache.</a>', [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 [];
+
+  }
+
 }