| 1 |
<?php namespace TierPricingTable\Addons\NonLoggedInUsers\Visibility\Admin; |
| 2 |
|
| 3 |
use TierPricingTable\Addons\NonLoggedInUsers\Visibility\VisibilityMeta; |
| 4 |
use TierPricingTable\Addons\NonLoggedInUsers\Visibility\VisibilityRule; |
| 5 |
|
| 6 |
/** |
| 7 |
* A rule in words, for the list-table columns. |
| 8 |
*/ |
| 9 |
class RuleText { |
| 10 |
|
| 11 |
public static function describe( array $rule ): string { |
| 12 |
$roles = self::roleLabels( $rule['roles'] ?? array() ); |
| 13 |
|
| 14 |
switch ( $rule['mode'] ?? '' ) { |
| 15 |
case VisibilityRule::MODE_INCLUDE: |
| 16 |
/* translators: %s: list of roles */ |
| 17 |
return $roles ? sprintf( __( 'Only %s', 'tier-pricing-table' ), $roles ) : __( 'Nobody', 'tier-pricing-table' ); |
| 18 |
case VisibilityRule::MODE_EXCLUDE: |
| 19 |
/* translators: %s: list of roles */ |
| 20 |
return $roles ? sprintf( __( 'Everyone except %s', 'tier-pricing-table' ), $roles ) : __( 'Everyone', 'tier-pricing-table' ); |
| 21 |
default: |
| 22 |
return __( 'Everyone', 'tier-pricing-table' ); |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
public static function roleLabels( array $roles ): string { |
| 27 |
$options = VisibilityMeta::getRoleOptions(); |
| 28 |
$labels = array(); |
| 29 |
|
| 30 |
foreach ( $roles as $role ) { |
| 31 |
$labels[] = VisibilityRule::GUEST === $role ? __( 'Guests', 'tier-pricing-table' ) : ( $options[ $role ] ?? $role ); |
| 32 |
} |
| 33 |
|
| 34 |
return implode( ', ', $labels ); |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* A rule with the name of where it comes from (a category), as HTML. |
| 39 |
*/ |
| 40 |
public static function line( array $rule, string $source = '' ): string { |
| 41 |
$html = esc_html( self::describe( $rule ) ); |
| 42 |
|
| 43 |
if ( '' !== $source ) { |
| 44 |
$html .= ' <span class="tpt-visibility-column__source">' . esc_html( $source ) . '</span>'; |
| 45 |
} |
| 46 |
|
| 47 |
return $html; |
| 48 |
} |
| 49 |
|
| 50 |
public static function defaultLine(): string { |
| 51 |
return '<span class="tpt-visibility-column__default">' . esc_html__( 'Everyone', 'tier-pricing-table' ) . '</span>'; |
| 52 |
} |
| 53 |
|
| 54 |
public static function ownLine( array $rule ): string { |
| 55 |
return '<span class="tpt-visibility-column__own">' . esc_html( self::describe( $rule ) ) . '</span>'; |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* @param int[] $termIds |
| 60 |
* |
| 61 |
* @return array<int, array> term id => rule, for the categories that restrict |
| 62 |
*/ |
| 63 |
public static function restrictingCategoryRules( array $termIds ): array { |
| 64 |
$rules = array(); |
| 65 |
|
| 66 |
foreach ( array_unique( array_map( 'intval', $termIds ) ) as $termId ) { |
| 67 |
$rule = VisibilityMeta::getCategoryRule( $termId ); |
| 68 |
|
| 69 |
if ( VisibilityRule::isRestricting( $rule ) ) { |
| 70 |
$rules[ $termId ] = $rule; |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
return $rules; |
| 75 |
} |
| 76 |
|
| 77 |
public static function categoryName( int $termId ): string { |
| 78 |
$term = get_term( $termId, 'product_cat' ); |
| 79 |
|
| 80 |
return $term && ! is_wp_error( $term ) ? $term->name : '#' . $termId; |
| 81 |
} |
| 82 |
|
| 83 |
public static function printStyles() { |
| 84 |
?> |
| 85 |
<style> |
| 86 |
.wp-list-table .column-tpt_visibility { width: 11%; } |
| 87 |
.tpt-visibility-column__source { display: block; color: #787c82; } |
| 88 |
.tpt-visibility-column__source::before { content: "("; } |
| 89 |
.tpt-visibility-column__source::after { content: ")"; } |
| 90 |
.tpt-visibility-column__default { color: #787c82; } |
| 91 |
</style> |
| 92 |
<?php |
| 93 |
} |
| 94 |
} |
| 95 |
|