Skip to content
Closed
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
2 changes: 1 addition & 1 deletion includes/Connector_Approval/Admin_Notice.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public function render(): void {
}

$screen = get_current_screen();
if ( $screen && 'tools_page_ai-connector-approval' === $screen->id ) {
if ( $screen && in_array( $screen->id, array( 'tools_page_ai-connector-approval', 'tools_page_ai-request-logs' ), true ) ) {
return;
}

Expand Down
53 changes: 49 additions & 4 deletions includes/Logging/AI_Request_Log_Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function enqueue_assets(): void {
'ai_request_logs',
'RequestLogsSettings',
array(
'rest' => array(
'rest' => array(
'nonce' => wp_create_nonce( 'wp_rest' ),
'root' => esc_url_raw( rest_url() ),
'routes' => array(
Expand All @@ -98,12 +98,13 @@ public function enqueue_assets(): void {
'filters' => 'ai/v1/logs/filters',
),
),
'initialState' => array(
'initialState' => array(
'summary' => $this->manager->get_summary( 'day' ),
'filters' => $this->manager->get_filter_options(),
),
'connectorsUrl' => admin_url( 'options-connectors.php' ),
'providerMetadata' => $this->get_provider_metadata(),
'connectorsUrl' => admin_url( 'options-connectors.php' ),
'providerMetadata' => $this->get_provider_metadata(),
'connectorApprovalNotice' => $this->get_connector_approval_notice_data(),
)
);
}
Expand All @@ -122,6 +123,50 @@ public function render_page(): void {
<?php
}

/**
* Returns connector approval pending-notice data for the React app.
*
* Returns null when the Connector Approval experiment is not active,
* there are no pending requests, or the notice has already been dismissed
* for the current pending set, so the React app only renders the notice
* when it is actually needed.
*
* @since 1.0.0
*
* @return array{count: int, reviewUrl: string, dismissUrl: string}|null
*/
private function get_connector_approval_notice_data(): ?array {
if ( ! class_exists( \WordPress\AI\Connector_Approval\Approvals_Store::class ) ) {
return null;
}

$store = new \WordPress\AI\Connector_Approval\Approvals_Store();
$pending = $store->get_pending();

if ( empty( $pending ) ) {
return null;
}

// Honour the same dismissal state that Admin_Notice uses.
$keys = array_keys( $pending );
sort( $keys );
$signature = md5( implode( '|', $keys ) );
$dismissed = (string) get_user_meta( get_current_user_id(), 'wpai_connector_approval_notice_dismissed', true );

if ( $signature === $dismissed ) {
return null;
}

return array(
'count' => count( $pending ),
'reviewUrl' => \WordPress\AI\Experiments\Connector_Approval\Admin_Page::url(),
'dismissUrl' => wp_nonce_url(
add_query_arg( 'wpai_ca_notice_dismiss', '1' ),
'wpai_ca_notice'
),
);
}

/**
* Builds the provider metadata payload sent to the React app.
*
Expand Down
24 changes: 23 additions & 1 deletion src/admin/ai-request-logs/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Page } from '@wordpress/admin-ui';
import apiFetch from '@wordpress/api-fetch';
import { Notice } from '@wordpress/components';
import { dispatch } from '@wordpress/data';
import { __ } from '@wordpress/i18n';
import { __, _n, sprintf } from '@wordpress/i18n';
import { store as noticesStore } from '@wordpress/notices';
import {
createRoot,
Expand Down Expand Up @@ -327,6 +327,28 @@ const App: React.FC = () => {
}
>
<div className="ai-request-logs__app">
{ settings.connectorApprovalNotice && (
<Notice status="warning" isDismissible={ false }>
{ sprintf(
/* translators: %d: number of pending approval requests. */
_n(
'%d plugin or theme is requesting access to an AI connector.',
'%d plugins or themes are requesting access to AI connectors.',
settings.connectorApprovalNotice.count,
'ai'
),
settings.connectorApprovalNotice.count
) }{ ' ' }
<a href={ settings.connectorApprovalNotice.reviewUrl }>
{ __( 'Review requests', 'ai' ) }
</a>
{ ' · ' }
<a href={ settings.connectorApprovalNotice.dismissUrl }>
{ __( 'Dismiss', 'ai' ) }
</a>
</Notice>
) }

{ error && (
<Notice status="error" onRemove={ () => setError( null ) }>
{ error }
Expand Down
7 changes: 7 additions & 0 deletions src/admin/ai-request-logs/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ export interface LogsQuery {
order: 'asc' | 'desc';
}

export interface ConnectorApprovalNotice {
count: number;
reviewUrl: string;
dismissUrl: string;
}

export interface LocalizedSettings {
rest: {
nonce: string;
Expand All @@ -98,6 +104,7 @@ export interface LocalizedSettings {
};
connectorsUrl: string;
providerMetadata: ProviderMetadataMap;
connectorApprovalNotice: ConnectorApprovalNotice | null;
}

declare global {
Expand Down
Loading