Skip to content

Commit df58ef8

Browse files
committed
improve, remote get code and errors
1 parent edc669c commit df58ef8

File tree

1 file changed

+61
-25
lines changed

1 file changed

+61
-25
lines changed

vendor/nextgenthemes/wp-settings/includes/WP/fn-remote-get.php

+61-25
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
namespace Nextgenthemes\WP;
66

7+
use DateTime;
8+
use Exception;
79
use WP_Error;
810

9-
use function Nextgenthemes\ARVE\arve_errors;
10-
1111
/**
1212
* Retrieves JSON data from a remote URL.
1313
*
@@ -32,16 +32,17 @@ function remote_get_json_cached( string $url, array $args = array(), string $jso
3232

3333
try {
3434
$response = json_decode( $response, true, 128, JSON_THROW_ON_ERROR );
35-
} catch ( \Exception $e ) {
35+
} catch ( Exception $exception ) {
3636

3737
return new WP_Error(
3838
'json-decode-error',
3939
sprintf(
4040
// Translators: %1$s URL, %2$s json_decode error
41-
__( 'url: %1$s json_decode error: %2$s.', 'advanced-responsive-video-embedder' ),
41+
esc_html__( 'Remote get url: %1$s json_decode error: %2$s.', 'advanced-responsive-video-embedder' ),
4242
esc_html( $url ),
43-
$e->getMessage()
44-
)
43+
esc_html( $exception->getMessage() )
44+
),
45+
compact( 'url', 'response', 'exception' )
4546
);
4647
}
4748

@@ -50,12 +51,16 @@ function remote_get_json_cached( string $url, array $args = array(), string $jso
5051
return new WP_Error(
5152
'json-value-empty',
5253
sprintf(
53-
// Translators: 1 URL 2 JSON value
54-
__( 'url: %1$s JSON value <code>%2$s</code> does not exist or is empty. Full Json: %3$s', 'advanced-responsive-video-embedder' ),
54+
wp_kses(
55+
// Translators: 1 URL 2 JSON value
56+
__( 'url: %1$s JSON value <code>%2$s</code> does not exist or is empty.', 'advanced-responsive-video-embedder' ),
57+
array( 'code' => array() ),
58+
array( 'https' )
59+
),
5560
esc_html( $url ),
56-
esc_html( $json_name ),
57-
esc_html( $response )
58-
)
61+
esc_html( $json_name )
62+
),
63+
compact( 'url', 'json_name', 'response' )
5964
);
6065
} else {
6166
return $response[ $json_name ];
@@ -76,6 +81,7 @@ function remote_get_body( string $url, array $args = array() ) {
7681

7782
$response = wp_safe_remote_get( $url, $args );
7883
$response_code = wp_remote_retrieve_response_code( $response );
84+
$body = wp_remote_retrieve_body( $response );
7985

8086
if ( is_wp_error( $response ) ) {
8187
return $response;
@@ -87,27 +93,27 @@ function remote_get_body( string $url, array $args = array() ) {
8793
$response_code,
8894
sprintf(
8995
// Translators: 1 URL 2 HTTP response code.
90-
__( 'url: %1$s Status code 200 expected but was %2$s.', 'advanced-responsive-video-embedder' ),
91-
$url,
96+
esc_html__( 'url: %1$s Status code 200 expected but was %2$d.', 'advanced-responsive-video-embedder' ),
97+
esc_html( $url ),
9298
$response_code
93-
)
99+
),
100+
compact( 'url', 'response_code', 'response' )
94101
);
95102
}
96103

97-
$response = wp_remote_retrieve_body( $response );
98-
99-
if ( '' === $response ) {
104+
if ( '' === $body ) {
100105
return new WP_Error(
101106
'empty-body',
102107
sprintf(
103108
// Translators: URL.
104-
__( 'url: %s Empty Body.', 'advanced-responsive-video-embedder' ),
105-
$url
106-
)
109+
esc_html__( 'Remote get url: %s Empty Body.', 'advanced-responsive-video-embedder' ),
110+
esc_html( $url )
111+
),
112+
compact( 'url', 'response_code', 'response' )
107113
);
108114
}
109115

110-
return $response;
116+
return $body;
111117
}
112118

113119
/**
@@ -132,7 +138,8 @@ function remote_get_head( string $url, array $args = array() ) {
132138
__( 'url: %1$s Status code 200 expected but was %2$s.', 'advanced-responsive-video-embedder' ),
133139
$url,
134140
$response_code
135-
)
141+
),
142+
compact( 'url', 'response_code', 'response' )
136143
);
137144
}
138145

@@ -166,6 +173,8 @@ function remote_get_head_cached( string $url, array $args = array(), int $time =
166173
/**
167174
* Retrieves the body content from a remote URL, with caching for improved performance.
168175
*
176+
* TODO maybe use json to encode WP_Error to avoid WP using serialize
177+
*
169178
* @param string $url The URL of the remote resource.
170179
* @param array $args Optional. Additional arguments to include in the request.
171180
* @param int $time Optional. The duration in seconds to cache the response. Default is DAY_IN_SECONDS. 0 to disable caching.
@@ -192,10 +201,26 @@ function _remote_get_cached( string $url, array $args, int $time, string $type )
192201
if ( $time ) {
193202

194203
if ( is_wp_error( $response ) ) {
195-
$response->add(
196-
$response->get_error_code(),
197-
'This error is cached for ' . $time . ' seconds. If you delete the transient ' . $transient_name . ' the call will be made again.'
204+
205+
$code = $response->get_error_code();
206+
$msg = $response->get_error_message();
207+
$data = $response->get_error_data( $code );
208+
209+
$response->remove( $code );
210+
211+
$msg .= '<br>' . sprintf(
212+
wp_kses(
213+
// Translators: 1 Time in seconds, 2 Transient name.
214+
__( 'Error triggerd on %1$s and is cached for %2$d seconds. If you delete the transient <code>%3$s</code> the remote call will be made again.', 'advanced-responsive-video-embedder' ),
215+
array( 'code' => [] ),
216+
array( 'https' )
217+
),
218+
current_datetime()->format( DATETIME::ATOM ),
219+
$time,
220+
esc_html( $transient_name )
198221
);
222+
223+
$response->add( $code, $msg, $data );
199224
}
200225

201226
set_transient( $transient_name, $response, $time );
@@ -205,6 +230,17 @@ function _remote_get_cached( string $url, array $args, int $time, string $type )
205230
return $response;
206231
}
207232

233+
/**
234+
* Shorten a transient name if it is too long.
235+
*
236+
* WordPress transient names are limited to 172 characters. This function
237+
* shortens the name by removing non-alphanumeric characters and then hashing
238+
* the name if it is still too long.
239+
*
240+
* @param string $transient_name The transient name to shorten.
241+
*
242+
* @return string The shortened transient name.
243+
*/
208244
function shorten_transient_name( string $transient_name ): string {
209245

210246
$transient_name = str_replace( 'https://', '', $transient_name );

0 commit comments

Comments
 (0)