| 1 |
<?php namespace TierPricingTable\Addons\TierLabels; |
| 2 |
|
| 3 |
class TierLabel { |
| 4 |
|
| 5 |
private $id; |
| 6 |
private $title; |
| 7 |
private $backgroundColor; |
| 8 |
private $textColor; |
| 9 |
private $style; |
| 10 |
private $borderColor; |
| 11 |
private $CSSClass; |
| 12 |
|
| 13 |
private $fontSize; |
| 14 |
private $borderWidth; |
| 15 |
private $paddingX; |
| 16 |
private $paddingY; |
| 17 |
private $borderRadius; |
| 18 |
private $borderStyle; |
| 19 |
private $icon; |
| 20 |
|
| 21 |
public function __construct( $id, $args = array() ) { |
| 22 |
$args = wp_parse_args( $args, array( |
| 23 |
'title' => '', |
| 24 |
'background_color' => '#333', |
| 25 |
'text_color' => '#fff', |
| 26 |
'border_color' => '', |
| 27 |
'style' => 'square', |
| 28 |
'css_class' => '', |
| 29 |
'font_size' => 12, |
| 30 |
'border_width' => 0, |
| 31 |
'padding_x' => 8, |
| 32 |
'padding_y' => 2, |
| 33 |
'border_radius' => 4, |
| 34 |
'border_style' => 'solid', |
| 35 |
'icon' => '', |
| 36 |
) ); |
| 37 |
|
| 38 |
$this->id = $id; |
| 39 |
$this->title = $args['title']; |
| 40 |
$this->backgroundColor = $args['background_color']; |
| 41 |
$this->textColor = $args['text_color']; |
| 42 |
$this->borderColor = $args['border_color']; |
| 43 |
$this->style = $args['style']; |
| 44 |
$this->CSSClass = $args['css_class']; |
| 45 |
$this->fontSize = isset($args['font_size']) ? (int) $args['font_size'] : 12; |
| 46 |
$this->borderWidth = isset($args['border_width']) ? (int) $args['border_width'] : 0; |
| 47 |
$this->paddingX = isset($args['padding_x']) ? (int) $args['padding_x'] : 8; |
| 48 |
$this->paddingY = isset($args['padding_y']) ? (int) $args['padding_y'] : 2; |
| 49 |
$this->borderRadius = isset($args['border_radius']) ? (int) $args['border_radius'] : 4; |
| 50 |
$this->borderStyle = $args['border_style'] ?? 'solid'; |
| 51 |
$this->icon = $args['icon'] ?? ''; |
| 52 |
} |
| 53 |
|
| 54 |
public function getId() { |
| 55 |
return $this->id; |
| 56 |
} |
| 57 |
|
| 58 |
public function getTitle() { |
| 59 |
return $this->title; |
| 60 |
} |
| 61 |
|
| 62 |
public function getBackgroundColor() { |
| 63 |
return $this->backgroundColor; |
| 64 |
} |
| 65 |
|
| 66 |
public function getTextColor() { |
| 67 |
return $this->textColor; |
| 68 |
} |
| 69 |
|
| 70 |
public function getStyle() { |
| 71 |
return $this->style; |
| 72 |
} |
| 73 |
|
| 74 |
public function getBorderColor() { |
| 75 |
return $this->borderColor; |
| 76 |
} |
| 77 |
|
| 78 |
public function getFontSize() { |
| 79 |
return $this->fontSize; |
| 80 |
} |
| 81 |
|
| 82 |
public function getBorderWidth() { |
| 83 |
return $this->borderWidth; |
| 84 |
} |
| 85 |
|
| 86 |
public function getPaddingX() { |
| 87 |
return $this->paddingX; |
| 88 |
} |
| 89 |
|
| 90 |
public function getPaddingY() { |
| 91 |
return $this->paddingY; |
| 92 |
} |
| 93 |
|
| 94 |
public function getBorderRadius() { |
| 95 |
return $this->borderRadius; |
| 96 |
} |
| 97 |
|
| 98 |
public function getBorderStyle(): string { |
| 99 |
return $this->borderStyle; |
| 100 |
} |
| 101 |
|
| 102 |
public function getIcon(): string { |
| 103 |
return $this->icon; |
| 104 |
} |
| 105 |
|
| 106 |
public function getCSSClass(): ?string { |
| 107 |
return $this->CSSClass; |
| 108 |
} |
| 109 |
|
| 110 |
public function toArray(): array { |
| 111 |
return array( |
| 112 |
'id' => $this->id, |
| 113 |
'title' => $this->title, |
| 114 |
'background_color' => $this->backgroundColor, |
| 115 |
'text_color' => $this->textColor, |
| 116 |
'border_color' => $this->borderColor, |
| 117 |
'style' => $this->style, |
| 118 |
'css_class' => $this->CSSClass, |
| 119 |
'font_size' => $this->fontSize, |
| 120 |
'border_width' => $this->borderWidth, |
| 121 |
'padding_x' => $this->paddingX, |
| 122 |
'padding_y' => $this->paddingY, |
| 123 |
'border_radius' => $this->borderRadius, |
| 124 |
'border_style' => $this->borderStyle, |
| 125 |
'icon' => $this->icon, |
| 126 |
); |
| 127 |
} |
| 128 |
|
| 129 |
public function isValid(): bool { |
| 130 |
return ! empty( $this->id ) && ! empty( $this->title ); |
| 131 |
} |
| 132 |
|
| 133 |
public static function fromArray( array $data ): self { |
| 134 |
return new self( $data['id'], $data ); |
| 135 |
} |
| 136 |
|
| 137 |
public function render(): string { |
| 138 |
$style = ''; |
| 139 |
|
| 140 |
$bgColor = $this->getBackgroundColor() ?: '#2271b1'; |
| 141 |
$textColor = $this->getTextColor() ?: '#ffffff'; |
| 142 |
$borderColor = $this->getBorderColor(); |
| 143 |
|
| 144 |
$fontSize = $this->getFontSize() ?: 12; |
| 145 |
$borderWidth = $this->getBorderWidth() !== null ? $this->getBorderWidth() : 0; |
| 146 |
$paddingX = $this->getPaddingX() !== null ? $this->getPaddingX() : 8; |
| 147 |
$paddingY = $this->getPaddingY() !== null ? $this->getPaddingY() : 2; |
| 148 |
$borderRadius = $this->getBorderRadius() !== null ? $this->getBorderRadius() : 4; |
| 149 |
$borderStyle = $this->getBorderStyle() ?: 'solid'; |
| 150 |
|
| 151 |
if ( $borderStyle === 'outline' || $borderStyle === 'dashed' ) { |
| 152 |
$style .= 'background-color: transparent;'; |
| 153 |
} else { |
| 154 |
$style .= 'background-color: ' . esc_attr( $bgColor ) . ';'; |
| 155 |
} |
| 156 |
|
| 157 |
$style .= 'color: ' . esc_attr( $textColor ) . ';'; |
| 158 |
$style .= 'padding: ' . esc_attr( $paddingY ) . 'px ' . esc_attr( $paddingX ) . 'px;'; |
| 159 |
$style .= 'font-size: ' . esc_attr( $fontSize ) . 'px;'; |
| 160 |
$style .= 'font-weight: 600;'; |
| 161 |
$style .= 'display: inline-block;'; |
| 162 |
$style .= 'white-space: nowrap;'; |
| 163 |
$style .= 'overflow: hidden;'; |
| 164 |
$style .= 'width: max-content;'; |
| 165 |
$style .= 'border-radius: ' . esc_attr( $borderRadius ) . 'px;'; |
| 166 |
|
| 167 |
if ( $borderStyle === 'dashed' ) { |
| 168 |
$actualBorderColor = $borderColor ?: $textColor; |
| 169 |
$style .= 'border: ' . esc_attr( $borderWidth ?: 2 ) . 'px dashed ' . esc_attr( $actualBorderColor ) . ';'; |
| 170 |
} elseif ( $borderStyle === 'outline' ) { |
| 171 |
$actualBorderColor = $borderColor ?: $textColor; |
| 172 |
$style .= 'border: ' . esc_attr( $borderWidth ?: 2 ) . 'px solid ' . esc_attr( $actualBorderColor ) . ';'; |
| 173 |
} elseif ( $borderWidth > 0 ) { |
| 174 |
$style .= 'border: ' . esc_attr( $borderWidth ) . 'px solid ' . esc_attr( $borderColor ?: '#000000' ) . ';'; |
| 175 |
} |
| 176 |
|
| 177 |
$cssClass = $this->getCSSClass() ? ' ' . esc_attr( $this->getCSSClass() ) : ''; |
| 178 |
|
| 179 |
$iconSvg = ''; |
| 180 |
if ( $this->getIcon() ) { |
| 181 |
$iconSvg = $this->getIconSvg( $this->getIcon() ); |
| 182 |
// Adjust layout for icon + text |
| 183 |
$style .= 'display: inline-flex; align-items: center; justify-content: center; gap: 2px;'; |
| 184 |
} |
| 185 |
|
| 186 |
$labelHTML = '<span id="' . esc_attr( $this->getId() ) . '" class="tiered-pricing-tier-label' . $cssClass . '" style="' . $style . '">' . $iconSvg . esc_html( $this->getTitle() ) . '</span>'; |
| 187 |
|
| 188 |
return apply_filters( 'tiered_pricing_table/addons/tier_labels/label_html', $labelHTML, $this ); |
| 189 |
} |
| 190 |
|
| 191 |
private function getIconSvg( string $iconName ): string { |
| 192 |
$icons = array( |
| 193 |
'star' => '<svg width="1em" height="1em" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polygon points="12 2 15.09 8.26 22 9.27 17 14.14 18.18 21.02 12 17.77 5.82 21.02 7 14.14 2 9.27 8.91 8.26 12 2"></polygon></svg>', |
| 194 |
'fire' => '<svg width="1em" height="1em" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M8.5 14.5A2.5 2.5 0 0 0 11 12c0-1.38-.5-2-1-3-1.072-2.143-.224-4.054 2-6 .5 2.5 2 4.9 4 6.5 2 1.6 3 3.5 3 5.5a7 7 0 1 1-14 0c0-1.153.433-2.294 1-3a2.5 2.5 0 0 0 2.5 2.5z"></path></svg>', |
| 195 |
'lightning' => '<svg width="1em" height="1em" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polygon points="13 2 3 14 12 14 11 22 21 10 12 10 13 2"></polygon></svg>', |
| 196 |
'tag' => '<svg width="1em" height="1em" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M20.59 13.41l-7.17 7.17a2 2 0 0 1-2.83 0L2 12V2h10l8.59 8.59a2 2 0 0 1 0 2.82z"></path><line x1="7" y1="7" x2="7.01" y2="7"></line></svg>', |
| 197 |
'crown' => '<svg width="1em" height="1em" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="m2 4 3 12h14l3-12-6 7-4-7-4 7-6-7zm3 16h14"></path></svg>', |
| 198 |
'percent' => '<svg width="1em" height="1em" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="19" y1="5" x2="5" y2="19"></line><circle cx="6.5" cy="6.5" r="2.5"></circle><circle cx="17.5" cy="17.5" r="2.5"></circle></svg>', |
| 199 |
'award' => '<svg width="1em" height="1em" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="8" r="7"></circle><polyline points="8.21 13.89 7 23 12 20 17 23 15.79 13.88"></polyline></svg>', |
| 200 |
); |
| 201 |
|
| 202 |
return $icons[$iconName] ?? ''; |
| 203 |
} |
| 204 |
} |