SimpleCalendarAbstractsFeed

The Feed.

Defined (1)

The class is defined in the following location(s).

/includes/abstracts/feed.php  
  1. abstract class Feed { 
  2.  
  3. /** 
  4. * Feed type. 
  5. * @access public 
  6. * @var string 
  7. */ 
  8. public $type = ''; 
  9.  
  10. /** 
  11. * Feed name. 
  12. * @access public 
  13. * @var string 
  14. */ 
  15. public $name = ''; 
  16.  
  17. /** 
  18. * Calendar post id. 
  19. * @access public 
  20. * @var int 
  21. */ 
  22. public $post_id = 0; 
  23.  
  24. /** 
  25. * Calendar opening. 
  26. * @access protected 
  27. * @var int 
  28. */ 
  29. protected $calendar_start = 0; 
  30.  
  31. /** 
  32. * Start of week. 
  33. * @access protected 
  34. * @var int 
  35. */ 
  36. protected $week_starts = 0; 
  37.  
  38. /** 
  39. * Events. 
  40. * @access public 
  41. * @var array 
  42. */ 
  43. public $events = array(); 
  44.  
  45. /** 
  46. * Events template. 
  47. * @access protected 
  48. * @var string 
  49. */ 
  50. protected $events_template = ''; 
  51.  
  52. /** 
  53. * Timezone setting. 
  54. * @access protected 
  55. * @var string 
  56. */ 
  57. protected $timezone_setting = ''; 
  58.  
  59. /** 
  60. * Timezone. 
  61. * @access public 
  62. * @var string 
  63. */ 
  64. public $timezone = ''; 
  65.  
  66. /** 
  67. * Earliest possible event. 
  68. * @access public 
  69. * @var int 
  70. */ 
  71. public $time_min = 0; 
  72.  
  73. /** 
  74. * Latest possible event. 
  75. * @access public 
  76. * @var int 
  77. */ 
  78. public $time_max = 0; 
  79.  
  80. /** 
  81. * Feed cache interval. 
  82. * @access protected 
  83. * @var int 
  84. */ 
  85. protected $cache = 7200; 
  86.  
  87. /** 
  88. * Feed settings. 
  89. * @access protected 
  90. * @var array 
  91. */ 
  92. protected $settings = array(); 
  93.  
  94. /** 
  95. * Constructor. 
  96. * @since 3.0.0 
  97. * @param string|Calendar $calendar 
  98. */ 
  99. public function __construct( $calendar = '' ) { 
  100.  
  101. if ( $calendar instanceof Calendar ) { 
  102.  
  103. if ( isset( $calendar->id ) ) { 
  104. $this->post_id = $calendar->id; 
  105. if ( isset( $calendar->start ) ) { 
  106. $this->calendar_start = $calendar->start; 
  107. $this->week_starts = isset( $calendar->week_starts ) ? $calendar->week_starts : get_option( 'start_of_week' ); 
  108. $this->events_template = ! empty( $calendar->events_template ) ? $calendar->events_template : simcal_default_event_template(); 
  109.  
  110. if ( $this->post_id > 0 ) { 
  111. $this->set_cache(); 
  112. $this->timezone_setting = get_post_meta( $this->post_id, '_feed_timezone_setting', true ); 
  113. $this->timezone = $calendar->timezone; 
  114. $this->set_earliest_event(); 
  115. $this->set_latest_event(); 
  116.  
  117. /** 
  118. * Input fields for settings page. 
  119. * @since 3.0.0 
  120. * @return false|array 
  121. */ 
  122. public function settings_fields() { 
  123. return $this->settings; 
  124.  
  125. /** 
  126. * Set earliest event. 
  127. * @since 3.0.0 
  128. * @param int $timestamp 
  129. */ 
  130. public function set_earliest_event( $timestamp = 0 ) { 
  131.  
  132. $earliest = intval( $timestamp ); 
  133.  
  134. if ( $earliest === 0 ) { 
  135.  
  136. $start = Carbon::createFromTimestamp( $this->calendar_start, $this->timezone ); 
  137.  
  138. $earliest_date = esc_attr( get_post_meta( $this->post_id, '_feed_earliest_event_date', true ) ); 
  139. $earliest_range = max( absint( get_post_meta( $this->post_id, '_feed_earliest_event_date_range', true ) ), 1 ); 
  140.  
  141. if ( 'days_before' == $earliest_date ) { 
  142. $earliest = $start->subDays( $earliest_range )->getTimestamp(); 
  143. } elseif ( 'weeks_before' == $earliest_date ) { 
  144. $earliest = $start->subWeeks( $earliest_range )->addDay()->getTimestamp(); 
  145. } elseif ( 'months_before' == $earliest_date ) { 
  146. $earliest = $start->subMonths( $earliest_range )->addDay()->getTimestamp(); 
  147. } elseif ( 'years_before' == $earliest_date ) { 
  148. $earliest = $start->subYears( $earliest_range )->addDay()->getTimestamp(); 
  149. } else { 
  150. $earliest = $start->getTimestamp(); 
  151.  
  152. $this->time_min = $earliest; 
  153.  
  154. /** 
  155. * Set latest event. 
  156. * @since 3.0.0 
  157. * @param int $timestamp 
  158. */ 
  159. public function set_latest_event( $timestamp = 0 ) { 
  160.  
  161. $latest = intval( $timestamp ); 
  162.  
  163. if ( $latest === 0 ) { 
  164.  
  165. $start = Carbon::createFromTimestamp( $this->calendar_start, $this->timezone )->endOfDay(); 
  166.  
  167. $latest_date = esc_attr( get_post_meta( $this->post_id, '_feed_latest_event_date', true ) ); 
  168. $latest_range = max( absint( get_post_meta( $this->post_id, '_feed_latest_event_date_range', true ) ), 1 ); 
  169.  
  170. if ( 'days_after' == $latest_date ) { 
  171. $latest = $start->addDays( $latest_range )->getTimestamp(); 
  172. } elseif ( 'weeks_after' == $latest_date ) { 
  173. $latest = $start->addWeeks( $latest_range )->subDay()->getTimestamp(); 
  174. } elseif ( 'months_after' == $latest_date ) { 
  175. $latest = $start->addMonths( $latest_range )->subDay()->getTimestamp(); 
  176. } elseif ( 'years_after' == $latest_date ) { 
  177. $latest = $start->addYears( $latest_range )->subDay()->getTimestamp(); 
  178. } else { 
  179. $latest = $start->getTimestamp(); 
  180.  
  181.  
  182. $this->time_max = $latest; 
  183.  
  184. /** 
  185. * Set cache. 
  186. * @since 3.0.0 
  187. * @param int $time 
  188. */ 
  189. public function set_cache( $time = 0 ) { 
  190. if ( $time === 0 || ! is_numeric( $time ) ) { 
  191. $cache = get_post_meta( $this->post_id, '_feed_cache', true ); 
  192. $time = is_numeric( $cache ) && $cache >= 0 ? absint( $cache ) : $this->cache; 
  193. $this->cache = absint( $time ); 
  194.  
  195. /** 
  196. * Get events feed. 
  197. * @since 3.0.0 
  198. * @return array 
  199. */ 
  200. abstract public function get_events(); 
  201.