woocommerce-multilingual
/
addons
/
wpml-dependencies
/
lib
/
classes
/
wpml-wp
/
class-wpml-wp-roles.php
class-wpml-wp-api.php
1 month ago
class-wpml-wp-roles.php
1 month ago
class-wpml-wp-taxonomy.php
1 month ago
iwpml-wp-element-type.php
1 month ago
wpml-php-functions.php
1 month ago
wpml-wp-post-type.php
1 month ago
class-wpml-wp-roles.php
92 lines
| 1 | <?php |
| 2 | |
| 3 | class WPML_WP_Roles { |
| 4 | const ROLES_ADMINISTRATOR = 'administrator'; |
| 5 | const ROLES_EDITOR = 'editor'; |
| 6 | const ROLES_CONTRIBUTOR = 'contributor'; |
| 7 | const ROLES_SUBSCRIBER = 'subscriber'; |
| 8 | |
| 9 | const EDITOR_LEVEL = 'level_7'; |
| 10 | const CONTRIBUTOR_LEVEL = 'level_1'; |
| 11 | const SUBSCRIBER_LEVEL = 'level_0'; |
| 12 | |
| 13 | public static function get_editor_roles() { |
| 14 | return self::get_roles_for_level( self::EDITOR_LEVEL, self::ROLES_EDITOR ); |
| 15 | } |
| 16 | |
| 17 | public static function get_contributor_roles() { |
| 18 | return self::get_roles_for_level( self::CONTRIBUTOR_LEVEL, self::ROLES_CONTRIBUTOR ); |
| 19 | } |
| 20 | |
| 21 | public static function get_subscriber_roles() { |
| 22 | return self::get_roles_for_level( self::SUBSCRIBER_LEVEL, self::ROLES_SUBSCRIBER ); |
| 23 | } |
| 24 | |
| 25 | public static function get_roles_up_to_user_level( WP_User $user ) { |
| 26 | return self::get_roles_with_max_level( self::get_user_max_level( $user ), self::ROLES_SUBSCRIBER ); |
| 27 | } |
| 28 | |
| 29 | public static function get_user_max_level( WP_User $user ) { |
| 30 | return self::get_highest_level( $user->get_role_caps() ); |
| 31 | } |
| 32 | |
| 33 | public static function get_highest_level( array $capabilities ) { |
| 34 | $capabilitiesWithLevel = function ( $has, $cap ) { |
| 35 | return $has && strpos( $cap, 'level_' ) === 0; |
| 36 | }; |
| 37 | $levelToNumber = function ( $cap ) { |
| 38 | return (int) substr( $cap, strlen( 'level_' ) ); |
| 39 | }; |
| 40 | |
| 41 | return \wpml_collect( $capabilities ) |
| 42 | ->filter( $capabilitiesWithLevel ) |
| 43 | ->keys() |
| 44 | ->map( $levelToNumber ) |
| 45 | ->sort() |
| 46 | ->last(); |
| 47 | } |
| 48 | |
| 49 | private static function get_roles_for_level( $level, $default = null ) { |
| 50 | return \wpml_collect( get_editable_roles() ) |
| 51 | ->filter( |
| 52 | function ( $role ) use ( $level ) { |
| 53 | return isset( $role['capabilities'][ $level ] ) && $role['capabilities'][ $level ]; |
| 54 | } |
| 55 | ) |
| 56 | ->map( self::create_build_role_entity( $level, $default ) ) |
| 57 | ->values() |
| 58 | ->toArray(); |
| 59 | } |
| 60 | |
| 61 | private static function get_roles_with_max_level( $level, $default = null ) { |
| 62 | $isRoleLowerThanLevel = function ( $role ) use ( $level ) { |
| 63 | return self::get_highest_level( $role['capabilities'] ) <= $level; |
| 64 | }; |
| 65 | |
| 66 | return \wpml_collect( get_editable_roles() ) |
| 67 | ->filter( $isRoleLowerThanLevel ) |
| 68 | ->map( self::create_build_role_entity( $level, $default ) ) |
| 69 | ->values() |
| 70 | ->toArray(); |
| 71 | } |
| 72 | |
| 73 | private static function create_build_role_entity( $level, $default = null ) { |
| 74 | $is_default = self::create_is_default( $level, $default ); |
| 75 | |
| 76 | return function ( $role, $id ) use ( $is_default ) { |
| 77 | return [ |
| 78 | 'id' => $id, |
| 79 | 'name' => $role['name'], |
| 80 | 'default' => $is_default( $id ), |
| 81 | ]; |
| 82 | }; |
| 83 | } |
| 84 | |
| 85 | private static function create_is_default( $level, $default = null ) { |
| 86 | $default = apply_filters( 'wpml_role_for_level_default', $default, $level ); |
| 87 | return function ( $id ) use ( $default ) { |
| 88 | return $default && ( $default === $id ); |
| 89 | }; |
| 90 | } |
| 91 | } |
| 92 |