-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from PatelUtkarsh/feature/dall-e-image-generation
Dall e image generation with featured image
- Loading branch information
Showing
17 changed files
with
32,876 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,4 @@ node_modules/ | |
*.tar.gz | ||
*.zip | ||
/vendor | ||
build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,35 @@ | ||
{ | ||
"name": "patelutkarsh/better-file-name", | ||
"license": "GPL-2.0-or-later", | ||
"description": "A WordPress plugin to rename the image based on the image content using openai.", | ||
"type": "wordpress-plugin", | ||
"autoload": { | ||
"psr-4": { | ||
"Better_File_Name_Ai\\": "src/" | ||
} | ||
}, | ||
"authors": [ | ||
{ | ||
"name": "Utkarsh Patel", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"config": { | ||
"allow-plugins": { | ||
"dealerdirect/phpcodesniffer-composer-installer": true | ||
} | ||
}, | ||
"require-dev": { | ||
"wp-coding-standards/wpcs": "^3.0", | ||
"phpcompatibility/phpcompatibility-wp": "*", | ||
"szepeviktor/phpstan-wordpress": "^1.3", | ||
"php-stubs/wp-cli-stubs": "^2.9" | ||
}, | ||
"scripts": { | ||
"lint": [ | ||
"phpcs --standard=.phpcs.xml.dist", | ||
"phpstan analyse" | ||
], | ||
"format": "phpcbf --standard=.phpcs.xml.dist" | ||
} | ||
"name": "patelutkarsh/better-file-name", | ||
"license": "GPL-2.0-or-later", | ||
"description": "A WordPress plugin to rename the image based on the image content using openai.", | ||
"type": "wordpress-plugin", | ||
"autoload": { | ||
"psr-4": { | ||
"Better_File_Name_Ai\\": "src/" | ||
} | ||
}, | ||
"authors": [ | ||
{ | ||
"name": "Utkarsh Patel", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"config": { | ||
"allow-plugins": { | ||
"dealerdirect/phpcodesniffer-composer-installer": true | ||
} | ||
}, | ||
"require-dev": { | ||
"wp-coding-standards/wpcs": "^3.0", | ||
"phpcompatibility/phpcompatibility-wp": "*", | ||
"szepeviktor/phpstan-wordpress": "^1.3", | ||
"php-stubs/wp-cli-stubs": "^2.9" | ||
}, | ||
"scripts": { | ||
"lint": [ | ||
"phpcs --standard=.phpcs.xml.dist", | ||
"phpstan analyse" | ||
], | ||
"format": "phpcbf --standard=.phpcs.xml.dist" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import { Button, Spinner, TextareaControl } from '@wordpress/components'; | ||
import { useDispatch, useSelect } from '@wordpress/data'; | ||
import { useState } from '@wordpress/element'; | ||
import { addFilter } from '@wordpress/hooks'; | ||
import { __ } from '@wordpress/i18n'; | ||
import apiFetch from '@wordpress/api-fetch'; | ||
|
||
const DalleIntegration = () => { | ||
const [ prompt, setPrompt ] = useState( '' ); | ||
const [ isLoading, setIsLoading ] = useState( false ); | ||
const [ errorMessage, setErrorMessage ] = useState( null ); | ||
const { postTitle, postContent } = useSelect( ( select ) => ( { | ||
postTitle: select( 'core/editor' ).getEditedPostAttribute( 'title' ), | ||
postContent: | ||
select( 'core/editor' ).getEditedPostAttribute( 'content' ), | ||
} ) ); | ||
|
||
const { editPost } = useDispatch( 'core/editor' ); | ||
|
||
const generateImage = async () => { | ||
setIsLoading( true ); | ||
setErrorMessage( null ); | ||
try { | ||
const data = await apiFetch( { | ||
path: '/better-file-name/v1/dalle-generate-image', | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
data: { | ||
prompt, | ||
postTitle, | ||
postContent, | ||
}, | ||
} ); | ||
|
||
if ( data?.attachment_id ) { | ||
editPost( { featured_media: data.attachment_id } ); | ||
} | ||
} catch ( error ) { | ||
if ( error?.error ) { | ||
setErrorMessage( error.error ); | ||
} | ||
} finally { | ||
setIsLoading( false ); | ||
} | ||
}; | ||
|
||
return ( | ||
<div style={ { marginBlockStart: '16px' } }> | ||
<TextareaControl | ||
label="Enter Additional prompt" | ||
value={ prompt } | ||
help={ __( | ||
'To generate a better image, you can enter additional prompt here in addition to title and content.', | ||
'better-file-name' | ||
) } | ||
onChange={ ( value ) => setPrompt( value ) } | ||
/> | ||
<Button isPrimary onClick={ generateImage } disabled={ isLoading }> | ||
{ isLoading ? ( | ||
<Spinner /> | ||
) : ( | ||
__( 'Generate Image', 'better-file-name' ) | ||
) } | ||
</Button> | ||
{ errorMessage && ( | ||
<div style={ { color: 'red', marginBlockStart: '10px' } }> | ||
{ errorMessage } | ||
</div> | ||
) } | ||
</div> | ||
); | ||
}; | ||
|
||
const DalleWrapper = ( FilteredComponent ) => { | ||
return ( props ) => { | ||
return ( | ||
<> | ||
<FilteredComponent | ||
{ ...props } | ||
hint="Override by featured video." | ||
/> | ||
<DalleIntegration /> | ||
</> | ||
); | ||
}; | ||
}; | ||
|
||
addFilter( | ||
'editor.PostFeaturedImage', | ||
'better-file-name/dalle-integration', | ||
DalleWrapper, | ||
10 | ||
); |
Oops, something went wrong.