PluginProbe
PostNL for WooCommerce / trunk
PostNL for WooCommerce vtrunk
5.9.11 5.9.10 5.9.9 5.9.8 5.9.7 5.9.6 trunk 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 3.1.4 3.1.5 3.1.6 3.1.7 4.0.0 4.0.1 4.0.2 4.3.2 4.3.3 4.4.0 4.4.1 4.4.2 All 71 releases
woo-postnl / src / User.php

User.php in PostNL for WooCommerce trunk, at src/User.php

84 lines 1.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class User file.
4 *
5 * @package PostNLWooCommerce
6 */
7
8 namespace PostNLWooCommerce;
9
10 if ( ! defined( 'ABSPATH' ) ) {
11 exit;
12 }
13
14 /**
15 * Class User
16 *
17 * @package PostNLWooCommerce
18 */
19 class User {
20 /**
21 * Manipulate the WooCommerce template file location.
22 *
23 * @param string $role Template filename before manipulated.
24 *
25 * @return String
26 */
27 public static function current_user_has_role( $role ) {
28 return self::user_has_role_by_user_id( get_current_user_id(), $role );
29 }
30
31 /**
32 * Get user roles by using user ID.
33 *
34 * @param int $user_id User ID.
35 *
36 * @return array
37 */
38 public static function get_user_roles_by_user_id( $user_id ) {
39 $user = get_userdata( $user_id );
40 return empty( $user ) ? array() : $user->roles;
41 }
42
43 /**
44 * Get user roles by using user ID.
45 *
46 * @param int $user_id User ID.
47 * @param array $role Roles of user.
48 *
49 * @return array
50 */
51 public static function user_has_role_by_user_id( $user_id, $role ) {
52 $user_roles = self::get_user_roles_by_user_id( $user_id );
53
54 if ( is_array( $role ) ) {
55 return array_intersect( $role, $user_roles ) ? true : false;
56 }
57
58 return in_array( $role, $user_roles, true );
59 }
60
61 /**
62 * Static function to check if user role is shop manager or not.
63 *
64 * @param int $user_id WordPress User ID.
65 *
66 * @return bool
67 */
68 public static function is_shop_manager( $user_id = false ) {
69 $user = new \WP_User( $user_id );
70
71 if ( ! $user->exists() ) {
72 $user = wp_get_current_user();
73 }
74
75 $allowed_roles = array( 'shop_manager', 'administrator' );
76
77 if ( array_intersect( $allowed_roles, $user->roles ) ) {
78 return true; // When user is shop manager.
79 } else {
80 return false; // When user is not shop manager.
81 }
82 }
83 }
84