From 9dc5d3c23749338dd384e642ac2bbfb9d4a4e7c6 Mon Sep 17 00:00:00 2001 From: Andrew Hunt Date: Thu, 9 Jul 2020 11:12:41 -0400 Subject: [PATCH] WP base page system check: suggest both the modified default and the default default --- CRM/Utils/Check/Component/Cms.php | 113 +++++++++++++++++++++--------- 1 file changed, 80 insertions(+), 33 deletions(-) diff --git a/CRM/Utils/Check/Component/Cms.php b/CRM/Utils/Check/Component/Cms.php index 578ba73f29..663d68037c 100644 --- a/CRM/Utils/Check/Component/Cms.php +++ b/CRM/Utils/Check/Component/Cms.php @@ -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 base page in WordPress, 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 WordPress integration settings.', [ @@ -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 does exist on this site.', - [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 does exist on this site.', + [ + 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 WordPress integration settings.', [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'); } } -- 2.25.1