Created
December 2, 2025 22:57
-
-
Save stingray82/2fea8c8427e6b843a0cd49e44250bd07 to your computer and use it in GitHub Desktop.
Hoster Updater.php - Set Transient on error
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public function request() { | |
| $remote = get_transient( $this->cache_key ); | |
| // If we have any cached value (success OR failure) take note. | |
| if ( false !== $remote ) { | |
| if ( ! is_array( $remote ) ) { | |
| $decoded = json_decode( $remote ); | |
| if ( json_last_error() === JSON_ERROR_NONE ) { | |
| $remote = $decoded; | |
| } | |
| } | |
| return $remote; | |
| } | |
| // No cache yet – do a live request. | |
| $response = wp_remote_get( | |
| $this->remote_url, | |
| array( | |
| 'timeout' => 10, | |
| 'headers' => array( 'Accept' => 'application/json' ), | |
| ) | |
| ); | |
| if ( | |
| is_wp_error($response) | |
| || 200 !== wp_remote_retrieve_response_code($response) | |
| || empty(wp_remote_retrieve_body($response)) | |
| ) { | |
| // lets add more details to the error log rather than just Hoster update check error: | |
| $debug = [ | |
| 'url' => $this->remote_url, | |
| 'http_code' => wp_remote_retrieve_response_code($response), | |
| 'body' => wp_remote_retrieve_body($response), | |
| 'wp_error' => is_wp_error($response) ? $response->get_error_message() : null, | |
| 'reason' => ( | |
| is_wp_error($response) ? 'wp_error' : | |
| (wp_remote_retrieve_response_code($response) !== 200 ? 'non_200_status' : | |
| (empty(wp_remote_retrieve_body($response)) ? 'empty_body' : | |
| 'unknown_error')) | |
| ), | |
| ]; | |
| error_log( $this->slug . ' Updater error: ' . json_encode($debug)); // Respects which plugin it is being used for i.e devkit/devkit.php | |
| // Prevent request spam – cache the failure for 1 hour | |
| set_transient($this->cache_key, json_encode(['error' => $debug]), HOUR_IN_SECONDS); | |
| return false; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment