| 1 |
<?php |
| 2 |
/** |
| 3 |
* Fetches Bing Bot IPs. |
| 4 |
* |
| 5 |
* @package WP_Defender\Component\Known_Bots\Bots |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WP_Defender\Component\Known_Bots\Bots; |
| 9 |
|
| 10 |
/** |
| 11 |
* This class is responsible for fetching and managing Bing Bot IPs. |
| 12 |
*/ |
| 13 |
class Bing_Bot implements Bots_Interface { |
| 14 |
/** |
| 15 |
* Returns the name of the bot. |
| 16 |
* |
| 17 |
* @return string The name of the bot. |
| 18 |
*/ |
| 19 |
public function get_name(): string { |
| 20 |
return 'bingbot'; |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* Fetches the IPs for Bing Bot. |
| 25 |
* |
| 26 |
* This method retrieves the IP ranges used by Bing Bot from a remote JSON endpoint. |
| 27 |
* It returns an array of IP addresses in both IPv4 and IPv6 formats. |
| 28 |
* |
| 29 |
* @return array An array of IP addresses used by Bing Bot. |
| 30 |
*/ |
| 31 |
public function fetch_ips(): array { |
| 32 |
$url = 'https://www.bing.com/toolbox/bingbot.json'; |
| 33 |
$ips = array(); |
| 34 |
|
| 35 |
$response = wp_remote_get( $url ); |
| 36 |
if ( is_array( $response ) && ! is_wp_error( $response ) ) { |
| 37 |
$data = json_decode( wp_remote_retrieve_body( $response ), true ); |
| 38 |
foreach ( $data['prefixes'] ?? array() as $entry ) { |
| 39 |
if ( isset( $entry['ipv4Prefix'] ) ) { |
| 40 |
$ips[] = $entry['ipv4Prefix']; |
| 41 |
} |
| 42 |
|
| 43 |
if ( isset( $entry['ipv6Prefix'] ) ) { |
| 44 |
$ips[] = $entry['ipv6Prefix']; |
| 45 |
} |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
return $ips; |
| 50 |
} |
| 51 |
} |
| 52 |
|