brainchildmedia

The Events Calendar Shortcode Month Attribute

May 19th, 2015
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.16 KB | None | 0 0
  1. <?php
  2. /***
  3.  Plugin Name: The Events Calendar Shortcode by Month
  4.  Plugin URI: http://dandelionwebdesign.com/downloads/shortcode-modern-tribe/
  5.  Description: An addon to add shortcode functionality for <a href="http://wordpress.org/plugins/the-events-calendar/">The Events Calendar Plugin (Free Version) by Modern Tribe</a>.
  6.  Version: 1.0.6
  7.  Author: Dandelion Web Design Inc.
  8.  Contributors: Brainchild Media Group, Reddit user miahelf
  9.  Author URI: http://dandelionwebdesign.com
  10.  Contributor URL: http://brainchildmediagroup.com, http://www.reddit.com/user/miahelf
  11.  License: GPL2 or later
  12.  License URI: http://www.gnu.org/licenses/gpl-2.0.html
  13. */
  14.  
  15. // Avoid direct calls to this file
  16. if ( !defined( 'ABSPATH' ) ) {
  17.     header( 'Status: 403 Forbidden' );
  18.     header( 'HTTP/1.1 403 Forbidden' );
  19.     exit();
  20. }
  21.  
  22. /**
  23.  * Events calendar shortcode addon main class
  24.  *
  25.  * @package events-calendar-shortcode
  26.  * @author Dandelion Web Design Inc.
  27.  * @version 1.0.0
  28.  */
  29. class Events_Calendar_Shortcode
  30. {
  31.     /**
  32.      * Current version of the plugin.
  33.      *
  34.      * @since 1.0.0
  35.      */
  36.     const VERSION = '1.0.6';
  37.  
  38.     /**
  39.      * Constructor. Hooks all interactions to initialize the class.
  40.      *
  41.      * @since 1.0.0
  42.      * @access public
  43.      *
  44.      * @see  add_shortcode()
  45.      */
  46.     public function __construct()
  47.     {
  48.         add_shortcode('ecs-list-events', array($this, 'ecs_fetch_events') ); // link new function to shortcode name
  49.     } // END __construct()
  50.  
  51.     /**
  52.      * Fetch and return required events.
  53.      * @param  array $atts  shortcode attributes
  54.      * @return string   shortcode output
  55.      */
  56.     public function ecs_fetch_events( $atts )
  57.     {
  58.         /**
  59.          * Check if events calendar plugin method exists
  60.          */
  61.         if ( !function_exists( 'tribe_get_events' ) ) {
  62.             return;
  63.         }
  64.  
  65.         global $wp_query, $post;
  66.         $output = '';
  67.         $ecs_event_tax = '';
  68.  
  69.         extract( shortcode_atts( array(
  70.             'cat' => '',
  71.             'month' => '',
  72.             'limit' => 5,
  73.             'eventdetails' => 'true',
  74.             'venue' => 'false',
  75.             'message' => 'There are no upcoming events at this time.',
  76.             'order' => 'ASC',
  77.             'viewall' => 'false',
  78.             'excerpt' => 'false',
  79.             'thumb' => 'false',
  80.             'thumbwidth' => '',
  81.             'thumbheight' => ''
  82.         ), $atts, 'ecs-list-events' ), EXTR_PREFIX_ALL, 'ecs' );
  83.  
  84.         if ($ecs_cat) {
  85.             $ecs_event_tax = array(
  86.                 array(
  87.                     'taxonomy' => 'tribe_events_cat',
  88.                     'field' => 'slug',
  89.                     'terms' => $ecs_cat
  90.                 )
  91.             );
  92.         }
  93.  
  94.         if ($ecs_month) {
  95.  
  96.             $month_array = explode("-", $ecs_month);
  97.  
  98.             $month_yearstr = $month_array[0];
  99.             $month_monthstr = $month_array[1];
  100.  
  101.             $month_startdate = date($month_yearstr . "-" . $month_monthstr . "-1");
  102.             $month_enddate = date($month_yearstr . "-" . $month_monthstr . "-t");
  103.  
  104.             $posts = get_posts( array(
  105.                     'post_type' => 'tribe_events',
  106.                     'posts_per_page' => $ecs_limit,
  107.                     'tax_query'=> $ecs_event_tax,
  108.                     'meta_key' => '_EventEndDate',
  109.                     'orderby' => 'meta_value',
  110.                     'order' => $ecs_order,
  111.                     'meta_query' => array(
  112.                             array(
  113.                                 'key' => '_EventEndDate',
  114.                                 'value' => array($month_startdate, $month_enddate),
  115.                                 'compare' => 'BETWEEN',
  116.                                 'type' => 'DATETIME'
  117.                             )
  118.                         )
  119.             ) );
  120.  
  121.         } else {
  122.  
  123.             $posts = get_posts( array(
  124.                     'post_type' => 'tribe_events',
  125.                     'posts_per_page' => $ecs_limit,
  126.                     'tax_query'=> $ecs_event_tax,
  127.                     'meta_key' => '_EventEndDate',
  128.                     'orderby' => 'meta_value',
  129.                     'order' => $ecs_order,
  130.                     'meta_query' => array(
  131.                                         array(
  132.                                             'key' => '_EventEndDate',
  133.                                             'value' => date('Y-m-d'),
  134.                                             'compare' => '>=',
  135.                                             'type' => 'DATETIME'
  136.                                         )
  137.                                     )
  138.             ) );
  139.  
  140.         }
  141.  
  142.         if ($posts) {
  143.  
  144.             $output .= '<ul class="ecs-event-list">';
  145.             foreach( $posts as $post ) :
  146.                 setup_postdata( $post );
  147.                 $output .= '<li class="ecs-event">';
  148.                     $output .= '<h4 class="entry-title summary">' .
  149.                                     '<a href="' . tribe_get_event_link() . '" rel="bookmark">' . get_the_title() . '</a>
  150.                                </h4>';
  151.  
  152.                     if( self::isValid($ecs_thumb) ) {
  153.                         $thumbWidth = is_numeric($ecs_thumbwidth) ? $ecs_thumbwidth : '';
  154.                         $thumbHeight = is_numeric($ecs_thumbheight) ? $ecs_thumbheight : '';
  155.                         if( !empty($thumbWidth) && !empty($thumbHeight) ) {
  156.                             $output .= get_the_post_thumbnail(get_the_ID(), array($thumbWidth, $thumbHeight) );
  157.                         } else {
  158.                             $output .= get_the_post_thumbnail(get_the_ID(), 'medium');
  159.                         }
  160.                     }
  161.  
  162.                     if( self::isValid($ecs_excerpt) ) {
  163.                         $excerptLength = is_numeric($ecs_excerpt) ? $ecs_excerpt : 100;
  164.                         $output .= '<p class="ecs-excerpt">' .
  165.                                         self::get_excerpt($excerptLength) .
  166.                                     '</p>';
  167.                     }
  168.  
  169.                     if( self::isValid($ecs_eventdetails) ) {    
  170.                         $output .= '<span class="duration time">' . tribe_events_event_schedule_details() . '</span>';
  171.                     }
  172.  
  173.                     if( self::isValid($ecs_venue) ) {
  174.                         $output .= '<span class="duration venue"><em> at </em>' . tribe_get_venue() . '</span>';    
  175.                     }
  176.                 $output .= '</li>';
  177.             endforeach;
  178.             $output .= '</ul>';
  179.  
  180.             if( self::isValid($ecs_viewall) ) {
  181.                 $output .= '<span class="ecs-all-events"><a href="' . tribe_get_events_link() . '" rel="bookmark">' . translate( 'View All Events', 'tribe-events-calendar' ) . '</a></span>';
  182.             }
  183.  
  184.         } else { //No Events were Found
  185.             $output .= translate( $ecs_message, 'tribe-events-calendar' );
  186.         } // endif
  187.  
  188.         wp_reset_query();
  189.  
  190.         return $output;
  191.     }
  192.  
  193.     /**
  194.      * Checks if the plugin attribute is valid
  195.      *
  196.      * @since 1.0.5
  197.      *
  198.      * @param string $prop
  199.      * @return boolean
  200.      */
  201.     private function isValid( $prop )
  202.     {
  203.         return ($prop !== 'false');
  204.     }
  205.  
  206.     /**
  207.      * Fetch and trims the excerpt to specified length
  208.      *
  209.      * @param integer $limit Characters to show
  210.      * @param string $source  content or excerpt
  211.      *
  212.      * @return string
  213.      */
  214.     private function get_excerpt( $limit, $source = null )
  215.     {
  216.         $excerpt = get_the_excerpt();
  217.         if( $source == "content" ) {
  218.             $excerpt = get_the_content();
  219.         }
  220.  
  221.         $excerpt = preg_replace(" (\[.*?\])", '', $excerpt);
  222.         $excerpt = strip_tags( strip_shortcodes($excerpt) );
  223.         $excerpt = substr($excerpt, 0, $limit);
  224.         $excerpt = trim(preg_replace( '/\s+/', ' ', $excerpt));
  225.         $excerpt .= '...';
  226.  
  227.         return $excerpt;
  228.     }
  229. }
  230.  
  231. /**
  232.  * Instantiate the main class
  233.  *
  234.  * @since 1.0.0
  235.  * @access public
  236.  *
  237.  * @var object  $events_calendar_shortcode holds the instantiated class {@uses Events_Calendar_Shortcode}
  238.  */
  239. global $events_calendar_shortcode;
  240. $events_calendar_shortcode = new Events_Calendar_Shortcode();
Add Comment
Please, Sign In to add comment