WP base page system check: suggest both the modified default and the default default
authorAndrew Hunt <andrew@aghstrategies.com>
Thu, 9 Jul 2020 15:12:41 +0000 (11:12 -0400)
committerAndrew Hunt <andrew@aghstrategies.com>
Thu, 9 Jul 2020 15:12:41 +0000 (11:12 -0400)
CRM/Utils/Check/Component/Cms.php

index 578ba73f29a7f579e68a6fb571b0e4789b5daa5a..663d68037c0532c378f06617f0fb699e98769cf5 100644 (file)
@@ -33,42 +33,53 @@ class CRM_Utils_Check_Component_Cms extends CRM_Utils_Check_Component {
       // now.
       return [];
     }
-    $messages = [];
 
-    $slug = $config->wpBasePage;
-    $basePage = get_page_by_path($slug);
-    if (!$basePage || $basePage->post_status != 'publish') {
-      $cmsSettings = CRM_Utils_System::url(
-        'civicrm/admin/setting',
-        $query = ['reset' => 1],
-        FALSE,
-        NULL,
-        TRUE,
-        FALSE,
-        TRUE
-      );
-      if ($basePage) {
+    switch (self::pageExists($config->wpBasePage)) {
+      case 1:
+        // Page is here and published
+        return [];
+
+      case 0:
         $messageText = [
           ts(
             'CiviCRM relies upon a <a href="%1%2">base page in WordPress</a>, but it is not published.',
             [
               1 => $config->userFrameworkBaseURL,
-              2 => $slug,
+              2 => $config->wpBasePage,
             ]
           ),
         ];
-      }
-      else {
+        break;
+
+      case -1:
+        // Page is missing, but let's look around to see if the default is there
+        // --either the default as modified by civicrm_basepage_slug or the
+        // default default, `civicrm`.
+        $cmsSettings = CRM_Utils_System::url(
+          'civicrm/admin/setting',
+          $query = ['reset' => 1],
+          FALSE,
+          NULL,
+          TRUE,
+          FALSE,
+          TRUE
+        );
         $messageText = [
           ts(
             'CiviCRM relies upon a base page in WordPress at %1%2, but it is missing.',
             [
               1 => $config->userFrameworkBaseURL,
-              2 => $slug,
+              2 => $config->wpBasePage,
             ]
           ),
         ];
-        if ($slug == 'civicrm') {
+
+        $altSlugs = array_unique([
+          apply_filters('civicrm_basepage_slug', 'civicrm'),
+          'civicrm',
+        ]);
+
+        if (in_array($config->wpBasePage, $altSlugs)) {
           $messageText[] = ts(
             'If you have an alternative base page, it can be set in the <a href="%2">WordPress integration settings</a>.',
             [
@@ -78,36 +89,72 @@ class CRM_Utils_Check_Component_Cms extends CRM_Utils_Check_Component {
           );
         }
         else {
-          $pageArgs['name'] = 'civicrm';
-          $defaultBasePage = get_posts($pageArgs);
-          if ($defaultBasePage) {
-            $messageText[] = ts(
-              'The default is %1civicrm, which <a href="%1civicrm">does exist on this site</a>.',
-              [1 => $config->userFrameworkBaseURL]
-            );
+          foreach ($altSlugs as $slug) {
+            $exists = self::pageExists($slug);
+            if ($exists >= 0) {
+              // One of the possible defaults is here, published or not.
+              $messageText[] = ts(
+                'The default is %1%2, which <a href="%1%2">does exist on this site</a>.',
+                [
+                  1 => $config->userFrameworkBaseURL,
+                  2 => $slug,
+                ]
+              );
+              if ($exists == 0) {
+                $messageText[] = ts('However, it is not published.');
+              }
+              // We've found one, and if the `civicrm_basepage_slug` filter has
+              // modified the default, we should go with it.
+              break;
+            }
           }
-          else {
+          if ($exists == -1) {
+            // We went through the default(s) and couldn't find one.  Defer to
+            // the one modified by the filter.
             $messageText[] = ts(
-              'The default is %1civicrm, but that does not exist on this site either.',
-              [1 => $config->userFrameworkBaseURL]
+              'The default is %1%2, but that does not exist on this site either.',
+              [
+                1 => $config->userFrameworkBaseURL,
+                2 => $altSlugs[0],
+              ]
             );
           }
+
           $messageText[] = ts(
             'You can set the correct base page in the <a href="%1">WordPress integration settings</a>.',
             [1 => $cmsSettings]
           );
         }
-      }
-      $messages[] = new CRM_Utils_Check_Message(
+    }
+
+    return [
+      new CRM_Utils_Check_Message(
         __FUNCTION__,
         implode(' ', $messageText),
         ts('WordPress Base Page Missing'),
         \Psr\Log\LogLevel::ERROR,
         'fa-wordpress'
-      );
+      ),
+    ];
+  }
+
+  /**
+   * See if a page exists and is published.
+   *
+   * @param string $slug
+   *   The page path.
+   * @return int
+   *   -1 if it's missing
+   *   0 if it's present but not published
+   *   1 if it's present and published
+   */
+  private static function pageExists($slug) {
+    $basePage = get_page_by_path($slug);
+    if (!$basePage) {
+      return -1;
     }
 
-    return $messages;
+    return (int) ($basePage->post_status == 'publish');
   }
 
 }