Check if $post defined before trying to use its field values.
[civicrm-core.git] / CRM / Utils / System / WordPress.php
index a4c79c71933ad8658094395ed31545d07b969b3a..f3691eff7c04c085ac6bb9a40fcb921e2a65af6b 100644 (file)
@@ -13,8 +13,6 @@
  *
  * @package CRM
  * @copyright CiviCRM LLC https://civicrm.org/licensing
- * $Id$
- *
  */
 
 /**
@@ -56,13 +54,6 @@ class CRM_Utils_System_WordPress extends CRM_Utils_System_Base {
       };
       Civi::paths()->register('cms', $cmsRoot);
       Civi::paths()->register('cms.root', $cmsRoot);
-      Civi::paths()->register('civicrm.files', function () {
-        $upload_dir = wp_get_upload_dir();
-        return [
-          'path' => $upload_dir['basedir'] . DIRECTORY_SEPARATOR . 'civicrm' . DIRECTORY_SEPARATOR,
-          'url' => $upload_dir['baseurl'] . '/civicrm/',
-        ];
-      });
       Civi::paths()->register('civicrm.root', function () {
         return [
           'path' => CIVICRM_PLUGIN_DIR . 'civicrm' . DIRECTORY_SEPARATOR,
@@ -91,6 +82,44 @@ class CRM_Utils_System_WordPress extends CRM_Utils_System_Base {
           'url' => admin_url('admin.php'),
         ];
       });
+      Civi::paths()->register('civicrm.files', function () {
+        $upload_dir = wp_get_upload_dir();
+
+        $old = CRM_Core_Config::singleton()->userSystem->getDefaultFileStorage();
+        $new = [
+          'path' => $upload_dir['basedir'] . DIRECTORY_SEPARATOR . 'civicrm' . DIRECTORY_SEPARATOR,
+          'url' => $upload_dir['baseurl'] . '/civicrm/',
+        ];
+
+        if ($old['path'] === $new['path']) {
+           return $new;
+        }
+
+        $oldExists = file_exists($old['path']);
+        $newExists = file_exists($new['path']);
+
+        if ($oldExists && !$newExists) {
+          return $old;
+        }
+        elseif (!$oldExists && $newExists) {
+          return $new;
+        }
+        elseif (!$oldExists && !$newExists) {
+          // neither exists. but that's ok. we're in one of these two cases:
+          // - we're just starting installation... which will get sorted in a moment
+          //   when someone calls mkdir().
+          // - we're running a bespoke setup... which will get sorted in a moment
+          //   by applying $civicrm_paths.
+          return $new;
+        }
+        elseif ($oldExists && $newExists) {
+          // situation ambiguous. encourage admin to set value explicitly.
+          if (!isset($GLOBALS['civicrm_paths']['civicrm.files'])) {
+            \Civi::log()->warning("The system has data from both old+new conventions. Please use civicrm.settings.php to set civicrm.files explicitly.");
+          }
+          return $new;
+        }
+      });
     }
     else {
       // Legacy support - only relevant for older extern routes.
@@ -142,6 +171,9 @@ class CRM_Utils_System_WordPress extends CRM_Utils_System_Base {
    * Moved from CRM_Utils_System_Base
    */
   public function getDefaultFileStorage() {
+    // NOTE: On WordPress, this will be circumvented in the future. However,
+    // should retain it to allow transitional/upgrade code determine the old value.
+
     $config = CRM_Core_Config::singleton();
     $cmsUrl = CRM_Utils_System::languageNegotiationURL($config->userFrameworkBaseURL, FALSE, TRUE);
     $cmsPath = $this->cmsRootPath();
@@ -287,9 +319,9 @@ class CRM_Utils_System_WordPress extends CRM_Utils_System_Base {
     if ($config->userFrameworkFrontend) {
       global $post;
       if (get_option('permalink_structure') != '') {
-        $script = get_permalink($post->ID);
+        $script = $post ? get_permalink($post->ID) : "";
       }
-      if ($config->wpBasePage == $post->post_name) {
+      if ($post && $config->wpBasePage == $post->post_name) {
         $basepage = TRUE;
       }
       // when shortcode is included in page
@@ -1016,4 +1048,36 @@ class CRM_Utils_System_WordPress extends CRM_Utils_System_Base {
     CRM_Utils_System::civiExit();
   }
 
+  /**
+   * Start a new session if there's no existing session ID.
+   *
+   * Checks are needed to prevent sessions being started when not necessary.
+   */
+  public function sessionStart() {
+    $session_id = session_id();
+
+    // Check WordPress pseudo-cron.
+    $wp_cron = FALSE;
+    if (function_exists('wp_doing_cron') && wp_doing_cron()) {
+      $wp_cron = TRUE;
+    }
+
+    // Check WP-CLI.
+    $wp_cli = FALSE;
+    if (defined('WP_CLI') && WP_CLI) {
+      $wp_cli = TRUE;
+    }
+
+    // Check PHP on the command line - e.g. `cv`.
+    $php_cli = TRUE;
+    if (PHP_SAPI !== 'cli') {
+      $php_cli = FALSE;
+    }
+
+    // Maybe start session.
+    if (empty($session_id) && !$wp_cron && !$wp_cli && !$php_cli) {
+      session_start();
+    }
+  }
+
 }