PageRenderTime 64ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/the-events-calendar/vendor/tickets/common/src/Tribe/Tabbed_View.php

https://bitbucket.org/RickCalder/mcc
PHP | 381 lines | 146 code | 64 blank | 171 comment | 21 complexity | 6bfba5b3ab9ee2cf31f32c92511693e2 MD5 | raw file
Possible License(s): MIT, Apache-2.0, GPL-3.0, BSD-3-Clause
  1. <?php
  2. /**
  3. * Class Tribe__Tabbed_View
  4. *
  5. * Models a tabbed view containing tabs.
  6. */
  7. class Tribe__Tabbed_View {
  8. /**
  9. * A list of all the tabs registered for the tabbed view.
  10. *
  11. * @var array An associative array in the [<slug> => <instance>] format.
  12. */
  13. protected $items = array();
  14. /**
  15. * The slug of the default tab
  16. *
  17. * @var string
  18. */
  19. protected $default_tab;
  20. /**
  21. * @var string The absolute path to this tabbed view template file.
  22. */
  23. protected $template;
  24. /**
  25. * An array or value object of data that should be used to render the tabbed view.
  26. *
  27. * @var array|object
  28. */
  29. protected $data = array();
  30. /**
  31. * @var string
  32. */
  33. protected $label;
  34. /**
  35. * @var string
  36. */
  37. protected $url;
  38. /**
  39. * @var string
  40. */
  41. protected $active;
  42. /**
  43. * Returns the tabbed view URL.
  44. *
  45. * @param array|string $args Query String or Array with the arguments
  46. * @param boolean $relative Return a relative URL or absolute
  47. *
  48. * @return string
  49. */
  50. public function get_url( $args, $relative ) {
  51. $relative_path = add_query_arg( $args, $this->url );
  52. return $relative ? $relative_path : admin_url( $relative_path );
  53. }
  54. /**
  55. * The currently set template for this tabbed view.
  56. *
  57. * @return string
  58. */
  59. public function get_template() {
  60. return ! empty( $this->template ) ? $this->template : $this->get_default_template_path();
  61. }
  62. /**
  63. * @param string $template
  64. */
  65. public function set_template( $template ) {
  66. $this->template = $template;
  67. }
  68. /**
  69. * @return string
  70. */
  71. public function get_label() {
  72. return $this->label;
  73. }
  74. /**
  75. * @param string $label
  76. */
  77. public function set_label( $label ) {
  78. $this->label = $label;
  79. }
  80. /**
  81. * Returns only the visible tabs for this tabbed view.
  82. *
  83. * @return Tribe__Tabbed_View__Tab[] An array of all the active and visible tabs.
  84. */
  85. public function get_visibles() {
  86. return array_filter( $this->get(), array( $this, 'is_tab_visible' ) );
  87. }
  88. /**
  89. * @param string $url
  90. */
  91. public function set_url( $url ) {
  92. $this->url = $url;
  93. }
  94. /**
  95. * Sets the slug of the currently active tab.
  96. *
  97. * This value, if the tab exists, will override the value specified in the GET request.
  98. *
  99. * @param string $active
  100. */
  101. public function set_active( $active ) {
  102. $this->active = $active;
  103. }
  104. /**
  105. * A method to sort tabs by priority in ascending order.
  106. *
  107. * @param object $a First tab to compare
  108. * @param object $b Second tab to compare
  109. *
  110. * @return int
  111. */
  112. protected function sort_by_priority( $a, $b ) {
  113. $a_priority = $a->get_priority();
  114. $b_priority = $b->get_priority();
  115. if ( $a_priority == $b_priority ) {
  116. return 0;
  117. }
  118. return ( $a_priority < $b_priority ) ? - 1 : 1;
  119. }
  120. /**
  121. * Removes a tab from the tabbed view items.
  122. *
  123. * @param string $slug The slug of the tab to remove
  124. *
  125. * @return boolean `true` if the slug was registered and removed, `false` otherwise
  126. */
  127. public function remove( $slug ) {
  128. if ( ! $this->exists( $slug ) ) {
  129. return false;
  130. }
  131. unset( $this->items[ $slug ] );
  132. return true;
  133. }
  134. /**
  135. * Checks if a given tab exist
  136. *
  137. * @param string $slug The slug of the tab
  138. *
  139. * @return boolean
  140. */
  141. public function exists( $slug ) {
  142. return is_object( $this->get( $slug ) ) ? true : false;
  143. }
  144. /**
  145. * Fetches the Instance of the Tab or all the tabs
  146. *
  147. * @param string $slug (optional) The Slug of the Tab
  148. *
  149. * @return null|array|object If we couldn't find the tab it will be null, if the slug is null will return all tabs
  150. */
  151. public function get( $slug = null ) {
  152. uasort( $this->items, array( $this, 'sort_by_priority' ) );
  153. if ( is_null( $slug ) ) {
  154. return $this->items;
  155. }
  156. // Prevent weird stuff here
  157. $slug = sanitize_title_with_dashes( $slug );
  158. if ( ! empty( $this->items[ $slug ] ) ) {
  159. return $this->items[ $slug ];
  160. }
  161. return null;
  162. }
  163. /**
  164. * Checks if a given Tab (slug) is active
  165. *
  166. * @param string $slug The Slug of the Tab
  167. *
  168. * @return boolean Is this tab active?
  169. */
  170. public function is_active( $slug = null ) {
  171. $slug = $this->get_requested_slug( $slug );
  172. $tab = $this->get_active();
  173. return $slug === $tab->get_slug();
  174. }
  175. /**
  176. * Returns the slug of tab requested in the `_GET` array or the default one.
  177. *
  178. * @param string|null $slug
  179. * @param mixed $default A default value to return if the tab was not requested.
  180. *
  181. * @return string|bool Either the slug of the requested tab or `false` if no slug was requested
  182. * and no default tab is set.
  183. */
  184. protected function get_requested_slug( $slug = null, $default = null ) {
  185. if ( is_null( $slug ) ) {
  186. $default = null === $default ? $this->get_default_tab() : $default;
  187. // Set the slug
  188. $slug = ! empty( $_GET['tab'] ) && $this->exists( $_GET['tab'] ) ? $_GET['tab'] : $default;
  189. }
  190. return $slug;
  191. }
  192. /**
  193. * Fetches the current active tab instance.
  194. *
  195. * @return Tribe__Tabbed_View__Tab|bool The active tab, the default tab if no tab is active,
  196. * `false` if no tabs are registered in the Tabbed View.
  197. */
  198. public function get_active() {
  199. if ( ! empty( $this->active ) && $this->exists( $this->active ) ) {
  200. return $this->get( $this->active );
  201. }
  202. $tab = ! empty( $_GET['tab'] ) && $this->exists( $_GET['tab'] ) ? $_GET['tab'] : $this->get_default_tab();
  203. // Return the active tab or the default one
  204. return ! empty( $tab ) ? $this->get( $tab ) : false;
  205. }
  206. /**
  207. * Returns the slug of the default tab for this tabbed view.
  208. *
  209. * @return string The slug of the default tab, the slug of the first tab if
  210. * a default tab is not set, `false` otherwise.
  211. */
  212. public function get_default_tab() {
  213. if ( ! empty( $this->default_tab ) && $this->exists( $this->default_tab ) ) {
  214. return $this->default_tab;
  215. }
  216. $tabs = $this->get_tabs();
  217. if ( empty( $tabs ) ) {
  218. return false;
  219. }
  220. return reset( $tabs )->get_slug();
  221. }
  222. /**
  223. * @param Tribe__Tabbed_View__Tab|string $tab
  224. *
  225. * @return Tribe__Tabbed_View__Tab
  226. */
  227. public function register( $tab ) {
  228. $is_object = is_a( $tab, 'Tribe__Tabbed_View__Tab' );
  229. if ( ! ( $is_object || ( is_string( $tab ) && class_exists( $tab ) ) ) ) {
  230. return false;
  231. }
  232. if ( ! $is_object ) {
  233. $tab = $this->get_new_tab_instance( $tab );
  234. }
  235. // Set the Tab Item on the array of Tabs
  236. $tab_slug = $tab->get_slug();
  237. if ( empty( $tab_slug ) ) {
  238. return false;
  239. }
  240. $this->items[ $tab_slug ] = $tab;
  241. // Return the tab
  242. return $tab;
  243. }
  244. /**
  245. * Returns all the registered tabs.
  246. *
  247. * @return Tribe__Tabbed_View__Tab[]
  248. */
  249. public function get_tabs() {
  250. uasort( $this->items, array( $this, 'sort_by_priority' ) );
  251. return array_values( $this->items );
  252. }
  253. /**
  254. * Builds an instance of the specified tab class.
  255. *
  256. * @param string $tab_class
  257. *
  258. * @return Tribe__Tabbed_View__Tab
  259. */
  260. protected function get_new_tab_instance( $tab_class ) {
  261. return new $tab_class( $this );
  262. }
  263. /**
  264. * Renders the tabbed view and returns the resulting HTML.
  265. *
  266. * @return string
  267. */
  268. public function render() {
  269. $visibles = $this->get_visibles();
  270. if ( empty( $visibles ) ) {
  271. return '';
  272. }
  273. if ( empty( $this->template ) ) {
  274. $this->template = $this->get_default_template_path();
  275. }
  276. $template = $this->template;
  277. if ( empty( $template ) ) {
  278. return '';
  279. }
  280. $default_data = array(
  281. 'view' => $this,
  282. );
  283. $data = array_merge( $default_data, (array) $this->data );
  284. extract( $data );
  285. ob_start();
  286. include $template;
  287. $html = ob_get_clean();
  288. return $html;
  289. }
  290. /**
  291. * Sets the default tab for the tabbed view.
  292. *
  293. * Please note that no check is made on the tabbed view items to ensure the value
  294. * corresponds to a registered tab.
  295. *
  296. * @param string $default_tab The slug of the default tab.
  297. */
  298. public function set_default_tab( $default_tab ) {
  299. $this->default_tab = $default_tab;
  300. }
  301. /**
  302. * @param Tribe__Tabbed_View__Tab $tab
  303. *
  304. * @return bool
  305. */
  306. protected function is_tab_visible( Tribe__Tabbed_View__Tab $tab ) {
  307. return $tab->is_visible();
  308. }
  309. /**
  310. * Returns the absolute path to the default template for the tabbed view.
  311. *
  312. * @return string
  313. */
  314. public function get_default_template_path() {
  315. return Tribe__Main::instance()->plugin_path . '/src/admin-views/tabbed-view/tabbed-view.php';
  316. }
  317. }