Advertisement
developerjustin

Untitled

Feb 28th, 2014
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 12.45 KB | None | 0 0
  1. <?php
  2. class wp_better_weather extends WP_Widget {
  3.    
  4.     var $image_dir   = IMAGE_PATH;
  5.    
  6.     /** constructor */
  7.     function wp_current_weather() {
  8.         parent::WP_Widget( 'wp_better_weather', $name = 'Current Weather' );
  9.     }
  10.  
  11.     /**
  12.     * Update the widget settings.
  13.     */
  14.     function update( $new_instance, $old_instance ) {
  15.         $instance = $old_instance;
  16.  
  17.         $instance['title']      = strip_tags( $new_instance['title'] );
  18.         $instance['woeid']      = trim ( $new_instance['woeid'] );
  19.         $instance['units']      = $new_instance['units']['select_value'];
  20.         $instance['condensed']  = $new_instance['condensed'];
  21.         $instance['location']   = $new_instance['location'];
  22.         $instance['forecast']   = $new_instance['forecast'];
  23.        
  24.         return $instance;
  25.     }
  26.  
  27.     /**
  28.      * Displays the widget settings controls on the widget panel.
  29.      * Make use of the get_field_id() and get_field_name() function
  30.      * when creating your form elements. This handles the confusing stuff.
  31.      */
  32.     function form( $instance ) {
  33.    
  34.         // instance exist? if not set defaults
  35.             if ( $instance ) {
  36.                 $title      = $instance['title'];
  37.                 $woeid      = $instance['woeid'];
  38.                 $units      = $instance['units'];
  39.                 $condensed  = $instance['condensed'];
  40.                 $location   = $instance['location'];
  41.                 $forecast   = $instance['forecast'];
  42.             } else {
  43.                 //These are our defaults
  44.                 $title      = 'Weather';
  45.                 $woeid      = '';
  46.                 $units      = 'f';
  47.                 $condensed  = false;
  48.                 $location   = true;
  49.                 $forecast   = true;
  50.             }
  51.        
  52.         ?> 
  53.        
  54.         <p>
  55.         <label for="<?php echo $this->get_field_id('title'); ?>"><?php echo __( 'Title:' ); ?></label>
  56.         <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" />
  57.         </p>
  58.  
  59.         <p>
  60.         <label for="<?php echo $this->get_field_id( 'woeid' ); ?>"><?php _e('Location Code:'); ?></label>
  61.         <input id="<?php echo $this->get_field_id( 'woeid' ); ?>" name="<?php echo $this->get_field_name( 'woeid' ); ?>" value="<?php echo $woeid; ?>"  class="widefat" />
  62.         <span style="font-size: 10px;">(e.g. This could be a zip code; or city, state.)</span>
  63.         </p>
  64.  
  65.         <p>
  66.         <label for="<?php echo $this->get_field_id( 'units' ); ?>"><?php _e('Units:'); ?></label>
  67.         <select id="<?php echo $this->get_field_id( 'units' ); ?>" name="<?php echo $this->get_field_name( 'units' ); ?>[select_value]" class="widefat">
  68.             <option value="c" <?php if ($units == 'c') echo 'selected'; ?>>Celsius</option>
  69.             <option value="f" <?php if ($units == 'f') echo 'selected'; ?>>Fahrenheit</option>
  70.         </select>          
  71.         </p>
  72.        
  73.         <p>
  74.         <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' ); ?>" />
  75.         <label for="<?php echo $this->get_field_id( 'condensed' ); ?>"><?php _e('Condensed format?'); ?></label>
  76.         </p>
  77.        
  78.         <p>
  79.         <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' ); ?>" />
  80.         <label for="<?php echo $this->get_field_id( 'location' ); ?>"><?php _e('Show the location?'); ?></label>
  81.         </p>
  82.        
  83.         <p>
  84.         <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' ); ?>" />
  85.         <label for="<?php echo $this->get_field_id( 'forecast' ); ?>"><?php _e('Display forecast?'); ?></label>
  86.         </p>
  87.                
  88.     <?php
  89.     }
  90.  
  91.     /**
  92.     * This is our Widget
  93.     **/
  94.     function widget( $args, $instance ) {
  95.         extract( $args );
  96.  
  97.         #Our variables from the widget settings
  98.         $title      = apply_filters('widget_title', $instance['title'] );
  99.         $woeid      = $instance['woeid'];
  100.         $units      = $instance['units'];
  101.         $condensed  = $instance['condensed'];
  102.         $location   = $instance['location'];
  103.         $forecast   = $instance['forecast'];
  104.  
  105.         #Before widget (defined by themes)
  106.         echo $before_widget;
  107.  
  108.         #Display the widget title if one was input (before and after defined by themes)
  109.         if ( $title )
  110.             echo $before_title . $title . $after_title;
  111.  
  112.         #Display name from widget settings if one was input    
  113.         $this->buildWidget($woeid,$units,$location,$forecast,$condensed);
  114.            
  115.         #After widget (defined by themes)
  116.         echo $after_widget;
  117.     }
  118.    
  119.        
  120.     function getData($woeid,$units) {
  121.        
  122.         $woeid   = str_replace(' ',' ',$woeid);
  123.        
  124.         //Yahoo! GeoLocation Service
  125.         $locationXML   = 'http://query.yahooapis.com/v1/public/yql?q=select * from geo.places where text="'.$woeid.'"&format=xml';
  126.         $location_id   = wp_remote_fopen($locationXML);
  127.         $location_data = simplexml_load_string($location_id);
  128.         $loc_id        = $location_data->results->place->woeid;
  129.  
  130.         //Yahoo! Weather API       
  131.         $xmlURL = 'http://weather.yahooapis.com/forecastrss?w='.$loc_id.'&u='.$units;
  132.        
  133.         //We need to cache the data for an hour to eliminate some of our API request limitations.
  134.         $cache = PLUGIN_PATH . '/includes/cache/cache-weather-'.$loc_id.'.xml';
  135.         if(!file_exists($cache) || (time() - filemtime($cache)) > 60*15) {
  136.               //let's grab the XML data
  137.               $data = wp_remote_fopen($xmlURL);
  138.               file_put_contents($cache, $data);
  139.         }
  140.  
  141.         $output = file_get_contents($cache);
  142.            
  143.         //load resource into a xmldom
  144.         $xmlData = simplexml_load_string($output);
  145.        
  146.         //handle yweather: namespace elements.
  147.         $channel_yweather = $xmlData->channel->children("http://xml.weather.yahoo.com/ns/rss/1.0"); //location information
  148.         $item_yweather    = $xmlData->channel->item->children("http://xml.weather.yahoo.com/ns/rss/1.0"); //current conditions and forecast
  149.        
  150.         foreach($channel_yweather as $x => $channel_item) {
  151.             foreach($channel_item->attributes() as $k => $attr) {
  152.                $yw_channel[$x][$k] = $attr;
  153.             }
  154.         }
  155.        
  156.         $r = -1;
  157.         foreach($item_yweather as $x => $yw_item) {
  158.             foreach($yw_item->attributes() as $k => $attr) {
  159.                 if($k == 'day') { $day = $attr; $r++; }
  160.                 if($x == 'forecast') { $yw_forecast[$x][$r][$k] = $attr;}
  161.                 else { $yw_forecast[$x][$k] = $attr; }
  162.             }
  163.         }
  164.  
  165.         //define conditions array
  166.         $conditions = array();
  167.        
  168.         //F and C html
  169.         if($units=='c') {
  170.             $t = '&deg;C';
  171.         } else {
  172.             $t = '&deg;F';
  173.         }
  174.        
  175.         $conditions['current']['city']        = $yw_channel['location']['city'];
  176.         $conditions['current']['region']      = $yw_channel['location']['region'];
  177.         $conditions['current']['country']     = $yw_channel['location']['country'];
  178.         $conditions['current']['temperature'] = $yw_forecast['condition']['temp'].$t;
  179.         $conditions['current']['conditions']  = $yw_forecast['condition']['text'];
  180.         $conditions['current']['icon']        = $yw_forecast['condition']['code'];
  181.         $conditions['attribution']['link']    = $xmlData->channel->item->link;
  182.         $conditions['current']['test']        = /*$yw_forecast['forecast']*/$locationXML;
  183.         //print_r($conditions['current']['test']);
  184.        
  185.         $i = 0;
  186.         foreach ($yw_forecast['forecast'] as $day) {
  187.             $conditions['forecast'][$i]['day']  = $day['day'];
  188.             $conditions['forecast'][$i]['date'] = $day['date'];        
  189.             $conditions['forecast'][$i]['hi']   = $day['high'];
  190.             $conditions['forecast'][$i]['low']  = $day['low'];
  191.             $conditions['forecast'][$i]['icon'] = $day['code'];
  192.             $conditions['forecast'][$i]['cond'] = $day['text'];
  193.             $i++;
  194.         }
  195.        
  196.         return $conditions;
  197.        
  198.     }
  199.    
  200.     function buildWidget($woeid,$units,$location,$forecast,$condensed) {
  201.         $conditions = $this->getData($woeid,$units);
  202.        
  203.         $icons = array(
  204.             "0" => array(
  205.                 "name"=>"tornado",
  206.                 "class"=>"wi-tornado"),
  207.             "1" => array(
  208.                 "name"=>"tropical storm",
  209.                 "class"=>"wi-day-thunderstorm"),
  210.             "2" => array(
  211.                 "name"=>"hurricane",
  212.                 "class"=>"wi-day-thunderstorm"),
  213.             "3" => array(
  214.                 "name"=>"severe thunderstorms",
  215.                 "class"=>"wi-day-thunderstorm"),
  216.             "4" => array(
  217.                 "name"=>"thunderstorms",
  218.                 "class"=>"wi-day-thunderstorm"),
  219.             "5" => array(
  220.                 "name"=>"mixed rain and snow",
  221.                 "class"=>"wi-day-rain-mix"),
  222.             "6" => array(
  223.                 "name"=>"mixed rain and sleet",
  224.                 "class"=>"wi-day-rain-mix"),
  225.             "7" => array(
  226.                 "name"=>"mixed snow and sleet",
  227.                 "class"=>"wi-day-snow"),
  228.             "8" => array(
  229.                 "name"=>"freezing drizzle",
  230.                 "class"=>"wi-day-snow"),
  231.             "9" => array(
  232.                 "name"=>"drizzle",
  233.                 "class"=>"wi-day-sprinkle"),
  234.             "10" => array(
  235.                 "name"=>"freezing rain",
  236.                 "class"=>"wi-day-sunny"),
  237.             "11" => array(
  238.                 "name"=>"showers",
  239.                 "class"=>"wi-day-sunny"),
  240.             "12" => array(
  241.                 "name"=>"showers",
  242.                 "class"=>"wi-day-sunny"),
  243.             "13" => array(
  244.                 "name"=>"snow flurries",
  245.                 "class"=>"wi-day-sunny"),
  246.             "14" => array(
  247.                 "name"=>"light snow showers",
  248.                 "class"=>"wi-day-sunny"),
  249.             "15" => array(
  250.                 "name"=>"blowing snow",
  251.                 "class"=>"wi-day-sunny"),
  252.             "16" => array(
  253.                 "name"=>"snow",
  254.                 "class"=>"wi-day-sunny"),
  255.             "17" => array(
  256.                 "name"=>"hail",
  257.                 "class"=>"wi-day-sunny"),
  258.             "18" => array(
  259.                 "name"=>"sleet",
  260.                 "class"=>"wi-day-sunny"),
  261.             "19" => array(
  262.                 "name"=>"dust",
  263.                 "class"=>"wi-day-sunny"),
  264.             "20" => array(
  265.                 "name"=>"foggy",
  266.                 "class"=>"wi-day-sunny"),
  267.             "21" => array(
  268.                 "name"=>"haze",
  269.                 "class"=>"wi-day-sunny"),
  270.             "22" => array(
  271.                 "name"=>"smoky",
  272.                 "class"=>"wi-day-sunny"),
  273.             "23" => array(
  274.                 "name"=>"blustery",
  275.                 "class"=>"wi-day-sunny"),
  276.             "24" => array(
  277.                 "name"=>"windy",
  278.                 "class"=>"wi-day-sunny"),
  279.             "25" => array(
  280.                 "name"=>"cold",
  281.                 "class"=>"wi-day-sunny"),
  282.             "26" => array(
  283.                 "name"=>"cloudy",
  284.                 "class"=>"wi-day-sunny"),
  285.             "27" => array(
  286.                 "name"=>"mostly cloudy (night)",
  287.                 "class"=>"wi-day-sunny"),
  288.             "28" => array(
  289.                 "name"=>"mostly cloudy (day)",
  290.                 "class"=>"wi-day-sunny"),
  291.             "29" => array(
  292.                 "name"=>"partly cloudy (night)",
  293.                 "class"=>"wi-day-sunny"),
  294.             "30" => array(
  295.                 "name"=>"partly cloudy (day)",
  296.                 "class"=>"wi-day-sunny"),
  297.             "31" => array(
  298.                 "name"=>"clear (night)",
  299.                 "class"=>"wi-day-sunny"),
  300.             "32" => array(
  301.                 "name"=>"sunny",
  302.                 "class"=>"wi-day-sunny"),
  303.             "33" => array(
  304.                 "name"=>"fair (night)",
  305.                 "class"=>"wi-day-sunny"),
  306.             "34" => array(
  307.                 "name"=>"fair (day)",
  308.                 "class"=>"wi-day-sunny"),
  309.             "35" => array(
  310.                 "name"=>"mixed rain and hail",
  311.                 "class"=>"wi-day-sunny"),
  312.             "36" => array(
  313.                 "name"=>"hot",
  314.                 "class"=>"wi-day-sunny"),
  315.             "37" => array(
  316.                 "name"=>"isolated thunderstorms",
  317.                 "class"=>"wi-day-sunny"),
  318.             "38" => array(
  319.                 "name"=>"scattered thunderstorms",
  320.                 "class"=>"wi-day-sunny"),
  321.             "39" => array(
  322.                 "name"=>"scattered thunderstorms",
  323.                 "class"=>"wi-day-sunny"),
  324.             "40" => array(
  325.                 "name"=>"scattered showers",
  326.                 "class"=>"wi-day-sunny"),
  327.             "41" => array(
  328.                 "name"=>"heavy snow",
  329.                 "class"=>"wi-day-sunny"),
  330.             "42" => array(
  331.                 "name"=>"scattered snow showers",
  332.                 "class"=>"wi-day-sunny"),
  333.             "43" => array(
  334.                 "name"=>"heavy snow",
  335.                 "class"=>"wi-day-sunny"),
  336.             "44" => array(
  337.                 "name"=>"partly cloudy",
  338.                 "class"=>"wi-day-sunny"),
  339.             "45" => array(
  340.                 "name"=>"thundershowers",
  341.                 "class"=>"wi-day-sunny"),
  342.             "46" => array(
  343.                 "name"=>"snow showers",
  344.                 "class"=>"wi-day-sunny"),
  345.             "47" => array(
  346.                 "name"=>"isolated thundershowers",
  347.                 "class"=>"wi-day-sunny")
  348.         ); 
  349.        
  350.         if($forecast && !$condensed)
  351.         {          
  352.    
  353.             $icon1 = $conditions['forecast'][0]['icon'];
  354.             $icon2 = $conditions['forecast'][1]['icon'];
  355.             $class1 = '';
  356.             $class2 = '';  
  357.             $i1 = -1;  
  358.             $i2 = -1;  
  359.             $state = $conditions['current']['region'][0];
  360.            
  361.             foreach($icons as $icon){
  362.                 $i1++;
  363.                 if($icon1==$i1){
  364.                     $class1=$icon['class'];
  365.                 }
  366.             }
  367.             foreach($icons as $icon){
  368.                 $i2++;
  369.                 if($icon2==$i2){
  370.                     $class2=$icon['class'];
  371.                 }              
  372.             }
  373.            
  374.                        
  375.            
  376.             echo '
  377.             <div class="weather">
  378.                 <span class="state">'.$state.'</span>
  379.                 <span class="day">'.$conditions['forecast'][0]['day'].'</span>
  380.                 <span class="date">'.$conditions['forecast'][0]['date'].'</span>
  381.                 <span class="temp-hi">Hi: '.$conditions['forecast'][0]['hi'].'</span>
  382.                 <span class="temp-lo">Lo: '.$conditions['forecast'][0]['low'].'</span>
  383.                 <i class="'.$class1.' icon"></i>   
  384.                 <span class="cond">'.$conditions['forecast'][0]['cond'].'</span>                                   
  385.             </div>
  386.             ';  
  387.            
  388.             //print_r($conditions);
  389.            
  390.            
  391.         }                      
  392.     }
  393.  
  394. } // class wp_current_weather
  395.  
  396. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement