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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
release
/vendor/*
/vendor-prefixed/*
phpunit.xml
.phpunit.result.cache
/dist
coverage
/docs
languages
strauss.phar

debug.log

Expand Down
49 changes: 40 additions & 9 deletions includes/Classifai/Features/ContentResizing.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 @@ -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 @@ -251,21 +265,28 @@ public function add_custom_settings_fields() {
*/
public function get_feature_default_settings(): array {
return [
'condense_text_prompt' => [
'condense_text_prompt' => [
[
'title' => esc_html__( 'ClassifAI default', 'classifai' ),
'prompt' => $this->get_prompt( 'condense' ),
'original' => 1,
],
],
'expand_text_prompt' => [
'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
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';
11 changes: 7 additions & 4 deletions includes/Classifai/Providers/Azure/OpenAI.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 @@ -1079,10 +1081,11 @@ public function get_debug_information(): array {
$debug_info[ __( 'Generate excerpt prompt', 'classifai' ) ] = wp_json_encode( $settings['generate_excerpt_prompt'] ?? [] );
$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'] ?? [] );
$debug_info[ __( 'Condense text prompt', 'classifai' ) ] = wp_json_encode( $settings['condense_text_prompt'] ?? [] );
$debug_info[ __( 'Latest response', 'classifai' ) ] = $this->get_formatted_latest_response( get_transient( 'classifai_azure_openai_content_resizing_latest_response' ) );
$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_content_resizing_latest_response' ) );
}

return apply_filters(
Expand Down
2 changes: 2 additions & 0 deletions includes/Classifai/Providers/Browser/ChromeAI.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,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
11 changes: 7 additions & 4 deletions includes/Classifai/Providers/GoogleAI/GeminiAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 @@ -596,10 +598,11 @@ public function get_debug_information(): array {
$debug_info[ __( 'Generate excerpt prompt', 'classifai' ) ] = wp_json_encode( $settings['generate_excerpt_prompt'] ?? [] );
$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'] ?? [] );
$debug_info[ __( 'Condense text prompt', 'classifai' ) ] = wp_json_encode( $settings['condense_text_prompt'] ?? [] );
$debug_info[ __( 'Latest response', 'classifai' ) ] = $this->get_formatted_latest_response( get_transient( 'classifai_googleai_gemini_api_content_resizing_latest_response' ) );
$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_content_resizing_latest_response' ) );
}

return apply_filters(
Expand Down
2 changes: 2 additions & 0 deletions includes/Classifai/Providers/Localhost/Ollama.php
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,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
11 changes: 7 additions & 4 deletions includes/Classifai/Providers/OpenAI/ChatGPT.php
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,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 @@ -1366,10 +1368,11 @@ public function get_debug_information(): array {
$debug_info[ __( 'Generate excerpt prompt', 'classifai' ) ] = wp_json_encode( $settings['generate_excerpt_prompt'] ?? [] );
$debug_info[ __( 'Latest response', 'classifai' ) ] = $this->get_formatted_latest_response( get_transient( 'classifai_openai_chatgpt_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'] ?? [] );
$debug_info[ __( 'Condense text prompt', 'classifai' ) ] = wp_json_encode( $settings['condense_text_prompt'] ?? [] );
$debug_info[ __( 'Latest response', 'classifai' ) ] = $this->get_formatted_latest_response( get_transient( 'classifai_openai_chatgpt_content_resizing_latest_response' ) );
$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_openai_chatgpt_content_resizing_latest_response' ) );
}

return apply_filters(
Expand Down
11 changes: 7 additions & 4 deletions includes/Classifai/Providers/XAI/Grok.php
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,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 @@ -849,10 +851,11 @@ public function get_debug_information(): array {
$debug_info[ __( 'Generate excerpt prompt', 'classifai' ) ] = wp_json_encode( $settings['generate_excerpt_prompt'] ?? [] );
$debug_info[ __( 'Latest response', 'classifai' ) ] = $this->get_formatted_latest_response( get_transient( 'classifai_xai_grok_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'] ?? [] );
$debug_info[ __( 'Condense text prompt', 'classifai' ) ] = wp_json_encode( $settings['condense_text_prompt'] ?? [] );
$debug_info[ __( 'Latest response', 'classifai' ) ] = $this->get_formatted_latest_response( get_transient( 'classifai_xai_grok_content_resizing_latest_response' ) );
$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_xai_grok_content_resizing_latest_response' ) );
}

return apply_filters(
Expand Down
31 changes: 26 additions & 5 deletions src/js/features/content-resizing/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,17 @@
'Expanded text suggestion',
'Expanded text suggestions',
textArray.length,
'Modal title after expand content resizing.',
'Modal title after expand writing tools.',
'classifai'
)
);
} else if ( 'fix_grammar' === resizingType ) {
setModalTitle(
_nx(
'Grammar and spelling correction suggestion',
'Grammar and spelling correction suggestions',
textArray.length,
'Modal title after fix grammar writing tools.',
'classifai'
)
);
Expand All @@ -172,7 +182,7 @@
'Condensed text suggestion',
'Condensed text suggestions',
textArray.length,
'Modal title after condense content resizing.',
'Modal title after condense writing tools.',
'classifai'
)
);
Expand All @@ -188,7 +198,7 @@
setIsModalOpen( true );
} )();
}
}, [ isResizing, selectedBlock ] );

