class AdminAudit { private $wpdb; public function __construct($wpdb) { $this->wpdb = $wpdb; // Agregar menú de administración add_action('admin_menu', array($this, 'add_admin_menu')); } public function add_admin_menu() { add_menu_page( 'Auditoría', 'Auditoría', 'manage_options', 'sistema-auditoria', array($this, 'render_admin_page'), 'dashicons-shield', 30 ); } public function render_admin_page() { // Cargar template principal require_once SA_PLUGIN_DIR . 'templates/admin-page.php'; } // Métodos auxiliares para los reportes public function get_woo_report_data($filters = array()) { $query = "SELECT * FROM {$this->wpdb->prefix}woo_audit_log WHERE 1=1"; $params = array(); // Validar filtros $filters['user_id'] = isset($filters['user_id']) ? intval($filters['user_id']) : null; $filters['date_from'] = isset($filters['date_from']) ? sanitize_text_field($filters['date_from']) : null; $filters['date_to'] = isset($filters['date_to']) ? sanitize_text_field($filters['date_to']) : null; if (!empty($filters['user_id'])) { $query .= " AND user_id = %d"; $params[] = $filters['user_id']; } if (!empty($filters['date_from'])) { $query .= " AND fecha_hora >= %s"; $params[] = $filters['date_from'] . ' 00:00:00'; } if (!empty($filters['date_to'])) { $query .= " AND fecha_hora <= %s"; $params[] = $filters['date_to'] . ' 23:59:59'; } $limit = isset($filters['limit']) ? intval($filters['limit']) : 500; $query .= " ORDER BY fecha_hora DESC LIMIT $limit"; if (!empty($params)) { $query = $this->wpdb->prepare($query, $params); } $results = $this->wpdb->get_results($query); if ($this->wpdb->last_error) { error_log("Error en la consulta de auditoría WooCommerce: " . $this->wpdb->last_error); return []; } return $results; } public function get_wp_report_data($filters = array()) { $query = "SELECT * FROM {$this->wpdb->prefix}wp_audit_log WHERE 1=1"; $params = array(); // Validar filtros $filters['user_id'] = isset($filters['user_id']) ? intval($filters['user_id']) : null; $filters['action_type'] = isset($filters['action_type']) ? sanitize_text_field($filters['action_type']) : null; $filters['date_from'] = isset($filters['date_from']) ? sanitize_text_field($filters['date_from']) : null; $filters['date_to'] = isset($filters['date_to']) ? sanitize_text_field($filters['date_to']) : null; if (!empty($filters['user_id'])) { $query .= " AND user_id = %d"; $params[] = $filters['user_id']; } if (!empty($filters['action_type'])) { $query .= " AND action_type = %s"; $params[] = $filters['action_type']; } if (!empty($filters['date_from'])) { $query .= " AND fecha_hora >= %s"; $params[] = $filters['date_from'] . ' 00:00:00'; } if (!empty($filters['date_to'])) { $query .= " AND fecha_hora <= %s"; $params[] = $filters['date_to'] . ' 23:59:59'; } $limit = isset($filters['limit']) ? intval($filters['limit']) : 500; $query .= " ORDER BY fecha_hora DESC LIMIT $limit"; if (!empty($params)) { $query = $this->wpdb->prepare($query, $params); } $results = $this->wpdb->get_results($query); if ($this->wpdb->last_error) { error_log("Error en la consulta de auditoría WordPress: " . $this->wpdb->last_error); return []; } return $results; } }