| 1 |
<?php namespace TierPricingTable\Addons\GlobalTieredPricing\CPT\Columns; |
| 2 |
|
| 3 |
use TierPricingTable\Addons\GlobalTieredPricing\Formatter; |
| 4 |
use TierPricingTable\Addons\GlobalTieredPricing\GlobalPricingRule; |
| 5 |
use WP_Term; |
| 6 |
|
| 7 |
/** |
| 8 |
* Two lines: the products the rule covers and the customers it covers, exclusions included. Products, |
| 9 |
* terms and customers link to their edit screens; the names are built as escaped HTML. |
| 10 |
*/ |
| 11 |
class AppliesTo { |
| 12 |
|
| 13 |
const MAX_NAMES = 5; |
| 14 |
|
| 15 |
public function getName(): string { |
| 16 |
return __( 'Applies to', 'tier-pricing-table' ); |
| 17 |
} |
| 18 |
|
| 19 |
public function render( GlobalPricingRule $rule ) { |
| 20 |
$this->renderLine( 'dashicons-archive', $this->productsText( $rule ), $this->productExceptionsText( $rule ) ); |
| 21 |
$this->renderLine( 'dashicons-admin-users', $this->customersText( $rule ), $this->customerExceptionsText( $rule ) ); |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* @param string $html Escaped HTML. |
| 26 |
* @param string $exceptions Escaped HTML. |
| 27 |
*/ |
| 28 |
protected function renderLine( string $icon, string $html, string $exceptions ) { |
| 29 |
?> |
| 30 |
<div class="tpt-rules-list__line"> |
| 31 |
<span class="dashicons <?php echo esc_attr( $icon ); ?>"></span> |
| 32 |
<span> |
| 33 |
<?php echo wp_kses_post( $html ); ?> |
| 34 |
<?php if ( $exceptions ) : ?> |
| 35 |
<span class="tpt-rules-list__muted"><?php echo wp_kses_post( $exceptions ); ?></span> |
| 36 |
<?php endif; ?> |
| 37 |
</span> |
| 38 |
</div> |
| 39 |
<?php |
| 40 |
} |
| 41 |
|
| 42 |
protected function productsText( GlobalPricingRule $rule ): string { |
| 43 |
$parts = array_filter( array( |
| 44 |
$this->named( __( 'Products', 'tier-pricing-table' ), $this->productNames( $rule->getIncludedProducts() ) ), |
| 45 |
$this->named( $rule->isIncludeSubcategories() ? __( 'Categories and subcategories', 'tier-pricing-table' ) : __( 'Categories', 'tier-pricing-table' ), $this->termNames( $rule->getIncludedProductCategories() ) ), |
| 46 |
$this->named( __( 'Tags', 'tier-pricing-table' ), $this->termNames( $rule->getIncludedProductTags() ) ), |
| 47 |
$this->named( __( 'Brands', 'tier-pricing-table' ), $this->termNames( $rule->getIncludedProductBrands() ) ), |
| 48 |
) ); |
| 49 |
|
| 50 |
return $parts ? implode( ' · ', $parts ) : esc_html__( 'All products', 'tier-pricing-table' ); |
| 51 |
} |
| 52 |
|
| 53 |
protected function productExceptionsText( GlobalPricingRule $rule ): string { |
| 54 |
$names = array_merge( |
| 55 |
$this->productNames( $rule->getExcludedProducts() ), |
| 56 |
$this->termNames( $rule->getExcludedProductCategories() ), |
| 57 |
$this->termNames( $rule->getExcludedProductTags() ), |
| 58 |
$this->termNames( $rule->getExcludedProductBrands() ) |
| 59 |
); |
| 60 |
|
| 61 |
/* translators: %s: list of names */ |
| 62 |
return $names ? sprintf( esc_html__( 'except %s', 'tier-pricing-table' ), $this->shorten( $names ) ) : ''; |
| 63 |
} |
| 64 |
|
| 65 |
protected function customersText( GlobalPricingRule $rule ): string { |
| 66 |
$parts = array_filter( array( |
| 67 |
$this->named( __( 'Roles', 'tier-pricing-table' ), $this->roleNames( $rule->getIncludedUserRoles() ) ), |
| 68 |
$this->named( __( 'Customers', 'tier-pricing-table' ), $this->customerNames( $rule->getIncludedUsers() ) ), |
| 69 |
) ); |
| 70 |
|
| 71 |
return $parts ? implode( ' · ', $parts ) : esc_html__( 'All customers', 'tier-pricing-table' ); |
| 72 |
} |
| 73 |
|
| 74 |
protected function customerExceptionsText( GlobalPricingRule $rule ): string { |
| 75 |
$names = array_merge( $this->roleNames( $rule->getExcludedUserRoles() ), $this->customerNames( $rule->getExcludedUsers() ) ); |
| 76 |
|
| 77 |
/* translators: %s: list of names */ |
| 78 |
return $names ? sprintf( esc_html__( 'except %s', 'tier-pricing-table' ), $this->shorten( $names ) ) : ''; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* @param string[] $names Escaped HTML. |
| 83 |
*/ |
| 84 |
protected function named( string $label, array $names ): string { |
| 85 |
return $names ? esc_html( $label ) . ': ' . $this->shorten( $names ) : ''; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* @param string[] $names Escaped HTML. |
| 90 |
*/ |
| 91 |
protected function shorten( array $names ): string { |
| 92 |
$shown = array_slice( $names, 0, self::MAX_NAMES ); |
| 93 |
$more = count( $names ) - count( $shown ); |
| 94 |
|
| 95 |
/* translators: %d: number of further items */ |
| 96 |
return implode( ', ', $shown ) . ( $more > 0 ? ' ' . esc_html( sprintf( __( '+%d more', 'tier-pricing-table' ), $more ) ) : '' ); |
| 97 |
} |
| 98 |
|
| 99 |
protected function link( string $url, string $name ): string { |
| 100 |
return $url |
| 101 |
? '<a href="' . esc_url( $url ) . '" target="_blank" rel="noopener">' . esc_html( $name ) . '</a>' |
| 102 |
: esc_html( $name ); |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* @return string[] Escaped HTML, one per id (only the first few are loaded; the rest keep the "+N more" count right). |
| 107 |
*/ |
| 108 |
protected function productNames( array $ids ): array { |
| 109 |
$names = array(); |
| 110 |
foreach ( array_slice( $ids, 0, self::MAX_NAMES ) as $id ) { |
| 111 |
$product = wc_get_product( $id ); |
| 112 |
$names[] = $product |
| 113 |
? $this->link( (string) get_edit_post_link( $product->get_parent_id() ? $product->get_parent_id() : $product->get_id(), 'raw' ), $product->get_name() ) |
| 114 |
: esc_html( '#' . (int) $id ); |
| 115 |
} |
| 116 |
|
| 117 |
return array_pad( $names, count( $ids ), '' ); |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* @return string[] Escaped HTML. |
| 122 |
*/ |
| 123 |
protected function termNames( array $ids ): array { |
| 124 |
$names = array(); |
| 125 |
foreach ( $ids as $id ) { |
| 126 |
$term = get_term( (int) $id ); |
| 127 |
|
| 128 |
if ( $term instanceof WP_Term ) { |
| 129 |
$editLink = get_edit_term_link( $term ); |
| 130 |
$names[] = $this->link( is_string( $editLink ) ? $editLink : '', $term->name ); |
| 131 |
} else { |
| 132 |
$names[] = esc_html( '#' . (int) $id ); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
return $names; |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* @return string[] Escaped HTML. |
| 141 |
*/ |
| 142 |
protected function roleNames( array $roles ): array { |
| 143 |
return array_map( function ( $role ) { |
| 144 |
return esc_html( Formatter::formatRoleString( $role ) ); |
| 145 |
}, $roles ); |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* @return string[] Escaped HTML. |
| 150 |
*/ |
| 151 |
protected function customerNames( array $ids ): array { |
| 152 |
$names = array(); |
| 153 |
foreach ( array_slice( $ids, 0, self::MAX_NAMES ) as $id ) { |
| 154 |
$user = get_userdata( (int) $id ); |
| 155 |
$names[] = $user ? $this->link( (string) get_edit_user_link( $user->ID ), $user->display_name ) : esc_html( '#' . (int) $id ); |
| 156 |
} |
| 157 |
|
| 158 |
return array_pad( $names, count( $ids ), '' ); |
| 159 |
} |
| 160 |
} |
| 161 |
|