This repository has been archived by the owner on Feb 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 219
/
Copy pathIntegrationRegistry.php
179 lines (155 loc) · 5.33 KB
/
IntegrationRegistry.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
namespace Automattic\WooCommerce\Blocks\Integrations;
/**
* Class used for tracking registered integrations with various Block types.
*/
class IntegrationRegistry {
/**
* Integration identifier is used to construct hook names and is given when the integration registry is initialized.
*
* @var string
*/
protected $registry_identifier = '';
/**
* Registered integrations, as `$name => $instance` pairs.
*
* @var IntegrationInterface[]
*/
protected $registered_integrations = [];
/**
* Initializes all registered integrations.
*
* Integration identifier is used to construct hook names and is given when the integration registry is initialized.
*
* @param string $registry_identifier Identifier for this registry.
*/
public function initialize( $registry_identifier = '' ) {
if ( $registry_identifier ) {
$this->registry_identifier = $registry_identifier;
}
if ( empty( $this->registry_identifier ) ) {
_doing_it_wrong( __METHOD__, esc_html( __( 'Integration registry requires an identifier.', 'woo-gutenberg-products-block' ) ) );
return false;
}
/**
* Fires when the IntegrationRegistry is initialized.
*
* Runs before integrations are initialized allowing new integration to be registered for use. This should be
* used as the primary hook for integrations to include their scripts, styles, and other code extending the
* blocks.
*
* @param IntegrationRegistry $this Instance of the IntegrationRegistry class which exposes the IntegrationRegistry::register() method.
*/
do_action( 'woocommerce_blocks_' . $this->registry_identifier . '_registration', $this );
foreach ( $this->get_all_registered() as $registered_integration ) {
$registered_integration->initialize();
}
}
/**
* Registers an integration.
*
* @param IntegrationInterface $integration An instance of IntegrationInterface.
*
* @return boolean True means registered successfully.
*/
public function register( IntegrationInterface $integration ) {
$name = $integration->get_name();
if ( $this->is_registered( $name ) ) {
/* translators: %s: Integration name. */
_doing_it_wrong( __METHOD__, esc_html( sprintf( __( '"%s" is already registered.', 'woo-gutenberg-products-block' ), $name ) ) );
return false;
}
$this->registered_integrations[ $name ] = $integration;
return true;
}
/**
* Checks if an integration is already registered.
*
* @param string $name Integration name.
* @return bool True if the integration is registered, false otherwise.
*/
public function is_registered( $name ) {
return isset( $this->registered_integrations[ $name ] );
}
/**
* Un-register an integration.
*
* @param string|IntegrationInterface $name Integration name, or alternatively a IntegrationInterface instance.
* @return boolean|IntegrationInterface Returns the unregistered integration instance if unregistered successfully.
*/
public function unregister( $name ) {
if ( $name instanceof IntegrationInterface ) {
$name = $name->get_name();
}
if ( ! $this->is_registered( $name ) ) {
/* translators: %s: Integration name. */
_doing_it_wrong( __METHOD__, esc_html( sprintf( __( 'Integration "%s" is not registered.', 'woo-gutenberg-products-block' ), $name ) ) );
return false;
}
$unregistered = $this->registered_integrations[ $name ];
unset( $this->registered_integrations[ $name ] );
return $unregistered;
}
/**
* Retrieves a registered Integration by name.
*
* @param string $name Integration name.
* @return IntegrationInterface|null The registered integration, or null if it is not registered.
*/
public function get_registered( $name ) {
return $this->is_registered( $name ) ? $this->registered_integrations[ $name ] : null;
}
/**
* Retrieves all registered integrations.
*
* @return IntegrationInterface[]
*/
public function get_all_registered() {
return $this->registered_integrations;
}
/**
* Gets an array of all registered integration's script handles for the editor.
*
* @return string[]
*/
public function get_all_registered_editor_script_handles() {
$script_handles = [];
$registered_integrations = $this->get_all_registered();
foreach ( $registered_integrations as $registered_integration ) {
$script_handles = array_merge(
$script_handles,
$registered_integration->get_editor_script_handles()
);
}
return array_unique( array_filter( $script_handles ) );
}
/**
* Gets an array of all registered integration's script handles.
*
* @return string[]
*/
public function get_all_registered_script_handles() {
$script_handles = [];
$registered_integrations = $this->get_all_registered();
foreach ( $registered_integrations as $registered_integration ) {
$script_handles = array_merge(
$script_handles,
$registered_integration->get_script_handles()
);
}
return array_unique( array_filter( $script_handles ) );
}
/**
* Gets an array of all registered integration's script data.
*
* @return array
*/
public function get_all_registered_script_data() {
$script_data = [];
$registered_integrations = $this->get_all_registered();
foreach ( $registered_integrations as $registered_integration ) {
$script_data[ $registered_integration->get_name() . '_data' ] = $registered_integration->get_script_data();
}
return array_filter( $script_data );
}
}