Skip to content

Commit

Permalink
Merge pull request #47 from katzwebservices/develop
Browse files Browse the repository at this point in the history
Version 2.1.2
  • Loading branch information
zackkatz authored Jun 30, 2016
2 parents 5664828 + 5e8063b commit a23284f
Show file tree
Hide file tree
Showing 9 changed files with 141 additions and 32 deletions.
20 changes: 20 additions & 0 deletions inc/class-KWS-wunderground.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,22 @@ class KWS_Wunderground {

var $alerts;

/**
* @since 2.1.2
* @var null
*/
var $error = null;

function __construct( Wunderground_Request $request ) {

$results = $request->get_results();

if( !empty( $results->error ) ) {
// TODO: Handle properly
Wunderground_Plugin::log_error( 'Error loading results KWS_Wunderground', $results->error );

$this->error = $results->error;

return NULL;
}

Expand All @@ -30,4 +39,15 @@ function __construct( Wunderground_Request $request ) {
Wunderground_Plugin::log_debug( 'Wunderground_Forecast', $this );
}

/**
* Return error, if any.
*
* @since 2.1.2
*
* @return null|string NULL if no error. Error string if error.
*/
public function get_error() {
return $this->error;
}

}
2 changes: 1 addition & 1 deletion inc/class-ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function autocomplete() {
'h' => 0, // No hurricanes, please.
'c' => $country,
'type' => 'city',
), 'http://autocomplete.wunderground.com/aq' );
), 'https://autocomplete.wunderground.com/aq' );

$response = Wunderground_Request::request( $url );

