| 1 |
<?php namespace TierPricingTable\Addons\GlobalTieredPricing\CPT\Columns; |
| 2 |
|
| 3 |
use Exception; |
| 4 |
use TierPricingTable\Addons\GlobalTieredPricing\GlobalPricingRule; |
| 5 |
use TierPricingTable\Addons\GlobalTieredPricing\Formatter; |
| 6 |
use WC_Customer; |
| 7 |
|
| 8 |
class AppliedCustomers { |
| 9 |
|
| 10 |
public function getName(): string { |
| 11 |
return __( 'Users & Roles', 'tier-pricing-table' ); |
| 12 |
} |
| 13 |
|
| 14 |
public function render( GlobalPricingRule $rule ) { |
| 15 |
|
| 16 |
$hasCustomers = $this->showCustomers( $rule->getIncludedUsers() ); |
| 17 |
$hasRoles = $this->showUserRoles( $rule->getIncludedUserRoles() ); |
| 18 |
|
| 19 |
$excludedUsers = $rule->getExcludedUsers(); |
| 20 |
$excludedRoles = $rule->getExcludedUserRoles(); |
| 21 |
$hasExceptions = ! empty( $excludedUsers ) || ! empty( $excludedRoles ); |
| 22 |
|
| 23 |
if ( ! $hasRoles && ! $hasCustomers ) { |
| 24 |
$badgeText = $hasExceptions ? __( 'Applied to every user', 'tier-pricing-table' ) : __( 'Applied to every user', 'tier-pricing-table' ); |
| 25 |
?> |
| 26 |
<span style="display: inline-block; background: #e0f0fa; color: #0070bc; border: 1px solid #bae0ff; padding: 4px 10px; border-radius: 4px; font-size: 13px; font-weight: 500; margin-bottom: 12px; line-height: 1.4;"> |
| 27 |
<?php echo esc_html( $badgeText ); ?> |
| 28 |
</span> |
| 29 |
<?php |
| 30 |
} |
| 31 |
|
| 32 |
$this->showCustomers( $excludedUsers, false ); |
| 33 |
$this->showUserRoles( $excludedRoles, false ); |
| 34 |
} |
| 35 |
|
| 36 |
public function showCustomers( array $customersIds, $included = true ): bool { |
| 37 |
$customersMoreThanCanBeShown = count( $customersIds ) > 10; |
| 38 |
|
| 39 |
$customersIds = array_slice( $customersIds, 0, 5 ); |
| 40 |
|
| 41 |
$customers = array_filter( array_map( function ( $customerId ) { |
| 42 |
try { |
| 43 |
return new WC_Customer( $customerId ); |
| 44 |
} catch ( Exception $e ) { |
| 45 |
return false; |
| 46 |
} |
| 47 |
}, $customersIds ) ); |
| 48 |
|
| 49 |
if ( ! empty( $customers ) ) { |
| 50 |
$title = $included ? __( 'Customers', 'tier-pricing-table' ) : __( 'Excluded Customers', 'tier-pricing-table' ); |
| 51 |
|
| 52 |
echo '<div style="margin-bottom: 12px;">'; |
| 53 |
echo sprintf('<strong style="display: block; margin-bottom: 4px;">%s:</strong>', esc_html($title)); |
| 54 |
echo '<div style="display: flex; flex-wrap: wrap; gap: 4px;">'; |
| 55 |
|
| 56 |
foreach ($customers as $customer) { |
| 57 |
echo sprintf( |
| 58 |
'<span style="display: inline-block; padding: 2px 8px; background: #f0f0f1; border-radius: 3px; font-size: 12px; color: #3c434a; border: 1px solid #dcdcdc; line-height: 1.4;">%s</span>', |
| 59 |
Formatter::formatCustomerString( $customer, true ) |
| 60 |
); |
| 61 |
} |
| 62 |
|
| 63 |
if ( $customersMoreThanCanBeShown ) { |
| 64 |
echo '<span style="display: inline-block; padding: 2px 8px; background: #f0f0f1; border-radius: 3px; font-size: 12px; color: #8c8f94; border: 1px solid #dcdcdc; line-height: 1.4;">...</span>'; |
| 65 |
} |
| 66 |
|
| 67 |
echo '</div></div>'; |
| 68 |
|
| 69 |
return true; |
| 70 |
} |
| 71 |
|
| 72 |
return false; |
| 73 |
} |
| 74 |
|
| 75 |
public function showUserRoles( array $roles, $included = true ): bool { |
| 76 |
|
| 77 |
if ( ! empty( $roles ) ) { |
| 78 |
$title = $included ? __( 'Roles', 'tier-pricing-table' ) : __( 'Excluded Roles', 'tier-pricing-table' ); |
| 79 |
|
| 80 |
echo '<div style="margin-bottom: 12px;">'; |
| 81 |
echo sprintf('<strong style="display: block; margin-bottom: 4px;">%s:</strong>', esc_html($title)); |
| 82 |
echo '<div style="display: flex; flex-wrap: wrap; gap: 4px;">'; |
| 83 |
|
| 84 |
foreach ($roles as $role) { |
| 85 |
echo sprintf( |
| 86 |
'<span style="display: inline-block; padding: 2px 8px; background: #f0f0f1; border-radius: 3px; font-size: 12px; color: #3c434a; border: 1px solid #dcdcdc; line-height: 1.4;">%s</span>', |
| 87 |
Formatter::formatRoleString( $role ) |
| 88 |
); |
| 89 |
} |
| 90 |
|
| 91 |
echo '</div></div>'; |
| 92 |
|
| 93 |
return true; |
| 94 |
} |
| 95 |
|
| 96 |
return false; |
| 97 |
} |
| 98 |
} |
| 99 |
|