PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 6.3.0
Defender Security – Malware Scanner, Login Security & Firewall v6.3.0
6.3.0 6.2.3 6.2.4 6.2.0 6.2.1 6.2.2 6.1.0 5.3.1 5.4.0 5.4.1 5.5.0 5.5.1 5.6.0 5.6.1 5.6.2 5.7.0 5.7.1 5.7.2 5.8.0 5.8.1 5.9.0 6.0.0 6.0.1 3.0.1 3.1.0 All 141 releases
defender-security / src / component / known-bots / class-known-bots.php

class-known-bots.php in Defender Security – Malware Scanner, Login Security & Firewall 6.3.0, at src/component/known-bots/class-known-bots.php

113 lines 2.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Responsible for handling known bot IPs.
4 *
5 * @package WP_Defender\Component
6 */
7
8 namespace WP_Defender\Component\Known_Bots;
9
10 use WP_Defender\Component\Known_Bots\Bots\Bots_Interface;
11
12 /**
13 * This class is responsible for managing known bot IPs.
14 * It fetches and caches the IPs from various providers.
15 */
16 class Known_Bots {
17 /**
18 * An array of bot providers that implement the Bots_Interface.
19 *
20 * @var Bots_Interface[]
21 */
22 protected $providers = array();
23
24 /**
25 * Constructor for the Known_Bots class.
26 *
27 * @param Bots_Interface[] $providers An array of bot providers that implement the Bots_Interface.
28 */
29 public function __construct( array $providers = array() ) {
30 $this->providers = $providers;
31 }
32
33 /**
34 * Fetches all known bot IPs from the registered providers.
35 *
36 * @return array An associative array where keys are provider names and values are arrays of IPs.
37 */
38 public function get_all_bot_ips(): array {
39 $data = array();
40
41 foreach ( $this->get_providers() as $provider ) {
42 if ( $provider instanceof Bots_Interface ) {
43 $name = $provider->get_name();
44 $data[ $name ] = $this->get_or_set_bot_ips( $provider );
45 }
46 }
47
48 return $data;
49 }
50
51 /**
52 * Fetches and caches bot IPs for a specific provider.
53 *
54 * @param Bots_Interface $provider The bot provider to fetch IPs from.
55 *
56 * @return array An array of bot IPs.
57 */
58 protected function get_or_set_bot_ips( Bots_Interface $provider ): array {
59 $transient_key = 'wpdef_known_bot_ips_' . $provider->get_name();
60 $cached = get_site_transient( $transient_key );
61
62 if ( false !== $cached ) {
63 return $cached;
64 }
65
66 $ips = $provider->fetch_ips();
67 set_site_transient( $transient_key, $ips, DAY_IN_SECONDS );
68
69 return $ips;
70 }
71
72 /**
73 * Retrieves bot IPs by provider name.
74 *
75 * @param string $name The name of the bot provider.
76 *
77 * @return array An array of bot IPs for the specified provider.
78 */
79 public function get_bot_ips_by_name( string $name ): array {
80 $key = 'wpdef_known_bot_ips_' . $name;
81 $ips = get_site_transient( $key );
82
83 return is_array( $ips ) && array() !== $ips ? $ips : array();
84 }
85
86 /**
87 * Returns the registered bot providers.
88 *
89 * @return Bots_Interface[]
90 */
91 public function get_providers(): array {
92 return $this->providers;
93 }
94
95 /**
96 * Checks if the given IP and user agent match any registered known bot.
97 *
98 * @param string $ip The IP address to check.
99 * @param string $user_agent Optional user agent string.
100 *
101 * @return bool
102 */
103 public function is_known_bot( string $ip, string $user_agent = '' ): bool {
104 foreach ( $this->get_providers() as $provider ) {
105 if ( $provider instanceof Bots_Interface && $provider->is_ua( $user_agent ) && $provider->is_ip( $ip ) ) {
106 return true;
107 }
108 }
109
110 return false;
111 }
112 }
113