diff --git a/includes/api/class-bc-api.php b/includes/api/class-bc-api.php index b6cecebe..30326bee 100644 --- a/includes/api/class-bc-api.php +++ b/includes/api/class-bc-api.php @@ -56,7 +56,6 @@ protected function get_account_id() { global $bc_accounts; return $bc_accounts->get_account_id(); - } /** @@ -90,7 +89,6 @@ public function get_last_error() { } return end( array_values( $this->errors ) ); - } /** @@ -98,9 +96,10 @@ public function get_last_error() { * * @param string $url the url to call * @param array $args the arguments to pass to the API call + * @param bool $cache_fail_response whether or not to cache the api fail response * @return array|false|mixed|WP_Error */ - private function cached_get( $url, $args ) { + private function cached_get( $url, $args, $cache_fail_response = false ) { global $bc_accounts; @@ -113,7 +112,9 @@ private function cached_get( $url, $args ) { $account_id = $bc_accounts->get_account_id(); $transient_key = BC_Utility::generate_transient_key( '_brightcove_req_', $account_id . BC_Utility::get_hash_for_object( $url ) ); $request = BC_Utility::get_cache_item( $transient_key ); - if ( false === $request ) { + $force_refresh = ! empty( $_GET['bc_refresh'] ) && '1' === $_GET['bc_refresh'] && ! empty( $_GET['nonce'] ) && wp_verify_nonce( $_GET['nonce'], 'bc_refresh' ); + + if ( false === $request || $force_refresh ) { if ( function_exists( 'vip_safe_wp_remote_get' ) ) { $request = vip_safe_wp_remote_get( $url, '', 3, 3, 20, $args ); } else { @@ -121,7 +122,20 @@ private function cached_get( $url, $args ) { } $successful_response_codes = array( 200, 201, 202, 204 ); - if ( in_array( wp_remote_retrieve_response_code( $request ), $successful_response_codes, true ) ) { + /** + * Filter whether to cache the api fail response. + * + * @since 2.8.5 + * @hook brightcove_cache_api_fail_response + * @param {bool} $cache_fail_response Whether to cache the response. + * @param {string} $url The URL of the request. + * @param {array} $args The arguments of the request. + * + * @return bool Whether to cache the fail response. + */ + $cache_fail_response = apply_filters( 'brightcove_cache_api_fail_response', $cache_fail_response, $url, $args ); + + if ( in_array( wp_remote_retrieve_response_code( $request ), $successful_response_codes, true ) || $cache_fail_response ) { BC_Utility::set_cache_item( $transient_key, '', $request, $cache_time_in_seconds ); } } @@ -139,10 +153,11 @@ private function cached_get( $url, $args ) { * @param string $method the http method to use * @param array $data array of further data to send to the server * @param boolean $force_new_token whether or not to force obtaining a new oAuth token + * @param boolean $cache_fail_response whether or not to cache the api fail response * * @return mixed the return data from the call of false if a failure occurred */ - protected function send_request( $url, $method = 'GET', $data = array(), $force_new_token = false ) { + protected function send_request( $url, $method = 'GET', $data = array(), $force_new_token = false, $cache_fail_response = false ) { $method = strtoupper( sanitize_text_field( $method ) ); @@ -206,7 +221,7 @@ protected function send_request( $url, $method = 'GET', $data = array(), $force_ switch ( $method ) { case 'GET': - $request = $this->cached_get( $url, $args ); + $request = $this->cached_get( $url, $args, $cache_fail_response ); break; @@ -225,7 +240,6 @@ protected function send_request( $url, $method = 'GET', $data = array(), $force_ $request = wp_remote_request( $url, $args ); break; - } if ( 401 === wp_remote_retrieve_response_code( $request ) ) { @@ -266,11 +280,9 @@ protected function send_request( $url, $method = 'GET', $data = array(), $force_ if ( isset( $body[0] ) && isset( $body[0]['error_code'] ) ) { $message = $body[0]['error_code']; - } elseif ( isset( $body['message'] ) ) { $message = $body['message']; - } $this->errors[] = array( @@ -285,18 +297,17 @@ protected function send_request( $url, $method = 'GET', $data = array(), $force_ if ( 204 === wp_remote_retrieve_response_code( $request ) ) { return true; - } if ( is_wp_error( $request ) ) { - $this->errors[] = array( - 'url' => $url, - 'error' => $request, - ); + $this->errors[] = array( + 'url' => $url, + 'error' => $request, + ); - BC_Logging::log( sprintf( 'BC API ERROR: %s', $request->get_error_message() ) ); + BC_Logging::log( sprintf( 'BC API ERROR: %s', $request->get_error_message() ) ); - return false; + return false; } if ( $transient_key && $body && ( ! defined( 'WP_DEBUG' ) || false === WP_DEBUG ) ) { @@ -305,7 +316,6 @@ protected function send_request( $url, $method = 'GET', $data = array(), $force_ } return $body; - } /** @@ -340,6 +350,5 @@ protected function get_authorization_header( $force_new_token = false ) { } return 'Bearer ' . $token; - } } diff --git a/includes/api/class-bc-player-management-api2.php b/includes/api/class-bc-player-management-api2.php index eda1837b..395723c8 100644 --- a/includes/api/class-bc-player-management-api2.php +++ b/includes/api/class-bc-player-management-api2.php @@ -64,7 +64,7 @@ public function get_all_players() { $bc_accounts->set_current_account_by_id( $account_id ); $url = esc_url_raw( self::BASE_URL . $account_id . '/players/' ); - $account_players = $this->send_request( $url ); + $account_players = $this->send_request( $url, 'GET', [], false, true ); if ( ! $account_players || is_wp_error( $account_players ) ) { return []; diff --git a/includes/class-bc-setup.php b/includes/class-bc-setup.php index 7957e830..aaa0068e 100644 --- a/includes/class-bc-setup.php +++ b/includes/class-bc-setup.php @@ -495,12 +495,20 @@ public static function bc_admin_notices() { } if ( count( $bc_accounts->get_sanitized_all_accounts() ) > 0 && empty( $players ) && is_array( $players ) ) { + $force_refresh_url = add_query_arg( + [ + 'bc_refresh' => 1, + 'nonce' => wp_create_nonce( 'bc_refresh' ), + ], + admin_url( 'admin.php?page=brightcove-sources' ) + ); + $notices[] = array( 'message' => sprintf( - '%s %s', - esc_html__( 'It looks like one or more of your accounts API authentication changed recently. Please update your settings ', 'brightcove' ), + // translators: %1$s: Update settings URL, %2$s: Force refresh URL. + esc_html__( 'It looks like one or more of your accounts API authentication has changed recently. Please update your settings here. Or click here to try again.', 'brightcove' ), esc_url( admin_url( 'admin.php?page=brightcove-sources' ) ), - esc_html__( 'here', 'brightcove' ) + esc_url( $force_refresh_url ) ), 'type' => 'error', ); diff --git a/includes/class-bc-utility.php b/includes/class-bc-utility.php index d1261634..a29a0fc0 100644 --- a/includes/class-bc-utility.php +++ b/includes/class-bc-utility.php @@ -745,13 +745,7 @@ public static function set_cache_item( $key, $type, $value, $expiration = 600 ) $type = sanitize_text_field( $type ); $expiration = absint( $expiration ); - $transient_value = get_transient( $key ); - - if ( false === $transient_value ) { - return set_transient( $key, $value, $expiration ); - } - - return true; // already cached + return set_transient( $key, $value, $expiration ); } /**