Admidio.
* Version: 1.0.0
* Author: fiwad
* Author URI: http://profiles.wordpress.org/fiwad/
* Text Domain: admidio-events
* Domain Path: /languages
*
* Module: admidio-events.php
* Description: Widget management.
*
* Copyright (C) 2014 fiwad
*
* 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
*
*/
class Admidio_Events_Widget extends WP_Widget {
/**
* Regular expressions for extracting event date information.
*
* @since 0.3.8
*/
private $date_formats;
/**
* 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.
)
);
// Init private variable.
$this->date_formats = array (
'd.m.Y' => '/^(\d{2}\.\d{2}\.\d{4})( - )?(\d{2}\.\d{2}\.\d{4})?\s/',
'd.m.' => '/^(\d{2}\.\d{2}\.)( - )?(\d{2}\.\d{2}\.)?\s/',
'Y-m-d' => '/^(\d{4}-\d{2}-\d{2})( - )?(\d{4}-\d{2}-\d{2})?\s/',
);
// Register style sheet and scripts.
add_action( 'wp_enqueue_scripts', array( $this, 'register_frontend_styles_and_scripts' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'register_admin_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_frontend_styles_and_scripts() {
wp_enqueue_style( 'admidio-events', plugins_url( 'css/admidio-events.css', __FILE__ ), array(), false );
wp_enqueue_script( 'admidio-events-js', plugins_url( 'js/admidio-events.js', __FILE__ ), array( 'jquery' ), false, true );
}
/**
* Register color picker.
*
* @see http://make.wordpress.org/core/2012/11/30/new-color-picker-in-wp-3-5/
*
* @since 0.3.7
*/
public function register_admin_styles_and_scripts( $hook ) {
// We'll need the color picker only for widgets admin screen.
if ( 'widgets.php' !== $hook ) {
return;
}
wp_enqueue_style( 'wp-color-picker' );
wp_enqueue_script( 'underscore' );
wp_enqueue_script( 'admidio-events-admin-js', plugins_url( 'js/admidio-events-admin.js', __FILE__ ), array( 'wp-color-picker' ), false, 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.
$date_formats_temp = array_keys( $this->date_formats ); // temporary variable necessary for PHP versions 5.3 and older
$defaults = array(
'title' => __( 'Upcoming Events', 'admidio-events' ),
'rss_feed' => '',
'date_format' => $date_formats_temp[0], // PHP 5.4 and higher: array_keys( $this->date_formats )[0],
'no_of_items' => '5',
'show_date' => '0',
'date_color' => '#888888',
'start_expanded' => '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' );
} elseif ( empty( $rss_items ) ) {
_e( 'No event data available.', 'admidio-events' );
} else {
// Extract Admidio data from RSS items.
$admidio_data = $this->extract_admidio_data( $rss_items, $date_format );
// 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' ) );
// The SimplePie feed object has an auto-detection built in. So when a wrong url is given,
// it tries to find a suitable one. We cannot use this feature here because we have to ensure
// correct data content.
if ( strcasecmp( $rss_feed_url,$rss->feed_url ) != 0 ) {
return false;
}
// Checks that the object was created correctly
if ( ! is_wp_error( $rss ) ) {
// 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 60;
}
/**
* Extract Admidio data from rss items.
*
* @since 0.3.1
*
* @param array $rss_items
* @param string $date_format
* @return array Extracted Admidio data.
*/
function extract_admidio_data( $rss_items, $date_format ) {
$admidio_data = array();
foreach ( $rss_items as $item ) {
// Check for correct date format setting.
$admidio_title = $item->get_title();
preg_match( $this->date_formats[ $date_format ], $admidio_title, $matches );
if ( ( ( count( $matches ) === 2 ) or ( count( $matches ) === 4 ) ) and ( date_create_from_format( $date_format, $matches[1] ) !== FALSE ) ) {
// Remove date(s) from title.
$admidio_title = str_replace( $matches[0],'',$admidio_title );
// Convert formatting to make it usable for sorting and for pretty display.
$admidio_start_date_key = date_format( date_create_from_format( $date_format, $matches[1]), 'Y-m-d' );
$admidio_start_date_pretty = date_i18n( get_option( 'date_format' ), strtotime( $admidio_start_date_key ) );
// Get description, decode HTML entities back to characters, remove any number of break tags directly followed by link tag at the end.
$admidio_description_raw = preg_replace( '/( )+$/', '', htmlspecialchars_decode( $item->get_description(), ENT_QUOTES ) );
// Replace two " " by one and delete any Line Feeds and Tabs. Strip tags except for break tags.
$admidio_description = strip_tags( str_replace( array('