Check warning on line 201 in src/js/features/content-resizing/index.js

View workflow job for this annotation

GitHub Actions / Run JavaScript code quality checks

React Hook useEffect has a missing dependency: 'getResizedContent'. Either include it or remove the dependency array

// Resets to default states.
function resetStates() {
Expand All @@ -202,7 +212,7 @@
/**
* Refreshes results.
*
* @param {string} resizingType Type of resizing. grow|shrink|null
* @param {string} resizingType Type of resizing. grow|shrink|fix_grammar|null
* @param {Block} selectedBlock The selected block.
*/
async function refreshResults( resizingType, selectedBlock ) {
Expand All @@ -216,7 +226,7 @@
}

/**
* Triggered when either `Grow content` or `Shrink content` is clicked from
* Triggered when `Expand this text`, `Condense this text`, or `Fix grammar and spelling` is clicked from
* the Block's "more options" menu.
*
* @return {void}
Expand Down Expand Up @@ -317,7 +327,7 @@
}
}

// We don't want to use the reszing feature when multiple blocks are selected.
// We don't want to use the resizing feature when multiple blocks are selected.
// Nor do we want to use it when a block's content resizing is under process.
if ( isMultiBlocksSelected || isResizing ) {
return null;
Expand Down Expand Up @@ -632,6 +642,17 @@
).setResizingType( 'shrink' );
},
},
{
title: __(
'Fix grammar and spelling',
'classifai'
),
onClick: () => {
dispatch(
resizeContentStore
).setResizingType( 'fix_grammar' );
},
},
] }
/>
</BlockControls>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import { STORE_NAME } from '../../data/store';
import { PromptRepeater } from './prompt-repeater';

/**
* Component for the Content Resizing feature settings.
* Component for the Writing Tools feature settings.
*
* This component is used within the FeatureSettings component to allow users to configure the Content Resizing feature.
* This component is used within the FeatureSettings component to allow users to configure the Writing Tools feature.
*
* @return {React.ReactElement} ContentResizingSettings component.
*/
Expand Down Expand Up @@ -54,6 +54,20 @@ export const ContentResizingSettings = () => {
} }
/>
</SettingsRow>
<SettingsRow
label={ __( 'Fix grammar and spelling', 'classifai' ) }
description={ __( 'Enter your custom prompt.', 'classifai' ) }
className="settings-fix-grammar-text-prompt"
>
<PromptRepeater
prompts={ featureSettings.fix_grammar_text_prompt }
setPrompts={ ( prompts ) => {
setFeatureSettings( {
fix_grammar_text_prompt: prompts,
} );
} }
/>
</SettingsRow>
</>
);
};
2 changes: 1 addition & 1 deletion tests/e2e/specs/admin/common-feature-fields.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const services: Record< string, Record< string, string > > = {
feature_classification: 'Classification',
feature_title_generation: 'Title Generation',
feature_excerpt_generation: 'Excerpt Generation',
feature_content_resizing: 'Content Resizing',
feature_content_resizing: 'Writing Tools',
feature_text_to_speech_generation: 'Text to Speech',
feature_audio_transcripts_generation: 'Audio Transcripts Generation',
},
Expand Down
Loading
Loading