Tab.php
239 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | /** |
| 5 | * Class Tribe__Tabbed_View__Tab |
| 6 | * |
| 7 | * Models a tab part of a tabbed view. |
| 8 | */ |
| 9 | class Tribe__Tabbed_View__Tab { |
| 10 | |
| 11 | /** |
| 12 | * To Order the Tabs on the UI you need to change the priority |
| 13 | * |
| 14 | * @var integer |
| 15 | */ |
| 16 | public $priority = 50; |
| 17 | |
| 18 | /** |
| 19 | * An array or value object of data that should be used to render the tabbed view. |
| 20 | * |
| 21 | * @var array|object |
| 22 | */ |
| 23 | protected $data = []; |
| 24 | |
| 25 | /** |
| 26 | * The template file that should be used to render the tab. |
| 27 | * |
| 28 | * @var string |
| 29 | */ |
| 30 | protected $template; |
| 31 | |
| 32 | /** |
| 33 | * The tabbed view instance containing this tab. |
| 34 | * |
| 35 | * @var Tribe__Tabbed_View |
| 36 | */ |
| 37 | protected $tabbed_view; |
| 38 | |
| 39 | /** |
| 40 | * @var string |
| 41 | */ |
| 42 | protected $slug; |
| 43 | |
| 44 | /** |
| 45 | * @var bool |
| 46 | */ |
| 47 | protected $visible = true; |
| 48 | |
| 49 | /** |
| 50 | * @var string |
| 51 | */ |
| 52 | protected $label = ''; |
| 53 | |
| 54 | /** |
| 55 | * @var string |
| 56 | */ |
| 57 | protected $url = ''; |
| 58 | |
| 59 | /** |
| 60 | * Tribe__Tabbed_View__Tab constructor. |
| 61 | * |
| 62 | * @param Tribe__Tabbed_View $tabbed_view |
| 63 | * @param string $slug |
| 64 | */ |
| 65 | public function __construct( Tribe__Tabbed_View $tabbed_view, $slug = null ) { |
| 66 | $this->tabbed_view = $tabbed_view; |
| 67 | $this->slug = ! empty( $slug ) ? $slug : $this->slug; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * @return int |
| 72 | */ |
| 73 | public function get_priority() { |
| 74 | return $this->priority; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * @param $priority |
| 79 | */ |
| 80 | public function set_priority( $priority ) { |
| 81 | $this->priority = $priority; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * @return array|object |
| 86 | */ |
| 87 | public function get_data() { |
| 88 | return $this->data; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * @param array $data |
| 93 | */ |
| 94 | public function set_data( $data ) { |
| 95 | $this->data = $data; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * @return string |
| 100 | */ |
| 101 | public function get_template() { |
| 102 | return ! empty( $this->template ) ? $this->template : $this->get_default_template_path(); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * @param string $template |
| 107 | */ |
| 108 | public function set_template( $template ) { |
| 109 | $this->template = $template; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Returns the absolute path to the default template for the tab. |
| 114 | * |
| 115 | * @return string |
| 116 | */ |
| 117 | public function get_default_template_path() { |
| 118 | return Tribe__Main::instance()->plugin_path . '/src/admin-views/tabbed-view/tab.php'; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Whether the tab should display or not. |
| 123 | * |
| 124 | * @return boolean |
| 125 | */ |
| 126 | public function is_visible() { |
| 127 | return $this->visible; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * @param boolean $visible |
| 132 | */ |
| 133 | public function set_visible( $visible ) { |
| 134 | $this->visible = $visible; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * @return string |
| 139 | */ |
| 140 | public function get_label() { |
| 141 | return $this->label; |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * @param string $label |
| 146 | */ |
| 147 | public function set_label( $label ) { |
| 148 | $this->label = $label; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Creates a way to include the this tab HTML easily |
| 153 | * |
| 154 | * @return string HTML content of the tab |
| 155 | */ |
| 156 | public function render() { |
| 157 | if ( empty( $this->template ) ) { |
| 158 | $this->template = Tribe__Main::instance()->plugin_path . '/src/admin-views/tabbed-view/tab.php'; |
| 159 | } |
| 160 | |
| 161 | $template = $this->template; |
| 162 | |
| 163 | if ( empty( $template ) ) { |
| 164 | return ''; |
| 165 | } |
| 166 | |
| 167 | $default_data = [ |
| 168 | 'tab' => $this, |
| 169 | ]; |
| 170 | |
| 171 | $data = array_merge( $default_data, (array) $this->data ); |
| 172 | |
| 173 | extract( $data ); |
| 174 | |
| 175 | ob_start(); |
| 176 | |
| 177 | include $template; |
| 178 | |
| 179 | $html = ob_get_clean(); |
| 180 | |
| 181 | return $html; |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Returns the link to this tab |
| 186 | * |
| 187 | * @param array|string $args Query String or Array with the arguments |
| 188 | * @param boolean $relative Return a relative URL or absolute |
| 189 | * |
| 190 | * @return string |
| 191 | */ |
| 192 | public function get_url( $args = [], $relative = false ) { |
| 193 | if ( ! empty( $this->url ) ) { |
| 194 | return $this->url; |
| 195 | } |
| 196 | |
| 197 | $defaults = [ |
| 198 | 'tab' => $this->get_slug(), |
| 199 | ]; |
| 200 | |
| 201 | // Allow the link to be "changed" on the fly |
| 202 | $args = wp_parse_args( $args, $defaults ); |
| 203 | |
| 204 | // Escape after the filter |
| 205 | return $this->tabbed_view->get_url( $args, $relative ); |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Sets this tab URL. |
| 210 | * |
| 211 | * This URL will override the tab natural URL. |
| 212 | * |
| 213 | * @param string $url |
| 214 | */ |
| 215 | public function set_url( $url ) { |
| 216 | $this->url = $url; |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Returns the tab slug |
| 221 | * |
| 222 | * @return string |
| 223 | */ |
| 224 | public function get_slug() { |
| 225 | return $this->slug; |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Determines if this Tab is currently displayed |
| 230 | * |
| 231 | * @return boolean |
| 232 | */ |
| 233 | public function is_active() { |
| 234 | $active = $this->tabbed_view->get_active(); |
| 235 | |
| 236 | return ! empty( $active ) ? $this->get_slug() === $active->get_slug() : false; |
| 237 | } |
| 238 | } |
| 239 |