/wp-content/plugins/the-events-calendar/src/functions/template-tags/event.php

https://github.com/livinglab/openlab · PHP · 185 lines · 48 code · 17 blank · 120 comment · 8 complexity · 9b806bcfcb7fce66b02c7907212702d4 MD5 · raw file

  1. <?php
  2. /**
  3. * Functions and template tags dedicated to Events.
  4. *
  5. * @since 4.9.7
  6. */
  7. use Tribe\Events\Models\Post_Types\Event;
  8. if ( ! function_exists( 'tribe_get_event' ) ) {
  9. /**
  10. * Fetches and returns a decorated post object representing an Event.
  11. *
  12. * @since 4.9.7
  13. *
  14. * @param null|int|WP_Post $event The event ID or post object or `null` to use the global one.
  15. * @param string|null $output The required return type. One of `OBJECT`, `ARRAY_A`, or `ARRAY_N`, which
  16. * correspond to a WP_Post object, an associative array, or a numeric array,
  17. * respectively. Defaults to `OBJECT`.
  18. * @param string $filter Type of filter to apply. Accepts 'raw', a valid date string or
  19. * object to localize the event in a specific time-frame.
  20. * @param bool $force Whether to force a re-fetch ignoring cached results or not.
  21. *
  22. * @return array|mixed|void|WP_Post|null {
  23. * The Event post object or array, `null` if not found.
  24. *
  25. * @type string $start_date The event start date, in `Y-m-d H:i:s` format.
  26. * @type string $start_date_utc The event UTC start date, in `Y-m-d H:i:s` format.
  27. * @type string $end_date The event end date, in `Y-m-d H:i:s` format.
  28. * @type string $end_date_utc The event UTC end date, in `Y-m-d H:i:s` format.
  29. * @type array $dates An array containing the event.start, end and UTC date objects. {
  30. * @type DateTimeImmutable $start The event start date object.
  31. * @type DateTimeImmutable $start_utc The event UTC start date object.
  32. * @type DateTimeImmutable $end The event end date object.
  33. * @type DateTimeImmutable $end_utc The event UTC end date object.
  34. * @type DateTimeImmutable $start_site The event start date object in
  35. * the site timezone.
  36. * @type DateTimeImmutable $end_site The event end date object in the
  37. * site timezone.
  38. * @type DateTimeImmutable $start_display The event start date object
  39. * in the site or event timezone
  40. * depending on the setting.
  41. * @type DateTimeImmutable $end_display The event end date object in
  42. * the site or event timezone
  43. * depending on the setting.
  44. * }
  45. * @type string $timezone The event timezone string.
  46. * @type int $duration The event duration in seconds.
  47. * @type false|int $multiday Whether the event is multi-day or not and its day.
  48. * duration if it is.
  49. * @type bool $all_day Whether the event is an all-day one or not.
  50. * @type null|bool $starts_this_week Whether the event starts on the week of the date
  51. * specified in the `$filter` argument or not, `null`
  52. * if no date is specified in the filter.
  53. * @type null|bool $ends_this_week Whether the event ends on the week of the date
  54. * specified in the `$filter` argument or not, `null`
  55. * if no date is specified in the filter.
  56. * @type null|bool $happens_this_week Whether the event happens on the week of the date
  57. * specified in the `$filter` argument or not, `null`
  58. * if no date is specified in the filter.
  59. * @type null|int $this_week_duration The days duration of the event on the week
  60. * specified in the `$filter` argument, `null`
  61. * if no date is specified in the filter.
  62. * @type bool $featured Whether the event is a featured one or not.
  63. * @type string $cost The event formatted cost string, as returned by the `tribe_get_cost`
  64. * `tribe_get_cost` function.
  65. * @type Lazy_Collection $organizers A collection of Organizers, lazily fetched and
  66. * eventually resolved to an array.
  67. * @type Lazy_Collection $venues A collection of Venues, lazily fetched and
  68. * eventually resolved to an array.
  69. * @type Post_Thumbnail $thumbnail The post thumbnail information.
  70. * @type Lazy_String $schedule_details The event schedule details, as produced by the
  71. * `tribe_events_event_schedule_details` function.
  72. * @type Lazy_String $plain_schedule_details The event schedule details, without HTML
  73. * tags.
  74. * }
  75. */
  76. function tribe_get_event( $event = null, $output = OBJECT, $filter = 'raw', $force = false ) {
  77. /**
  78. * Filters the event result before any logic applies.
  79. *
  80. * Returning a non `null` value here will short-circuit the function and return the value.
  81. * Note: this value will not be cached and the caching of this value is a duty left to the filtering function.
  82. *
  83. * @since 4.9.7
  84. *
  85. * @param mixed $return The event object to return.
  86. * @param mixed $event The event object to fetch.
  87. * @param string|null $output The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which
  88. * correspond to a `WP_Post` object, an associative array, or a numeric array,
  89. * respectively. Defaults to `OBJECT`.
  90. * @param string $filter Type of filter to apply. Accepts 'raw', a valid date string or
  91. * object to localize the event in a specific time-frame.
  92. */
  93. $return = apply_filters( 'tribe_get_event_before', null, $event, $output, $filter );
  94. if ( null !== $return ) {
  95. return $return;
  96. }
  97. $post = false;
  98. /** @var Tribe__Cache $cache */
  99. $cache = tribe( 'cache' );
  100. $cache_post = get_post( $event );
  101. if ( empty( $cache_post ) ) {
  102. return null;
  103. }
  104. $key_fields = [
  105. $cache_post->ID,
  106. $cache_post->post_modified,
  107. // Use the `post_password` field as we show/hide some information depending on that.
  108. $cache_post->post_password,
  109. // We must include options on cache key, because options influence the hydrated data on the Event object.
  110. wp_json_encode( Tribe__Settings_Manager::get_options() ),
  111. wp_json_encode( [
  112. get_option( 'start_of_week' ),
  113. get_option( 'timezone_string' ),
  114. get_option( 'gmt_offset' )
  115. ] ),
  116. $output,
  117. $filter,
  118. ];
  119. $cache_key = 'tribe_get_event_' . md5( wp_json_encode( $key_fields ) );
  120. if ( ! $force ) {
  121. $post = $cache->get( $cache_key, Tribe__Cache_Listener::TRIGGER_SAVE_POST );
  122. }
  123. if ( false === $post ) {
  124. $post = Event::from_post( $event )->to_post( $output, $filter );
  125. if ( empty( $post ) ) {
  126. return null;
  127. }
  128. /**
  129. * Filters the event post object before caching it and returning it.
  130. *
  131. * Note: this value will be cached; as such this filter might not run on each request.
  132. * If you need to filter the output value on each call of this function then use the `tribe_get_event_before`
  133. * filter.
  134. *
  135. * @since 4.9.7
  136. *
  137. * @param WP_Post $post The event post object, decorated with a set of custom properties.
  138. * @param string $output The output format to use.
  139. * @param string $filter The filter, or context of the fetch.
  140. */
  141. $post = apply_filters( 'tribe_get_event', $post, $output, $filter );
  142. // Dont try to reset cache when forcing.
  143. if ( ! $force ) {
  144. $cache->set( $cache_key, $post, WEEK_IN_SECONDS, Tribe__Cache_Listener::TRIGGER_SAVE_POST );
  145. }
  146. }
  147. /**
  148. * Filters the event result after the event has been built from the function.
  149. *
  150. * Note: this value will not be cached and the caching of this value is a duty left to the filtering function.
  151. *
  152. * @since 5.0.0
  153. *
  154. * @param WP_Post $post The event post object to filter and return.
  155. * @param int|WP_Post $event The event object to fetch.
  156. * @param string|null $output The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which
  157. * correspond to a `WP_Post` object, an associative array, or a numeric array,
  158. * respectively. Defaults to `OBJECT`.
  159. * @param string $filter Type of filter to apply. Accepts 'raw', a valid date string or
  160. * object to localize the event in a specific time-frame.
  161. */
  162. $post = apply_filters( 'tribe_get_event_after', $post, $event, $output, $filter );
  163. if ( OBJECT !== $output ) {
  164. $post = ARRAY_A === $output ? (array) $post : array_values( (array) $post );
  165. }
  166. return $post;
  167. }
  168. }