Advertisement
Guest User

EEWidget

a guest
Feb 25th, 2014
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 13.98 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4.  * To change this license header, choose License Headers in Project Properties.
  5.  * To change this template file, choose Tools | Templates
  6.  * and open the template in the editor.
  7.  */
  8.  
  9. /**
  10.  * Event Espresso
  11.  *
  12.  * Event Registration and Management Plugin for WordPress
  13.  *
  14.  * @ package            Event Espresso
  15.  * @ author         Seth Shoultes
  16.  * @ copyright      (c) 2008-2011 Event Espresso  All Rights Reserved.
  17.  * @ license            http://eventespresso.com/support/terms-conditions/   * see Plugin Licensing *
  18.  * @ link               http://www.eventespresso.com
  19.  * @ version            4.0
  20.  *
  21.  * ------------------------------------------------------------------------
  22.  *
  23.  * Upcoming Events Widget
  24.  *
  25.  * @package     Event Espresso
  26.  * @subpackage  /widgets/upcoming_events/
  27.  * @author      Brent Christensen
  28.  *
  29.  * ------------------------------------------------------------------------
  30.  */
  31. class EEW_Custom_Upcoming_Events  extends WP_Widget {
  32.  
  33.  
  34.     /**
  35.      * Register widget with WordPress.
  36.      */
  37.     function __construct() {
  38.         parent::__construct(
  39.             'ee-upcoming-events-widget',
  40.             __( 'My Custom Event Espresso Upcoming Events', 'event_espresso' ),
  41.              array( 'description' => __( 'A widget to display your upcoming events.', 'event_espresso' )),
  42.             array(
  43.                 'width' => 300,
  44.                 'height' => 350,
  45.                 'id_base' => 'ee-upcoming-events-widget'
  46.             )
  47.         );
  48.     }
  49.  
  50.  
  51.  
  52.     /**
  53.      * Back-end widget form.
  54.      *
  55.      * @see WP_Widget::form()
  56.      *
  57.      * @param array $instance Previously saved values from database.
  58.      */
  59.     public function form( $instance ) {
  60.        
  61.         EE_Registry::instance()->load_helper( 'Form_Fields' );
  62.         EE_Registry::instance()->load_class( 'Question_Option', array(), FALSE, FALSE, TRUE );     
  63.         // Set up some default widget settings.
  64.         $defaults = array(
  65.             'title' => __('Upcoming Events', 'event_espresso'),
  66.             'category_name' => '',
  67.             'show_expired' => FALSE,
  68.             'show_desc' => TRUE,
  69.             'show_dates' => TRUE,
  70.             'show_everywhere' => FALSE,
  71.             'limit' => 10,
  72.             'image_size' => 'medium'
  73.         );
  74.  
  75.         $instance = wp_parse_args( (array) $instance, $defaults );
  76.         // don't add HTML labels for EE_Form_Fields generated inputs
  77.         add_filter( 'FHEE__EEH_Form_Fields__label_html', '__return_empty_string' );    
  78.         $yes_no_values = array(
  79.             EE_Question_Option::new_instance( array( 'QSO_value' => FALSE, 'QSO_desc' => __('No', 'event_espresso'))),
  80.             EE_Question_Option::new_instance( array( 'QSO_value' => TRUE, 'QSO_desc' => __('Yes', 'event_espresso')))
  81.         );
  82.        
  83.     ?>
  84.  
  85.         <!-- Widget Title: Text Input -->
  86.  
  87.         <p>
  88.             <label for="<?php echo $this->get_field_id('title'); ?>">
  89.                 <?php _e('Title:', 'event_espresso'); ?>
  90.             </label>
  91.             <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" />
  92.         </p>
  93.         <p>
  94.             <label for="<?php echo $this->get_field_id('category_name'); ?>">
  95.                 <?php _e('Event Category:', 'event_espresso'); ?>
  96.             </label>
  97.             <?php
  98.             $event_categories = array();
  99.             if ( $categories = EE_Registry::instance()->load_model( 'Term' )->get_all_ee_categories( TRUE )) {             
  100.                 foreach ( $categories as $category ) {
  101.                     $event_categories[] = EE_Question_Option::new_instance( array( 'QSO_value' => $category->get( 'slug' ), 'QSO_desc' => $category->get( 'name' )));
  102.                 }
  103.             }          
  104.             echo EEH_Form_Fields::select(
  105.                  __('Event Category:', 'event_espresso'),
  106.                 $instance['category_name'],
  107.                 $event_categories,
  108.                 $this->get_field_name('category_name'),
  109.                 $this->get_field_id('category_name')
  110.             );
  111.             ?>             
  112.         </p>
  113.         <p>
  114.             <label for="<?php echo $this->get_field_id('limit'); ?>">
  115.                 <?php _e('Number of Events to Display:', 'event_espresso'); ?>
  116.             </label>
  117.             <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" />
  118.         </p>
  119.         <p>
  120.             <label for="<?php echo $this->get_field_id('show_expired'); ?>">
  121.                 <?php _e('Show Expired Events:', 'event_espresso'); ?>
  122.             </label>
  123.             <?php
  124.             echo EEH_Form_Fields::select(
  125.                  __('Show Expired Events:', 'event_espresso'),
  126.                 $instance['show_expired'],
  127.                 $yes_no_values,
  128.                 $this->get_field_name('show_expired'),
  129.                 $this->get_field_id('show_expired')
  130.             );
  131.             ?>         
  132.         </p>
  133.         <p>
  134.             <label for="<?php echo $this->get_field_id('image_size'); ?>">
  135.                 <?php _e('Image Size:', 'event_espresso'); ?>
  136.             </label>
  137.             <?php
  138.             $image_sizes = array();
  139.             if ( $sizes = get_intermediate_image_sizes() ) {
  140.                 // loop thru images and create option objects out of them
  141.                 foreach ( $sizes as $image_size ) {
  142.                     $image_size = trim( $image_size );
  143.                     // no big images plz
  144.                     if ( ! in_array( $image_size, array( 'large', 'post-thumbnail' ))) {
  145.                         $image_sizes[] = EE_Question_Option::new_instance( array( 'QSO_value' => $image_size, 'QSO_desc' => $image_size ));
  146.                     }
  147.                 }
  148.                 $image_sizes[] = EE_Question_Option::new_instance( array( 'QSO_value' => 'none', 'QSO_desc' =>  __('don\'t show images', 'event_espresso') ));
  149.             }          
  150.             echo EEH_Form_Fields::select(
  151.                  __('Image Size:', 'event_espresso'),
  152.                 $instance['image_size'],
  153.                 $image_sizes,
  154.                 $this->get_field_name('image_size'),
  155.                 $this->get_field_id('image_size')
  156.             );
  157.             ?>
  158.    
  159.         </p>
  160.         <p>
  161.             <label for="<?php echo $this->get_field_id('show_desc'); ?>">
  162.                 <?php _e('Show Description:', 'event_espresso'); ?>
  163.             </label>
  164.             <?php
  165.             echo EEH_Form_Fields::select(
  166.                  __('Show Description:', 'event_espresso'),
  167.                 $instance['show_desc'],
  168.                 $yes_no_values,
  169.                 $this->get_field_name('show_desc'),
  170.                 $this->get_field_id('show_desc')
  171.             );
  172.             ?>         
  173.         </p>
  174.         <p>
  175.             <label for="<?php echo $this->get_field_id('show_dates'); ?>">
  176.                 <?php _e('Show Dates:', 'event_espresso'); ?>
  177.             </label>
  178.             <?php
  179.             echo EEH_Form_Fields::select(
  180.                  __('Show Dates:', 'event_espresso'),
  181.                 $instance['show_dates'],
  182.                 $yes_no_values,
  183.                 $this->get_field_name('show_dates'),
  184.                 $this->get_field_id('show_dates')
  185.             );
  186.             ?>         
  187.         </p>
  188.         <p>
  189.             <label for="<?php echo $this->get_field_id('show_everywhere'); ?>">
  190.                 <?php _e('Show on all Pages:', 'event_espresso'); ?>
  191.             </label>
  192.             <?php
  193.             echo EEH_Form_Fields::select(
  194.                  __('Show on all Pages:', 'event_espresso'),
  195.                 $instance['show_everywhere'],
  196.                 $yes_no_values,
  197.                 $this->get_field_name('show_everywhere'),
  198.                 $this->get_field_id('show_everywhere')
  199.             );
  200.             ?>         
  201.         </p>
  202.  
  203.         <?php
  204.     }
  205.  
  206.  
  207.  
  208.     /**
  209.      * Sanitize widget form values as they are saved.
  210.      *
  211.      * @see WP_Widget::update()
  212.      *
  213.      * @param array $new_instance Values just sent to be saved.
  214.      * @param array $old_instance Previously saved values from database.
  215.      *
  216.      * @return array Updated safe values to be saved.
  217.      */
  218.     public function update( $new_instance, $old_instance ) {       
  219.         $instance = $old_instance;
  220.         $instance['title'] = ! empty( $new_instance['title'] ) ? strip_tags( $new_instance['title'] ) : '';
  221.         $instance['category_name'] = $new_instance['category_name'];
  222.         $instance['show_expired'] = $new_instance['show_expired'];
  223.         $instance['limit'] = $new_instance['limit'];
  224.         $instance['image_size'] = $new_instance['image_size'];
  225.         $instance['show_desc'] = $new_instance['show_desc'];
  226.         $instance['show_dates'] = $new_instance['show_dates'];
  227.         $instance['show_everywhere'] = $new_instance['show_everywhere'];
  228.         return $instance;
  229.     }
  230.  
  231.  
  232.  
  233.     /**
  234.      * Front-end display of widget.
  235.      *
  236.      * @see WP_Widget::widget()
  237.      *
  238.      * @param array $args     Widget arguments.
  239.      * @param array $instance Saved values from database.
  240.      */
  241.     public function widget( $args, $instance ) {
  242.        
  243.         global $post, $wpdb, $espresso_manager, $current_user, $org_options;
  244.         // make sure there is some kinda post object
  245.         if ( $post instanceof WP_Post ) {
  246.             // but NOT an events archives page, cuz that would be like two event lists on the same page
  247.             $show_everywhere = isset( $instance['show_everywhere'] ) ? (bool) absint( $instance['show_everywhere'] ) : TRUE;
  248.             if ( $show_everywhere || ! ( $post->post_type == 'espresso_events' && is_archive() )) {            
  249.                 // let's use some of the event helper functions'
  250.                 EE_Registry::instance()->load_helper( 'Event_View' );
  251.                 // make separate vars out of attributes
  252.                 extract($args);
  253.                 // filter the title
  254.                 $title = apply_filters('widget_title', $instance['title']);
  255.                 // Before widget (defined by themes).
  256.                 echo $before_widget;
  257.                 // Display the widget title if one was input (before and after defined by themes).
  258.                
  259.                 // grab widget settings
  260.                 $category = isset( $instance['category_name'] ) && ! empty( $instance['category_name'] ) ? $instance['category_name'] : FALSE;
  261.                 $show_expired = isset( $instance['show_expired'] ) ? (bool) absint( $instance['show_expired'] ) : FALSE;
  262.                 $image_size = isset( $instance['image_size'] ) && ! empty( $instance['image_size'] ) ? $instance['image_size'] : 'medium';
  263.                 $show_desc = isset( $instance['show_desc'] ) ? (bool) absint( $instance['show_desc'] ) : TRUE;
  264.                 $show_dates = isset( $instance['show_dates'] ) ? (bool) absint( $instance['show_dates'] ) : TRUE;
  265.                 // start to build our where clause
  266.                 $where = array(
  267. //                  'Datetime.DTT_is_primary' => 1,
  268.                     'status' => 'publish'
  269.                 );
  270.                 // add category
  271.                 if ( $category ) {
  272.                     $where['Term_Taxonomy.taxonomy'] = 'espresso_event_categories';
  273.                     $where['Term_Taxonomy.Term.slug'] = $category;
  274.                 }
  275.                 // if NOT expired then we want events that start today or in the future
  276.                 if ( ! $show_expired ) {
  277.                     $where['Datetime.DTT_EVT_start'] = array( '>=', date( 'Y-m-d' ));
  278.                 }
  279.                 // run the query
  280.                 $events = EE_Registry::instance()->load_model( 'Event' )->get_all( array(
  281.                     $where,    
  282.                     'limit' => $instance['limit'] > 0 ? '0,' . $instance['limit'] : '0,10',
  283.                     'order_by' => 'Datetime.DTT_EVT_start',
  284.                     'order' => 'ASC',
  285.                     'group_by' => 'EVT_ID'
  286.                 ));
  287.  
  288.                 // In the event list, the $post is the last event in the list
  289.                 // https://events.codebasehq.com/projects/event-espresso/tickets/5275
  290.                 $post_id = $_GET['espresso_events'] === $post->post_name ? $post->ID : NULL;
  291.                
  292.                 if ( ! empty( $events )) {
  293.                     echo '<div class="widget">';
  294.                     foreach ( $events as $event ) {
  295.                         if ( $event instanceof EE_Event && $post_id != $event->ID() ) {
  296.                             $event_id = "{$event->ID()}";
  297.  
  298.                             //printr( $event, '$event  <br /><span style="font-size:10px;font-weight:normal;">' . __FILE__ . '<br />line no: ' . __LINE__ . '</span>', 'auto' );
  299.                             echo '<div class="book-now">';
  300.                             if ( ! empty( $title )) {
  301.                                 echo $before_title . $title . $after_title;
  302.                             }  
  303.  
  304.                             // how big is the event name ?
  305.                             $name_length = strlen( $event->name() );
  306.                             switch( $name_length ) {
  307.                                 case $name_length > 70 :
  308.                                     $len_class =  'three-line';
  309.                                     break;
  310.                                 case $name_length > 35 :
  311.                                     $len_class =  'two-line';
  312.                                     break;
  313.                                 default :
  314.                                     $len_class =  'one-line';
  315.                             }
  316.                             // echo '<div id="ee-upcoming-events-widget-header-dv-' . $event->ID() . '" class="ee-upcoming-events-widget-header-dv '. $len_class . '">';
  317.                            
  318.                             // echo '<h5 class="ee-upcoming-events-widget-h5"><a href="' . get_permalink( $event->ID() ) . '">' . $event->name() . '</a></h5>';
  319.                             // if ( has_post_thumbnail( $event->ID() ) && $image_size != 'none' ) {                    
  320.                             //  echo '<a class="ee-upcoming-events-widget-img" href="' . get_permalink( $event->ID() ) . '">' . get_the_post_thumbnail( $event->ID(), $image_size ) . '</a>';      
  321.                             // }
  322.                             // echo '</div>';
  323.                             // $start_date = strtotime($event_details->start_date);
  324.                            
  325.                             $sql = "SELECT * FROM wp_esp_datetime WHERE EVT_ID = '$event_id'";
  326.                             $event_details = $wpdb->get_row($sql);
  327.  
  328.                             $current_date = strtotime(date('Y-m-d h:i:s'));
  329.                             $start_date = strtotime($event_details->DTT_EVT_start);
  330.                             $time_remaining = $start_date - $current_date;
  331.  
  332.                             $secondsInAMinute = 60;
  333.                             $secondsInAnHour  = 60 * $secondsInAMinute;
  334.                             $secondsInADay    = 24 * $secondsInAnHour;
  335.  
  336.                             // extract days
  337.                             $days = floor($time_remaining / $secondsInADay);
  338.                             // extract hours
  339.                             $hourSeconds = $time_remaining % $secondsInADay;
  340.                             $hours = floor($hourSeconds / $secondsInAnHour);
  341.                             // extract minutes
  342.                             $minuteSeconds = $hourSeconds % $secondsInAnHour;
  343.                             $minutes = floor($minuteSeconds / $secondsInAMinute);
  344.                             // extract the remaining seconds
  345.                             $remainingSeconds = $minuteSeconds % $secondsInAMinute;
  346.                             $seconds = ceil($remainingSeconds);
  347.  
  348.                             //echo '<div class="time">' . $days . ':' . $hours . ':' .  $minutes .'</div>';
  349.                             echo $event_details->DTT_EVT_start;
  350.                             echo '<div class="location">' . $event->name() . '</div>';
  351.  
  352.                             // if ( $show_desc && $desc = $event->short_description( 25 )) {
  353.                             //  echo  '<h6 class="">' . __('Event Details: ', 'event_espresso') . '</h6><p>' . $desc . '</p>';
  354.                             // }
  355.                             // if ( $show_dates ) {
  356.                             //  echo  '<h6 class="ee-calendar_year-icon-small">' . __('Event Dates: ', 'event_espresso') . '</h6>';
  357.                             //  echo espresso_list_of_event_dates( $event->ID(), 'D M jS, Y', '@ g:i a', FALSE );
  358.                             // }
  359.                             // echo '<br/><br/><br/></li>';
  360.                             ?>
  361.                             <a href="<?php echo get_permalink( $event->ID() ) ?>" class="book-button"><span class="float--left"><span class="dark-blue">Book</span> now </span><span class="arrow arr-dark"></span></a>
  362.                             </div>
  363.  
  364.                             <ul class="recent-links event-links">
  365.  
  366.                                 <li><a href="<?php echo get_permalink( $event->ID() ) ?>">more info on this event <span class="arrow"></span></a></li>
  367.                                 <li><a href="<?php echo home_url('/events') ?>">see other events <span class="arrow"></span></a></li>
  368.  
  369.                             </ul>
  370.  
  371.                         <?php
  372.                         }
  373.                        
  374.                     }
  375.                     echo '</div>';
  376.                
  377.                 }          
  378.                 // After widget (defined by themes).
  379.                 echo $after_widget;            
  380.             }
  381.         }      
  382.     }
  383.  
  384. }
  385. // End of file EEW_Upcoming_Events.widget.php
  386. // Location: /widgets/upcoming_events/EEW_Upcoming_Events.widget.php
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement