Plugin Directory

Changeset 3096591


Ignore:
Timestamp:
06/03/2024 07:35:48 AM (21 months ago)
Author:
hullcode
Message:

Added cache

Location:
weather-in-any-city-widget/trunk
Files:
1 added
3 edited

Legend:

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

    r3095101 r3096591  
    44Tested up to: 6.5
    55Requires PHP: 5.3
    6 Stable tag: 1.1.31
     6Stable tag: 1.1.33
    77License: GPLv2 or later
    88
  • weather-in-any-city-widget/trunk/resources/js/WIYCW-widget.js

    r3039227 r3096591  
    104104
    105105
    106                     fetchJSONFile("https://eltiempoen.com:9090/api/widget?id="+widget.dataset.cityid+"&r="+r, function(data){
     106                    fetchJSONFile(widget.dataset.url+"?action="+widget.dataset.action+"&nonce="+widget.dataset.nonce+"&id="+widget.dataset.cityid, function(data){
    107107
    108108                        if(data.error){
  • weather-in-any-city-widget/trunk/weather-in-any-city-widget.php

    r3062472 r3096591  
    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.31
     6 * Version: 1.1.33
    77 * Author: El tiempo
    88 * Author URI: https://eltiempoen.com
     
    1616defined( 'ABSPATH' ) or die( 'ABSPATH not defined' );
    1717
    18 define ('WIYCW_VERSION', '1.1.31');
     18define ('WIYCW_VERSION', '1.1.33');
    1919define ('WIYCW_DEF_PLUGIN', 'weather-in-any-city-widget');
    2020define ('WIYCW_DEF_BASEURL', plugins_url('', __FILE__));
     
    2222
    2323add_action('init', 'WIYCW_textdomain');
     24
    2425
    2526function WIYCW_textdomain() {
     
    6162    $instance['url_es'] = !empty($instance['url_es']) ? $instance['url_es'] : "";
    6263    $instance['time_format'] = !empty($instance['time_format']) ? $instance['time_format'] : "universal";
     64    $instance['url'] = admin_url('admin-ajax.php');
     65    $instance['action'] = "WIYCW_get_weather";
    6366    return $instance;
    6467}
     
    806809    $output .= " data-textcolor='".esc_attr($instance['textColor'])."'";
    807810    $output .= " data-shadow='".esc_attr($instance['shadow'])."'";
     811    $output .= " data-url='".esc_attr($instance['url'])."'";
     812    $output .= " data-action='".esc_attr($instance['action'])."'";
     813    $output .= " data-nonce='".esc_attr(wp_create_nonce(WIYCW_VERSION))."'";
    808814    $output .= " data-bordercolor='".esc_attr($instance['borderColor'])."'";
    809815    $output .=  ">";
     
    912918add_action( 'init', 'WIYCW_shortcodes_init' );
    913919
     920function WIYCW_get_weather(){
     921
     922    $response = array();
     923    $response['error'] = false;
     924
     925    if(isset($_GET['id'])) {
     926
     927        $id = intval(sanitize_text_field($_GET['id']));
     928        $cache_file = dirname(__FILE__)  .'/cache/' . $id . '.txt';
     929        $cache_minutes = 15;
     930
     931        if(file_exists($cache_file) && (filemtime($cache_file) > (time() - ($cache_minutes * 60)))) {
     932            wp_send_json(json_decode(unserialize(file_get_contents($cache_file))));
     933            die();
     934        }
     935
     936        $url = 'https://eltiempoen.com:9090/api/widget?id='.$id.'&r='.base64_encode(get_site_url()); 
     937        $arg = array('sslverify' => false);
     938
     939        $request = wp_remote_get($url, $arg);
     940        if(is_wp_error($request)) {
     941            return false;
     942        }else{
     943            $response['error'] = true;
     944        }
     945
     946       $response = wp_remote_retrieve_body($request);
     947       file_put_contents($cache_file, serialize($response), LOCK_EX);
     948
     949    } else {
     950        $response['error'] = true;
     951       
     952    }
     953
     954   
     955    wp_send_json(json_decode($response));
     956}
     957
     958add_action('wp_ajax_WIYCW_get_weather', 'WIYCW_get_weather');
     959add_action('wp_ajax_nopriv_WIYCW_get_weather', 'WIYCW_get_weather');
     960
    914961?>
Note: See TracChangeset for help on using the changeset viewer.