Skip to content

Commit

Permalink
Merge pull request #19 from PatelUtkarsh/feature/dall-e-image-generation
Browse files Browse the repository at this point in the history
Dall e image generation with featured image
  • Loading branch information
PatelUtkarsh authored Dec 24, 2023
2 parents 339d6ab + d6badbf commit 48fc250
Show file tree
Hide file tree
Showing 17 changed files with 32,876 additions and 47 deletions.
11 changes: 11 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@ jobs:
- name: Build
run: |
composer install --no-dev --no-interaction --no-progress --optimize-autoloader --ignore-platform-reqs
- name: Cache Node modules
uses: actions/cache@v3
with:
path: node_modules
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-
- name: npm build
run: |
npm install
npm run build
- name: Create Release Asset
id: deploy
run: |
Expand Down
14 changes: 13 additions & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,17 @@ jobs:
- name: Install dependencies
run: composer install --prefer-dist --no-progress

- name: Lint
- name: PHP Lint
run: composer lint

- name: Cache Node modules
uses: actions/cache@v3
with:
path: node_modules
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-
- name: JS lint
run: |
npm install
npm run lint:js
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ node_modules/
*.tar.gz
*.zip
/vendor
build
3 changes: 2 additions & 1 deletion .phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<exclude-pattern>/vendor/</exclude-pattern>
<exclude-pattern>/node_modules/</exclude-pattern>
<exclude-pattern>/tests/bootstrap.php</exclude-pattern>
<exclude-pattern>/build/</exclude-pattern>

<!-- How to scan -->
<!-- Usage instructions: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Usage -->
Expand All @@ -26,7 +27,7 @@
<!-- Rules: WordPress Coding Standards -->
<!-- https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards -->
<!-- https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/wiki/Customizable-sniff-properties -->
<config name="minimum_supported_wp_version" value="4.6"/>
<config name="minimum_supported_wp_version" value="6.0"/>
<rule ref="WordPress">
<!-- exclude -->
<exclude name="WordPress.Files.FileName.InvalidClassFileName" />
Expand Down
8 changes: 5 additions & 3 deletions better-file-name.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Author: Utkarsh
* Author URI: github.com/patelutkarsh
* Text Domain: better-file-name
* Version: 1.2.3
* Version: 1.3.0
* Requires PHP: 8.1
*
* @package Better_File_Name_Ai
Expand All @@ -15,6 +15,7 @@
declare( strict_types=1 );

use Better_File_Name_Ai\Admin;
use Better_File_Name_Ai\Dalle_Image_Generator;
use Better_File_Name_Ai\Settings;

if ( file_exists( __DIR__ . '/vendor/autoload.php' ) ) {
Expand All @@ -25,8 +26,9 @@
throw new \Exception( esc_html__( 'Could not find vendor/autoload.php, make sure you ran composer.', 'better-file-name' ) );
}

$better_file_name_settings = new Settings();
$better_file_name_admin = new Admin( $better_file_name_settings );
$better_file_name_settings = new Settings();
$better_file_name_admin = new Admin( $better_file_name_settings, plugins_url( 'build', __FILE__ ) );
$better_file_name_dalle_image_generator = new Dalle_Image_Generator( $better_file_name_settings );

if ( defined( 'WP_CLI' ) && WP_CLI ) {
\WP_CLI::add_command( 'better-file-name generate-alt-text', Better_File_Name_Ai\Generate_Alt_Text_Cli::class );
Expand Down
66 changes: 33 additions & 33 deletions composer.json
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"
}
}
95 changes: 95 additions & 0 deletions js/index.js
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
);
Loading

0 comments on commit 48fc250

Please sign in to comment.