| 1 |
<?php |
| 2 |
/** |
| 3 |
* Merchant Abilities Bootstrap. |
| 4 |
* |
| 5 |
* One-time initialization for the WP Abilities API integration. |
| 6 |
* Requires all abilities classes, creates the registry, |
| 7 |
* registers the category, and fires the extension hook. |
| 8 |
* |
| 9 |
* @package Merchant |
| 10 |
* @since 2.3.0 |
| 11 |
*/ |
| 12 |
|
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Merchant_Abilities_Bootstrap |
| 19 |
* |
| 20 |
* Entry point for the Merchant Abilities layer. |
| 21 |
* Called from merchant.php behind a `function_exists('wp_register_ability')` guard. |
| 22 |
* |
| 23 |
* @since 2.3.0 |
| 24 |
*/ |
| 25 |
class Merchant_Abilities_Bootstrap { |
| 26 |
|
| 27 |
/** |
| 28 |
* Whether init() has been called. |
| 29 |
* |
| 30 |
* @var bool |
| 31 |
*/ |
| 32 |
private static $initialized = false; |
| 33 |
|
| 34 |
/** |
| 35 |
* The abilities registry, held until wp_abilities_api_init fires. |
| 36 |
* |
| 37 |
* @var Merchant_Abilities_Registry|null |
| 38 |
*/ |
| 39 |
private static $registry = null; |
| 40 |
|
| 41 |
/** |
| 42 |
* Initialize the abilities layer. |
| 43 |
* |
| 44 |
* Idempotent — safe to call multiple times. |
| 45 |
* |
| 46 |
* @return void |
| 47 |
*/ |
| 48 |
public static function init() { |
| 49 |
if ( self::$initialized ) { |
| 50 |
return; |
| 51 |
} |
| 52 |
|
| 53 |
self::$initialized = true; |
| 54 |
self::require_files(); |
| 55 |
|
| 56 |
// Register the "merchant" ability category. |
| 57 |
Merchant_Abilities_Category::register(); |
| 58 |
|
| 59 |
// Build core services with dependency injection. |
| 60 |
$schema_generator = new Merchant_Schema_Generator(); |
| 61 |
|
| 62 |
// Validator resolves the field registry lazily: its field classes load on `init`, after this plugins_loaded bootstrap. |
| 63 |
$field_validator = new Merchant_Field_Validator( $schema_generator ); |
| 64 |
$campaign_resolver = new Merchant_Campaign_Resolver(); |
| 65 |
|
| 66 |
// Build the registry (no wp_register_ability calls yet). |
| 67 |
$registry = new Merchant_Abilities_Registry( |
| 68 |
$schema_generator, |
| 69 |
$field_validator, |
| 70 |
$campaign_resolver |
| 71 |
); |
| 72 |
|
| 73 |
// Wire the product-settings pair's contribution hook (init() only adds |
| 74 |
// the action below — no strings, no def building at this point). |
| 75 |
Merchant_Product_Settings_Abilities::init(); |
| 76 |
|
| 77 |
/** |
| 78 |
* Allows Merchant Pro and third-party plugins to extend the abilities |
| 79 |
* layer in two ways: |
| 80 |
* |
| 81 |
* 1. Register custom MODULE ADAPTERS (campaign summary formatters) |
| 82 |
* via $registry->register_adapter(). Pro does not extend field |
| 83 |
* definitions via any filter — all 'pro' => true fields already |
| 84 |
* live in core. |
| 85 |
* 2. Contribute additional ability definitions via |
| 86 |
* $registry->register_ability_definition() — the seam |
| 87 |
* Merchant_Product_Settings_Abilities and Merchant Pro's |
| 88 |
* Merchant_Pro_Abilities_Registrar both use, deferring their |
| 89 |
* definition builds to wp_abilities_api_init so no __() call |
| 90 |
* ever loads a textdomain before `init`. |
| 91 |
* |
| 92 |
* @param Merchant_Abilities_Registry $registry The abilities registry. |
| 93 |
* |
| 94 |
* @since 2.3.0 |
| 95 |
*/ |
| 96 |
do_action( 'merchant_abilities_register_modules', $registry ); |
| 97 |
|
| 98 |
// Abilities must be registered on the wp_abilities_api_init action. |
| 99 |
self::$registry = $registry; |
| 100 |
add_action( 'wp_abilities_api_init', array( __CLASS__, 'register_abilities' ) ); |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Register all abilities with WordPress. |
| 105 |
* |
| 106 |
* Fires on wp_abilities_api_init as required by the WP Abilities API. |
| 107 |
* |
| 108 |
* @return void |
| 109 |
*/ |
| 110 |
public static function register_abilities() { |
| 111 |
if ( self::$registry ) { |
| 112 |
self::$registry->register_all_abilities(); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Require all abilities layer files. |
| 118 |
* |
| 119 |
* @return void |
| 120 |
*/ |
| 121 |
private static function require_files() { |
| 122 |
$dir = MERCHANT_DIR . 'inc/abilities/'; |
| 123 |
|
| 124 |
// Core infrastructure. |
| 125 |
require_once $dir . 'class-merchant-abilities-category.php'; |
| 126 |
require_once $dir . 'class-merchant-abilities-registry.php'; |
| 127 |
require_once $dir . 'class-merchant-abilities-permissions.php'; |
| 128 |
require_once $dir . 'class-merchant-schema-generator.php'; |
| 129 |
require_once $dir . 'class-merchant-field-validator.php'; |
| 130 |
require_once $dir . 'class-merchant-campaign-resolver.php'; |
| 131 |
|
| 132 |
// Module adapters. |
| 133 |
require_once $dir . 'module-adapters/interface-merchant-module-abilities.php'; |
| 134 |
require_once $dir . 'module-adapters/class-merchant-default-module-adapter.php'; |
| 135 |
|
| 136 |
// Handlers. |
| 137 |
$handlers_dir = $dir . 'handlers/'; |
| 138 |
// Handler base class (must be loaded before concrete handlers). |
| 139 |
require_once $handlers_dir . 'class-merchant-abstract-handler.php'; |
| 140 |
require_once $handlers_dir . 'class-merchant-list-modules-handler.php'; |
| 141 |
require_once $handlers_dir . 'class-merchant-toggle-module-handler.php'; |
| 142 |
require_once $handlers_dir . 'class-merchant-get-settings-handler.php'; |
| 143 |
require_once $handlers_dir . 'class-merchant-update-settings-handler.php'; |
| 144 |
require_once $handlers_dir . 'class-merchant-list-campaigns-handler.php'; |
| 145 |
require_once $handlers_dir . 'class-merchant-create-campaign-handler.php'; |
| 146 |
require_once $handlers_dir . 'class-merchant-update-campaign-handler.php'; |
| 147 |
require_once $handlers_dir . 'class-merchant-delete-campaign-handler.php'; |
| 148 |
require_once $handlers_dir . 'class-merchant-get-analytics-handler.php'; |
| 149 |
require_once $handlers_dir . 'class-merchant-get-recommendations-handler.php'; |
| 150 |
|
| 151 |
// Recommendations engine. |
| 152 |
require_once $dir . 'class-merchant-recommendations-engine.php'; |
| 153 |
|
| 154 |
// Pro extends the module set via the merchant_product_settings_metabox_classes filter. |
| 155 |
$product_settings_dir = $dir . 'product-settings/'; |
| 156 |
require_once $product_settings_dir . 'class-merchant-product-settings-validator.php'; |
| 157 |
require_once $product_settings_dir . 'class-merchant-product-settings-registry.php'; |
| 158 |
require_once $handlers_dir . 'class-merchant-get-product-settings-handler.php'; |
| 159 |
require_once $handlers_dir . 'class-merchant-update-product-settings-handler.php'; |
| 160 |
require_once $product_settings_dir . 'class-merchant-product-settings-abilities.php'; |
| 161 |
} |
| 162 |
} |
| 163 |
|