Skip to content

Commit

Permalink
Refactoring, error handling, GPT4o
Browse files Browse the repository at this point in the history
why not 2.0? Well... 🥷🏽
  • Loading branch information
phalkmin committed May 18, 2024
1 parent 9d673c0 commit addaaa4
Show file tree
Hide file tree
Showing 5 changed files with 179 additions and 139 deletions.
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
name: Deploy to WordPress.org
on:
release:
types: [released]
push:
tags:
- "*"
jobs:
tag:
name: GATTAI
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- name: Build
run: |
npm install
npm run build
- name: HASSHIN
uses: 10up/action-wordpress-plugin-deploy@stable
with:
dry-run: true
env:
SVN_USERNAME: ${{ secrets.SVN_USERNAME }}
SVN_PASSWORD: ${{ secrets.SVN_PASSWORD }}
SLUG: ${{ secrets.SLUG }}
tag:
name: GATTAI
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- name: HASSHIN
run: |
npm install
npm run build
- name: WordPress Plugin Deploy
uses: 10up/action-wordpress-plugin-deploy@master
env:
SVN_PASSWORD: ${{ secrets.SVN_PASSWORD }}
SVN_USERNAME: ${{ secrets.SVN_USERNAME }}
SLUG: ${{ secrets.SLUG }}

52 changes: 35 additions & 17 deletions README.txt
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
=== WP-AutoInsight ===
Contributors: phalkmin
Tags: openai, geminiai, blog, post, generator
Tags: openai, geminiai, blog, post, generator, gpt4, gpt4o, gpt35, dalle3
Requires at least: 5.8
Tested up to: 6.5
Stable tag: 1.6
Stable tag: 1.9
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html

Create blog posts automatically using the OpenAI and Gemini APIs!

== Description ==

The Automated Blog Content Creator plugin allows you to automatically generate blog posts using the power of the OpenAI API. With this plugin, you can harness the capabilities of ChatGPT to create SEO-focused articles on your WordPress site. Simply provide a set of keywords or subjects, and the plugin will generate engaging content for you.
WP-AutoInsight revolutionizes blog content creation by using OpenAI's GPT models and the Gemini API. From crafting SEO-optimized articles to generating relevant images, this plugin automates content generation for your WordPress site!

Features:

- Seamless integration with the OpenAI API.
- Automatically generates blog posts based on specified keywords.
- Customizable character limit for generated posts.
- Ability to generate images related to the content.
- Integration with multiple OpenAI GPT models (GPT-3.5, GPT-4, and GPT-4o).
- Gemini API support for alternative content generation.
- Advanced settings to specify content generation models and customize token limits.
- Automatic and manual blog post generation with SEO-focused keywords.
- Dynamic image creation related to the content using OpenAI's powerful DALL-E model.
- Supports Gutenberg blocks for easy content formatting.
- Option for daily automatic post creation.
- AJAX-powered manual post creation.
- Scheduled post creation options: hourly, daily, or weekly, to keep your blog constantly updated.
- AJAX-powered interface for immediate manual post generation.
- Translatable strings for internationalization.

== Installation ==
Expand All @@ -34,23 +33,31 @@ Features:

= How do I get an OpenAI API key? =

To use the OpenAI API, you need to sign up for an API key on the OpenAI website. Visit the OpenAI website and follow the instructions to obtain an API key.
To use the OpenAI API, you need to sign up for an API key on the OpenAI website. Sign up/log in to OpenAI, go to https://platform.openai.com/api-keys, and click "Create new API key".

= How do I get an GeminiAI API key? =

To use the GeminiAI API, you'll need an API key. You can create one in Google AI Studio https://support.gemini.com/hc/en-us/articles/360031080191-How-do-I-create-an-API-key.

= How do I select between different GPT models and Gemini API? =

= How can I specify the subjects or keywords for generating blog posts? =
In the plugin settings, you can select your preferred AI model from GPT-3.5, GPT-4, and GPT-4o. Gemini API is also available as an alternative or supplementary option.

