Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jentheo/54aba6f1a4515dcb161e3e4af4b3ac5e to your computer and use it in GitHub Desktop.
Save jentheo/54aba6f1a4515dcb161e3e4af4b3ac5e to your computer and use it in GitHub Desktop.
Add "include" option for IDs
<?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
'include' => '',
), $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 );
$include = preg_replace( '/\s+/', '', $this->atts['include'] ); // remove all whitespaces
$include = explode( ',', $include );
$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
'post__in' => $include, // 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' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment