Skip to content

Commit eee44dc

Browse files
committed
Connectors: Add is_active callback support to plugin registration
Backport of WordPress/gutenberg#76994
1 parent aa72dfe commit eee44dc

2 files changed

Lines changed: 34 additions & 3 deletions

File tree

src/wp-includes/class-wp-connector-registry.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@
4040
* env_var_name?: non-empty-string
4141
* },
4242
* plugin?: array{
43-
* file: non-empty-string
43+
* file: non-empty-string,
44+
* is_active?: callable(): bool
4445
* }
4546
* }
4647
*/
@@ -111,6 +112,8 @@ final class WP_Connector_Registry {
111112
*
112113
* @type string $file The plugin's main file path relative to the plugins
113114
* directory (e.g. 'my-plugin/my-plugin.php' or 'hello.php').
115+
* @type callable $is_active Optional callback to determine whether the plugin
116+
* is active. Receives no arguments and must return bool.
114117
* }
115118
* }
116119
* @return array|null The registered connector data on success, null on failure.
@@ -245,6 +248,20 @@ public function register( string $id, array $args ): ?array {
245248

246249
if ( ! empty( $args['plugin'] ) && is_array( $args['plugin'] ) && ! empty( $args['plugin']['file'] ) ) {
247250
$connector['plugin'] = array( 'file' => $args['plugin']['file'] );
251+
252+
if ( isset( $args['plugin']['is_active'] ) ) {
253+
if ( ! is_callable( $args['plugin']['is_active'] ) ) {
254+
_doing_it_wrong(
255+
__METHOD__,
256+
/* translators: %s: Connector ID. */
257+
sprintf( __( 'Connector "%s" plugin is_active must be callable.' ), esc_html( $id ) ),
258+
'7.0.0'
259+
);
260+
return null;
261+
}
262+
263+
$connector['plugin']['is_active'] = $args['plugin']['is_active'];
264+
}
248265
}
249266

250267
$this->registered_connectors[ $id ] = $connector;

src/wp-includes/connectors.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -674,8 +674,22 @@ function _wp_connectors_get_connector_script_module_data( array $data ): array {
674674

675675
if ( ! empty( $connector_data['plugin']['file'] ) ) {
676676
$file = $connector_data['plugin']['file'];
677-
$is_installed = file_exists( wp_normalize_path( WP_PLUGIN_DIR . '/' . $file ) );
678-
$is_activated = $is_installed && is_plugin_active( $file );
677+
$is_installed = false;
678+
$is_activated = false;
679+
680+
if ( ! empty( $connector_data['plugin']['is_active'] ) && is_callable( $connector_data['plugin']['is_active'] ) ) {
681+
$is_activated = (bool) call_user_func( $connector_data['plugin']['is_active'] );
682+
}
683+
684+
if ( ! $is_activated ) {
685+
$is_activated = is_plugin_active( $file );
686+
}
687+
688+
if ( $is_activated ) {
689+
$is_installed = true;
690+
} else {
691+
$is_installed = file_exists( WP_PLUGIN_DIR . '/' . $file );
692+
}
679693

680694
$connector_out['plugin'] = array(
681695
'file' => $file,

0 commit comments

Comments
 (0)