filesystem.php
13 years ago
formating.php
13 years ago
modules.php
13 years ago
objects.php
13 years ago
plugins.php
13 years ago
system.php
13 years ago
themes.php
13 years ago
users.php
13 years ago
users.php
143 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Users, Roles and Capabilities related functions. |
| 4 | * |
| 5 | * @version $Rev: 203758 $ |
| 6 | * @author Jordi Canals |
| 7 | * @copyright Copyright (C) 2008, 2009, 2010 Jordi Canals |
| 8 | * @license GNU General Public License version 2 |
| 9 | * @link http://alkivia.org |
| 10 | * @package Alkivia |
| 11 | * @subpackage Framework |
| 12 | * |
| 13 | |
| 14 | Copyright 2008, 2009, 2010 Jordi Canals <devel@jcanals.cat> |
| 15 | |
| 16 | This program is free software; you can redistribute it and/or |
| 17 | modify it under the terms of the GNU General Public License |
| 18 | version 2 as published by the Free Software Foundation. |
| 19 | |
| 20 | This program is distributed in the hope that it will be useful, |
| 21 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 23 | GNU General Public License for more details. |
| 24 | |
| 25 | You should have received a copy of the GNU General Public License |
| 26 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 27 | */ |
| 28 | |
| 29 | /** |
| 30 | * Gets current user ID. |
| 31 | * |
| 32 | * @return int Current user ID or 0 if not logged in. |
| 33 | */ |
| 34 | function ak_current_user_id () |
| 35 | { |
| 36 | $user = wp_get_current_user(); |
| 37 | return $user->id; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Returns all valid roles. |
| 42 | * The returned list can be translated or not. |
| 43 | * |
| 44 | * @uses apply_filters() Calls the 'alkivia_roles_translate' hook on translated roles array. |
| 45 | * @since 0.5 |
| 46 | * |
| 47 | * @param boolean $translate If the returned roles have to be translated or not. |
| 48 | * @return array All defined roles. If translated, the key is the role name and value is the translated role. |
| 49 | */ |
| 50 | function ak_get_roles( $translate = false ) { |
| 51 | global $wp_roles; |
| 52 | if ( ! isset( $wp_roles ) ) { |
| 53 | $wp_roles = new WP_Roles(); |
| 54 | } |
| 55 | |
| 56 | $roles = $wp_roles->get_names(); |
| 57 | if ( $translate ) { |
| 58 | foreach ($roles as $k => $r) { |
| 59 | $roles[$k] = _x($r, 'User role'); |
| 60 | } |
| 61 | asort($roles); |
| 62 | return apply_filters('alkivia_roles_translate', $roles); |
| 63 | } else { |
| 64 | $roles = array_keys($roles); |
| 65 | asort($roles); |
| 66 | return $roles; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Return the user role. Taken from WordPress roles and Capabilities. |
| 72 | * |
| 73 | * @since 0.5 |
| 74 | * |
| 75 | * @param int|object $user_ID User ID or the user object to find the role. |
| 76 | * @return string User role in this blog (key, not translated). |
| 77 | */ |
| 78 | function ak_get_user_role( $user ) { |
| 79 | global $wpdb, $wp_roles; |
| 80 | if ( ! isset( $wp_roles ) ) { |
| 81 | $wp_roles = new WP_Roles(); |
| 82 | } |
| 83 | $caps_name = $wpdb->prefix . 'capabilities'; |
| 84 | |
| 85 | if ( ! is_object($user) ) { |
| 86 | $user = get_userdata($user); |
| 87 | } |
| 88 | $roles = array_filter( array_keys( (array) $user->$caps_name ), array( &$wp_roles, 'is_role' ) ); |
| 89 | |
| 90 | return array_pop($roles); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Generates the caps names from user level. |
| 95 | * |
| 96 | * @since 0.5 |
| 97 | * |
| 98 | * @param int $level Level to convert to caps |
| 99 | * @return array Generated caps |
| 100 | */ |
| 101 | function ak_level2caps( $level ) { |
| 102 | $caps = array(); |
| 103 | $level = min(10, intval($level)); |
| 104 | |
| 105 | for ( $i = $level; $i >= 0; $i--) { |
| 106 | $caps["level_{$i}"] = "Level {$i}"; |
| 107 | } |
| 108 | |
| 109 | return $caps; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Finds the proper level from a capabilities list. |
| 114 | * |
| 115 | * @since 0.5 |
| 116 | * |
| 117 | * @uses _ak_level_reduce() |
| 118 | * @param array $caps List of capabilities. |
| 119 | * @return int Level found, if no level found, will return 0. |
| 120 | */ |
| 121 | function ak_caps2level( $caps ) { |
| 122 | $level = array_reduce( array_keys( $caps ), '_ak_caps2level_CB', 0); |
| 123 | return $level; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Callback function to find the level from caps. |
| 128 | * Taken from WordPress 2.7.1 |
| 129 | * |
| 130 | * @since 0.5 |
| 131 | * @access private |
| 132 | * |
| 133 | * @return int level Level found. |
| 134 | */ |
| 135 | function _ak_caps2level_CB( $max, $item ) { |
| 136 | if ( preg_match( '/^level_(10|[0-9])$/i', $item, $matches ) ) { |
| 137 | $level = intval( $matches[1] ); |
| 138 | return max( $max, $level ); |
| 139 | } else { |
| 140 | return $max; |
| 141 | } |
| 142 | } |
| 143 |