| @@ -5,82 +5,99 @@ | ||
| 5 | 5 | use AcyMailing\Core\AcymObject; |
| 6 | 6 | |
| 7 | 7 | class TabHelper extends AcymObject |
| 8 | 8 | { |
| 9 | - var $titles = []; | |
| 10 | - var $content = []; | |
| 11 | - var $tabNumber = 0; | |
| 12 | - var $opened = false; | |
| 13 | - var $identifier; | |
| 14 | - var $inBarElements = []; | |
| 9 | + private array $titles = []; | |
| 10 | + private array $content = []; | |
| 11 | + private array $inBarElements = []; | |
| 12 | + private int $tabNumber = 0; | |
| 13 | + private bool $opened = false; | |
| 14 | + private string $identifier; | |
| 15 | 15 | |
| 16 | 16 | public function __construct() |
| 17 | 17 | { |
| 18 | 18 | parent::__construct(); |
| 19 | - $this->identifier = rand(1000, 9000); | |
| 19 | + | |
| 20 | + $this->identifier = (string)acym_rand(1000, 9000); | |
| 20 | 21 | } |
| 21 | 22 | |
| 22 | - public function startTab($title, $selected = false, $attributes = '', $clickable = true) | |
| 23 | + public function startTab(string $title, bool $selected = false, bool $clickable = true): string | |
| 23 | 24 | { |
| 24 | - if ($this->opened) $this->endTab(); | |
| 25 | + if ($this->opened) { | |
| 26 | + $this->endTab(); | |
| 27 | + } | |
| 28 | + | |
| 25 | 29 | $this->opened = true; |
| 30 | + $this->identifier = preg_replace('#[^a-z0-9]#is', '_', strtolower($title)); | |
| 26 | 31 | |
| 27 | - $attributes .= $clickable ? '' : 'data-empty="true"'; | |
| 28 | - $attributes .= $selected ? 'data-selected="true"' : ''; | |
| 29 | - $classLi = $clickable ? '' : 'tabs-title-empty'; | |
| 32 | + $this->titles[] = [ | |
| 33 | + 'title' => $title, | |
| 34 | + 'clickable' => $clickable, | |
| 35 | + 'selected' => $selected, | |
| 36 | + 'identifier' => $this->identifier, | |
| 37 | + 'tabNumber' => $this->tabNumber, | |
| 38 | + ]; | |
| 30 | 39 | |
| 31 | - $this->identifier = preg_replace('#[^a-z0-9]#is', '_', strtolower((string)$title)); | |
| 32 | - | |
| 33 | - $this->titles[] = '<li class="tabs-title '.$classLi.'"><a class="acym_tab acym__color__medium-gray" '.$attributes.' href="#" data-tab-identifier="'.$this->identifier.'" data-tabs-target="tab_'.$this->identifier.'_'.$this->tabNumber.'">'.$title.'</a></li>'; | |
| 34 | - | |
| 35 | 40 | ob_start(); |
| 36 | 41 | |
| 37 | 42 | return $this->identifier; |
| 38 | 43 | } |
| 39 | 44 | |
| 40 | - public function endTab() | |
| 45 | + public function endTab(): void | |
| 41 | 46 | { |
| 42 | - if (!$this->opened) return; | |
| 47 | + if (!$this->opened) { | |
| 48 | + return; | |
| 49 | + } | |
| 43 | 50 | |
| 44 | 51 | $this->opened = false; |
| 45 | - $this->content[] = '<div class="tabs-panel" id="tab_'.$this->identifier.'_'.$this->tabNumber.'">'.ob_get_clean().'</div>'; | |
| 52 | + $this->content[] = '<div class="tabs-panel" id="tab_'.esc_attr($this->identifier.'_'.$this->tabNumber).'">'.ob_get_clean().'</div>'; | |
| 46 | 53 | $this->tabNumber++; |
| 47 | 54 | } |
| 48 | 55 | |
| 49 | - public function addElementInBar($element, $identifier = '') | |
| 56 | + public function addCallbackElement(callable $callable, string $identifier = ''): void | |
| 50 | 57 | { |
| 51 | 58 | $this->inBarElements[] = [ |
| 52 | - 'element' => $element, | |
| 59 | + 'callable' => $callable, | |
| 53 | 60 | 'identifier' => $identifier, |
| 54 | 61 | ]; |
| 55 | 62 | } |
| 56 | 63 | |
| 57 | - public function display($id) | |
| 64 | + public function display(string $tabId): void | |
| 58 | 65 | { |
| 59 | 66 | if ($this->opened) { |
| 60 | 67 | $this->endTab(); |
| 61 | 68 | } |
| 62 | 69 | |
| 63 | - $tabSystem = '<ul class="tabs" data-tabs id="'.acym_escape($id).'">'; | |
| 64 | - $tabSystem .= implode('', $this->titles); | |
| 70 | + echo '<ul class="tabs" data-tabs id="'.esc_attr($tabId).'">'; | |
| 71 | + foreach ($this->titles as $title) { | |
| 72 | + echo '<li class="tabs-title '.esc_attr($title['clickable'] ? '' : 'tabs-title-empty').'"> | |
| 73 | + <a class="acym_tab acym__color__medium-gray" | |
| 74 | + '.($title['clickable'] ? '' : 'data-empty="true"').' | |
| 75 | + '.($title['selected'] ? 'data-selected="true"' : '').' | |
| 76 | + href="#" | |
| 77 | + data-tab-identifier="'.esc_attr($title['identifier']).'" | |
| 78 | + data-tabs-target="tab_'.esc_attr($title['identifier'].'_'.$title['tabNumber']).'">'.esc_html($title['title']).'</a> | |
| 79 | + </li>'; | |
| 80 | + } | |
| 65 | 81 | |
| 66 | 82 | if (!empty($this->inBarElements)) { |
| 67 | - $tabSystem .= '<div class="acym__tabs__inbar">'; | |
| 83 | + echo '<div class="acym__tabs__inbar">'; | |
| 68 | 84 | foreach ($this->inBarElements as $oneElement) { |
| 69 | - $displayOptions = ''; | |
| 85 | + echo '<div class="acym__tabs__inbar__element" '; | |
| 70 | 86 | if (!empty($oneElement['identifier'])) { |
| 71 | - $displayOptions = 'acym-data-identifier="'.acym_escape($oneElement['identifier']).'" style="display: none;"'; | |
| 87 | + echo 'acym-data-identifier="'.esc_attr($oneElement['identifier']).'" style="display: none;"'; | |
| 72 | 88 | } |
| 73 | - $tabSystem .= '<div class="acym__tabs__inbar__element" '.$displayOptions.'>'.$oneElement['element'].'</div>'; | |
| 89 | + echo '>'; | |
| 90 | + $oneElement['callable'](); | |
| 91 | + echo '</div>'; | |
| 74 | 92 | } |
| 75 | - $tabSystem .= '</div>'; | |
| 93 | + echo '</div>'; | |
| 76 | 94 | } |
| 77 | 95 | |
| 78 | - $tabSystem .= '</ul>'; | |
| 96 | + echo '</ul>'; | |
| 79 | 97 | |
| 80 | - $tabSystem .= '<div class="tabs-content margin-bottom-1" data-tabs-content="'.acym_escape($id).'">'; | |
| 81 | - $tabSystem .= implode('', $this->content); | |
| 82 | - $tabSystem .= '</div>'; | |
| 83 | - | |
| 84 | - echo $tabSystem; | |
| 98 | + echo '<div class="tabs-content margin-bottom-1" data-tabs-content="'.esc_attr($tabId).'">'; | |
| 99 | + // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Content escaped from the add-on/plugin side. | |
| 100 | + echo implode('', $this->content); | |
| 101 | + echo '</div>'; | |
| 85 | 102 | } |
| 86 | 103 | } |