Files
WooNooW/includes/Api/ModulesController.php
Dwindi Ramadhana c6cef97ef8 feat: Implement Phase 2, 3, 4 - Module Settings System with Schema Forms and Addon API
Phase 2: Schema-Based Form System
- Add ModuleSettingsController with GET/POST/schema endpoints
- Create SchemaField component supporting 8 field types (text, textarea, email, url, number, toggle, checkbox, select)
- Create SchemaForm component for automatic form generation from schema
- Add ModuleSettings page with dynamic routing (/settings/modules/:moduleId)
- Add useModuleSettings React hook for settings management
- Implement NewsletterSettings as example with 8 configurable fields
- Add has_settings flag to module registry
- Settings stored as woonoow_module_{module_id}_settings

Phase 3: Advanced Features
- Create windowAPI.ts exposing React, hooks, components, icons, utils to addons via window.WooNooW
- Add DynamicComponentLoader for loading external React components
- Create TypeScript definitions (woonoow-addon.d.ts) for addon developers
- Initialize Window API in App.tsx on mount
- Enable custom React components for addon settings pages

Phase 4: Production Polish & Example
- Create complete Biteship addon example demonstrating both approaches:
  * Schema-based settings (no build required)
  * Custom React component (with build)
- Add comprehensive README with installation and testing guide
- Include package.json with esbuild configuration
- Demonstrate window.WooNooW API usage in custom component

Bug Fixes:
- Fix footer newsletter form visibility (remove redundant module check)
- Fix footer contact_data and social_links not saving (parameter name mismatch: snake_case vs camelCase)
- Fix useModules hook returning undefined (remove .data wrapper, add fallback)
- Add optional chaining to footer settings rendering
- Fix TypeScript errors in woonoow-addon.d.ts (use any for external types)

Files Added (15):
- includes/Api/ModuleSettingsController.php
- includes/Modules/NewsletterSettings.php
- admin-spa/src/components/forms/SchemaField.tsx
- admin-spa/src/components/forms/SchemaForm.tsx
- admin-spa/src/routes/Settings/ModuleSettings.tsx
- admin-spa/src/hooks/useModuleSettings.ts
- admin-spa/src/lib/windowAPI.ts
- admin-spa/src/components/DynamicComponentLoader.tsx
- types/woonoow-addon.d.ts
- examples/biteship-addon/biteship-addon.php
- examples/biteship-addon/src/Settings.jsx
- examples/biteship-addon/package.json
- examples/biteship-addon/README.md
- PHASE_2_3_4_SUMMARY.md

Files Modified (11):
- admin-spa/src/App.tsx
- admin-spa/src/hooks/useModules.ts
- admin-spa/src/routes/Appearance/Footer.tsx
- admin-spa/src/routes/Settings/Modules.tsx
- customer-spa/src/hooks/useModules.ts
- customer-spa/src/layouts/BaseLayout.tsx
- customer-spa/src/components/NewsletterForm.tsx
- includes/Api/Routes.php
- includes/Api/ModulesController.php
- includes/Core/ModuleRegistry.php
- woonoow.php

API Endpoints Added:
- GET /woonoow/v1/modules/{module_id}/settings
- POST /woonoow/v1/modules/{module_id}/settings
- GET /woonoow/v1/modules/{module_id}/schema

For Addon Developers:
- Schema-based: Define settings via woonoow/module_settings_schema filter
- Custom React: Build component using window.WooNooW API, externalize react/react-dom
- Both approaches use same storage and retrieval methods
- TypeScript definitions provided for type safety
- Complete working example (Biteship) included
2025-12-26 21:16:06 +07:00

164 lines
4.6 KiB
PHP

<?php
/**
* Modules REST API Controller
*
* @package WooNooW\Api
*/
namespace WooNooW\Api;
use WP_REST_Controller;
use WP_REST_Server;
use WP_REST_Request;
use WP_REST_Response;
use WP_Error;
use WooNooW\Core\ModuleRegistry;
class ModulesController extends WP_REST_Controller {
/**
* REST API namespace
*/
protected $namespace = 'woonoow/v1';
/**
* REST API base
*/
protected $rest_base = 'modules';
/**
* Register routes
*/
public function register_routes() {
// GET /woonoow/v1/modules
register_rest_route($this->namespace, '/' . $this->rest_base, [
[
'methods' => WP_REST_Server::READABLE,
'callback' => [$this, 'get_modules'],
'permission_callback' => [$this, 'check_permission'],
],
]);
// POST /woonoow/v1/modules/toggle
register_rest_route($this->namespace, '/' . $this->rest_base . '/toggle', [
[
'methods' => WP_REST_Server::CREATABLE,
'callback' => [$this, 'toggle_module'],
'permission_callback' => [$this, 'check_permission'],
'args' => [
'module_id' => [
'required' => true,
'type' => 'string',
'sanitize_callback' => 'sanitize_text_field',
],
'enabled' => [
'required' => true,
'type' => 'boolean',
],
],
],
]);
// GET /woonoow/v1/modules/enabled (public endpoint for frontend)
register_rest_route($this->namespace, '/' . $this->rest_base . '/enabled', [
[
'methods' => WP_REST_Server::READABLE,
'callback' => [$this, 'get_enabled_modules'],
'permission_callback' => '__return_true',
],
]);
}
/**
* Check permission
*
* @return bool
*/
public function check_permission() {
return current_user_can('manage_options');
}
/**
* Get all modules with status
*
* @param WP_REST_Request $request
* @return WP_REST_Response
*/
public function get_modules($request) {
$modules = ModuleRegistry::get_all_with_status();
$grouped = ModuleRegistry::get_grouped_modules();
// Add enabled status to grouped modules
$enabled_modules = ModuleRegistry::get_enabled_modules();
foreach ($grouped as $category => &$category_modules) {
foreach ($category_modules as &$module) {
$module['enabled'] = in_array($module['id'], $enabled_modules);
}
}
return new WP_REST_Response([
'modules' => $modules,
'grouped' => $grouped,
'categories' => ModuleRegistry::get_categories(),
], 200);
}
/**
* Toggle module enabled/disabled
*
* @param WP_REST_Request $request
* @return WP_REST_Response|WP_Error
*/
public function toggle_module($request) {
$module_id = $request->get_param('module_id');
$enabled = $request->get_param('enabled');
$modules = ModuleRegistry::get_all_modules();
if (!isset($modules[$module_id])) {
return new WP_Error(
'invalid_module',
__('Invalid module ID', 'woonoow'),
['status' => 400]
);
}
if ($enabled) {
$result = ModuleRegistry::enable($module_id);
} else {
$result = ModuleRegistry::disable($module_id);
}
if ($result) {
return new WP_REST_Response([
'success' => true,
'message' => $enabled
? __('Module enabled successfully', 'woonoow')
: __('Module disabled successfully', 'woonoow'),
'module_id' => $module_id,
'enabled' => $enabled,
], 200);
}
return new WP_Error(
'toggle_failed',
__('Failed to toggle module', 'woonoow'),
['status' => 500]
);
}
/**
* Get enabled modules (public endpoint)
*
* @param WP_REST_Request $request
* @return WP_REST_Response
*/
public function get_enabled_modules($request) {
$enabled = ModuleRegistry::get_enabled_modules();
return new WP_REST_Response([
'enabled' => $enabled,
], 200);
}
}