class WooAudit { private $wpdb; public function __construct($wpdb) { $this->wpdb = $wpdb; // Registrar hooks add_action('woocommerce_before_product_object_save', array($this, 'audit_product_changes')); } public function audit_product_changes($product) { $product_id = $product->get_id(); if ($product_id) { $old_product = wc_get_product($product_id); $changes = array(); // Verificar cambios en el precio regular if ($old_product->get_regular_price() != $product->get_regular_price()) { $changes['precio_regular'] = [ 'old' => $old_product->get_regular_price(), 'new' => $product->get_regular_price() ]; } // Verificar cambios en el precio de oferta if ($old_product->get_sale_price() != $product->get_sale_price()) { $changes['precio_oferta'] = [ 'old' => $old_product->get_sale_price(), 'new' => $product->get_sale_price() ]; } // Verificar cambios en el stock if ($old_product->get_stock_quantity() != $product->get_stock_quantity()) { $changes['stock'] = [ 'old' => $old_product->get_stock_quantity(), 'new' => $product->get_stock_quantity() ]; } // Registrar cambios if (!empty($changes)) { $this->audit_changes($product_id, $product->get_name(), $changes); } } } private function audit_changes($product_id, $product_name, $changes) { $current_user = wp_get_current_user(); foreach ($changes as $field => $values) { $result = $this->wpdb->insert( $this->wpdb->prefix . 'woo_audit_log', array( 'user_id' => $current_user ? $current_user->ID : 0, 'username' => $current_user ? $current_user->user_login : 'Sistema', 'product_id' => $product_id, 'product_name' => sanitize_text_field($product_name), 'field_changed' => sanitize_text_field($field), 'old_value' => sanitize_text_field($values['old']), 'new_value' => sanitize_text_field($values['new']), 'ip_address' => $_SERVER['REMOTE_ADDR'] ) ); if ($result === false) { error_log("Error al insertar en woo_audit_log: " . $this->wpdb->last_error); } } } } 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; } }