Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ phpunit.xml
coverage
/docs
languages
strauss.phar

debug.log

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Default prompt for condensing content via the Content Resizing feature.
* Default prompt for condensing content via the Writing Tools feature.
*
* @package Classifai
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Default prompt for expanding content via the Content Resizing feature.
* Default prompt for expanding content via the Writing Tools feature.
*
* @package Classifai
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
/**
* Default prompt for fixing grammar via the Writing Tools feature.
*
* @package Classifai
*/

return 'Please correct any spelling errors and grammatical mistakes in the following text';
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
}

/**
* Class ContentResizing
* Class WritingTools
*/
class ContentResizing extends Feature {
class WritingTools extends Feature {
/**
* ID of the current feature.
*
Expand All @@ -35,7 +35,7 @@ class ContentResizing extends Feature {
* Constructor.
*/
public function __construct() {
$this->label = __( 'Content Resizing', 'classifai' );
$this->label = __( 'Writing Tools', 'classifai' );

// Contains all providers that are registered to the service.
$this->provider_instances = $this->get_provider_instances( LanguageProcessing::get_service_providers() );
Expand Down Expand Up @@ -111,7 +111,7 @@ public function register_endpoints() {
'type' => 'string',
'sanitize_callback' => 'sanitize_text_field',
'validate_callback' => 'rest_validate_request_arg',
'description' => esc_html__( 'The type of resize operation. "expand" or "condense".', 'classifai' ),
'description' => esc_html__( 'The type of resize operation. "expand", "condense", or "fix_grammar".', 'classifai' ),
),
),
)
Expand Down Expand Up @@ -142,7 +142,7 @@ public function resize_content_permissions_check( WP_REST_Request $request ) {

// Ensure the feature is enabled. Also runs a user check.
if ( ! $this->is_feature_enabled() ) {
return new WP_Error( 'not_enabled', esc_html__( 'Content resizing is not currently enabled.', 'classifai' ) );
return new WP_Error( 'not_enabled', esc_html__( 'Writing tools are not currently enabled.', 'classifai' ) );
}

return true;
Expand Down Expand Up @@ -184,18 +184,18 @@ public function enqueue_editor_assets() {
}

wp_enqueue_script(
'classifai-plugin-content-resizing-js',
CLASSIFAI_PLUGIN_URL . 'dist/classifai-plugin-content-resizing.js',
array_merge( get_asset_info( 'classifai-plugin-content-resizing', 'dependencies' ), array( 'lodash' ) ),
get_asset_info( 'classifai-plugin-content-resizing', 'version' ),
'classifai-plugin-writing-tools-js',
CLASSIFAI_PLUGIN_URL . 'dist/classifai-plugin-writing-tools.js',
array_merge( get_asset_info( 'classifai-plugin-writing-tools', 'dependencies' ), [ 'lodash' ] ),
get_asset_info( 'classifai-plugin-writing-tools', 'version' ),
true
);

wp_enqueue_style(
'classifai-plugin-content-resizing-css',
CLASSIFAI_PLUGIN_URL . 'dist/classifai-plugin-content-resizing.css',
array(),
get_asset_info( 'classifai-plugin-content-resizing', 'version' ),
'classifai-plugin-writing-tools-css',
CLASSIFAI_PLUGIN_URL . 'dist/classifai-plugin-writing-tools.css',
[],
get_asset_info( 'classifai-plugin-writing-tools', 'version' ),
'all'
);
}
Expand All @@ -206,7 +206,7 @@ public function enqueue_editor_assets() {
* @return string
*/
public function get_enable_description(): string {
return esc_html__( '"Condense this text" and "Expand this text" menu items will be added to the paragraph block\'s toolbar menu.', 'classifai' );
return esc_html__( '"Condense this text", "Expand this text", and "Fix grammar and spelling" menu items will be added to the paragraph block\'s toolbar menu.', 'classifai' );
}

/**
Expand Down Expand Up @@ -242,6 +242,20 @@ public function add_custom_settings_fields() {
'description' => esc_html__( 'Enter your custom prompt.', 'classifai' ),
)
);

add_settings_field(
'fix_grammar_text_prompt',
esc_html__( 'Fix grammar and spelling', 'classifai' ),
[ $this, 'render_prompt_repeater_field' ],
$this->get_option_name(),
$this->get_option_name() . '_section',
[
'label_for' => 'fix_grammar_text_prompt',
'placeholder' => esc_html__( 'Please correct any spelling errors and grammatical mistakes in the following text', 'classifai' ),
'default_value' => $settings['fix_grammar_text_prompt'],
'description' => esc_html__( 'Enter your custom prompt.', 'classifai' ),
]
);
}

/**
Expand All @@ -250,23 +264,30 @@ public function add_custom_settings_fields() {
* @return array
*/
public function get_feature_default_settings(): array {
return array(
'condense_text_prompt' => array(
array(
return [
'condense_text_prompt' => [
[
'title' => esc_html__( 'ClassifAI default', 'classifai' ),
'prompt' => $this->get_prompt( 'condense' ),
'original' => 1,
),
),
'expand_text_prompt' => array(
array(
],
],
'expand_text_prompt' => [
[
'title' => esc_html__( 'ClassifAI default', 'classifai' ),
'prompt' => $this->get_prompt( 'expand' ),
'original' => 1,
),
),
'provider' => ChatGPT::ID,
);
],
],
'fix_grammar_text_prompt' => [
[
'title' => esc_html__( 'ClassifAI default', 'classifai' ),
'prompt' => $this->get_prompt( 'fix_grammar' ),
'original' => 1,
],
],
'provider' => ChatGPT::ID,
];
}

/**
Expand Down Expand Up @@ -297,6 +318,15 @@ public function get_settings( $index = false ) {
}
}

if ( $settings && ! empty( $settings['fix_grammar_text_prompt'] ) ) {
foreach ( $settings['fix_grammar_text_prompt'] as $key => $prompt ) {
if ( 1 === intval( $prompt['original'] ) ) {
$settings['fix_grammar_text_prompt'][ $key ]['prompt'] = $this->get_prompt( 'fix_grammar' );
break;
}
}
}

return $settings;
}

Expand All @@ -309,8 +339,9 @@ public function get_settings( $index = false ) {
public function sanitize_default_feature_settings( array $new_settings ): array {
$settings = $this->get_settings();

$new_settings['condense_text_prompt'] = sanitize_prompts( 'condense_text_prompt', $new_settings );
$new_settings['expand_text_prompt'] = sanitize_prompts( 'expand_text_prompt', $new_settings );
$new_settings['condense_text_prompt'] = sanitize_prompts( 'condense_text_prompt', $new_settings );
$new_settings['expand_text_prompt'] = sanitize_prompts( 'expand_text_prompt', $new_settings );
$new_settings['fix_grammar_text_prompt'] = sanitize_prompts( 'fix_grammar_text_prompt', $new_settings );

return $new_settings;
}
Expand Down
2 changes: 1 addition & 1 deletion includes/Classifai/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ public function maybe_migrate_to_v3() {
if ( ! empty( $chatgpt_settings ) ) {
$features[] = \Classifai\Features\TitleGeneration::class;
$features[] = \Classifai\Features\ExcerptGeneration::class;
$features[] = \Classifai\Features\ContentResizing::class;
$features[] = \Classifai\Features\WritingTools::class;
}

if ( ! empty( $tts_settings ) ) {
Expand Down
27 changes: 15 additions & 12 deletions includes/Classifai/Providers/Azure/OpenAI.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace Classifai\Providers\Azure;

use Classifai\Features\ContentResizing;
use Classifai\Features\WritingTools;
use Classifai\Features\ExcerptGeneration;
use Classifai\Features\TitleGeneration;
use Classifai\Features\ContentGeneration;
Expand Down Expand Up @@ -142,7 +142,7 @@ public function render_provider_fields() {
);

switch ( $this->feature_instance::ID ) {
case ContentResizing::ID:
case WritingTools::ID:
case TitleGeneration::ID:
add_settings_field(
static::ID . '_number_of_suggestions',
Expand Down Expand Up @@ -184,7 +184,7 @@ public function get_default_provider_settings(): array {
* Default values for feature specific settings.
*/
switch ( $this->feature_instance::ID ) {
case ContentResizing::ID:
case WritingTools::ID:
case TitleGeneration::ID:
$common_settings['number_of_suggestions'] = 1;
break;
Expand Down Expand Up @@ -221,7 +221,7 @@ public function sanitize_settings( array $new_settings ): array {
$new_settings[ static::ID ]['deployment'] = sanitize_text_field( $new_settings[ static::ID ]['deployment'] ?? $settings[ static::ID ]['deployment'] );

switch ( $this->feature_instance::ID ) {
case ContentResizing::ID:
case WritingTools::ID:
case TitleGeneration::ID:
$new_settings[ static::ID ]['number_of_suggestions'] = sanitize_number_of_responses_field( 'number_of_suggestions', $new_settings[ static::ID ], $settings[ static::ID ] );
break;
Expand All @@ -246,7 +246,7 @@ protected function prep_api_url( ?\Classifai\Features\Feature $feature = null ):
}

if (
( $feature instanceof ContentResizing ||
( $feature instanceof WritingTools ||
$feature instanceof ExcerptGeneration ||
$feature instanceof TitleGeneration ||
$feature instanceof KeyTakeaways ||
Expand Down Expand Up @@ -584,7 +584,7 @@ public function resize_content( int $post_id, array $args = array() ) {
return new WP_Error( 'post_id_required', esc_html__( 'Post ID is required to resize content.', 'classifai' ) );
}

$feature = new ContentResizing();
$feature = new WritingTools();
$settings = $feature->get_settings();

$args = wp_parse_args(
Expand All @@ -596,6 +596,8 @@ public function resize_content( int $post_id, array $args = array() ) {

if ( 'shrink' === $args['resize_type'] ) {
$prompt = esc_textarea( get_default_prompt( $settings['condense_text_prompt'] ) ?? $feature->get_prompt( 'condense' ) );
} elseif ( 'fix_grammar' === $args['resize_type'] ) {
$prompt = esc_textarea( get_default_prompt( $settings['fix_grammar_text_prompt'] ) ?? $feature->get_prompt( 'fix_grammar' ) );
} else {
$prompt = esc_textarea( get_default_prompt( $settings['expand_text_prompt'] ) ?? $feature->get_prompt( 'expand' ) );
}
Expand Down Expand Up @@ -657,7 +659,7 @@ public function resize_content( int $post_id, array $args = array() ) {
);
$response = $this->get_result( $response );

set_transient( 'classifai_azure_openai_content_resizing_latest_response', $response, DAY_IN_SECONDS * 30 );
set_transient( 'classifai_azure_openai_writing_tools_latest_response', $response, DAY_IN_SECONDS * 30 );

if ( is_wp_error( $response ) ) {
return $response;
Expand Down Expand Up @@ -1080,11 +1082,12 @@ public function get_debug_information(): array {
$debug_info[ __( 'Excerpt length', 'classifai' ) ] = $settings['length'] ?? 55;
$debug_info[ __( 'Generate excerpt prompt', 'classifai' ) ] = wp_json_encode( $settings['generate_excerpt_prompt'] ?? array() );
$debug_info[ __( 'Latest response', 'classifai' ) ] = $this->get_formatted_latest_response( get_transient( 'classifai_azure_openai_excerpt_generation_latest_response' ) );
} elseif ( $this->feature_instance instanceof ContentResizing ) {
$debug_info[ __( 'No. of suggestions', 'classifai' ) ] = $provider_settings['number_of_suggestions'] ?? 1;
$debug_info[ __( 'Expand text prompt', 'classifai' ) ] = wp_json_encode( $settings['expand_text_prompt'] ?? array() );
$debug_info[ __( 'Condense text prompt', 'classifai' ) ] = wp_json_encode( $settings['condense_text_prompt'] ?? array() );
$debug_info[ __( 'Latest response', 'classifai' ) ] = $this->get_formatted_latest_response( get_transient( 'classifai_azure_openai_content_resizing_latest_response' ) );
} elseif ( $this->feature_instance instanceof WritingTools ) {
$debug_info[ __( 'No. of suggestions', 'classifai' ) ] = $provider_settings['number_of_suggestions'] ?? 1;
$debug_info[ __( 'Expand text prompt', 'classifai' ) ] = wp_json_encode( $settings['expand_text_prompt'] ?? [] );
$debug_info[ __( 'Condense text prompt', 'classifai' ) ] = wp_json_encode( $settings['condense_text_prompt'] ?? [] );
$debug_info[ __( 'Fix grammar and spelling', 'classifai' ) ] = wp_json_encode( $settings['fix_grammar_text_prompt'] ?? [] );
$debug_info[ __( 'Latest response', 'classifai' ) ] = $this->get_formatted_latest_response( get_transient( 'classifai_azure_openai_writing_tools_latest_response' ) );
}

return apply_filters(
Expand Down
6 changes: 4 additions & 2 deletions includes/Classifai/Providers/Browser/ChromeAI.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace Classifai\Providers\Browser;

use Classifai\Features\ContentResizing;
use Classifai\Features\WritingTools;
use Classifai\Features\ExcerptGeneration;
use Classifai\Features\TitleGeneration;
use Classifai\Providers\Provider;
Expand Down Expand Up @@ -283,11 +283,13 @@ public function resize_content( int $post_id, array $args = array() ) {
return new WP_Error( 'post_id_required', esc_html__( 'Post ID is required to resize content.', 'classifai' ) );
}

$feature = new ContentResizing();
$feature = new WritingTools();
$settings = $feature->get_settings();

if ( 'shrink' === $args['resize_type'] ) {
$prompt = esc_textarea( get_default_prompt( $settings['condense_text_prompt'] ) ?? $feature->get_prompt( 'condense' ) );
} elseif ( 'fix_grammar' === $args['resize_type'] ) {
$prompt = esc_textarea( get_default_prompt( $settings['fix_grammar_text_prompt'] ) ?? $feature->get_prompt( 'fix_grammar' ) );
} else {
$prompt = esc_textarea( get_default_prompt( $settings['expand_text_prompt'] ) ?? $feature->get_prompt( 'expand' ) );
}
Expand Down
19 changes: 11 additions & 8 deletions includes/Classifai/Providers/GoogleAI/GeminiAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace Classifai\Providers\GoogleAI;

use Classifai\Features\ContentResizing;
use Classifai\Features\WritingTools;
use Classifai\Features\ExcerptGeneration;
use Classifai\Features\TitleGeneration;
use Classifai\Providers\Provider;
Expand Down Expand Up @@ -439,7 +439,7 @@ public function resize_content( int $post_id, array $args = array() ) {
return new WP_Error( 'post_id_required', esc_html__( 'Post ID is required to resize content.', 'classifai' ) );
}

$feature = new ContentResizing();
$feature = new WritingTools();
$settings = $feature->get_settings();

$args = wp_parse_args(
Expand All @@ -453,6 +453,8 @@ public function resize_content( int $post_id, array $args = array() ) {

if ( 'shrink' === $args['resize_type'] ) {
$prompt = esc_textarea( get_default_prompt( $settings['condense_text_prompt'] ) ?? $feature->get_prompt( 'condense' ) );
} elseif ( 'fix_grammar' === $args['resize_type'] ) {
$prompt = esc_textarea( get_default_prompt( $settings['fix_grammar_text_prompt'] ) ?? $feature->get_prompt( 'fix_grammar' ) );
} else {
$prompt = esc_textarea( get_default_prompt( $settings['expand_text_prompt'] ) ?? $feature->get_prompt( 'expand' ) );
}
Expand Down Expand Up @@ -510,7 +512,7 @@ public function resize_content( int $post_id, array $args = array() ) {
)
);

set_transient( 'classifai_googleai_gemini_api_content_resizing_latest_response', $response, DAY_IN_SECONDS * 30 );
set_transient( 'classifai_googleai_gemini_api_writing_tools_latest_response', $response, DAY_IN_SECONDS * 30 );

if ( is_wp_error( $response ) ) {
return $response;
Expand Down Expand Up @@ -595,11 +597,12 @@ public function get_debug_information(): array {
$debug_info[ __( 'Excerpt length', 'classifai' ) ] = $settings['length'] ?? 55;
$debug_info[ __( 'Generate excerpt prompt', 'classifai' ) ] = wp_json_encode( $settings['generate_excerpt_prompt'] ?? array() );
$debug_info[ __( 'Latest response', 'classifai' ) ] = $this->get_formatted_latest_response( get_transient( 'classifai_googleai_gemini_api_excerpt_generation_latest_response' ) );
} elseif ( $this->feature_instance instanceof ContentResizing ) {
$debug_info[ __( 'No. of suggestions', 'classifai' ) ] = 1;
$debug_info[ __( 'Expand text prompt', 'classifai' ) ] = wp_json_encode( $settings['expand_text_prompt'] ?? array() );
$debug_info[ __( 'Condense text prompt', 'classifai' ) ] = wp_json_encode( $settings['condense_text_prompt'] ?? array() );
$debug_info[ __( 'Latest response', 'classifai' ) ] = $this->get_formatted_latest_response( get_transient( 'classifai_googleai_gemini_api_content_resizing_latest_response' ) );
} elseif ( $this->feature_instance instanceof WritingTools ) {
$debug_info[ __( 'No. of suggestions', 'classifai' ) ] = 1;
$debug_info[ __( 'Expand text prompt', 'classifai' ) ] = wp_json_encode( $settings['expand_text_prompt'] ?? [] );
$debug_info[ __( 'Condense text prompt', 'classifai' ) ] = wp_json_encode( $settings['condense_text_prompt'] ?? [] );
$debug_info[ __( 'Fix grammar and spelling', 'classifai' ) ] = wp_json_encode( $settings['fix_grammar_text_prompt'] ?? [] );
$debug_info[ __( 'Latest response', 'classifai' ) ] = $this->get_formatted_latest_response( get_transient( 'classifai_googleai_gemini_api_writing_tools_latest_response' ) );
}

return apply_filters(
Expand Down
Loading
Loading