15, 'user-agent' => self::user_agent(), 'headers' => array( 'Accept' => 'application/json' ), ) ); $response = wp_remote_get( $url, $args ); $code = is_wp_error( $response ) ? 0 : (int) wp_remote_retrieve_response_code( $response ); $data = null; if ( 200 === $code ) { $data = json_decode( wp_remote_retrieve_body( $response ), true ); } elseif ( 404 === $code ) { // "Not found" is a valid answer (e.g. no vulnerabilities recorded for a premium plugin). $data = array(); } if ( is_array( $data ) ) { set_transient( $key, $data, $ttl ); update_option( $key . '_stale', $data, false ); return $data; } $host = wp_parse_url( $url, PHP_URL_HOST ); self::$errors[] = sprintf( /* translators: 1: host, 2: error */ __( 'Could not reach %1$s (%2$s). Using last known data.', 'sunsetly' ), $host, is_wp_error( $response ) ? $response->get_error_message() : 'HTTP ' . $code ); // Back off for an hour to avoid hammering a failing API. $stale = get_option( $key . '_stale', null ); if ( is_array( $stale ) ) { set_transient( $key, $stale, HOUR_IN_SECONDS ); return $stale; } return null; } /** * User-agent for every outgoing request. Deliberately excludes the site address, * which WordPress' default user-agent would otherwise include. * * @return string */ public static function user_agent() { return 'Sunsetly/' . SUNSETLY_VERSION . '; WordPress/' . get_bloginfo( 'version' ); } /** * Delete all Sunsetly HTTP caches. */ public static function flush() { global $wpdb; // phpcs:ignore WordPress.DB.DirectDatabaseQuery $wpdb->query( "DELETE FROM {$wpdb->options} WHERE option_name LIKE '\_transient\_sunsetly\_http\_%' OR option_name LIKE '\_transient\_timeout\_sunsetly\_http\_%'" ); } }