current; $weather_data['forecast'] = $weather_apixu->forecast; } else { // NOW $weather_apixu = apixuWeather::get_current_weather($key, $location_city, $location_country); $weather_data['now'] = $weather_apixu->current; } $weather_data['location'] = $weather_apixu->location; if ($weather_data['now'] OR $weather_data['forecast']) { set_transient($weather_transient_name, $weather_data, apply_filters('apixu_weather_cache', 1800)); } } // NO WEATHER if (!$weather_data OR !isset($weather_data['now'])) return apixu_weather_error(); // TODAYS TEMPS $today = $weather_data['now']; $today_temp = ($units == "imperial") ? (int)$today->temp_f : (int)$today->temp_c; $location = $weather_data['location']; // BACKGROUND DATA, CLASSES AND OR IMAGES $background_classes = array(); $background_classes[] = "apixu-weather-wrap"; $background_classes[] = "awecf"; $background_classes[] = "awe_" . $size; // DATA //$header_title = $override_title ? $override_title : $today->name; // WIND $wind_direction = false; if (isset($today->wind_dir)) $wind_direction = apply_filters('apixu_weather_wind_direction', __($today->wind_dir, 'apixu-weather')); $background_classes[] = ($show_stats) ? "awe_with_stats" : "awe_without_stats"; // ADD WEATHER CONDITIONS CLASSES TO WRAP if (isset($today->condition)) { $weather_code = $today->condition->code; $weather_descr = explode(' ', strtolower(trim($today->condition->text))); $weather_description_slug = sanitize_title($weather_descr[ count($weather_descr) - 1 ]); $background_classes[] = "awe-code-" . $weather_code; $background_classes[] = "awe-desc-" . $weather_description_slug; } // EXTRA STYLES $background_class_string = @implode(" ", apply_filters('apixu_weather_background_classes', $background_classes)); $today_sign = ($today_temp > 0) ? '+' : ''; if (in_array($widget_scheme, [41, 42, 43, 44, 45])) { $widget_scheme_group = 'shm_4'; } elseif (in_array($widget_scheme, [71, 72, 73])) { $widget_scheme_group = 'shm_7'; } elseif (in_array($widget_scheme, [81, 82, 83])) { $widget_scheme_group = 'shm_8'; } elseif (in_array($widget_scheme, [91])) { $widget_scheme_group = 'shm_9'; } if (in_array($widget_scheme, array(1,81))){ $inline_style = "color:{$text_color};background:{$custom_bg_color}"; } if ($inline_style){ $inline_style = "style='{$inline_style}'"; } $background_class_string .= ' ' . $widget_scheme_group; // DISPLAY WIDGET $rtn .= "
"; $rtn .= "
"; // Picture if (isset($today->condition->icon)) { $rtn1 = '
'; $folder = 'large'; $pict_apend = 'lg'; $pict = explode('/', $today->condition->icon); $day_case = $pict[ count($pict) - 2 ]; $pict_code = (int)$pict[ count($pict) - 1 ]; $path_apixu_pict = plugin_dir_url(__FILE__) . "img/{$folder}/{$pict_code}_{$day_case}_{$pict_apend}.png"; $rtn1 .= ""; $rtn1 .= '
'; } // Weather stat if ($widget_scheme_group !== 'shm_8') { $rtn1 .= '
' . $today->condition->text . '
'; } //location $rtn2 = '
'; if ($widget_scheme_group == 'shm_8') { $rtn2 .= '
' . $location->name . ', ' . $location->country; $rtn2 .= '
' . $today->condition->text . '
'; $rtn2 .= '
'; } else { $rtn2 .= '
' . $location->name . ', ' . $location->country . '
'; } $rtn2 .= '
'; if ($widget_scheme_group == 'shm_7') { $rtn .= $rtn2 . $rtn1; } else { $rtn .= $rtn1 . $rtn2; } //current weather $rtn .= "
{$today_sign}{$today_temp}{$units_display_symbol}{$units_display}
"; //show stats if ($show_stats AND isset($weather_data['now'])) { $wind_speed = isset($today->wind_kph) ? $today->wind_kph / 3.6 : false; $feelslike = ($units == "imperial") ? (int)$today->feelslike_f : (int)$today->feelslike_c; $wind_speed_text = ($units == "imperial") ? __('mph', 'apixu-weather') : __('m/s', 'apixu-weather'); $wind_speed_obj = apply_filters('apixu_weather_wind_speed', array( 'text' => apply_filters('apixu_weather_wind_speed_text', $wind_speed_text), 'speed' => round($wind_speed), 'direction' => $wind_direction), $wind_speed, $wind_direction); // CURRENT WEATHER STATS $rtn .= '
'; if (isset($today->condition->text)) $rtn .= '
' . __('Feels like:', 'apixu-weather') . ' ' .$today_sign.$feelslike.$units_display_symbol.$units_display . '
'; if (isset($today->humidity)) $rtn .= '
' . __('humidity:', 'apixu-weather') . " " . $today->humidity . '%
'; if ($wind_speed AND $wind_direction) $rtn .= '
' . __('wind:', 'apixu-weather') . ' ' . $wind_speed_obj['speed'] . $wind_speed_obj['text'] . ' ' . $wind_speed_obj['direction'] . '
'; $rtn .= '
'; } //show forecast if ($days_to_show != "hide") { $rtn .= "
"; $c = 1; $dt_today = date('Ymd', current_time('timestamp', 0)); $forecast = $weather_data['forecast']; $days_to_show = (int)$days_to_show; $days_of_week = apply_filters('apixu_weather_days_of_week', array(__('Sun', 'apixu-weather'), __('Mon', 'apixu-weather'), __('Tue', 'apixu-weather'), __('Wed', 'apixu-weather'), __('Thu', 'apixu-weather'), __('Fri', 'apixu-weather'), __('Sat', 'apixu-weather'))); foreach ((array)$forecast->forecastday as $forecast) { if ($dt_today >= date('Ymd', $forecast->date_epoch)) { $c = 1; continue; } $forecast->temp = ($units == "imperial") ? (int)$forecast->day->avgtemp_f : (int)$forecast->day->avgtemp_c; $day_of_week = $days_of_week[ date('w', $forecast->date_epoch) ]; if (isset($forecast->day->condition->icon)) { $rtn_ = '
'; $folder = 'small'; $pict_apend = 'sm'; $pict = explode('/', $forecast->day->condition->icon); $day_case = $pict[ count($pict) - 2 ]; $pict_code = (int)$pict[ count($pict) - 1 ]; $path_apixu_pict_sm = plugin_dir_url(__FILE__) . "img/{$folder}/{$pict_code}_{$day_case}_{$pict_apend}.png"; $rtn_ .= ""; $rtn_ .= '
'; } $rtn .= "
{$day_of_week}
{$rtn_}
{$today_sign}{$forecast->temp}{$units_display_symbol}{$units_display}
"; if ($c == $days_to_show) { break; } $c++; } $rtn .= "
"; } if ($show_link) { $show_link_text = apply_filters('apixu_weather_extended_forecast_text', __('Apixu.com', 'apixu-weather')); $rtn .= "
"; $rtn .= "{$show_link_text}"; $rtn .= "
"; } $rtn .= "
"; $rtn .= "
"; return $rtn; } // RETURN ERROR function apixu_weather_error( $msg = false ) { $error_handling = get_option('aw-error-handling'); if (!$error_handling) $error_handling = "source"; if (!$msg) $msg = __('No weather information available', 'apixu-weather'); if ($error_handling == "display-admin") { // DISPLAY ADMIN if (current_user_can('manage_options')) { return "
" . $msg . "
"; } } else if ($error_handling == "display-all") { // DISPLAY ALL return "
" . $msg . "
"; } else { return apply_filters('apixu_weather_error', ""); } } // ENQUEUE ADMIN SCRIPTS function apixu_weather_admin_scripts( $hook ) { if ('widgets.php' != $hook) return; wp_enqueue_style('jquery'); wp_enqueue_style('underscore'); wp_enqueue_style('wp-color-picker'); wp_enqueue_script('wp-color-picker'); wp_enqueue_script('apixu_weather_admin_script', plugin_dir_url(__FILE__) . '/apixu-weather-widget-admin.js', array('jquery', 'underscore')); wp_localize_script('apixu_weather_admin_script', 'awe_script', array( 'no_owm_city' => esc_attr(__("No city found in Apixu.", 'apixu-weather')), 'one_city_found' => esc_attr(__('Only one location found. The ID has been set automatically above.', 'apixu-weather')), 'confirm_city' => esc_attr(__('Please confirm your city:  ', 'apixu-weather')), ) ); } add_action('admin_enqueue_scripts', 'apixu_weather_admin_scripts'); // GET APPID function awe_get_appid() { return defined('APIXU_WEATHER_APPID') ? APIXU_WEATHER_APPID : get_option('open-weather-key'); } // PING OPENWEATHER FOR OWMID add_action('wp_ajax_awe_ping_owm_for_id', 'awe_ping_owm_for_id'); function awe_ping_owm_for_id() { $key = awe_get_appid(); $location = urlencode($_GET['location']); $weather_apixu = apixuWeather::search($key, $location); header("Content-Type: application/json"); echo $weather_apixu; die; } // WIDGET require_once(dirname(__FILE__) . "/widget.php"); // SETTINGS require_once(dirname(__FILE__) . "/apixu-weather-settings.php");