Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions lib/experimental/connectors/default-connectors.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,52 @@ function _gutenberg_get_real_api_key( string $option_name, callable $mask_callba
return (string) $value;
}

/**
* 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 ) ) );
Comment thread
gziolo marked this conversation as resolved.
Resolved
}

_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.
*
Expand All @@ -137,6 +183,7 @@ function _gutenberg_get_real_api_key( string $option_name, callable $mask_callba
* @type array $plugin Optional. Plugin data for install/activate UI.
* @type string $slug The WordPress.org plugin slug.
* }
* @type string $logo_url Optional. URL to the connector's logo image.
* @type array $authentication {
* Authentication configuration. When method is 'api_key', includes
* credentials_url and setting_name. When 'none', only method is present.
Expand Down Expand Up @@ -209,6 +256,9 @@ function _gutenberg_get_connector_settings(): array {

$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.
Expand All @@ -218,6 +268,9 @@ function _gutenberg_get_connector_settings(): array {
if ( $description ) {
$connectors[ $connector_id ]['description'] = $description;
}
if ( $logo_url ) {
$connectors[ $connector_id ]['logo_url'] = $logo_url;
Comment thread
gziolo marked this conversation as resolved.
Resolved
}
// Always update auth method; keep existing credentials_url as fallback.
$connectors[ $connector_id ]['authentication']['method'] = $authentication['method'];
if ( ! empty( $authentication['credentials_url'] ) ) {
Expand All @@ -227,6 +280,7 @@ function _gutenberg_get_connector_settings(): array {
$connectors[ $connector_id ] = array(
'name' => $name ? $name : ucwords( $connector_id ),
'description' => $description ? $description : '',
'logo_url' => $logo_url,
'type' => 'ai_provider',
'authentication' => $authentication,
);
Expand Down Expand Up @@ -400,6 +454,7 @@ function _gutenberg_get_connector_script_module_data( array $data ): array {
$connector_out = array(
'name' => $connector_data['name'],
'description' => $connector_data['description'],
'logoUrl' => ! empty( $connector_data['logo_url'] ) ? $connector_data['logo_url'] : null,
'type' => $connector_data['type'],
'authentication' => $auth_out,
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php
/**
* Unit tests for _gutenberg_resolve_ai_provider_logo_url().
*
* @package Gutenberg
*/
class Tests_Resolve_AI_Provider_Logo_URL extends WP_UnitTestCase {

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, '<svg></svg>' );

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, '<svg></svg>' );

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, '<svg></svg>' );

try {
$result = _gutenberg_resolve_ai_provider_logo_url( $tmp_file );
$this->assertNull( $result );
} finally {
unlink( $tmp_file );
}
}
}
35 changes: 30 additions & 5 deletions routes/connectors-home/default-connectors.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ import { Badge } from '@wordpress/ui';
* Internal dependencies
*/
import { useConnectorPlugin } from './use-connector-plugin';
import { OpenAILogo, ClaudeLogo, GeminiLogo } from './logos';
import {
OpenAILogo,
ClaudeLogo,
GeminiLogo,
DefaultConnectorLogo,
} from './logos';

type ConnectorAuthentication =
| {
Expand All @@ -31,6 +36,7 @@ type ConnectorAuthentication =
interface ConnectorData {
name: string;
description: string;
logoUrl?: string;
type: 'ai_provider';
plugin?: {
slug: string;
Expand Down Expand Up @@ -62,6 +68,21 @@ const CONNECTOR_LOGOS: Record< string, React.ComponentType > = {
anthropic: ClaudeLogo,
};

function getConnectorLogo(
connectorId: string,
name: string,
logoUrl?: string
): React.ReactNode {
if ( logoUrl ) {
return <img src={ logoUrl } alt={ name } width={ 40 } height={ 40 } />;
}
const Logo = CONNECTOR_LOGOS[ connectorId ];
if ( Logo ) {
return <Logo />;
}
return <DefaultConnectorLogo />;
}

const ConnectedBadge = () => (
<span
style={ {
Expand All @@ -84,7 +105,7 @@ interface ApiKeyConnectorConfig {
pluginSlug?: string;
settingName: string;
helpUrl?: string;
Logo?: React.ComponentType;
icon?: React.ReactNode;
isInstalled?: boolean;
isActivated?: boolean;
keySource?: ApiKeySource;
Expand All @@ -97,7 +118,7 @@ function ApiKeyConnector( {
pluginSlug,
settingName,
helpUrl,
Logo,
icon,
isInstalled,
isActivated,
keySource: initialKeySource,
Expand Down Expand Up @@ -146,7 +167,7 @@ function ApiKeyConnector( {
className={
pluginSlug ? `connector-item--${ pluginSlug }` : undefined
}
icon={ Logo ? <Logo /> : undefined }
icon={ icon }
name={ label }
description={ description }
actionArea={
Expand Down Expand Up @@ -229,7 +250,11 @@ export function registerDefaultConnectors() {
pluginSlug={ data.plugin?.slug }
settingName={ authentication.settingName }
helpUrl={ authentication.credentialsUrl ?? undefined }
Logo={ CONNECTOR_LOGOS[ connectorId ] }
icon={ getConnectorLogo(
connectorId,
data.name,
data.logoUrl
) }
isInstalled={ data.plugin?.isInstalled }
isActivated={ data.plugin?.isActivated }
keySource={ authentication.keySource }
Expand Down
20 changes: 20 additions & 0 deletions routes/connectors-home/logos.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,26 @@ export const ClaudeLogo = () => (
</svg>
);

// Default connector logo.
export const DefaultConnectorLogo = () => (
<svg
width="40"
height="40"
viewBox="0 0 32 32"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M0 4C0 1.79086 1.79086 0 4 0H28C30.2091 0 32 1.79086 32 4V28C32 30.2091 30.2091 32 28 32H4C1.79086 32 0 30.2091 0 28V4Z"
fill="#F0F0F0"
/>
<path
d="M14.5 8V12H17.5V8H19V12H20.5C20.7652 12 21.0196 12.1054 21.2071 12.2929C21.3946 12.4804 21.5 12.7348 21.5 13V17L18.5 21V23C18.5 23.2652 18.3946 23.5196 18.2071 23.7071C18.0196 23.8946 17.7652 24 17.5 24H14.5C14.2348 24 13.9804 23.8946 13.7929 23.7071C13.6054 23.5196 13.5 23.2652 13.5 23V21L10.5 17V13C10.5 12.7348 10.6054 12.4804 10.7929 12.2929C10.9804 12.1054 11.2348 12 11.5 12H13V8H14.5ZM15 20.5V22.5H17V20.5L20 16.5V13.5H12V16.5L15 20.5Z"
fill="#949494"
/>
</svg>
);

// Gemini logo as inline SVG
export const GeminiLogo = () => (
<svg
Expand Down
Loading