| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class for UV widget displaying the data from OpenUV.com |
| 4 |
* |
| 5 |
* @package wp-forecast |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! class_exists( 'WpfUvWidget' ) ) { |
| 9 |
/** |
| 10 |
* Class for UV widget displaying the data from OpenUV.com |
| 11 |
*/ |
| 12 |
class WpfUvWidget extends WP_Widget { |
| 13 |
/** |
| 14 |
* Constructor |
| 15 |
*/ |
| 16 |
public function __construct() { |
| 17 |
$widget_ops = array( |
| 18 |
'classname' => 'wp_forecast_uv_widget', |
| 19 |
'description' => 'WP Forecast UV-Widget', |
| 20 |
); |
| 21 |
$control_ops = array( |
| 22 |
'width' => 300, |
| 23 |
'height' => 150, |
| 24 |
); |
| 25 |
parent::__construct( 'wp-forecast-uv', 'WP Forecast UV', $widget_ops, $control_ops ); |
| 26 |
} |
| 27 |
/** |
| 28 |
* Instantiate widget |
| 29 |
* |
| 30 |
* @param array $args Widgetparameters. |
| 31 |
* @param array $instance Instance of the widget. |
| 32 |
*/ |
| 33 |
public function widget( $args, $instance ) { |
| 34 |
// get widget params from instance. |
| 35 |
$title = ( isset( $instance['title'] ) ? $instance['title'] : '' ); |
| 36 |
$wpfcid = ( isset( $instance['wpfcid'] ) ? $instance['wpfcid'] : '' ); |
| 37 |
$safeexposure = ( isset( $instance['safeexposure'] ) ? esc_attr( $instance['safeexposure'] ) : '' ); |
| 38 |
$showgraph = ( isset( $instance['showgraph'] ) ? esc_attr( $instance['showgraph'] ) : '' ); |
| 39 |
|
| 40 |
if ( trim( $wpfcid ) == '' ) { |
| 41 |
$wpfcid = 'A'; |
| 42 |
} |
| 43 |
// pass title to show function. |
| 44 |
$args['title'] = $title; |
| 45 |
|
| 46 |
if ( '?' == $wpfcid ) { |
| 47 |
$wpf_vars = get_wpf_opts( 'A' ); |
| 48 |
} else { |
| 49 |
$wpf_vars = get_wpf_opts( $wpfcid ); |
| 50 |
} |
| 51 |
|
| 52 |
if ( ! empty( $language_override ) ) { |
| 53 |
$wpf_vars['wpf_language'] = $language_override; |
| 54 |
} |
| 55 |
// call display method. |
| 56 |
$this->show( $wpfcid, $args, $wpf_vars, $showgraph, $safeexposure ); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Update the values from the form for the widget. |
| 61 |
* |
| 62 |
* @param array $new_instance New instance of th widget. |
| 63 |
* @param array $old_instance Old instance of the widget. |
| 64 |
*/ |
| 65 |
public function update( $new_instance, $old_instance ) { |
| 66 |
// update semaphor counter for loading wpf ajax script. |
| 67 |
|
| 68 |
if ( $old_instance['wpfcid'] != $new_instance['wpfcid'] ) { |
| 69 |
$semnow = get_option( 'wpf_sem_ajaxload' ); |
| 70 |
|
| 71 |
if ( '?' == $new_instance['wpfcid'] ) { |
| 72 |
update_option( 'wpf_sem_ajaxload', $semnow + 1 ); |
| 73 |
} else { |
| 74 |
update_option( 'wpf_sem_ajaxload', ( $semnow - 1 < 0 ? 0 : $semnow - 1 ) ); |
| 75 |
} |
| 76 |
} |
| 77 |
return $new_instance; |
| 78 |
} |
| 79 |
/** |
| 80 |
* Show the form. |
| 81 |
* |
| 82 |
* @param array $instance The instance of the widget. |
| 83 |
*/ |
| 84 |
public function form( $instance ) { |
| 85 |
$count = wpf_get_option( 'wp-forecast-count' ); |
| 86 |
// get translation. |
| 87 |
$locale = get_locale(); |
| 88 |
|
| 89 |
if ( empty( $locale ) ) { |
| 90 |
$locale = 'en_US'; |
| 91 |
} |
| 92 |
|
| 93 |
if ( function_exists( 'load_plugin_textdomain' ) ) { |
| 94 |
add_filter( 'plugin_locale', 'wpf_lplug', 10, 2 ); |
| 95 |
load_plugin_textdomain( 'wp-forecast_' . $locale, false, dirname( plugin_basename( __FILE__ ) ) . '/lang/' ); |
| 96 |
remove_filter( 'plugin_locale', 'wpf_lplug', 10, 2 ); |
| 97 |
} |
| 98 |
$title = ( isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : '' ); |
| 99 |
$wpfcid = ( isset( $instance['wpfcid'] ) ? esc_attr( $instance['wpfcid'] ) : '' ); |
| 100 |
$safeexposure = ( isset( $instance['safeexposure'] ) ? esc_attr( $instance['safeexposure'] ) : '' ); |
| 101 |
$showgraph = ( isset( $instance['showgraph'] ) ? esc_attr( $instance['showgraph'] ) : '' ); |
| 102 |
|
| 103 |
// code for widget title form. |
| 104 |
$out = ''; |
| 105 |
$out .= '<p><label for="' . $this->get_field_id( 'title' ) . '" >'; |
| 106 |
$out .= wpf__( 'Title:', $locale ); |
| 107 |
$out .= '<input class="widefat" id="' . $this->get_field_id( 'title' ) . '" name="' . $this->get_field_name( 'title' ) . '" type="text" value="' . $title . '" /></label></p>'; |
| 108 |
// print out widget selector. |
| 109 |
$out .= '<p><label for ="' . $this->get_field_id( 'wpfcid' ) . '" >'; |
| 110 |
$out .= wpf__( 'Available widgets', $locale ); |
| 111 |
$out .= "<select name='" . $this->get_field_name( 'wpfcid' ) . "' id='" . $this->get_field_id( 'wpfcid' ) . "' size='1' >"; |
| 112 |
// option for choose dialog. |
| 113 |
$out .= "<option value='?' "; |
| 114 |
|
| 115 |
if ( '?' == $wpfcid ) { |
| 116 |
$out .= " selected='selected' "; |
| 117 |
} |
| 118 |
$out .= '>?</option>'; |
| 119 |
|
| 120 |
for ( $i = 0;$i < $count;$i++ ) { |
| 121 |
$id = get_widget_id( $i ); |
| 122 |
$out .= "<option value='" . $id . "' "; |
| 123 |
|
| 124 |
if ( $wpfcid == $id || ( '' == $wpfcid && 'A' == $id ) ) { |
| 125 |
$out .= " selected='selected' "; |
| 126 |
} |
| 127 |
$out .= '>' . $id . '</option>'; |
| 128 |
} |
| 129 |
$out .= '</select></label></p>'; |
| 130 |
$out .= '<p><label for ="' . $this->get_field_id( 'showgraph' ) . '" >'; |
| 131 |
$out .= wpf__( 'Show UV graph:', $locale ); |
| 132 |
$checked = ( 1 == $showgraph ? "checked='checked'" : '' ); |
| 133 |
$out .= '<input type="checkbox" name="' . $this->get_field_name( 'showgraph' ) . '" id="' . $this->get_field_id( 'showgraph' ) . '" value="1" ' . $checked . '>'; |
| 134 |
$out .= '</label></p>'; |
| 135 |
$out .= '<p><label for ="' . $this->get_field_id( 'safeexposure' ) . '" >'; |
| 136 |
$out .= wpf__( 'Show safe exposure time:', $locale ); |
| 137 |
$checked = ( 1 == $safeexposure ? "checked='checked'" : '' ); |
| 138 |
$out .= '<input type="checkbox" name="' . $this->get_field_name( 'safeexposure' ) . '" id="' . $this->get_field_id( 'sageexposure' ) . '" value="1" ' . $checked . '>'; |
| 139 |
$out .= '</label></p>'; |
| 140 |
echo wp_kses( $out, wpf_allowed_tags() ); |
| 141 |
} |
| 142 |
/** |
| 143 |
* Show the widget. |
| 144 |
* |
| 145 |
* @param string $wpfcid The Widget ID. |
| 146 |
* @param array $args The Widget arguments. |
| 147 |
* @param array $wpf_vars The wp-forecast parameters. |
| 148 |
* @param bool $showgraph Show the graph. |
| 149 |
* @param bool $safeexposure Show the safe exposure time for the skin. |
| 150 |
*/ |
| 151 |
public function show( $wpfcid, $args, $wpf_vars, $showgraph, $safeexposure ) { |
| 152 |
// check how we are called as a widget or from sidebar. |
| 153 |
|
| 154 |
if ( count( $args ) == 0 ) { |
| 155 |
$show_from_widget = 0; |
| 156 |
} else { |
| 157 |
$show_from_widget = 1; |
| 158 |
} |
| 159 |
|
| 160 |
// take title from widget args. |
| 161 |
$title = ''; |
| 162 |
if ( isset( $args['title'] ) ) { |
| 163 |
// remember title from widget. |
| 164 |
$wpf_vars['title'] = $args['title']; |
| 165 |
// apply wp default filters to title for output. |
| 166 |
$title = apply_filters( 'widget_title', $args['title'] ); |
| 167 |
} |
| 168 |
|
| 169 |
// get translations. |
| 170 |
if ( function_exists( 'load_plugin_textdomain' ) ) { |
| 171 |
add_filter( 'plugin_locale', 'wpf_lplug', 10, 2 ); |
| 172 |
load_plugin_textdomain( 'wp-forecast_' . $wpf_vars['wpf_language'], false, dirname( plugin_basename( __FILE__ ) ) . '/lang/' ); |
| 173 |
remove_filter( 'plugin_locale', 'wpf_lplug', 10, 2 ); |
| 174 |
} |
| 175 |
$plugin_path = plugins_url( '', __FILE__ ); |
| 176 |
$w = wp_forecast_data( $wpfcid, $wpf_vars['wpf_language'] ); |
| 177 |
// output current conditions. |
| 178 |
$out = ''; |
| 179 |
$out .= "\n<div class=\"wp-forecast-curr\">\n"; |
| 180 |
|
| 181 |
// if the provider sends us a failure notice print it and return. |
| 182 |
if ( '' != $w['failure'] ) { |
| 183 |
$out .= wpf_esc_attr__( 'Failure notice from provider', $wpf_vars['wpf_language'] ) . ':<br />'; |
| 184 |
$out .= $w['failure'] . '</div>'; |
| 185 |
|
| 186 |
// print it. |
| 187 |
if ( 1 == $show_from_widget ) { |
| 188 |
echo wp_kses( $before_widget . $before_title . $title . $after_title . $out . $after_widget, wpf_allowed_tags() ); |
| 189 |
} else { |
| 190 |
echo wp_kses( $out, wpf_allowed_tags() ); |
| 191 |
} |
| 192 |
return false; |
| 193 |
} |
| 194 |
// if error print an error message and return. |
| 195 |
|
| 196 |
if ( count( $w ) <= 0 ) { |
| 197 |
$out .= wpf_esc_attr__( 'Sorry, no valid weather data available.', $wpf_vars['wpf_language'] ) . '<br />'; |
| 198 |
$out .= wpf_esc_attr__( 'Please try again later.', $wpf_vars['wpf_language'] ) . '</div>'; |
| 199 |
// print it. |
| 200 |
|
| 201 |
if ( 1 == $show_from_widget ) { |
| 202 |
echo wp_kses( $before_widget . $before_title . $title . $after_title . $out . $after_widget, wpf_allowed_tags() ); |
| 203 |
} else { |
| 204 |
echo wp_kses( $out, wpf_allowed_tags() ); |
| 205 |
} |
| 206 |
return false; |
| 207 |
} |
| 208 |
// ortsnamen ausgeben parameter fuer open in new window berücksichtigen. |
| 209 |
$servicelink = ''; |
| 210 |
$servicelink_end = ''; |
| 211 |
|
| 212 |
if ( substr( $wpf_vars['dispconfig'], 25, 1 ) == '1' ) { |
| 213 |
$servicelink = '<a href="' . $w['servicelink'] . '"'; |
| 214 |
$servicelink_end = '</a>'; |
| 215 |
|
| 216 |
if ( substr( $wpf_vars['dispconfig'], 26, 1 ) == '1' ) { |
| 217 |
$servicelink = $servicelink . ' target="_blank" >'; |
| 218 |
} else { |
| 219 |
$servicelink = $servicelink . ' >'; |
| 220 |
} |
| 221 |
} |
| 222 |
$out .= '<div class="wp-forecast-curr-head">'; |
| 223 |
|
| 224 |
if ( '' == $w['location'] || 1 == $wpf_vars['visitorlocation'] ) { |
| 225 |
$out .= '<div>' . $servicelink . $w['locname'] . $servicelink_end . "</div>\n"; |
| 226 |
} elseif ( '' != trim( $w['location'] ) && ' ' != $w['location'] ) { |
| 227 |
$out .= '<div>' . $servicelink . $w['location'] . $servicelink_end . "</div>\n"; |
| 228 |
} |
| 229 |
// show date / time. |
| 230 |
|
| 231 |
// if current time should be used. |
| 232 |
if ( '1' == $wpf_vars['currtime'] ) { |
| 233 |
$cd = $w['blogdate']; |
| 234 |
$ct = $w['blogtime']; |
| 235 |
} else { |
| 236 |
// else take given accuweather time. |
| 237 |
$cd = $w['accudate']; |
| 238 |
$ct = $w['accutime']; |
| 239 |
} |
| 240 |
|
| 241 |
if ( substr( $wpf_vars['dispconfig'], 18, 1 ) == '1' || substr( $wpf_vars['dispconfig'], 1, 1 ) == '1' ) { |
| 242 |
$out .= '<div>'; |
| 243 |
|
| 244 |
if ( substr( $wpf_vars['dispconfig'], 18, 1 ) == '1' ) { |
| 245 |
$out .= $cd; |
| 246 |
} else { |
| 247 |
$out .= wpf__( 'time', $wpf_vars['wpf_language'] ) . ': '; |
| 248 |
} |
| 249 |
|
| 250 |
if ( substr( $wpf_vars['dispconfig'], 18, 1 ) == '1' && substr( $wpf_vars['dispconfig'], 1, 1 ) == '1' ) { |
| 251 |
$out .= ', '; |
| 252 |
} |
| 253 |
|
| 254 |
if ( substr( $wpf_vars['dispconfig'], 1, 1 ) == '1' ) { |
| 255 |
$out .= $ct; |
| 256 |
} |
| 257 |
$out .= "</div>\n"; |
| 258 |
} |
| 259 |
$out .= "</div>\n"; |
| 260 |
$out .= '<div class="wp-forecast-curr-block">'; |
| 261 |
// show icon. |
| 262 |
$out .= "<div class='wp-forecast-curr-left'>"; |
| 263 |
|
| 264 |
$uviconno = ( isset( $w['openuv']['uv'] ) ? round( $w['openuv']['uv'] ) : 0 ); |
| 265 |
error_log( print_r( $wpf_vars, true ) ); |
| 266 |
$uvicon = 'UVIndex' . $uviconno . '.jpg'; |
| 267 |
if ( $wpf_vars['ouv_alticons'] ) { |
| 268 |
$uvicon = 'A' . $uvicon; |
| 269 |
} |
| 270 |
$breite = 0; |
| 271 |
$hoehe = 0; |
| 272 |
|
| 273 |
if ( file_exists( plugin_dir_path( __FILE__ ) . '/icons/' . $uvicon ) ) { |
| 274 |
$isize = getimagesize( plugin_dir_path( __FILE__ ) . '/icons/' . $uvicon ); |
| 275 |
|
| 276 |
if ( false != $isize ) { |
| 277 |
$breite = $isize[0]; |
| 278 |
$hoehe = $isize[1]; |
| 279 |
} |
| 280 |
} |
| 281 |
$out .= "<img class='awp-forecast-curr-left' src='" . $plugin_path . '/icons/' . $uvicon . "' alt='" . $w['shorttext'] . "' width='" . $breite . "' height='" . $hoehe . "' />\n"; |
| 282 |
|
| 283 |
$out .= '<br />'; |
| 284 |
$out .= '</div>'; |
| 285 |
$out .= "</div>\n"; // end of block. |
| 286 |
$out .= '<div class="wp-forecast-curr-details">'; |
| 287 |
// show data from OpenUV.io if applicable. |
| 288 |
|
| 289 |
if ( trim( $wpf_vars['ouv_apikey'] ) != '' ) { |
| 290 |
|
| 291 |
if ( $wpf_vars['ouv_uv'] ) { |
| 292 |
$out .= '<div>' . wpf__( 'Current UV-Index', $wpf_vars['wpf_language'] ) . ': ' . $w['openuv']['uv'] . "</div>\n"; |
| 293 |
} |
| 294 |
|
| 295 |
if ( $wpf_vars['ouv_uvmax'] ) { |
| 296 |
$out .= '<div>' . wpf__( 'Max. UV-Index', $wpf_vars['wpf_language'] ) . ': ' . $w['openuv']['uv_max'] . "</div>\n"; |
| 297 |
} |
| 298 |
|
| 299 |
if ( $wpf_vars['ouv_ozone'] ) { |
| 300 |
$out .= '<div>' . wpf__( 'Ozone', $wpf_vars['wpf_language'] ) . ': ' . $w['openuv']['ozone'] . " DU</div>\n"; |
| 301 |
} |
| 302 |
$out .= '<br/>'; |
| 303 |
|
| 304 |
if ( $wpf_vars['ouv_safetime'] || $safeexposure ) { |
| 305 |
$j = 1; |
| 306 |
|
| 307 |
foreach ( $w['openuv']['safe_exposure_time'] as $set ) { |
| 308 |
|
| 309 |
if ( trim( $set ) != '' ) { |
| 310 |
$out .= '<div>' . wpf__( 'Safe Exposure Time for Skin Type', $wpf_vars['wpf_language'] ) . " $j: " . $set . " Min.</div>\n"; |
| 311 |
} else { |
| 312 |
$out .= '<div>' . wpf__( 'Safe Exposure Time for Skin Type', $wpf_vars['wpf_language'] ) . " $j: ∞ Min.</div>\n"; |
| 313 |
} |
| 314 |
$j++; |
| 315 |
} |
| 316 |
$out .= '<br/>'; |
| 317 |
} |
| 318 |
|
| 319 |
if ( $showgraph ) { |
| 320 |
$count_values = count( $w['openuv']['forecast'] ); |
| 321 |
// determine min and max values. |
| 322 |
$uvmin = 99; |
| 323 |
$uvmax = 0; |
| 324 |
$data = array(); |
| 325 |
|
| 326 |
foreach ( $w['openuv']['forecast'] as $uvfc ) { |
| 327 |
|
| 328 |
if ( $uvfc['uv'] > $uvmax ) { |
| 329 |
$uvmax = $uvfc['uv']; |
| 330 |
} |
| 331 |
|
| 332 |
if ( $uvfc['uv'] < $uvmin ) { |
| 333 |
$uvmin = $uvfc['uv']; |
| 334 |
} |
| 335 |
$d = new DateTime( $uvfc['uv_time'] ); |
| 336 |
$data[ (int) $d->format( 'H' ) ] = round( $uvfc['uv'], 1 ) * 10; |
| 337 |
} |
| 338 |
$scale_ymax = ( (int) $uvmax + 1 ) * 10; |
| 339 |
// print out diagram. |
| 340 |
$out .= '<style>'; |
| 341 |
$out .= 'div.wpf-uv-bar-on {float: left; width: 15px; height: 5px; background-color: #666666; margin: 1px;}'; |
| 342 |
$out .= 'div.wpf-uv-bar-off {float: left; width: 15px; height: 5px; background-color: rgb(255,255,255,0); margin: 1px;}'; |
| 343 |
$out .= 'div.wpf-uv-row-clear {clear: both;}'; |
| 344 |
$out .= 'div.wpf-uv-bar-left {float:left; width: 20px; height: 5px; border-style: none none none none; border-color: #335588; border-width: 3px;}'; |
| 345 |
$out .= 'div.wpf-uv-bar-right {float:left; width: 20px; height: 5px; border-style: none none none solid; border-color: #335588; border-width: 3px;}'; |
| 346 |
$out .= 'div.wpf-uv-row-left {float:left; width: 20px; height: 5px; border-style: solid none none none; border-color: #335588; border-width: 3px;margin-top:7px;}'; |
| 347 |
$out .= '</style>'; |
| 348 |
|
| 349 |
for ( $i = $scale_ymax;$i >= 0;$i-- ) { |
| 350 |
|
| 351 |
if ( 0 == $i % 10 ) { |
| 352 |
$out .= "<div class='wpf-uv-bar-left'>" . (int) ( $i / 10 ) . '</div>'; |
| 353 |
} else { |
| 354 |
$out .= "<div class='wpf-uv-bar-left'> </div>"; |
| 355 |
} |
| 356 |
|
| 357 |
foreach ( $data as $dk => $dv ) { |
| 358 |
|
| 359 |
if ( $dv >= $i ) { |
| 360 |
$out .= "<div class='wpf-uv-bar-on'> </div>"; |
| 361 |
} else { |
| 362 |
$out .= "<div class='wpf-uv-bar-off'> </div>"; |
| 363 |
} |
| 364 |
} |
| 365 |
$out .= "<div class='wpf-uv-row-clear'></div>"; |
| 366 |
} |
| 367 |
// print x -axis. |
| 368 |
$out .= "<div class='wpf-uv-row-left'> </div>"; |
| 369 |
|
| 370 |
foreach ( $data as $dk => $dv ) { |
| 371 |
$out .= "<div class='wpf-uv-row-left'>$dk</div>"; |
| 372 |
} |
| 373 |
$out .= "<div class='wpf-uv-row-clear'></div><br/>"; |
| 374 |
$d = new DateTime( $w['openuv']['forecast'][0]['uv_time'] ); |
| 375 |
$d1 = new DateTime( $w['openuv']['forecast'][ count( $w['openuv']['forecast'] ) - 1 ]['uv_time'] ); |
| 376 |
$out .= '<div>Werte vom ' . $d->format( 'd.m.y' ) . ' von ' . $d->format( 'H:i' ) . ' bis ' . $d1->format( 'H:i' ) . ' Uhr.</div>'; |
| 377 |
} |
| 378 |
// print copyright notice. |
| 379 |
$out .= "<div class='wpf-uv-row-clear'></div><br/>"; |
| 380 |
$out .= '<div class="wp-forecast-copyright">' . wpf__( $w['openuv']['copyright'], $wpf_vars['wpf_language'] ) . '</div>'; |
| 381 |
} |
| 382 |
// show copyright. |
| 383 |
|
| 384 |
if ( substr( $wpf_vars['dispconfig'], 21, 1 ) == '1' ) { |
| 385 |
$out .= '<div class="wp-forecast-copyright">' . $w['copyright'] . '</div>'; |
| 386 |
} |
| 387 |
$out .= "</div>\n"; // end of details. |
| 388 |
$out .= "</div>\n"; // end of curr. |
| 389 |
|
| 390 |
// print it. |
| 391 |
if ( 1 == $show_from_widget ) { |
| 392 |
echo wp_kses( $args['before_widget'] . $args['before_title'] . $title . $args['after_title'], wpf_allowed_tags() ); |
| 393 |
} |
| 394 |
echo '<div id="wp-forecast' . esc_attr( $wpfcid ) . '" class="wp-forecast">' . wp_kses( $out, wpf_allowed_tags() ) . '</div>' . "\n"; |
| 395 |
// to come back to theme floating status. |
| 396 |
echo '<div style="clear:inherit;"> </div>'; |
| 397 |
|
| 398 |
if ( 1 == $show_from_widget ) { |
| 399 |
echo wp_kses( $args['after_widget'], wpf_allowed_tags() ); |
| 400 |
} |
| 401 |
} |
| 402 |
} |
| 403 |
} |
| 404 |
|