Skip to content

Jetpack AI: allow site editors to use Jetpack AI features #43181

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

Open
wants to merge 19 commits into
base: trunk
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

Added new isAdminConnected utility
1 change: 1 addition & 0 deletions projects/js-packages/shared-extension-utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: changed

Jetpack AI: Allow non admin users
4 changes: 2 additions & 2 deletions projects/packages/my-jetpack/src/class-rest-ai.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
},
)
);
Expand All @@ -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(
Expand Down
6 changes: 4 additions & 2 deletions projects/plugins/jetpack/_inc/lib/class-jetpack-ai-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
},
)
);
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: other

Jetpack AI: Allow non admin users
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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();

Expand Down
Original file line number Diff line number Diff line change
@@ -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';
/*
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
Loading