diff --git a/lib/compat/wordpress-7.0/class-wp-connector-registry.php b/lib/compat/wordpress-7.0/class-wp-connector-registry.php index 691e480b4fa7a3..e6fbd2d5842c5f 100644 --- a/lib/compat/wordpress-7.0/class-wp-connector-registry.php +++ b/lib/compat/wordpress-7.0/class-wp-connector-registry.php @@ -28,7 +28,8 @@ * env_var_name?: non-empty-string * }, * plugin?: array{ - * file: non-empty-string + * file: non-empty-string, + * is_active?: callable(): bool * } * } */ @@ -99,6 +100,8 @@ final class WP_Connector_Registry { * * @type string $file The plugin's main file path relative to the plugins * directory (e.g. 'akismet/akismet.php' or 'hello.php'). + * @type callable $is_active Optional callback to determine whether the plugin + * is active. Receives no arguments and must return bool. * } * } * @return array|null The registered connector data on success, null on failure. @@ -233,6 +236,20 @@ public function register( string $id, array $args ): ?array { if ( ! empty( $args['plugin'] ) && is_array( $args['plugin'] ) && ! empty( $args['plugin']['file'] ) ) { $connector['plugin'] = array( 'file' => $args['plugin']['file'] ); + + if ( isset( $args['plugin']['is_active'] ) ) { + if ( ! is_callable( $args['plugin']['is_active'] ) ) { + _doing_it_wrong( + __METHOD__, + /* translators: %s: Connector ID. */ + sprintf( __( 'Connector "%s" plugin is_active must be callable.' ), esc_html( $id ) ), + '7.0.0' + ); + return null; + } + + $connector['plugin']['is_active'] = $args['plugin']['is_active']; + } } $this->registered_connectors[ $id ] = $connector; diff --git a/lib/experimental/connectors/default-connectors.php b/lib/experimental/connectors/default-connectors.php index 9842c3a8e4ae7b..2d08f162738004 100644 --- a/lib/experimental/connectors/default-connectors.php +++ b/lib/experimental/connectors/default-connectors.php @@ -525,8 +525,22 @@ function _gutenberg_get_connector_script_module_data( array $data ): array { if ( ! empty( $connector_data['plugin']['file'] ) ) { $file = $connector_data['plugin']['file']; - $is_installed = file_exists( WP_PLUGIN_DIR . '/' . $file ); - $is_activated = $is_installed && is_plugin_active( $file ); + $is_installed = false; + $is_activated = false; + + if ( ! empty( $connector_data['plugin']['is_active'] ) && is_callable( $connector_data['plugin']['is_active'] ) ) { + $is_activated = (bool) call_user_func( $connector_data['plugin']['is_active'] ); + } + + if ( ! $is_activated ) { + $is_activated = is_plugin_active( $file ); + } + + if ( $is_activated ) { + $is_installed = true; + } else { + $is_installed = file_exists( WP_PLUGIN_DIR . '/' . $file ); + } $connector_out['plugin'] = array( 'file' => $file,