Add system check to ensure WP base page exists
authorAndrew Hunt <andrew@aghstrategies.com>
Thu, 25 Jun 2020 15:51:19 +0000 (11:51 -0400)
committerAndrew Hunt <andrew@aghstrategies.com>
Thu, 25 Jun 2020 15:51:19 +0000 (11:51 -0400)
CRM/Utils/Check/Component/Cms.php [new file with mode: 0644]

diff --git a/CRM/Utils/Check/Component/Cms.php b/CRM/Utils/Check/Component/Cms.php
new file mode 100644 (file)
index 0000000..99cff8e
--- /dev/null
@@ -0,0 +1,100 @@
+<?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_Cms extends CRM_Utils_Check_Component {
+
+  /**
+   * For sites running in WordPress, make sure the configured base page exists.
+   *
+   * @return array
+   *   Instances of CRM_Utils_Check_Message
+   */
+  public static function checkWpBasePage() {
+    $config = CRM_Core_Config::singleton();
+    if ($config->userFramework != 'WordPress') {
+      return [];
+    }
+    $messages = [];
+
+    $slug = $config->wpBasePage;
+    $pageArgs = [
+      'name' => $slug,
+      'post_type' => 'page',
+      'post_status' => 'publish',
+      'numberposts' => 1,
+    ];
+    $basePage = get_posts($pageArgs);
+    if (!$basePage) {
+      $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,
+          ]
+        ),
+      ];
+      if ($slug == 'civicrm') {
+        $messageText[] = ts(
+          'If you have an alternative base page, it can be set in the <a href="%2">WordPress integration settings</a>.',
+          [
+            1 => $config->userFrameworkBaseURL,
+            2 => $cmsSettings,
+          ]
+        );
+      }
+      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]
+          );
+        }
+        else {
+          $messageText[] = ts(
+            'The default is %1civicrm, but that does not exist on this site either.',
+            [1 => $config->userFrameworkBaseURL]
+          );
+        }
+        $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(
+        __FUNCTION__,
+        implode(' ', $messageText),
+        ts('WordPress Base Page Missing'),
+        \Psr\Log\LogLevel::ERROR,
+        'fa-wordpress'
+      );
+    }
+
+    return $messages;
+  }
+
+}