Advertisement
Apina

ee4widgetdatechange

Jul 28th, 2014
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.24 KB | None | 0 0
  1. // Please do NOT include the opening php tag, except of course if you're starting with a blank file
  2.  
  3.  
  4. class EEW_Upcoming_Events extends WP_Widget {
  5.  
  6.  
  7. /**
  8. * Register widget with WordPress.
  9. */
  10. function __construct() {
  11. parent::__construct(
  12. 'ee-upcoming-events-widget',
  13. __( 'Event Espresso Upcoming Events', 'event_espresso' ),
  14. array( 'description' => __( 'A widget to display your upcoming eventsq.', 'event_espresso' )),
  15. array(
  16. 'width' => 300,
  17. 'height' => 350,
  18. 'id_base' => 'ee-upcoming-events-widget'
  19. )
  20. );
  21. }
  22.  
  23.  
  24.  
  25. /**
  26. * Back-end widget form.
  27. *
  28. * @see WP_Widget::form()
  29. *
  30. * @param array $instance Previously saved values from database.
  31. */
  32. public function form( $instance ) {
  33.  
  34. EE_Registry::instance()->load_helper( 'Form_Fields' );
  35. EE_Registry::instance()->load_class( 'Question_Option', array(), FALSE, FALSE, TRUE );
  36. // Set up some default widget settings.
  37. $defaults = array(
  38. 'title' => __('Upcoming Events', 'event_espresso'),
  39. 'category_name' => '',
  40. 'show_expired' => FALSE,
  41. 'show_desc' => TRUE,
  42. 'show_dates' => TRUE,
  43. 'limit' => 10,
  44. 'image_size' => 'medium'
  45. );
  46.  
  47. $instance = wp_parse_args( (array) $instance, $defaults );
  48. // don't add HTML labels for EE_Form_Fields generated inputs
  49. add_filter( 'FHEE__EEH_Form_Fields__label_html', '__return_empty_string' );
  50. $yes_no_values = array(
  51. EE_Question_Option::new_instance( array( 'QSO_value' => 0, 'QSO_desc' => __('No', 'event_espresso'))),
  52. EE_Question_Option::new_instance( array( 'QSO_value' => 1, 'QSO_desc' => __('Yes', 'event_espresso')))
  53. );
  54.  
  55. ?>
  56.  
  57. <!-- Widget Title: Text Input -->
  58.  
  59. <p>
  60. <label for="<?php echo $this->get_field_id('title'); ?>">
  61. <?php _e('Title:', 'event_espresso'); ?>
  62. </label>
  63. <input id="<?php echo $this->get_field_id('title'); ?>" class="widefat" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo esc_attr( $instance['title'] ); ?>" type="text" />
  64. </p>
  65. <p>
  66. <label for="<?php echo $this->get_field_id('category_name'); ?>">
  67. <?php _e('Event Category:', 'event_espresso'); ?>
  68. </label>
  69. <?php
  70. $event_categories = array();
  71. if ( $categories = EE_Registry::instance()->load_model( 'Term' )->get_all_ee_categories( TRUE )) {
  72. foreach ( $categories as $category ) {
  73. $event_categories[] = EE_Question_Option::new_instance( array( 'QSO_value' => $category->get( 'slug' ), 'QSO_desc' => $category->get( 'name' )));
  74. }
  75. }
  76. array_unshift( $event_categories, EE_Question_Option::new_instance( array( 'QSO_value' => '', 'QSO_desc' => __(' - display all - ', 'event_espresso'))));
  77. echo EEH_Form_Fields::select(
  78. __('Event Category:', 'event_espresso'),
  79. $instance['category_name'],
  80. $event_categories,
  81. $this->get_field_name('category_name'),
  82. $this->get_field_id('category_name')
  83. );
  84. ?>
  85. </p>
  86. <p>
  87. <label for="<?php echo $this->get_field_id('limit'); ?>">
  88. <?php _e('Number of Events to Display:', 'event_espresso'); ?>
  89. </label>
  90. <input id="<?php echo $this->get_field_id('limit'); ?>" name="<?php echo $this->get_field_name('limit'); ?>" value="<?php echo $instance['limit']; ?>" size="3" type="text" />
  91. </p>
  92. <p>
  93. <label for="<?php echo $this->get_field_id('show_expired'); ?>">
  94. <?php _e('Show Expired Events:', 'event_espresso'); ?>
  95. </label>
  96. <?php
  97. echo EEH_Form_Fields::select(
  98. __('Show Expired Events:', 'event_espresso'),
  99. $instance['show_expired'],
  100. $yes_no_values,
  101. $this->get_field_name('show_expired'),
  102. $this->get_field_id('show_expired')
  103. );
  104. ?>
  105. </p>
  106. <p>
  107. <label for="<?php echo $this->get_field_id('image_size'); ?>">
  108. <?php _e('Image Size:', 'event_espresso'); ?>
  109. </label>
  110. <?php
  111. $image_sizes = array();
  112. if ( $sizes = get_intermediate_image_sizes() ) {
  113. // loop thru images and create option objects out of them
  114. foreach ( $sizes as $image_size ) {
  115. $image_size = trim( $image_size );
  116. // no big images plz
  117. if ( ! in_array( $image_size, array( 'large', 'post-thumbnail' ))) {
  118. $image_sizes[] = EE_Question_Option::new_instance( array( 'QSO_value' => $image_size, 'QSO_desc' => $image_size ));
  119. }
  120. }
  121. $image_sizes[] = EE_Question_Option::new_instance( array( 'QSO_value' => 'none', 'QSO_desc' => __('don\'t show images', 'event_espresso') ));
  122. }
  123. echo EEH_Form_Fields::select(
  124. __('Image Size:', 'event_espresso'),
  125. $instance['image_size'],
  126. $image_sizes,
  127. $this->get_field_name('image_size'),
  128. $this->get_field_id('image_size')
  129. );
  130. ?>
  131.  
  132. </p>
  133. <p>
  134. <label for="<?php echo $this->get_field_id('show_desc'); ?>">
  135. <?php _e('Show Description:', 'event_espresso'); ?>
  136. </label>
  137. <?php
  138. echo EEH_Form_Fields::select(
  139. __('Show Description:', 'event_espresso'),
  140. $instance['show_desc'],
  141. $yes_no_values,
  142. $this->get_field_name('show_desc'),
  143. $this->get_field_id('show_desc')
  144. );
  145. ?>
  146. </p>
  147. <p>
  148. <label for="<?php echo $this->get_field_id('show_dates'); ?>">
  149. <?php _e('Show Dates:', 'event_espresso'); ?>
  150. </label>
  151. <?php
  152. echo EEH_Form_Fields::select(
  153. __('Show Dates:', 'event_espresso'),
  154. $instance['show_dates'],
  155. $yes_no_values,
  156. $this->get_field_name('show_dates'),
  157. $this->get_field_id('show_dates')
  158. );
  159. ?>
  160. </p>
  161.  
  162. <?php
  163. }
  164.  
  165.  
  166.  
  167. /**
  168. * Sanitize widget form values as they are saved.
  169. *
  170. * @see WP_Widget::update()
  171. *
  172. * @param array $new_instance Values just sent to be saved.
  173. * @param array $old_instance Previously saved values from database.
  174. *
  175. * @return array Updated safe values to be saved.
  176. */
  177. public function update( $new_instance, $old_instance ) {
  178. $instance = $old_instance;
  179. $instance['title'] = ! empty( $new_instance['title'] ) ? strip_tags( $new_instance['title'] ) : '';
  180. $instance['category_name'] = $new_instance['category_name'];
  181. $instance['show_expired'] = $new_instance['show_expired'];
  182. $instance['limit'] = $new_instance['limit'];
  183. $instance['image_size'] = $new_instance['image_size'];
  184. $instance['show_desc'] = $new_instance['show_desc'];
  185. $instance['show_dates'] = $new_instance['show_dates'];
  186. return $instance;
  187. }
  188.  
  189.  
  190.  
  191. /**
  192. * Front-end display of widget.
  193. *
  194. * @see WP_Widget::widget()
  195. *
  196. * @param array $args Widget arguments.
  197. * @param array $instance Saved values from database.
  198. */
  199. public function widget( $args, $instance ) {
  200.  
  201. global $post;
  202. // make sure there is some kinda post object
  203. if ( $post instanceof WP_Post ) {
  204. // but NOT an events archives page, cuz that would be like two event lists on the same page
  205. if ( ! ( $post->post_type == 'espresso_events' && is_archive() )) {
  206. // let's use some of the event helper functions'
  207. EE_Registry::instance()->load_helper( 'Event_View' );
  208. // make separate vars out of attributes
  209. extract($args);
  210. // filter the title
  211. $title = apply_filters('widget_title', $instance['title']);
  212. // Before widget (defined by themes).
  213. echo $before_widget;
  214. // Display the widget title if one was input (before and after defined by themes).
  215. if ( ! empty( $title )) {
  216. echo $before_title . $title . $after_title;
  217. }
  218. // grab widget settings
  219. $category = isset( $instance['category_name'] ) && ! empty( $instance['category_name'] ) ? $instance['category_name'] : FALSE;
  220. $show_expired = isset( $instance['show_expired'] ) ? (bool) absint( $instance['show_expired'] ) : FALSE;
  221. $image_size = isset( $instance['image_size'] ) && ! empty( $instance['image_size'] ) ? $instance['image_size'] : 'medium';
  222. $show_desc = isset( $instance['show_desc'] ) ? (bool) absint( $instance['show_desc'] ) : TRUE;
  223. $show_dates = isset( $instance['show_dates'] ) ? (bool) absint( $instance['show_dates'] ) : TRUE;
  224. // start to build our where clause
  225. $where = array(
  226. // 'Datetime.DTT_is_primary' => 1,
  227. 'status' => 'publish'
  228. );
  229. // add category
  230. if ( $category ) {
  231. $where['Term_Taxonomy.taxonomy'] = 'espresso_event_categories';
  232. $where['Term_Taxonomy.Term.slug'] = $category;
  233. }
  234. // if NOT expired then we want events that start today or in the future
  235. if ( ! $show_expired ) {
  236. $where['Datetime.DTT_EVT_start'] = array( '>=', date( 'Y-m-d' ));
  237. }
  238. // run the query
  239. $events = EE_Registry::instance()->load_model( 'Event' )->get_all( array(
  240. $where,
  241. 'limit' => $instance['limit'] > 0 ? '0,' . $instance['limit'] : '0,10',
  242. 'order_by' => 'Datetime.DTT_EVT_start',
  243. 'order' => 'ASC',
  244. 'group_by' => 'EVT_ID'
  245. ));
  246.  
  247. if ( ! empty( $events )) {
  248. echo '<ul class="ee-upcoming-events-widget-ul">';
  249. foreach ( $events as $event ) {
  250. if ( $event instanceof EE_Event && $post->ID != $event->ID() ) {
  251. //printr( $event, '$event <br /><span style="font-size:10px;font-weight:normal;">' . __FILE__ . '<br />line no: ' . __LINE__ . '</span>', 'auto' );
  252. echo '<li id="ee-upcoming-events-widget-li-' . $event->ID() . '" class="ee-upcoming-events-widget-li">';
  253. // how big is the event name ?
  254. $name_length = strlen( $event->name() );
  255. switch( $name_length ) {
  256. case $name_length > 70 :
  257. $len_class = 'three-line';
  258. break;
  259. case $name_length > 35 :
  260. $len_class = 'two-line';
  261. break;
  262. default :
  263. $len_class = 'one-line';
  264. }
  265. echo '<a class="ee-widget-event-name-a" href="' . get_permalink( $event->ID() ) . '">' . $event->name() . '</a>';
  266. if ( has_post_thumbnail( $event->ID() ) && $image_size != 'none' ) {
  267. echo '<a class="ee-upcoming-events-widget-img" href="' . get_permalink( $event->ID() ) . '">' . get_the_post_thumbnail( $event->ID(), $image_size ) . '</a>';
  268. }
  269. if ( $show_dates ) {
  270. echo espresso_list_of_event_dates( $event->ID(), 'D j. M, Y', 'H:i', FALSE, NULL, TRUE, TRUE );
  271. }
  272. if ( $show_desc && $desc = $event->short_description( 25 )) {
  273. echo '<p>' . $desc . '</p>';
  274. }
  275. echo '</li>';
  276. }
  277. }
  278. echo '</ul>';
  279. }
  280. // After widget (defined by themes).
  281. echo $after_widget;
  282. }
  283. }
  284. }
  285.  
  286. }
  287. // End of file EEW_Upcoming_Events.widget.php
  288. // Location: /widgets/upcoming_events/EEW_Upcoming_Events.widget.php
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement