import React, { useEffect, useRef, useState } from 'react';
import { __, _n, sprintf } from '@wordpress/i18n';
import { api } from '../api';
import ScreenVerdict from '../components/ScreenVerdict';
import Bing from './Bing';
import { Card, Button, Badge, Notice, Switch, Bar, Tabs, Panel, Advisor, PageState } from '../components/ui';
import RichResults from '../components/RichResults';
const VERIFY = [
{ key: 'verify_google', label: __('Google Search Console', 'ai-rank-suite'), hint: __('The content value of the google-site-verification tag', 'ai-rank-suite') },
{ key: 'verify_bing', label: __('Bing Webmaster Tools', 'ai-rank-suite'), hint: 'msvalidate.01' },
{ key: 'verify_pinterest', label: __('Pinterest', 'ai-rank-suite'), hint: 'p:domain_verify' },
{ key: 'verify_yandex', label: __('Yandex', 'ai-rank-suite'), hint: 'yandex-verification' },
{ key: 'verify_baidu', label: __('Baidu', 'ai-rank-suite'), hint: __('baidu-site-verification — China’s biggest engine, and its AI answer layer', 'ai-rank-suite') },
{ key: 'verify_naver', label: __('Naver', 'ai-rank-suite'), hint: __('naver-site-verification — the default engine in South Korea', 'ai-rank-suite') },
{ key: 'verify_facebook', label: __('Meta / Facebook', 'ai-rank-suite'), hint: __('facebook-domain-verification — needed for catalogue and ad domains', 'ai-rank-suite') },
];
function IndexNow() {
const [data, setData] = useState(null);
const [busy, setBusy] = useState(false);
const [saved, setSaved] = useState(false);
const [error, setError] = useState(null);
const [urls, setUrls] = useState('');
useEffect(() => {
api.get('/settings/indexnow').then(setData).catch((e) => setError(e.message));
}, []);
if (!data) return ;
const s = data.settings;
const update = (patch) => setData({ ...data, settings: { ...s, ...patch } });
const run = async (fn) => {
setBusy(true);
setError(null);
try {
setData(await fn());
return true;
} catch (e) {
setError(e.message);
return false;
} finally {
setBusy(false);
}
};
const save = async () => {
if (await run(() => api.put('/settings/indexnow', s))) {
setSaved(true);
setTimeout(() => setSaved(false), 2200);
}
};
const list = urls
.split(/[\n\s]+/)
.map((u) => u.trim())
.filter(Boolean);
return (
<>
{error && {error} }
{data.recent?.length > 0 && (
{
const failed = data.recent.filter((r) => !r.ok).length;
return failed > 0
? sprintf(__('%1$d recent, %2$d rejected — a 429 means you submitted the same URL too often', 'ai-rank-suite'), data.recent.length, failed)
: sprintf(__('%d recent, all accepted', 'ai-rank-suite'), data.recent.length);
})()
}
action={ !r.ok) ? 'warning' : 'good'}>{data.recent.length} }
>
{__('URLs', 'ai-rank-suite')}
{__('First URL', 'ai-rank-suite')}
{__('Result', 'ai-rank-suite')}
{__('When', 'ai-rank-suite')}
{data.recent.slice(0, 12).map((r, i) => (
{r.count}
{String(r.first).replace(/^https?:\/\/[^/]+/, '')}
{r.ok ? (
✓ {r.code}
) : (
✕ {r.code || __('error', 'ai-rank-suite')}
)}
{new Date(r.time * 1000).toLocaleString()}
))}
)}
✓ {__('Active', 'ai-rank-suite')} : {__('Off', 'ai-rank-suite')} }
>
{!data.permalinks && (
{__('IndexNow needs pretty permalinks so the key file can be served. Switch Settings → Permalinks away from “Plain” first.', 'ai-rank-suite')}
)}
update({ enabled: v })}
label={__('Enable IndexNow submissions', 'ai-rank-suite')}
/>
update({ auto: v })}
label={__('Submit automatically when a page is published, updated or unpublished', 'ai-rank-suite')}
/>
{data.post_types.map((pt) => (
update({
post_types: e.target.checked
? [...s.post_types, pt]
: s.post_types.filter((x) => x !== pt),
})
}
/>
{pt}
))}
{__('Endpoint', 'ai-rank-suite')}
update({ endpoint: e.target.value })}>
{Object.entries(data.endpoints).map(([url, label]) => (
{label}
))}
{__('All participating engines share one network — the first option reaches every one of them.', 'ai-rank-suite')}
{__('Save', 'ai-rank-suite')}
{saved && ✓ {__('Saved', 'ai-rank-suite')} }
run(() => api.post('/indexnow/key', {}))} busy={busy}>{__('Generate a new key', 'ai-rank-suite')}
{__('Open the link — it must show the key as plain text. Regenerating invalidates the old key immediately.', 'ai-rank-suite')}
>
);
}
export default function Google({ tab = 'indexing', onTab }) {
const [data, setData] = useState(null);
const [busy, setBusy] = useState(false);
const [saved, setSaved] = useState(false);
const [error, setError] = useState(null);
const [manualUrl, setManualUrl] = useState('');
const [pastedKey, setPastedKey] = useState('');
const fileRef = useRef(null);
useEffect(() => {
api.get('/settings/google').then(setData).catch((e) => setError(e.message));
}, []);
if (!data) return ;
const s = data.settings;
const update = (patch) => setData({ ...data, settings: { ...s, ...patch } });
const save = async () => {
setBusy(true);
setError(null);
try {
setData(await api.put('/settings/google', s));
setSaved(true);
setTimeout(() => setSaved(false), 2200);
} catch (e) {
setError(e.message);
} finally {
setBusy(false);
}
};
/*
* One path for both doors. The route has always taken the key as a string;
* the file is read into one before it is sent, so pasting needed no server
* change at all — only a field.
*/
const saveKey = async (json) => {
setBusy(true);
setError(null);
try {
setData(await api.post('/google/indexing/account', { json }));
setPastedKey('');
} catch (e) {
setError(e.message);
} finally {
setBusy(false);
if (fileRef.current) fileRef.current.value = '';
}
};
const uploadKey = async (file) => saveKey(await file.text());
const removeKey = async () => {
setBusy(true);
try {
setData(await api.del('/google/indexing/account'));
} catch (e) {
setError(e.message);
} finally {
setBusy(false);
}
};
const submitUrl = async () => {
setBusy(true);
setError(null);
try {
setData(await api.post('/google/indexing/submit', { url: manualUrl }));
setManualUrl('');
} catch (e) {
setError(e.message);
} finally {
setBusy(false);
}
};
const idx = data.indexing;
const quotaPct = Math.round((idx.used_today / idx.quota) * 100);
return (
{error &&
{error} }
{tab === 'indexing' && (
<>
✓ {__('Connected', 'ai-rank-suite')} : {__('Not connected', 'ai-rank-suite')} }
>
{!idx.configured ? (
{__('In', 'ai-rank-suite')}{' '}
{__('Google Cloud → Service Accounts', 'ai-rank-suite')}
{__(', create a service account and download its', 'ai-rank-suite')} {__('JSON key', 'ai-rank-suite')} .
{__('Enable the', 'ai-rank-suite')} {__('Indexing API', 'ai-rank-suite')} {__('for that project (APIs & Services → Library).', 'ai-rank-suite')}
{__('In', 'ai-rank-suite')}{' '}
{__('Search Console → Settings → Users and permissions', 'ai-rank-suite')}
{__(', add the service account\'s email address as an', 'ai-rank-suite')} {__('Owner', 'ai-rank-suite')} {__('of this property.', 'ai-rank-suite')}
{__('Upload the JSON key here:', 'ai-rank-suite')}
{/*
* Two ways in, because the key arrives both ways.
* Google hands you a download, but by the time it
* reaches the person setting the site up it is
* usually a block of text in a message — and a
* screen that only takes a file makes them save
* the clipboard to disk first for no reason.
*/}
) : (
{idx.email}
{__('Remove key', 'ai-rank-suite')}
{__('Daily quota', 'ai-rank-suite')}
{sprintf(__('%1$s / %2$s URLs', 'ai-rank-suite'), idx.used_today, idx.quota)}
{__('Google officially supports the Indexing API for job postings and livestream pages. Other page types are commonly submitted too, but Google does not guarantee they will be crawled faster.', 'ai-rank-suite')}
)}
{idx.recent?.length > 0 && (
{__('URL', 'ai-rank-suite')}
{__('Type', 'ai-rank-suite')}
{__('Result', 'ai-rank-suite')}
{__('When', 'ai-rank-suite')}
{idx.recent.slice(0, 15).map((r, i) => (
{r.url.replace(/^https?:\/\/[^/]+/, '')}
{r.type === 'URL_DELETED' ? __('Removed', 'ai-rank-suite') : __('Updated', 'ai-rank-suite')}
{r.ok ? (
✓ {__('Sent', 'ai-rank-suite')}
) : (
✕ {__('Failed', 'ai-rank-suite')}
)}
{new Date(r.time * 1000).toLocaleString()}
))}
)}
>
)}
{tab === 'rich' &&
}
{tab === 'bing' &&
}
{tab === 'indexnow' &&
}
{tab === 'verify' && (
✓ {__('Saved', 'ai-rank-suite')}}
>
{VERIFY.map((v) => (
{v.label}
update({ [v.key]: e.target.value })}
/>
))}
{__('Save', 'ai-rank-suite')}
{__('Paste only the content value, not the whole meta tag — the plugin builds the tag for you.', 'ai-rank-suite')}
)}
);
}