Plugin Directory

Changeset 3097742


Ignore:
Timestamp:
06/05/2024 07:27:50 AM (21 months ago)
Author:
hullcode
Message:

Using transient instead of file storage to save cache

Location:
weather-in-any-city-widget/trunk
Files:
1 deleted
2 edited

Legend:

Unmodified
Added
Removed
  • weather-in-any-city-widget/trunk/README.txt

    r3097418 r3097742  
    44Tested up to: 6.5
    55Requires PHP: 5.3
    6 Stable tag: 1.1.36
     6Stable tag: 1.1.40
    77License: GPLv2 or later
    88
  • weather-in-any-city-widget/trunk/weather-in-any-city-widget.php

    r3097418 r3097742  
    44 * Plugin URI: https://weatherin.org
    55 * Description: Weather Widget Pro provides a complete weather forecast for any location around the world.
    6  * Version: 1.1.36
     6 * Version: 1.1.40
    77 * Author: El tiempo
    88 * Author URI: https://eltiempoen.com
     
    1616defined( 'ABSPATH' ) or die( 'ABSPATH not defined' );
    1717
    18 define ('WIYCW_VERSION', '1.1.36');
     18define ('WIYCW_VERSION', '1.1.40');
    1919define ('WIYCW_DEF_PLUGIN', 'weather-in-any-city-widget');
    2020define ('WIYCW_DEF_BASEURL', plugins_url('', __FILE__));
     
    921921function WIYCW_get_weather(){
    922922
    923     try {
    924         $response = array();
    925         $response['error'] = false;
    926 
    927         if(isset($_GET['id'])) {
    928 
    929             $id = intval(sanitize_text_field($_GET['id']));
    930             $cache_file = dirname(__FILE__)  .'/cache/' . $id . '.txt';
    931             $cache_minutes = 15;
    932 
    933             if(file_exists($cache_file) && (filemtime($cache_file) > (time() - ($cache_minutes * 60)))) {
    934                 wp_send_json(json_decode(unserialize(file_get_contents($cache_file))));
    935                 die();
     923    $error = array('error' => true, 'message' => '');
     924
     925    if(!isset($_GET['id'])){
     926        $error['message'] = 'ID not set';
     927        wp_send_json($error);
     928        die();
     929    }
     930
     931
     932    $id = intval(sanitize_text_field($_GET['id']));
     933    $cache_key = substr('wiycw_'.$id, 0, 44);
     934    $response = get_transient($cache_key);
     935
     936    if(empty($response)) {
     937
     938        $params = array(
     939         'id' => $id,
     940         's' => get_site_url(),
     941         'v' => WIYCW_VERSION,
     942         'c' => 'true'
     943        );
     944
     945        $url = add_query_arg($params, 'https://eltiempoen.com/api/weather/widget');
     946        $arg = array('sslverify' => false, 'timeout' => 10);
     947
     948        $request = wp_remote_get($url, $arg);
     949
     950        if(!is_wp_error($request) && ($request['response']['code'] == 200 || $request['response']['code'] == 201)) {
     951           $response = wp_remote_retrieve_body($request);
     952           set_transient($cache_key, $response, MINUTE_IN_SECONDS * 30);
     953        }else{
     954            if(is_wp_error($request)){
     955                $error['message'] = $request->get_error_message();
     956            }else{
     957                $error['message'] = 'Response code: '.$request['response']['code'];
    936958            }
    937 
    938             $params = array(
    939              'id' => $id,
    940              's' => get_site_url(),
    941              'v' => WIYCW_VERSION,
    942              'c' => 'true'
    943             );
    944 
    945             $url = add_query_arg($params, 'https://eltiempoen.com/api/weather/widget');
    946             $arg = array('sslverify' => false, 'timeout' => 10);
    947 
    948             $request = wp_remote_get($url, $arg);
    949 
    950              if(!is_wp_error($request) && ($request['response']['code'] == 200 || $request['response']['code'] == 201)) {
    951                $response = wp_remote_retrieve_body($request);
    952                file_put_contents($cache_file, serialize($response), LOCK_EX);
    953                wp_send_json(json_decode($response));
    954             }else{
    955                 $response['error'] = true;
    956                 if(is_wp_error($request)){
    957                     $response['message'] = $request->get_error_message();
    958                 }else{
    959                     $response['message'] = 'Response code: '.$request['response']['code'];
    960                 }
    961                 wp_send_json($response);
    962             }
    963 
    964         } else {
    965             $response['error'] = true;
    966             $response['message'] = 'ID not set';
    967             wp_send_json($response);
     959           
     960            wp_send_json($error);
     961            die();
    968962        }
    969    
    970     } catch (Exception $e) {
    971         echo 'Caught exception: ',  $e->getMessage(), "\n";
     963
    972964    }
    973        
     965
     966    wp_send_json(json_decode($response));
     967
    974968}
    975969
Note: See TracChangeset for help on using the changeset viewer.