Comment block fixes
[civicrm-core.git] / CRM / Utils / System / WordPress.php
index 389c2c06927d8f84687b5367bc196fddf1ec3e9f..0573a3786b0d51606515d8e01974f232c6c5d195 100644 (file)
@@ -1,9 +1,9 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.4                                                |
+ | CiviCRM version 4.5                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2013                                |
+ | Copyright CiviCRM LLC (c) 2004-2014                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
@@ -28,7 +28,7 @@
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2013
+ * @copyright CiviCRM LLC (c) 2004-2014
  * $Id$
  *
  */
@@ -45,6 +45,8 @@ class CRM_Utils_System_WordPress extends CRM_Utils_System_Base {
    * sets the title of the page
    *
    * @param string $title
+   * @param null $pageTitle
+   *
    * @paqram string $pageTitle
    *
    * @return void
@@ -65,8 +67,10 @@ class CRM_Utils_System_WordPress extends CRM_Utils_System_Base {
   /**
    * Append an additional breadcrumb tag to the existing breadcrumb
    *
-   * @param string $title
-   * @param string $url
+   * @param $breadCrumbs
+   *
+   * @internal param string $title
+   * @internal param string $url
    *
    * @return void
    * @access public
@@ -245,9 +249,10 @@ class CRM_Utils_System_WordPress extends CRM_Utils_System_Base {
    * @param $htmlize  boolean  whether to convert to html eqivalant
    * @param $frontend boolean  a gross joomla hack
    *
+   * @param bool $forceBackend
+   *
    * @return string            an HTML string containing a link to the given path.
    * @access public
-   *
    */
   function url(
     $path = NULL,
@@ -261,18 +266,18 @@ class CRM_Utils_System_WordPress extends CRM_Utils_System_Base {
     $config    = CRM_Core_Config::singleton();
     $script    = '';
     $separator = $htmlize ? '&amp;' : '&';
-    $pageID    = '';
+    $wpPageParam    = '';
+    $fragment = isset($fragment) ? ('#' . $fragment) : '';
 
     $path = CRM_Utils_String::stripPathChars($path);
 
     //this means wp function we are trying to use is not available,
     //so load bootStrap
     if (!function_exists('get_option')) {
-      $this->loadBootStrap();
+      $this->loadBootStrap(); // FIXME: Why bootstrap in url()? Generally want to define 1-2 strategic places to put bootstrap
     }
-    $permlinkStructure = get_option('permalink_structure');
     if ($config->userFrameworkFrontend) {
-      if ($permlinkStructure != '') {
+      if (get_option('permalink_structure') != '') {
         global $post;
         $script = get_permalink($post->ID);
       }
@@ -282,19 +287,48 @@ class CRM_Utils_System_WordPress extends CRM_Utils_System_Base {
       global $wp_query;
       if ( method_exists( $wp_query, 'get' ) ) {
         if (get_query_var('page_id')) {
-          $pageID = "{$separator}page_id=" . get_query_var('page_id');
+          $wpPageParam = "page_id=" . get_query_var('page_id');
         }
         elseif (get_query_var('p')) {
           // when shortcode is inserted in post
-          $pageID = "{$separator}p=" . get_query_var('p');
+          $wpPageParam = "p=" . get_query_var('p');
         }
       }
     }
 
-    if (isset($fragment)) {
-      $fragment = '#' . $fragment;
+    $base = $this->getBaseUrl($absolute, $frontend, $forceBackend);
+
+    if (!isset($path) && !isset($query)) {
+      // FIXME: This short-circuited codepath is the same as the general one below, except
+      // in that it ignores "permlink_structure" /  $wpPageParam / $script . I don't know
+      // why it's different (and I can only find two obvious use-cases for this codepath,
+      // of which at least one looks gratuitous). A more ambitious person would simply remove
+      // this code.
+      return $base . $fragment;
+    }
+
+    if (!$forceBackend && get_option('permalink_structure') != '' && ($wpPageParam || $script != '')) {
+      $base = $script;
     }
 
+    $queryParts = array();
+    if (isset($path)) {
+      $queryParts[] = 'page=CiviCRM';
+      $queryParts[] = "q={$path}";
+    }
+    if ($wpPageParam) {
+      $queryParts[] = $wpPageParam;
+    }
+    if (isset($query)) {
+      $queryParts[] = $query;
+    }
+
+    return $base . '?' . implode($separator, $queryParts) . $fragment;
+  }
+
+  private function getBaseUrl($absolute, $frontend, $forceBackend) {
+    $config    = CRM_Core_Config::singleton();
+
     if (!isset($config->useFrameworkRelativeBase)) {
       $base = parse_url($config->userFrameworkBaseURL);
       $config->useFrameworkRelativeBase = $base['path'];
@@ -304,56 +338,31 @@ class CRM_Utils_System_WordPress extends CRM_Utils_System_Base {
 
     if ((is_admin() && !$frontend) || $forceBackend) {
       $base .= 'wp-admin/admin.php';
+      return $base;
     }
     elseif (defined('CIVICRM_UF_WP_BASEPAGE')) {
       $base .= CIVICRM_UF_WP_BASEPAGE;
+      return $base;
     }
     elseif (isset($config->wpBasePage)) {
       $base .= $config->wpBasePage;
+      return $base;
     }
-
-    if (isset($path)) {
-      if (isset($query)) {
-        if ($permlinkStructure != '' && ($pageID || $script != '')) {
-          return $script . '?page=CiviCRM'. $separator . 'q=' . $path . $pageID . $separator . $query . $fragment;
-        }
-        else {
-          return $base . '?page=CiviCRM' . $separator . 'q=' . $path . $pageID . $separator . $query . $fragment;
-        }
-      }
-      else {
-        if ($permlinkStructure != '' && ($pageID || $script != '')) {
-          return $script . '?page=CiviCRM' . $separator . 'q=' . $path . $pageID . $fragment;
-        }
-        else {
-          return $base . '?page=CiviCRM' . $separator . 'q=' . $path . $pageID . $fragment;
-        }
-      }
-    }
-    else {
-      if (isset($query)) {
-        if ($permlinkStructure != '' && ($pageID || $script != '')) {
-          return $script . '?' . $query . $pageID . $fragment;
-        }
-        else {
-          return $base . $script . '?' . $query . $pageID . $fragment;
-        }
-      }
-      else {
-        return $base . $fragment;
-      }
-    }
+    return $base;
   }
 
   /**
    * Authenticate the user against the wordpress db
    *
-   * @param string $name     the user name
+   * @param string $name the user name
    * @param string $password the password for the above user name
    *
+   * @param bool $loadCMSBootstrap
+   * @param null $realPath
+   *
    * @return mixed false if no auth
    *               array(
-      contactID, ufID, unique string ) if success
+   * contactID, ufID, unique string ) if success
    * @access public
    * @static
    */
@@ -415,7 +424,18 @@ class CRM_Utils_System_WordPress extends CRM_Utils_System_Base {
    * @return string  with the locale or null for none
    */
   function getUFLocale() {
-    return NULL;
+    // WPML plugin
+    if (defined('ICL_LANGUAGE_CODE')) {
+      $language = ICL_LANGUAGE_CODE;
+    }
+
+    // TODO: set language variable for others WordPress plugin
+
+    if (isset($language)) {
+      return CRM_Core_I18n_PseudoConstant::longForShort(substr($language, 0, 2));
+    } else {
+      return NULL;
+    }
   }
 
   /**
@@ -423,6 +443,8 @@ class CRM_Utils_System_WordPress extends CRM_Utils_System_Base {
    *
    * @param $name string  optional username for login
    * @param $pass string  optional password for login
+   *
+   * @return bool
    */
   function loadBootStrap($name = NULL, $pass = NULL) {
     global $wp, $wp_rewrite, $wp_the_query, $wp_query, $wpdb;
@@ -516,7 +538,7 @@ class CRM_Utils_System_WordPress extends CRM_Utils_System_Base {
     return $uid;
   }
 
-  /*
+  /**
    * Change user name in host CMS
    *
    * @param integer $ufID User ID in CMS
@@ -542,7 +564,7 @@ class CRM_Utils_System_WordPress extends CRM_Utils_System_Base {
     $name  = $dao->escape(CRM_Utils_Array::value('name', $params));
     $email = $dao->escape(CRM_Utils_Array::value('mail', $params));
 
-    if (CRM_Utils_Array::value('name', $params)) {
+    if (!empty($params['name'])) {
       if (!validate_username($params['name'])) {
         $errors['cms_name'] = ts("Your username contains invalid characters");
       }
@@ -551,7 +573,7 @@ class CRM_Utils_System_WordPress extends CRM_Utils_System_Base {
       }
     }
 
-    if (CRM_Utils_Array::value('mail', $params)) {
+    if (!empty($params['mail'])) {
       if (!is_email($params['mail'])) {
         $errors[$emailName] = "Your email is invaid";
       }
@@ -578,6 +600,13 @@ class CRM_Utils_System_WordPress extends CRM_Utils_System_Base {
     return $isloggedIn;
   }
 
+  function getLoggedInUserObject() {
+    if (function_exists('is_user_logged_in') &&
+    is_user_logged_in()) {
+      global $current_user;
+    }
+    return $current_user;
+  }
   /**
    * Get currently logged in user uf id.
    *
@@ -585,13 +614,37 @@ class CRM_Utils_System_WordPress extends CRM_Utils_System_Base {
    */
   public function getLoggedInUfID() {
     $ufID = NULL;
-    if (function_exists('is_user_logged_in') &&
-      is_user_logged_in()
-    ) {
-      global $current_user;
-      $ufID = $current_user->ID;
-    }
-    return $ufID;
+    $current_user = $this->getLoggedInUserObject();
+    return isset($current_user->ID) ? $current_user->ID : NULL;
+  }
+
+  /**
+   * Get currently logged in user unique identifier - this tends to be the email address or user name.
+   *
+   * @return string $userID logged in user unique identifier
+   */
+  function getLoggedInUniqueIdentifier() {
+    $user = $this->getLoggedInUserObject();
+    return $this->getUniqueIdentifierFromUserObject($user);
+  }
+
+  /**
+   * Get User ID from UserFramework system (Joomla)
+   * @param object $user object as described by the CMS
+   * @return mixed <NULL, number>
+   */
+  function getUserIDFromUserObject($user) {
+    return !empty($user->ID) ? $user->ID : NULL;
+  }
+
+  /**
+   * Get Unique Identifier from UserFramework system (CMS)
+   * @param object $user object as described by the User Framework
+   * @return mixed $uniqueIdentifer Unique identifier from the user Framework system
+   *
+   */
+  function getUniqueIdentifierFromUserObject($user) {
+    return empty($user->user_email) ? NULL : $user->user_email;
   }
 
   /**