| 1 |
<?php namespace TierPricingTable\Addons\TierLabels\Frontend; |
| 2 |
|
| 3 |
use TierPricingTable\Addons\TierLabels\TierLabel; |
| 4 |
use TierPricingTable\Addons\TierLabels\TierLabelsManager; |
| 5 |
use TierPricingTable\PricingRule; |
| 6 |
|
| 7 |
class DisplayManager { |
| 8 |
|
| 9 |
public function __construct() { |
| 10 |
$this->hooks(); |
| 11 |
} |
| 12 |
|
| 13 |
private function hooks() { |
| 14 |
|
| 15 |
$hooks = array( |
| 16 |
'options' => 'tiered_pricing_table/options/label', |
| 17 |
'dropdown' => 'tiered_pricing_table/dropdown/label', |
| 18 |
'horizontal-table' => 'tiered_pricing_table/horizontal-table/label', |
| 19 |
'plain-text' => 'tiered_pricing_table/plain-text/label', |
| 20 |
'blocks' => 'tiered_pricing_table/blocks/label', |
| 21 |
'table' => 'tiered_pricing_table/table/label', |
| 22 |
); |
| 23 |
|
| 24 |
foreach ( $hooks as $layout => $hook ) { |
| 25 |
add_action( $hook, function ( PricingRule $rule, $quantity, $args ) use ( $layout ) { |
| 26 |
$this->render( $rule, $quantity, $args, $layout ); |
| 27 |
}, 10, 3 ); |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
public function getLabelForQuantity( PricingRule $rule, $quantity ): ?TierLabel { |
| 32 |
|
| 33 |
$labelsData = $rule->data['tier_labels'][ $rule->getType() ] ?? array(); |
| 34 |
|
| 35 |
if ( empty( $labelsData ) ) { |
| 36 |
return null; |
| 37 |
} |
| 38 |
|
| 39 |
$amountKey = null; |
| 40 |
|
| 41 |
if ( (int) $quantity === $rule->getMinimum( true ) ) { |
| 42 |
$amountKey = 'first_row'; |
| 43 |
} else { |
| 44 |
foreach ( $rule->getRules() as $tierQty => $price ) { |
| 45 |
|
| 46 |
if ( $tierQty === $quantity ) { |
| 47 |
$amountKey = $tierQty; |
| 48 |
break; |
| 49 |
} |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
if ( ! $amountKey || empty( $labelsData[ $amountKey ] ) ) { |
| 54 |
return null; |
| 55 |
} |
| 56 |
|
| 57 |
return TierLabelsManager::getInstance()->getLabel( $labelsData[ $amountKey ] ); |
| 58 |
} |
| 59 |
|
| 60 |
public function render( PricingRule $rule, $quantity, $args = array(), $layout = '' ) { |
| 61 |
|
| 62 |
$label = $this->getLabelForQuantity( $rule, $quantity ); |
| 63 |
|
| 64 |
if ( ! $label ) { |
| 65 |
return; |
| 66 |
} |
| 67 |
|
| 68 |
$args = wp_parse_args( $args, array( |
| 69 |
'id' => '', |
| 70 |
'style' => 'default', |
| 71 |
) ); |
| 72 |
|
| 73 |
$CSS = ''; |
| 74 |
|
| 75 |
// CSS Fixes |
| 76 |
if ( 'blocks' === $layout ) { |
| 77 |
if ( in_array( $args['style'], array( 'default', '1', '2', '4' ) ) ) { |
| 78 |
$CSS .= "#{$args['id']} { gap: 20px 10px }"; |
| 79 |
$CSS .= "#{$args['id']} .tiered-pricing-block { position: relative; }"; |
| 80 |
$CSS .= "#{$args['id']} .tiered-pricing-tier-label {margin: 0 !important; |
| 81 |
position: absolute; |
| 82 |
left: 50%; |
| 83 |
top: 0; |
| 84 |
transform: translate(-50%, -50%); |
| 85 |
text-transform: uppercase; |
| 86 |
z-index: 20;}"; |
| 87 |
} |
| 88 |
|
| 89 |
if ( in_array( $args['style'], array( 'default', '2', '4' ) ) ) { |
| 90 |
$CSS .= "#{$args['id']} .tiered-pricing-block:has(#" . $label->getId() . ") { padding-top: 12px; }"; |
| 91 |
} |
| 92 |
|
| 93 |
if ( $args['style'] === '1' ) { |
| 94 |
$CSS .= "#{$args['id']} .tiered-pricing-block:has(#" . $label->getId() . ") {overflow: unset !important;}"; |
| 95 |
$CSS .= "#{$args['id']} .tiered-pricing-block:has(#" . $label->getId() . ") .tiered-pricing-block__quantity {padding-top: 12px !important;}"; |
| 96 |
} |
| 97 |
} elseif ( 'table' === $layout ) { |
| 98 |
$CSS .= "#{$args['id']} tbody td:has(#" . $label->getId() . ") { display: flex; align-items: center; gap: 5px; }"; |
| 99 |
} elseif ( 'plain-text' === $layout ) { |
| 100 |
$CSS .= ".tiered-pricing-plain-text:has(#" . $label->getId() . ") {display:flex; align-items: center; gap: 5px; }"; |
| 101 |
} |
| 102 |
|
| 103 |
if ( $CSS ) { |
| 104 |
?> |
| 105 |
<style> |
| 106 |
<?php echo wp_kses_post( $CSS ); ?> |
| 107 |
</style> |
| 108 |
<?php |
| 109 |
} |
| 110 |
|
| 111 |
// Output is escaped internally in TierLabel::render() |
| 112 |
echo $label->render(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 113 |
} |
| 114 |
} |