Files
WooNooW/includes/Api/Routes.php
dwindown 829d9d0d8f feat(api): Add CustomersController with full CRUD operations
Backend implementation for Customer module

Created CustomersController.php:
 GET /customers - List with pagination, search, role filter
 GET /customers/{id} - Get single customer with full details
 POST /customers - Create new customer with validation
 PUT /customers/{id} - Update customer data
 DELETE /customers/{id} - Delete customer (with safety checks)
 GET /customers/search - Autocomplete search

Features:
- Full WooCommerce integration (WC_Customer)
- Billing and shipping address management
- Order stats (total_orders, total_spent)
- Email uniqueness validation
- Username auto-generation from email
- Password generation if not provided
- Role-based permissions (list_users, create_users, etc.)
- Cannot delete current user (safety)
- Optional new account email notification

Data format:
- List: Basic customer info (id, name, email, registered)
- Detail: Full data including billing, shipping, stats
- Search: Minimal data for autocomplete (id, name, email)

Registered routes in Routes.php:
- Added CustomersController import
- Registered all customer endpoints

Next: Frontend API client and CRUD pages
2025-11-20 22:40:59 +07:00

122 lines
4.7 KiB
PHP

<?php
namespace WooNooW\Api;
use WP_REST_Request;
use WP_REST_Response;
use WooNooW\Api\CheckoutController;
use WooNooW\Api\OrdersController;
use WooNooW\Api\AnalyticsController;
use WooNooW\Api\AuthController;
use WooNooW\Api\PaymentsController;
use WooNooW\Api\StoreController;
use WooNooW\Api\ShippingController;
use WooNooW\Api\TaxController;
use WooNooW\Api\PickupLocationsController;
use WooNooW\Api\EmailController;
use WooNooW\Api\DeveloperController;
use WooNooW\Api\SystemController;
use WooNooW\Api\NotificationsController;
use WooNooW\Api\ActivityLogController;
use WooNooW\Api\ProductsController;
use WooNooW\Api\CouponsController;
use WooNooW\Api\CustomersController;
class Routes {
public static function init() {
// Initialize controllers (register action hooks)
OrdersController::init();
// Log ALL REST API requests to debug routing
add_filter('rest_pre_dispatch', function($result, $server, $request) {
$route = $request->get_route();
$method = $request->get_method();
$result_type = is_null($result) ? 'NULL (will call handler)' : 'NON-NULL (handler bypassed!)';
error_log("WooNooW REST: {$method} {$route} - Result: {$result_type}");
if (!is_null($result)) {
error_log("WooNooW REST: BYPASSED! Result type: " . gettype($result));
}
return $result;
}, 10, 3);
add_action('rest_api_init', function () {
error_log('WooNooW Routes: rest_api_init hook fired');
$namespace = 'woonoow/v1';
// Auth endpoints (public - no permission check)
register_rest_route( $namespace, '/auth/login', [
'methods' => 'POST',
'callback' => [ AuthController::class, 'login' ],
'permission_callback' => '__return_true',
] );
register_rest_route( $namespace, '/auth/logout', [
'methods' => 'POST',
'callback' => [ AuthController::class, 'logout' ],
'permission_callback' => '__return_true',
] );
register_rest_route( $namespace, '/auth/check', [
'methods' => 'GET',
'callback' => [ AuthController::class, 'check' ],
'permission_callback' => '__return_true',
] );
// Defer to controllers to register their endpoints
CheckoutController::register();
OrdersController::register();
AnalyticsController::register_routes();
// Payments controller
$payments_controller = new PaymentsController();
$payments_controller->register_routes();
// Store controller
$store_controller = new StoreController();
$store_controller->register_routes();
// Shipping controller
$shipping_controller = new ShippingController();
$shipping_controller->register_routes();
// Tax controller
$tax_controller = new TaxController();
$tax_controller->register_routes();
// Pickup locations controller
$pickup_controller = new PickupLocationsController();
$pickup_controller->register_routes();
// Email controller
$email_controller = new EmailController();
$email_controller->register_routes();
// Developer controller
$developer_controller = new DeveloperController();
$developer_controller->register_routes();
// System controller
$system_controller = new SystemController();
$system_controller->register_routes();
// Notifications controller
$notifications_controller = new NotificationsController();
$notifications_controller->register_routes();
// Activity Log controller
$activity_log_controller = new ActivityLogController();
$activity_log_controller->register_routes();
// Products controller
error_log('WooNooW Routes: Registering ProductsController routes');
ProductsController::register_routes();
error_log('WooNooW Routes: ProductsController routes registered');
// Coupons controller
CouponsController::register_routes();
// Customers controller
CustomersController::register_routes();
});
}
}