users.php
110 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 | * Returns all valid roles. |
| 31 | * The returned list can be translated or not. |
| 32 | * |
| 33 | * @uses apply_filters() Calls the 'alkivia_roles_translate' hook on translated roles array. |
| 34 | * @since 0.5 |
| 35 | * |
| 36 | * @param boolean $translate If the returned roles have to be translated or not. |
| 37 | * @return array All defined roles. If translated, the key is the role name and value is the translated role. |
| 38 | */ |
| 39 | function ak_get_roles( $translate = false ) { |
| 40 | $wp_roles_obj = wp_roles(); |
| 41 | |
| 42 | $roles = $wp_roles_obj->get_names(); |
| 43 | if ( $translate ) { |
| 44 | foreach ($roles as $k => $r) { |
| 45 | $roles[$k] = _x($r, 'User role'); |
| 46 | } |
| 47 | asort($roles); |
| 48 | return apply_filters('alkivia_roles_translate', $roles); |
| 49 | } else { |
| 50 | $roles = array_keys($roles); |
| 51 | asort($roles); |
| 52 | return $roles; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Generates the caps names from user level. |
| 58 | * |
| 59 | * @since 0.5 |
| 60 | * |
| 61 | * @param int $level Level to convert to caps |
| 62 | * @return array Generated caps |
| 63 | */ |
| 64 | function ak_level2caps( $level ) { |
| 65 | $caps = array(); |
| 66 | $level = min(10, intval($level)); |
| 67 | |
| 68 | for ( $i = $level; $i >= 0; $i--) { |
| 69 | $caps["level_{$i}"] = true; |
| 70 | } |
| 71 | |
| 72 | return $caps; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Finds the proper level from a capabilities list. |
| 77 | * |
| 78 | * @since 0.5 |
| 79 | * |
| 80 | * @uses _ak_level_reduce() |
| 81 | * @param array $caps List of capabilities. |
| 82 | * @return int Level found, if no level found, will return 0. |
| 83 | */ |
| 84 | function ak_caps2level( $caps ) { |
| 85 | if (!is_array($caps)) { |
| 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | $level = array_reduce( array_keys( $caps ), '_ak_caps2level_CB', 0); |
| 90 | return $level; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Callback function to find the level from caps. |
| 95 | * Taken from WordPress 2.7.1 |
| 96 | * |
| 97 | * @since 0.5 |
| 98 | * @access private |
| 99 | * |
| 100 | * @return int level Level found. |
| 101 | */ |
| 102 | function _ak_caps2level_CB( $max, $item ) { |
| 103 | if ( preg_match( '/^level_(10|[0-9])$/i', $item, $matches ) ) { |
| 104 | $level = intval( $matches[1] ); |
| 105 | return max( $max, $level ); |
| 106 | } else { |
| 107 | return $max; |
| 108 | } |
| 109 | } |
| 110 |