| 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 |
if ( ! $hasRoles && ! $hasCustomers ) { |
| 20 |
?> |
| 21 |
<mark class="order-status status-processing tips"> |
| 22 |
<span> |
| 23 |
<?php esc_html_e( 'Applied to every user', 'tier-pricing-table' ); ?> |
| 24 |
</span> |
| 25 |
</mark> |
| 26 |
<?php |
| 27 |
} |
| 28 |
|
| 29 |
$this->showCustomers( $rule->getExcludedUsers(), false ); |
| 30 |
$this->showUserRoles( $rule->getExcludedUserRoles(), false ); |
| 31 |
} |
| 32 |
|
| 33 |
public function showCustomers( array $customersIds, $included = true ): bool { |
| 34 |
$customersMoreThanCanBeShown = count( $customersIds ) > 10; |
| 35 |
|
| 36 |
$customersIds = array_slice( $customersIds, 0, 5 ); |
| 37 |
|
| 38 |
$customers = array_filter( array_map( function ( $customerId ) { |
| 39 |
try { |
| 40 |
return new WC_Customer( $customerId ); |
| 41 |
} catch ( Exception $e ) { |
| 42 |
return false; |
| 43 |
} |
| 44 |
}, $customersIds ) ); |
| 45 |
|
| 46 |
if ( ! empty( $customers ) ) { |
| 47 |
|
| 48 |
if ( $included ) { |
| 49 |
esc_html_e( 'Customers: ', 'tier-pricing-table' ); |
| 50 |
} else { |
| 51 |
esc_html_e( 'Excluded customers: ', 'tier-pricing-table' ); |
| 52 |
} |
| 53 |
|
| 54 |
$customersString = array_map( function ( WC_Customer $customer ) { |
| 55 |
return Formatter::formatCustomerString( $customer, true ); |
| 56 |
}, $customers ); |
| 57 |
|
| 58 |
echo wp_kses_post( implode( ', ', |
| 59 |
$customersString ) . ( $customersMoreThanCanBeShown ? '<span> ...</span>' : '' ) ); |
| 60 |
|
| 61 |
echo '<br><br>'; |
| 62 |
|
| 63 |
return true; |
| 64 |
} |
| 65 |
|
| 66 |
return false; |
| 67 |
} |
| 68 |
|
| 69 |
public function showUserRoles( array $roles, $included = true ): bool { |
| 70 |
|
| 71 |
if ( ! empty( $roles ) ) { |
| 72 |
if ( $included ) { |
| 73 |
esc_html_e( 'Roles: ', 'tier-pricing-table' ); |
| 74 |
} else { |
| 75 |
esc_html_e( 'Excluded roles: ', 'tier-pricing-table' ); |
| 76 |
} |
| 77 |
|
| 78 |
$rolesString = array_map( function ( $role ) { |
| 79 |
return sprintf( '<span>%s</span>', Formatter::formatRoleString( $role ) ); |
| 80 |
}, $roles ); |
| 81 |
|
| 82 |
echo wp_kses_post( implode( ', ', $rolesString ) ); |
| 83 |
|
| 84 |
echo '<br><br>'; |
| 85 |
|
| 86 |
return true; |
| 87 |
} |
| 88 |
|
| 89 |
return false; |
| 90 |
} |
| 91 |
} |
| 92 |
|