| 1 |
<?php |
| 2 |
/** |
| 3 |
* Handles interaction with MainWP |
| 4 |
* |
| 5 |
* @package WP_Defender\Integrations |
| 6 |
* @since 5.0.2 |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace WP_Defender\Integrations; |
| 10 |
|
| 11 |
use WP_Defender\Traits\IP; |
| 12 |
|
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
die; |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* MainWP plugin integration module. |
| 19 |
* |
| 20 |
* @since 5.0.2 |
| 21 |
*/ |
| 22 |
class Main_Wp { |
| 23 |
use IP; |
| 24 |
|
| 25 |
/** |
| 26 |
* The option name for the whitelist MainWP dashboard public IP. |
| 27 |
*/ |
| 28 |
public const WHITELIST_DASHBOARD_PUBLIC_IP_OPTION = 'wpdef_firewall_whitelist_mainwp_dashboard_public_ip'; |
| 29 |
|
| 30 |
/** |
| 31 |
* Constructor for the class. |
| 32 |
* Registers the hook. |
| 33 |
* |
| 34 |
* @return void |
| 35 |
*/ |
| 36 |
public function __construct() { |
| 37 |
add_action( 'mainwp_child_site_stats', array( $this, 'set_whitelist_dashboard_public_ip' ) ); |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Is whitelisting the MainWP Dashboard's public IP enabled. |
| 42 |
* |
| 43 |
* @return bool |
| 44 |
*/ |
| 45 |
public function is_whitelist_dashboard_public_ip_enabled(): bool { |
| 46 |
/** |
| 47 |
* Filter to enable/disable whitelisting the MainWP Dashboard site's public IP. |
| 48 |
* |
| 49 |
* @param bool $enable True to enable whitelisting, false otherwise. |
| 50 |
* |
| 51 |
* @since 5.0.2 |
| 52 |
*/ |
| 53 |
$enabled = apply_filters( 'wpdef_firewall_whitelist_mainwp_dashboard_public_ip_enabled', true ); |
| 54 |
return is_bool( $enabled ) ? $enabled : (bool) $enabled; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Whitelist MainWP dashboard site's public IP. |
| 59 |
* |
| 60 |
* @return void |
| 61 |
*/ |
| 62 |
public function set_whitelist_dashboard_public_ip(): void { |
| 63 |
if ( ! $this->is_whitelist_dashboard_public_ip_enabled() ) { |
| 64 |
return; |
| 65 |
} |
| 66 |
|
| 67 |
$ips = $this->get_user_ip(); |
| 68 |
|
| 69 |
if ( array() === $ips ) { |
| 70 |
return; |
| 71 |
} |
| 72 |
|
| 73 |
$stored_ips = $this->get_whitelist_dashboard_public_ip(); |
| 74 |
$stored_ips = array_unique( $stored_ips ); |
| 75 |
$ips = array_unique( $ips ); |
| 76 |
|
| 77 |
sort( $stored_ips ); |
| 78 |
sort( $ips ); |
| 79 |
|
| 80 |
if ( $stored_ips !== $ips ) { |
| 81 |
update_site_option( self::WHITELIST_DASHBOARD_PUBLIC_IP_OPTION, $ips ); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Get the whitelisted MainWP dashboard site's public IP. |
| 87 |
* |
| 88 |
* @return array |
| 89 |
*/ |
| 90 |
public function get_whitelist_dashboard_public_ip(): array { |
| 91 |
return $this->is_whitelist_dashboard_public_ip_enabled() ? |
| 92 |
get_site_option( self::WHITELIST_DASHBOARD_PUBLIC_IP_OPTION, array() ) : |
| 93 |
array(); |
| 94 |
} |
| 95 |
} |
| 96 |
|