| 1 |
<?php |
| 2 |
defined( 'ABSPATH' ) || die( 'Cheatin\' uh?' ); |
| 3 |
|
| 4 |
add_filter( 'http_request_args', 'imagify_siteground_change_user_agent', 10, 2 ); |
| 5 |
/** |
| 6 |
* Filter the arguments used in a HTTP request to change the User Agent for requests "to self". |
| 7 |
* SiteGround blocks (error 403) HTTP requests with a User-Agent containing the site's domain. |
| 8 |
* The problem is that, for a given site at https://example.com, WordPress uses the UA `WordPress/4.8.2; https://example.com`. |
| 9 |
* |
| 10 |
* @since 1.6.13 |
| 11 |
* @author Grégory Viguier |
| 12 |
* |
| 13 |
* @param array $r An array of HTTP request arguments. |
| 14 |
* @param string $url The request URL. |
| 15 |
* @return array |
| 16 |
*/ |
| 17 |
function imagify_siteground_change_user_agent( $r, $url ) { |
| 18 |
static $user_agent; |
| 19 |
$site_url = site_url( '/' ); |
| 20 |
|
| 21 |
if ( false === strpos( $url, $site_url ) ) { |
| 22 |
return $r; |
| 23 |
} |
| 24 |
|
| 25 |
$site_url = wp_parse_url( $site_url ); |
| 26 |
|
| 27 |
if ( false === strpos( $r['user-agent'], $site_url['host'] ) ) { |
| 28 |
return $r; |
| 29 |
} |
| 30 |
|
| 31 |
if ( ! isset( $user_agent ) ) { |
| 32 |
$user_agent = wp_generate_password( 12, false ); |
| 33 |
} |
| 34 |
|
| 35 |
$r['user-agent'] = $user_agent; |
| 36 |
|
| 37 |
return $r; |
| 38 |
} |
| 39 |
|