| 1 |
<?php namespace TierPricingTable\Addons\NonLoggedInUsers\Visibility; |
| 2 |
|
| 3 |
/** |
| 4 |
* Who may see a product or a category. Pure: no WordPress calls, so it is unit-tested on its own. |
| 5 |
* |
| 6 |
* A rule is [ 'mode' => everyone|include|exclude|inherit, 'roles' => string[] ]. Roles are WordPress role |
| 7 |
* keys plus the pseudo role "guest" for visitors who are not logged in. |
| 8 |
*/ |
| 9 |
class VisibilityRule { |
| 10 |
|
| 11 |
const MODE_INHERIT = 'inherit'; |
| 12 |
const MODE_EVERYONE = 'everyone'; |
| 13 |
const MODE_INCLUDE = 'include'; |
| 14 |
const MODE_EXCLUDE = 'exclude'; |
| 15 |
|
| 16 |
const GUEST = 'guest'; |
| 17 |
|
| 18 |
public static function getModes(): array { |
| 19 |
return array( self::MODE_INHERIT, self::MODE_EVERYONE, self::MODE_INCLUDE, self::MODE_EXCLUDE ); |
| 20 |
} |
| 21 |
|
| 22 |
public static function normalize( $mode, $roles ): array { |
| 23 |
$mode = in_array( $mode, self::getModes(), true ) ? $mode : self::MODE_INHERIT; |
| 24 |
|
| 25 |
if ( is_string( $roles ) ) { |
| 26 |
$roles = explode( ',', $roles ); |
| 27 |
} |
| 28 |
|
| 29 |
$roles = array_values( array_unique( array_filter( array_map( function ( $role ) { |
| 30 |
return strtolower( trim( (string) preg_replace( '/[^A-Za-z0-9_\-]/', '', (string) $role ) ) ); |
| 31 |
}, (array) $roles ), 'strlen' ) ) ); |
| 32 |
|
| 33 |
return array( 'mode' => $mode, 'roles' => $roles ); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Whether the rule restricts anybody at all. |
| 38 |
*/ |
| 39 |
public static function isRestricting( array $rule ): bool { |
| 40 |
return in_array( $rule['mode'] ?? '', array( self::MODE_INCLUDE, self::MODE_EXCLUDE ), true ); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Whether a visitor with these roles passes a single rule. |
| 45 |
* |
| 46 |
* @param array $rule |
| 47 |
* @param string[] $userRoles the visitor's roles; empty for a guest |
| 48 |
* @param bool $isGuest |
| 49 |
*/ |
| 50 |
public static function canSee( array $rule, array $userRoles, bool $isGuest ): bool { |
| 51 |
$mode = $rule['mode'] ?? self::MODE_INHERIT; |
| 52 |
$roles = (array) ( $rule['roles'] ?? array() ); |
| 53 |
|
| 54 |
if ( ! self::isRestricting( $rule ) ) { |
| 55 |
return true; |
| 56 |
} |
| 57 |
|
| 58 |
$matches = $isGuest ? in_array( self::GUEST, $roles, true ) : (bool) array_intersect( $userRoles, $roles ); |
| 59 |
|
| 60 |
return self::MODE_INCLUDE === $mode ? $matches : ! $matches; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Whether a product is visible: its own rule decides when it has one, otherwise every rule of its |
| 65 |
* categories (and their parents) must let the visitor through. |
| 66 |
* |
| 67 |
* @param array $productRule |
| 68 |
* @param array[] $categoryRules |
| 69 |
* @param string[] $userRoles |
| 70 |
* @param bool $isGuest |
| 71 |
*/ |
| 72 |
public static function isProductVisible( array $productRule, array $categoryRules, array $userRoles, bool $isGuest ): bool { |
| 73 |
if ( self::MODE_INHERIT !== ( $productRule['mode'] ?? self::MODE_INHERIT ) ) { |
| 74 |
return self::canSee( $productRule, $userRoles, $isGuest ); |
| 75 |
} |
| 76 |
|
| 77 |
foreach ( $categoryRules as $categoryRule ) { |
| 78 |
if ( ! self::canSee( $categoryRule, $userRoles, $isGuest ) ) { |
| 79 |
return false; |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
return true; |
| 84 |
} |
| 85 |
} |
| 86 |
|