diff --git a/bluehost-wordpress-plugin.php b/bluehost-wordpress-plugin.php index 5dc0034cb..754a08344 100644 --- a/bluehost-wordpress-plugin.php +++ b/bluehost-wordpress-plugin.php @@ -2,7 +2,7 @@ /** * Plugin Name: Bluehost * Description: This plugin integrates your WordPress site with the Bluehost control panel, including performance, security, and update features. - * Version: 2.2 + * Version: 2.2.1 * Requires at least: 4.7 * Requires PHP: 5.6 * Author: Bluehost @@ -27,7 +27,7 @@ } // Define constants -define( 'BLUEHOST_PLUGIN_VERSION', '2.2' ); +define( 'BLUEHOST_PLUGIN_VERSION', '2.2.1' ); define( 'BLUEHOST_PLUGIN_DIR', plugin_dir_path( __FILE__ ) ); define( 'BLUEHOST_PLUGIN_URL', plugin_dir_url( __FILE__ ) ); diff --git a/inc/AccessToken.php b/inc/AccessToken.php index c31b2f4cf..33c44c367 100644 --- a/inc/AccessToken.php +++ b/inc/AccessToken.php @@ -49,7 +49,7 @@ public static function get_token() { /** * Save an access token. * - * @param string $token Access token + * @param string $token Access token * @param int $expiration Timestamp of expiration */ public static function set_token( $token, $expiration ) { @@ -60,9 +60,9 @@ public static function set_token( $token, $expiration ) { /** * Request an access token. * - * @throws \Exception The random_bytes() function may throw an Exception in some cases. - * * @return array|\WP_Error + * + * @throws \Exception The random_bytes() function may throw an Exception in some cases. */ public static function request_token() { @@ -101,16 +101,21 @@ public static function request_token() { /** * Refresh the stored token. + * + * @throws \RuntimeException On error or unexpected payload shape. */ public static function refresh_token() { try { $response = self::request_token(); - $data = ResponseUtilities::parse_json_response( $response ); + $data = ResponseUtilities::parse_json_response( $response, true ); if ( isset( $data['access_token'], $data['expires_in'] ) ) { $token = $data['access_token']; $expires_in = (int) $data['expires_in']; $timestamp = ResponseUtilities::get_response_timestamp( $response ); self::set_token( $token, $timestamp + $expires_in ); + } else { + $data['_status_code'] = wp_remote_retrieve_response_code( $response ); + throw new \RuntimeException( wp_json_encode( $data ) ); } } catch ( \Exception $e ) { trigger_error( $e->getMessage() ); // phpcs:ignore @@ -121,6 +126,9 @@ public static function refresh_token() { * Check for proper conditions before refreshing the token. */ public static function maybe_refresh_token() { + if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { + return; + } if ( current_user_can( 'manage_options' ) && ! self::has_token() ) { self::refresh_token(); } diff --git a/inc/ResponseUtilities.php b/inc/ResponseUtilities.php index be77b7b21..a6d02ccd6 100644 --- a/inc/ResponseUtilities.php +++ b/inc/ResponseUtilities.php @@ -23,13 +23,11 @@ class ResponseUtilities { public static function parse_json_response( $response, $assoc_array = false ) { $data = array(); - if ( 200 === wp_remote_retrieve_response_code( $response ) ) { - $body = wp_remote_retrieve_body( $response ); - if ( $body ) { - $payload = json_decode( $body, $assoc_array ); - if ( $payload && is_array( $payload ) ) { - $data = (array) $payload; - } + $body = wp_remote_retrieve_body( $response ); + if ( $body ) { + $payload = json_decode( $body, $assoc_array ); + if ( $payload && is_array( $payload ) ) { + $data = (array) $payload; } } diff --git a/languages/bluehost-wordpress-plugin.pot b/languages/bluehost-wordpress-plugin.pot index 129c83a21..aa6d7cb2c 100644 --- a/languages/bluehost-wordpress-plugin.pot +++ b/languages/bluehost-wordpress-plugin.pot @@ -2,7 +2,7 @@ # This file is distributed under the GPLv2 or later. msgid "" msgstr "" -"Project-Id-Version: Bluehost 2.2\n" +"Project-Id-Version: Bluehost 2.2.1\n" "Report-Msgid-Bugs-To: https://github.com/bluehost/bluehost-wordpress-plugin/issues\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n"