146 lines
4.5 KiB
PHP
146 lines
4.5 KiB
PHP
<?php
|
|
/**
|
|
* Plugin Activation
|
|
*
|
|
* @package WP_Debug
|
|
* @since 1.0.0
|
|
*/
|
|
|
|
// Exit if accessed directly
|
|
if (!defined('ABSPATH')) {
|
|
exit;
|
|
}
|
|
|
|
/**
|
|
* WP_Debug_Activation class
|
|
*/
|
|
class WP_Debug_Activation {
|
|
|
|
/**
|
|
* Run activation tasks
|
|
*/
|
|
public static function activate() {
|
|
// Set default options
|
|
self::set_default_options();
|
|
|
|
// Create logs directory
|
|
self::create_logs_directory();
|
|
|
|
// Create database tables if needed
|
|
self::create_tables();
|
|
|
|
// Set activation timestamp
|
|
update_option('wp_debug_activated_at', time());
|
|
|
|
// Flush rewrite rules
|
|
flush_rewrite_rules();
|
|
}
|
|
|
|
/**
|
|
* Set default plugin options
|
|
*/
|
|
private static function set_default_options() {
|
|
// Main toggle
|
|
add_option('wp_debug_enabled', true);
|
|
|
|
// Module toggles (all enabled by default)
|
|
add_option('wp_debug_module_logger', true);
|
|
add_option('wp_debug_module_hook_monitor', true);
|
|
add_option('wp_debug_module_template_tracer', true);
|
|
add_option('wp_debug_module_asset_tracker', true);
|
|
add_option('wp_debug_module_profiler', true);
|
|
add_option('wp_debug_module_query_analyzer', true);
|
|
add_option('wp_debug_module_frontend_panel', true);
|
|
add_option('wp_debug_module_cli_commands', true);
|
|
|
|
// Logger settings
|
|
add_option('wp_debug_log_level', 'INFO'); // INFO, WARNING, ERROR, DEBUG
|
|
add_option('wp_debug_log_retention_days', 7); // Keep logs for 7 days
|
|
add_option('wp_debug_log_to_file', true);
|
|
add_option('wp_debug_log_to_db', true);
|
|
|
|
// Performance settings
|
|
add_option('wp_debug_profiler_enabled', true);
|
|
add_option('wp_debug_profiler_threshold', 1000); // Log functions taking >1000ms
|
|
|
|
// Frontend panel settings
|
|
add_option('wp_debug_panel_enabled', true);
|
|
add_option('wp_debug_panel_position', 'bottom-right'); // bottom-right, bottom-left, top-right, top-left
|
|
|
|
// Query analyzer settings
|
|
add_option('wp_debug_query_threshold', 100); // Log queries taking >100ms
|
|
add_option('wp_debug_detect_n_plus_one', true);
|
|
}
|
|
|
|
/**
|
|
* Create logs directory
|
|
*/
|
|
private static function create_logs_directory() {
|
|
$logs_dir = WP_CONTENT_DIR . '/logs/wp-debug';
|
|
|
|
if (!file_exists($logs_dir)) {
|
|
wp_mkdir_p($logs_dir);
|
|
|
|
// Create .htaccess to protect logs
|
|
$htaccess_file = $logs_dir . '/.htaccess';
|
|
$htaccess_content = "Order deny,allow\nDeny from all";
|
|
file_put_contents($htaccess_file, $htaccess_content);
|
|
|
|
// Create index.php to prevent directory listing
|
|
$index_file = $logs_dir . '/index.php';
|
|
$index_content = "<?php\n// Silence is golden.";
|
|
file_put_contents($index_file, $index_content);
|
|
}
|
|
|
|
// Set proper permissions
|
|
if (file_exists($logs_dir)) {
|
|
chmod($logs_dir, 0755);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Create database tables
|
|
*/
|
|
private static function create_tables() {
|
|
global $wpdb;
|
|
|
|
$charset_collate = $wpdb->get_charset_collate();
|
|
|
|
// Logs table
|
|
$table_logs = $wpdb->prefix . 'wp_debug_logs';
|
|
$sql_logs = "CREATE TABLE IF NOT EXISTS $table_logs (
|
|
id bigint(20) NOT NULL AUTO_INCREMENT,
|
|
timestamp datetime DEFAULT CURRENT_TIMESTAMP NOT NULL,
|
|
level varchar(20) DEFAULT 'INFO' NOT NULL,
|
|
message text NOT NULL,
|
|
context text,
|
|
user_id bigint(20),
|
|
url varchar(255),
|
|
ip_address varchar(45),
|
|
PRIMARY KEY (id),
|
|
KEY level (level),
|
|
KEY timestamp (timestamp),
|
|
KEY user_id (user_id)
|
|
) $charset_collate;";
|
|
|
|
// Hooks table
|
|
$table_hooks = $wpdb->prefix . 'wp_debug_hooks';
|
|
$sql_hooks = "CREATE TABLE IF NOT EXISTS $table_hooks (
|
|
id bigint(20) NOT NULL AUTO_INCREMENT,
|
|
timestamp datetime DEFAULT CURRENT_TIMESTAMP NOT NULL,
|
|
hook_name varchar(255) NOT NULL,
|
|
hook_type varchar(20) NOT NULL,
|
|
execution_time float,
|
|
args_count int,
|
|
PRIMARY KEY (id),
|
|
KEY hook_name (hook_name),
|
|
KEY timestamp (timestamp),
|
|
KEY hook_type (hook_type)
|
|
) $charset_collate;";
|
|
|
|
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
|
|
dbDelta($sql_logs);
|
|
dbDelta($sql_hooks);
|
|
}
|
|
}
|