Repository navigation
Connectors: Add is_active callback support to plugin registration #77897
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 8 commits
f36ba9e
0b06523
d261e8d
50612ef
8f54b96
44c6825
3e64029
8588683
3d990ad
c9bb9ef
e62f202
8610c82
1d66f6f
9f687dc
8414c35
881e0c5
adc2600
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| https://github.com/WordPress/wordpress-develop/pull/11701 | ||
|
|
||
| * https://github.com/WordPress/gutenberg/pull/77897 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -98,10 +98,12 @@ final class WP_Connector_Registry { | |
| * @type array $plugin { | ||
| * Optional. Plugin data for install/activate UI. | ||
| * | ||
| * @type string $file The plugin's main file path relative to the plugins | ||
| * directory (e.g. 'akismet/akismet.php' or 'hello.php'). | ||
| * @type string $file Optional. 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. | ||
| * Defaults to `__return_true`. | ||
|
t-hamano marked this conversation as resolved.
Resolved
|
||
| * } | ||
| * } | ||
| * @return array|null The registered connector data on success, null on failure. | ||
|
|
@@ -234,8 +236,12 @@ 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'] ); | ||
| $connector['plugin'] = array(); | ||
|
|
||
| if ( ! empty( $args['plugin'] ) && is_array( $args['plugin'] ) ) { | ||
| if ( ! empty( $args['plugin']['file'] ) ) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's possible here that |
||
| $connector['plugin']['file'] = $args['plugin']['file']; | ||
| } | ||
|
|
||
| if ( isset( $args['plugin']['is_active'] ) ) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we aligned with core and set $connector['plugin']['is_active'] == '__return_true', if a is_active was not passed?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see that in the phpdoc for and in the method: if ( ! isset( $connector['plugin']['is_active'] ) ) {
$connector['plugin']['is_active'] = '__return_true';
}So it does seem to make sense to update this to match what is in core. This aligns with what was done in WordPress/wordpress-develop@b3b40be. This would undo some things in the companion core PR: WordPress/wordpress-develop#11701 |
||
| if ( ! is_callable( $args['plugin']['is_active'] ) ) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -31,7 +31,10 @@ function _gutenberg_connectors_init(): void { | |||||||||||||||||||
| 'description' => __( 'Protect your site from spam.', 'gutenberg' ), | ||||||||||||||||||||
| 'type' => 'spam_filtering', | ||||||||||||||||||||
| 'plugin' => array( | ||||||||||||||||||||
| 'file' => 'akismet/akismet.php', | ||||||||||||||||||||
| 'file' => 'akismet/akismet.php', | ||||||||||||||||||||
| 'is_active' => static function (): bool { | ||||||||||||||||||||
| return defined( 'AKISMET_VERSION' ); | ||||||||||||||||||||
| }, | ||||||||||||||||||||
|
t-hamano marked this conversation as resolved.
Resolved
|
||||||||||||||||||||
| ), | ||||||||||||||||||||
| 'authentication' => array( | ||||||||||||||||||||
| 'method' => 'api_key', | ||||||||||||||||||||
|
|
@@ -201,6 +204,17 @@ function _gutenberg_register_default_ai_providers( WP_Connector_Registry $regist | |||||||||||||||||||
| } | ||||||||||||||||||||
| } | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| if ( ! isset( $args['plugin']['is_active'] ) ) { | ||||||||||||||||||||
| $args['plugin']['is_active'] = static function () use ( $ai_registry, $id ): bool { | ||||||||||||||||||||
| try { | ||||||||||||||||||||
| return $ai_registry->hasProvider( $id ); | ||||||||||||||||||||
| } catch ( Exception $e ) { | ||||||||||||||||||||
| return false; | ||||||||||||||||||||
| } | ||||||||||||||||||||
| }; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| $registry->register( $id, $args ); | ||||||||||||||||||||
| } | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
@@ -485,10 +499,6 @@ function _gutenberg_get_connector_script_module_data( array $data ): array { | |||||||||||||||||||
|
|
||||||||||||||||||||
| $registry = \WordPress\AiClient\AiClient::defaultRegistry(); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| if ( ! function_exists( 'is_plugin_active' ) ) { | ||||||||||||||||||||
| require_once ABSPATH . 'wp-admin/includes/plugin.php'; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| $connectors = array(); | ||||||||||||||||||||
| foreach ( wp_get_connectors() as $connector_id => $connector_data ) { | ||||||||||||||||||||
| $auth = $connector_data['authentication']; | ||||||||||||||||||||
|
|
@@ -524,23 +534,14 @@ function _gutenberg_get_connector_script_module_data( array $data ): array { | |||||||||||||||||||
| ); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| if ( ! empty( $connector_data['plugin']['file'] ) ) { | ||||||||||||||||||||
| $file = $connector_data['plugin']['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; | ||||||||||||||||||||
| $file = $connector_data['plugin']['file']; | ||||||||||||||||||||
| if ( ! isset( $connector_data['plugin']['is_active'] ) ) { | ||||||||||||||||||||
| // Assume plugin has registered own connector and is therefore active. | ||||||||||||||||||||
| $is_activated = true; | ||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess we could set a default of is_active '__return_true' and here just always call is_acttive.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This makes sense to me. I this would only apply for the non-default providers. For them, they are getting gutenberg/lib/experimental/connectors/default-connectors.php Lines 208 to 216 in 1d66f6f
|
||||||||||||||||||||
| } else { | ||||||||||||||||||||
| $is_installed = file_exists( WP_PLUGIN_DIR . '/' . $file ); | ||||||||||||||||||||
| $is_activated = (bool) call_user_func( $connector_data['plugin']['is_active'] ); | ||||||||||||||||||||
| } | ||||||||||||||||||||
| $is_installed = $is_activated || file_exists( wp_normalize_path( WP_PLUGIN_DIR . '/' . $file ) ); | ||||||||||||||||||||
|
westonruter marked this conversation as resolved.
Resolved
|
||||||||||||||||||||
|
|
||||||||||||||||||||
| $connector_out['plugin'] = array( | ||||||||||||||||||||
| 'file' => $file, | ||||||||||||||||||||
|
|
||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.