Advertisement
Guest User

The Events Calendar Shortcode Month Attribute

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