Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class wp_better_weather extends WP_Widget {
- var $image_dir = IMAGE_PATH;
- /** constructor */
- function wp_current_weather() {
- parent::WP_Widget( 'wp_better_weather', $name = 'Current Weather' );
- }
- /**
- * Update the widget settings.
- */
- function update( $new_instance, $old_instance ) {
- $instance = $old_instance;
- $instance['title'] = strip_tags( $new_instance['title'] );
- $instance['woeid'] = trim ( $new_instance['woeid'] );
- $instance['units'] = $new_instance['units']['select_value'];
- $instance['condensed'] = $new_instance['condensed'];
- $instance['location'] = $new_instance['location'];
- $instance['forecast'] = $new_instance['forecast'];
- return $instance;
- }
- /**
- * Displays the widget settings controls on the widget panel.
- * Make use of the get_field_id() and get_field_name() function
- * when creating your form elements. This handles the confusing stuff.
- */
- function form( $instance ) {
- // instance exist? if not set defaults
- if ( $instance ) {
- $title = $instance['title'];
- $woeid = $instance['woeid'];
- $units = $instance['units'];
- $condensed = $instance['condensed'];
- $location = $instance['location'];
- $forecast = $instance['forecast'];
- } else {
- //These are our defaults
- $title = 'Weather';
- $woeid = '';
- $units = 'f';
- $condensed = false;
- $location = true;
- $forecast = true;
- }
- ?>
- <p>
- <label for="<?php echo $this->get_field_id('title'); ?>"><?php echo __( 'Title:' ); ?></label>
- <input id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" class="widefat" />
- </p>
- <p>
- <label for="<?php echo $this->get_field_id( 'woeid' ); ?>"><?php _e('Location Code:'); ?></label>
- <input id="<?php echo $this->get_field_id( 'woeid' ); ?>" name="<?php echo $this->get_field_name( 'woeid' ); ?>" value="<?php echo $woeid; ?>" class="widefat" />
- <span style="font-size: 10px;">(e.g. This could be a zip code; or city, state.)</span>
- </p>
- <p>
- <label for="<?php echo $this->get_field_id( 'units' ); ?>"><?php _e('Units:'); ?></label>
- <select id="<?php echo $this->get_field_id( 'units' ); ?>" name="<?php echo $this->get_field_name( 'units' ); ?>[select_value]" class="widefat">
- <option value="c" <?php if ($units == 'c') echo 'selected'; ?>>Celsius</option>
- <option value="f" <?php if ($units == 'f') echo 'selected'; ?>>Fahrenheit</option>
- </select>
- </p>
- <p>
- <input class="checkbox" type="checkbox" <?php if($condensed == true) echo 'checked'; ?> id="<?php echo $this->get_field_id( 'condensed' ); ?>" name="<?php echo $this->get_field_name( 'condensed' ); ?>" />
- <label for="<?php echo $this->get_field_id( 'condensed' ); ?>"><?php _e('Condensed format?'); ?></label>
- </p>
- <p>
- <input class="checkbox" type="checkbox" <?php if($location == true) echo 'checked'; ?> id="<?php echo $this->get_field_id( 'location' ); ?>" name="<?php echo $this->get_field_name( 'location' ); ?>" />
- <label for="<?php echo $this->get_field_id( 'location' ); ?>"><?php _e('Show the location?'); ?></label>
- </p>
- <p>
- <input class="checkbox" type="checkbox" <?php if($forecast == true) echo 'checked'; ?> id="<?php echo $this->get_field_id( 'forecast' ); ?>" name="<?php echo $this->get_field_name( 'forecast' ); ?>" />
- <label for="<?php echo $this->get_field_id( 'forecast' ); ?>"><?php _e('Display forecast?'); ?></label>
- </p>
- <?php
- }
- /**
- * This is our Widget
- **/
- function widget( $args, $instance ) {
- extract( $args );
- #Our variables from the widget settings
- $title = apply_filters('widget_title', $instance['title'] );
- $woeid = $instance['woeid'];
- $units = $instance['units'];
- $condensed = $instance['condensed'];
- $location = $instance['location'];
- $forecast = $instance['forecast'];
- #Before widget (defined by themes)
- echo $before_widget;
- #Display the widget title if one was input (before and after defined by themes)
- if ( $title )
- echo $before_title . $title . $after_title;
- #Display name from widget settings if one was input
- $this->buildWidget($woeid,$units,$location,$forecast,$condensed);
- #After widget (defined by themes)
- echo $after_widget;
- }
- function getData($woeid,$units) {
- $woeid = str_replace(' ',' ',$woeid);
- //Yahoo! GeoLocation Service
- $locationXML = 'http://query.yahooapis.com/v1/public/yql?q=select * from geo.places where text="'.$woeid.'"&format=xml';
- $location_id = wp_remote_fopen($locationXML);
- $location_data = simplexml_load_string($location_id);
- $loc_id = $location_data->results->place->woeid;
- //Yahoo! Weather API
- $xmlURL = 'http://weather.yahooapis.com/forecastrss?w='.$loc_id.'&u='.$units;
- //We need to cache the data for an hour to eliminate some of our API request limitations.
- $cache = PLUGIN_PATH . '/includes/cache/cache-weather-'.$loc_id.'.xml';
- if(!file_exists($cache) || (time() - filemtime($cache)) > 60*15) {
- //let's grab the XML data
- $data = wp_remote_fopen($xmlURL);
- file_put_contents($cache, $data);
- }
- $output = file_get_contents($cache);
- //load resource into a xmldom
- $xmlData = simplexml_load_string($output);
- //handle yweather: namespace elements.
- $channel_yweather = $xmlData->channel->children("http://xml.weather.yahoo.com/ns/rss/1.0"); //location information
- $item_yweather = $xmlData->channel->item->children("http://xml.weather.yahoo.com/ns/rss/1.0"); //current conditions and forecast
- foreach($channel_yweather as $x => $channel_item) {
- foreach($channel_item->attributes() as $k => $attr) {
- $yw_channel[$x][$k] = $attr;
- }
- }
- $r = -1;
- foreach($item_yweather as $x => $yw_item) {
- foreach($yw_item->attributes() as $k => $attr) {
- if($k == 'day') { $day = $attr; $r++; }
- if($x == 'forecast') { $yw_forecast[$x][$r][$k] = $attr;}
- else { $yw_forecast[$x][$k] = $attr; }
- }
- }
- //define conditions array
- $conditions = array();
- //F and C html
- if($units=='c') {
- $t = '°C';
- } else {
- $t = '°F';
- }
- $conditions['current']['city'] = $yw_channel['location']['city'];
- $conditions['current']['region'] = $yw_channel['location']['region'];
- $conditions['current']['country'] = $yw_channel['location']['country'];
- $conditions['current']['temperature'] = $yw_forecast['condition']['temp'].$t;
- $conditions['current']['conditions'] = $yw_forecast['condition']['text'];
- $conditions['current']['icon'] = $yw_forecast['condition']['code'];
- $conditions['attribution']['link'] = $xmlData->channel->item->link;
- $conditions['current']['test'] = /*$yw_forecast['forecast']*/$locationXML;
- //print_r($conditions['current']['test']);
- $i = 0;
- foreach ($yw_forecast['forecast'] as $day) {
- $conditions['forecast'][$i]['day'] = $day['day'];
- $conditions['forecast'][$i]['date'] = $day['date'];
- $conditions['forecast'][$i]['hi'] = $day['high'];
- $conditions['forecast'][$i]['low'] = $day['low'];
- $conditions['forecast'][$i]['icon'] = $day['code'];
- $conditions['forecast'][$i]['cond'] = $day['text'];
- $i++;
- }
- return $conditions;
- }
- function buildWidget($woeid,$units,$location,$forecast,$condensed) {
- $conditions = $this->getData($woeid,$units);
- $icons = array(
- "0" => array(
- "name"=>"tornado",
- "class"=>"wi-tornado"),
- "1" => array(
- "name"=>"tropical storm",
- "class"=>"wi-day-thunderstorm"),
- "2" => array(
- "name"=>"hurricane",
- "class"=>"wi-day-thunderstorm"),
- "3" => array(
- "name"=>"severe thunderstorms",
- "class"=>"wi-day-thunderstorm"),
- "4" => array(
- "name"=>"thunderstorms",
- "class"=>"wi-day-thunderstorm"),
- "5" => array(
- "name"=>"mixed rain and snow",
- "class"=>"wi-day-rain-mix"),
- "6" => array(
- "name"=>"mixed rain and sleet",
- "class"=>"wi-day-rain-mix"),
- "7" => array(
- "name"=>"mixed snow and sleet",
- "class"=>"wi-day-snow"),
- "8" => array(
- "name"=>"freezing drizzle",
- "class"=>"wi-day-snow"),
- "9" => array(
- "name"=>"drizzle",
- "class"=>"wi-day-sprinkle"),
- "10" => array(
- "name"=>"freezing rain",
- "class"=>"wi-day-sunny"),
- "11" => array(
- "name"=>"showers",
- "class"=>"wi-day-sunny"),
- "12" => array(
- "name"=>"showers",
- "class"=>"wi-day-sunny"),
- "13" => array(
- "name"=>"snow flurries",
- "class"=>"wi-day-sunny"),
- "14" => array(
- "name"=>"light snow showers",
- "class"=>"wi-day-sunny"),
- "15" => array(
- "name"=>"blowing snow",
- "class"=>"wi-day-sunny"),
- "16" => array(
- "name"=>"snow",
- "class"=>"wi-day-sunny"),
- "17" => array(
- "name"=>"hail",
- "class"=>"wi-day-sunny"),
- "18" => array(
- "name"=>"sleet",
- "class"=>"wi-day-sunny"),
- "19" => array(
- "name"=>"dust",
- "class"=>"wi-day-sunny"),
- "20" => array(
- "name"=>"foggy",
- "class"=>"wi-day-sunny"),
- "21" => array(
- "name"=>"haze",
- "class"=>"wi-day-sunny"),
- "22" => array(
- "name"=>"smoky",
- "class"=>"wi-day-sunny"),
- "23" => array(
- "name"=>"blustery",
- "class"=>"wi-day-sunny"),
- "24" => array(
- "name"=>"windy",
- "class"=>"wi-day-sunny"),
- "25" => array(
- "name"=>"cold",
- "class"=>"wi-day-sunny"),
- "26" => array(
- "name"=>"cloudy",
- "class"=>"wi-day-sunny"),
- "27" => array(
- "name"=>"mostly cloudy (night)",
- "class"=>"wi-day-sunny"),
- "28" => array(
- "name"=>"mostly cloudy (day)",
- "class"=>"wi-day-sunny"),
- "29" => array(
- "name"=>"partly cloudy (night)",
- "class"=>"wi-day-sunny"),
- "30" => array(
- "name"=>"partly cloudy (day)",
- "class"=>"wi-day-sunny"),
- "31" => array(
- "name"=>"clear (night)",
- "class"=>"wi-day-sunny"),
- "32" => array(
- "name"=>"sunny",
- "class"=>"wi-day-sunny"),
- "33" => array(
- "name"=>"fair (night)",
- "class"=>"wi-day-sunny"),
- "34" => array(
- "name"=>"fair (day)",
- "class"=>"wi-day-sunny"),
- "35" => array(
- "name"=>"mixed rain and hail",
- "class"=>"wi-day-sunny"),
- "36" => array(
- "name"=>"hot",
- "class"=>"wi-day-sunny"),
- "37" => array(
- "name"=>"isolated thunderstorms",
- "class"=>"wi-day-sunny"),
- "38" => array(
- "name"=>"scattered thunderstorms",
- "class"=>"wi-day-sunny"),
- "39" => array(
- "name"=>"scattered thunderstorms",
- "class"=>"wi-day-sunny"),
- "40" => array(
- "name"=>"scattered showers",
- "class"=>"wi-day-sunny"),
- "41" => array(
- "name"=>"heavy snow",
- "class"=>"wi-day-sunny"),
- "42" => array(
- "name"=>"scattered snow showers",
- "class"=>"wi-day-sunny"),
- "43" => array(
- "name"=>"heavy snow",
- "class"=>"wi-day-sunny"),
- "44" => array(
- "name"=>"partly cloudy",
- "class"=>"wi-day-sunny"),
- "45" => array(
- "name"=>"thundershowers",
- "class"=>"wi-day-sunny"),
- "46" => array(
- "name"=>"snow showers",
- "class"=>"wi-day-sunny"),
- "47" => array(
- "name"=>"isolated thundershowers",
- "class"=>"wi-day-sunny")
- );
- if($forecast && !$condensed)
- {
- $icon1 = $conditions['forecast'][0]['icon'];
- $icon2 = $conditions['forecast'][1]['icon'];
- $class1 = '';
- $class2 = '';
- $i1 = -1;
- $i2 = -1;
- $state = $conditions['current']['region'][0];
- foreach($icons as $icon){
- $i1++;
- if($icon1==$i1){
- $class1=$icon['class'];
- }
- }
- foreach($icons as $icon){
- $i2++;
- if($icon2==$i2){
- $class2=$icon['class'];
- }
- }
- echo '
- <div class="weather">
- <span class="state">'.$state.'</span>
- <span class="day">'.$conditions['forecast'][0]['day'].'</span>
- <span class="date">'.$conditions['forecast'][0]['date'].'</span>
- <span class="temp-hi">Hi: '.$conditions['forecast'][0]['hi'].'</span>
- <span class="temp-lo">Lo: '.$conditions['forecast'][0]['low'].'</span>
- <i class="'.$class1.' icon"></i>
- <span class="cond">'.$conditions['forecast'][0]['cond'].'</span>
- </div>
- ';
- //print_r($conditions);
- }
- }
- } // class wp_current_weather
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement