| 1 |
<?php |
| 2 |
/** |
| 3 |
* Main AtomicEdge Plugin Class |
| 4 |
* |
| 5 |
* Initializes and coordinates all plugin functionality. |
| 6 |
* |
| 7 |
* @package AtomicEdge |
| 8 |
*/ |
| 9 |
|
| 10 |
// Prevent direct access. |
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
exit; |
| 13 |
} |
| 14 |
|
| 15 |
/** |
| 16 |
* Class AtomicEdge |
| 17 |
* |
| 18 |
* Main plugin class implementing singleton pattern. |
| 19 |
*/ |
| 20 |
class AtomicEdge { |
| 21 |
|
| 22 |
/** |
| 23 |
* Singleton instance. |
| 24 |
* |
| 25 |
* @var AtomicEdge|null |
| 26 |
*/ |
| 27 |
private static $instance = null; |
| 28 |
|
| 29 |
/** |
| 30 |
* API client instance. |
| 31 |
* |
| 32 |
* @var AtomicEdge_API |
| 33 |
*/ |
| 34 |
public $api; |
| 35 |
|
| 36 |
/** |
| 37 |
* Admin instance. |
| 38 |
* |
| 39 |
* @var AtomicEdge_Admin |
| 40 |
*/ |
| 41 |
public $admin; |
| 42 |
|
| 43 |
/** |
| 44 |
* AJAX handler instance. |
| 45 |
* |
| 46 |
* @var AtomicEdge_Ajax |
| 47 |
*/ |
| 48 |
public $ajax; |
| 49 |
|
| 50 |
/** |
| 51 |
* Scanner instance. |
| 52 |
* |
| 53 |
* @var AtomicEdge_Scanner |
| 54 |
*/ |
| 55 |
public $scanner; |
| 56 |
|
| 57 |
/** |
| 58 |
* Vulnerability scanner instance. |
| 59 |
* |
| 60 |
* @var AtomicEdge_Vulnerability_Scanner |
| 61 |
*/ |
| 62 |
public $vulnerability_scanner; |
| 63 |
|
| 64 |
/** |
| 65 |
* Cron handler instance. |
| 66 |
* |
| 67 |
* @var AtomicEdge_Cron |
| 68 |
*/ |
| 69 |
public $cron; |
| 70 |
|
| 71 |
/** |
| 72 |
* Get singleton instance. |
| 73 |
* |
| 74 |
* @return AtomicEdge |
| 75 |
*/ |
| 76 |
public static function get_instance() { |
| 77 |
if ( null === self::$instance ) { |
| 78 |
self::$instance = new self(); |
| 79 |
} |
| 80 |
return self::$instance; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Constructor - private to enforce singleton. |
| 85 |
*/ |
| 86 |
private function __construct() { |
| 87 |
$this->init_components(); |
| 88 |
$this->init_hooks(); |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Prevent cloning. |
| 93 |
*/ |
| 94 |
private function __clone() {} |
| 95 |
|
| 96 |
/** |
| 97 |
* Prevent unserialization. |
| 98 |
* |
| 99 |
* @throws Exception Always throws exception. |
| 100 |
*/ |
| 101 |
public function __wakeup() { |
| 102 |
throw new Exception( esc_html__( 'Cannot unserialize singleton', 'atomic-edge-security' ) ); |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Initialize plugin components. |
| 107 |
* |
| 108 |
* @return void |
| 109 |
*/ |
| 110 |
private function init_components() { |
| 111 |
$this->api = new AtomicEdge_API(); |
| 112 |
$this->admin = new AtomicEdge_Admin( $this->api ); |
| 113 |
$this->ajax = new AtomicEdge_Ajax( $this->api ); |
| 114 |
$this->scanner = new AtomicEdge_Scanner(); |
| 115 |
$this->vulnerability_scanner = new AtomicEdge_Vulnerability_Scanner( $this->api ); |
| 116 |
$this->cron = new AtomicEdge_Cron( $this->api, $this->scanner ); |
| 117 |
|
| 118 |
// Initialize 2FA component (singleton). |
| 119 |
AtomicEdge_2FA::get_instance(); |
| 120 |
|
| 121 |
// Initialize CDN components (frontend only, when CDN is enabled). |
| 122 |
if ( ! is_admin() && AtomicEdge_CDN::is_cdn_enabled() ) { |
| 123 |
new AtomicEdge_CDN_Rewrite(); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Initialize WordPress hooks. |
| 129 |
* |
| 130 |
* @return void |
| 131 |
*/ |
| 132 |
private function init_hooks() { |
| 133 |
// Admin-specific hooks. |
| 134 |
if ( is_admin() ) { |
| 135 |
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_assets' ) ); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Enqueue admin CSS and JavaScript. |
| 141 |
* |
| 142 |
* @param string $hook Current admin page hook. |
| 143 |
* @return void |
| 144 |
*/ |
| 145 |
public function enqueue_admin_assets( $hook ) { |
| 146 |
// Only load on our plugin pages. |
| 147 |
// WordPress hook format uses sanitize_title($menu_title) which yields 'atomic-edge'. |
| 148 |
// Submenus use slugs like 'atomicedge-scanner', so check for both patterns. |
| 149 |
$is_plugin_page = ( strpos( $hook, 'atomic-edge' ) !== false ) || ( strpos( $hook, 'atomicedge' ) !== false ); |
| 150 |
if ( ! $is_plugin_page ) { |
| 151 |
return; |
| 152 |
} |
| 153 |
|
| 154 |
// Determine version for cache busting. |
| 155 |
$version = ATOMICEDGE_VERSION; |
| 156 |
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { |
| 157 |
$version .= '-' . time(); |
| 158 |
} |
| 159 |
|
| 160 |
// Enqueue Chart.js for analytics. |
| 161 |
wp_enqueue_script( |
| 162 |
'atomicedge-chartjs', |
| 163 |
ATOMICEDGE_PLUGIN_URL . 'admin/assets/js/chart.min.js', |
| 164 |
array(), |
| 165 |
'4.4.1', |
| 166 |
true |
| 167 |
); |
| 168 |
|
| 169 |
// Enqueue admin CSS. |
| 170 |
wp_enqueue_style( |
| 171 |
'atomicedge-admin', |
| 172 |
ATOMICEDGE_PLUGIN_URL . 'admin/css/admin.css', |
| 173 |
array(), |
| 174 |
$version |
| 175 |
); |
| 176 |
|
| 177 |
// Enqueue admin JavaScript. |
| 178 |
wp_enqueue_script( |
| 179 |
'atomicedge-admin', |
| 180 |
ATOMICEDGE_PLUGIN_URL . 'admin/js/admin.js', |
| 181 |
array( 'jquery', 'atomicedge-chartjs' ), |
| 182 |
$version, |
| 183 |
true |
| 184 |
); |
| 185 |
|
| 186 |
// Localize script with data for main admin.js. |
| 187 |
wp_localize_script( |
| 188 |
'atomicedge-admin', |
| 189 |
'atomicedgeAdmin', |
| 190 |
array( |
| 191 |
'ajaxUrl' => admin_url( 'admin-ajax.php' ), |
| 192 |
'nonce' => wp_create_nonce( 'atomicedge_ajax' ), |
| 193 |
'connected' => $this->api->is_connected(), |
| 194 |
'strings' => array( |
| 195 |
'loading' => esc_html__( 'Loading...', 'atomic-edge-security' ), |
| 196 |
'error' => esc_html__( 'An error occurred. Please try again.', 'atomic-edge-security' ), |
| 197 |
'success' => esc_html__( 'Success!', 'atomic-edge-security' ), |
| 198 |
'confirm' => esc_html__( 'Are you sure?', 'atomic-edge-security' ), |
| 199 |
'confirmIp' => esc_html__( 'Are you sure you want to remove this IP?', 'atomic-edge-security' ), |
| 200 |
'invalidIp' => esc_html__( 'Please enter a valid IP address or CIDR range.', 'atomic-edge-security' ), |
| 201 |
'scanning' => esc_html__( 'Scanning files...', 'atomic-edge-security' ), |
| 202 |
'scanComplete' => esc_html__( 'Scan complete!', 'atomic-edge-security' ), |
| 203 |
), |
| 204 |
) |
| 205 |
); |
| 206 |
|
| 207 |
// Enqueue Adaptive Defense JavaScript on its page. |
| 208 |
if ( strpos( $hook, 'atomicedge-adaptive-defense' ) !== false ) { |
| 209 |
wp_enqueue_script( |
| 210 |
'atomicedge-adaptive-defense', |
| 211 |
ATOMICEDGE_PLUGIN_URL . 'admin/js/adaptive-defense.js', |
| 212 |
array( 'jquery' ), |
| 213 |
$version, |
| 214 |
true |
| 215 |
); |
| 216 |
|
| 217 |
// Localize script with data for adaptive-defense.js. |
| 218 |
wp_localize_script( |
| 219 |
'atomicedge-adaptive-defense', |
| 220 |
'atomicedge_admin', |
| 221 |
array( |
| 222 |
'ajax_url' => admin_url( 'admin-ajax.php' ), |
| 223 |
'nonce' => wp_create_nonce( 'atomicedge_ajax' ), |
| 224 |
) |
| 225 |
); |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
/** |
| 230 |
* Check if the site is connected to AtomicEdge. |
| 231 |
* |
| 232 |
* @return bool |
| 233 |
*/ |
| 234 |
public function is_connected() { |
| 235 |
return $this->api->is_connected(); |
| 236 |
} |
| 237 |
|
| 238 |
/** |
| 239 |
* Get the API client instance. |
| 240 |
* |
| 241 |
* @return AtomicEdge_API |
| 242 |
*/ |
| 243 |
public function get_api() { |
| 244 |
return $this->api; |
| 245 |
} |
| 246 |
|
| 247 |
/** |
| 248 |
* Log debug information. |
| 249 |
* |
| 250 |
* @param string $message Debug message. |
| 251 |
* @param mixed $data Optional data to log. |
| 252 |
* @return void |
| 253 |
*/ |
| 254 |
public static function log( $message, $data = null ) { |
| 255 |
if ( defined( 'WP_DEBUG' ) && WP_DEBUG && defined( 'WP_DEBUG_LOG' ) && WP_DEBUG_LOG ) { |
| 256 |
$log_message = 'AtomicEdge: ' . $message; |
| 257 |
if ( null !== $data ) { |
| 258 |
$log_message .= ' | Data: ' . wp_json_encode( $data ); |
| 259 |
} |
| 260 |
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log |
| 261 |
error_log( $log_message ); |
| 262 |
} |
| 263 |
} |
| 264 |
} |
| 265 |
|