diff --git a/projects/js-packages/shared-extension-utils/changelog/is-admin-connected b/projects/js-packages/shared-extension-utils/changelog/is-admin-connected new file mode 100644 index 0000000000000..d3f383f2177f4 --- /dev/null +++ b/projects/js-packages/shared-extension-utils/changelog/is-admin-connected @@ -0,0 +1,4 @@ +Significance: minor +Type: added + +Added new isAdminConnected utility diff --git a/projects/js-packages/shared-extension-utils/index.js b/projects/js-packages/shared-extension-utils/index.js index 2ca584b2545d6..3671280e0483c 100644 --- a/projects/js-packages/shared-extension-utils/index.js +++ b/projects/js-packages/shared-extension-utils/index.js @@ -16,6 +16,7 @@ export { getUsableBlockProps, } from './src/plan-utils'; export { default as isCurrentUserConnected } from './src/is-current-user-connected'; +export { default as isAdminConnected } from './src/is-admin-connected'; export { default as useAnalytics } from './src/hooks/use-analytics'; export { default as useAutosaveAndRedirect } from './src/hooks/use-autosave-and-redirect'; export * from './src/hooks/use-plan-type'; diff --git a/projects/js-packages/shared-extension-utils/src/is-admin-connected.js b/projects/js-packages/shared-extension-utils/src/is-admin-connected.js new file mode 100644 index 0000000000000..f40aeaf840c64 --- /dev/null +++ b/projects/js-packages/shared-extension-utils/src/is-admin-connected.js @@ -0,0 +1,17 @@ +import getJetpackData from './get-jetpack-data'; + +/** + * Return whether the site has a connected admin user. + * + * @return {boolean} Whether the site has a connected admin. + */ +export default function isAdminConnected() { + if ( + getJetpackData()?.jetpack?.has_connected_admin || + window?.JP_CONNECTION_INITIAL_STATE?.connectionStatus?.hasConnectedOwner || + window?.Jetpack_Editor_Initial_State?.connectionStatus?.hasConnectedAdmin + ) { + return true; + } + return false; +} diff --git a/projects/packages/my-jetpack/changelog/feature-ai-assistant-editor b/projects/packages/my-jetpack/changelog/feature-ai-assistant-editor new file mode 100644 index 0000000000000..d3c3936c972a7 --- /dev/null +++ b/projects/packages/my-jetpack/changelog/feature-ai-assistant-editor @@ -0,0 +1,4 @@ +Significance: patch +Type: changed + +Jetpack AI: Allow non admin users diff --git a/projects/packages/my-jetpack/src/class-rest-ai.php b/projects/packages/my-jetpack/src/class-rest-ai.php index 3969810726da4..f5536bf7b17b9 100644 --- a/projects/packages/my-jetpack/src/class-rest-ai.php +++ b/projects/packages/my-jetpack/src/class-rest-ai.php @@ -34,7 +34,7 @@ public function __construct() { 'methods' => \WP_REST_Server::EDITABLE, 'callback' => __CLASS__ . '::get_openai_jwt', 'permission_callback' => function () { - return ( new Connection_Manager( 'jetpack' ) )->is_user_connected() && current_user_can( 'edit_posts' ); + return ( new Connection_Manager( 'jetpack' ) )->has_connected_admin() && current_user_can( 'edit_posts' ); }, ) ); @@ -61,7 +61,7 @@ public static function is_rest_endpoint_registered( $namespace, $route ) { public static function get_openai_jwt() { $blog_id = Jetpack_Options::get_option( 'id' ); - $response = Client::wpcom_json_api_request_as_user( + $response = Client::wpcom_json_api_request_as_blog( "/sites/$blog_id/jetpack-openai-query/jwt", '2', array( diff --git a/projects/plugins/jetpack/_inc/lib/class-jetpack-ai-helper.php b/projects/plugins/jetpack/_inc/lib/class-jetpack-ai-helper.php index 1d31419d01721..e53528046a26a 100644 --- a/projects/plugins/jetpack/_inc/lib/class-jetpack-ai-helper.php +++ b/projects/plugins/jetpack/_inc/lib/class-jetpack-ai-helper.php @@ -254,7 +254,7 @@ function ( $msg ) { return $response; } - $response = Client::wpcom_json_api_request_as_user( + $response = Client::wpcom_json_api_request_as_blog( sprintf( '/sites/%d/jetpack-ai/completions', $site_id ), 2, array( @@ -328,7 +328,7 @@ public static function get_dalle_generation( $prompt, $post_id ) { return $result; } - $response = Client::wpcom_json_api_request_as_user( + $response = Client::wpcom_json_api_request_as_blog( sprintf( '/sites/%d/jetpack-ai/images/generations', $site_id ), 2, array( @@ -428,6 +428,8 @@ public static function get_ai_assistance_feature() { $request_path = sprintf( '/sites/%d/jetpack-ai/ai-assistant-feature', $blog_id ); + // This is the only request to an AI endpoint that is made as a user and it should stay that way because of the + // permission checks in get_status_permission_check(). $wpcom_request = Client::wpcom_json_api_request_as_user( $request_path, 'v2', diff --git a/projects/plugins/jetpack/_inc/lib/class.core-rest-api-endpoints.php b/projects/plugins/jetpack/_inc/lib/class.core-rest-api-endpoints.php index 3797d21fadbd5..317fe91a1b4fa 100644 --- a/projects/plugins/jetpack/_inc/lib/class.core-rest-api-endpoints.php +++ b/projects/plugins/jetpack/_inc/lib/class.core-rest-api-endpoints.php @@ -84,7 +84,7 @@ public static function register_endpoints() { 'methods' => WP_REST_Server::EDITABLE, 'callback' => __CLASS__ . '::get_openai_jwt', 'permission_callback' => function () { - return ( new Connection_Manager( 'jetpack' ) )->is_user_connected() && current_user_can( 'edit_posts' ); + return ( new Connection_Manager( 'jetpack' ) )->has_connected_admin() && current_user_can( 'edit_posts' ); }, ) ); @@ -817,7 +817,7 @@ public static function register_endpoints() { public static function get_openai_jwt() { $blog_id = \Jetpack_Options::get_option( 'id' ); - $response = \Automattic\Jetpack\Connection\Client::wpcom_json_api_request_as_user( + $response = \Automattic\Jetpack\Connection\Client::wpcom_json_api_request_as_blog( "/sites/$blog_id/jetpack-openai-query/jwt", '2', array( diff --git a/projects/plugins/jetpack/changelog/feature-ai-assistant-editor b/projects/plugins/jetpack/changelog/feature-ai-assistant-editor new file mode 100644 index 0000000000000..eb6f53b359667 --- /dev/null +++ b/projects/plugins/jetpack/changelog/feature-ai-assistant-editor @@ -0,0 +1,4 @@ +Significance: patch +Type: other + +Jetpack AI: Allow non admin users diff --git a/projects/plugins/jetpack/extensions/blocks/ai-assistant/edit.js b/projects/plugins/jetpack/extensions/blocks/ai-assistant/edit.js index 2817ee97ea823..47e26cdade1da 100644 --- a/projects/plugins/jetpack/extensions/blocks/ai-assistant/edit.js +++ b/projects/plugins/jetpack/extensions/blocks/ai-assistant/edit.js @@ -17,7 +17,7 @@ import { PLAN_TYPE_FREE, PLAN_TYPE_UNLIMITED, usePlanType, - isUserConnected, + isAdminConnected, } from '@automattic/jetpack-shared-extension-utils'; import { useBlockProps, InspectorControls } from '@wordpress/block-editor'; import { rawHandler } from '@wordpress/blocks'; @@ -140,7 +140,7 @@ export default function AIAssistantEdit( { attributes, setAttributes, clientId, const isWaitingResponse = requestingState === 'requesting'; const isLoadingCompletion = [ 'requesting', 'suggesting' ].includes( requestingState ); - const connected = isUserConnected(); + const connected = isAdminConnected(); const { productPageUrl } = useAiProductPage(); diff --git a/projects/plugins/jetpack/extensions/blocks/ai-assistant/extensions/lib/can-ai-assistant-be-enabled.ts b/projects/plugins/jetpack/extensions/blocks/ai-assistant/extensions/lib/can-ai-assistant-be-enabled.ts index e5b2e7b99f63d..c0f06bdfc6f04 100644 --- a/projects/plugins/jetpack/extensions/blocks/ai-assistant/extensions/lib/can-ai-assistant-be-enabled.ts +++ b/projects/plugins/jetpack/extensions/blocks/ai-assistant/extensions/lib/can-ai-assistant-be-enabled.ts @@ -1,7 +1,7 @@ /* * External dependencies */ -import { isUserConnected } from '@automattic/jetpack-shared-extension-utils'; +import { isAdminConnected } from '@automattic/jetpack-shared-extension-utils'; import { getBlockType } from '@wordpress/blocks'; import { select } from '@wordpress/data'; /* @@ -32,7 +32,7 @@ export function canAIAssistantBeEnabled(): boolean { } // Do not enable AI Assistant if the site is not connected. - const connected = isUserConnected(); + const connected = isAdminConnected(); if ( ! connected ) { return false; } diff --git a/projects/plugins/jetpack/extensions/plugins/ai-content-lens/ai-content-lens.php b/projects/plugins/jetpack/extensions/plugins/ai-content-lens/ai-content-lens.php index e008b0b7325da..fcf6a5209938b 100644 --- a/projects/plugins/jetpack/extensions/plugins/ai-content-lens/ai-content-lens.php +++ b/projects/plugins/jetpack/extensions/plugins/ai-content-lens/ai-content-lens.php @@ -25,7 +25,7 @@ function register_plugin() { // Connection check. if ( ( new Host() )->is_wpcom_simple() - || ( ( new Connection_Manager( 'jetpack' ) )->has_connected_owner() && ! ( new Status() )->is_offline_mode() ) + || ( ( new Connection_Manager( 'jetpack' ) )->has_connected_admin() && ! ( new Status() )->is_offline_mode() ) ) { // Register AI Content lens plugin. \Jetpack_Gutenberg::set_extension_available( FEATURE_NAME );