'',
'background_color' => '#333',
'text_color' => '#fff',
'border_color' => '',
'style' => 'square',
'css_class' => '',
'font_size' => 12,
'border_width' => 0,
'padding_x' => 8,
'padding_y' => 2,
'border_radius' => 4,
'border_style' => 'solid',
'icon' => '',
) );
$this->id = $id;
$this->title = $args['title'];
$this->backgroundColor = $args['background_color'];
$this->textColor = $args['text_color'];
$this->borderColor = $args['border_color'];
$this->style = $args['style'];
$this->CSSClass = $args['css_class'];
$this->fontSize = isset($args['font_size']) ? (int) $args['font_size'] : 12;
$this->borderWidth = isset($args['border_width']) ? (int) $args['border_width'] : 0;
$this->paddingX = isset($args['padding_x']) ? (int) $args['padding_x'] : 8;
$this->paddingY = isset($args['padding_y']) ? (int) $args['padding_y'] : 2;
$this->borderRadius = isset($args['border_radius']) ? (int) $args['border_radius'] : 4;
$this->borderStyle = $args['border_style'] ?? 'solid';
$this->icon = $args['icon'] ?? '';
}
public function getId() {
return $this->id;
}
public function getTitle() {
return $this->title;
}
public function getBackgroundColor() {
return $this->backgroundColor;
}
public function getTextColor() {
return $this->textColor;
}
public function getStyle() {
return $this->style;
}
public function getBorderColor() {
return $this->borderColor;
}
public function getFontSize() {
return $this->fontSize;
}
public function getBorderWidth() {
return $this->borderWidth;
}
public function getPaddingX() {
return $this->paddingX;
}
public function getPaddingY() {
return $this->paddingY;
}
public function getBorderRadius() {
return $this->borderRadius;
}
public function getBorderStyle(): string {
return $this->borderStyle;
}
public function getIcon(): string {
return $this->icon;
}
public function getCSSClass(): ?string {
return $this->CSSClass;
}
public function toArray(): array {
return array(
'id' => $this->id,
'title' => $this->title,
'background_color' => $this->backgroundColor,
'text_color' => $this->textColor,
'border_color' => $this->borderColor,
'style' => $this->style,
'css_class' => $this->CSSClass,
'font_size' => $this->fontSize,
'border_width' => $this->borderWidth,
'padding_x' => $this->paddingX,
'padding_y' => $this->paddingY,
'border_radius' => $this->borderRadius,
'border_style' => $this->borderStyle,
'icon' => $this->icon,
);
}
public function isValid(): bool {
return ! empty( $this->id ) && ! empty( $this->title );
}
public static function fromArray( array $data ): self {
return new self( $data['id'], $data );
}
public function render(): string {
$style = '';
$bgColor = $this->getBackgroundColor() ?: '#2271b1';
$textColor = $this->getTextColor() ?: '#ffffff';
$borderColor = $this->getBorderColor();
$fontSize = $this->getFontSize() ?: 12;
$borderWidth = $this->getBorderWidth() !== null ? $this->getBorderWidth() : 0;
$paddingX = $this->getPaddingX() !== null ? $this->getPaddingX() : 8;
$paddingY = $this->getPaddingY() !== null ? $this->getPaddingY() : 2;
$borderRadius = $this->getBorderRadius() !== null ? $this->getBorderRadius() : 4;
$borderStyle = $this->getBorderStyle() ?: 'solid';
if ( $borderStyle === 'outline' || $borderStyle === 'dashed' ) {
$style .= 'background-color: transparent;';
} else {
$style .= 'background-color: ' . esc_attr( $bgColor ) . ';';
}
$style .= 'color: ' . esc_attr( $textColor ) . ';';
$style .= 'padding: ' . esc_attr( $paddingY ) . 'px ' . esc_attr( $paddingX ) . 'px;';
$style .= 'font-size: ' . esc_attr( $fontSize ) . 'px;';
$style .= 'font-weight: 600;';
$style .= 'display: inline-block;';
$style .= 'white-space: nowrap;';
$style .= 'overflow: hidden;';
$style .= 'width: max-content;';
$style .= 'border-radius: ' . esc_attr( $borderRadius ) . 'px;';
if ( $borderStyle === 'dashed' ) {
$actualBorderColor = $borderColor ?: $textColor;
$style .= 'border: ' . esc_attr( $borderWidth ?: 2 ) . 'px dashed ' . esc_attr( $actualBorderColor ) . ';';
} elseif ( $borderStyle === 'outline' ) {
$actualBorderColor = $borderColor ?: $textColor;
$style .= 'border: ' . esc_attr( $borderWidth ?: 2 ) . 'px solid ' . esc_attr( $actualBorderColor ) . ';';
} elseif ( $borderWidth > 0 ) {
$style .= 'border: ' . esc_attr( $borderWidth ) . 'px solid ' . esc_attr( $borderColor ?: '#000000' ) . ';';
}
$cssClass = $this->getCSSClass() ? ' ' . esc_attr( $this->getCSSClass() ) : '';
$iconSvg = '';
if ( $this->getIcon() ) {
$iconSvg = $this->getIconSvg( $this->getIcon() );
// Adjust layout for icon + text
$style .= 'display: inline-flex; align-items: center; justify-content: center; gap: 2px;';
}
$labelHTML = '' . $iconSvg . esc_html( $this->getTitle() ) . '';
return apply_filters( 'tiered_pricing_table/addons/tier_labels/label_html', $labelHTML, $this );
}
private function getIconSvg( string $iconName ): string {
$icons = array(
'star' => '',
'fire' => '',
'lightning' => '',
'tag' => '',
'crown' => '',
'percent' => '',
'award' => '',
);
return $icons[$iconName] ?? '';
}
}