getInstance() public static method

Create a singleton instance of WPDKUserRoles class
public static getInstance ( ) : WPDKUserRoles
return WPDKUserRoles
Example #1
0
 /**
  * Gets an array of capabilities according to each user role. Each role will return its caps, which are then
  * added to the overall $capabilities array.
  *
  * Note that if no role has the capability, it technically no longer exists. Since this could be a problem with
  * folks accidentally deleting the default WordPress capabilities, the members_default_capabilities() will
  * return all the defaults.
  *
  *     [cap] = [cap, desc, owner]
  *
  * @brief Get all role capabilities
  *
  * @return array $capabilities All the capabilities of all the user roles.
  */
 public function roleCapabilities()
 {
     // Get WPDKUserRoles
     $wpdk_roles = WPDKUserRoles::getInstance();
     // Set up an empty capabilities array
     $capabilities = array();
     // Loop through each role object because we need to get the caps
     foreach ($wpdk_roles->role_objects as $key => $role) {
         // Roles without capabilities will cause an error, so we need to check if $role->capabilities is an array
         if (is_array($role->capabilities)) {
             // Loop through the role's capabilities and add them to the $capabilities array
             $exclude = self::oldLevels();
             foreach ($role->capabilities as $cap => $grant) {
                 if (!isset($exclude[$cap])) {
                     $capabilities[$cap] = isset($this->_extendedData[$cap]) ? $this->_extendedData[$cap] : array($cap, '', '');
                 }
             }
         }
     }
     // Sort the capabilities by name so they're easier to read when shown on the screen
     ksort($capabilities);
     // Return the capabilities array
     return $capabilities;
 }
Example #2
0
 /**
  * Update the extra role information
  *
  * @brief Update
  *
  * @return bool
  */
 public function update()
 {
     // WPDKUserRoles
     $wpdk_roles = WPDKUserRoles::getInstance();
     // Roles
     if (isset($wpdk_roles->roles[$this->name])) {
         // Reset all capabilities
         $wpdk_roles->roles[$this->name]['capabilities'] = array();
         // Set new capabilities
         foreach ($this->capabilities as $cap) {
             $wpdk_roles->roles[$this->name]['capabilities'][$cap] = true;
         }
         // Updated
         if ($wpdk_roles->use_db) {
             update_option($wpdk_roles->role_key, $wpdk_roles->roles);
             /**
              * Fires when the role is updated.
              *
              * @param string $role_key The role key.
              * @param array  $roles    The role capabilities array.
              */
             do_action('wpdk_user_role_update', $wpdk_roles->role_key, $wpdk_roles->roles);
         }
     }
     $extend = get_option(WPDKUserRoles::OPTION_KEY);
     // Stability - however $extend is never empty - see WPDKUserRoles constructor in /classes/users/wpdk-user-roles.php
     $extend = empty($extend) ? array() : $extend;
     $extend[$this->name] = array($this->displayName, $this->description, $this->owner);
     $result = update_option(WPDKUserRoles::OPTION_KEY, $extend);
     /**
      * Fires when the role is updated.
      *
      * @param string $role_key The role key.
      * @param array  $roles    The role capabilities array.
      * @param array  $extend    The array extra (extends) data.
      */
     do_action('wpdk_user_role_extend_update', $wpdk_roles->role_key, $wpdk_roles->roles, $extend);
     return $result;
 }