WordFenceCompatibility.php
2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
namespace WP_Rocket\ThirdParty\Plugins\Security;
use WP_Rocket\Event_Management\Subscriber_Interface;
use wordfence;
use wfConfig;
/**
* Compatibility file for WordFence plugin
*
* @since 3.10
*/
class WordFenceCompatibility implements Subscriber_Interface {
/**
* Whitelisted_IPS.
*/
const WHITELISTED_IPS = [ '141.94.254.72' ];
/**
* Old Whitelisted IP.
*
* @var string
*/
private $old_rucss_ip = '135.125.83.227';
/**
* Return an array of events that this subscriber wants to listen to.
*
* @since 3.10
*
* @return array
*/
public static function get_subscribed_events() {
if ( ! defined( 'WORDFENCE_VERSION' ) ) {
return [];
}
return [
'init' => [ 'whitelist_wordfence_firewall_ips', 11 ],
'rocket_deactivation' => 'pop_ip_from_whitelist',
];
}
/**
* Removes old ip from whitelist.
*
* @param string $old_rucss_ip Old RUCSS IP.
* @return void
*/
public function pop_old_ip( string $old_rucss_ip ) {
$whitelist = $this->can_pop_ip( $old_rucss_ip );
if ( ! $whitelist ) {
return;
}
// Update whitelist.
wfConfig::set( 'whitelisted', implode( ',', $whitelist ) );
}
/**
* Remove ip from whitelist.
*
* @return void
*/
public function pop_ip_from_whitelist() {
$whitelist = $this->can_pop_ip( self::WHITELISTED_IPS[0] );
if ( ! $whitelist ) {
return;
}
// Update whitelist.
wfConfig::set( 'whitelisted', implode( ',', $whitelist ) );
}
/**
* Check if ip can be removed.
*
* @param string $ip IP.
* @return array
*/
private function can_pop_ip( string $ip ) {
// Get all whitelists.
$whitelists = wfConfig::get( 'whitelisted', '' );
// Convert to array.
$whitelist_array = explode( ',', $whitelists );
// Get ip index.
$ip = array_search( $ip, $whitelist_array, true );
if ( false === $ip ) {
return false;
}
// Remove ip from whitelist.
unset( $whitelist_array[ $ip ] );
return $whitelist_array;
}
/**
* Whitelist wp-rocket ips in wordfence firewall
*
* @since 3.10
*
* @return void
*/
public function whitelist_wordfence_firewall_ips() {
// Pop old rucss ip.
$this->pop_old_ip( $this->old_rucss_ip );
/**
* Rocket wordfence whitelisted ips filter which adds IPs to wordfence whitelist.
*
* @since 3.10
*
* @param array list of IPs should be whitelisted
*/
$ips = apply_filters( 'rocket_wordfence_whitelisted_ips', self::WHITELISTED_IPS );
if ( empty( $ips ) ) {
return;
}
foreach ( $ips as $ip ) {
wordfence::whitelistIP( $ip );
}
}
}