Admidio.
* Version: 0.3.3
* Author: Ulrik Schoth
* Author URI: http://fechten-in-waldkirch.de/kontakt/webmaster/
* Text Domain: admidio-events
* Domain Path: /languages
*
* Module: admidio-events.php
* Description: Widget management.
*
* Copyright (C) 2014 Ulrik Schoth
*
* 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 .
*/
/**
* Admidio Events Widget class.
* This class handles everything that needs to be handled with the widget: Settings (functions *form* and *update*) and output (function *widget*).
*
* @since 0.3.1
*
* @todo Zusätzliche Widget-Option: Start mit Expand-View.
* @todo Direkte Übernahme der HTML-Tags von Admidio unterbinden (Security!).
* @todo Weshalb wird die Beschreibung beim Admidio-Test-Server nicht sauber getrennt?
* @todo Fehler im Setzen der Feed-Cache-Time? Zeit erscheint viel zu kurz.
* @todo Beispiel-RSS-URL entfernen (und lieber in Installationsanleitung darauf verweisen).
* @todo README.TXT fertigstellen.
*/
class Admidio_Events_Widget extends WP_Widget {
/**
* Widget class initialization (set up widget name etc).
*
* @since 0.3.1
*/
function __construct() {
// Create widget.
parent::__construct(
'admidio-events', // Base ID for CSS (usually appended by '-2', '-3', etc. depending on no of used widget instances).
'Admidio Events', // Name used on widget configuration page.
array(
'classname' => 'admidio-events', // Class name for CSS.
'description' => __( 'Event data from Admidio.', 'admidio-events' ) // Description used on widget configuration page.
),
array(
'width' => 400,
'height' => 200
)
);
// Register style sheet and scripts.
add_action( 'wp_enqueue_scripts', array( $this, 'register_plugin_styles_and_scripts' ) );
}
/**
* Register and enqueue style sheet and javascript.
*
* @see http://codex.wordpress.org/Function_Reference/wp_register_style/
* @see https://codex.wordpress.org/Function_Reference/wp_enqueue_style/
*
* @since 0.3.1
*/
public function register_plugin_styles_and_scripts() {
wp_enqueue_style( 'admidio-events', plugins_url( 'admidio-events.css', __FILE__ ), array(), '0.3.1' );
wp_enqueue_script( 'admidio-events-js', plugins_url( 'admidio-events.js', __FILE__ ), array( 'jquery' ), '0.3.1', true );
}
/**
* Echo the settings update form on backend.
*
* @since 0.3.1
*
* @param array $instance - Current settings.
*/
function form( $instance ) {
// Set up default widget settings.
$defaults = array(
'title' => __( 'Upcoming Events', 'admidio-events' ),
'rss_feed' => 'http://demo.admidio.org/adm_program/modules/dates/rss_dates.php',
'no_of_items' => '5',
'show_date' => '0',
);
$instance = wp_parse_args( (array) $instance, $defaults );
?>
/>
' . $title . '' . $after_title;
}
// Get RSS data and handle result.
$rss_items = $this->get_rss_feed_data( $rss_feed, 20 );
if ( $rss_items === false ) {
_e( 'Error when fetching event data.', 'admidio-events' );
return;
} elseif ( empty( $rss_items ) ) {
_e( 'No event data available.', 'admidio-events' );
return;
}
// Extract Admidio data from RSS items.
$admidio_data = $this->extract_admidio_data( $rss_items );
// Sort items for date.
ksort( $admidio_data );
$items_counter = 1;
echo "
";
// After widget (defined by themes).
echo $after_widget;
}
/**
* Get rss feed data.
*
* @since 0.3.1
*
* @param string $rss_feed_url
* @param int $items_limit - Maximum number of items that are read from the rss url (0 = unlimited number).
*
* @return mixed Array with rss feed items (reversely sorted by publishing date) or empty array. False if error occurred.
*/
function get_rss_feed_data( $rss_feed_url, $items_limit ) {
/**
* Get RSS Feed.
* @see https://codex.wordpress.org/Function_Reference/fetch_feed/
*/
include_once( ABSPATH . WPINC . '/feed.php' );
// Change the feed cache lifetime from default value (12 hours) to 1 hour.
add_filter( 'wp_feed_cache_transient_lifetime', array( $this, 'set_feed_cache_lifetime' ) );
// Get a SimplePie feed object from the specified feed url.
$rss = fetch_feed( $rss_feed_url );
// Change the feed cache lifetime back to default value.
remove_filter( 'wp_feed_cache_transient_lifetime', array( $this, 'set_feed_cache_lifetime' ) );
if ( ! is_wp_error( $rss ) ) { // Checks that the object was created correctly
// Figure out how many total items there are, but limit it to $items_limit.
$maxitems = $rss->get_item_quantity( $items_limit );
if ( $maxitems>0) {
// Build an array of all the items, starting with element 0 (first element).
return $rss->get_items( 0, $maxitems );
}
return array();
}
return false;
}
/**
* Set feed cache lifetime
*
* @see https://codex.wordpress.org/Plugin_API/Filter_Reference/wp_feed_cache_transient_lifetime/
* @since 0.3.1
*
* @param int $seconds - Time in seconds between cache recreation.
* @return int New time.
*/
public function set_feed_cache_lifetime( $seconds ) {
return 3600;
}
/**
* Extract Admidio data from rss items.
*
* @since 0.3.1
*
* @param array $rss_items
* @return array Extracted Admidio data.
*/
function extract_admidio_data( $rss_items ) {
$admidio_data = array();
foreach ( $rss_items as $item ) {
// Remove date(s) from RSS title.
$admidio_title = $item->get_title();
preg_match( '/^(\d{2}\.\d{2}\.\d{4})( - )?(\d{2}\.\d{2}\.\d{4})?\s/', $admidio_title, $matches );
if ( (count( $matches )==2) or (count( $matches )==4) ) {
$admidio_title = str_replace( $matches[0],'',$admidio_title );
}
// Get description, remove link at the end and decode HTML entities back to characters.
$admidio_description_raw = htmlspecialchars_decode( preg_replace('/$/', '', $item->get_description()) );
// Also replace two " " by one, delete any Line Feeds and Tabs.
$admidio_description = str_replace( array('
', "\n", "\t"), array(' ','',''), $admidio_description_raw );
// Remove date and time information from description and keep it.
preg_match( '/(.*) (.*) $/', $admidio_description, $matches );
if ( count( $matches==3 ) ) {
$admidio_date_time_place = $matches[1];
$admidio_description = $matches[2];
}
// Get start date and convert formatting to make it usable for sorting and for pretty display.
$admidio_start_date = substr( $admidio_date_time_place, 0, 10 );
$admidio_start_date_key = date_format( date_create_from_format( 'd.m.Y', $admidio_start_date), 'Y-m-d' );
$admidio_start_date_pretty = date_i18n( get_option( 'date_format' ), strtotime( $admidio_start_date_key ) );
// Get start time for finer sorting.
$admidio_start_time_key = substr( $admidio_date_time_place, 11, 5 );
// Create array for sorting.
$admidio_data[$admidio_start_date_key . ' ' . $admidio_start_time_key] = array( 'title' => $admidio_title, 'start_date' => $admidio_start_date_pretty, 'date_time_place' => $admidio_date_time_place, 'desc' => $admidio_description );
} // foreach ( $rss_items as $item )...
return $admidio_data;
}
} // class Admidio_Events_Widget ...
/**
* Register widget *Admidio_Events_Widget*.
*
* @since 0.3.1
*/
add_action( 'widgets_init', function(){
register_widget( 'Admidio_Events_Widget' );
});
// Load language file.
$plugin_dir = basename( dirname( __FILE__ ) );
load_plugin_textdomain( 'admidio-events', null, $plugin_dir . '/languages/' );
?>