| @@ -37,21 +37,12 @@ | ||
| 37 | 37 | /** |
| 38 | 38 | * Filters the HTTP headers user agent string. |
| 39 | 39 | * |
| 40 | 40 | * @param string $user_agent The user agent string. |
| 41 | - * @param string $url The request URL. | |
| 42 | 41 | */ |
| 43 | - $user_agent = \apply_filters( 'http_headers_useragent', 'WordPress/' . get_masked_wp_version() . '; ' . \get_bloginfo( 'url' ), $url ); | |
| 44 | - | |
| 45 | - /** | |
| 46 | - * Filters the timeout duration for remote POST requests in ActivityPub. | |
| 47 | - * | |
| 48 | - * @param int $timeout The timeout value in seconds. Default 10 seconds. | |
| 49 | - */ | |
| 50 | - $timeout = \apply_filters( 'activitypub_remote_post_timeout', 10 ); | |
| 51 | - | |
| 52 | - $args = array( | |
| 53 | - 'timeout' => $timeout, | |
| 42 | + $user_agent = \apply_filters( 'http_headers_useragent', 'WordPress/' . get_masked_wp_version() . '; ' . \get_bloginfo( 'url' ) ); | |
| 43 | + $args = array( | |
| 44 | + 'timeout' => 100, | |
| 54 | 45 | 'limit_response_size' => 1048576, |
| 55 | 46 | 'redirection' => 3, |
| 56 | 47 | 'user-agent' => "$user_agent; ActivityPub", |
| 57 | 48 | 'headers' => array( |
| @@ -95,26 +86,13 @@ | ||
| 95 | 86 | /** |
| 96 | 87 | * Send a GET Request with the needed HTTP Headers. |
| 97 | 88 | * |
| 98 | 89 | * @param string $url The URL endpoint. |
| 99 | - * @param array $args Optional. Additional arguments to customize the request. | |
| 100 | - * - 'headers': Array of headers to override defaults. | |
| 101 | - * @param bool|int $cached Optional. Whether to return cached results, or cache duration. Default false. | |
| 90 | + * @param bool|int $cached Optional. Whether the result should be cached, or its duration. Default false. | |
| 102 | 91 | * |
| 103 | 92 | * @return array|\WP_Error The GET Response or a WP_Error. |
| 104 | 93 | */ |
| 105 | - public static function get( $url, $args = array(), $cached = false ) { | |
| 106 | - // Backward compatibility: if $args is boolean/int, it's the old $cached parameter. | |
| 107 | - if ( ! \is_array( $args ) ) { | |
| 108 | - \_deprecated_argument( | |
| 109 | - __METHOD__, | |
| 110 | - '7.9.0', | |
| 111 | - \esc_html__( 'The $cached parameter should now be passed as the third argument.', 'activitypub' ) | |
| 112 | - ); | |
| 113 | - $cached = $args; | |
| 114 | - $args = array(); | |
| 115 | - } | |
| 116 | - | |
| 94 | + public static function get( $url, $cached = false ) { | |
| 117 | 95 | /** |
| 118 | 96 | * Fires before an HTTP GET request is made. |
| 119 | 97 | * |
| 120 | 98 | * @param string $url The URL endpoint. |
| @@ -120,12 +98,11 @@ | ||
| 120 | 98 | * @param string $url The URL endpoint. |
| 121 | 99 | */ |
| 122 | 100 | \do_action( 'activitypub_pre_http_get', $url ); |
| 123 | 101 | |
| 124 | - $transient_key = self::generate_cache_key( $url ); | |
| 102 | + if ( $cached ) { | |
| 103 | + $transient_key = self::generate_cache_key( $url ); | |
| 125 | 104 | |
| 126 | - // Check cache only if caching is requested. | |
| 127 | - if ( $cached ) { | |
| 128 | 105 | $response = \get_transient( $transient_key ); |
| 129 | 106 | |
| 130 | 107 | if ( $response ) { |
| 131 | 108 | /** |
| @@ -142,21 +119,23 @@ | ||
| 142 | 119 | |
| 143 | 120 | /** |
| 144 | 121 | * Filters the HTTP headers user agent string. |
| 145 | 122 | * |
| 123 | + * This filter allows developers to modify the user agent string that is | |
| 124 | + * sent with HTTP requests. | |
| 125 | + * | |
| 146 | 126 | * @param string $user_agent The user agent string. |
| 147 | - * @param string $url The request URL. | |
| 148 | 127 | */ |
| 149 | - $user_agent = \apply_filters( 'http_headers_useragent', 'WordPress/' . get_masked_wp_version() . '; ' . \get_bloginfo( 'url' ), $url ); | |
| 128 | + $user_agent = \apply_filters( 'http_headers_useragent', 'WordPress/' . get_masked_wp_version() . '; ' . \get_bloginfo( 'url' ) ); | |
| 150 | 129 | |
| 151 | 130 | /** |
| 152 | 131 | * Filters the timeout duration for remote GET requests in ActivityPub. |
| 153 | 132 | * |
| 154 | - * @param int $timeout The timeout value in seconds. Default 10 seconds. | |
| 133 | + * @param int $timeout The timeout value in seconds. Default 100 seconds. | |
| 155 | 134 | */ |
| 156 | - $timeout = \apply_filters( 'activitypub_remote_get_timeout', 10 ); | |
| 135 | + $timeout = \apply_filters( 'activitypub_remote_get_timeout', 100 ); | |
| 157 | 136 | |
| 158 | - $defaults = array( | |
| 137 | + $args = array( | |
| 159 | 138 | 'timeout' => $timeout, |
| 160 | 139 | 'limit_response_size' => 1048576, |
| 161 | 140 | 'redirection' => 3, |
| 162 | 141 | 'user-agent' => "$user_agent; ActivityPub", |
| @@ -168,34 +147,13 @@ | ||
| 168 | 147 | 'key_id' => Actors::get_by_id( Actors::APPLICATION_USER_ID )->get_id() . '#main-key', |
| 169 | 148 | 'private_key' => Actors::get_private_key( Actors::APPLICATION_USER_ID ), |
| 170 | 149 | ); |
| 171 | 150 | |
| 172 | - $args = \wp_parse_args( $args, $defaults ); | |
| 173 | - $args['headers'] = \wp_parse_args( $args['headers'], $defaults['headers'] ); | |
| 174 | - | |
| 175 | 151 | $response = \wp_safe_remote_get( $url, $args ); |
| 176 | 152 | $code = \wp_remote_retrieve_response_code( $response ); |
| 177 | 153 | |
| 178 | - if ( \is_wp_error( $response ) || $code >= 400 ) { | |
| 179 | - if ( ! $code ) { | |
| 180 | - $code = 0; | |
| 181 | - } | |
| 154 | + if ( $code >= 400 ) { | |
| 182 | 155 | $response = new \WP_Error( $code, __( 'Failed HTTP Request', 'activitypub' ), array( 'status' => $code ) ); |
| 183 | - | |
| 184 | - /* | |
| 185 | - * Always cache errors to prevent repeated timeout waits. | |
| 186 | - * - Retriable errors (timeouts, 5xx): 1 minute (server may recover quickly). | |
| 187 | - * - Other errors (4xx): 15 minutes (client errors are more permanent). | |
| 188 | - */ | |
| 189 | - if ( \in_array( $code, ACTIVITYPUB_RETRY_ERROR_CODES, true ) || 0 === $code ) { | |
| 190 | - $cache_duration = MINUTE_IN_SECONDS; | |
| 191 | - } else { | |
| 192 | - $cache_duration = 15 * MINUTE_IN_SECONDS; | |
| 193 | - } | |
| 194 | - | |
| 195 | - \set_transient( $transient_key, $response, $cache_duration ); | |
| 196 | - | |
| 197 | - return $response; | |
| 198 | 156 | } |
| 199 | 157 | |
| 200 | 158 | /** |
| 201 | 159 | * Action to save the response of the remote GET request. |
| @@ -204,14 +162,15 @@ | ||
| 204 | 162 | * @param string $url The URL endpoint. |
| 205 | 163 | */ |
| 206 | 164 | \do_action( 'activitypub_safe_remote_get_response', $response, $url ); |
| 207 | 165 | |
| 208 | - // Always cache successful responses. | |
| 209 | - $cache_duration = $cached; | |
| 210 | - if ( ! is_int( $cache_duration ) ) { | |
| 211 | - $cache_duration = HOUR_IN_SECONDS; | |
| 166 | + if ( $cached ) { | |
| 167 | + $cache_duration = $cached; | |
| 168 | + if ( ! is_int( $cache_duration ) ) { | |
| 169 | + $cache_duration = HOUR_IN_SECONDS; | |
| 170 | + } | |
| 171 | + \set_transient( $transient_key, $response, $cache_duration ); | |
| 212 | 172 | } |
| 213 | - \set_transient( $transient_key, $response, $cache_duration ); | |
| 214 | 173 | |
| 215 | 174 | return $response; |
| 216 | 175 | } |
| 217 | 176 | |
| @@ -290,9 +249,9 @@ | ||
| 290 | 249 | ) |
| 291 | 250 | ); |
| 292 | 251 | } |
| 293 | 252 | |
| 294 | - $response = self::get( $url, array(), $cached ); | |
| 253 | + $response = self::get( $url, $cached ); | |
| 295 | 254 | |
| 296 | 255 | if ( \is_wp_error( $response ) ) { |
| 297 | 256 | return $response; |
| 298 | 257 | } |