In the plugin settings, you can enter a list of subjects or keywords separated by line breaks. The generated blog posts will be focused on these subjects.
= How can I customize the generated content? =

The plugin settings allow you to specify keywords, select AI models, set the character limit, and choose content generation frequency. This customization ensures the generated posts align with your blog's theme and audience.

= Can I customize the maximum character limit for generated posts? =

Yes, you can specify the maximum character limit for the generated posts in the plugin settings. The default limit is 200 characters.

= Can the plugin automatically create blog posts? =
= Is it possible to schedule content generation? =

Yes, the plugin provides an option for daily automatic post creation. When enabled, it will generate and save new blog posts based on the specified keywords.
Yes, WP-AutoInsight offers flexible scheduling options. You can automate content creation on an hourly, daily, or weekly basis.

= How can I manually generate a blog post? =

You can manually generate a blog post by clicking the "Create post manually" button on the plugin settings page. This will trigger an AJAX request to generate a new post.
You can manually generate a blog post by clicking the "Create post manually" button on the plugin settings page. This utilizes AJAX to generate a new post without reloading the page..

== Screenshots ==

Expand All @@ -59,6 +66,17 @@ You can manually generate a blog post by clicking the "Create post manually" but

== Changelog ==

= 1.9 =
- Added:
- Added support for selecting GPT-3.5, GPT-4 and GPT-4o models for content generation;
- Users of the old GPT-3.5 won't be affected by the change as there is backwards compability;
- Enhanced plugin settings UI for better user experience and clearer model selection;

- Fixed:
- Code refinements for better modularity and maintainability.
- Comprehensive error handling and logging.
- Updated documentation to guide users through new features and configurations.

= 1.6 =
- Fixed
- Select2;
Expand Down
5 changes: 5 additions & 0 deletions admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ function abcc_openai_text_settings_page() {
);
?>
<div class="wrap">

<h1><?php echo esc_html__( 'OpenAI Blog Post Generator', 'automated-blog-content-creator' ); ?></h1>
<div id="poststuff">
<div id="post-body" class="metabox-holder columns-2">
Expand Down Expand Up @@ -297,6 +298,10 @@ class="regular-text"><small>for extra security, it's advised to include
<select id="prompt_select" name="prompt_select">
<option value="openai" <?php selected( $prompt_select, 'openai' ); ?>>
GPT3.5 - default option</option>
<option value="gpt-4" <?php selected( $prompt_select, 'gpt-4' ); ?>>
GPT4 - Better content, cost more</option>
<option value="gpt-4o" <?php selected( $prompt_select, 'gpt-4o' ); ?>>
GPT4o - Better content, cost less</option>
<option value="gemini" <?php selected( $prompt_select, 'gemini' ); ?>>
Gemini - images won't be created</option>
</select>
Expand Down
88 changes: 61 additions & 27 deletions auto-post.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* Plugin Name: WP-AutoInsight
* Description: Create blog posts automatically using the OpenAI and Gemini APIs!
* Version: 1.6
* Version: 1.9
* Author: Paulo H. Alkmin
* Author URI: https://phalkmin.me/
* Text Domain: automated-wordpress-content-creator
Expand All @@ -22,6 +22,34 @@
require plugin_dir_path( __FILE__ ) . 'admin.php';
require plugin_dir_path( __FILE__ ) . 'gpt.php';

/**
* Handle API request errors.
*
* @param mixed $response The API response.
* @param string $api The API name.
*/
function handle_api_request_error( $response, $api ) {
if ( ! ( $response instanceof WP_Error ) && ! empty( $response ) ) {
$response = new WP_Error( 'api_error', $response );
}

// Error logging.
$error_message = 'Unknown error occurred.';
if ( is_wp_error( $response ) ) {
$error_message = $response->get_error_message();
}

error_log( sprintf( '%s API Request Error: %s', $api, $error_message ) );

add_settings_error(
'openai-settings',
'api-request-error',
// Translators: %1$s is the API name, %2$s is the error message.
sprintf( esc_html__( 'Error in %1$s API request: %2$s', 'automated-blog-content-creator' ), $api, $error_message ),
'error'
);
}


