| 1 |
<?php |
| 2 |
|
| 3 |
namespace Better_Payment\Lite\Campaign\Elements; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) { |
| 6 |
exit; |
| 7 |
} |
| 8 |
|
| 9 |
/** |
| 10 |
* Registry for campaign builder element types. |
| 11 |
* |
| 12 |
* Mirrors Fluent Forms' Components.php pattern: a central registry that PHP |
| 13 |
* populates, third-party code can extend via filter, and the JS builder |
| 14 |
* reads via localized data. |
| 15 |
* |
| 16 |
* Usage: |
| 17 |
* ElementRegistry::get_all() — returns all registered elements |
| 18 |
* ElementRegistry::get($type) — returns a single element schema |
| 19 |
* |
| 20 |
* To add custom elements from another plugin: |
| 21 |
* add_filter( 'better_payment/campaign_elements', function( $elements ) { |
| 22 |
* $elements['my_element'] = [ 'type' => 'my_element', ... ]; |
| 23 |
* return $elements; |
| 24 |
* } ); |
| 25 |
*/ |
| 26 |
class ElementRegistry { |
| 27 |
|
| 28 |
/** @var array<string, array> */ |
| 29 |
private static array $elements = []; |
| 30 |
|
| 31 |
/** |
| 32 |
* Register an element type. |
| 33 |
* |
| 34 |
* @param string $type Unique element type key. |
| 35 |
* @param array $schema Element schema (see CampaignElements.php for shape). |
| 36 |
*/ |
| 37 |
public static function register( string $type, array $schema ): void { |
| 38 |
self::$elements[ $type ] = array_merge( $schema, [ 'type' => $type ] ); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Get all registered element schemas, after applying the extension filter. |
| 43 |
* |
| 44 |
* @return array<string, array> |
| 45 |
*/ |
| 46 |
public static function get_all(): array { |
| 47 |
return apply_filters( 'better_payment/campaign_elements', self::$elements ); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Get a single element schema by type. |
| 52 |
* |
| 53 |
* @param string $type |
| 54 |
* @return array|null |
| 55 |
*/ |
| 56 |
public static function get( string $type ): ?array { |
| 57 |
$all = self::get_all(); |
| 58 |
return $all[ $type ] ?? null; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Get default settings for an element type. |
| 63 |
* |
| 64 |
* @param string $type |
| 65 |
* @return array |
| 66 |
*/ |
| 67 |
public static function get_defaults( string $type ): array { |
| 68 |
$schema = self::get( $type ); |
| 69 |
return $schema['defaultSettings'] ?? []; |
| 70 |
} |
| 71 |
} |
| 72 |
|