Expand Down
2 changes: 1 addition & 1 deletion inc/class-display.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function print_scripts( $force = false ) {
wp_enqueue_script( 'wunderground-widget', plugins_url( 'assets/js/widget'.$min.'.js', Wunderground_Plugin::$file ), array('jquery-ui-autocomplete'), Wunderground_Plugin::version );

wp_localize_script( 'wunderground-widget', 'WuWidget', array(
'apiKey' => '3ffab52910ec1a0e',
'apiKey' => esc_attr( Wunderground_Plugin::$api_key ),
'_wpnonce' => wp_create_nonce('wunderground-aq'),
'ajaxurl' => admin_url( 'admin-ajax.php' ),
'is_admin' => is_admin(),
Expand Down
56 changes: 34 additions & 22 deletions inc/class-request.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@

class Wunderground_Request {

/**
* Your Wunderground.com API key
* @var string
*/
private $apiKey = '3ffab52910ec1a0e';

/**
* What features should be fetched by the request?
*
Expand Down Expand Up @@ -174,24 +168,35 @@ private function build_url() {

$location = $this->location;

$include_pws = false;

// We've got a PWS!
// Match both pws:KCASANFR70 and KCASANFR70 formats
// I716, MBGLA2, M41101, MRNDA2, MBGLA2
if( preg_match( '/(pws\:)?([A-Z]{1,11}[0-9]{1,11})/', $location, $matches ) ) {
$location = isset( $matches[2] ) ? $matches[2] : $location;
$location = '/q/pws:'.urlencode($location);
$include_pws = true;
}

/*
/**
* Include PWS stations in the results?
* @since TODO
* @param int `0` for no, `1` for yes. Default: `0`
*/
$pws = sprintf( 'pws:%d', intval( apply_filters( 'wunderground_include_pws', $include_pws ) ) );

/*
http://www.wunderground.com/weather-forecast/FR/Paris.html
http://www.wunderground.com/q/FR/Paris.html
http://www.wunderground.com/personal-weather-station/dashboard?ID=I75003PA1*/
http://www.wunderground.com/personal-weather-station/dashboard?ID=I75003PA1
*/

// If the location is a link, we don't need to turn it...wait for it...into a link.
$location_path = preg_match( '/\/q\//ism', $location ) ? $location : '/q/'.rawurlencode($location);

// Combine into one URL
$url = sprintf('%s/%s/v:2.0/%s/%s/%s%s.json', $this->apiUrl, $this->apiKey, $language, $units, $features, $location_path );
$url = sprintf('%s/%s/v:2.0/%s/%s/%s/%s%s.json', $this->apiUrl, Wunderground_Plugin::$api_key, $language, $units, $pws, $features, $location_path );

return $url;
}
Expand All @@ -213,7 +218,7 @@ static function request($url, $cache = true) {
// Generate a cache key based on the result. Only get the first 44 characters because of
// the transient key length limit.
$cache_key = substr( 'wu_'.sha1($url) , 0, 44 );

$response = get_transient( $cache_key );

// If there's no cached result or caching is disabled
Expand All @@ -229,21 +234,28 @@ static function request($url, $cache = true) {

$request = wp_remote_request( $url , $atts );

$response = wp_remote_retrieve_body( $request );
if( is_wp_error( $request ) ) {
$response = false;

} else {

/**
* Modify the number of seconds to cache the request for.
*
* Default: cache the request for one hour, since we're dealing with changing conditions
*
* @var int
*/
$cache_time = apply_filters( 'wunderground_cache_time', HOUR_IN_SECONDS );
$response = wp_remote_retrieve_body( $request );

/**
* Modify the number of seconds to cache the request for.
*
* Default: cache the request for one hour, since we're dealing with changing conditions
*
* @var int
*/
$cache_time = apply_filters( 'wunderground_cache_time', HOUR_IN_SECONDS );

// Backward compatible with 1.x
$cache_time = apply_filters( 'wp_wunderground_forecast_cache', $cache_time );
// Backward compatible with 1.x
$cache_time = apply_filters( 'wp_wunderground_forecast_cache', $cache_time );

set_transient( $cache_key, $response, (int)$cache_time );
set_transient( $cache_key, $response, (int)$cache_time );

}
}

return $response;
Expand Down
13 changes: 11 additions & 2 deletions inc/class-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,12 @@ function strings() {

}

/**
* @param string $template Template slug based on value passed by `wunderground_render_template` filter
* @param array $data
*/
function render( $template = NULL, $data = array() ) {

// The translation text
$data['strings'] = $this->strings();

Expand All @@ -97,7 +101,6 @@ function render( $template = NULL, $data = array() ) {

// Map the keys so that they are consistent instead of having some
// using key => key and others using index => key
$showdata = array();
foreach ( (array)$data['showdata'] as $key => $value ) {
$data['showdata'][$value] = $value;
}
Expand Down Expand Up @@ -134,6 +137,12 @@ function render( $template = NULL, $data = array() ) {
set_transient( $cache_key, $output, ( $cache_time * 2 ) );
}

/**
* Modify the HTML output of the forecast
* @param string $output HTML of the forecast
* @param string $template Template slug based on value passed by `wunderground_render_template` filter
* @param array $data Template data array, with keys `strings`, `showdata`, `subdomain`, `logo`, `user_icon_url`, `language`
*/
$output = apply_filters( 'wp_wunderground_forecast', $output, $template, $data );

echo $output;
Expand Down
2 changes: 1 addition & 1 deletion inc/class-widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function __construct() {

$control_options = array( 'width' => 450 ); // Min-width of widgets config with expanded sidebar

parent::__construct( 'wunderground', __('Wunderground', 'wunderground'), $widget_ops, $control_options );
parent::__construct( false, __('Wunderground', 'wunderground'), $widget_ops, $control_options );
}

/**
Expand Down
2 changes: 1 addition & 1 deletion inc/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ function wunderground_get_autocomplete_country_code() {
/**
* Get the date format for the output.
*
* Backward compatibile with 1.x by converting %%weekday%%, %%day%%, %%month%% and %%year%% into PHP date formats. Also supports converting `date('d/m/Y')` to `d/m/Y`
* Backward compatible with 1.x by converting %%weekday%%, %%day%%, %%month%% and %%year%% into PHP date formats. Also supports converting `date('d/m/Y')` to `d/m/Y`
*
* @link http://codex.wordpress.org/Formatting_Date_and_Time Learn more about formatting datetime
* @filter wunderground_date_format Filter the date format sitewide.
Expand Down
31 changes: 30 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
=== Weather Underground ===
Tags: weather, weather.com, wunderground, weather underground, weatherbug, forecast, Yahoo! Weather, wp-weather, wp weather, local weather, weather man, weather widget, cool weather, accuweather, get weather, wordpress weather
Requires at least: 3.6
Tested up to: 4.3
Tested up to: 4.5.2
Stable tag: trunk
Contributors: katzwebdesign, katzwebservices
Donate link: https://gravityview.co
Expand Down Expand Up @@ -64,6 +64,29 @@ If your location isn't working any more, follow the steps below:
* Use that as your location in the shortcode, like this: `[wunderground location="zmw:00000.4.17340" /]`
* That should work!

= How do I use my own API key? =
Weather Underground has generously donated free API access to users of this plugin.

If you prefer, you can define your own API key in your installation's `wp-config.php` file by setting a `WUNDERGROUND_API_KEY` constant, like so:

<pre>
define( 'WUNDERGROUND_API_KEY', 'myapikey' );
</pre>

You can also override the API key using the `wunderground_api_key` filter, like so:

<pre>
add_filter( 'wunderground_api_key', 'my_custom_wunderground_api_key' );

/**
* Use my own API key!
* @return string My API key
*/
function wunderground_api_key( $old_api_key = '' ) {
return 'myapikey';
}
</pre>

= How do I use my own icons? =

If you want to use your own icons, you would add a filter to the bottom of your theme's <code>functions.php</code> file. See a [list of icons you should have available](http://www.wunderground.com/weather/api/d/docs?d=resources/icon-sets). Here's sample code:
Expand Down Expand Up @@ -97,6 +120,12 @@ Weather Underground has been very gracious and has provided the plugin with free

== Changelog ==

= 2.1.2 on June 30, 2016 =
* Added: Ability to override API key using the `wunderground_api_key` filter or the `WUNDERGROUND_API_KEY` constant
* Fixed: Location autocomplete issue on HTTPS websites
* Fixed: Don't cache responses if they result in errors (good idea, eh?)
* Added: `wunderground_include_pws` filter to toggle whether to include Personal Weather Stations as data sources (Default: false, unless the location requested is specifically a PWS station)

= 2.1.1 on August 25, 2015 =
* Fixed: WordPress 4.3 compatibility
* Added: Display any errors while fetching forecasts to administrators
Expand Down
45 changes: 42 additions & 3 deletions wunderground.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,35 @@ class Wunderground_Plugin {
*/
const version = '2.1.1';

var $logger;
var $is_debug = false;

/**
* @var string The full path and filename of the main plugin file
*/
static $file;

/**
* @var string Filesystem path to main plugin file, with trailing slash
*/
static $dir_path;

/**
* Filter the Weather Underground API key used by the plugin
* Modify using the `wunderground_api_key` filter
* @since 2.1.2
*/
static $api_key = '3ffab52910ec1a0e';

function __construct() {

self::$file = __FILE__;

self::$dir_path = plugin_dir_path( __FILE__ );

/**
* Filter the Weather Underground API key used by the plugin
* @since 2.1.2
*/
self::$api_key = $this->get_api_key();

// Fire AJAX requests immediately
include_once self::$dir_path.'inc/class-ajax.php';

Expand All @@ -45,6 +61,29 @@ function __construct() {
add_action( 'wunderground_log_debug', array( 'Wunderground_Plugin', 'log_debug'), 10, 2 );
}

/**
* Get the Weather Underground API key used by the plugin
*
* You can define your own Weather Underground API key using the `WUNDERGROUND_API_KEY` constant in wp-config.php,
* or you can filter the key using the `wunderground_api_key` filter.
*
* @since 2.1.2
*
* @return string The Weather Underground API key. Default: the plugin key ("3ffab52910ec1a0e")
*/
private function get_api_key() {

$api_key = self::$api_key;

if( defined( 'WUNDERGROUND_API_KEY' ) ) {
$api_key = WUNDERGROUND_API_KEY;
}

$api_key = apply_filters( 'wunderground_api_key', $api_key );

return $api_key;
}

function init() {

// Add translation support
Expand Down

0 comments on commit a23284f

Please sign in to comment.