finalizing subscription moduile, ready to test
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Module Registry
|
||||
*
|
||||
@@ -10,14 +11,16 @@
|
||||
|
||||
namespace WooNooW\Core;
|
||||
|
||||
class ModuleRegistry {
|
||||
|
||||
class ModuleRegistry
|
||||
{
|
||||
|
||||
/**
|
||||
* Get built-in modules
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private static function get_builtin_modules() {
|
||||
private static function get_builtin_modules()
|
||||
{
|
||||
$modules = [
|
||||
'newsletter' => [
|
||||
'id' => 'newsletter',
|
||||
@@ -68,6 +71,7 @@ class ModuleRegistry {
|
||||
'category' => 'products',
|
||||
'icon' => 'refresh-cw',
|
||||
'default_enabled' => false,
|
||||
'has_settings' => true,
|
||||
'features' => [
|
||||
__('Recurring billing', 'woonoow'),
|
||||
__('Subscription management', 'woonoow'),
|
||||
@@ -91,19 +95,20 @@ class ModuleRegistry {
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
|
||||
return $modules;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get addon modules from AddonRegistry
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private static function get_addon_modules() {
|
||||
private static function get_addon_modules()
|
||||
{
|
||||
$addons = apply_filters('woonoow/addon_registry', []);
|
||||
$modules = [];
|
||||
|
||||
|
||||
foreach ($addons as $addon_id => $addon) {
|
||||
$modules[$addon_id] = [
|
||||
'id' => $addon_id,
|
||||
@@ -120,31 +125,33 @@ class ModuleRegistry {
|
||||
'settings_component' => $addon['settings_component'] ?? null,
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
return $modules;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get all modules (built-in + addons)
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function get_all_modules() {
|
||||
public static function get_all_modules()
|
||||
{
|
||||
$builtin = self::get_builtin_modules();
|
||||
$addons = self::get_addon_modules();
|
||||
|
||||
|
||||
return array_merge($builtin, $addons);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get categories dynamically from registered modules
|
||||
*
|
||||
* @return array Associative array of category_id => label
|
||||
*/
|
||||
public static function get_categories() {
|
||||
public static function get_categories()
|
||||
{
|
||||
$all_modules = self::get_all_modules();
|
||||
$categories = [];
|
||||
|
||||
|
||||
// Extract unique categories from modules
|
||||
foreach ($all_modules as $module) {
|
||||
$cat = $module['category'] ?? 'other';
|
||||
@@ -152,27 +159,28 @@ class ModuleRegistry {
|
||||
$categories[$cat] = self::get_category_label($cat);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Sort by predefined order
|
||||
$order = ['marketing', 'customers', 'products', 'shipping', 'payments', 'analytics', 'other'];
|
||||
uksort($categories, function($a, $b) use ($order) {
|
||||
uksort($categories, function ($a, $b) use ($order) {
|
||||
$pos_a = array_search($a, $order);
|
||||
$pos_b = array_search($b, $order);
|
||||
if ($pos_a === false) $pos_a = 999;
|
||||
if ($pos_b === false) $pos_b = 999;
|
||||
return $pos_a - $pos_b;
|
||||
});
|
||||
|
||||
|
||||
return $categories;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get human-readable label for category
|
||||
*
|
||||
* @param string $category Category ID
|
||||
* @return string
|
||||
*/
|
||||
private static function get_category_label($category) {
|
||||
private static function get_category_label($category)
|
||||
{
|
||||
$labels = [
|
||||
'marketing' => __('Marketing & Sales', 'woonoow'),
|
||||
'customers' => __('Customer Experience', 'woonoow'),
|
||||
@@ -182,19 +190,20 @@ class ModuleRegistry {
|
||||
'analytics' => __('Analytics & Reports', 'woonoow'),
|
||||
'other' => __('Other Extensions', 'woonoow'),
|
||||
];
|
||||
|
||||
|
||||
return $labels[$category] ?? ucfirst($category);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Group modules by category
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function get_grouped_modules() {
|
||||
public static function get_grouped_modules()
|
||||
{
|
||||
$all_modules = self::get_all_modules();
|
||||
$grouped = [];
|
||||
|
||||
|
||||
foreach ($all_modules as $module) {
|
||||
$cat = $module['category'] ?? 'other';
|
||||
if (!isset($grouped[$cat])) {
|
||||
@@ -202,18 +211,19 @@ class ModuleRegistry {
|
||||
}
|
||||
$grouped[$cat][] = $module;
|
||||
}
|
||||
|
||||
|
||||
return $grouped;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get enabled modules
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function get_enabled_modules() {
|
||||
public static function get_enabled_modules()
|
||||
{
|
||||
$enabled = get_option('woonoow_enabled_modules', null);
|
||||
|
||||
|
||||
// First time - use defaults
|
||||
if ($enabled === null) {
|
||||
$modules = self::get_all_modules();
|
||||
@@ -225,89 +235,93 @@ class ModuleRegistry {
|
||||
}
|
||||
update_option('woonoow_enabled_modules', $enabled);
|
||||
}
|
||||
|
||||
|
||||
return $enabled;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Check if a module is enabled
|
||||
*
|
||||
* @param string $module_id
|
||||
* @return bool
|
||||
*/
|
||||
public static function is_enabled($module_id) {
|
||||
public static function is_enabled($module_id)
|
||||
{
|
||||
$enabled = self::get_enabled_modules();
|
||||
return in_array($module_id, $enabled);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Enable a module
|
||||
*
|
||||
* @param string $module_id
|
||||
* @return bool
|
||||
*/
|
||||
public static function enable($module_id) {
|
||||
public static function enable($module_id)
|
||||
{
|
||||
$modules = self::get_all_modules();
|
||||
|
||||
|
||||
if (!isset($modules[$module_id])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
$enabled = self::get_enabled_modules();
|
||||
|
||||
|
||||
if (!in_array($module_id, $enabled)) {
|
||||
$enabled[] = $module_id;
|
||||
update_option('woonoow_enabled_modules', $enabled);
|
||||
|
||||
|
||||
// Clear navigation cache when module is toggled
|
||||
if (class_exists('\WooNooW\Compat\NavigationRegistry')) {
|
||||
\WooNooW\Compat\NavigationRegistry::flush();
|
||||
}
|
||||
|
||||
|
||||
do_action('woonoow/module/enabled', $module_id);
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Disable a module
|
||||
*
|
||||
* @param string $module_id
|
||||
* @return bool
|
||||
*/
|
||||
public static function disable($module_id) {
|
||||
public static function disable($module_id)
|
||||
{
|
||||
$enabled = self::get_enabled_modules();
|
||||
|
||||
|
||||
if (in_array($module_id, $enabled)) {
|
||||
$enabled = array_diff($enabled, [$module_id]);
|
||||
update_option('woonoow_enabled_modules', array_values($enabled));
|
||||
|
||||
|
||||
// Clear navigation cache when module is toggled
|
||||
if (class_exists('\WooNooW\Compat\NavigationRegistry')) {
|
||||
\WooNooW\Compat\NavigationRegistry::flush();
|
||||
}
|
||||
|
||||
|
||||
do_action('woonoow/module/disabled', $module_id);
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get modules by category
|
||||
*
|
||||
* @param string $category
|
||||
* @return array
|
||||
*/
|
||||
public static function get_by_category($category) {
|
||||
public static function get_by_category($category)
|
||||
{
|
||||
$modules = self::get_all_modules();
|
||||
$enabled = self::get_enabled_modules();
|
||||
|
||||
|
||||
$result = [];
|
||||
foreach ($modules as $module) {
|
||||
if ($module['category'] === $category) {
|
||||
@@ -315,23 +329,63 @@ class ModuleRegistry {
|
||||
$result[] = $module;
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get all modules with enabled status
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function get_all_with_status() {
|
||||
public static function get_all_with_status()
|
||||
{
|
||||
$modules = self::get_all_modules();
|
||||
$enabled = self::get_enabled_modules();
|
||||
|
||||
foreach ($modules as $id => $module) {
|
||||
$modules[$id]['enabled'] = in_array($id, $enabled);
|
||||
|
||||
foreach ($modules as $id => &$module) {
|
||||
$module['enabled'] = in_array($id, $enabled);
|
||||
}
|
||||
|
||||
|
||||
return $modules;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get module settings
|
||||
*
|
||||
* @param string $module_id
|
||||
* @return array
|
||||
*/
|
||||
public static function get_settings($module_id)
|
||||
{
|
||||
$settings = get_option("woonoow_module_{$module_id}_settings", []);
|
||||
|
||||
// Apply defaults from schema if available
|
||||
$schema = apply_filters('woonoow/module_settings_schema', []);
|
||||
|
||||
if (isset($schema[$module_id])) {
|
||||
$defaults = self::get_schema_defaults($schema[$module_id]);
|
||||
$settings = wp_parse_args($settings, $defaults);
|
||||
}
|
||||
|
||||
return $settings;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get default values from schema
|
||||
*
|
||||
* @param array $schema
|
||||
* @return array
|
||||
*/
|
||||
private static function get_schema_defaults($schema)
|
||||
{
|
||||
$defaults = [];
|
||||
|
||||
foreach ($schema as $key => $field) {
|
||||
if (isset($field['default'])) {
|
||||
$defaults[$key] = $field['default'];
|
||||
}
|
||||
}
|
||||
|
||||
return $defaults;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user