# ai/trunk/build-scripts/experiments/editorial-notes.js

AI, version trunk. 6 lines.

- Page: https://pluginprobe.com/plugins/ai/trunk/code/build-scripts/experiments/editorial-notes.js
- Raw: https://pluginprobe.com/plugins/ai/trunk/raw/build-scripts/experiments/editorial-notes.js
- Modified: 2026-08-18T21:09:32+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/ai/trunk/code/build-scripts/experiments/editorial-notes.js#L10-L20`.

```javascript
(()=>{"use strict";const e={n:t=>{const n=t&&t.__esModule?()=>t.default:()=>t;return e.d(n,{a:n}),n},d:(t,n)=>{if(Array.isArray(n))for(var i=0;i<n.length;){var o=n[i++],s=n[i++];e.o(t,o)?0===s&&i++:0===s?Object.defineProperty(t,o,{enumerable:!0,value:n[i++]}):Object.defineProperty(t,o,{enumerable:!0,get:s})}else for(var o in n)e.o(n,o)&&!e.o(t,o)&&Object.defineProperty(t,o,{enumerable:!0,get:n[o]})},o:(e,t)=>Object.hasOwn(e,t)},t=window.wp.editor,n=window.wp.plugins,i=window.wp.components,o=window.wp.blockEditor,s=window.wp.data,r=window.wp.editPost,a=window.wp.element,c=window.wp.i18n,l=window.wp.primitives,d=window.ReactJSXRuntime;var u=(0,d.jsx)(l.SVG,{xmlns:"http://www.w3.org/2000/svg",viewBox:"0 0 24 24",children:(0,d.jsx)(l.Path,{fillRule:"evenodd",clipRule:"evenodd",d:"M6.68822 16.625L5.5 17.8145L5.5 5.5L18.5 5.5L18.5 16.625L6.68822 16.625ZM7.31 18.125L19 18.125C19.5523 18.125 20 17.6773 20 17.125L20 5C20 4.44772 19.5523 4 19 4H5C4.44772 4 4 4.44772 4 5V19.5247C4 19.8173 4.16123 20.086 4.41935 20.2237C4.72711 20.3878 5.10601 20.3313 5.35252 20.0845L7.31 18.125ZM16 9.99997H8V8.49997H16V9.99997ZM8 14H13V12.5H8V14Z"})});const p=window.wp.compose,h=window.wp.apiFetch;var g=e.n(h);const w=["core/paragraph","core/heading","core/list-item","core/verse","core/image","core/table","core/preformatted","core/pullquote"];async function f(e,t){const n=[];let i=1;for(;;)try{const o=await g()({path:`/wp/v2/comments?type=note&status=${t}&post=${e}&per_page=100&page=${i}`,method:"GET"});if(n.push(...o),o.length<100)return n;i+=1}catch(e){return console.warn(`[AI Notes] Failed to fetch ${t} Notes page ${i}:`,e),n}}const b=window.wp.coreData,m=window.wp.notices,v=window.wp.blocks;function y(e){return e.reduce((e,t)=>(e.push(t),t.innerBlocks?.length&&e.push(...y(t.innerBlocks)),e),[])}const _=window.wp.url;let S=!1;const C=()=>{S||(console.warn("[AI] Client ability execution is unavailable. Falling back to REST."),S=!0)},k=(e,t,n)=>{if("GET"===n||"DELETE"===n)return{path:null==t?`/wp-abilities/v1/abilities/${e}/run`:(0,_.addQueryArgs)(`/wp-abilities/v1/abilities/${e}/run`,{input:t}),method:n};const i={path:`/wp-abilities/v1/abilities/${e}/run`,method:"POST"};return void 0!==t&&(i.data={input:t}),i};const x=window.wp.wordcount;function E(e,t){return function(e){return(0,x.count)(e,"characters_excluding_spaces")}(e)>=t}const j="[[BLOCK_GOES_HERE]]",R="ai_editorial_notes_error";function N(){const e=(0,s.useSelect)(e=>e(t.store).getEditedPostContent(),[])??"",n=window.aiEditorialNotesData?.minContentLength??75;return{content:e,minContentLength:n,isContentTooShort:!E(e,n)}}async function I(e,t,n,i,r,a){const c=e.attributes.metadata?.noteId??null;if(c&&i.has(c))return 0;const l=function(e){const t=e.attributes;switch(e.name){case"core/image":return[t.alt??"",t.caption??""].filter(Boolean).join(" ");case"core/table":return"";default:return"string"==typeof(n=t.content??t.value??"")?n:n&&"object"==typeof n&&"text"in n&&"string"==typeof n.text?n.text:""}var n}(e);if(0===l.length)return 0;const d=[];if(c){const e=r.get(c);e&&d.push(e);for(const e of a)if(e.parent===c){const t=r.get(e.id);t&&d.push(t)}}const u=void 0!==e.clientId?function(e,t,n){const i=(0,s.select)(o.store).getBlock(t);if(!i)return e;const r=(0,v.serialize)([i]);if(!r||!e.includes(r))return e;const a=y((0,s.select)(o.store).getBlocks()??[]);let c=1,l=0;for(const e of a)if((0,v.serialize)(e)===r&&(l+=1,e.clientId===t)){c=l;break}let d=0,u=0;for(;;){const t=e.indexOf(r,u);if(-1===t)return e;if(d+=1,d===c)return e.slice(0,t)+n+e.slice(t+r.length);u=t+r.length}}(n,e.clientId,j):n,p=function(e,t){const n=e.indexOf(t);if(-1===n||e.length<=1600)return e;const i=Math.max(0,n-800),o=Math.min(e.length,n+19+800),s=e=>/\s/.test(e);let r=i;if(r>0&&!s(e.charAt(r-1)))for(;r<o&&!s(e.charAt(r));)r+=1;let a=o;if(a<e.length&&!s(e.charAt(a)))for(;a>r&&!s(e.charAt(a-1));)a-=1;const c=r>0?"[TRUNCATED BEFORE]\n":"",l=a<e.length?"\n[TRUNCATED AFTER]":"";return`${c}${e.slice(r,a)}${l}`}(u,j),h=await async function(e,t,n){try{const n=await(async()=>{try{const{ready:e}=await import("@wordpress/core-abilities");return await e,await import("@wordpress/abilities")}catch{return null}})();if("function"==typeof n?.executeAbility)return await n.executeAbility(e,t);C()}catch(e){if(console.error(e),!(e=>{if(!e||"object"!=typeof e)return!1;const t="message"in e&&"string"==typeof e.message?e.message:"",n="code"in e&&"string"==typeof e.code?e.code:"";return"ability_not_found"===n||t.includes("Ability not found")||t.includes("missing callback")||"ability_invalid_input"===n})(e))throw e;C()}const i=n?.method??"POST";return await g()({...k(e,t,i),...n?.signal?{signal:n.signal}:{}})}("ai/editorial-notes",{block_type:e.name,block_content:l,context:p,post_id:t,existing_notes:d});return h?.suggestions&&h.suggestions.length>0?(await async function(e,t,n,i){const r=n.map(e=>`[${e.review_type.toUpperCase()}] ${e.text}`).join("\n\n"),a=await(0,s.dispatch)(b.store).saveEntityRecord("root","comment",{post:t,content:r,type:"note",status:"hold",parent:i??0,meta:{wpai_note:!0}});if(!i&&a?.id){const t=(0,s.select)(o.store).getBlockAttributes(e.clientId)?.metadata??{};(0,s.dispatch)(o.store).updateBlockAttributes(e.clientId,{metadata:{...t,noteId:a.id}})}}(e,t,h.suggestions,c),h.suggestions.length):0}function P(){const[e,n]=(0,a.useState)(!1),[i,r]=(0,a.useState)(0),[l,d]=(0,a.useState)(0),[u,p]=(0,a.useState)(null),{content:h,isContentTooShort:g,minContentLength:v}=N();return{isReviewing:e,progress:i,total:l,lastRunCount:u,isContentTooShort:g,minContentLength:v,runReview:async()=>{if(function(e){const t=window.aiProviderData??{hasProvider:!1,connectorsUrl:""};if(t.hasProvider)return!0;const{connectorsUrl:n}=t;return(0,s.dispatch)(m.store).createErrorNotice((0,c.__)("This feature requires an AI Connector to function properly.","ai"),{id:e,isDismissible:!0,actions:n?[{label:(0,c.__)("Manage Connectors","ai"),url:n}]:[]}),!1}(R)&&!g){n(!0),r(0),d(0),p(null),(0,s.dispatch)(m.store).removeNotice(R);try{const e=(0,s.select)(t.store).getCurrentPostId(),n=y((0,s.select)(o.store).getBlocks()).filter(e=>w.includes(e.name));if(0===n.length)return void p(0);d(n.length);const[i,a]=await Promise.all([f(e,"hold"),f(e,"approve")]),l=new Set(a.map(e=>e.id)),u=new Map;for(const e of i)u.set(e.id,e.content?.rendered??"");let g=0;for(let t=0;t<n.length;t+=4){const o=n.slice(t,t+4);g+=(await Promise.all(o.map(t=>I(t,e,h,l,u,i)))).reduce((e,t)=>e+t,0),r(Math.min(t+4,n.length))}p(g),g>0&&((0,s.dispatch)(b.store).invalidateResolutionForStoreSelector("getEntityRecords"),(0,s.dispatch)(m.store).createSuccessNotice((0,c.sprintf)(/* translators: %d: number of suggestions added. */ /* translators: %d: number of suggestions added. */
(0,c._n)("%d suggestion added. Save to keep changes.","%d suggestions added. Save to keep changes.",g,"ai"),g),{type:"snackbar"}))}catch(e){(0,s.dispatch)(m.store).createErrorNotice(e?.message??String(e),{id:R,isDismissible:!0})}finally{n(!1)}}}}}function T(){const{isReviewing:e,progress:n,total:l,lastRunCount:h,isContentTooShort:g,minContentLength:v,runReview:y}=P(),{isReviewing:_,reviewingClientId:S,reviewBlock:C,isContentTooShort:k}=function(){const[e,n]=(0,a.useState)(null),{content:i,isContentTooShort:l,minContentLength:d}=N();return{isReviewing:!!e,reviewingClientId:e,isContentTooShort:l,minContentLength:d,reviewBlock:async e=>{if(!l){n(e),(0,s.dispatch)(m.store).removeNotice("ai_editorial_block_error");try{const n=(0,s.select)(o.store).getBlock(e);if(!n)return;const a=(0,s.select)(t.store).getCurrentPostId(),[l,d]=await Promise.all([f(a,"hold"),f(a,"approve")]),u=new Set(d.map(e=>e.id)),p=new Map;for(const e of l)p.set(e.id,e.content?.rendered??"");const h=await I(n,a,i,u,p,l);h>0?((0,s.dispatch)(b.store).invalidateResolutionForStoreSelector("getEntityRecords"),(0,s.dispatch)(r.store).openGeneralSidebar?.("edit-post/collab-sidebar"),(0,s.dispatch)(m.store).createSuccessNotice((0,c.sprintf)(/* translators: %d: number of suggestions added. */ /* translators: %d: number of suggestions added. */
(0,c._n)("%d suggestion added. Save to keep changes.","%d suggestions added. Save to keep changes.",h,"ai"),h),{type:"snackbar"})):(0,s.dispatch)(m.store).createNotice("info",(0,c.__)("No new suggestions found.","ai"),{type:"snackbar"})}catch(e){(0,s.dispatch)(m.store).createErrorNotice(e?.message??String(e),{id:"ai_editorial_block_error",isDismissible:!0})}finally{n(null)}}}}}(),{openGeneralSidebar:x}=(0,s.useDispatch)(r.store),E=(0,s.useSelect)(e=>e(o.store).getSettings().isPreviewMode,[]),j=(0,p.useInstanceId)(T,"editorial-notes-plugin-description");if(!window.aiEditorialNotesData?.enabled)return null;const R=(0,c.sprintf)(/* translators: 1: number of blocks reviewed so far, 2: total blocks to review. */ /* translators: 1: number of blocks reviewed so far, 2: total blocks to review. */
(0,c.__)("Reviewing blocks… (%1$d of %2$d)","ai"),n,l),A=e?R:(0,c.__)("Generate Editorial Notes","ai"),B=g?(0,c.sprintf)(/* translators: %d: minimum number of characters required. */ /* translators: %d: minimum number of characters required. */
(0,c.__)("Editorial Notes will be available when the post content has at least %d characters.","ai"),v):(0,c.__)("This analyzes the content of this post block-by-block and adds editorial Notes with suggestions on each block.","ai");return(0,d.jsxs)(d.Fragment,{children:[(0,d.jsx)(t.PluginPostStatusInfo,{children:(0,d.jsxs)(i.Flex,{direction:"column",gap:2,children:[(0,d.jsx)(i.FlexItem,{children:(0,d.jsx)(i.Button,{accessibleWhenDisabled:!0,variant:"secondary",icon:u,onClick:y,isBusy:e,disabled:e||g,style:{justifyContent:"center",width:"100%"},__next40pxDefaultSize:!0,"aria-describedby":j,children:A})}),null!==h&&(0,d.jsx)(i.FlexItem,{children:(0,d.jsx)("span",{className:"description",role:"status",children:0===h?(0,c.__)("No new suggestions found.","ai"):(0,a.createInterpolateElement)((0,c.sprintf)(/* translators: %d: number of suggestions added. The <a> tags wrap a link to open the Notes panel. */ /* translators: %d: number of suggestions added. The <a> tags wrap a link to open the Notes panel. */
(0,c._n)("%d suggestion added, view those Notes <a>here</a>.","%d suggestions added, view those Notes <a>here</a>.",h,"ai"),h),{a:(0,d.jsx)(i.Button,{variant:"link",onClick:()=>x?.("edit-post/collab-sidebar")})})})}),(0,d.jsx)(i.FlexItem,{children:(0,d.jsx)("span",{id:j,className:"description",style:{color:"#757575"},children:B})})]})}),(0,d.jsx)(o.BlockSettingsMenuControls,{children:({selectedBlocks:e,selectedClientIds:t})=>{if(!e.every(e=>w.includes(e))||E)return null;const n=t[0]??null,o=S===n;return(0,d.jsx)(i.MenuItem,{icon:o?(0,d.jsx)(i.Spinner,{}):u,disabled:_||k,onClick:()=>{n&&C(n)},..._&&!o&&{info:(0,c.__)("Another block is currently being reviewed.","ai")},children:o?(0,c.__)("Reviewing…","ai"):(0,c.__)("Generate Editorial Note","ai")})}})]})}(0,n.registerPlugin)("ai-editorial-notes",{render:()=>(0,d.jsx)(t.PostTypeSupportCheck,{supportKeys:"editor.notes",children:(0,d.jsx)(T,{})})})})();
```
