Skip to content
Snippets Groups Projects
civicrm.users.php 10.4 KiB
Newer Older
  • Learn to ignore specific revisions
  • Kevin Cristiano's avatar
    Kevin Cristiano committed
    <?php
    /*
     +--------------------------------------------------------------------+
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
     | CiviCRM version 5                                                  |
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
     +--------------------------------------------------------------------+
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
     | Copyright CiviCRM LLC (c) 2004-2019                                |
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
     +--------------------------------------------------------------------+
     | This file is a part of CiviCRM.                                    |
     |                                                                    |
     | CiviCRM is free software; you can copy, modify, and distribute it  |
     | under the terms of the GNU Affero General Public License           |
     | Version 3, 19 November 2007 and the CiviCRM Licensing Exception.   |
     |                                                                    |
     | CiviCRM is distributed in the hope that it will be useful, but     |
     | WITHOUT ANY WARRANTY; without even the implied warranty of         |
     | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               |
     | See the GNU Affero General Public License for more details.        |
     |                                                                    |
     | You should have received a copy of the GNU Affero General Public   |
     | License and the CiviCRM Licensing Exception along                  |
     | with this program; if not, contact CiviCRM LLC                     |
     | at info[AT]civicrm[DOT]org. If you have questions about the        |
     | GNU Affero General Public License or the licensing of CiviCRM,     |
     | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
     +--------------------------------------------------------------------+
    */
    
    /**
     *
     * @package CRM
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
     * @copyright CiviCRM LLC (c) 2004-2019
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
    // This file must not accessed directly
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
    if ( ! defined( 'ABSPATH' ) ) exit;
    
    
    /**
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
     * Define CiviCRM_For_WordPress_Users Class.
     *
     * @since 4.6
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
     */
    class CiviCRM_For_WordPress_Users {
    
      /**
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
       * Plugin object reference.
       *
       * @since 4.6
       * @access public
       * @var object $civi The plugin object reference.
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
       */
      public $civi;
    
    
      /**
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
       * Instance constructor.
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
       *
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
       * @since 4.6
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
       */
      function __construct() {
    
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
        // Store reference to CiviCRM plugin object
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
        $this->civi = civi_wp();
    
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
        // Always listen for activation action
        add_action( 'civicrm_activation', array( $this, 'activate' ) );
    
      }
    
    
      /**
       * Plugin activation tasks.
       *
       * @since 5.6
       */
      public function activate() {
    
        // Assign minimum capabilities for all WP roles and create 'anonymous_user' role
        $this->set_wp_user_capabilities();
    
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
       * Register hooks.
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
       *
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
       * @since 4.6
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
       */
      public function register_hooks() {
    
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
        // Add CiviCRM access capabilities to WordPress roles
        $this->set_access_capabilities();
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
    
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
        // Do not hook into user updates if CiviCRM not installed yet
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
        if ( ! CIVICRM_INSTALLED ) return;
    
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
        // Synchronise users on insert and update
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
        add_action( 'user_register', array( $this, 'update_user' ) );
        add_action( 'profile_update', array( $this, 'update_user' ) );
    
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
        // Delete ufMatch record when a WordPress user is deleted
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
        add_action( 'deleted_user', array( $this, 'delete_user_ufmatch' ), 10, 1 );
    
      }
    
    
      /**
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
       * Check permissions.
       *
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
       * Authentication function used by basepage_register_hooks()
       *
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
       * @since 4.6
       *
       * @param array $args The page arguments array.
       * @return bool True if authenticated, false otherwise.
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
       */
      public function check_permission( $args ) {
    
        if ( $args[0] != 'civicrm' ) {
          return FALSE;
        }
    
        $config = CRM_Core_Config::singleton();
    
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
        // Set frontend true
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
        $config->userFrameworkFrontend = TRUE;
    
        require_once 'CRM/Utils/Array.php';
    
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
        // All profile and file urls, as well as user dashboard and tell-a-friend are valid
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
        $arg1 = CRM_Utils_Array::value(1, $args);
        $invalidPaths = array('admin');
        if ( in_array( $arg1, $invalidPaths ) ) {
          return FALSE;
        }
    
        return TRUE;
    
      }
    
    
      /**
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
       * Get "permission denied" text.
       *
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
       * Called when authentication fails in basepage_register_hooks()
       *
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
       * @since 4.6
       *
       * @return string Warning message.
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
       */
      public function get_permission_denied() {
        return __( 'You do not have permission to access this content.', 'civicrm' );
      }
    
    
      /**
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
       * Handle WordPress user events.
       *
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
       * Callback function for 'user_register' hook
       * Callback function for 'profile_update' hook
       *
       * CMW: seems to (wrongly) create new CiviCRM Contact every time a user changes their
       * first_name or last_name attributes in WordPress.
       *
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
       * @since 4.6
       *
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
       * @param int $user_id The numeric ID of the WordPress user
       */
      public function update_user( $user_id ) {
    
        $user = get_userdata( $user_id );
        if ( $user ) {
          $this->sync_user( $user );
        }
    
      }
    
    
      /**
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
       * Keep WordPress user synced with CiviCRM Contact.
       *
       * @since 4.6
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
       *
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
       * @param object $user The WordPress user object.
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
       */
      public function sync_user( $user = FALSE ) {
    
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
        // Sanity check
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
        if ( $user === FALSE OR !is_a($user, 'WP_User') ) {
          return;
        }
    
        if (!$this->civi->initialize()) {
          return;
        }
    
        require_once 'CRM/Core/BAO/UFMatch.php';
    
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
        /*
         * This does not return anything, so if we want to do anything further
         * to the CiviCRM Contact, we have to search for it all over again.
         */
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
        CRM_Core_BAO_UFMatch::synchronize(
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
          $user, // User object
          TRUE, // Update = true
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
          'WordPress', // CMS
          'Individual' // contact type
        );
    
        /*
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
        // IN PROGRESS: synchronizeUFMatch does return the contact object, however
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
        $civi_contact = CRM_Core_BAO_UFMatch::synchronizeUFMatch(
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
          $user, // User object
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
          $user->ID, // ID
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
          $user->user_mail, // Unique identifier
          null // Unused
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
          'WordPress' // CMS
          'Individual' // contact type
        );
    
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
        // Now we can allow other plugins to do their thing
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
        do_action( 'civicrm_contact_synced', $user, $civi_contact );
        */
    
      }
    
    
      /**
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
       * When a WordPress user is deleted, delete the ufMatch record.
       *
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
       * Callback function for 'delete_user' hook
       *
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
       * @since 4.6
       *
       * @param $user_id The numerical ID of the WordPress user.
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
       */
      public function delete_user_ufmatch( $user_id ) {
    
        if (!$this->civi->initialize()) {
          return;
        }
    
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
        // Delete the ufMatch record
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
        require_once 'CRM/Core/BAO/UFMatch.php';
        CRM_Core_BAO_UFMatch::deleteUser($user_id);
    
      }
    
    
      /**
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
       * Create anonymous role and define capabilities.
       *
       * Function to create 'anonymous_user' role, if 'anonymous_user' role is not
       * in the WordPress installation and assign minimum capabilities for all
       * WordPress roles.
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
       *
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
       * The legacy global scope function civicrm_wp_set_capabilities() is called
       * from upgrade_4_3_alpha1()
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
       *
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
       * @since 4.6
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
       */
      public function set_wp_user_capabilities() {
    
        global $wp_roles;
        if ( ! isset( $wp_roles ) ) {
          $wp_roles = new WP_Roles();
        }
    
        // Minimum capabilities (Civicrm permissions) arrays
        $default_min_capabilities =  array(
          'access_civimail_subscribe_unsubscribe_pages' => 1,
          'access_all_custom_data' => 1,
          'access_uploaded_files' => 1,
          'make_online_contributions' => 1,
          'profile_create' => 1,
          'profile_edit' => 1,
          'profile_view' => 1,
          'register_for_events' => 1,
          'view_event_info' => 1,
          'sign_civicrm_petition' => 1,
          'view_public_civimail_content' => 1,
        );
    
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
        /**
         * Allow minimum capabilities to be filtered.
         *
         * @since 4.6
         *
         * @param array $default_min_capabilities The minimum capabilities.
         * @return array $default_min_capabilities The modified capabilities.
         */
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
        $min_capabilities = apply_filters( 'civicrm_min_capabilities', $default_min_capabilities );
    
        // Assign the Minimum capabilities (Civicrm permissions) to all WP roles
        foreach ( $wp_roles->role_names as $role => $name ) {
          $roleObj = $wp_roles->get_role( $role );
          foreach ( $min_capabilities as $capability_name => $capability_value ) {
            $roleObj->add_cap( $capability_name );
          }
        }
    
        // Add the 'anonymous_user' role with minimum capabilities.
        if ( ! in_array( 'anonymous_user' , $wp_roles->roles ) ) {
          add_role(
            'anonymous_user',
            __( 'Anonymous User', 'civicrm' ),
            $min_capabilities
          );
        }
    
      }
    
    
      /**
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
       * Add CiviCRM access capabilities to WordPress roles.
       *
       * This is a callback for the 'init' hook in register_hooks().
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
       *
       * The legacy global scope function wp_civicrm_capability() is called by
       * postProcess() in civicrm/CRM/ACL/Form/WordPress/Permissions.php
       *
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
       * @since 4.6
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
       */
      public function set_access_capabilities() {
    
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
        // Test for existing global
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
        global $wp_roles;
        if ( ! isset( $wp_roles ) ) {
          $wp_roles = new WP_Roles();
        }
    
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
        /**
         * Filter the default roles with access to CiviCRM.
         *
         * The 'access_civicrm' capability is the most basic CiviCRM capability and
         * is required to see the CiviCRM menu link in the WordPress Admin menu.
         *
         * @since 4.6
         *
         * @param array The default roles with access to CiviCRM.
         * @return array The modified roles with access to CiviCRM.
         */
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
        $roles = apply_filters( 'civicrm_access_roles', array( 'super admin', 'administrator' ) );
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
    
         // Give access to CiviCRM to particular roles.
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
        foreach ( $roles as $role ) {
          $roleObj = $wp_roles->get_role( $role );
          if (
            is_object( $roleObj ) &&
            is_array( $roleObj->capabilities ) &&
            ! array_key_exists( 'access_civicrm', $wp_roles->get_role( $role )->capabilities )
          ) {
            $wp_roles->add_cap( $role, 'access_civicrm' );
          }
        }
    
      }
    
    
      /**
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
       * Get CiviCRM contact type.
       *
       * @since 4.6
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
       *
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
       * @param string $default The requested contact type.
       * @return string $ctype The computed contact type.
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
       */
      public function get_civicrm_contact_type( $default = NULL ) {
    
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
        // Here we are creating a new contact
        // Get the contact type from the POST variables if any
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
        if ( isset( $_REQUEST['ctype'] ) ) {
          $ctype = $_REQUEST['ctype'];
        } elseif (
          isset( $_REQUEST['edit'] ) &&
          isset( $_REQUEST['edit']['ctype'] )
        ) {
          $ctype = $_REQUEST['edit']['ctype'];
        } else {
          $ctype = $default;
        }
    
        if (
          $ctype != 'Individual' &&
          $ctype != 'Organization' &&
          $ctype != 'Household'
        ) {
          $ctype = $default;
        }
    
        return $ctype;
    
      }
    
    
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
    } // Class CiviCRM_For_WordPress_Users ends