Skip to content
52 changes: 45 additions & 7 deletions includes/Classifai/Features/ContentResizing.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ class ContentResizing extends Feature {
*/
public $expand_prompt = 'Increase the content length no more than 2 to 4 sentences.';

/**
* Prompt for fixing grammar and spelling.
*
* @var string
*/
public $fix_grammar_prompt = 'Please correct any spelling errors and grammatical mistakes in the following text';

/**
* Constructor.
*/
Expand Down Expand Up @@ -125,7 +132,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 @@ -220,7 +227,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 @@ -256,6 +263,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 @@ -265,21 +286,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->condense_prompt,
'original' => 1,
],
],
'expand_text_prompt' => [
'expand_text_prompt' => [
[
'title' => esc_html__( 'ClassifAI default', 'classifai' ),
'prompt' => $this->expand_prompt,
'original' => 1,
],
],
'provider' => ChatGPT::ID,
'fix_grammar_text_prompt' => [
[
'title' => esc_html__( 'ClassifAI default', 'classifai' ),
'prompt' => $this->fix_grammar_prompt,
'original' => 1,
],
],
'provider' => ChatGPT::ID,
];
}

Expand Down Expand Up @@ -311,6 +339,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->fix_grammar_prompt;
break;
}
}
}

return $settings;
}

Expand All @@ -323,8 +360,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
11 changes: 7 additions & 4 deletions includes/Classifai/Providers/Azure/OpenAI.php
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,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->condense_prompt );
} elseif ( 'fix_grammar' === $args['resize_type'] ) {
$prompt = esc_textarea( get_default_prompt( $settings['fix_grammar_text_prompt'] ) ?? $feature->fix_grammar_prompt );
} else {
$prompt = esc_textarea( get_default_prompt( $settings['expand_text_prompt'] ) ?? $feature->expand_prompt );
}
Expand Down Expand Up @@ -1103,10 +1105,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->condense_prompt );
} elseif ( 'fix_grammar' === $args['resize_type'] ) {
$prompt = esc_textarea( get_default_prompt( $settings['fix_grammar_text_prompt'] ) ?? $feature->fix_grammar_prompt );
} else {
$prompt = esc_textarea( get_default_prompt( $settings['expand_text_prompt'] ) ?? $feature->expand_prompt );
}
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 @@ -452,6 +452,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->condense_prompt );
} elseif ( 'fix_grammar' === $args['resize_type'] ) {
$prompt = esc_textarea( get_default_prompt( $settings['fix_grammar_text_prompt'] ) ?? $feature->fix_grammar_prompt );
} else {
$prompt = esc_textarea( get_default_prompt( $settings['expand_text_prompt'] ) ?? $feature->expand_prompt );
}
Expand Down Expand Up @@ -595,10 +597,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->condense_prompt );
} elseif ( 'fix_grammar' === $args['resize_type'] ) {
$prompt = esc_textarea( get_default_prompt( $settings['fix_grammar_text_prompt'] ) ?? $feature->fix_grammar_prompt );
} else {
$prompt = esc_textarea( get_default_prompt( $settings['expand_text_prompt'] ) ?? $feature->expand_prompt );
}
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->condense_prompt );
} elseif ( 'fix_grammar' === $args['resize_type'] ) {
$prompt = esc_textarea( get_default_prompt( $settings['fix_grammar_text_prompt'] ) ?? $feature->fix_grammar_prompt );
} else {
$prompt = esc_textarea( get_default_prompt( $settings['expand_text_prompt'] ) ?? $feature->expand_prompt );
}
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 @@ -691,6 +691,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->condense_prompt );
} elseif ( 'fix_grammar' === $args['resize_type'] ) {
$prompt = esc_textarea( get_default_prompt( $settings['fix_grammar_text_prompt'] ) ?? $feature->fix_grammar_prompt );
} else {
$prompt = esc_textarea( get_default_prompt( $settings['expand_text_prompt'] ) ?? $feature->expand_prompt );
}
Expand Down Expand Up @@ -854,10 +856,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
25 changes: 23 additions & 2 deletions src/js/features/content-resizing/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,16 @@
'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 content resizing.',
'classifai'
)
);
} else {
setModalTitle(
_nx(
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 / eslint

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 @@ -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 @@ -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>
</>
);
};