Files
WooNooW/includes/Api/ActivityLogController.php
dwindown debe42f4e1 feat: Implement activity log system
##  Activity Log System - Complete

### Backend Implementation

**1. Database Table**
- `ActivityLogTable.php` - Table creation and management
- Auto-creates on plugin init
- Indexed for performance (user_id, action, object, created_at)

**2. Logger Class**
- `Logger.php` - Main logging functionality
- `log()` - Log activities
- `get_activities()` - Query with filters
- `get_stats()` - Activity statistics
- `cleanup()` - Delete old logs

**3. REST API**
- `ActivityLogController.php` - REST endpoints
- GET `/activity-log` - List activities
- POST `/activity-log` - Create activity
- GET `/activity-log/stats` - Get statistics

### Features

**Logging:**
- User ID and name
- Action type (order.created, product.updated, etc.)
- Object type and ID
- Object name (auto-resolved)
- Description
- Metadata (JSON)
- IP address
- User agent
- Timestamp

**Querying:**
- Pagination
- Filter by action, object, user, date
- Search by description, object name, user name
- Sort by date (newest first)

**Statistics:**
- Total activities
- By action (top 10)
- By user (top 10)
- Date range filtering

### Activity Types

**Orders:**
- order.created, order.updated, order.status_changed
- order.payment_completed, order.refunded, order.deleted

**Products:**
- product.created, product.updated
- product.stock_changed, product.deleted

**Customers:**
- customer.created, customer.updated, customer.deleted

**Notifications:**
- notification.sent, notification.failed, notification.clicked

**Settings:**
- settings.updated, channel.toggled, event.toggled

### Integration

- Registered in Bootstrap
- REST API routes registered
- Ready for WooCommerce hooks
- Ready for frontend UI

---

**Next:** Frontend UI + WooCommerce hooks
2025-11-11 17:52:03 +07:00

146 lines
3.5 KiB
PHP

<?php
/**
* Activity Log REST API Controller
*
* @package WooNooW\Api
*/
namespace WooNooW\Api;
use WooNooW\Core\ActivityLog\Logger;
use WP_REST_Request;
use WP_REST_Response;
use WP_Error;
class ActivityLogController {
/**
* REST API namespace
*/
private $namespace = 'woonoow/v1';
/**
* REST API base
*/
private $rest_base = 'activity-log';
/**
* Register routes
*/
public function register_routes() {
// GET /woonoow/v1/activity-log
register_rest_route($this->namespace, '/' . $this->rest_base, [
[
'methods' => 'GET',
'callback' => [$this, 'get_activities'],
'permission_callback' => [$this, 'check_permission'],
],
]);
// POST /woonoow/v1/activity-log
register_rest_route($this->namespace, '/' . $this->rest_base, [
[
'methods' => 'POST',
'callback' => [$this, 'create_activity'],
'permission_callback' => [$this, 'check_permission'],
],
]);
// GET /woonoow/v1/activity-log/stats
register_rest_route($this->namespace, '/' . $this->rest_base . '/stats', [
[
'methods' => 'GET',
'callback' => [$this, 'get_stats'],
'permission_callback' => [$this, 'check_permission'],
],
]);
}
/**
* Get activities
*
* @param WP_REST_Request $request
* @return WP_REST_Response
*/
public function get_activities(WP_REST_Request $request) {
$args = [
'page' => $request->get_param('page') ?: 1,
'per_page' => $request->get_param('per_page') ?: 20,
'action' => $request->get_param('action'),
'object_type' => $request->get_param('object_type'),
'object_id' => $request->get_param('object_id'),
'user_id' => $request->get_param('user_id'),
'date_from' => $request->get_param('date_from'),
'date_to' => $request->get_param('date_to'),
'search' => $request->get_param('search'),
];
$result = Logger::get_activities($args);
return new WP_REST_Response($result, 200);
}
/**
* Create activity
*
* @param WP_REST_Request $request
* @return WP_REST_Response|WP_Error
*/
public function create_activity(WP_REST_Request $request) {
$params = $request->get_json_params();
$action = isset($params['action']) ? $params['action'] : null;
$object_type = isset($params['object_type']) ? $params['object_type'] : null;
$object_id = isset($params['object_id']) ? $params['object_id'] : null;
$description = isset($params['description']) ? $params['description'] : '';
$metadata = isset($params['metadata']) ? $params['metadata'] : [];
if (empty($action) || empty($object_type) || empty($object_id)) {
return new WP_Error(
'invalid_params',
__('Action, object_type, and object_id are required', 'woonoow'),
['status' => 400]
);
}
$activity_id = Logger::log($action, $object_type, $object_id, $description, $metadata);
if ($activity_id === false) {
return new WP_Error(
'log_failed',
__('Failed to log activity', 'woonoow'),
['status' => 500]
);
}
return new WP_REST_Response([
'success' => true,
'activity_id' => $activity_id,
], 201);
}
/**
* Get activity stats
*
* @param WP_REST_Request $request
* @return WP_REST_Response
*/
public function get_stats(WP_REST_Request $request) {
$date_from = $request->get_param('date_from');
$date_to = $request->get_param('date_to');
$stats = Logger::get_stats($date_from, $date_to);
return new WP_REST_Response($stats, 200);
}
/**
* Check permission
*
* @return bool
*/
public function check_permission() {
return current_user_can('manage_woocommerce') || current_user_can('manage_options');
}
}