/wp-content/plugins/all-in-one-event-calendar/app/view/admin/class-ai1ec-agenda-widget.php

https://gitlab.com/Blueprint-Marketing/interoccupy.net · PHP · 278 lines · 201 code · 25 blank · 52 comment · 31 complexity · 5a74cc022f29584bc3d7d45543823d85 MD5 · raw file

  1. <?php
  2. /**
  3. * Ai1ec_Agenda_Widget class
  4. *
  5. * A widget that displays the next X upcoming events (similar to Agenda view).
  6. */
  7. class Ai1ec_Agenda_Widget extends WP_Widget {
  8. /**
  9. * Constructor for widget.
  10. */
  11. function __construct() {
  12. parent::__construct(
  13. 'ai1ec_agenda_widget',
  14. __( 'Upcoming Events', AI1EC_PLUGIN_NAME ),
  15. array(
  16. 'description' => __( 'All-in-One Event Calendar: Lists upcoming events in Agenda view', AI1EC_PLUGIN_NAME ),
  17. 'class' => 'ai1ec-agenda-widget',
  18. )
  19. );
  20. }
  21. /**
  22. * form function
  23. *
  24. * Renders the widget's configuration form for the Manage Widgets page.
  25. *
  26. * @param array $instance The data array for the widget instance being
  27. * configured.
  28. */
  29. function form( $instance )
  30. {
  31. global $ai1ec_view_helper;
  32. $default = array(
  33. 'title' => __( 'Upcoming Events', AI1EC_PLUGIN_NAME ),
  34. 'events_seek_type' => 'events',
  35. 'events_per_page' => 10,
  36. 'days_per_page' => 10,
  37. 'show_subscribe_buttons' => true,
  38. 'show_calendar_button' => true,
  39. 'hide_on_calendar_page' => true,
  40. 'limit_by_cat' => false,
  41. 'limit_by_tag' => false,
  42. 'limit_by_post' => false,
  43. 'event_cat_ids' => array(),
  44. 'event_tag_ids' => array(),
  45. 'event_post_ids' => array(),
  46. );
  47. $instance = wp_parse_args( (array) $instance, $default );
  48. // Get available cats, tags, events to allow user to limit widget to certain categories
  49. $events_categories = get_terms( 'events_categories', array( 'orderby' => 'name', "hide_empty" => false ) );
  50. $events_tags = get_terms( 'events_tags', array( 'orderby' => 'name', "hide_empty" => false ) );
  51. $get_events = new WP_Query( array ( 'post_type' => AI1EC_POST_TYPE, 'posts_per_page' => -1 ) );
  52. $events_options = $get_events->posts;
  53. // Generate unique IDs and NAMEs of all needed form fields
  54. $fields = array(
  55. 'title' => array('value' => $instance['title']),
  56. 'events_seek_type' => array('value' => $instance['events_seek_type']),
  57. 'events_per_page' => array('value' => $instance['events_per_page']),
  58. 'days_per_page' => array('value' => $instance['days_per_page']),
  59. 'show_subscribe_buttons' => array('value' => $instance['show_subscribe_buttons']),
  60. 'show_calendar_button' => array('value' => $instance['show_calendar_button']),
  61. 'hide_on_calendar_page' => array('value' => $instance['hide_on_calendar_page']),
  62. 'limit_by_cat' => array('value' => $instance['limit_by_cat']),
  63. 'limit_by_tag' => array('value' => $instance['limit_by_tag']),
  64. 'limit_by_post' => array('value' => $instance['limit_by_post']),
  65. 'event_cat_ids' => array(
  66. 'value' => (array)$instance['event_cat_ids'],
  67. 'options' => $events_categories
  68. ),
  69. 'event_tag_ids' => array(
  70. 'value' => (array)$instance['event_tag_ids'],
  71. 'options' => $events_tags
  72. ),
  73. 'event_post_ids' => array(
  74. 'value' => (array)$instance['event_post_ids'],
  75. 'options' => $events_options
  76. ),
  77. );
  78. foreach( $fields as $field => $data ) {
  79. $fields[$field]['id'] = $this->get_field_id( $field );
  80. $fields[$field]['name'] = $this->get_field_name( $field );
  81. $fields[$field]['value'] = $data['value'];
  82. if( isset($data['options']) ) {
  83. $fields[$field]['options'] = $data['options'];
  84. }
  85. }
  86. $ai1ec_view_helper->display_admin( 'agenda-widget-form.php', $fields );
  87. }
  88. /**
  89. * update function
  90. *
  91. * Called when a user submits the widget configuration form. The data should
  92. * be validated and returned.
  93. *
  94. * @param array $new_instance The new data that was submitted.
  95. * @param array $old_instance The widget's old data.
  96. * @return array The new data to save for this widget instance.
  97. */
  98. function update( $new_instance, $old_instance )
  99. {
  100. // Save existing data as a base to modify with new data
  101. $instance = $old_instance;
  102. $instance['title'] = strip_tags( $new_instance['title'] );
  103. $instance['events_per_page'] = Ai1ec_Number_Utility::index(
  104. $new_instance['events_per_page'],
  105. 1,
  106. 1
  107. );
  108. $instance['days_per_page'] = Ai1ec_Number_Utility::index(
  109. $new_instance['days_per_page'],
  110. 1,
  111. 1
  112. );
  113. $instance['events_seek_type'] = $this->_valid_seek_type(
  114. $new_instance['events_seek_type']
  115. );
  116. $instance['show_subscribe_buttons'] = $new_instance['show_subscribe_buttons'] ? true : false;
  117. $instance['show_calendar_button'] = $new_instance['show_calendar_button'] ? true : false;
  118. $instance['hide_on_calendar_page'] = $new_instance['hide_on_calendar_page'] ? true : false;
  119. // For limits, set the limit to False if no IDs were selected, or set the respective IDs to empty if "limit by" was unchecked
  120. $instance['limit_by_cat'] = false;
  121. $instance['event_cat_ids'] = array();
  122. if( isset( $new_instance['event_cat_ids'] ) && $new_instance['event_cat_ids'] != false ) {
  123. $instance['limit_by_cat'] = true;
  124. }
  125. if( isset( $new_instance['limit_by_cat'] ) && $new_instance['limit_by_cat'] != false ) {
  126. $instance['limit_by_cat'] = true;
  127. }
  128. if( isset( $new_instance['event_cat_ids'] ) && $instance['limit_by_cat'] === true ) {
  129. $instance['event_cat_ids'] = $new_instance['event_cat_ids'];
  130. }
  131. $instance['limit_by_tag'] = false;
  132. $instance['event_tag_ids'] = array();
  133. if( isset( $new_instance['event_tag_ids'] ) && $new_instance['event_tag_ids'] != false ) {
  134. $instance['limit_by_tag'] = true;
  135. }
  136. if( isset( $new_instance['limit_by_tag'] ) && $new_instance['limit_by_tag'] != false ) {
  137. $instance['limit_by_tag'] = true;
  138. }
  139. if( isset( $new_instance['event_tag_ids'] ) && $instance['limit_by_tag'] === true ) {
  140. $instance['event_tag_ids'] = $new_instance['event_tag_ids'];
  141. }
  142. $instance['limit_by_post'] = false;
  143. $instance['event_post_ids'] = array();
  144. if( isset( $new_instance['event_post_ids'] ) && $new_instance['event_post_ids'] != false ) {
  145. $instance['limit_by_post'] = true;
  146. }
  147. if( isset( $new_instance['limit_by_post'] ) && $new_instance['limit_by_post'] != false ) {
  148. $instance['limit_by_post'] = true;
  149. }
  150. if( isset( $new_instance['event_post_ids'] ) && $instance['limit_by_post'] === true ) {
  151. $instance['event_post_ids'] = $new_instance['event_post_ids'];
  152. }
  153. return $instance;
  154. }
  155. /**
  156. * widget function
  157. *
  158. * Outputs the given instance of the widget to the front-end.
  159. *
  160. * @param array $args Display arguments passed to the widget
  161. * @param array $instance The settings for this widget instance
  162. */
  163. function widget( $args, $instance )
  164. {
  165. global $ai1ec_view_helper,
  166. $ai1ec_events_helper,
  167. $ai1ec_calendar_helper,
  168. $ai1ec_settings,
  169. $ai1ec_themes_controller;
  170. if ( $ai1ec_themes_controller->frontend_outdated_themes_notice() ) {
  171. return;
  172. }
  173. $defaults = array(
  174. 'hide_on_calendar_page' => true,
  175. 'event_cat_ids' => array(),
  176. 'event_tag_ids' => array(),
  177. 'event_post_ids' => array(),
  178. 'events_per_page' => 10,
  179. 'days_per_page' => 10,
  180. 'events_seek_type' => 'events',
  181. );
  182. $instance = wp_parse_args( $instance, $defaults );
  183. if( $instance['hide_on_calendar_page'] &&
  184. is_page( $ai1ec_settings->calendar_page_id ) ) {
  185. return;
  186. }
  187. // Add params to the subscribe_url for filtering by Limits (category, tag)
  188. $subscribe_filter = '';
  189. $subscribe_filter .= $instance['event_cat_ids'] ? '&ai1ec_cat_ids=' . join( ',', $instance['event_cat_ids'] ) : '';
  190. $subscribe_filter .= $instance['event_tag_ids'] ? '&ai1ec_tag_ids=' . join( ',', $instance['event_tag_ids'] ) : '';
  191. $subscribe_filter .= $instance['event_post_ids'] ? '&ai1ec_post_ids=' . join( ',', $instance['event_post_ids'] ) : '';
  192. // Get localized time
  193. $timestamp = $ai1ec_events_helper->gmt_to_local(
  194. Ai1ec_Time_Utility::current_time()
  195. );
  196. // Set $limit to the specified category/tag
  197. $limit = array(
  198. 'cat_ids' => $instance['event_cat_ids'],
  199. 'tag_ids' => $instance['event_tag_ids'],
  200. 'post_ids' => $instance['event_post_ids'],
  201. );
  202. // Get events, then classify into date array
  203. // JB: apply seek check here
  204. $seek_days = ( 'days' === $instance['events_seek_type'] );
  205. $seek_count = $instance['events_per_page'];
  206. $last_day = false;
  207. if ( $seek_days ) {
  208. $seek_count = $instance['days_per_page'] * 5;
  209. $last_day = strtotime(
  210. '+' . $instance['days_per_page'] . ' days'
  211. );
  212. }
  213. $event_results = $ai1ec_calendar_helper->get_events_relative_to(
  214. $timestamp,
  215. $seek_count,
  216. 0,
  217. $limit
  218. );
  219. if ( $seek_days ) {
  220. foreach ( $event_results['events'] as $ek => $event ) {
  221. if ( $event->start >= $last_day ) {
  222. unset( $event_results['events'][$ek] );
  223. }
  224. }
  225. }
  226. $dates = $ai1ec_calendar_helper->get_agenda_like_date_array( $event_results['events'] );
  227. $args['title'] = $instance['title'];
  228. $args['show_subscribe_buttons'] = $instance['show_subscribe_buttons'];
  229. $args['show_calendar_button'] = $instance['show_calendar_button'];
  230. $args['dates'] = $dates;
  231. $args['show_location_in_title'] = $ai1ec_settings->show_location_in_title;
  232. $args['show_year_in_agenda_dates'] = $ai1ec_settings->show_year_in_agenda_dates;
  233. $args['calendar_url'] = $ai1ec_calendar_helper->get_calendar_url( $limit );
  234. $args['subscribe_url'] = AI1EC_EXPORT_URL . $subscribe_filter;
  235. $ai1ec_view_helper->display_theme( 'agenda-widget.php', $args );
  236. }
  237. /**
  238. * _valid_seek_type method
  239. *
  240. * Return valid seek type for given user input (selection).
  241. *
  242. * @param string $value User selection for seek type
  243. *
  244. * @return string Seek type to use
  245. */
  246. protected function _valid_seek_type( $value ) {
  247. static $list = array( 'events', 'days' );
  248. if ( ! in_array( $value, $list ) ) {
  249. return (string)reset( $list );
  250. }
  251. return $value;
  252. }
  253. }