Skip to content

Commit

Permalink
Updates for AccessToken logic (#48)
Browse files Browse the repository at this point in the history
* Don't refresh token on AJAX calls

* Always return the JSON response, regardless of status code

* Ensure that errors are logged

* Bump version to 2.2.1

* Update .pot file
  • Loading branch information
wpscholar authored Sep 21, 2020
1 parent 1c8fe6f commit 3b2f94b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 14 deletions.
4 changes: 2 additions & 2 deletions bluehost-wordpress-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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__ ) );

Expand Down
16 changes: 12 additions & 4 deletions inc/AccessToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand All @@ -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() {

Expand Down Expand Up @@ -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
Expand All @@ -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();
}
Expand Down
12 changes: 5 additions & 7 deletions inc/ResponseUtilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
2 changes: 1 addition & 1 deletion languages/bluehost-wordpress-plugin.pot
Original file line number Diff line number Diff line change
Expand Up @@ -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 <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
Expand Down

0 comments on commit 3b2f94b

Please sign in to comment.