PluginProbe
Gutenberg / 23.1.0
Gutenberg v23.1.0
23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 7.4.0 All 402 releases
gutenberg / lib / compat / wordpress-7.0 / connectors.php

connectors.php in Gutenberg 23.1.0, at lib/compat/wordpress-7.0/connectors.php

199 lines 6.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Connectors API functions.
4 *
5 * @package gutenberg
6 * @since 7.0.0
7 */
8
9 if ( ! function_exists( 'wp_is_connector_registered' ) ) {
10 /**
11 * Checks if a connector is registered.
12 *
13 * @since 7.0.0
14 *
15 * @see WP_Connector_Registry::is_registered()
16 *
17 * @param string $id The connector identifier.
18 * @return bool True if the connector is registered, false otherwise.
19 */
20 function wp_is_connector_registered( string $id ): bool {
21 $registry = WP_Connector_Registry::get_instance();
22 if ( null === $registry ) {
23 return false;
24 }
25
26 return $registry->is_registered( $id );
27 }
28 }
29
30 if ( ! function_exists( 'wp_get_connector' ) ) {
31 /**
32 * Retrieves a registered connector.
33 *
34 * @since 7.0.0
35 *
36 * @see WP_Connector_Registry::get_registered()
37 *
38 * @param string $id The connector identifier.
39 * @return array|null {
40 * Connector data, or null if not registered.
41 *
42 * @type string $name The connector's display name.
43 * @type string $description The connector's description.
44 * @type string $logo_url Optional. URL to the connector's logo image.
45 * @type string $type The connector type, e.g. 'ai_provider' or 'spam_filtering'.
46 * @type array $authentication {
47 * Authentication configuration. When method is 'api_key', includes
48 * credentials_url, setting_name, and optionally constant_name and
49 * env_var_name. When 'none', only method is present.
50 *
51 * @type string $method The authentication method: 'api_key' or 'none'.
52 * @type string $credentials_url Optional. URL where users can obtain API credentials.
53 * @type string $setting_name Optional. The setting name for the API key.
54 * @type string $constant_name Optional. PHP constant name for the API key.
55 * @type string $env_var_name Optional. Environment variable name for the API key.
56 * }
57 * @type array $plugin {
58 * Optional. Plugin data for install/activate UI.
59 *
60 * @type string $slug The WordPress.org plugin slug.
61 * }
62 * }
63 * @phpstan-return ?array{
64 * name: non-empty-string,
65 * description: non-empty-string,
66 * logo_url?: non-empty-string,
67 * type: non-empty-string,
68 * authentication: array{
69 * method: 'api_key'|'none',
70 * credentials_url?: non-empty-string,
71 * setting_name?: non-empty-string,
72 * constant_name?: non-empty-string,
73 * env_var_name?: non-empty-string
74 * },
75 * plugin?: array{
76 * slug: non-empty-string
77 * }
78 * }
79 */
80 function wp_get_connector( string $id ): ?array {
81 $registry = WP_Connector_Registry::get_instance();
82 if ( null === $registry ) {
83 return null;
84 }
85
86 return $registry->get_registered( $id );
87 }
88 }
89
90 if ( ! function_exists( 'wp_get_connectors' ) ) {
91 /**
92 * Retrieves all registered connectors.
93 *
94 * @since 7.0.0
95 *
96 * @see WP_Connector_Registry::get_all_registered()
97 *
98 * @return array {
99 * Connector settings keyed by connector ID.
100 *
101 * @type array ...$0 {
102 * Data for a single connector.
103 *
104 * @type string $name The connector's display name.
105 * @type string $description The connector's description.
106 * @type string $logo_url Optional. URL to the connector's logo image.
107 * @type string $type The connector type, e.g. 'ai_provider' or 'spam_filtering'.
108 * @type array $authentication {
109 * Authentication configuration. When method is 'api_key', includes
110 * credentials_url, setting_name, and optionally constant_name and
111 * env_var_name. When 'none', only method is present.
112 *
113 * @type string $method The authentication method: 'api_key' or 'none'.
114 * @type string $credentials_url Optional. URL where users can obtain API credentials.
115 * @type string $setting_name Optional. The setting name for the API key.
116 * @type string $constant_name Optional. PHP constant name for the API key.
117 * @type string $env_var_name Optional. Environment variable name for the API key.
118 * }
119 * @type array $plugin {
120 * Optional. Plugin data for install/activate UI.
121 *
122 * @type string $slug The WordPress.org plugin slug.
123 * }
124 * }
125 * }
126 * @phpstan-return array<string, array{
127 * name: non-empty-string,
128 * description: non-empty-string,
129 * logo_url?: non-empty-string,
130 * type: non-empty-string,
131 * authentication: array{
132 * method: 'api_key'|'none',
133 * credentials_url?: non-empty-string,
134 * setting_name?: non-empty-string,
135 * constant_name?: non-empty-string,
136 * env_var_name?: non-empty-string
137 * },
138 * plugin?: array{
139 * slug: non-empty-string
140 * }
141 * }>
142 */
143 function wp_get_connectors(): array {
144 $registry = WP_Connector_Registry::get_instance();
145 if ( null === $registry ) {
146 return array();
147 }
148
149 return $registry->get_all_registered();
150 }
151 }
152
153 if ( ! function_exists( '_wp_connectors_resolve_ai_provider_logo_url' ) ) {
154 /**
155 * Resolves an AI provider logo file path to a URL.
156 *
157 * Converts an absolute file path within the plugins or must-use plugins
158 * directory to the corresponding URL.
159 *
160 * @access private
161 * @since 7.0.0
162 *
163 * @param string $path Absolute file path to the logo. Must be within
164 * WP_PLUGIN_DIR or WPMU_PLUGIN_DIR; triggers
165 * _doing_it_wrong() otherwise.
166 * @return string|null The logo URL, or null if the path is empty or
167 * outside the supported directories.
168 */
169 function _wp_connectors_resolve_ai_provider_logo_url( string $path ): ?string {
170 if ( ! $path ) {
171 return null;
172 }
173
174 $path = wp_normalize_path( $path );
175
176 if ( ! file_exists( $path ) ) {
177 return null;
178 }
179
180 $mu_plugin_dir = wp_normalize_path( WPMU_PLUGIN_DIR );
181 if ( str_starts_with( $path, $mu_plugin_dir . '/' ) ) {
182 return plugins_url( substr( $path, strlen( $mu_plugin_dir ) ), WPMU_PLUGIN_DIR . '/.' );
183 }
184
185 $plugin_dir = wp_normalize_path( WP_PLUGIN_DIR );
186 if ( str_starts_with( $path, $plugin_dir . '/' ) ) {
187 return plugins_url( substr( $path, strlen( $plugin_dir ) ) );
188 }
189
190 _doing_it_wrong(
191 __FUNCTION__,
192 __( 'Provider logo path must be located within the plugins or must-use plugins directory.' ),
193 '7.0.0'
194 );
195
196 return null;
197 }
198 }
199