| @@ -1,18 +1,15 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | namespace ImageOptimization\Modules\ConnectManager\Components; |
| 3 | 3 | |
| 4 | 4 | use ImageOptimization\Modules\Connect\{ |
| 5 | - Module as ConnectModule, | |
| 6 | - Classes\Data, | |
| 7 | - Classes\Service, | |
| 8 | - Classes\Utils as ConnectUtils, | |
| 9 | - Rest\Authorize, | |
| 10 | - Rest\Version | |
| 5 | + Module as Connect_Module, | |
| 11 | 6 | }; |
| 12 | 7 | |
| 8 | +use ImageOptimization\Modules\Settings\Classes\Settings; | |
| 13 | 9 | use ImageOptimization\Classes\Logger; |
| 14 | 10 | use ImageOptimization\Classes\Utils; |
| 11 | +use ImageOptimization\Classes\Client\Client; | |
| 15 | 12 | use Throwable; |
| 16 | 13 | |
| 17 | 14 | if ( ! defined( 'ABSPATH' ) ) { |
| 18 | 15 | exit; // exit if accessed directly |
| @@ -18,21 +15,22 @@ | ||
| 18 | 15 | exit; // exit if accessed directly |
| 19 | 16 | } |
| 20 | 17 | |
| 21 | 18 | class Connect implements Connect_Interface { |
| 22 | - | |
| 23 | 19 | const STATUS_CHECK_TRANSIENT = 'image_optimizer_status_check'; |
| 20 | + const STATUS_CHECK_ERROR_TRANSIENT = 'image_optimizer_status_check_error'; | |
| 21 | + const ERROR_CACHE_DURATION = MINUTE_IN_SECONDS * 5; | |
| 24 | 22 | |
| 25 | - public function is_connected() : bool { | |
| 26 | - return ConnectModule::is_connected(); | |
| 23 | + public function is_connected(): bool { | |
| 24 | + return Connect_Module::is_connected(); | |
| 27 | 25 | } |
| 28 | 26 | |
| 29 | - public function is_activated() : bool { | |
| 30 | - return ConnectModule::is_connected(); | |
| 27 | + public function is_activated(): bool { | |
| 28 | + return Connect_Module::is_connected(); | |
| 31 | 29 | } |
| 32 | 30 | |
| 33 | - public function is_valid_home_url() : bool { | |
| 34 | - return ConnectUtils::is_valid_home_url(); | |
| 31 | + public function is_valid_home_url(): bool { | |
| 32 | + return Connect_Module::get_connect()->utils()->is_valid_home_url(); | |
| 35 | 33 | } |
| 36 | 34 | |
| 37 | 35 | public function get_connect_status() { |
| 38 | 36 | if ( ! $this->is_connected() ) { |
| @@ -45,8 +43,15 @@ | ||
| 45 | 43 | if ( $cached_status ) { |
| 46 | 44 | return $cached_status; |
| 47 | 45 | } |
| 48 | 46 | |
| 47 | + $cached_error = get_transient( self::STATUS_CHECK_ERROR_TRANSIENT ); | |
| 48 | + | |
| 49 | + if ( $cached_error ) { | |
| 50 | + Logger::debug( 'Status check skipped due to recent error: ' . $cached_error ); | |
| 51 | + return null; | |
| 52 | + } | |
| 53 | + | |
| 49 | 54 | try { |
| 50 | 55 | $response = Utils::get_api_client()->make_request( |
| 51 | 56 | 'POST', |
| 52 | 57 | 'status/check' |
| @@ -51,26 +56,39 @@ | ||
| 51 | 56 | 'POST', |
| 52 | 57 | 'status/check' |
| 53 | 58 | ); |
| 54 | 59 | } catch ( Throwable $t ) { |
| 55 | - Logger::log( | |
| 56 | - Logger::LEVEL_ERROR, | |
| 57 | - 'Status check error. Reason: ' . $t->getMessage() | |
| 58 | - ); | |
| 60 | + $error_message = $t->getMessage(); | |
| 59 | 61 | |
| 62 | + Logger::error( 'Status check error. Reason: ' . $error_message ); | |
| 63 | + | |
| 64 | + set_transient( self::STATUS_CHECK_ERROR_TRANSIENT, $error_message, self::ERROR_CACHE_DURATION ); | |
| 65 | + | |
| 60 | 66 | return null; |
| 61 | 67 | } |
| 62 | 68 | |
| 63 | 69 | if ( ! isset( $response->status ) ) { |
| 64 | - Logger::log( Logger::LEVEL_ERROR, 'Invalid response from server' ); | |
| 70 | + $error_message = 'Invalid response from server'; | |
| 65 | 71 | |
| 72 | + Logger::error( $error_message ); | |
| 73 | + | |
| 74 | + set_transient( self::STATUS_CHECK_ERROR_TRANSIENT, $error_message, self::ERROR_CACHE_DURATION ); | |
| 75 | + | |
| 66 | 76 | return null; |
| 67 | 77 | } |
| 68 | 78 | |
| 69 | - if ( ! empty( $response->site_url ) && Data::get_home_url() !== $response->site_url ) { | |
| 70 | - Data::set_home_url( $response->site_url ); | |
| 79 | + if ( ! empty( $response->site_url ) && Connect_Module::get_connect()->data()->get_home_url() !== $response->site_url ) { | |
| 80 | + Connect_Module::get_connect()->data()->set_home_url( $response->site_url ); | |
| 71 | 81 | } |
| 72 | 82 | |
| 83 | + Settings::set( Settings::SUBSCRIPTION_ID, $response->subscription_id ); | |
| 84 | + | |
| 85 | + // Append subscription info to response | |
| 86 | + $subscription_info = Client::get_subscription_info(); | |
| 87 | + if ( $subscription_info ) { | |
| 88 | + $response->subscription_info = $subscription_info; | |
| 89 | + } | |
| 90 | + | |
| 73 | 91 | set_transient( self::STATUS_CHECK_TRANSIENT, $response, MINUTE_IN_SECONDS * 5 ); |
| 74 | 92 | |
| 75 | 93 | return $response; |
| 76 | 94 | } |
| @@ -117,9 +135,9 @@ | ||
| 117 | 135 | |
| 118 | 136 | set_transient( self::STATUS_CHECK_TRANSIENT, $connect_status, MINUTE_IN_SECONDS * 5 ); |
| 119 | 137 | } |
| 120 | 138 | |
| 121 | - public function get_activation_state() : string { | |
| 139 | + public function get_activation_state(): string { | |
| 122 | 140 | /** |
| 123 | 141 | * Returning true because the license key is |
| 124 | 142 | * not used for deactivation in connect. |
| 125 | 143 | */ |
| @@ -126,18 +144,17 @@ | ||
| 126 | 144 | return true; |
| 127 | 145 | } |
| 128 | 146 | |
| 129 | 147 | public function get_access_token() { |
| 130 | - return Data::get_access_token(); | |
| 131 | - | |
| 148 | + return Connect_Module::get_connect()->data()->get_access_token(); | |
| 132 | 149 | } |
| 133 | 150 | |
| 134 | 151 | public function get_client_id(): string { |
| 135 | - return Data::get_client_id(); | |
| 152 | + return Connect_Module::get_connect()->data()->get_client_id(); | |
| 136 | 153 | } |
| 137 | 154 | |
| 138 | 155 | public function get_client_secret(): string { |
| 139 | - return Data::get_client_secret(); | |
| 156 | + return Connect_Module::get_connect()->data()->get_client_secret(); | |
| 140 | 157 | } |
| 141 | 158 | |
| 142 | 159 | public function images_left(): int { |
| 143 | 160 | $plan_data = $this->get_connect_status(); |
| @@ -152,34 +169,34 @@ | ||
| 152 | 169 | return max( $quota - $used_quota, 0 ); |
| 153 | 170 | } |
| 154 | 171 | |
| 155 | 172 | public function user_is_subscription_owner(): bool { |
| 156 | - return Data::user_is_subscription_owner(); | |
| 173 | + return Connect_Module::get_connect()->data()->user_is_subscription_owner(); | |
| 157 | 174 | } |
| 158 | 175 | |
| 159 | 176 | // Nonces. |
| 160 | 177 | public function connect_init_nonce(): string { |
| 161 | - return Authorize::NONCE_NAME; | |
| 178 | + return 'wp_rest'; | |
| 162 | 179 | } |
| 163 | 180 | |
| 164 | 181 | public function disconnect_nonce(): string { |
| 165 | - return Authorize::NONCE_NAME; | |
| 182 | + return 'wp_rest'; | |
| 166 | 183 | } |
| 167 | 184 | |
| 168 | 185 | public function deactivate_nonce(): string { |
| 169 | - return Authorize::NONCE_NAME; | |
| 186 | + return 'wp_rest'; | |
| 170 | 187 | } |
| 171 | 188 | |
| 172 | 189 | public function get_subscriptions_nonce(): string { |
| 173 | - return Authorize::NONCE_NAME; | |
| 190 | + return 'wp_rest'; | |
| 174 | 191 | } |
| 175 | 192 | |
| 176 | 193 | public function activate_nonce(): string { |
| 177 | - return Authorize::NONCE_NAME; | |
| 194 | + return 'wp_rest'; | |
| 178 | 195 | } |
| 179 | 196 | |
| 180 | 197 | public function version_nonce(): string { |
| 181 | - return Version::NONCE_NAME; | |
| 198 | + return 'wp_rest'; | |
| 182 | 199 | } |
| 183 | 200 | |
| 184 | 201 | public function get_is_connect_on_fly(): bool { |
| 185 | 202 | return true; |
| @@ -185,7 +202,11 @@ | ||
| 185 | 202 | return true; |
| 186 | 203 | } |
| 187 | 204 | |
| 188 | 205 | public function refresh_token(): void { |
| 189 | - Service::refresh_token(); | |
| 206 | + Connect_Module::get_connect()->service()->renew_access_token(); | |
| 207 | + } | |
| 208 | + | |
| 209 | + public function clear_error_cache(): void { | |
| 210 | + delete_transient( self::STATUS_CHECK_ERROR_TRANSIENT ); | |
| 190 | 211 | } |
| 191 | 212 | } |