| 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", to prevent firewalls to be triggered. |
| 7 |
* |
| 8 |
* @since 1.6.13 |
| 9 |
* @author Grégory Viguier |
| 10 |
* |
| 11 |
* @param array $r An array of HTTP request arguments. |
| 12 |
* @param string $url The request URL. |
| 13 |
* @return array |
| 14 |
*/ |
| 15 |
function imagify_siteground_change_user_agent( $r, $url ) { |
| 16 |
static $user_agent; |
| 17 |
static $site_url; |
| 18 |
|
| 19 |
$url = wp_parse_url( $url ); |
| 20 |
|
| 21 |
if ( empty( $url['path'] ) ) { |
| 22 |
return $r; |
| 23 |
} |
| 24 |
|
| 25 |
$paths = [ |
| 26 |
'/wp-admin/admin-ajax.php', |
| 27 |
'/wp-admin/admin-post.php', |
| 28 |
'/wp-cron.php', |
| 29 |
]; |
| 30 |
|
| 31 |
if ( ! isset( $site_url ) ) { |
| 32 |
$site_url = wp_parse_url( site_url( '/' ) ); |
| 33 |
} |
| 34 |
|
| 35 |
// Limit to requests to self. |
| 36 |
if ( false === strpos( $url['path'], $site_url['path'] ) && false === strpos( $site_url['path'], $url['path'] ) ) { |
| 37 |
return $r; |
| 38 |
} |
| 39 |
|
| 40 |
// Limit to requests to admin-ajax.php, admin-post.php, and wp-cron.php. |
| 41 |
foreach ( $paths as $i => $path ) { |
| 42 |
if ( false !== strpos( $url['path'], $path ) ) { |
| 43 |
$paths = false; |
| 44 |
break; |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
if ( $paths ) { |
| 49 |
return $r; |
| 50 |
} |
| 51 |
|
| 52 |
// Randomize the User-Agent. |
| 53 |
if ( ! isset( $user_agent ) ) { |
| 54 |
$user_agent = wp_generate_password( 12, false ); |
| 55 |
/** |
| 56 |
* Filter the User-Agent used for requests "to self". |
| 57 |
* |
| 58 |
* @since 1.7.1 |
| 59 |
* @author Grégory Viguier |
| 60 |
* |
| 61 |
* @param string $user_agent The User-Agent. |
| 62 |
* @param array $r An array of HTTP request arguments. |
| 63 |
* @param array $url The request URL, parsed. |
| 64 |
*/ |
| 65 |
$user_agent = apply_filters( 'imagify_user_agent_for_internal_requests', $user_agent, $r, $url ); |
| 66 |
} |
| 67 |
|
| 68 |
$r['user-agent'] = $user_agent; |
| 69 |
|
| 70 |
return $r; |
| 71 |
} |
| 72 |
|