Skip to content
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

SEO Enhancer: Count requests for image alt text #42659

Open
wants to merge 2 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: patch
Type: other

SEO Enhancer: Count requests for image alt text
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
askQuestionSync,
getAllBlocks,
getBase64Image,
useAiFeature,
usePostContent,
} from '@automattic/jetpack-ai-client';
import { select as globalSelect, useDispatch, useSelect } from '@wordpress/data';
Expand Down Expand Up @@ -50,6 +51,7 @@ export const useSeoRequests = (
const { isImageBusy, hasImageFailed } = useSelect( select => select( store ), [] );
const { setImageBusy, setImageFailed } = useDispatch( store );
const { createInfoNotice } = useDispatch( 'core/notices' );
const { increaseRequestsCount, dequeueAsyncRequest, requireUpgrade } = useAiFeature();

const request = useCallback(
async ( type: PromptType, block?: Block, useBase64Image: boolean = false ) => {
Expand Down Expand Up @@ -159,6 +161,11 @@ export const useSeoRequests = (

const updateAltText = useCallback(
async ( block: Block, useBase64Image: boolean = false ) => {
if ( requireUpgrade ) {
debug( 'Upgrade required, skipping' );
return null;
}

if ( isImageBusy( block.clientId ) ) {
debug( 'Already updating alt text, skipping' );
return null;
Expand All @@ -171,8 +178,14 @@ export const useSeoRequests = (

try {
setImageBusy( block.clientId, true );
dequeueAsyncRequest();

const response = await request( 'images-alt-text', block, useBase64Image );

increaseRequestsCount();

const altText = parseResponse( response ).texts?.[ 0 ];

await updateBlockAttributes( block.clientId, { alt: altText } );
setImageBusy( block.clientId, false );

Expand All @@ -193,7 +206,17 @@ export const useSeoRequests = (
return false;
}
},
[ isImageBusy, hasImageFailed, setImageBusy, request, updateBlockAttributes, setImageFailed ]
[
requireUpgrade,
isImageBusy,
hasImageFailed,
setImageBusy,
dequeueAsyncRequest,
request,
increaseRequestsCount,
updateBlockAttributes,
setImageFailed,
]
);

const updateAltTexts = useCallback(
Expand Down
Loading