| @@ -1,0 +1,103 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace AcyMailing\Helpers; | |
| 4 | + | |
| 5 | +use AcyMailing\Core\AcymObject; | |
| 6 | + | |
| 7 | +class TabHelper extends AcymObject | |
| 8 | +{ | |
| 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 | + | |
| 16 | + public function __construct() | |
| 17 | + { | |
| 18 | + parent::__construct(); | |
| 19 | + | |
| 20 | + $this->identifier = (string)acym_rand(1000, 9000); | |
| 21 | + } | |
| 22 | + | |
| 23 | + public function startTab(string $title, bool $selected = false, bool $clickable = true): string | |
| 24 | + { | |
| 25 | + if ($this->opened) { | |
| 26 | + $this->endTab(); | |
| 27 | + } | |
| 28 | + | |
| 29 | + $this->opened = true; | |
| 30 | + $this->identifier = preg_replace('#[^a-z0-9]#is', '_', strtolower($title)); | |
| 31 | + | |
| 32 | + $this->titles[] = [ | |
| 33 | + 'title' => $title, | |
| 34 | + 'clickable' => $clickable, | |
| 35 | + 'selected' => $selected, | |
| 36 | + 'identifier' => $this->identifier, | |
| 37 | + 'tabNumber' => $this->tabNumber, | |
| 38 | + ]; | |
| 39 | + | |
| 40 | + ob_start(); | |
| 41 | + | |
| 42 | + return $this->identifier; | |
| 43 | + } | |
| 44 | + | |
| 45 | + public function endTab(): void | |
| 46 | + { | |
| 47 | + if (!$this->opened) { | |
| 48 | + return; | |
| 49 | + } | |
| 50 | + | |
| 51 | + $this->opened = false; | |
| 52 | + $this->content[] = '<div class="tabs-panel" id="tab_'.esc_attr($this->identifier.'_'.$this->tabNumber).'">'.ob_get_clean().'</div>'; | |
| 53 | + $this->tabNumber++; | |
| 54 | + } | |
| 55 | + | |
| 56 | + public function addCallbackElement(callable $callable, string $identifier = ''): void | |
| 57 | + { | |
| 58 | + $this->inBarElements[] = [ | |
| 59 | + 'callable' => $callable, | |
| 60 | + 'identifier' => $identifier, | |
| 61 | + ]; | |
| 62 | + } | |
| 63 | + | |
| 64 | + public function display(string $tabId): void | |
| 65 | + { | |
| 66 | + if ($this->opened) { | |
| 67 | + $this->endTab(); | |
| 68 | + } | |
| 69 | + | |
| 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 | + } | |
| 81 | + | |
| 82 | + if (!empty($this->inBarElements)) { | |
| 83 | + echo '<div class="acym__tabs__inbar">'; | |
| 84 | + foreach ($this->inBarElements as $oneElement) { | |
| 85 | + echo '<div class="acym__tabs__inbar__element" '; | |
| 86 | + if (!empty($oneElement['identifier'])) { | |
| 87 | + echo 'acym-data-identifier="'.esc_attr($oneElement['identifier']).'" style="display: none;"'; | |
| 88 | + } | |
| 89 | + echo '>'; | |
| 90 | + $oneElement['callable'](); | |
| 91 | + echo '</div>'; | |
| 92 | + } | |
| 93 | + echo '</div>'; | |
| 94 | + } | |
| 95 | + | |
| 96 | + echo '</ul>'; | |
| 97 | + | |
| 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>'; | |
| 102 | + } | |
| 103 | +} | |