/wp-content/plugins/the-events-calendar/src/Tribe/Views/V2/View_Register.php

https://github.com/livinglab/openlab · PHP · 201 lines · 69 code · 22 blank · 110 comment · 0 complexity · d470268eae0abef18d9a4d21f0ff0efd MD5 · raw file

  1. <?php
  2. /**
  3. * The View registration facade.
  4. *
  5. * @package Tribe\Events\Views\V2
  6. * @since 5.7.0
  7. */
  8. namespace Tribe\Events\Views\V2;
  9. /**
  10. * Class View_Register
  11. *
  12. * @package Tribe\Events\Views\V2
  13. * @since 5.7.0
  14. * @since 5.10.0 Added feature to define the route slug used for this view, decoupled from the view slug.
  15. */
  16. class View_Register {
  17. /**
  18. * Slug for locating the view file.
  19. *
  20. * @var string
  21. */
  22. protected $slug;
  23. /**
  24. * Name for the view.
  25. *
  26. * @var string
  27. */
  28. protected $name;
  29. /**
  30. * Class name for the view.
  31. *
  32. * @var string
  33. */
  34. protected $class;
  35. /**
  36. * Priority order for the view registration.
  37. *
  38. * @var int
  39. */
  40. protected $priority;
  41. /**
  42. * The slug applied to the route for this view.
  43. *
  44. * @var string
  45. */
  46. protected $route_slug;
  47. /**
  48. * View_Register constructor.
  49. *
  50. * @param string $slug Slug for locating the view file.
  51. * @param string $name Name for the view.
  52. * @param string $class Class name for the view.
  53. * @param int $priority Priority order for the view registration.
  54. * @param string $route_slug The slug applied to the route for this view.
  55. */
  56. public function __construct( $slug, $name, $class, $priority = 40, $route_slug = null ) {
  57. $this->slug = $slug;
  58. $this->route_slug = $route_slug ? $route_slug : $slug;
  59. $this->name = $name;
  60. $this->class = $class;
  61. $this->priority = $priority;
  62. $this->add_actions();
  63. $this->add_filters();
  64. $asset_registration_object = call_user_func( $this->class . '::get_asset_origin', $this->slug );
  65. call_user_func( $this->class . '::register_assets', $asset_registration_object );
  66. }
  67. /**
  68. * Adds actions for view registration.
  69. *
  70. * @since 5.7.0
  71. */
  72. protected function add_actions() {
  73. add_action( 'tribe_events_pre_rewrite', [ $this, 'filter_add_routes' ], 5 );
  74. }
  75. /**
  76. * Adds filters for view registration.
  77. *
  78. * @since 5.7.0
  79. */
  80. protected function add_filters() {
  81. add_filter( 'tribe_events_views', [ $this, 'filter_events_views' ] );
  82. add_filter( 'tribe-events-bar-views', [ $this, 'filter_tec_bar_views' ], $this->priority );
  83. add_filter( 'tribe_events_rewrite_base_slugs', [ $this, 'filter_add_base_slugs' ], $this->priority );
  84. add_filter( 'tribe_events_rewrite_matchers_to_query_vars_map', [ $this, 'filter_add_matchers_to_query_vars_map' ], $this->priority, 2 );
  85. }
  86. /**
  87. * Add rewrite routes for custom PRO stuff and views.
  88. *
  89. * @since 5.7.0
  90. * @since 5.10.0 Adds optional decoupling of view name to route slug
  91. *
  92. * @param \Tribe__Events__Rewrite $rewrite The Tribe__Events__Rewrite object
  93. *
  94. * @return void
  95. */
  96. public function filter_add_routes( $rewrite ) {
  97. // Setup base rewrite rules
  98. $rewrite
  99. ->archive( [ '{{ ' . $this->route_slug . ' }}', '{{ page }}', '(\d+)' ], [ 'eventDisplay' => $this->slug, 'paged' => '%1' ] )
  100. ->archive( [ '{{ ' . $this->route_slug . ' }}', '{{ featured }}', '{{ page }}', '(\d+)' ], [ 'eventDisplay' => $this->slug, 'featured' => true, 'paged' => '%1' ] )
  101. ->archive( [ '{{ ' . $this->route_slug . ' }}' ], [ 'eventDisplay' => $this->slug ] )
  102. ->archive( [ '{{ ' . $this->route_slug . ' }}', '{{ featured }}' ], [ 'eventDisplay' => $this->slug, 'featured' => true ] )
  103. ->archive( [ '{{ ' . $this->route_slug . ' }}', '(\d{4}-\d{2}-\d{2})' ], [ 'eventDisplay' => $this->slug, 'eventDate' => '%1' ] )
  104. ->archive( [ '{{ ' . $this->route_slug . ' }}', '(\d{4}-\d{2}-\d{2})', '{{ featured }}' ], [ 'eventDisplay' => $this->slug, 'eventDate' => '%1', 'featured' => true ] );
  105. // Setup taxonomy based rewrite rules.
  106. $rewrite
  107. ->tax( [ '{{ ' . $this->route_slug . ' }}', '{{ page }}', '(\d+)' ], [ 'eventDisplay' => $this->slug, 'paged' => '%2' ] )
  108. ->tax( [ '{{ ' . $this->route_slug . ' }}', '{{ featured }}', '{{ page }}', '(\d+)' ], [ 'eventDisplay' => $this->slug, 'featured' => true, 'paged' => '%2' ] )
  109. ->tax( [ '{{ ' . $this->route_slug . ' }}', '{{ featured }}' ], [ 'eventDisplay' => $this->slug, 'featured' => true ] )
  110. ->tax( [ '{{ ' . $this->route_slug . ' }}' ], [ 'eventDisplay' => $this->slug ] );
  111. // Setup post_tag rewrite rules.
  112. $rewrite
  113. ->tag( [ '{{ ' . $this->route_slug . ' }}', '{{ page }}', '(\d+)' ], [ 'eventDisplay' => $this->slug, 'paged' => '%2' ] )
  114. ->tag( [ '{{ ' . $this->route_slug . ' }}', '{{ featured }}', '{{ page }}', '(\d+)' ], [ 'eventDisplay' => $this->slug, 'featured' => true, 'paged' => '%2' ] )
  115. ->tag( [ '{{ ' . $this->route_slug . ' }}', '{{ featured }}' ], [ 'eventDisplay' => $this->slug, 'featured' => true ] )
  116. ->tag( [ '{{ ' . $this->route_slug . ' }}' ], [ 'eventDisplay' => $this->slug ] );
  117. }
  118. /**
  119. * Add the required bases for the Pro Views
  120. *
  121. * @since 5.7.0
  122. * @since 5.10.0 Using the decoupled route slug.
  123. *
  124. * @param array $bases Bases that are already set
  125. *
  126. * @return array The modified version of the array of bases
  127. */
  128. public function filter_add_base_slugs( $bases = [] ) {
  129. // Support the original and translated forms for added robustness
  130. $bases[ $this->route_slug ] = [ $this->route_slug , $this->route_slug ];
  131. return $bases;
  132. }
  133. /**
  134. * Add the required bases for the Summary View.
  135. *
  136. * @since 5.7.0
  137. * @since 5.10.0 Using the decoupled route slug.
  138. *
  139. * @param array $bases Bases that are already set.
  140. *
  141. * @return array The modified version of the array of bases.
  142. */
  143. public function filter_add_matchers_to_query_vars_map( $matchers = [], $rewrite = null ) {
  144. $matchers[ $this->route_slug ] = 'eventDisplay';
  145. return $matchers;
  146. }
  147. /**
  148. * Filters the available views.
  149. *
  150. * @since 5.7.0
  151. *
  152. * @param array $views An array of available Views.
  153. *
  154. * @return array The array of available views, including the PRO ones.
  155. */
  156. public function filter_events_views( array $views = [] ) {
  157. $views[ $this->slug ] = $this->class;
  158. return $views;
  159. }
  160. /**
  161. * Add the view to the views selector in the TEC bar.
  162. *
  163. * @since 5.7.0
  164. * @since 5.10.0 Using the route slug to build the `url` element.
  165. *
  166. * @param array $views The current array of views registered to the tribe bar.
  167. *
  168. * @return array The views registered with photo view added.
  169. */
  170. public function filter_tec_bar_views( $views ) {
  171. $views[] = [
  172. 'displaying' => $this->slug,
  173. 'anchor' => $this->name,
  174. 'event_bar_hook' => 'tribe_events_before_template',
  175. 'url' => \tribe_get_view_permalink( $this->route_slug ),
  176. ];
  177. return $views;
  178. }
  179. }