Skip to content

Instantly share code, notes, and snippets.

@bpmore
Forked from tomfinitely/events-widgets.php
Created August 2, 2016 18:56
Show Gist options
  • Save bpmore/7eb65693c1a429a221b2bc9008b835e7 to your computer and use it in GitHub Desktop.
Save bpmore/7eb65693c1a429a221b2bc9008b835e7 to your computer and use it in GitHub Desktop.
The Events Calendar Pro - Cross-site Events Widget
<?php
if ( ! defined( 'ABSPATH' ) ) die('-1');
/*
* WDG-customized version of the Events Calendar Pro mini calendar widget.
* Changes are noted inline.
* Replacing class name: TribeEventsMiniCalendarWidget
*/
class MultisiteTribeEventsMiniCalendarWidget extends WP_Widget {
function __construct() {
// @M new classname
$widget_ops = array( 'classname' => 'multisite_tribe_mini_calendar_widget',
'description' => __( 'The events calendar mini calendar widget', 'tribe-events-calendar-pro' ) );
// @M new widget slug, widget name
parent::__construct( 'multisite-tribe-mini-calendar', __( 'University Events Calendar', 'tribe-events-calendar-pro' ), $widget_ops );
add_action( 'admin_enqueue_scripts', array( $this, 'load_scripts' ) );
}
public function load_scripts( $hook ) {
if ( $hook != 'widgets.php' )
return;
Tribe_Template_Factory::asset_package( 'select2' );
wp_enqueue_script( 'calendar-widget-admin', TribeEventsPro::instance()->pluginUrl . 'resources/calendar-widget-admin.js', array(), apply_filters( 'tribe_events_pro_js_version', TribeEventsPro::VERSION ) );
}
function widget( $args, $instance ) {
$ecp = TribeEventsPro::instance();
$tooltip_status = $ecp->recurring_info_tooltip_status();
$ecp->disable_recurring_info_tooltip();
add_filter( 'tribe_events_list_show_ical_link', '__return_false' );
echo $args['before_widget'];
$defaults = array( 'title' => __( 'Events Calendar', 'tribe-events-calendar-pro' ), 'count' => 5, 'filters' => null, 'operand' => 'OR' );
$instance = wp_parse_args( (array) $instance, $defaults );
// @M switch to blog 1 for taxonomy query
global $blog_id;
if($blog_id != 1) {
switch_to_blog(1);
}
$tax_query = TribeEventsPro_Widgets::form_tax_query( json_decode( $instance['filters'] ), $instance['operand'] );
do_action( 'tribe_events_mini_cal_before_the_title' );
echo ( $instance['title'] ) ? $args['before_title'] . $instance['title'] . $args['after_title'] : '';
do_action( 'tribe_events_mini_cal_after_the_title' );
$instance['tax_query'] = $tax_query;
TribeEventsMiniCalendar::instance()->do_calendar( $instance );
echo $args['after_widget'];
remove_filter( 'tribe_events_list_show_ical_link', '__return_false' );
if ( $tooltip_status ) {
$ecp->enable_recurring_info_tooltip();
}
// @M switch back if needed
if(ms_is_switched()) {
restore_current_blog();
}
}
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags( $new_instance['title'] );
$instance['count'] = intval( strip_tags( $new_instance['count'] ) );
$instance['operand'] = strip_tags( $new_instance['operand'] );
$instance['filters'] = maybe_unserialize( $new_instance['filters'] );
return $instance;
}
function form( $instance ) {
$defaults = array( 'title' => __( 'Events Calendar', 'tribe-events-calendar-pro' ), 'layout' => "tall", 'count' => 5, 'operand' => 'OR', 'filters' => null );
$instance = wp_parse_args( (array) $instance, $defaults );
// @M switch to blog 1 for taxonomy query
global $blog_id;
if($blog_id != 1) {
switch_to_blog(1);
}
$taxonomies = get_object_taxonomies( TribeEvents::POSTTYPE, 'objects' );
$taxonomies = array_reverse( $taxonomies );
$ts = TribeEventsPro::instance();
include $ts->pluginPath . 'admin-views/widget-calendar.php';
// @M switch back if needed
if(ms_is_switched()) {
restore_current_blog();
}
}
}
/*
* WDG-customized version of the Events Calendar Pro events list widget.
* Changes are noted inline.
* Replacing class name: TribeEventsListWidget
*/
class MultisiteTribeEventsListWidget extends WP_Widget
{
/**
* Allows widgets extending this one to pass through their own unique name, ID base etc.
*
* @param string $id_base
* @param string $name
* @param array $widget_options
* @param array $control_options
*/
public function __construct($id_base = '', $name = '', $widget_options = array(), $control_options = array())
{
$widget_options = array_merge(array(
// @M new classname
'classname' => 'multisite-tribe-events-list-widget',
'description' => __('A widget that displays upcoming events.', 'tribe-events-calendar')),
$widget_options
);
// @M new widget slug
$control_options = array_merge(array('id_base' => 'multisite-tribe-events-list-widget'), $control_options);
// @M new widget slug
$id_base = empty($id_base) ? 'multisite-tribe-events-list-widget' : $id_base;
// @M new widget name
$name = empty($name) ? __('University Events List', 'tribe-events-calendar') : $name;
parent::__construct($id_base, $name, $widget_options, $control_options);
}
/**
* The main widget output function.
*
* @param array $args
* @param array $instance
* @return string The widget output (html).
*/
function widget($args, $instance)
{
return $this->widget_output($args, $instance);
}
/**
* The main widget output function (called by the class's widget() function).
*
* @param array $args
* @param array $instance
* @param string $template_name The template name.
* @param string $subfolder The subfolder where the template can be found.
* @param string $namespace The namespace for the widget template stuff.
* @param string $pluginPath The pluginpath so we can locate the template stuff.
*/
function widget_output($args, $instance, $template_name = 'widgets/list-widget')
{
global $wp_query, $tribe_ecp, $post;
$instance = wp_parse_args($instance, array(
'limit' => 5,
'title' => ''
));
/**
* @var $after_title
* @var $after_widget
* @var $before_title
* @var $before_widget
* @var $limit
* @var $no_upcoming_events
* @var $title
*/
extract($args, EXTR_SKIP);
extract($instance, EXTR_SKIP);
// Temporarily unset the tribe bar params so they don't apply
$hold_tribe_bar_args = array();
foreach ($_REQUEST as $key => $value) {
if ($value && strpos($key, 'tribe-bar-') === 0) {
$hold_tribe_bar_args[$key] = $value;
unset($_REQUEST[$key]);
}
}
$title = apply_filters('widget_title', $title);
if (!function_exists('tribe_get_events')) return;
// @M switch to blog 1 for events query
global $blog_id;
if($blog_id != 1) {
switch_to_blog(1);
}
$posts = tribe_get_events(apply_filters('tribe_events_list_widget_query_args', array(
'eventDisplay' => 'upcoming',
'posts_per_page' => $limit
)));
// If no posts, and the don't show if no posts checked, let's bail
if (!$posts && $no_upcoming_events) return;
echo $before_widget;
do_action('tribe_events_before_list_widget');
do_action('tribe_events_list_widget_before_the_title');
echo ($title) ? $before_title . $title . $after_title : '';
do_action('tribe_events_list_widget_after_the_title');
// Include template file
include TribeEventsTemplates::getTemplateHierarchy($template_name);
do_action('tribe_events_after_list_widget');
echo $after_widget;
wp_reset_query();
// Reinstate the tribe bar params
if (!empty($hold_tribe_bar_args))
foreach ($hold_tribe_bar_args as $key => $value)
$_REQUEST[$key] = $value;
// @M switch back if needed
if(ms_is_switched()) {
restore_current_blog();
}
}
/**
* The function for saving widget updates in the admin section.
*
* @param array $new_instance
* @param array $old_instance
* @return array The new widget settings.
*/
function update($new_instance, $old_instance)
{
$instance = $old_instance;
/* Strip tags (if needed) and update the widget settings. */
$instance['title'] = strip_tags($new_instance['title']);
$instance['limit'] = $new_instance['limit'];
$instance['no_upcoming_events'] = $new_instance['no_upcoming_events'];
return $instance;
}
/**
* Output the admin form for the widget.
*
* @param array $instance
* @return string The output for the admin widget form.
*/
function form($instance)
{
/* Set up default widget settings. */
$defaults = array('title' => __('Upcoming Events', 'tribe-events-calendar'), 'limit' => '5', 'no_upcoming_events' => false);
$instance = wp_parse_args((array)$instance, $defaults);
// @M switch to blog 1 for events query
global $blog_id;
if($blog_id != 1) {
switch_to_blog(1);
}
$tribe_ecp = TribeEvents::instance();
include($tribe_ecp->pluginPath . 'admin-views/widget-admin-list.php');
// @M switch back if needed
if(ms_is_switched()) {
restore_current_blog();
}
}
}
/*
* WDG-customized version of the Events Calendar Pro events list (pro version) widget
* The pro version and free version of this widget is kept split to ease the update process (if needed)
* Changes are noted inline.
* Replacing class name: TribeEventsAdvancedListWidget originally extending TribeEventsListWidget
*/
class MultisiteTribeEventsAdvancedListWidget extends MultisiteTribeEventsListWidget {
/**
* @var array
*/
public $instance = array();
public function __construct() {
$widget_ops = array(
// @M new classname
'classname' => 'multisite-tribe-events-adv-list-widget',
'description' => __( 'A widget that displays the next upcoming x events.', 'tribe-events-calendar-pro' ) );
// @M new widget slug
$control_ops = array( 'id_base' => 'multisite-tribe-events-adv-list-widget' );
// @M new widget name
parent::__construct( 'multisite-tribe-events-adv-list-widget', __( 'University Events List', 'tribe-events-calendar-pro' ), $widget_ops, $control_ops );
add_filter( 'tribe_events_list_widget_query_args', array( $this, 'taxonomy_filters' ) );
}
public function taxonomy_filters( $query ) {
if ( empty( $this->instance ) ) return $query;
$tax_query = TribeEventsPro_Widgets::form_tax_query( json_decode( $this->instance['filters'] ), $this->instance['operand'] );
if ( isset( $query['tax_query'] ) ) $query['tax_query'] = array_merge( $query['tax_query'], $tax_query );
else $query['tax_query'] = $tax_query;
return $query;
}
public function widget( $args, $instance ) {
// @M switch to blog 1 for events widget instance
global $blog_id;
if($blog_id != 1) {
switch_to_blog(1);
}
$ecp = TribeEventsPro::instance();
$tooltip_status = $ecp->recurring_info_tooltip_status();
$ecp->disable_recurring_info_tooltip();
$this->instance_defaults( $instance );
// @todo remove after 3.7 (continuity helper for upgrading users)
if ( isset( $this->instance['category'] ) ) $this->include_cat_id( $this->instance['filters'], $this->instance['category'] );
if ( $tooltip_status ) $ecp->enable_recurring_info_tooltip();
parent::widget_output( $args, $this->instance, 'pro/widgets/list-widget' );
// @M switch back if needed
if(ms_is_switched()) {
restore_current_blog();
}
}
public function update( $new_instance, $old_instance ) {
$instance = parent::update( $new_instance, $old_instance );
$instance['venue'] = $new_instance['venue'];
$instance['country'] = $new_instance['country'];
$instance['address'] = $new_instance['address'];
$instance['city'] = $new_instance['city'];
$instance['region'] = $new_instance['region'];
$instance['zip'] = $new_instance['zip'];
$instance['phone'] = $new_instance['phone'];
$instance['cost'] = $new_instance['cost'];
$instance['organizer'] = $new_instance['organizer'];
$instance['operand'] = strip_tags( $new_instance['operand'] );
$instance['filters'] = maybe_unserialize( $new_instance['filters'] );
// @todo remove after 3.7 (added for continuity when users transition from 3.5.x or earlier to this release)
if ( isset( $old_instance['category'] ) ) {
$this->include_cat_id($instance['filters'], $old_instance['category']);
unset( $instance['category'] );
}
return $instance;
}
public function form( $instance ) {
$this->instance_defaults( $instance );
$this->include_cat_id( $this->instance['filters'], $this->instance['category'] ); // @todo remove after 3.7
// @M switch to blog 1 for events taxonomy query
global $blog_id;
if($blog_id != 1) {
switch_to_blog(1);
}
$taxonomies = get_object_taxonomies( TribeEvents::POSTTYPE, 'objects' );
$taxonomies = array_reverse( $taxonomies );
$instance = $this->instance;
include( TribeEventsPro::instance()->pluginPath . 'admin-views/widget-admin-advanced-list.php' );
// @M switch back if needed
if(ms_is_switched()) {
restore_current_blog();
}
}
protected function instance_defaults( $instance ) {
$this->instance = wp_parse_args( (array) $instance, array(
'title' => __( 'Upcoming Events', 'tribe-events-calendar-pro' ),
'limit' => '5',
'no_upcoming_events' => false,
'venue' => false,
'country' => true,
'address' => false,
'city' => true,
'region' => true,
'zip' => false,
'phone' => false,
'cost' => false,
'category' => false, // @todo remove this element after 3.7
'organizer' => false,
'operand' => 'OR',
'filters' => ''
) );
}
/**
* Adds the provided category ID to the list of filters.
*
* In 3.6 taxonomy filters were added to this widget (as already existed for the calendar
* widget): this helper exists to provide some continuity for users upgrading from a 3.5.x
* release or earlier, transitioning any existing category setting to the new filters
* list.
*
* @todo remove after 3.7
* @param mixed &$filters
* @param int $id
*/
protected function include_cat_id( &$filters, $id ) {
$id = (string) absint( $id ); // An absint for sanity but a string for comparison purposes
$tax = TribeEvents::TAXONOMY;
if ( '0' === $id || ! is_string( $filters ) ) return;
$filters = (array) json_decode( $filters, true );
if ( isset( $filters[$tax] ) && ! in_array( $id, $filters[$tax] ) ) $filters[$tax][] = $id;
elseif ( ! isset( $filters[$tax] ) ) $filters[$tax] = array( $id );
$filters = json_encode( $filters );
}
}
// @M Registering new widgets
/*
* Add new events widgets
*/
add_action( 'widgets_init', 'su_add_multisite_events_widgets', 99 );
function su_add_multisite_events_widgets()
{
register_widget( 'MultisiteTribeEventsMiniCalendarWidget' );
register_widget( 'MultisiteTribeEventsAdvancedListWidget' );
}
// @M All following lines are for customizing which widgets are available
/*
* Unregister unused events widgets
*/
function su_unregister_multisite_events_widgets() {
unregister_widget( 'TribeCountdownWidget' );
}
add_action( 'widgets_init', 'su_unregister_multisite_events_widgets', 99 );
/*
* Remove action for unused events widgets
*/
remove_action( 'widgets_init', 'tribe_venue_register_widget', 100 );
// @M Registering new widgets
/*
* Buy link addition to events on all sites.
*/
if(function_exists("register_field_group"))
{
register_field_group(array (
'id' => 'acf_events-fields',
'title' => 'Events Fields',
'fields' => array (
array (
'key' => 'field_5411b3e0ad4c3',
'label' => 'Buy Link',
'name' => 'buy_link',
'type' => 'text',
'instructions' => 'Insert a url for purchasing tickets if applicable.',
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'formatting' => 'none',
'maxlength' => '',
),
),
'location' => array (
array (
array (
'param' => 'post_type',
'operator' => '==',
'value' => 'tribe_events',
'order_no' => 0,
'group_no' => 0,
),
),
),
'options' => array (
'position' => 'side',
'layout' => 'no_box',
'hide_on_screen' => array (
),
),
'menu_order' => 0,
));
}
/*
* Delete during cleanup
*/
// Testing multisite events blog switch
//add_action( 'tribe_events_parse_query', 'su_test_events');
/*
add_action( 'tribe_events_pre_get_posts', 'su_test_events');
add_action( 'wp', 'su_restore_events');
function su_test_events($query) {
global $blog_id;
if($blog_id != 1) {
switch_to_blog(1);
}
print '<pre>';
print_r($query);
print '</pre>';
}
function su_restore_events() {
if(ms_is_switched()) {
restore_current_blog();
}
}
*/
/*
add_action('tribe_cpt_filters_init', 'su_events_switch_blog');
add_action('tribe_cpt_filters_after_init', 'su_events_switch_back');
function su_events_switch_blog() {
global $blog_id;
if($blog_id != 1) {
switch_to_blog(1);
}
}
function su_events_switch_back() {
restore_current_blog();
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment