| 1 |
<?php |
| 2 |
|
| 3 |
namespace StoreEngine\Classes; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) { |
| 6 |
exit; |
| 7 |
} |
| 8 |
|
| 9 |
class Role { |
| 10 |
|
| 11 |
const MIGRATION_OPTION = 'storeengine_role_migration_v2'; |
| 12 |
|
| 13 |
public static function add_customer_role() { |
| 14 |
if ( ! get_role( Capabilities::ROLE_CUSTOMER ) ) { |
| 15 |
add_role( |
| 16 |
Capabilities::ROLE_CUSTOMER, |
| 17 |
esc_html__( 'StoreEngine Customer', 'storeengine' ), |
| 18 |
[] |
| 19 |
); |
| 20 |
} |
| 21 |
|
| 22 |
$customer = get_role( Capabilities::ROLE_CUSTOMER ); |
| 23 |
if ( ! $customer ) { |
| 24 |
return; |
| 25 |
} |
| 26 |
|
| 27 |
foreach ( Capabilities::customer_caps() as $cap ) { |
| 28 |
$customer->add_cap( $cap ); |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
public static function add_shop_manager_role() { |
| 33 |
if ( ! get_role( Capabilities::ROLE_SHOP_MANAGER ) ) { |
| 34 |
add_role( |
| 35 |
Capabilities::ROLE_SHOP_MANAGER, |
| 36 |
esc_html__( 'StoreEngine Shop manager', 'storeengine' ), |
| 37 |
[] |
| 38 |
); |
| 39 |
} |
| 40 |
|
| 41 |
$shop_manager = get_role( Capabilities::ROLE_SHOP_MANAGER ); |
| 42 |
if ( $shop_manager ) { |
| 43 |
foreach ( Capabilities::shop_manager_caps() as $cap ) { |
| 44 |
$shop_manager->add_cap( $cap ); |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
$administrator = get_role( 'administrator' ); |
| 49 |
if ( $administrator ) { |
| 50 |
foreach ( Capabilities::admin_extra_caps() as $cap ) { |
| 51 |
$administrator->add_cap( $cap ); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
if ( current_user_can( 'administrator' ) ) { |
| 56 |
$user_id = get_current_user_id(); |
| 57 |
$shop_manager = new \WP_User( $user_id ); |
| 58 |
$shop_manager->add_role( Capabilities::ROLE_SHOP_MANAGER ); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
public static function remove_customer_role(): void { |
| 63 |
remove_role( Capabilities::ROLE_CUSTOMER ); |
| 64 |
} |
| 65 |
|
| 66 |
public static function remove_shop_manager_role(): void { |
| 67 |
$administrator = get_role( 'administrator' ); |
| 68 |
if ( $administrator ) { |
| 69 |
foreach ( Capabilities::admin_extra_caps() as $cap ) { |
| 70 |
$administrator->remove_cap( $cap ); |
| 71 |
} |
| 72 |
} |
| 73 |
remove_role( Capabilities::ROLE_SHOP_MANAGER ); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* One-shot back-fill that adds newly-introduced caps to the existing |
| 78 |
* shop_manager and administrator role objects on plugin update. Idempotent |
| 79 |
* via the `storeengine_role_migration_v2` option flag. |
| 80 |
*/ |
| 81 |
public static function migrate_caps_v2(): void { |
| 82 |
if ( get_option( self::MIGRATION_OPTION ) ) { |
| 83 |
return; |
| 84 |
} |
| 85 |
|
| 86 |
$new_caps = [ 'manage_storeengine_settings', 'edit_storeengine_orders' ]; |
| 87 |
|
| 88 |
$shop_manager = get_role( Capabilities::ROLE_SHOP_MANAGER ); |
| 89 |
if ( $shop_manager ) { |
| 90 |
foreach ( $new_caps as $cap ) { |
| 91 |
$shop_manager->add_cap( $cap ); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
$administrator = get_role( 'administrator' ); |
| 96 |
if ( $administrator ) { |
| 97 |
foreach ( $new_caps as $cap ) { |
| 98 |
$administrator->add_cap( $cap ); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
update_option( self::MIGRATION_OPTION, 1, false ); |
| 103 |
} |
| 104 |
} |
| 105 |
|