예제 #1
0
 /**
  * @param $atts
  * @param $weather_data - the precomputed weather data
  * @return bool|string
  *  - bool:true - we have the $weather_data (from cache or from a real request)
  *  - string - error message
  */
 private static function get_weather_data($atts, &$weather_data)
 {
     if (empty($atts['w_language'])) {
         $atts['w_language'] = 'en';
         $sytem_locale = get_locale();
         $available_locales = array('en', 'es', 'sp', 'fr', 'it', 'de', 'pt', 'ro', 'pl', 'ru', 'uk', 'ua', 'fi', 'nl', 'bg', 'sv', 'se', 'ca', 'tr', 'hr', 'zh', 'zh_tw', 'zh_cn', 'hu');
         // CHECK FOR LOCALE
         if (in_array($sytem_locale, $available_locales)) {
             $atts['w_language'] = $sytem_locale;
         }
         // CHECK FOR LOCALE BY FIRST TWO DIGITS
         if (in_array(substr($sytem_locale, 0, 2), $available_locales)) {
             $atts['w_language'] = substr($sytem_locale, 0, 2);
         }
     }
     $cache_key = strtolower($atts['w_location'] . '_' . $atts['w_language'] . '_' . $weather_data['current_unit']);
     if (td_remote_cache::is_expired(__CLASS__, $cache_key) === true) {
         // cache is expired - do a request
         $today_api_data = self::owm_get_today_data($atts, $weather_data);
         $forecast_api_data = self::owm_get_five_days_data($atts, $weather_data);
         // check the api call response
         if ($today_api_data !== true or $forecast_api_data !== true) {
             // we have an error on one of the apis
             $weather_data = td_remote_cache::get(__CLASS__, $cache_key);
             if ($weather_data === false) {
                 // miss and io error... shit / die
                 return self::error('Weather API error: ' . $today_api_data . ' ' . $forecast_api_data);
             }
             td_remote_cache::extend(__CLASS__, $cache_key, self::$caching_time);
             return 'api_fail_cache';
         }
         td_remote_cache::set(__CLASS__, $cache_key, $weather_data, self::$caching_time);
         //we have a reply and we set it
         return 'api';
     } else {
         // cache is valid
         $weather_data = td_remote_cache::get(__CLASS__, $cache_key);
         return 'cache';
     }
 }
예제 #2
0
 /**
  * @internal
  * disables the cache. Warning it also CLEARS the cache
  */
 static function _disable_cache()
 {
     delete_option(TD_THEME_OPTIONS_NAME . '_remote_cache');
     self::$is_cache_enabled = false;
 }