*/ private static array $addons = array(); /** * Whether add-ons have been collected. * * @var bool */ private static bool $initialized = false; /** * Required keys for a valid add-on registration. * * @var string[] */ private const REQUIRED_KEYS = array( 'id', 'name', 'version' ); /** * Valid plan slugs. * * @var string[] */ private const VALID_PLANS = array( 'free', 'pro', 'business', 'agency' ); /** * Register an add-on. * * @param array $addon Add-on data. * * @return bool True if registered successfully. */ public static function register_addon( array $addon ): bool { foreach ( self::REQUIRED_KEYS as $key ) { if ( empty( $addon[ $key ] ) ) { return false; } } // Check for duplicate IDs. foreach ( self::$addons as $existing ) { if ( $existing['id'] === $addon['id'] ) { return false; } } // Validate plan. if ( isset( $addon['requires_plan'] ) && ! in_array( $addon['requires_plan'], self::VALID_PLANS, true ) ) { $addon['requires_plan'] = 'free'; } // Set defaults. $addon = array_merge( array( 'description' => '', 'author' => '', 'author_url' => '', 'requires_plan' => 'free', 'active' => true, ), $addon ); self::$addons[] = $addon; return true; } /** * Get all registered add-ons. * * Applies the aicommerce/addons/registered filter on first call. * * @return array */ public static function get_registered_addons(): array { if ( ! self::$initialized ) { /** * Filters the list of registered AICommerce add-ons. * * Third-party plugins should hook here to register their add-ons. * * @param array $addons Array of registered add-on data. */ $external = apply_filters( 'aicommerce/addons/registered', array() ); if ( is_array( $external ) ) { foreach ( $external as $addon ) { if ( is_array( $addon ) ) { self::register_addon( $addon ); } } } self::$initialized = true; /** * Fires after all add-ons have been registered. * * Third-party plugins can safely initialize their code here, * knowing the registry is complete. */ do_action( 'aicommerce/addons/init' ); } return self::$addons; } /** * Reset the registry (for testing). * * @return void */ public static function reset(): void { self::$addons = array(); self::$initialized = false; } /** * Get add-on count. * * @return int */ public static function count(): int { return count( self::get_registered_addons() ); } /** * Handle REST request to list add-ons. * * @return \WP_REST_Response */ public static function handle_list_addons(): \WP_REST_Response { return new \WP_REST_Response( self::get_registered_addons(), 200 ); } }