| 1 |
<?php |
| 2 |
|
| 3 |
namespace Bookit\Classes\Base; |
| 4 |
|
| 5 |
use Bookit\Classes\Database\Staff; |
| 6 |
|
| 7 |
/** |
| 8 |
* Class User |
| 9 |
* Bookit users connection with wp user, plugin custom roles and capabilities |
| 10 |
*/ |
| 11 |
class User { |
| 12 |
|
| 13 |
public static $staff_role = 'bookit_staff'; |
| 14 |
public static $customer_role = 'bookit_customer'; |
| 15 |
protected static $wp_roles = array( 'administrator', 'editor' ); |
| 16 |
private static $bookit_user_roles = array( |
| 17 |
'bookit_customer' => array( |
| 18 |
'name' => 'Bookit Customer', |
| 19 |
'roles' => array( |
| 20 |
'read' => false, |
| 21 |
'edit_posts' => false, |
| 22 |
'upload_files' => false, |
| 23 |
), |
| 24 |
), |
| 25 |
'bookit_staff' => array( |
| 26 |
'name' => 'Bookit Staff', |
| 27 |
'roles' => array( |
| 28 |
'read' => true, |
| 29 |
'edit_posts' => false, |
| 30 |
'upload_files' => false, |
| 31 |
'manage_bookit_staff' => true, |
| 32 |
), |
| 33 |
), |
| 34 |
); |
| 35 |
|
| 36 |
public static function getUserData() { |
| 37 |
$user = wp_get_current_user(); |
| 38 |
|
| 39 |
$staff = array(); |
| 40 |
$isStaff = false; |
| 41 |
|
| 42 |
if ( array_intersect( array( self::$staff_role ), $user->roles ) ) { |
| 43 |
$isStaff = true; |
| 44 |
$staff = Staff::get_by_wp_user_id( $user->data->ID ); |
| 45 |
} |
| 46 |
|
| 47 |
return array( |
| 48 |
'staff' => $staff, |
| 49 |
'is_staff' => $isStaff, |
| 50 |
); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Add user roles with capabilities for bookit plugin |
| 55 |
*/ |
| 56 |
public static function addBookitUserRoles() { |
| 57 |
foreach ( self::$bookit_user_roles as $bookitRoleName => $bookitRoleInfo ) { |
| 58 |
remove_role( $bookitRoleName ); |
| 59 |
add_role( $bookitRoleName, $bookitRoleInfo['name'], $bookitRoleInfo['roles'] ); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Add 'manage_bookit_staff' capability to exist WordPress roles ($wp_roles) |
| 65 |
*/ |
| 66 |
public static function addBookitCapabilitiesToWpRoles() { |
| 67 |
|
| 68 |
foreach ( self::$wp_roles as $wpRole ) { |
| 69 |
$role = get_role( $wpRole ); |
| 70 |
$role->add_cap( 'manage_bookit_staff' ); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Remove bookit user roles |
| 76 |
*/ |
| 77 |
public static function cleanRoles() { |
| 78 |
foreach ( self::$bookit_user_roles as $bookitRoleName => $bookitRoleInfo ) { |
| 79 |
remove_role( $bookitRoleName ); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Remove 'manage_bookit_staff' capability from exist WordPress roles ($wp_roles) |
| 85 |
*/ |
| 86 |
public static function cleanCapabilities() { |
| 87 |
foreach ( self::$wp_roles as $wpRole ) { |
| 88 |
$role = get_role( $wpRole ); |
| 89 |
$role->remove_cap( 'manage_bookit_staff' ); |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
|