| 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 |
|
| 20 |
public function __construct( $id, $args = array() ) { |
| 21 |
$args = wp_parse_args( $args, array( |
| 22 |
'title' => '', |
| 23 |
'background_color' => '#333', |
| 24 |
'text_color' => '#fff', |
| 25 |
'border_color' => '', |
| 26 |
'style' => 'square', |
| 27 |
'css_class' => '', |
| 28 |
'font_size' => 12, |
| 29 |
'border_width' => 0, |
| 30 |
'padding_x' => 8, |
| 31 |
'padding_y' => 2, |
| 32 |
'border_radius' => 4, |
| 33 |
'border_style' => 'solid', |
| 34 |
) ); |
| 35 |
|
| 36 |
$this->id = $id; |
| 37 |
$this->title = $args['title']; |
| 38 |
$this->backgroundColor = $args['background_color']; |
| 39 |
$this->textColor = $args['text_color']; |
| 40 |
$this->borderColor = $args['border_color']; |
| 41 |
$this->style = $args['style']; |
| 42 |
$this->CSSClass = $args['css_class']; |
| 43 |
$this->fontSize = intval( $args['font_size'] ); |
| 44 |
$this->borderWidth = intval( $args['border_width'] ); |
| 45 |
$this->paddingX = intval( $args['padding_x'] ); |
| 46 |
$this->paddingY = intval( $args['padding_y'] ); |
| 47 |
$this->borderRadius = intval( $args['border_radius'] ); |
| 48 |
$this->borderStyle = $args['border_style']; |
| 49 |
} |
| 50 |
|
| 51 |
public function getId() { |
| 52 |
return $this->id; |
| 53 |
} |
| 54 |
|
| 55 |
public function getTitle() { |
| 56 |
return $this->title; |
| 57 |
} |
| 58 |
|
| 59 |
public function getBackgroundColor() { |
| 60 |
return $this->backgroundColor; |
| 61 |
} |
| 62 |
|
| 63 |
public function getTextColor() { |
| 64 |
return $this->textColor; |
| 65 |
} |
| 66 |
|
| 67 |
public function getStyle() { |
| 68 |
return $this->style; |
| 69 |
} |
| 70 |
|
| 71 |
public function getBorderColor() { |
| 72 |
return $this->borderColor; |
| 73 |
} |
| 74 |
|
| 75 |
public function getFontSize() { |
| 76 |
return $this->fontSize; |
| 77 |
} |
| 78 |
|
| 79 |
public function getBorderWidth() { |
| 80 |
return $this->borderWidth; |
| 81 |
} |
| 82 |
|
| 83 |
public function getPaddingX() { |
| 84 |
return $this->paddingX; |
| 85 |
} |
| 86 |
|
| 87 |
public function getPaddingY() { |
| 88 |
return $this->paddingY; |
| 89 |
} |
| 90 |
|
| 91 |
public function getBorderRadius() { |
| 92 |
return $this->borderRadius; |
| 93 |
} |
| 94 |
|
| 95 |
public function getBorderStyle() { |
| 96 |
return $this->borderStyle; |
| 97 |
} |
| 98 |
|
| 99 |
public function getCSSClass(): ?string { |
| 100 |
return $this->CSSClass; |
| 101 |
} |
| 102 |
|
| 103 |
public function toArray(): array { |
| 104 |
return array( |
| 105 |
'id' => $this->getId(), |
| 106 |
'title' => $this->getTitle(), |
| 107 |
'background_color' => $this->getBackgroundColor(), |
| 108 |
'text_color' => $this->getTextColor(), |
| 109 |
'border_color' => $this->getBorderColor(), |
| 110 |
'style' => $this->getStyle(), |
| 111 |
'css_class' => $this->getCSSClass(), |
| 112 |
'font_size' => $this->getFontSize(), |
| 113 |
'border_width' => $this->getBorderWidth(), |
| 114 |
'padding_x' => $this->getPaddingX(), |
| 115 |
'padding_y' => $this->getPaddingY(), |
| 116 |
'border_radius' => $this->getBorderRadius(), |
| 117 |
'border_style' => $this->getBorderStyle(), |
| 118 |
); |
| 119 |
} |
| 120 |
|
| 121 |
public function isValid(): bool { |
| 122 |
return ! empty( $this->id ) && ! empty( $this->title ); |
| 123 |
} |
| 124 |
|
| 125 |
public static function fromArray( array $data ): self { |
| 126 |
return new self( $data['id'], $data ); |
| 127 |
} |
| 128 |
|
| 129 |
public function render(): string { |
| 130 |
$style = ''; |
| 131 |
|
| 132 |
$bgColor = $this->getBackgroundColor() ?: '#2271b1'; |
| 133 |
$textColor = $this->getTextColor() ?: '#ffffff'; |
| 134 |
$borderColor = $this->getBorderColor(); |
| 135 |
|
| 136 |
$fontSize = $this->getFontSize() ?: 12; |
| 137 |
$borderWidth = $this->getBorderWidth() !== null ? $this->getBorderWidth() : 0; |
| 138 |
$paddingX = $this->getPaddingX() !== null ? $this->getPaddingX() : 8; |
| 139 |
$paddingY = $this->getPaddingY() !== null ? $this->getPaddingY() : 2; |
| 140 |
$borderRadius = $this->getBorderRadius() !== null ? $this->getBorderRadius() : 4; |
| 141 |
$borderStyle = $this->getBorderStyle() ?: 'solid'; |
| 142 |
|
| 143 |
if ( $borderStyle === 'outline' || $borderStyle === 'dashed' ) { |
| 144 |
$style .= 'background-color: transparent;'; |
| 145 |
} else { |
| 146 |
$style .= 'background-color: ' . esc_attr( $bgColor ) . ';'; |
| 147 |
} |
| 148 |
|
| 149 |
$style .= 'color: ' . esc_attr( $textColor ) . ';'; |
| 150 |
$style .= 'padding: ' . esc_attr( $paddingY ) . 'px ' . esc_attr( $paddingX ) . 'px;'; |
| 151 |
$style .= 'font-size: ' . esc_attr( $fontSize ) . 'px;'; |
| 152 |
$style .= 'font-weight: 600;'; |
| 153 |
$style .= 'display: inline-block;'; |
| 154 |
$style .= 'white-space: nowrap;'; |
| 155 |
$style .= 'overflow: hidden;'; |
| 156 |
$style .= 'width: max-content;'; |
| 157 |
$style .= 'border-radius: ' . esc_attr( $borderRadius ) . 'px;'; |
| 158 |
|
| 159 |
if ( $borderStyle === 'dashed' ) { |
| 160 |
$actualBorderColor = $borderColor ?: $textColor; |
| 161 |
$style .= 'border: ' . esc_attr( $borderWidth ?: 2 ) . 'px dashed ' . esc_attr( $actualBorderColor ) . ';'; |
| 162 |
} elseif ( $borderStyle === 'outline' ) { |
| 163 |
$actualBorderColor = $borderColor ?: $textColor; |
| 164 |
$style .= 'border: ' . esc_attr( $borderWidth ?: 2 ) . 'px solid ' . esc_attr( $actualBorderColor ) . ';'; |
| 165 |
} elseif ( $borderWidth > 0 ) { |
| 166 |
$style .= 'border: ' . esc_attr( $borderWidth ) . 'px solid ' . esc_attr( $borderColor ?: '#000000' ) . ';'; |
| 167 |
} |
| 168 |
|
| 169 |
$cssClass = $this->getCSSClass() ? ' ' . esc_attr( $this->getCSSClass() ) : ''; |
| 170 |
|
| 171 |
$labelHTML = '<span id="' . esc_attr( $this->getId() ) . '" class="tiered-pricing-tier-label' . $cssClass . '" style="' . $style . '">' . esc_html( $this->getTitle() ) . '</span>'; |
| 172 |
|
| 173 |
return apply_filters( 'tiered_pricing_table/addons/tier_labels/label_html', $labelHTML, $this ); |
| 174 |
} |
| 175 |
} |