Permalink
Browse files

Reimplementing widget class.

  • Loading branch information...
1 parent e189bdb commit 5d17d2eac3e7d4661ee32f78c1ec0ab8e3a960aa Sabrina Leandro committed Jun 26, 2012
View
@@ -2,8 +2,8 @@
Contributors: saleandro, coox
Tags: songkick, concerts, events, festivals, widget
Requires at least: 2.8.2, PHP 5 or higher
-Tested up to: 3.3.1
-Stable tag: 0.9.3.2
+Tested up to: 3.4
+Stable tag: 0.9.4
This plugin lets you display events for a Songkick user, artist, venue, or metro area on your WordPress blog, as a widget or shortcode.
@@ -28,25 +28,25 @@ Events can be displayed by adding the Songkick widget to your template, or by ad
= Requirements =
-* You'll need a Songkick API key. Apply for a key here: [Songkick API docs](http://www.songkick.com/developer)
+* This plugin uses a non-commercial Songkick API key. If you have a commercial website, you’ll need your own Songkick API key. Please read through [Songkick’s API terms of use](http://www.songkick.com/developer/api-terms-of-use). Apply for a key here: [Songkick API docs](http://www.songkick.com/developer)
* This plugin requires PHP 5
= Settings =
-Go to the Settings page to configure the plugin.
+Go to the Settings page to configure default options for the plugin. You can also specify your settings under Plugins/Widget or via shortcode options.
-* For a user, simply put your username in the admin interface.
+* For a user, simply put your username in the admin interface.
* For an artist, you should use the artist’s Songkick id, as shown in the url for your artist page. For example, the url "http://www.songkick.com/artists/123-your-name" has the id "123".
* The same goes for metro areas: "http://www.songkick.com/metro_areas/123-city-name" has the id "123".
* And venues: "http://www.songkick.com/venues/123-venue-name" has the id "123".
= Widget =
-After configuring the plugin, go to the admin Widgets page and simply drag the widget into a sidebar.
+Go to the admin Widgets page and simply drag the widget into a sidebar and configure it.
= Shortcode =
-After configuring the plugin, add the shortcode [songkick_concerts_and_festivals] in the content of any blog post.
+Add the shortcode [songkick_concerts_and_festivals] in the content of any blog post.
When using a shortcode, you can set which artist, venue, metro area, or user you want to display events for, allowing you to show events for different entities:
@@ -70,6 +70,7 @@ You can call the shortcode method directly in your PHP code:
= Blogs using this plugin =
+* [Blur](http://www.blur.co.uk/info/live-dates/)
* [Big Boi](http://bigboi.com/tour/)
* [OK Go](http://www.okgo.net/shows/)
* [Haircut Records](http://haircutrecords.co.uk/site/)
@@ -87,7 +88,8 @@ The code lives on [Github](http://github.com/saleandro/songkick-wp-plugin). To s
1. Upload the directory "songkick_concerts_and_festivals" to the "/wp-content/plugins/" directory
1. Activate the plugin through the "Plugins" menu
-1. Go to the Settings page for Songkick and set your username/artist/venue/metro area ID and API key. Apply for a key here: http://www.songkick.com/developer
+1. Go to the Settings page for Songkick or the Widgets page and set your username/artist/venue/metro area ID.
+1. Please read through [Songkick’s API terms of use](http://www.songkick.com/developer/api-terms-of-use). If you have a commercial website, please apply for your own API key here: http://www.songkick.com/developer and configure it on the Settings page.
1. Add the widget to a sidebar or the shortcode anywhere in your blog.
== Screenshots ==
@@ -125,4 +127,9 @@ See Songkick’s admin settings for details.
* Support for displaying venue events.
* Markup with support for events as defined by [Schema.org](http://www.schema.org/)
-* Paginated list of events for shortcode option.
+* Paginated list of events for shortcode option.
+
+= 0.9.4 -
+
+* Reimplementation of the Widget class. Allows for multilple Widget instances.
+* Remove requirement for an API key for *non-commercial* websites.
@@ -15,9 +15,10 @@ function profile_url() {
return "http://www.songkick.com/artists/$this->id";
}
- protected function url($page, $per_page){
+ protected function url($page, $per_page) {
$method = $this->gigography ? 'gigography' : 'calendar';
return "$this->apiurl/artists/$this->id/$method.json?apikey=$this->apikey&per_page=$per_page&page=$page";
}
}
+
?>
View
@@ -1,6 +1,6 @@
div.songkick-events .profile-title, .pagination {
- margin-top: 8px;
- margin-bottom: 10px;
+ margin-top: 8px;
+ margin-bottom: 10px;
float: right;
}
@@ -14,16 +14,16 @@ div.songkick-events .powered-by img {
}
div.songkick-events .event-name {
- display:block;
- margin-left:44px;
+ display: block;
+ margin-left: 44px;
}
div.songkick-events ul {
- border-bottom:1px solid #CCCCCC;
- margin-bottom:5px;
+ border-bottom: 1px solid #CCCCCC;
+ margin-bottom: 5px;
margin-top: 10px;
margin-left: 0px;
- list-style:none outside none;
+ list-style: none outside none;
}
div.songkick-events li {
@@ -3,19 +3,18 @@
/*
Plugin Name: Songkick Concerts and Festivals
Plugin URI: http://github.com/saleandro/songkick-wp-plugin
-Description: Plugin to show concerts based on your Songkick profile. It can display upcoming events for a user, an artist, venue, or metro area/location.
+Description: Plugin to show concerts based on your Songkick profile. It can display upcoming events for a user, an artist, venue, or metro area/location.
It can also display past events for users and artists. For a user, simply put your username in the admin interface. For an artist, you should use the artist's Songkick id, as shown in the url for your artist page.
For example, the url "http://www.songkick.com/artists/123-your-name" has the id "123". The same goes for metro areas or venues: "http://www.songkick.com/venues/123-venue-name" and "http://www.songkick.com/metro_areas/123-city-name" both have the id "123".
-You can also specify different user, artist, venue, or metro area ids when using the shortcode function.
-Version: 0.9.3.2
+Version: 0.9.4
Author: Sabrina Leandro
Author URI: http://github.com/saleandro
License: GPL3
*/
/*
- Copyright 2010 Sabrina Leandro (saleandro@yahoo.com)
+ Copyright 2012 Sabrina Leandro (saleandro@yahoo.com)
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
@@ -37,6 +36,9 @@
// define('WP_DEBUG', true);
// @ini_set('display_errors','On');
+defined('ABSPATH') or die("Cannot access pages directly.");
+defined("DS") or define("DS", DIRECTORY_SEPARATOR);
+
if (!class_exists('WP_Http'))
include_once(ABSPATH . WPINC . '/class-http.php');
@@ -48,6 +50,7 @@
require_once dirname(__FILE__) . '/songkick_presentable_events.php';
require_once dirname(__FILE__) . '/songkick_settings.php';
+require_once dirname(__FILE__) . '/songkick_widget.php';
/**
* Global Initialization of the Songkick Plugin
@@ -63,7 +66,7 @@ function songkick_concerts_and_festivals_shortcode_handler($options = null) {
try {
wp_enqueue_style('songkick_concerts', '/wp-content/plugins/songkick-concerts-and-festivals/songkick_concerts.css') ;
- $default_options = get_option(SONGKICK_OPTIONS);
+ $default_options = get_option(SONGKICK_OPTIONS);
$default_options['logo'] = $default_options['shortcode_logo'];
$default_options['date_color'] = $default_options['shortcode_date_color'];
$default_options['number_of_events'] = $default_options['shortcode_number_of_events'];
@@ -73,7 +76,7 @@ function songkick_concerts_and_festivals_shortcode_handler($options = null) {
$options = $default_options;
}
- if (!isset($options['show_pagination'])) $options['show_pagination'] = false;
+ if (!isset($options['show_pagination'])) $options['show_pagination'] = false;
if ($options['show_pagination'] && isset($_GET['skp']))
$options['page'] = $_GET['skp'];
@@ -83,63 +86,16 @@ function songkick_concerts_and_festivals_shortcode_handler($options = null) {
$str .= '</div>';
return $str;
} catch (Exception $e) {
- $msg = 'Error on '.get_bloginfo('url').' while trying to display Songkick Concerts plugin: '. $e->getMessage();
+ $msg = 'Error on ' . get_bloginfo('url') . ' while trying to display Songkick Concerts plugin: ' . $e->getMessage();
error_log($msg, 0);
+ return '';
}
}
-/**
- * Global Initialization of the Songkick Sidebar Widget
- */
-function songkick_widget_init() {
- if (!function_exists('register_sidebar_widget'))
- return;
-
- wp_enqueue_style('songkick_concerts', '/wp-content/plugins/songkick-concerts-and-festivals/songkick_concerts.css') ;
-
- function songkick_widget($args) {
- try {
- extract($args);
-
- $options = get_option(SONGKICK_OPTIONS);
- $hide_if_empty = $options['hide_if_empty'];
- $options['show_pagination'] = false;
-
- $sk = new SongkickPresentableEvents($options);
-
- if ($hide_if_empty && $sk->no_events()) {
- echo '';
- } else {
- $options = get_option(SONGKICK_OPTIONS);
- $title = $options['title'];
- if (!$title || $title == '') {
- $title = __('Concerts', SONGKICK_TEXT_DOMAIN);
- }
- $title = htmlentities($title, ENT_QUOTES, SONGKICK_I18N_ENCODING);
-
- echo $before_widget;
- echo '<div class="songkick-events">';
- echo $before_title . $title . $after_title;
- echo $sk->to_html();
- echo '</div>';
- echo $after_widget;
- }
- } catch (Exception $e) {
- $msg = 'Error on '.get_bloginfo('url').' while trying to display Songkick Concerts plugin: '. $e->getMessage();
- error_log($msg, 0);
- }
- }
-
- register_sidebar_widget(array('Songkick Concerts and Festivals', 'widgets'), 'songkick_widget');
- register_widget_control(array('Songkick Concerts and Festivals', 'widgets'), 'songkick_widget_settings');
-}
-
add_action('admin_menu', 'songkick_admin_menu');
function songkick_admin_menu() {
add_options_page('Songkick Concerts and Festivals', 'Songkick', 'administrator', 'songkick-concerts-and-festivals', 'songkick_admin_settings');
}
-
add_shortcode("songkick_concerts_and_festivals", "songkick_concerts_and_festivals_shortcode_handler");
-add_action('widgets_init', 'songkick_widget_init');
?>
View
@@ -1,25 +1,31 @@
<?php
+class InvalidApiKeyException extends Exception { }
+
class SongkickEvents {
public $apikey;
public $upcoming_events = array();
function SongkickEvents($apikey) {
- $this->apikey = trim($apikey);
+ $this->apikey = ($apikey && $apikey != '') ? trim($apikey) : 'io09K9l3ebJxmxe2';
$this->apiurl = 'http://api.songkick.com/api/3.0';
}
- function get_upcoming_events($page=1, $per_page=10) {
+ function get_events($page=1, $per_page=10) {
$url = $this->url($page, $per_page);
$cached_results = $this->get_cached_results($url);
if ($this->cache_expired($cached_results)) {
- $cached_results = $this->get_uncached_upcoming_events($url);
+ $cached_results = $this->get_uncached_events($url);
$cached_results['timestamp'] = time();
$this->set_cached_results($url, $cached_results);
}
return $cached_results;
}
+ function test_api_call() {
+ $this->fetch("$this->apiurl/metro_areas/24426/calendar.xml?apikey=$this->apikey&page=1&per_page=1");
+ }
+
protected function get_cached_results($key) {
$all_cache = get_option(SONGKICK_CACHE);
if (isset($all_cache[$key]) && $all_cache[$key]) {
@@ -29,7 +35,7 @@ protected function get_cached_results($key) {
}
}
- protected function get_uncached_upcoming_events($url) {
+ protected function get_uncached_events($url) {
$response = $this->fetch($url);
return $this->events_from_json($response);
}
@@ -51,10 +57,13 @@ protected function cache_expired($cached_results) {
protected function fetch($url) {
$http = new WP_Http;
$response = $http->request($url);
+
if (is_wp_error($response)) {
throw new Exception('WP_Http/WP_Error message: '.$response->get_error_message());
} elseif (!is_array($response)) {
throw new Exception('WP_Http/Invalid response');
+ } elseif ($response['response']['code'] == 403) {
+ throw new InvalidApiKeyException();
} elseif ($response['response']['code'] != 200) {
throw new Exception('WP_Http error response: '.$response['response']['code']);
}
@@ -14,8 +14,9 @@ function profile_url() {
return "http://www.songkick.com/metro_areas/$this->id";
}
- protected function url($page, $per_page){
+ protected function url($page, $per_page) {
return "$this->apiurl/metro_areas/$this->id/calendar.json?apikey=$this->apikey&per_page=$per_page&page=$page";
}
}
+
?>
@@ -21,12 +21,15 @@ function SongkickPresentableEvents($options) {
if (!isset($options['gigography'])) $options['gigography'] = false;
$gigography = $options['gigography'];
- $this->number_of_events = $options['number_of_events'];
+ $this->number_of_events = is_numeric($options['number_of_events']) ? $options['number_of_events'] : 10;
if (!isset($options['show_pagination'])) $options['show_pagination'] = false;
$this->show_pagination = $options['show_pagination'];
if (!isset($options['page'])) $options['page'] = 1;
$this->page = intval($options['page']);
+ if (empty($songkick_id)) {
+ throw new Exception("Blank songkick id");
+ }
switch ($songkick_id_type) {
case 'user':
$this->songkick_events = new SongkickUserEvents($apikey, $songkick_id, $attendance, $gigography);
@@ -44,7 +47,7 @@ function SongkickPresentableEvents($options) {
throw new Exception("Unknown songkick id type: $songkick_id_type");
}
- $results = $this->songkick_events->get_upcoming_events($this->page, $this->number_of_events);
+ $results = $this->songkick_events->get_events($this->page, $this->number_of_events);
$this->events = $results['events'];
$this->total = $results['total'];
$this->date_color = $options['date_color'];
@@ -105,7 +108,7 @@ function to_html() {
$str .= $this->powered_by_songkick($this->logo);
return $str;
}
-
+
function no_events() {
return empty($this->events);
}
Oops, something went wrong.

0 comments on commit 5d17d2e

Please sign in to comment.