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:
212
includes/Compat/MetaFieldsRegistry.php
Normal file
212
includes/Compat/MetaFieldsRegistry.php
Normal file
@@ -0,0 +1,212 @@
|
||||
<?php
|
||||
namespace WooNooW\Compat;
|
||||
|
||||
/**
|
||||
* MetaFieldsRegistry
|
||||
*
|
||||
* Allows plugins to register custom meta fields for display in WooNooW admin.
|
||||
* Part of Level 1 compatibility - plugins using standard WP/WooCommerce meta
|
||||
* storage can register their fields to be displayed automatically.
|
||||
*
|
||||
* Zero coupling with specific plugins - just provides the mechanism.
|
||||
*
|
||||
* @package WooNooW\Compat
|
||||
*/
|
||||
class MetaFieldsRegistry {
|
||||
|
||||
/**
|
||||
* Registered order meta fields
|
||||
* @var array
|
||||
*/
|
||||
private static $order_fields = [];
|
||||
|
||||
/**
|
||||
* Registered product meta fields
|
||||
* @var array
|
||||
*/
|
||||
private static $product_fields = [];
|
||||
|
||||
/**
|
||||
* Initialize the registry
|
||||
*/
|
||||
public static function init() {
|
||||
// Allow plugins to register their meta fields
|
||||
add_action('init', [__CLASS__, 'trigger_registration'], 20);
|
||||
|
||||
// Localize fields to JavaScript
|
||||
add_action('admin_enqueue_scripts', [__CLASS__, 'localize_fields']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Trigger the registration action for plugins
|
||||
*/
|
||||
public static function trigger_registration() {
|
||||
/**
|
||||
* Fires when plugins can register their meta fields
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @example
|
||||
* add_action('woonoow/register_meta_fields', function() {
|
||||
* \WooNooW\Compat\MetaFieldsRegistry::register_order_field('_tracking_number', [
|
||||
* 'label' => __('Tracking Number'),
|
||||
* 'type' => 'text',
|
||||
* 'section' => 'Shipment Tracking',
|
||||
* ]);
|
||||
* });
|
||||
*/
|
||||
do_action('woonoow/register_meta_fields');
|
||||
}
|
||||
|
||||
/**
|
||||
* Register an order meta field
|
||||
*
|
||||
* @param string $key Meta key (e.g., '_tracking_number')
|
||||
* @param array $args Field configuration
|
||||
* - label: string - Field label (default: formatted key)
|
||||
* - type: string - Field type: text, textarea, number, select, date, checkbox (default: text)
|
||||
* - section: string - Section name for grouping (default: 'Additional Fields')
|
||||
* - description: string - Help text (default: '')
|
||||
* - placeholder: string - Placeholder text (default: '')
|
||||
* - options: array - For select type: [{value: 'x', label: 'X'}] (default: [])
|
||||
*/
|
||||
public static function register_order_field($key, $args = []) {
|
||||
$defaults = [
|
||||
'key' => $key,
|
||||
'label' => self::format_label($key),
|
||||
'type' => 'text',
|
||||
'section' => 'Additional Fields',
|
||||
'description' => '',
|
||||
'placeholder' => '',
|
||||
'options' => [],
|
||||
];
|
||||
|
||||
self::$order_fields[$key] = array_merge($defaults, $args);
|
||||
|
||||
// Auto-add to allowed meta lists (Level 1 compatibility)
|
||||
self::add_to_allowed_meta('order', $key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a product meta field
|
||||
*
|
||||
* @param string $key Meta key (e.g., '_custom_field')
|
||||
* @param array $args Field configuration (same as register_order_field)
|
||||
*/
|
||||
public static function register_product_field($key, $args = []) {
|
||||
$defaults = [
|
||||
'key' => $key,
|
||||
'label' => self::format_label($key),
|
||||
'type' => 'text',
|
||||
'section' => 'Additional Fields',
|
||||
'description' => '',
|
||||
'placeholder' => '',
|
||||
'options' => [],
|
||||
];
|
||||
|
||||
self::$product_fields[$key] = array_merge($defaults, $args);
|
||||
|
||||
// Auto-add to allowed meta lists (Level 1 compatibility)
|
||||
self::add_to_allowed_meta('product', $key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Format meta key to human-readable label
|
||||
*
|
||||
* @param string $key Meta key
|
||||
* @return string Formatted label
|
||||
*/
|
||||
private static function format_label($key) {
|
||||
// Remove leading underscore
|
||||
$label = ltrim($key, '_');
|
||||
|
||||
// Replace underscores with spaces
|
||||
$label = str_replace('_', ' ', $label);
|
||||
|
||||
// Capitalize words
|
||||
$label = ucwords($label);
|
||||
|
||||
return $label;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add meta key to allowed lists automatically
|
||||
*
|
||||
* @param string $type 'order' or 'product'
|
||||
* @param string $key Meta key
|
||||
*/
|
||||
private static function add_to_allowed_meta($type, $key) {
|
||||
if ($type === 'order') {
|
||||
// Add to allowed private meta (for reading)
|
||||
add_filter('woonoow/order_allowed_private_meta', function($allowed) use ($key) {
|
||||
if (!in_array($key, $allowed, true)) {
|
||||
$allowed[] = $key;
|
||||
}
|
||||
return $allowed;
|
||||
});
|
||||
|
||||
// Add to updatable meta (for writing)
|
||||
add_filter('woonoow/order_updatable_meta', function($allowed) use ($key) {
|
||||
if (!in_array($key, $allowed, true)) {
|
||||
$allowed[] = $key;
|
||||
}
|
||||
return $allowed;
|
||||
});
|
||||
} elseif ($type === 'product') {
|
||||
// Add to allowed private meta (for reading)
|
||||
add_filter('woonoow/product_allowed_private_meta', function($allowed) use ($key) {
|
||||
if (!in_array($key, $allowed, true)) {
|
||||
$allowed[] = $key;
|
||||
}
|
||||
return $allowed;
|
||||
});
|
||||
|
||||
// Add to updatable meta (for writing)
|
||||
add_filter('woonoow/product_updatable_meta', function($allowed) use ($key) {
|
||||
if (!in_array($key, $allowed, true)) {
|
||||
$allowed[] = $key;
|
||||
}
|
||||
return $allowed;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Localize fields to JavaScript
|
||||
* Exposes registered fields to frontend via window.WooNooWMetaFields
|
||||
*/
|
||||
public static function localize_fields() {
|
||||
// Only on admin pages
|
||||
if (!is_admin()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Allow plugins to modify fields before localizing
|
||||
$order_fields = apply_filters('woonoow/meta_fields_orders', array_values(self::$order_fields));
|
||||
$product_fields = apply_filters('woonoow/meta_fields_products', array_values(self::$product_fields));
|
||||
|
||||
// Localize to JavaScript
|
||||
wp_localize_script('woonoow-admin', 'WooNooWMetaFields', [
|
||||
'orders' => $order_fields,
|
||||
'products' => $product_fields,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get registered order fields (for testing/debugging)
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function get_order_fields() {
|
||||
return self::$order_fields;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get registered product fields (for testing/debugging)
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function get_product_fields() {
|
||||
return self::$product_fields;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user