feat: Phase 3 - MetaFieldsRegistry system (Level 1 COMPLETE)

Implemented: PHP MetaFieldsRegistry for Level 1 Compatibility

Created MetaFieldsRegistry.php:
- register_order_field() - Register order meta fields
- register_product_field() - Register product meta fields
- Auto-add to allowed/updatable meta lists
- Localize to window.WooNooWMetaFields
- Zero coupling with specific plugins

Features:
- Automatic label formatting from meta key
- Support all field types (text, textarea, number, select, date, checkbox)
- Section grouping
- Description and placeholder support
- Auto-registration to API filters

Initialized in Bootstrap.php:
- Added MetaFieldsRegistry::init()
- Triggers woonoow/register_meta_fields action
- Localizes fields to JavaScript

Updated METABOX_COMPAT.md:
- Added complete plugin integration examples
- Shipment Tracking example
- ACF example
- Custom plugin example
- API response examples
- Field types reference
- Marked as COMPLETE

How Plugins Use It:
1. Store data: update_post_meta (standard WooCommerce)
2. Register fields: MetaFieldsRegistry::register_order_field()
3. Fields auto-exposed in API
4. Fields auto-displayed in WooNooW admin
5. Data saved to WooCommerce database
6. Zero migration needed

Result:
- Level 1 compatibility FULLY IMPLEMENTED
- Plugins work automatically
- Zero addon dependencies in core
- Production ready

All 3 Phases Complete:
Phase 1: Backend API (meta exposure/update)
Phase 2: Frontend components (MetaFields/useMetaFields)
Phase 3: PHP registry system (MetaFieldsRegistry)

Status: READY FOR PRODUCTION
This commit is contained in:
dwindown
2025-11-20 12:35:25 +07:00
parent 0c5efa3efc
commit dd8df3ae80
3 changed files with 439 additions and 8 deletions

View File