/**
* The function `abcc_enqueue_scripts` is used to enqueue CSS and JavaScript files, including Select2
Expand All @@ -39,7 +67,7 @@ function abcc_enqueue_scripts() {
function abcc_check_api_key() {
$prompt_select = get_option( 'prompt_select' );

if ( 'openai' === $prompt_select ) {
if ( ( 'openai' === $prompt_select ) || ( 'gpt-4' === $prompt_select ) || ( 'gpt-4o' === $prompt_select ) ) {
if ( defined( 'OPENAI_API' ) ) {
$api_key = OPENAI_API;
} else {
Expand Down Expand Up @@ -214,44 +242,50 @@ function abcc_openai_generate_post( $api_key, $keywords, $prompt_select, $tone =
$cat_images,
$site_url
);
if ( 'openai' === $prompt_select ) {
$text = abcc_openai_generate_text( $api_key, $prompt, $char_limit );
if ( ( 'openai' === $prompt_select ) || ( 'gpt-4' === $prompt_select ) || ( 'gpt-4o' === $prompt_select ) ) {
$text = abcc_openai_generate_text( $api_key, $prompt, $char_limit, $prompt_select );
$images = abcc_openai_generate_images( $api_key, $prompt_images, 1 );
} elseif ( 'gemini' === $prompt_select ) {
$text = abcc_gemini_generate_text( $api_key, $prompt, $char_limit );
}

foreach ( $text as $key => $value ) {
if ( strpos( $value, '<h1>' ) !== false && strpos( $value, '</h1>' ) !== false ) {
$title = str_replace( array( '<h1>', '</h1>' ), '', $value );
unset( $text[ $key ] );
break;
if ( ! empty( $text ) && is_array( $text ) ) {
foreach ( $text as $key => $value ) {
if ( strpos( $value, '<h1>' ) !== false && strpos( $value, '</h1>' ) !== false ) {
$title = str_replace( array( '<h1>', '</h1>' ), '', $value );
unset( $text[ $key ] );
break;
}
}
}

$format_content = abcc_create_blocks( $text );
$post_content = abcc_gutenberg_blocks( $format_content );
$format_content = abcc_create_blocks( $text );
$post_content = abcc_gutenberg_blocks( $format_content );

$post_data = array(
'post_title' => $title,
'post_content' => wp_kses_post( $post_content ),
'post_status' => 'draft',
'post_author' => 1,
'post_type' => 'post',
'post_category' => get_option( 'openai_selected_categories', array() ),
$post_data = array(
'post_title' => $title,
'post_content' => wp_kses_post( $post_content ),
'post_status' => 'draft',
'post_author' => 1,
'post_type' => 'post',
'post_category' => get_option( 'openai_selected_categories', array() ),

);
);

$post_id = wp_insert_post( $post_data );
$post_id = wp_insert_post( $post_data );

if ( get_option( 'openai_email_notifications', false ) ) {
$admin_email = get_option( 'admin_email' );
$subject = esc_html__( 'New Automated Post Created', 'automated-blog-content-creator' );
$message = esc_html__( 'A new post has been created automatically using OpenAI.', 'automated-blog-content-creator' );
if ( is_wp_error( $post_id ) ) {
handle_api_request_error( $post_id, 'WordPress' ); // Use the error handler for WordPress errors.
}

if ( get_option( 'openai_email_notifications', false ) ) {
$admin_email = get_option( 'admin_email' );
$subject = esc_html__( 'New Automated Post Created', 'automated-blog-content-creator' );
$message = esc_html__( 'A new post has been created automatically using OpenAI.', 'automated-blog-content-creator' );

// Utilize a função `wp_mail` para enviar e-mails.
wp_mail( $admin_email, $subject, $message );
wp_mail( $admin_email, $subject, $message );
}
}

if ( ! empty( $images ) ) {
foreach ( $images as $image_url ) {
if ( ! function_exists( 'media_sideload_image' ) ) {
Expand Down
Loading

0 comments on commit addaaa4

Please sign in to comment.