diff --git a/src/wp-includes/class-wp-connector-registry.php b/src/wp-includes/class-wp-connector-registry.php index 9fe51be96aa8e..62baac907b9fd 100644 --- a/src/wp-includes/class-wp-connector-registry.php +++ b/src/wp-includes/class-wp-connector-registry.php @@ -40,7 +40,8 @@ * env_var_name?: non-empty-string * }, * plugin?: array{ - * file: non-empty-string + * file: non-empty-string, + * is_active?: callable(): bool * } * } */ @@ -111,6 +112,8 @@ final class WP_Connector_Registry { * * @type string $file The plugin's main file path relative to the plugins * directory (e.g. 'my-plugin/my-plugin.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. @@ -245,6 +248,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/src/wp-includes/connectors.php b/src/wp-includes/connectors.php index 63e018074fd58..6cfb8e615bc43 100644 --- a/src/wp-includes/connectors.php +++ b/src/wp-includes/connectors.php @@ -674,8 +674,22 @@ function _wp_connectors_get_connector_script_module_data( array $data ): array { if ( ! empty( $connector_data['plugin']['file'] ) ) { $file = $connector_data['plugin']['file']; - $is_installed = file_exists( wp_normalize_path( 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,