class-vi-w2s-activator.php
2 years ago
class-vi-w2s-deactivator.php
4 years ago
class-vi-w2s-i18n.php
1 year ago
class-vi-w2s-loader.php
4 years ago
class-vi-w2s.php
5 months ago
class-viw2s-api-ajax-handler.php
5 months ago
class-viw2s-api-settings.php
5 months ago
class-viw2s-oauth-handler.php
5 months ago
class-viw2s-oauth-token-refresh-cron.php
5 months ago
data.php
5 months ago
support.php
1 week ago
class-viw2s-oauth-token-refresh-cron.php
105 lines
| 1 | <?php |
| 2 | /** |
| 3 | * OAuth Token Refresh Cron |
| 4 | * |
| 5 | * Automatically refreshes OAuth tokens every 18 hours to prevent expiration |
| 6 | * |
| 7 | * @package w2s-migrate-woo-to-shopify |
| 8 | * @subpackage w2s-migrate-woo-to-shopify/includes |
| 9 | */ |
| 10 | |
| 11 | if ( ! defined( 'ABSPATH' ) ) { |
| 12 | exit; |
| 13 | } |
| 14 | |
| 15 | class Viw2s_OAuth_Token_Refresh_Cron { |
| 16 | |
| 17 | const CRON_HOOK = 'viw2s_oauth_token_refresh'; |
| 18 | const CRON_INTERVAL = 64800; |
| 19 | |
| 20 | public static function init() { |
| 21 | add_filter( 'cron_schedules', array( __CLASS__, 'add_cron_interval' ) ); |
| 22 | add_action( self::CRON_HOOK, array( __CLASS__, 'refresh_all_tokens' ) ); |
| 23 | |
| 24 | if ( ! wp_next_scheduled( self::CRON_HOOK ) ) { |
| 25 | wp_schedule_event( time(), 'viw2s_18hours', self::CRON_HOOK ); |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Add custom cron interval |
| 31 | * |
| 32 | * @param array $schedules Existing schedules |
| 33 | * @return array Modified schedules |
| 34 | */ |
| 35 | public static function add_cron_interval( $schedules ) { |
| 36 | $schedules['viw2s_18hours'] = array( |
| 37 | 'interval' => self::CRON_INTERVAL, |
| 38 | 'display' => esc_html__( 'Every 18 Hours (OAuth Token Refresh)', 'w2s-migrate-woo-to-shopify' ), |
| 39 | ); |
| 40 | return $schedules; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Refresh all OAuth tokens |
| 45 | */ |
| 46 | public static function refresh_all_tokens() { |
| 47 | $all_settings = Viw2s_API_Settings::get_settings(); |
| 48 | |
| 49 | if ( empty( $all_settings ) || ! is_array( $all_settings ) ) { |
| 50 | return; |
| 51 | } |
| 52 | |
| 53 | foreach ( $all_settings as $shop_domain => $settings ) { |
| 54 | if ( ( $settings['api_type'] ?? 'legacy' ) !== 'oauth' ) { |
| 55 | continue; |
| 56 | } |
| 57 | |
| 58 | if ( empty( $settings['client_id'] ) || empty( $settings['client_secret'] ) ) { |
| 59 | continue; |
| 60 | } |
| 61 | |
| 62 | $credentials = array( |
| 63 | 'shop_domain' => $shop_domain, |
| 64 | 'client_id' => $settings['client_id'], |
| 65 | 'client_secret' => Viw2s_OAuth_Handler::decrypt_secret( $settings['client_secret'] ), |
| 66 | ); |
| 67 | |
| 68 | $result = Viw2s_OAuth_Handler::refresh_access_token( $credentials ); |
| 69 | |
| 70 | if ( is_wp_error( $result ) ) { |
| 71 | continue; |
| 72 | } |
| 73 | |
| 74 | Viw2s_API_Settings::save_settings( $shop_domain, array( |
| 75 | 'access_token' => $result['access_token'], |
| 76 | 'token_expires_at' => $result['token_expires_at'], |
| 77 | 'token_scope' => $result['scope'], |
| 78 | ) ); |
| 79 | |
| 80 | $old_settings = get_option( 'viw2s_params', [] ); |
| 81 | if ( isset( $old_settings['viw2s_store_setting'] ) && is_array( $old_settings['viw2s_store_setting'] ) ) { |
| 82 | foreach ( $old_settings['viw2s_store_setting'] as &$store ) { |
| 83 | if ( ( $store['domain'] ?? '' ) === $shop_domain || ( $store['shop_domain'] ?? '' ) === $shop_domain ) { |
| 84 | $store['access_token'] = $result['access_token']; |
| 85 | $store['token_expires_at'] = $result['token_expires_at']; |
| 86 | break; |
| 87 | } |
| 88 | } |
| 89 | update_option( 'viw2s_params', $old_settings ); |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | public static function manual_refresh() { |
| 95 | self::refresh_all_tokens(); |
| 96 | } |
| 97 | |
| 98 | public static function deactivate() { |
| 99 | $timestamp = wp_next_scheduled( self::CRON_HOOK ); |
| 100 | if ( $timestamp ) { |
| 101 | wp_unschedule_event( $timestamp, self::CRON_HOOK ); |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 |