PluginProbe
Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More / 2.2.0
Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More v2.2.0
2.3.4 2.3.3 2.3.2 2.3.1 2.3.0 2.2.2 2.2.1 2.2.0 2.1.2 2.1.1 trunk 0.0.1 0.0.2 0.0.3 0.0.4 0.0.5 0.0.6 0.0.7 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 All 66 releases
better-payment / includes / Campaign / Elements / ElementRegistry.php

ElementRegistry.php in Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More 2.2.0, at includes/Campaign/Elements/ElementRegistry.php

72 lines 1.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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