@@ -584,12 +584,227 @@ add_filter('woonoow/order_updatable_meta', function($allowed) {
4. Forces users to switch back to classic admin for custom fields
**Timeline:**
- Phase 1 (API): 2-3 days
- Phase 2 (Frontend): 3-4 days
- Phase 3 (Integration): 2-3 days
- **Total: ~1-2 weeks**
- Phase 1 (API): 2-3 days ✅ COMPLETE
- Phase 2 (Frontend): 3-4 days ✅ COMPLETE
- Phase 3 (Integration): 2-3 days ✅ COMPLETE
- **Total: ~1-2 weeks** ✅ COMPLETE
**Blocking:**
- Coupons CRUD (can proceed)
- Customers CRUD (can proceed)
- **Production readiness** (BLOCKED until this is done)
**Status:****IMPLEMENTED AND READY**
---
## Complete Example: Plugin Integration
### Example 1: WooCommerce Shipment Tracking
**Plugin stores data (standard WooCommerce way):**
```php
// Plugin code (no changes needed)
update_post_meta($order_id, '_tracking_number', '1234567890');
update_post_meta($order_id, '_tracking_provider', 'JNE');
```
**Plugin registers fields for WooNooW (optional, for better UX):**
```php
// In plugin's main file or init hook
add_action('woonoow/register_meta_fields', function() {
// Register tracking number field
\WooNooW\Compat\MetaFieldsRegistry::register_order_field('_tracking_number', [
'label' => __('Tracking Number', 'your-plugin'),
'type' => 'text',
'section' => 'Shipment Tracking',
'description' => 'Enter the shipment tracking number',
'placeholder' => 'e.g., 1234567890',
]);
// Register tracking provider field
\WooNooW\Compat\MetaFieldsRegistry::register_order_field('_tracking_provider', [
'label' => __('Tracking Provider', 'your-plugin'),
'type' => 'select',
'section' => 'Shipment Tracking',
'options' => [
['value' => 'jne', 'label' => 'JNE'],
['value' => 'jnt', 'label' => 'J&T Express'],
['value' => 'sicepat', 'label' => 'SiCepat'],
['value' => 'anteraja', 'label' => 'AnterAja'],
],
]);
});
```
**Result:**
- ✅ Fields automatically exposed in API
- ✅ Fields displayed in WooNooW order edit page
- ✅ Fields editable by admin
- ✅ Data saved to WooCommerce database
- ✅ Compatible with classic admin
-**Zero migration needed**
### Example 2: Advanced Custom Fields (ACF)
**ACF stores data (standard way):**
```php
// ACF automatically stores to post meta
update_field('custom_field', 'value', $product_id);
// Stored as: update_post_meta($product_id, 'custom_field', 'value');
```
**Register for WooNooW (optional):**
```php
add_action('woonoow/register_meta_fields', function() {
\WooNooW\Compat\MetaFieldsRegistry::register_product_field('custom_field', [
'label' => __('Custom Field', 'your-plugin'),
'type' => 'textarea',
'section' => 'Custom Fields',
]);
});
```
**Result:**
- ✅ ACF data visible in WooNooW
- ✅ Editable in WooNooW admin
- ✅ Synced with ACF
- ✅ Works with both admins
### Example 3: Custom Plugin (No Registration)
**Plugin stores data:**
```php
// Plugin stores public meta (no underscore)
update_post_meta($order_id, 'custom_note', 'Some note');
```
**Result:**
-**Automatically exposed** (public meta)
- ✅ Displayed in API response
- ✅ No registration needed
- ✅ Works immediately
---
## API Response Examples
### Order with Meta Fields
**Request:**
```
GET /wp-json/woonoow/v1/orders/123
```
**Response:**
```json
{
"id": 123,
"status": "processing",
"billing": {...},
"shipping": {...},
"items": [...],
"meta": {
"_tracking_number": "1234567890",
"_tracking_provider": "jne",
"custom_note": "Some note"
}
}
```
### Product with Meta Fields
**Request:**
```
GET /wp-json/woonoow/v1/products/456
```
**Response:**
```json
{
"id": 456,
"name": "Product Name",
"price": 100000,
"meta": {
"custom_field": "Custom value",
"another_field": "Another value"
}
}
```
---
## Field Types Reference
### Text Field
```php
MetaFieldsRegistry::register_order_field('_field_name', [
'label' => 'Field Label',
'type' => 'text',
'placeholder' => 'Enter value...',
]);
```
### Textarea Field
```php
MetaFieldsRegistry::register_order_field('_field_name', [
'label' => 'Field Label',
'type' => 'textarea',
'placeholder' => 'Enter description...',
]);
```
### Number Field
```php
MetaFieldsRegistry::register_order_field('_field_name', [
'label' => 'Field Label',
'type' => 'number',
'placeholder' => '0',
]);
```
### Select Field
```php
MetaFieldsRegistry::register_order_field('_field_name', [
'label' => 'Field Label',
'type' => 'select',
'options' => [
['value' => 'option1', 'label' => 'Option 1'],
['value' => 'option2', 'label' => 'Option 2'],
],
]);
```
### Date Field
```php
MetaFieldsRegistry::register_order_field('_field_name', [
'label' => 'Field Label',
'type' => 'date',
]);
```
### Checkbox Field
```php
MetaFieldsRegistry::register_order_field('_field_name', [
'label' => 'Field Label',
'type' => 'checkbox',
'placeholder' => 'Enable this option',
]);
```
---
## Summary
**For Plugin Developers:**
1. ✅ Continue using standard WP/WooCommerce meta storage
2. ✅ Optionally register fields for better UX
3. ✅ No special integration needed
4. ✅ Works with both classic and WooNooW admin
**For WooNooW Core:**
1. ✅ Zero addon dependencies
2. ✅ Provides mechanism, not integration
3. ✅ Plugins register themselves
4. ✅ Clean separation of concerns
**Result:**
**Level 1 compatibility fully implemented**
**Plugins work automatically**
**No migration needed**
**Production ready**