From 14bb20c1eea15f0f63e9ff656063c66ca64f7de3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ant=C3=B3nio=20Gon=C3=A7alves?= Date: Tue, 27 Aug 2024 15:07:36 +0100 Subject: [PATCH] Stable version with subscription running on background --- includes/iotcat_subscription.php | 31 +++++++++++-------------- index.php | 40 +++++++------------------------- 2 files changed, 23 insertions(+), 48 deletions(-) diff --git a/includes/iotcat_subscription.php b/includes/iotcat_subscription.php index ae9629b..63b8d68 100644 --- a/includes/iotcat_subscription.php +++ b/includes/iotcat_subscription.php @@ -13,9 +13,12 @@ function __construct($token, $iotcat_elements_instances,$base_url ){ } - - private function get_tpi_elements_id($page_name){ - $response = wp_remote_get( $this->base_url.'/api/getTPIElementsId?access_token='.$this->token.'&pageName='.$page_name); + public function get_json($url, $attempt = 0){ + if($attempt > 10){ + iotcat_log_me("Invalid response from: ".$url); + return; + } + $response = wp_remote_get($url); if( is_array($response) && array_key_exists("response", $response) && @@ -24,25 +27,19 @@ private function get_tpi_elements_id($page_name){ $response["response"]["code"] === 200 ){ return json_decode( wp_remote_retrieve_body( $response ), true ); + }else{ - iotcat_log_me("Invalid response from getTPIElementsId"); + iotcat_log_me('Failed when trying to retrieve info from '.$url.' attempt number: '.($attempt + 1).' retrying'); + return $this->get_json($url, $attempt + 1); } } - private function get_tpi_element($page_name, $id){ - $response = wp_remote_get( $this->base_url.'/api/getTPIElement?access_token='.$this->token.'&pageName='.$page_name.'&id='.$id); - if( - is_array($response) && - array_key_exists("response", $response) && - is_array($response["response"]) && - array_key_exists("code", $response["response"]) && - $response["response"]["code"] === 200 - ){ - return json_decode( wp_remote_retrieve_body( $response ), true ); - }else{ - iotcat_log_me("Invalid response from getTPIElement"); - } + private function get_tpi_elements_id($page_name){ + return $this->get_json($this->base_url.'/api/getTPIElementsId?access_token='.$this->token.'&pageName='.$page_name); + } + private function get_tpi_element($page_name, $id){ + return $this->get_json( $this->base_url.'/api/getTPIElement?access_token='.$this->token.'&pageName='.$page_name.'&id='.$id); } diff --git a/index.php b/index.php index ada336e..5d6fe51 100644 --- a/index.php +++ b/index.php @@ -3,7 +3,7 @@ /** * Plugin Name: IoT Catalogue Integration * Description: Display in WordPress content from IoT Catalogue - * Version: 1.10.0-4 + * Version: 1.10.0 * Author: UNPARALLEL Innovation, Lda * Author URI: https://www.unparallel.pt */ @@ -39,9 +39,16 @@ add_action( 'async_subscribe', 'send_async_subscribe', 10, 2 ); function send_async_subscribe() { + global $iotcat_field_data_update_interval; iotcat_log_me("Starting subscription"); $current_subscription = get_option('iotcat_subscription_instance'); + if($current_subscription == false) { + return; + } $current_subscription -> get_data(); + iotcat_log_me("Data synchronized"); + $interval = intval($iotcat_field_data_update_interval)*60; + as_schedule_single_action(time() + $interval ,'async_subscribe', array() ); } @@ -144,6 +151,7 @@ function iotcat_destroy_current_subscription(){ $current_subscription = get_option('iotcat_subscription_instance'); if($current_subscription){ $current_subscription->destroy(); + as_unschedule_action( 'async_subscribe', array() ); delete_option('iotcat_subscription_instance'); } } @@ -172,36 +180,6 @@ function iotcat_updated_option($option, $old_value,$value){ add_action( 'updated_option', 'iotcat_updated_option', 10, 3 ); add_action( 'added_option', 'iotcat_added_option', 10, 2 ); -add_filter( 'cron_schedules', 'iotcat_add_update_interval' ); -function iotcat_add_update_interval($schedules){ - global $iotcat_field_data_update_interval; - $schedules['iotcat_update_interval'] = array( - 'interval' => intval($iotcat_field_data_update_interval)*60, - 'display' => esc_html__( "Every .$iotcat_field_data_update_interval minutes" ), ); - return $schedules; - -} - - -function iotcat_update_data_exec(){ - $user_id = get_option("iotcat_plugin_admin_user_id"); - if($user_id > 0){ - wp_set_current_user($user_id); - $current_subscription = get_option('iotcat_subscription_instance'); - if($current_subscription){ - iotcat_sync_data($current_subscription); - } - } - -} - -add_action( 'iotcat_update_data', 'iotcat_update_data_exec', 10, 0 ); - -if ( ! wp_next_scheduled( 'iotcat_update_data' ) ) { - - $current_time = time() + 10; - wp_schedule_event($current_time, 'iotcat_update_interval', 'iotcat_update_data' , array(), true); -} function iotcat_activate() {