diff --git a/backport-changelog/7.0/11175.md b/backport-changelog/7.0/11175.md new file mode 100644 index 00000000000000..6f3951eb3c4265 --- /dev/null +++ b/backport-changelog/7.0/11175.md @@ -0,0 +1,3 @@ +https://github.com/WordPress/wordpress-develop/pull/11175 + +* https://github.com/WordPress/gutenberg/pull/76364 diff --git a/lib/compat/wordpress-7.0/class-wp-connector-registry.php b/lib/compat/wordpress-7.0/class-wp-connector-registry.php new file mode 100644 index 00000000000000..3bbef9b1a9bf1c --- /dev/null +++ b/lib/compat/wordpress-7.0/class-wp-connector-registry.php @@ -0,0 +1,290 @@ + + * @phpstan-var array + */ + private array $registered_connectors = array(); + + /** + * Registers a new connector. + * + * @since 7.0.0 + * + * @param string $id The unique connector identifier. Must contain only lowercase + * alphanumeric characters and underscores. + * @param array $args { + * An associative array of arguments for the connector. + * + * @type string $name Required. The connector's display name. + * @type string $description Optional. The connector's description. Default empty string. + * @type string|null $logo_url Optional. URL to the connector's logo image. Default null. + * @type string $type Required. The connector type. Currently, only 'ai_provider' is supported. + * @type array $authentication { + * Required. Authentication configuration. + * + * @type string $method Required. The authentication method: 'api_key' or 'none'. + * @type string|null $credentials_url Optional. URL where users can obtain API credentials. + * } + * @type array $plugin { + * Optional. Plugin data for install/activate UI. + * + * @type string $slug The WordPress.org plugin slug. + * } + * } + * @return array|null The registered connector data on success, null on failure. + * + * @phpstan-param Connector $args + * @phpstan-return Connector|null + */ + public function register( string $id, array $args ): ?array { + if ( ! preg_match( '/^[a-z0-9_]+$/', $id ) ) { + _doing_it_wrong( + __METHOD__, + __( + 'Connector ID must contain only lowercase alphanumeric characters and underscores.' + ), + '7.0.0' + ); + return null; + } + + if ( $this->is_registered( $id ) ) { + _doing_it_wrong( + __METHOD__, + /* translators: %s: Connector ID. */ + sprintf( __( 'Connector "%s" is already registered.' ), esc_html( $id ) ), + '7.0.0' + ); + return null; + } + + // Validate required fields. + if ( empty( $args['name'] ) || ! is_string( $args['name'] ) ) { + _doing_it_wrong( + __METHOD__, + /* translators: %s: Connector ID. */ + sprintf( __( 'Connector "%s" requires a non-empty "name" string.' ), esc_html( $id ) ), + '7.0.0' + ); + return null; + } + + if ( empty( $args['type'] ) || ! is_string( $args['type'] ) ) { + _doing_it_wrong( + __METHOD__, + /* translators: %s: Connector ID. */ + sprintf( __( 'Connector "%s" requires a non-empty "type" string.' ), esc_html( $id ) ), + '7.0.0' + ); + return null; + } + + if ( ! isset( $args['authentication'] ) || ! is_array( $args['authentication'] ) ) { + _doing_it_wrong( + __METHOD__, + /* translators: %s: Connector ID. */ + sprintf( __( 'Connector "%s" requires an "authentication" array.' ), esc_html( $id ) ), + '7.0.0' + ); + return null; + } + + if ( empty( $args['authentication']['method'] ) || ! in_array( $args['authentication']['method'], array( 'api_key', 'none' ), true ) ) { + _doing_it_wrong( + __METHOD__, + /* translators: %s: Connector ID. */ + sprintf( __( 'Connector "%s" authentication method must be "api_key" or "none".' ), esc_html( $id ) ), + '7.0.0' + ); + return null; + } + + $connector = array( + 'name' => $args['name'], + 'description' => isset( $args['description'] ) && is_string( $args['description'] ) ? $args['description'] : '', + 'type' => $args['type'], + 'authentication' => array( + 'method' => $args['authentication']['method'], + ), + ); + + if ( ! empty( $args['logo_url'] ) && is_string( $args['logo_url'] ) ) { + $connector['logo_url'] = $args['logo_url']; + } + + if ( 'api_key' === $args['authentication']['method'] ) { + $connector['authentication']['credentials_url'] = $args['authentication']['credentials_url'] ?? null; + $connector['authentication']['setting_name'] = "connectors_ai_{$id}_api_key"; + } + + if ( ! empty( $args['plugin'] ) && is_array( $args['plugin'] ) ) { + $connector['plugin'] = $args['plugin']; + } + + $this->registered_connectors[ $id ] = $connector; + return $connector; + } + + /** + * Unregisters a connector. + * + * @since 7.0.0 + * + * @param string $id The connector identifier. + * @return array|null The unregistered connector data on success, null on failure. + * + * @phpstan-return Connector|null + */ + public function unregister( string $id ): ?array { + if ( ! $this->is_registered( $id ) ) { + _doing_it_wrong( + __METHOD__, + /* translators: %s: Connector ID. */ + sprintf( __( 'Connector "%s" not found.' ), esc_html( $id ) ), + '7.0.0' + ); + return null; + } + + $unregistered = $this->registered_connectors[ $id ]; + unset( $this->registered_connectors[ $id ] ); + + return $unregistered; + } + + /** + * Retrieves the list of all registered connectors. + * + * Do not use this method directly. Instead, use the `wp_get_connectors()` function. + * + * @since 7.0.0 + * + * @see wp_get_connectors() + * + * @return array The array of registered connectors keyed by connector ID. + * @phpstan-return array + */ + public function get_all_registered(): array { + return $this->registered_connectors; + } + + /** + * Checks if a connector is registered. + * + * Do not use this method directly. Instead, use the `wp_is_connector_registered()` function. + * + * @since 7.0.0 + * + * @see wp_is_connector_registered() + * + * @param string $id The connector identifier. + * @return bool True if the connector is registered, false otherwise. + */ + public function is_registered( string $id ): bool { + return isset( $this->registered_connectors[ $id ] ); + } + + /** + * Retrieves a registered connector. + * + * Do not use this method directly. Instead, use the `wp_get_connector()` function. + * + * @since 7.0.0 + * + * @see wp_get_connector() + * + * @param string $id The connector identifier. + * @return array|null The registered connector data, or null if it is not registered. + * @phpstan-return Connector|null + */ + public function get_registered( string $id ): ?array { + if ( ! $this->is_registered( $id ) ) { + _doing_it_wrong( + __METHOD__, + /* translators: %s: Connector ID. */ + sprintf( __( 'Connector "%s" not found.' ), esc_html( $id ) ), + '7.0.0' + ); + return null; + } + return $this->registered_connectors[ $id ]; + } + + /** + * Retrieves the main instance of the registry class. + * + * @since 7.0.0 + * + * @return WP_Connector_Registry|null The main registry instance, or null if not yet initialized. + */ + public static function get_instance(): ?self { + return self::$instance; + } + + /** + * Sets the main instance of the registry class. + * + * @since 7.0.0 + * @access private + * + * @param WP_Connector_Registry $registry The registry instance. + */ + public static function set_instance( WP_Connector_Registry $registry ): void { + if ( ! doing_action( 'init' ) ) { + _doing_it_wrong( + __METHOD__, + __( 'The connector registry instance must be set during the init action.' ), + '7.0.0' + ); + return; + } + + self::$instance = $registry; + } + } +} diff --git a/lib/compat/wordpress-7.0/connectors.php b/lib/compat/wordpress-7.0/connectors.php new file mode 100644 index 00000000000000..7f1a52681180c3 --- /dev/null +++ b/lib/compat/wordpress-7.0/connectors.php @@ -0,0 +1,116 @@ +is_registered( $id ); + } +} + +if ( ! function_exists( 'wp_get_connector' ) ) { + /** + * Retrieves a registered connector. + * + * @since 7.0.0 + * + * @see WP_Connector_Registry::get_registered() + * + * @param string $id The connector identifier. + * @return array|null The registered connector data, or null if not registered. + */ + function wp_get_connector( string $id ): ?array { + $registry = WP_Connector_Registry::get_instance(); + if ( null === $registry ) { + return null; + } + + return $registry->get_registered( $id ); + } +} + +if ( ! function_exists( 'wp_get_connectors' ) ) { + /** + * Retrieves all registered connectors. + * + * @since 7.0.0 + * + * @see WP_Connector_Registry::get_all_registered() + * + * @return array[] An array of registered connectors keyed by connector ID. + */ + function wp_get_connectors(): array { + $registry = WP_Connector_Registry::get_instance(); + if ( null === $registry ) { + return array(); + } + + return $registry->get_all_registered(); + } +} + +if ( ! function_exists( '_wp_connectors_resolve_ai_provider_logo_url' ) ) { + /** + * Resolves an AI provider logo file path to a URL. + * + * Converts an absolute file path within the plugins or must-use plugins + * directory to the corresponding URL. + * + * @access private + * @since 7.0.0 + * + * @param string $path Absolute file path to the logo. Must be within + * WP_PLUGIN_DIR or WPMU_PLUGIN_DIR; triggers + * _doing_it_wrong() otherwise. + * @return string|null The logo URL, or null if the path is empty or + * outside the supported directories. + */ + function _wp_connectors_resolve_ai_provider_logo_url( string $path ): ?string { + if ( ! $path ) { + return null; + } + + $path = wp_normalize_path( $path ); + + if ( ! file_exists( $path ) ) { + return null; + } + + $mu_plugin_dir = wp_normalize_path( WPMU_PLUGIN_DIR ); + if ( str_starts_with( $path, $mu_plugin_dir . '/' ) ) { + return plugins_url( substr( $path, strlen( $mu_plugin_dir ) ), WPMU_PLUGIN_DIR . '/.' ); + } + + $plugin_dir = wp_normalize_path( WP_PLUGIN_DIR ); + if ( str_starts_with( $path, $plugin_dir . '/' ) ) { + return plugins_url( substr( $path, strlen( $plugin_dir ) ) ); + } + + _doing_it_wrong( + __FUNCTION__, + __( 'Provider logo path must be located within the plugins or must-use plugins directory.' ), + '7.0.0' + ); + + return null; + } +} diff --git a/lib/experimental/connectors/default-connectors.php b/lib/experimental/connectors/default-connectors.php index 7855ff9f025e42..df9433f5fb0ee6 100644 --- a/lib/experimental/connectors/default-connectors.php +++ b/lib/experimental/connectors/default-connectors.php @@ -5,6 +5,154 @@ * @package gutenberg */ +/** + * Initializes the connector registry with default connectors and fires the registration action. + * + * Creates the registry instance, registers built-in connectors (which cannot be unhooked), + * and then fires the `wp_connectors_init` action for plugins to register their own connectors. + * + * @access private + * @since 7.0.0 + */ +function _gutenberg_connectors_init(): void { + if ( ! class_exists( '\WordPress\AiClient\AiClient' ) ) { + return; + } + + $registry = new WP_Connector_Registry(); + WP_Connector_Registry::set_instance( $registry ); + + // Built-in connectors. + $defaults = array( + 'anthropic' => array( + 'name' => 'Anthropic', + 'description' => __( 'Text generation with Claude.', 'gutenberg' ), + 'type' => 'ai_provider', + 'plugin' => array( + 'slug' => 'ai-provider-for-anthropic', + ), + 'authentication' => array( + 'method' => 'api_key', + 'credentials_url' => 'https://platform.claude.com/settings/keys', + ), + ), + 'google' => array( + 'name' => 'Google', + 'description' => __( 'Text and image generation with Gemini and Imagen.', 'gutenberg' ), + 'type' => 'ai_provider', + 'plugin' => array( + 'slug' => 'ai-provider-for-google', + ), + 'authentication' => array( + 'method' => 'api_key', + 'credentials_url' => 'https://aistudio.google.com/api-keys', + ), + ), + 'openai' => array( + 'name' => 'OpenAI', + 'description' => __( 'Text and image generation with GPT and Dall-E.', 'gutenberg' ), + 'type' => 'ai_provider', + 'plugin' => array( + 'slug' => 'ai-provider-for-openai', + ), + 'authentication' => array( + 'method' => 'api_key', + 'credentials_url' => 'https://platform.openai.com/api-keys', + ), + ), + ); + + // Merge AI Client registry data on top of defaults. + // Registry values (from provider plugins) take precedence over hardcoded fallbacks. + $ai_registry = \WordPress\AiClient\AiClient::defaultRegistry(); + + foreach ( $ai_registry->getRegisteredProviderIds() as $connector_id ) { + $provider_class_name = $ai_registry->getProviderClassName( $connector_id ); + $provider_metadata = $provider_class_name::metadata(); + + $auth_method = $provider_metadata->getAuthenticationMethod(); + $is_api_key = null !== $auth_method && $auth_method->isApiKey(); + + if ( $is_api_key ) { + $credentials_url = $provider_metadata->getCredentialsUrl(); + $authentication = array( + 'method' => 'api_key', + 'credentials_url' => $credentials_url ? $credentials_url : null, + ); + } else { + $authentication = array( 'method' => 'none' ); + } + + $name = $provider_metadata->getName(); + $description = $provider_metadata->getDescription(); + $logo_url = method_exists( $provider_metadata, 'getLogoPath' ) && $provider_metadata->getLogoPath() + ? _wp_connectors_resolve_ai_provider_logo_url( $provider_metadata->getLogoPath() ) + : null; + + if ( isset( $defaults[ $connector_id ] ) ) { + // Override fields with non-empty registry values. + if ( $name ) { + $defaults[ $connector_id ]['name'] = $name; + } + if ( $description ) { + $defaults[ $connector_id ]['description'] = $description; + } + if ( $logo_url ) { + $defaults[ $connector_id ]['logo_url'] = $logo_url; + } + // Always update auth method; keep existing credentials_url as fallback. + $defaults[ $connector_id ]['authentication']['method'] = $authentication['method']; + if ( ! empty( $authentication['credentials_url'] ) ) { + $defaults[ $connector_id ]['authentication']['credentials_url'] = $authentication['credentials_url']; + } + } else { + $defaults[ $connector_id ] = array( + 'name' => $name ? $name : ucwords( $connector_id ), + 'description' => $description ? $description : '', + 'type' => 'ai_provider', + 'authentication' => $authentication, + 'logo_url' => $logo_url, + ); + } + } + + // Register all default connectors directly on the registry. + foreach ( $defaults as $id => $args ) { + $registry->register( $id, $args ); + } + + /** + * Fires when the connector registry is ready for plugins to register connectors. + * + * Default connectors have already been registered at this point and cannot be + * unhooked. Use `$registry->register()` within this action to add new connectors. + * + * Example usage: + * + * add_action( 'wp_connectors_init', function ( WP_Connector_Registry $registry ) { + * $registry->register( + * 'my_custom_ai', + * array( + * 'name' => __( 'My Custom AI', 'my-plugin' ), + * 'description' => __( 'Custom AI provider integration.', 'my-plugin' ), + * 'type' => 'ai_provider', + * 'authentication' => array( + * 'method' => 'api_key', + * 'credentials_url' => 'https://example.com/api-keys', + * ), + * ) + * ); + * } ); + * + * @since 7.0.0 + * + * @param WP_Connector_Registry $registry Connector registry instance. + */ + do_action( 'wp_connectors_init', $registry ); +} +remove_action( 'init', '_wp_connectors_init' ); +add_action( 'init', '_gutenberg_connectors_init' ); + /** * Determines the source of an API key for a given provider. * @@ -102,55 +250,11 @@ function _gutenberg_is_ai_api_key_valid( string $key, string $provider_id ): ?bo } } -/** - * Resolves an AI provider logo file path to a URL. - * - * The AI Client library returns absolute file paths (not URLs) for logo files - * since it is not WordPress-specific. This function converts a path within - * the plugins or must-use plugins directory to the corresponding URL. - * - * @access private - * @since 7.0.0 - * - * @param string $path Absolute file path to the logo. Must be within - * WP_PLUGIN_DIR or WPMU_PLUGIN_DIR; triggers - * _doing_it_wrong() otherwise. - * @return string|null The logo URL, or null if the path is empty or - * outside the supported directories. - */ -function _gutenberg_resolve_ai_provider_logo_url( string $path ): ?string { - if ( ! $path ) { - return null; - } - - $path = wp_normalize_path( $path ); - - if ( ! file_exists( $path ) ) { - return null; - } - - $mu_plugin_dir = wp_normalize_path( WPMU_PLUGIN_DIR ); - if ( str_starts_with( $path, $mu_plugin_dir . '/' ) ) { - return plugins_url( substr( $path, strlen( $mu_plugin_dir ) ), WPMU_PLUGIN_DIR . '/.' ); - } - - $plugin_dir = wp_normalize_path( WP_PLUGIN_DIR ); - if ( str_starts_with( $path, $plugin_dir . '/' ) ) { - return plugins_url( substr( $path, strlen( $plugin_dir ) ) ); - } - - _doing_it_wrong( - __FUNCTION__, - __( 'Provider logo path must be located within the plugins or must-use plugins directory.', 'gutenberg' ), - '7.0.0' - ); - - return null; -} - /** * Gets the registered connector settings. * + * Enriches registry data with logo URLs and plugin installation status. + * * @access private * * @return array { @@ -183,103 +287,8 @@ function _gutenberg_get_connector_settings(): array { return $cached; } - $connectors = array( - 'google' => array( - 'name' => 'Google', - 'description' => __( 'Text and image generation with Gemini and Imagen.', 'gutenberg' ), - 'type' => 'ai_provider', - 'plugin' => array( - 'slug' => 'ai-provider-for-google', - ), - 'authentication' => array( - 'method' => 'api_key', - 'credentials_url' => 'https://aistudio.google.com/api-keys', - ), - ), - 'openai' => array( - 'name' => 'OpenAI', - 'description' => __( 'Text and image generation with GPT and Dall-E.', 'gutenberg' ), - 'type' => 'ai_provider', - 'plugin' => array( - 'slug' => 'ai-provider-for-openai', - ), - 'authentication' => array( - 'method' => 'api_key', - 'credentials_url' => 'https://platform.openai.com/api-keys', - ), - ), - 'anthropic' => array( - 'name' => 'Anthropic', - 'description' => __( 'Text generation with Claude.', 'gutenberg' ), - 'type' => 'ai_provider', - 'plugin' => array( - 'slug' => 'ai-provider-for-anthropic', - ), - 'authentication' => array( - 'method' => 'api_key', - 'credentials_url' => 'https://platform.claude.com/settings/keys', - ), - ), - ); - - $registry = \WordPress\AiClient\AiClient::defaultRegistry(); - - foreach ( $registry->getRegisteredProviderIds() as $connector_id ) { - $provider_class = $registry->getProviderClassName( $connector_id ); - $metadata = $provider_class::metadata(); - - $auth_method = $metadata->getAuthenticationMethod(); - $is_api_key = null !== $auth_method && $auth_method->isApiKey(); - - if ( $is_api_key ) { - $credentials_url = $metadata->getCredentialsUrl(); - $authentication = array( - 'method' => 'api_key', - 'credentials_url' => $credentials_url ? $credentials_url : null, - ); - } else { - $authentication = array( 'method' => 'none' ); - } - - $name = $metadata->getName(); - $description = method_exists( $metadata, 'getDescription' ) ? $metadata->getDescription() : null; - $logo_url = method_exists( $metadata, 'getLogoPath' ) && $metadata->getLogoPath() - ? _gutenberg_resolve_ai_provider_logo_url( $metadata->getLogoPath() ) - : null; - - if ( isset( $connectors[ $connector_id ] ) ) { - // Override fields with non-empty registry values. - if ( $name ) { - $connectors[ $connector_id ]['name'] = $name; - } - if ( $description ) { - $connectors[ $connector_id ]['description'] = $description; - } - if ( $logo_url ) { - $connectors[ $connector_id ]['logo_url'] = $logo_url; - } - // Always update auth method; keep existing credentials_url as fallback. - $connectors[ $connector_id ]['authentication']['method'] = $authentication['method']; - if ( ! empty( $authentication['credentials_url'] ) ) { - $connectors[ $connector_id ]['authentication']['credentials_url'] = $authentication['credentials_url']; - } - } else { - $connectors[ $connector_id ] = array( - 'name' => $name ? $name : ucwords( $connector_id ), - 'description' => $description ? $description : '', - 'logo_url' => $logo_url, - 'type' => 'ai_provider', - 'authentication' => $authentication, - ); - } - } - - // Add setting_name for connectors that use API key authentication. - foreach ( $connectors as $connector_id => $connector ) { - if ( 'api_key' === $connector['authentication']['method'] ) { - $connectors[ $connector_id ]['authentication']['setting_name'] = "connectors_ai_{$connector_id}_api_key"; - } - } + $connectors = wp_get_connectors(); + ksort( $connectors ); // Add plugin installation and activation status. // Build a slug-to-file map following the same pattern as WP_Plugin_Dependencies::get_plugin_dirnames(). @@ -388,9 +397,16 @@ function _gutenberg_register_default_connector_settings(): void { return; } - foreach ( _gutenberg_get_connector_settings() as $connector_data ) { + $ai_registry = \WordPress\AiClient\AiClient::defaultRegistry(); + + foreach ( _gutenberg_get_connector_settings() as $connector_id => $connector_data ) { $auth = $connector_data['authentication']; - if ( 'api_key' !== $auth['method'] || empty( $auth['setting_name'] ) ) { + if ( 'ai_provider' !== $connector_data['type'] || 'api_key' !== $auth['method'] || empty( $auth['setting_name'] ) ) { + continue; + } + + // Skip registering the setting if the provider is not in the registry. + if ( ! $ai_registry->hasProvider( $connector_id ) ) { continue; } @@ -430,7 +446,7 @@ function _gutenberg_pass_default_connector_keys_to_ai_client(): void { } try { - $registry = \WordPress\AiClient\AiClient::defaultRegistry(); + $ai_registry = \WordPress\AiClient\AiClient::defaultRegistry(); foreach ( _gutenberg_get_connector_settings() as $connector_id => $connector_data ) { if ( 'ai_provider' !== $connector_data['type'] ) { continue; @@ -441,6 +457,10 @@ function _gutenberg_pass_default_connector_keys_to_ai_client(): void { continue; } + if ( ! $ai_registry->hasProvider( $connector_id ) ) { + continue; + } + // Skip if the key is already provided via env var or constant. $key_source = _gutenberg_get_api_key_source( $connector_id ); if ( 'env' === $key_source || 'constant' === $key_source ) { @@ -448,11 +468,11 @@ function _gutenberg_pass_default_connector_keys_to_ai_client(): void { } $api_key = get_option( $auth['setting_name'], '' ); - if ( '' === $api_key || ! $registry->hasProvider( $connector_id ) ) { + if ( '' === $api_key ) { continue; } - $registry->setProviderRequestAuthentication( + $ai_registry->setProviderRequestAuthentication( $connector_id, new \WordPress\AiClient\Providers\Http\DTO\ApiKeyRequestAuthentication( $api_key ) ); diff --git a/lib/load.php b/lib/load.php index dc6d9b4a99ab06..55cecf7e0b4c02 100644 --- a/lib/load.php +++ b/lib/load.php @@ -75,6 +75,8 @@ function gutenberg_is_experiment_enabled( $name ) { require __DIR__ . '/compat/wordpress-7.0/template-activate.php'; require __DIR__ . '/compat/wordpress-7.0/rest-api.php'; require __DIR__ . '/compat/wordpress-7.0/global-styles.php'; + require __DIR__ . '/compat/wordpress-7.0/class-wp-connector-registry.php'; + require __DIR__ . '/compat/wordpress-7.0/connectors.php'; // Plugin specific code. require_once __DIR__ . '/class-wp-rest-global-styles-controller-gutenberg.php'; diff --git a/phpunit/experimental/connectors/resolve-ai-provider-logo-url-test.php b/phpunit/experimental/connectors/resolve-ai-provider-logo-url-test.php deleted file mode 100644 index 32daa6351cdab5..00000000000000 --- a/phpunit/experimental/connectors/resolve-ai-provider-logo-url-test.php +++ /dev/null @@ -1,72 +0,0 @@ -markTestSkipped( 'The AI Client library is not available.' ); - } - } - - public function test_returns_null_when_path_is_empty() { - $this->assertNull( _gutenberg_resolve_ai_provider_logo_url( '' ) ); - } - - public function test_resolves_plugin_dir_path_to_url() { - $logo_path = WP_PLUGIN_DIR . '/my-plugin/logo.svg'; - - // Create the file so file_exists() passes. - wp_mkdir_p( dirname( $logo_path ) ); - file_put_contents( $logo_path, '' ); - - try { - $result = _gutenberg_resolve_ai_provider_logo_url( $logo_path ); - $this->assertSame( site_url( '/wp-content/plugins/my-plugin/logo.svg' ), $result ); - } finally { - unlink( $logo_path ); - rmdir( dirname( $logo_path ) ); - } - } - - public function test_resolves_mu_plugin_dir_path_to_url() { - $logo_path = WPMU_PLUGIN_DIR . '/my-mu-plugin/logo.svg'; - - // Create the file so file_exists() passes. - wp_mkdir_p( dirname( $logo_path ) ); - file_put_contents( $logo_path, '' ); - - try { - $result = _gutenberg_resolve_ai_provider_logo_url( $logo_path ); - $this->assertSame( site_url( '/wp-content/mu-plugins/my-mu-plugin/logo.svg' ), $result ); - } finally { - unlink( $logo_path ); - rmdir( dirname( $logo_path ) ); - } - } - - public function test_returns_null_when_file_does_not_exist() { - $result = _gutenberg_resolve_ai_provider_logo_url( WP_PLUGIN_DIR . '/nonexistent/logo.svg' ); - $this->assertNull( $result ); - } - - /** - * @expectedIncorrectUsage _gutenberg_resolve_ai_provider_logo_url - */ - public function test_returns_null_and_triggers_doing_it_wrong_for_path_outside_plugin_dirs() { - $tmp_file = tempnam( sys_get_temp_dir(), 'logo_' ); - file_put_contents( $tmp_file, '' ); - - try { - $result = _gutenberg_resolve_ai_provider_logo_url( $tmp_file ); - $this->assertNull( $result ); - } finally { - unlink( $tmp_file ); - } - } -}