| 1 |
<?php namespace TierPricingTable\Addons\NonLoggedInUsers; |
| 2 |
|
| 3 |
/** |
| 4 |
* The roles a wholesale rule can name: the customer-facing WordPress roles plus "guest" for visitors |
| 5 |
* who are not logged in. |
| 6 |
*/ |
| 7 |
class Roles { |
| 8 |
|
| 9 |
const GUEST = 'guest'; |
| 10 |
|
| 11 |
/** |
| 12 |
* @return array<string, string> role key => label |
| 13 |
*/ |
| 14 |
public static function getOptions(): array { |
| 15 |
$options = array( self::GUEST => __( 'Guests (not logged in)', 'tier-pricing-table' ) ); |
| 16 |
|
| 17 |
if ( ! function_exists( 'wp_roles' ) ) { |
| 18 |
return $options; |
| 19 |
} |
| 20 |
|
| 21 |
foreach ( wp_roles()->roles as $key => $role ) { |
| 22 |
if ( in_array( $key, array( 'administrator', 'editor', 'author', 'contributor', 'shop_manager' ), true ) ) { |
| 23 |
continue; |
| 24 |
} |
| 25 |
|
| 26 |
$options[ $key ] = translate_user_role( $role['name'] ); |
| 27 |
} |
| 28 |
|
| 29 |
return $options; |
| 30 |
} |
| 31 |
|
| 32 |
public static function label( string $role ): string { |
| 33 |
if ( self::GUEST === $role ) { |
| 34 |
return __( 'Guests', 'tier-pricing-table' ); |
| 35 |
} |
| 36 |
|
| 37 |
return self::getOptions()[ $role ] ?? $role; |
| 38 |
} |
| 39 |
} |
| 40 |
|