Changeset 3327070
- Timestamp:
- 07/13/2025 04:07:14 PM (9 months ago)
- Location:
- kaigen
- Files:
-
- 12 added
- 12 edited
- 1 copied
-
tags/v0.2.1 (copied) (copied from kaigen/trunk)
-
tags/v0.2.1/.wordpress-org/icon-128x128.png (modified) (previous)
-
tags/v0.2.1/assets/KaiGen-logo-128x128.png (added)
-
tags/v0.2.1/assets/KaiGen-logo-no-background.png (added)
-
tags/v0.2.1/assets/KaiGen-logo-original.png (added)
-
tags/v0.2.1/assets/icon-256x256.png (added)
-
tags/v0.2.1/build/images (added)
-
tags/v0.2.1/build/images/KaiGen-logo-128x128.e1e4be15.png (added)
-
tags/v0.2.1/build/index.asset.php (modified) (1 diff)
-
tags/v0.2.1/build/index.js (modified) (1 diff)
-
tags/v0.2.1/inc/class-admin.php (modified) (2 diffs)
-
tags/v0.2.1/inc/class-rest-api.php (modified) (2 diffs)
-
tags/v0.2.1/kaigen.php (modified) (1 diff)
-
trunk/.wordpress-org/icon-128x128.png (modified) (previous)
-
trunk/assets/KaiGen-logo-128x128.png (added)
-
trunk/assets/KaiGen-logo-no-background.png (added)
-
trunk/assets/KaiGen-logo-original.png (added)
-
trunk/assets/icon-256x256.png (added)
-
trunk/build/images (added)
-
trunk/build/images/KaiGen-logo-128x128.e1e4be15.png (added)
-
trunk/build/index.asset.php (modified) (1 diff)
-
trunk/build/index.js (modified) (1 diff)
-
trunk/inc/class-admin.php (modified) (2 diffs)
-
trunk/inc/class-rest-api.php (modified) (2 diffs)
-
trunk/kaigen.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
kaigen/tags/v0.2.1/build/index.asset.php
r3315977 r3327070 1 <?php return array('dependencies' => array('react', 'wp- block-editor', 'wp-components', 'wp-data', 'wp-element', 'wp-hooks', 'wp-rich-text'), 'version' => 'bcdcd0944f242d728c85');1 <?php return array('dependencies' => array('react', 'wp-api-fetch', 'wp-block-editor', 'wp-components', 'wp-data', 'wp-element', 'wp-hooks', 'wp-rich-text'), 'version' => 'f8292307639d2bbd9613'); -
kaigen/tags/v0.2.1/build/index.js
r3315977 r3327070 1 (()=>{"use strict"; const e=window.React,t=window.wp.element,r=window.wp.components,a=async(e,t,r={})=>{try{const a=wp.data.select("core/editor")?.getEditorSettings()?.kaigen_provider;if(!a)throw new Error("No provider configured. Please check your plugin settings.");const n={prompt:e,provider:a};r.sourceImageUrl&&(n.source_image_url=r.sourceImageUrl),r.additionalImageUrls&&Array.isArray(r.additionalImageUrls)&&(n.additional_image_urls=r.additionalImageUrls),r.maskUrl&&(n.mask_url=r.maskUrl),r.moderation&&["auto","low"].includes(r.moderation)&&(n.moderation=r.moderation),r.style&&["natural","vivid"].includes(r.style)&&(n.style=r.style);const o=await wp.apiFetch({path:"/kaigen/v1/generate-image",method:"POST",data:n});if(o.code&&o.message){if("content_moderation"===o.code)throw new Error(o.message);if("replicate_error"===o.code)throw new Error("Image generation failed: "+o.message);throw new Error(o.message)}if(!o||!o.url)throw new Error("Invalid response from server: "+JSON.stringify(o));o.id&&"number"==typeof o.id&&o.id>0?t({url:o.url,alt:e,id:o.id,caption:""}):t({url:o.url,alt:e,caption:""})}catch(e){console.error("Image generation failed:",e),e.message&&console.error("Error message:",e.message),e.stack&&console.error("Error stack:",e.stack),t({error:e.message||"An unknown error occurred while generating the image"})}},n=({onSelect:n,shouldDisplay:o})=>{const[i,l]=(0,t.useState)(!1),[c,s]=(0,t.useState)(""),[d,m]=(0,t.useState)(!1),[u,g]=(0,t.useState)(null);return o?(0,e.createElement)(e.Fragment,null,(0,e.createElement)("div",{className:"block-editor-media-placeholder__url-input-container"},(0,e.createElement)(r.Button,{variant:"secondary",onClick:()=>l(!0),className:"components-button is-next-40px-default-size is-secondary"},"KaiGen")),i&&(0,e.createElement)(r.Modal,{title:"KaiGen",onRequestClose:()=>l(!1)},u&&(0,e.createElement)("p",{style:{color:"red"}},u),(0,e.createElement)(r.TextareaControl,{label:"Prompt",value:c,onChange:s,rows:4}),(0,e.createElement)(r.Button,{variant:"primary",onClick:()=>{c.trim()?(m(!0),g(null),a(c.trim(),(e=>{e.error?(g(e.error),m(!1)):(n(e),m(!1),l(!1))}))):g("Please enter a prompt for image generation.")},disabled:d||!c.trim()},d?(0,e.createElement)(e.Fragment,null,(0,e.createElement)(r.Spinner,null)," ","KaiGen is generating..."):"KaiGen"))):null},o=({isGenerating:a,onGenerateImage:n,isRegenerating:o,onRegenerateImage:i,isImageBlock:l,isTextSelected:c,supportsImageToImage:s})=>{const[d,m]=(0,t.useState)(!1),[u,g]=(0,t.useState)(""),[p,w]=(0,t.useState)(null);return l&&s?(0,e.createElement)(e.Fragment,null,(0,e.createElement)(r.ToolbarGroup,null,(0,e.createElement)(r.ToolbarButton,{icon:o?(0,e.createElement)(r.Spinner,null):"update",label:o?"KaiGen is generating...":"KaiGen",onClick:()=>m(!0),disabled:o})),d&&(0,e.createElement)(r.Modal,{title:"KaiGen : Edit Image",onRequestClose:()=>{m(!1),g(""),w(null)}},p&&(0,e.createElement)("p",{style:{color:"red"}},p),(0,e.createElement)(r.TextareaControl,{label:"Editing Instructions (optional)",value:u,onChange:g,rows:4}),(0,e.createElement)(r.Button,{variant:"primary",onClick:()=>{i(u.trim()),m(!1),g(""),w(null)},disabled:o},o?(0,e.createElement)(e.Fragment,null,(0,e.createElement)(r.Spinner,null),"Regenerating..."):"Regenerate Image"))):c?(0,e.createElement)(r.ToolbarGroup,null,(0,e.createElement)(r.ToolbarButton,{icon:a?(0,e.createElement)(r.Spinner,null):"format-image",label:a?"KaiGen is generating...":"KaiGen",onClick:n,disabled:a})):null},i=window.wp.blockEditor,l=window.wp.data;(0,window.wp.richText.registerFormatType)("kaigen/custom-format",{title:"AI Image Gen",tagName:"span",className:"kaigen-format",edit:({isActive:r,value:n,onChange:c})=>{const[s,d]=(0,t.useState)(!1),m=(0,l.useSelect)((e=>e("core/block-editor").getSelectedBlock()),[]),{replaceBlocks:u}=(0,l.useDispatch)("core/block-editor"),g=(0,t.useCallback)((()=>{if(m&&"core/paragraph"===m.name){const e=n.text.slice(n.start,n.end).trim();if(!e)return void wp.data.dispatch("core/notices").createErrorNotice("Please select some text to use as the image generation prompt.",{type:"snackbar"});const t=wp.data.select("core/editor")?.getEditorSettings()?.kaigen_provider;if(!t)return void wp.data.dispatch("core/notices").createErrorNotice("No AI provider configured. Please set one in the plugin settings.",{type:"snackbar"});const r=wp.blocks.createBlock("core/heading",{content:"Generating AI image...",level:2,style:{textAlign:"center"}});u(m.clientId,[r,m]),d(!0),a(e,(e=>{if(d(!1),e.error)console.error("Image generation failed:",e.error),wp.data.dispatch("core/notices").createErrorNotice("Failed to generate image: "+e.error,{type:"snackbar"}),u(r.clientId,[]);else{let t={url:e.url,alt:e.alt,caption:""};e.id&&"number"==typeof e.id&&e.id>0&&(t.id=e.id);const a=wp.blocks.createBlock("core/image",t);u(r.clientId,[a])}}))}}),[m,n.text,n.start,n.end,u]),p=""!==n.text.slice(n.start,n.end).trim();return(0,e.createElement)(i.BlockControls,null,(0,e.createElement)(o,{isGenerating:s,onGenerateImage:g,isTextSelected:p}))}});const c=window.wp.hooks;(0,c.addFilter)("editor.MediaUpload","kaigen/add-ai-tab",(t=>r=>{const a=r.allowedTypes&&r.allowedTypes.includes("image")&&!r.multiple,o=wp.data.select("core/block-editor").getSelectedBlock(),i=o&&"core/image"===o.name,l=a&&i&&!(o&&o.attributes&&o.attributes.url);return(0,e.createElement)(t,{...r,render:t=>(0,e.createElement)(e.Fragment,null,r.render(t),(0,e.createElement)(n,{onSelect:r.onSelect,shouldDisplay:l}))})})),(0,c.addFilter)("editor.BlockEdit","kaigen/add-regenerate-button",(r=>n=>{if("core/image"!==n.name)return(0,e.createElement)(r,{...n});const[l,c]=(0,t.useState)(!1),[s,d]=(0,t.useState)(null),[m,u]=(0,t.useState)(!1);return(0,t.useEffect)((()=>{(async()=>{try{const e=window.kaiGen?.provider,t=window.kaiGen?.supportsImageToImage||!1;if(!e)return void console.error("No provider configured in localized data");u(t)}catch(e){console.error("Failed to initialize provider:",e)}})()}),[]),(0,e.createElement)(e.Fragment,null,(0,e.createElement)(r,{...n}),(0,e.createElement)(i.BlockControls,null,(0,e.createElement)(o,{isRegenerating:l,onRegenerateImage:async e=>{d(null);const t=e||n.attributes.alt||"no alt text or prompt, please just enhance",r=window.kaiGen?.provider;if(!r)return console.error("No provider configured"),void wp.data.dispatch("core/notices").createErrorNotice("No AI provider configured. Please check your plugin settings.",{type:"snackbar"});c(!0);try{const e=n.attributes.url,r={};m&&e?r.sourceImageUrl=e:m&&!e&&(console.warn("Image-to-image requested but no source image URL available"),wp.data.dispatch("core/notices").createWarningNotice("Image-to-image generation requires a source image. Please ensure the image is properly loaded.",{type:"snackbar"}));const o=await new Promise(((e,n)=>{a(t,(t=>{t.error?n(new Error(t.error)):e(t)}),r)}));o.id&&"number"==typeof o.id&&o.id>0?n.setAttributes({url:o.url,id:o.id}):n.setAttributes({url:o.url,id:void 0}),wp.data.dispatch("core/notices").createSuccessNotice("Image regenerated successfully!",{type:"snackbar"})}catch(e){console.error("Image regeneration failed:",e);let t=e.message||"Unknown error",r="";t.includes("organization verification")?r=" Please verify your organization in the OpenAI dashboard.":t.includes("parameter")?t="API configuration error. Please contact the plugin developer.":t.includes("content policy")&&(r=" Try a different prompt."),wp.data.dispatch("core/notices").createErrorNotice("Failed to regenerate image: "+t+r,{type:"snackbar"})}finally{c(!1)}},isImageBlock:!0,supportsImageToImage:m})))}))})();1 (()=>{"use strict";var e={n:t=>{var r=t&&t.__esModule?()=>t.default:()=>t;return e.d(r,{a:r}),r},d:(t,r)=>{for(var a in r)e.o(r,a)&&!e.o(t,a)&&Object.defineProperty(t,a,{enumerable:!0,get:r[a]})}};e.g=function(){if("object"==typeof globalThis)return globalThis;try{return this||new Function("return this")()}catch(e){if("object"==typeof window)return window}}(),e.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),(()=>{var t;e.g.importScripts&&(t=e.g.location+"");var r=e.g.document;if(!t&&r&&(r.currentScript&&(t=r.currentScript.src),!t)){var a=r.getElementsByTagName("script");if(a.length)for(var n=a.length-1;n>-1&&(!t||!/^http(s?):/.test(t));)t=a[n--].src}if(!t)throw new Error("Automatic publicPath is not supported in this browser");t=t.replace(/#.*$/,"").replace(/\?.*$/,"").replace(/\/[^\/]+$/,"/"),e.p=t})();const t=window.React,r=window.wp.element,a=window.wp.components,n=e.p+"images/KaiGen-logo-128x128.e1e4be15.png",o=async(e,t,r={})=>{try{const a=wp.data.select("core/editor")?.getEditorSettings()?.kaigen_provider;if(!a)throw new Error("No provider configured. Please check your plugin settings.");const n={prompt:e,provider:a};r.sourceImageUrl&&(n.source_image_url=r.sourceImageUrl),r.additionalImageUrls&&Array.isArray(r.additionalImageUrls)&&(n.additional_image_urls=r.additionalImageUrls),r.maskUrl&&(n.mask_url=r.maskUrl),r.moderation&&["auto","low"].includes(r.moderation)&&(n.moderation=r.moderation),r.style&&["natural","vivid"].includes(r.style)&&(n.style=r.style);const o=await wp.apiFetch({path:"/kaigen/v1/generate-image",method:"POST",data:n});if(o.code&&o.message){if("content_moderation"===o.code)throw new Error(o.message);if("replicate_error"===o.code)throw new Error("Image generation failed: "+o.message);throw new Error(o.message)}if(!o||!o.url)throw new Error("Invalid response from server: "+JSON.stringify(o));o.id&&"number"==typeof o.id&&o.id>0?t({url:o.url,alt:e,id:o.id,caption:""}):t({url:o.url,alt:e,caption:""})}catch(e){console.error("Image generation failed:",e),e.message&&console.error("Error message:",e.message),e.stack&&console.error("Error stack:",e.stack),t({error:e.message||"An unknown error occurred while generating the image"})}},i=({onSelect:e,shouldDisplay:i})=>{const[l,c]=(0,r.useState)(!1),[s,d]=(0,r.useState)(""),[g,m]=(0,r.useState)(!1),[u,p]=(0,r.useState)(null),[w,h]=(0,r.useState)([]),[b,f]=(0,r.useState)(null),y=window.kaiGen?.supportsImageToImage||!1;return(0,r.useEffect)((()=>{l&&y&&(async()=>{try{const e=await wp.apiFetch({path:"/kaigen/v1/reference-images",method:"GET"});return Array.isArray(e)?e:[]}catch(e){return console.error("Failed to fetch reference images:",e),[]}})().then(h)}),[l]),i?(0,t.createElement)(t.Fragment,null,(0,t.createElement)(a.Button,{onClick:()=>c(!0),className:"kaigen-placeholder-button",style:{order:99,padding:0,background:"transparent",border:"none",boxShadow:"none",minWidth:"auto",display:"flex",alignItems:"center",justifyContent:"center"},"aria-label":"KaiGen",role:"button",title:"KaiGen"},(0,t.createElement)("img",{src:n,alt:"KaiGen",style:{height:"40px",width:"40px",objectFit:"contain",border:"none",background:"transparent"},"aria-label":"KaiGen logo",role:"button",title:"KaiGen logo"})),l&&(0,t.createElement)(a.Modal,{title:(0,t.createElement)("img",{src:n,alt:"KaiGen logo",style:{height:"80px",width:"auto",display:"block"}}),"aria-label":"KaiGen",onRequestClose:()=>c(!1)},u&&(0,t.createElement)("p",{style:{color:"red"}},u),(0,t.createElement)(a.TextareaControl,{label:"Prompt",value:s,onChange:d,rows:4}),y&&w.length>0&&(0,t.createElement)(t.Fragment,null,(0,t.createElement)("div",{style:{width:"250px",marginBottom:"8px"}},(0,t.createElement)("h4",{style:{margin:"0 0 4px 0"}},"Reference Images"),(0,t.createElement)("div",{style:{display:"flex",overflowX:"auto",overflowY:"hidden",gap:"4px",WebkitOverflowScrolling:"touch"}},w.map((e=>(0,t.createElement)("img",{key:e.id,src:e.url,alt:e.alt||"",onClick:()=>b&&b.id===e.id?f(null):f(e),style:{width:"80px",height:"80px",objectFit:"contain",cursor:"pointer",flex:"0 0 auto",border:b&&b.id===e.id?"4px solid #007cba":"4px solid transparent"}})))))),(0,t.createElement)(a.Button,{variant:"primary",onClick:()=>{if(!s.trim())return void p("Please enter a prompt for image generation.");m(!0),p(null);const t={};b&&(t.sourceImageUrl=b.url),o(s.trim(),(t=>{t.error?(p(t.error),m(!1)):(e(t),m(!1),c(!1))}),t)},disabled:g||!s.trim()},g?(0,t.createElement)(t.Fragment,null,(0,t.createElement)(a.Spinner,null)," ","KaiGen is generating..."):"KaiGen"))):null},l=({isGenerating:e,onGenerateImage:o,isRegenerating:i,onRegenerateImage:l,isImageBlock:c,isTextSelected:s,supportsImageToImage:d})=>{const[g,m]=(0,r.useState)(!1),[u,p]=(0,r.useState)(""),[w,h]=(0,r.useState)(null);return c&&d?(0,t.createElement)(t.Fragment,null,(0,t.createElement)(a.ToolbarGroup,null,(0,t.createElement)(a.ToolbarButton,{icon:i?(0,t.createElement)(a.Spinner,null):(0,t.createElement)("img",{src:n,alt:"KaiGen logo",style:{height:"20px",width:"20px"}}),label:i?"KaiGen is generating...":"KaiGen",onClick:()=>m(!0),disabled:i})),g&&(0,t.createElement)(a.Modal,{title:(0,t.createElement)("img",{src:n,alt:"KaiGen logo",style:{height:"80px",width:"auto",display:"block"}}),onRequestClose:()=>{m(!1),p(""),h(null)}},w&&(0,t.createElement)("p",{style:{color:"red"}},w),(0,t.createElement)(a.TextareaControl,{label:"Editing Instructions (optional)",value:u,onChange:p,rows:4}),(0,t.createElement)(a.Button,{variant:"primary",onClick:()=>{l(u.trim()),m(!1),p(""),h(null)},disabled:i},i?(0,t.createElement)(t.Fragment,null,(0,t.createElement)(a.Spinner,null),"Regenerating..."):"Regenerate Image"))):s?(0,t.createElement)(a.ToolbarGroup,null,(0,t.createElement)(a.ToolbarButton,{icon:e?(0,t.createElement)(a.Spinner,null):"format-image",label:e?"KaiGen is generating...":"KaiGen",onClick:o,disabled:e})):null},c=window.wp.blockEditor,s=window.wp.data;(0,window.wp.richText.registerFormatType)("kaigen/custom-format",{title:"AI Image Gen",tagName:"span",className:"kaigen-format",edit:({isActive:e,value:a,onChange:n})=>{const[i,d]=(0,r.useState)(!1),g=(0,s.useSelect)((e=>e("core/block-editor").getSelectedBlock()),[]),{replaceBlocks:m}=(0,s.useDispatch)("core/block-editor"),u=(0,r.useCallback)((()=>{if(g&&"core/paragraph"===g.name){const e=a.text.slice(a.start,a.end).trim();if(!e)return void wp.data.dispatch("core/notices").createErrorNotice("Please select some text to use as the image generation prompt.",{type:"snackbar"});const t=wp.data.select("core/editor")?.getEditorSettings()?.kaigen_provider;if(!t)return void wp.data.dispatch("core/notices").createErrorNotice("No AI provider configured. Please set one in the plugin settings.",{type:"snackbar"});const r=wp.blocks.createBlock("core/heading",{content:"Generating AI image...",level:2,style:{textAlign:"center"}});m(g.clientId,[r,g]),d(!0),o(e,(e=>{if(d(!1),e.error)console.error("Image generation failed:",e.error),wp.data.dispatch("core/notices").createErrorNotice("Failed to generate image: "+e.error,{type:"snackbar"}),m(r.clientId,[]);else{let t={url:e.url,alt:e.alt,caption:""};e.id&&"number"==typeof e.id&&e.id>0&&(t.id=e.id);const a=wp.blocks.createBlock("core/image",t);m(r.clientId,[a])}}))}}),[g,a.text,a.start,a.end,m]),p=""!==a.text.slice(a.start,a.end).trim();return(0,t.createElement)(c.BlockControls,null,(0,t.createElement)(l,{isGenerating:i,onGenerateImage:u,isTextSelected:p}))}});const d=window.wp.hooks;(0,d.addFilter)("editor.MediaUpload","kaigen/add-ai-tab",(e=>r=>{const a=r.allowedTypes&&r.allowedTypes.includes("image")&&!r.multiple,n=wp.data.select("core/block-editor").getSelectedBlock(),o=n&&"core/image"===n.name,l=a&&o&&!(n&&n.attributes&&n.attributes.url);return(0,t.createElement)(e,{...r,render:e=>(0,t.createElement)(t.Fragment,null,r.render(e),(0,t.createElement)(i,{onSelect:r.onSelect,shouldDisplay:l}))})}));const g=window.wp.apiFetch;var m=e.n(g);(0,d.addFilter)("editor.BlockEdit","kaigen/add-regenerate-button",(e=>n=>{if("core/image"!==n.name)return(0,t.createElement)(e,{...n});const i=n.attributes.id&&"number"==typeof n.attributes.id&&n.attributes.id>0,[s,d]=(0,r.useState)(!1),[g,u]=(0,r.useState)(null),[p,w]=(0,r.useState)(!1);return(0,r.useEffect)((()=>{(async()=>{try{const e=window.kaiGen?.provider,t=window.kaiGen?.supportsImageToImage||!1;if(!e)return void console.error("No provider configured in localized data");w(t)}catch(e){console.error("Failed to initialize provider:",e)}})()}),[]),(0,t.createElement)(t.Fragment,null,(0,t.createElement)(e,{...n}),(0,t.createElement)(c.BlockControls,null,(0,t.createElement)(l,{isRegenerating:s,onRegenerateImage:async e=>{u(null);const t=e||n.attributes.alt||"no alt text or prompt, please just enhance",r=window.kaiGen?.provider;if(!r)return console.error("No provider configured"),void wp.data.dispatch("core/notices").createErrorNotice("No AI provider configured. Please check your plugin settings.",{type:"snackbar"});d(!0);try{const e=n.attributes.url,r={};p&&e?r.sourceImageUrl=e:p&&!e&&(console.warn("Image-to-image requested but no source image URL available"),wp.data.dispatch("core/notices").createWarningNotice("Image-to-image generation requires a source image. Please ensure the image is properly loaded.",{type:"snackbar"}));const a=await new Promise(((e,a)=>{o(t,(t=>{t.error?a(new Error(t.error)):e(t)}),r)}));a.id&&"number"==typeof a.id&&a.id>0?n.setAttributes({url:a.url,id:a.id}):n.setAttributes({url:a.url,id:void 0}),wp.data.dispatch("core/notices").createSuccessNotice("Image regenerated successfully!",{type:"snackbar"})}catch(e){console.error("Image regeneration failed:",e);let t=e.message||"Unknown error",r="";t.includes("organization verification")?r=" Please verify your organization in the OpenAI dashboard.":t.includes("parameter")?t="API configuration error. Please contact the plugin developer.":t.includes("content policy")&&(r=" Try a different prompt."),wp.data.dispatch("core/notices").createErrorNotice("Failed to regenerate image: "+t+r,{type:"snackbar"})}finally{d(!1)}},isImageBlock:!0,supportsImageToImage:p})),i&&(0,t.createElement)(c.InspectorControls,null,(0,t.createElement)(a.PanelBody,{title:"KaiGen Settings",initialOpen:!1},(0,t.createElement)(a.CheckboxControl,{label:"Reference image",checked:n.attributes.kaigen_reference_image||!1,onChange:async e=>{n.setAttributes({kaigen_reference_image:e});try{await m()({path:`/wp/v2/media/${n.attributes.id}`,method:"POST",data:{meta:{kaigen_reference_image:e?1:0}}})}catch(e){console.error("Failed to update reference image meta:",e),wp.data.dispatch("core/notices").createErrorNotice("Failed to update reference image meta",{type:"snackbar"})}},help:"Add to the list of reference images."}))))})),(0,d.addFilter)("blocks.registerBlockType","kaigen/add-reference-image-attribute",((e,t)=>"core/image"!==t?e:{...e,attributes:{...e.attributes,kaigen_reference_image:{type:"boolean",default:!1}}}))})(); -
kaigen/tags/v0.2.1/inc/class-admin.php
r3315977 r3327070 60 60 add_action('admin_init', [$this, 'register_settings']); 61 61 add_action('admin_enqueue_scripts', [$this, 'enqueue_scripts']); 62 62 add_action('init', [$this, 'register_reference_image_meta']); 63 63 64 // Add settings link to plugin page 64 65 add_filter('plugin_action_links_' . plugin_basename(KAIGEN_PLUGIN_FILE), [$this, 'add_plugin_action_links']); … … 457 458 private function provider_supports_image_to_image($provider) { 458 459 if (empty($provider)) { 459 return false;460 return false; 460 461 } 461 462 462 463 $provider_manager = kaigen_provider_manager(); 463 464 return $provider_manager->provider_supports_image_to_image($provider); 465 } 466 467 /** 468 * Registers the reference image meta and edit form fields. 469 * 470 * @return void 471 */ 472 public function register_reference_image_meta() { 473 register_post_meta( 474 'attachment', 475 'kaigen_reference_image', 476 [ 477 'show_in_rest' => true, 478 'single' => true, 479 'type' => 'boolean', 480 'auth_callback' => function() { 481 return current_user_can( 'upload_files' ); 482 }, 483 ] 484 ); 485 486 add_filter('attachment_fields_to_edit', [$this, 'add_reference_field'], 10, 2); 487 add_filter('attachment_fields_to_save', [$this, 'save_reference_field'], 10, 2); 488 } 489 490 /** 491 * Adds the reference image checkbox to the attachment edit form. 492 * 493 * @param array \$form_fields Current form fields. 494 * @param WP_Post \$post Attachment post. 495 * @return array Modified form fields. 496 */ 497 public function add_reference_field($form_fields, $post) { 498 $value = (bool) get_post_meta($post->ID, 'kaigen_reference_image', true); 499 $form_fields['kaigen_reference_image'] = [ 500 'label' => __('Reference Image', 'kaigen'), 501 'input' => 'html', 502 'html' => '<input type="checkbox" name="attachments[' . esc_attr($post->ID) . '][kaigen_reference_image]" value="1"' . checked($value, true, false) . '/>', 503 ]; 504 return $form_fields; 505 } 506 507 /** 508 * Saves the reference image checkbox value. 509 * 510 * @param array \$post Attachment post data. 511 * @param array \$attachment Attachment form fields. 512 * @return array Modified post data. 513 */ 514 public function save_reference_field($post, $attachment) { 515 $value = isset($attachment['kaigen_reference_image']) ? 1 : 0; 516 update_post_meta($post['ID'], 'kaigen_reference_image', $value); 517 return $post; 464 518 } 465 519 } -
kaigen/tags/v0.2.1/inc/class-rest-api.php
r3315977 r3327070 74 74 'methods' => 'GET', 75 75 'callback' => [$this, 'get_image_to_image_providers'], 76 'permission_callback' => [$this, 'check_permission'], 77 ]); 78 79 // Register the reference images endpoint 80 register_rest_route(self::API_NAMESPACE, '/reference-images', [ 81 'methods' => 'GET', 82 'callback' => [$this, 'get_reference_images'], 76 83 'permission_callback' => [$this, 'check_permission'], 77 84 ]); … … 381 388 } 382 389 } 390 391 /** 392 * Retrieves all images marked as reference images. 393 * 394 * @return WP_REST_Response The response containing reference images. 395 */ 396 public function get_reference_images() { 397 $query = new WP_Query([ 398 'post_type' => 'attachment', 399 'post_status' => 'inherit', 400 'posts_per_page' => 100, 401 'meta_key' => 'kaigen_reference_image', 402 'meta_value' => 1, 403 ]); 404 405 $images = []; 406 foreach ($query->posts as $post) { 407 $images[] = [ 408 'id' => $post->ID, 409 'url' => wp_get_attachment_url($post->ID), 410 'alt' => get_post_meta($post->ID, '_wp_attachment_image_alt', true), 411 ]; 412 } 413 414 return new WP_REST_Response($images, 200); 415 } 383 416 } 384 417 -
kaigen/tags/v0.2.1/kaigen.php
r3315977 r3327070 5 5 * Requires at least: 6.1 6 6 * Requires PHP: 7.0 7 * Version: 0.2. 07 * Version: 0.2.1 8 8 * Author: Jacob Schweitzer 9 9 * License: GPL-2.0-or-later -
kaigen/trunk/build/index.asset.php
r3315977 r3327070 1 <?php return array('dependencies' => array('react', 'wp- block-editor', 'wp-components', 'wp-data', 'wp-element', 'wp-hooks', 'wp-rich-text'), 'version' => 'bcdcd0944f242d728c85');1 <?php return array('dependencies' => array('react', 'wp-api-fetch', 'wp-block-editor', 'wp-components', 'wp-data', 'wp-element', 'wp-hooks', 'wp-rich-text'), 'version' => 'f8292307639d2bbd9613'); -
kaigen/trunk/build/index.js
r3315977 r3327070 1 (()=>{"use strict"; const e=window.React,t=window.wp.element,r=window.wp.components,a=async(e,t,r={})=>{try{const a=wp.data.select("core/editor")?.getEditorSettings()?.kaigen_provider;if(!a)throw new Error("No provider configured. Please check your plugin settings.");const n={prompt:e,provider:a};r.sourceImageUrl&&(n.source_image_url=r.sourceImageUrl),r.additionalImageUrls&&Array.isArray(r.additionalImageUrls)&&(n.additional_image_urls=r.additionalImageUrls),r.maskUrl&&(n.mask_url=r.maskUrl),r.moderation&&["auto","low"].includes(r.moderation)&&(n.moderation=r.moderation),r.style&&["natural","vivid"].includes(r.style)&&(n.style=r.style);const o=await wp.apiFetch({path:"/kaigen/v1/generate-image",method:"POST",data:n});if(o.code&&o.message){if("content_moderation"===o.code)throw new Error(o.message);if("replicate_error"===o.code)throw new Error("Image generation failed: "+o.message);throw new Error(o.message)}if(!o||!o.url)throw new Error("Invalid response from server: "+JSON.stringify(o));o.id&&"number"==typeof o.id&&o.id>0?t({url:o.url,alt:e,id:o.id,caption:""}):t({url:o.url,alt:e,caption:""})}catch(e){console.error("Image generation failed:",e),e.message&&console.error("Error message:",e.message),e.stack&&console.error("Error stack:",e.stack),t({error:e.message||"An unknown error occurred while generating the image"})}},n=({onSelect:n,shouldDisplay:o})=>{const[i,l]=(0,t.useState)(!1),[c,s]=(0,t.useState)(""),[d,m]=(0,t.useState)(!1),[u,g]=(0,t.useState)(null);return o?(0,e.createElement)(e.Fragment,null,(0,e.createElement)("div",{className:"block-editor-media-placeholder__url-input-container"},(0,e.createElement)(r.Button,{variant:"secondary",onClick:()=>l(!0),className:"components-button is-next-40px-default-size is-secondary"},"KaiGen")),i&&(0,e.createElement)(r.Modal,{title:"KaiGen",onRequestClose:()=>l(!1)},u&&(0,e.createElement)("p",{style:{color:"red"}},u),(0,e.createElement)(r.TextareaControl,{label:"Prompt",value:c,onChange:s,rows:4}),(0,e.createElement)(r.Button,{variant:"primary",onClick:()=>{c.trim()?(m(!0),g(null),a(c.trim(),(e=>{e.error?(g(e.error),m(!1)):(n(e),m(!1),l(!1))}))):g("Please enter a prompt for image generation.")},disabled:d||!c.trim()},d?(0,e.createElement)(e.Fragment,null,(0,e.createElement)(r.Spinner,null)," ","KaiGen is generating..."):"KaiGen"))):null},o=({isGenerating:a,onGenerateImage:n,isRegenerating:o,onRegenerateImage:i,isImageBlock:l,isTextSelected:c,supportsImageToImage:s})=>{const[d,m]=(0,t.useState)(!1),[u,g]=(0,t.useState)(""),[p,w]=(0,t.useState)(null);return l&&s?(0,e.createElement)(e.Fragment,null,(0,e.createElement)(r.ToolbarGroup,null,(0,e.createElement)(r.ToolbarButton,{icon:o?(0,e.createElement)(r.Spinner,null):"update",label:o?"KaiGen is generating...":"KaiGen",onClick:()=>m(!0),disabled:o})),d&&(0,e.createElement)(r.Modal,{title:"KaiGen : Edit Image",onRequestClose:()=>{m(!1),g(""),w(null)}},p&&(0,e.createElement)("p",{style:{color:"red"}},p),(0,e.createElement)(r.TextareaControl,{label:"Editing Instructions (optional)",value:u,onChange:g,rows:4}),(0,e.createElement)(r.Button,{variant:"primary",onClick:()=>{i(u.trim()),m(!1),g(""),w(null)},disabled:o},o?(0,e.createElement)(e.Fragment,null,(0,e.createElement)(r.Spinner,null),"Regenerating..."):"Regenerate Image"))):c?(0,e.createElement)(r.ToolbarGroup,null,(0,e.createElement)(r.ToolbarButton,{icon:a?(0,e.createElement)(r.Spinner,null):"format-image",label:a?"KaiGen is generating...":"KaiGen",onClick:n,disabled:a})):null},i=window.wp.blockEditor,l=window.wp.data;(0,window.wp.richText.registerFormatType)("kaigen/custom-format",{title:"AI Image Gen",tagName:"span",className:"kaigen-format",edit:({isActive:r,value:n,onChange:c})=>{const[s,d]=(0,t.useState)(!1),m=(0,l.useSelect)((e=>e("core/block-editor").getSelectedBlock()),[]),{replaceBlocks:u}=(0,l.useDispatch)("core/block-editor"),g=(0,t.useCallback)((()=>{if(m&&"core/paragraph"===m.name){const e=n.text.slice(n.start,n.end).trim();if(!e)return void wp.data.dispatch("core/notices").createErrorNotice("Please select some text to use as the image generation prompt.",{type:"snackbar"});const t=wp.data.select("core/editor")?.getEditorSettings()?.kaigen_provider;if(!t)return void wp.data.dispatch("core/notices").createErrorNotice("No AI provider configured. Please set one in the plugin settings.",{type:"snackbar"});const r=wp.blocks.createBlock("core/heading",{content:"Generating AI image...",level:2,style:{textAlign:"center"}});u(m.clientId,[r,m]),d(!0),a(e,(e=>{if(d(!1),e.error)console.error("Image generation failed:",e.error),wp.data.dispatch("core/notices").createErrorNotice("Failed to generate image: "+e.error,{type:"snackbar"}),u(r.clientId,[]);else{let t={url:e.url,alt:e.alt,caption:""};e.id&&"number"==typeof e.id&&e.id>0&&(t.id=e.id);const a=wp.blocks.createBlock("core/image",t);u(r.clientId,[a])}}))}}),[m,n.text,n.start,n.end,u]),p=""!==n.text.slice(n.start,n.end).trim();return(0,e.createElement)(i.BlockControls,null,(0,e.createElement)(o,{isGenerating:s,onGenerateImage:g,isTextSelected:p}))}});const c=window.wp.hooks;(0,c.addFilter)("editor.MediaUpload","kaigen/add-ai-tab",(t=>r=>{const a=r.allowedTypes&&r.allowedTypes.includes("image")&&!r.multiple,o=wp.data.select("core/block-editor").getSelectedBlock(),i=o&&"core/image"===o.name,l=a&&i&&!(o&&o.attributes&&o.attributes.url);return(0,e.createElement)(t,{...r,render:t=>(0,e.createElement)(e.Fragment,null,r.render(t),(0,e.createElement)(n,{onSelect:r.onSelect,shouldDisplay:l}))})})),(0,c.addFilter)("editor.BlockEdit","kaigen/add-regenerate-button",(r=>n=>{if("core/image"!==n.name)return(0,e.createElement)(r,{...n});const[l,c]=(0,t.useState)(!1),[s,d]=(0,t.useState)(null),[m,u]=(0,t.useState)(!1);return(0,t.useEffect)((()=>{(async()=>{try{const e=window.kaiGen?.provider,t=window.kaiGen?.supportsImageToImage||!1;if(!e)return void console.error("No provider configured in localized data");u(t)}catch(e){console.error("Failed to initialize provider:",e)}})()}),[]),(0,e.createElement)(e.Fragment,null,(0,e.createElement)(r,{...n}),(0,e.createElement)(i.BlockControls,null,(0,e.createElement)(o,{isRegenerating:l,onRegenerateImage:async e=>{d(null);const t=e||n.attributes.alt||"no alt text or prompt, please just enhance",r=window.kaiGen?.provider;if(!r)return console.error("No provider configured"),void wp.data.dispatch("core/notices").createErrorNotice("No AI provider configured. Please check your plugin settings.",{type:"snackbar"});c(!0);try{const e=n.attributes.url,r={};m&&e?r.sourceImageUrl=e:m&&!e&&(console.warn("Image-to-image requested but no source image URL available"),wp.data.dispatch("core/notices").createWarningNotice("Image-to-image generation requires a source image. Please ensure the image is properly loaded.",{type:"snackbar"}));const o=await new Promise(((e,n)=>{a(t,(t=>{t.error?n(new Error(t.error)):e(t)}),r)}));o.id&&"number"==typeof o.id&&o.id>0?n.setAttributes({url:o.url,id:o.id}):n.setAttributes({url:o.url,id:void 0}),wp.data.dispatch("core/notices").createSuccessNotice("Image regenerated successfully!",{type:"snackbar"})}catch(e){console.error("Image regeneration failed:",e);let t=e.message||"Unknown error",r="";t.includes("organization verification")?r=" Please verify your organization in the OpenAI dashboard.":t.includes("parameter")?t="API configuration error. Please contact the plugin developer.":t.includes("content policy")&&(r=" Try a different prompt."),wp.data.dispatch("core/notices").createErrorNotice("Failed to regenerate image: "+t+r,{type:"snackbar"})}finally{c(!1)}},isImageBlock:!0,supportsImageToImage:m})))}))})();1 (()=>{"use strict";var e={n:t=>{var r=t&&t.__esModule?()=>t.default:()=>t;return e.d(r,{a:r}),r},d:(t,r)=>{for(var a in r)e.o(r,a)&&!e.o(t,a)&&Object.defineProperty(t,a,{enumerable:!0,get:r[a]})}};e.g=function(){if("object"==typeof globalThis)return globalThis;try{return this||new Function("return this")()}catch(e){if("object"==typeof window)return window}}(),e.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),(()=>{var t;e.g.importScripts&&(t=e.g.location+"");var r=e.g.document;if(!t&&r&&(r.currentScript&&(t=r.currentScript.src),!t)){var a=r.getElementsByTagName("script");if(a.length)for(var n=a.length-1;n>-1&&(!t||!/^http(s?):/.test(t));)t=a[n--].src}if(!t)throw new Error("Automatic publicPath is not supported in this browser");t=t.replace(/#.*$/,"").replace(/\?.*$/,"").replace(/\/[^\/]+$/,"/"),e.p=t})();const t=window.React,r=window.wp.element,a=window.wp.components,n=e.p+"images/KaiGen-logo-128x128.e1e4be15.png",o=async(e,t,r={})=>{try{const a=wp.data.select("core/editor")?.getEditorSettings()?.kaigen_provider;if(!a)throw new Error("No provider configured. Please check your plugin settings.");const n={prompt:e,provider:a};r.sourceImageUrl&&(n.source_image_url=r.sourceImageUrl),r.additionalImageUrls&&Array.isArray(r.additionalImageUrls)&&(n.additional_image_urls=r.additionalImageUrls),r.maskUrl&&(n.mask_url=r.maskUrl),r.moderation&&["auto","low"].includes(r.moderation)&&(n.moderation=r.moderation),r.style&&["natural","vivid"].includes(r.style)&&(n.style=r.style);const o=await wp.apiFetch({path:"/kaigen/v1/generate-image",method:"POST",data:n});if(o.code&&o.message){if("content_moderation"===o.code)throw new Error(o.message);if("replicate_error"===o.code)throw new Error("Image generation failed: "+o.message);throw new Error(o.message)}if(!o||!o.url)throw new Error("Invalid response from server: "+JSON.stringify(o));o.id&&"number"==typeof o.id&&o.id>0?t({url:o.url,alt:e,id:o.id,caption:""}):t({url:o.url,alt:e,caption:""})}catch(e){console.error("Image generation failed:",e),e.message&&console.error("Error message:",e.message),e.stack&&console.error("Error stack:",e.stack),t({error:e.message||"An unknown error occurred while generating the image"})}},i=({onSelect:e,shouldDisplay:i})=>{const[l,c]=(0,r.useState)(!1),[s,d]=(0,r.useState)(""),[g,m]=(0,r.useState)(!1),[u,p]=(0,r.useState)(null),[w,h]=(0,r.useState)([]),[b,f]=(0,r.useState)(null),y=window.kaiGen?.supportsImageToImage||!1;return(0,r.useEffect)((()=>{l&&y&&(async()=>{try{const e=await wp.apiFetch({path:"/kaigen/v1/reference-images",method:"GET"});return Array.isArray(e)?e:[]}catch(e){return console.error("Failed to fetch reference images:",e),[]}})().then(h)}),[l]),i?(0,t.createElement)(t.Fragment,null,(0,t.createElement)(a.Button,{onClick:()=>c(!0),className:"kaigen-placeholder-button",style:{order:99,padding:0,background:"transparent",border:"none",boxShadow:"none",minWidth:"auto",display:"flex",alignItems:"center",justifyContent:"center"},"aria-label":"KaiGen",role:"button",title:"KaiGen"},(0,t.createElement)("img",{src:n,alt:"KaiGen",style:{height:"40px",width:"40px",objectFit:"contain",border:"none",background:"transparent"},"aria-label":"KaiGen logo",role:"button",title:"KaiGen logo"})),l&&(0,t.createElement)(a.Modal,{title:(0,t.createElement)("img",{src:n,alt:"KaiGen logo",style:{height:"80px",width:"auto",display:"block"}}),"aria-label":"KaiGen",onRequestClose:()=>c(!1)},u&&(0,t.createElement)("p",{style:{color:"red"}},u),(0,t.createElement)(a.TextareaControl,{label:"Prompt",value:s,onChange:d,rows:4}),y&&w.length>0&&(0,t.createElement)(t.Fragment,null,(0,t.createElement)("div",{style:{width:"250px",marginBottom:"8px"}},(0,t.createElement)("h4",{style:{margin:"0 0 4px 0"}},"Reference Images"),(0,t.createElement)("div",{style:{display:"flex",overflowX:"auto",overflowY:"hidden",gap:"4px",WebkitOverflowScrolling:"touch"}},w.map((e=>(0,t.createElement)("img",{key:e.id,src:e.url,alt:e.alt||"",onClick:()=>b&&b.id===e.id?f(null):f(e),style:{width:"80px",height:"80px",objectFit:"contain",cursor:"pointer",flex:"0 0 auto",border:b&&b.id===e.id?"4px solid #007cba":"4px solid transparent"}})))))),(0,t.createElement)(a.Button,{variant:"primary",onClick:()=>{if(!s.trim())return void p("Please enter a prompt for image generation.");m(!0),p(null);const t={};b&&(t.sourceImageUrl=b.url),o(s.trim(),(t=>{t.error?(p(t.error),m(!1)):(e(t),m(!1),c(!1))}),t)},disabled:g||!s.trim()},g?(0,t.createElement)(t.Fragment,null,(0,t.createElement)(a.Spinner,null)," ","KaiGen is generating..."):"KaiGen"))):null},l=({isGenerating:e,onGenerateImage:o,isRegenerating:i,onRegenerateImage:l,isImageBlock:c,isTextSelected:s,supportsImageToImage:d})=>{const[g,m]=(0,r.useState)(!1),[u,p]=(0,r.useState)(""),[w,h]=(0,r.useState)(null);return c&&d?(0,t.createElement)(t.Fragment,null,(0,t.createElement)(a.ToolbarGroup,null,(0,t.createElement)(a.ToolbarButton,{icon:i?(0,t.createElement)(a.Spinner,null):(0,t.createElement)("img",{src:n,alt:"KaiGen logo",style:{height:"20px",width:"20px"}}),label:i?"KaiGen is generating...":"KaiGen",onClick:()=>m(!0),disabled:i})),g&&(0,t.createElement)(a.Modal,{title:(0,t.createElement)("img",{src:n,alt:"KaiGen logo",style:{height:"80px",width:"auto",display:"block"}}),onRequestClose:()=>{m(!1),p(""),h(null)}},w&&(0,t.createElement)("p",{style:{color:"red"}},w),(0,t.createElement)(a.TextareaControl,{label:"Editing Instructions (optional)",value:u,onChange:p,rows:4}),(0,t.createElement)(a.Button,{variant:"primary",onClick:()=>{l(u.trim()),m(!1),p(""),h(null)},disabled:i},i?(0,t.createElement)(t.Fragment,null,(0,t.createElement)(a.Spinner,null),"Regenerating..."):"Regenerate Image"))):s?(0,t.createElement)(a.ToolbarGroup,null,(0,t.createElement)(a.ToolbarButton,{icon:e?(0,t.createElement)(a.Spinner,null):"format-image",label:e?"KaiGen is generating...":"KaiGen",onClick:o,disabled:e})):null},c=window.wp.blockEditor,s=window.wp.data;(0,window.wp.richText.registerFormatType)("kaigen/custom-format",{title:"AI Image Gen",tagName:"span",className:"kaigen-format",edit:({isActive:e,value:a,onChange:n})=>{const[i,d]=(0,r.useState)(!1),g=(0,s.useSelect)((e=>e("core/block-editor").getSelectedBlock()),[]),{replaceBlocks:m}=(0,s.useDispatch)("core/block-editor"),u=(0,r.useCallback)((()=>{if(g&&"core/paragraph"===g.name){const e=a.text.slice(a.start,a.end).trim();if(!e)return void wp.data.dispatch("core/notices").createErrorNotice("Please select some text to use as the image generation prompt.",{type:"snackbar"});const t=wp.data.select("core/editor")?.getEditorSettings()?.kaigen_provider;if(!t)return void wp.data.dispatch("core/notices").createErrorNotice("No AI provider configured. Please set one in the plugin settings.",{type:"snackbar"});const r=wp.blocks.createBlock("core/heading",{content:"Generating AI image...",level:2,style:{textAlign:"center"}});m(g.clientId,[r,g]),d(!0),o(e,(e=>{if(d(!1),e.error)console.error("Image generation failed:",e.error),wp.data.dispatch("core/notices").createErrorNotice("Failed to generate image: "+e.error,{type:"snackbar"}),m(r.clientId,[]);else{let t={url:e.url,alt:e.alt,caption:""};e.id&&"number"==typeof e.id&&e.id>0&&(t.id=e.id);const a=wp.blocks.createBlock("core/image",t);m(r.clientId,[a])}}))}}),[g,a.text,a.start,a.end,m]),p=""!==a.text.slice(a.start,a.end).trim();return(0,t.createElement)(c.BlockControls,null,(0,t.createElement)(l,{isGenerating:i,onGenerateImage:u,isTextSelected:p}))}});const d=window.wp.hooks;(0,d.addFilter)("editor.MediaUpload","kaigen/add-ai-tab",(e=>r=>{const a=r.allowedTypes&&r.allowedTypes.includes("image")&&!r.multiple,n=wp.data.select("core/block-editor").getSelectedBlock(),o=n&&"core/image"===n.name,l=a&&o&&!(n&&n.attributes&&n.attributes.url);return(0,t.createElement)(e,{...r,render:e=>(0,t.createElement)(t.Fragment,null,r.render(e),(0,t.createElement)(i,{onSelect:r.onSelect,shouldDisplay:l}))})}));const g=window.wp.apiFetch;var m=e.n(g);(0,d.addFilter)("editor.BlockEdit","kaigen/add-regenerate-button",(e=>n=>{if("core/image"!==n.name)return(0,t.createElement)(e,{...n});const i=n.attributes.id&&"number"==typeof n.attributes.id&&n.attributes.id>0,[s,d]=(0,r.useState)(!1),[g,u]=(0,r.useState)(null),[p,w]=(0,r.useState)(!1);return(0,r.useEffect)((()=>{(async()=>{try{const e=window.kaiGen?.provider,t=window.kaiGen?.supportsImageToImage||!1;if(!e)return void console.error("No provider configured in localized data");w(t)}catch(e){console.error("Failed to initialize provider:",e)}})()}),[]),(0,t.createElement)(t.Fragment,null,(0,t.createElement)(e,{...n}),(0,t.createElement)(c.BlockControls,null,(0,t.createElement)(l,{isRegenerating:s,onRegenerateImage:async e=>{u(null);const t=e||n.attributes.alt||"no alt text or prompt, please just enhance",r=window.kaiGen?.provider;if(!r)return console.error("No provider configured"),void wp.data.dispatch("core/notices").createErrorNotice("No AI provider configured. Please check your plugin settings.",{type:"snackbar"});d(!0);try{const e=n.attributes.url,r={};p&&e?r.sourceImageUrl=e:p&&!e&&(console.warn("Image-to-image requested but no source image URL available"),wp.data.dispatch("core/notices").createWarningNotice("Image-to-image generation requires a source image. Please ensure the image is properly loaded.",{type:"snackbar"}));const a=await new Promise(((e,a)=>{o(t,(t=>{t.error?a(new Error(t.error)):e(t)}),r)}));a.id&&"number"==typeof a.id&&a.id>0?n.setAttributes({url:a.url,id:a.id}):n.setAttributes({url:a.url,id:void 0}),wp.data.dispatch("core/notices").createSuccessNotice("Image regenerated successfully!",{type:"snackbar"})}catch(e){console.error("Image regeneration failed:",e);let t=e.message||"Unknown error",r="";t.includes("organization verification")?r=" Please verify your organization in the OpenAI dashboard.":t.includes("parameter")?t="API configuration error. Please contact the plugin developer.":t.includes("content policy")&&(r=" Try a different prompt."),wp.data.dispatch("core/notices").createErrorNotice("Failed to regenerate image: "+t+r,{type:"snackbar"})}finally{d(!1)}},isImageBlock:!0,supportsImageToImage:p})),i&&(0,t.createElement)(c.InspectorControls,null,(0,t.createElement)(a.PanelBody,{title:"KaiGen Settings",initialOpen:!1},(0,t.createElement)(a.CheckboxControl,{label:"Reference image",checked:n.attributes.kaigen_reference_image||!1,onChange:async e=>{n.setAttributes({kaigen_reference_image:e});try{await m()({path:`/wp/v2/media/${n.attributes.id}`,method:"POST",data:{meta:{kaigen_reference_image:e?1:0}}})}catch(e){console.error("Failed to update reference image meta:",e),wp.data.dispatch("core/notices").createErrorNotice("Failed to update reference image meta",{type:"snackbar"})}},help:"Add to the list of reference images."}))))})),(0,d.addFilter)("blocks.registerBlockType","kaigen/add-reference-image-attribute",((e,t)=>"core/image"!==t?e:{...e,attributes:{...e.attributes,kaigen_reference_image:{type:"boolean",default:!1}}}))})(); -
kaigen/trunk/inc/class-admin.php
r3315977 r3327070 60 60 add_action('admin_init', [$this, 'register_settings']); 61 61 add_action('admin_enqueue_scripts', [$this, 'enqueue_scripts']); 62 62 add_action('init', [$this, 'register_reference_image_meta']); 63 63 64 // Add settings link to plugin page 64 65 add_filter('plugin_action_links_' . plugin_basename(KAIGEN_PLUGIN_FILE), [$this, 'add_plugin_action_links']); … … 457 458 private function provider_supports_image_to_image($provider) { 458 459 if (empty($provider)) { 459 return false;460 return false; 460 461 } 461 462 462 463 $provider_manager = kaigen_provider_manager(); 463 464 return $provider_manager->provider_supports_image_to_image($provider); 465 } 466 467 /** 468 * Registers the reference image meta and edit form fields. 469 * 470 * @return void 471 */ 472 public function register_reference_image_meta() { 473 register_post_meta( 474 'attachment', 475 'kaigen_reference_image', 476 [ 477 'show_in_rest' => true, 478 'single' => true, 479 'type' => 'boolean', 480 'auth_callback' => function() { 481 return current_user_can( 'upload_files' ); 482 }, 483 ] 484 ); 485 486 add_filter('attachment_fields_to_edit', [$this, 'add_reference_field'], 10, 2); 487 add_filter('attachment_fields_to_save', [$this, 'save_reference_field'], 10, 2); 488 } 489 490 /** 491 * Adds the reference image checkbox to the attachment edit form. 492 * 493 * @param array \$form_fields Current form fields. 494 * @param WP_Post \$post Attachment post. 495 * @return array Modified form fields. 496 */ 497 public function add_reference_field($form_fields, $post) { 498 $value = (bool) get_post_meta($post->ID, 'kaigen_reference_image', true); 499 $form_fields['kaigen_reference_image'] = [ 500 'label' => __('Reference Image', 'kaigen'), 501 'input' => 'html', 502 'html' => '<input type="checkbox" name="attachments[' . esc_attr($post->ID) . '][kaigen_reference_image]" value="1"' . checked($value, true, false) . '/>', 503 ]; 504 return $form_fields; 505 } 506 507 /** 508 * Saves the reference image checkbox value. 509 * 510 * @param array \$post Attachment post data. 511 * @param array \$attachment Attachment form fields. 512 * @return array Modified post data. 513 */ 514 public function save_reference_field($post, $attachment) { 515 $value = isset($attachment['kaigen_reference_image']) ? 1 : 0; 516 update_post_meta($post['ID'], 'kaigen_reference_image', $value); 517 return $post; 464 518 } 465 519 } -
kaigen/trunk/inc/class-rest-api.php
r3315977 r3327070 74 74 'methods' => 'GET', 75 75 'callback' => [$this, 'get_image_to_image_providers'], 76 'permission_callback' => [$this, 'check_permission'], 77 ]); 78 79 // Register the reference images endpoint 80 register_rest_route(self::API_NAMESPACE, '/reference-images', [ 81 'methods' => 'GET', 82 'callback' => [$this, 'get_reference_images'], 76 83 'permission_callback' => [$this, 'check_permission'], 77 84 ]); … … 381 388 } 382 389 } 390 391 /** 392 * Retrieves all images marked as reference images. 393 * 394 * @return WP_REST_Response The response containing reference images. 395 */ 396 public function get_reference_images() { 397 $query = new WP_Query([ 398 'post_type' => 'attachment', 399 'post_status' => 'inherit', 400 'posts_per_page' => 100, 401 'meta_key' => 'kaigen_reference_image', 402 'meta_value' => 1, 403 ]); 404 405 $images = []; 406 foreach ($query->posts as $post) { 407 $images[] = [ 408 'id' => $post->ID, 409 'url' => wp_get_attachment_url($post->ID), 410 'alt' => get_post_meta($post->ID, '_wp_attachment_image_alt', true), 411 ]; 412 } 413 414 return new WP_REST_Response($images, 200); 415 } 383 416 } 384 417 -
kaigen/trunk/kaigen.php
r3315977 r3327070 5 5 * Requires at least: 6.1 6 6 * Requires PHP: 7.0 7 * Version: 0.2. 07 * Version: 0.2.1 8 8 * Author: Jacob Schweitzer 9 9 * License: GPL-2.0-or-later
Note: See TracChangeset
for help on using the changeset viewer.