cliffordp
6/14/2016 - 9:45 PM

v1.1 of https://theeventscalendar.com/knowledgebase/listing-venues-and-organizers/ to add ORDER, ORDERBY, and EXCLUDE arguments. Also adds l

v1.1 of https://theeventscalendar.com/knowledgebase/listing-venues-and-organizers/ to add ORDER, ORDERBY, and EXCLUDE arguments. Also adds logic for linking to each item only if PRO is active.

<?php
/**
 * Plugin name: The Events Calendar: List Venues/Organizers Shortcodes
 * Description: Adds shortcodes to help list venues and organizers. More info at http://m.tri.be/194s
 * Author:      Modern Tribe, Inc
 * Author URI:  http://theeventscalendar.com
 * Version:     1.1
 * License:     GPL v3 - see http://www.gnu.org/licenses/gpl.html
 *
 *     The Events Calendar: Venue/Organizer Shortcodes
 *     Copyright (C) 2015 Modern Tribe, Inc
 *
 *     This program is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


class TEC_VenueOrganizer_List {
	protected $atts = array();
	protected $query;
	protected $output = '';


	public function __construct( $atts ) {
		$this->atts = shortcode_atts( array(
			'post_type' => self::get_type( 'VENUE_POST_TYPE' ),
			'limit'     => -1,
			'order'		=> 'ASC',
			'orderby'	=> 'post_title',
			'exclude'	=> '', // comma-separated list of Venue post IDs or Organizer post IDs
		), $atts );
	}

	public function __toString() {
		$this->query();
		$this->format();
		return $this->output;
	}

	protected function query() {
		$exclude = preg_replace( '/\s+/', '', $this->atts['exclude'] ); // remove all whitespaces
		$exclude = explode( ',', $exclude );
		
		$args = array(
			'post_type'      => $this->atts['post_type'],
			'posts_per_page' => $this->atts['limit'],
			'order'			 => $this->atts['order'],
			'orderby'		 => $this->atts['orderby'],
			'post__not_in'	 => $exclude, // must be an array
		);

		$this->query = new WP_Query( apply_filters( __CLASS__ . '.args', $args, $this->atts ) );
	}

	protected function format() {
		$opening_tag  = '<ul class="tec list ' . $this->atts['post_type'] . '">';
		$this->output = apply_filters( __CLASS__ . '.list.open', $opening_tag, $this->atts );

		while ( $this->query->have_posts() ) {
			$this->query->the_post();
			
			// if TEC PRO, link to Venue/Organizer, else just output title without link
			if ( defined( 'EVENTS_CALENDAR_PRO_DIR' ) ) {
				$link = '<a href="' . get_the_permalink() . '">' . get_the_title() . '</a>';
			} else {
				$link = get_the_title();
			}
			
			$item = '<li id="' . $this->atts['post_type'] . '-' . get_the_ID() . '" class="tec list ' . $this->atts['post_type'] . '">' . $link . '</li>';
			
			$this->output .= apply_filters( __CLASS__ . '.list.item', $item, $this->atts );
		}

		$this->output .= apply_filters( __CLASS__ . '.list.close', '</ul>', $this->atts );
		wp_reset_postdata();
	}

	public static function get_type( $type_const ) {
		if ( class_exists( 'Tribe__Events__Main' ) ) $class = 'Tribe__Events__Main';
		elseif ( class_exists( 'TribeEvents' ) )     $class = 'TribeEvents';
		else return false;

		$class = new ReflectionClass( $class );
		return $class->getConstant( $type_const );
	}
}


function tec_do_venue_shortcode( $atts ) {
	$type = TEC_VenueOrganizer_List::get_type( 'VENUE_POST_TYPE' );
	if ( ! $type  ) return '';

	$atts['post_type'] = $type;
	return new TEC_VenueOrganizer_List( $atts );
}

function tec_do_organizer_shortcode( $atts ) {
	$type = TEC_VenueOrganizer_List::get_type( 'ORGANIZER_POST_TYPE' );
	if ( ! $type  ) return '';

	$atts['post_type'] = $type;
	return new TEC_VenueOrganizer_List( $atts );
}


add_shortcode( 'list_venues', 'tec_do_venue_shortcode' );
add_shortcode( 'list_organizers', 'tec_do_organizer_shortcode' );