Skip to content

Commit 41e5e33

Browse files
committed
Move number of suggestions field to provider (ChatGPT).
1 parent e9a71af commit 41e5e33

File tree

3 files changed

+77
-50
lines changed

3 files changed

+77
-50
lines changed

includes/Classifai/Features/ContentResizing.php

+2-20
Original file line numberDiff line numberDiff line change
@@ -236,22 +236,6 @@ public function get_enable_description(): string {
236236
public function add_custom_settings_fields() {
237237
$settings = $this->get_settings();
238238

239-
add_settings_field(
240-
'number_of_suggestions',
241-
esc_html__( 'Number of suggestions', 'classifai' ),
242-
[ $this, 'render_input' ],
243-
$this->get_option_name(),
244-
$this->get_option_name() . '_section',
245-
[
246-
'label_for' => 'number_of_suggestions',
247-
'input_type' => 'number',
248-
'min' => 1,
249-
'step' => 1,
250-
'default_value' => $settings['number_of_suggestions'],
251-
'description' => esc_html__( 'Number of suggestions that will be generated in one request.', 'classifai' ),
252-
]
253-
);
254-
255239
add_settings_field(
256240
'condense_text_prompt',
257241
esc_html__( 'Condense text prompt', 'classifai' ),
@@ -288,7 +272,6 @@ public function add_custom_settings_fields() {
288272
*/
289273
public function get_feature_default_settings(): array {
290274
return [
291-
'number_of_suggestions' => 1,
292275
'condense_text_prompt' => [
293276
[
294277
'title' => esc_html__( 'ClassifAI default', 'classifai' ),
@@ -316,9 +299,8 @@ public function get_feature_default_settings(): array {
316299
public function sanitize_default_feature_settings( array $new_settings ): array {
317300
$settings = $this->get_settings();
318301

319-
$new_settings['number_of_suggestions'] = sanitize_number_of_responses_field( 'number_of_suggestions', $new_settings, $settings );
320-
$new_settings['condense_text_prompt'] = sanitize_prompts( 'condense_text_prompt', $new_settings );
321-
$new_settings['expand_text_prompt'] = sanitize_prompts( 'expand_text_prompt', $new_settings );
302+
$new_settings['condense_text_prompt'] = sanitize_prompts( 'condense_text_prompt', $new_settings );
303+
$new_settings['expand_text_prompt'] = sanitize_prompts( 'expand_text_prompt', $new_settings );
322304

323305
return $new_settings;
324306
}

includes/Classifai/Providers/GoogleAI/GeminiAPI.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ public function resize_content( int $post_id, array $args = array() ) {
438438
$args = wp_parse_args(
439439
array_filter( $args ),
440440
[
441-
'num' => $settings['number_of_suggestions'] ?? 1,
441+
'num' => 1, // Gemini API only returns 1 variation as of now.
442442
]
443443
);
444444

@@ -581,16 +581,16 @@ public function get_debug_information(): array {
581581

582582
if ( $this->feature_instance instanceof TitleGeneration ) {
583583
$debug_info[ __( 'No. of titles', 'classifai' ) ] = 1;
584-
$debug_info[ __( 'Generate title prompt', 'classifai' ) ] = wp_json_encode( $provider_settings['generate_title_prompt'] ?? [] );
584+
$debug_info[ __( 'Generate title prompt', 'classifai' ) ] = wp_json_encode( $settings['generate_title_prompt'] ?? [] );
585585
$debug_info[ __( 'Latest response', 'classifai' ) ] = $this->get_formatted_latest_response( get_transient( 'classifai_googleai_gemini_api_title_generation_latest_response' ) );
586586
} elseif ( $this->feature_instance instanceof ExcerptGeneration ) {
587587
$debug_info[ __( 'Excerpt length', 'classifai' ) ] = $settings['length'] ?? 55;
588-
$debug_info[ __( 'Generate excerpt prompt', 'classifai' ) ] = wp_json_encode( $provider_settings['generate_excerpt_prompt'] ?? [] );
588+
$debug_info[ __( 'Generate excerpt prompt', 'classifai' ) ] = wp_json_encode( $settings['generate_excerpt_prompt'] ?? [] );
589589
$debug_info[ __( 'Latest response', 'classifai' ) ] = $this->get_formatted_latest_response( get_transient( 'classifai_googleai_gemini_api_excerpt_generation_latest_response' ) );
590590
} elseif ( $this->feature_instance instanceof ContentResizing ) {
591-
$debug_info[ __( 'No. of suggestions', 'classifai' ) ] = $provider_settings['number_of_suggestions'] ?? 1;
592-
$debug_info[ __( 'Expand text prompt', 'classifai' ) ] = wp_json_encode( $provider_settings['expand_text_prompt'] ?? [] );
593-
$debug_info[ __( 'Condense text prompt', 'classifai' ) ] = wp_json_encode( $provider_settings['condense_text_prompt'] ?? [] );
591+
$debug_info[ __( 'No. of suggestions', 'classifai' ) ] = 1;
592+
$debug_info[ __( 'Expand text prompt', 'classifai' ) ] = wp_json_encode( $settings['expand_text_prompt'] ?? [] );
593+
$debug_info[ __( 'Condense text prompt', 'classifai' ) ] = wp_json_encode( $settings['condense_text_prompt'] ?? [] );
594594
$debug_info[ __( 'Latest response', 'classifai' ) ] = $this->get_formatted_latest_response( get_transient( 'classifai_googleai_gemini_api_content_resizing_latest_response' ) );
595595
}
596596

includes/Classifai/Providers/OpenAI/ChatGPT.php

+69-24
Original file line numberDiff line numberDiff line change
@@ -85,23 +85,47 @@ public function render_provider_fields() {
8585
]
8686
);
8787

88-
add_settings_field(
89-
'number_of_titles',
90-
esc_html__( 'Number of titles', 'classifai' ),
91-
[ $this->feature_instance, 'render_input' ],
92-
$this->feature_instance->get_option_name(),
93-
$this->feature_instance->get_option_name() . '_section',
94-
[
95-
'option_index' => static::ID,
96-
'label_for' => 'number_of_titles',
97-
'input_type' => 'number',
98-
'min' => 1,
99-
'step' => 1,
100-
'default_value' => $settings['number_of_titles'],
101-
'class' => 'classifai-provider-field hidden provider-scope-' . static::ID, // Important to add this.
102-
'description' => esc_html__( 'Number of titles that will be generated in one request.', 'classifai' ),
103-
]
104-
);
88+
switch ( $this->feature_instance::ID ) {
89+
case TitleGeneration::ID:
90+
add_settings_field(
91+
'number_of_titles',
92+
esc_html__( 'Number of titles', 'classifai' ),
93+
[ $this->feature_instance, 'render_input' ],
94+
$this->feature_instance->get_option_name(),
95+
$this->feature_instance->get_option_name() . '_section',
96+
[
97+
'option_index' => static::ID,
98+
'label_for' => 'number_of_titles',
99+
'input_type' => 'number',
100+
'min' => 1,
101+
'step' => 1,
102+
'default_value' => $settings['number_of_titles'],
103+
'class' => 'classifai-provider-field hidden provider-scope-' . static::ID, // Important to add this.
104+
'description' => esc_html__( 'Number of titles that will be generated in one request.', 'classifai' ),
105+
]
106+
);
107+
break;
108+
109+
case ContentResizing::ID:
110+
add_settings_field(
111+
'number_of_suggestions',
112+
esc_html__( 'Number of suggestions', 'classifai' ),
113+
[ $this->feature_instance, 'render_input' ],
114+
$this->feature_instance->get_option_name(),
115+
$this->feature_instance->get_option_name() . '_section',
116+
[
117+
'option_index' => static::ID,
118+
'label_for' => 'number_of_suggestions',
119+
'input_type' => 'number',
120+
'min' => 1,
121+
'step' => 1,
122+
'default_value' => $settings['number_of_suggestions'],
123+
'class' => 'classifai-provider-field hidden provider-scope-' . static::ID, // Important to add this.
124+
'description' => esc_html__( 'Number of suggestions that will be generated in one request.', 'classifai' ),
125+
]
126+
);
127+
break;
128+
}
105129

106130
do_action( 'classifai_' . static::ID . '_render_provider_fields', $this );
107131
}
@@ -115,9 +139,21 @@ public function get_default_provider_settings(): array {
115139
$common_settings = [
116140
'api_key' => '',
117141
'authenticated' => false,
118-
'number_of_titles' => 1,
119142
];
120143

144+
/**
145+
* Default values for feature specific settings.
146+
*/
147+
switch ( $this->feature_instance::ID ) {
148+
case TitleGeneration::ID:
149+
$common_settings['number_of_titles'] = 1;
150+
break;
151+
152+
case ContentResizing::ID:
153+
$common_settings['number_of_suggestions'] = 1;
154+
break;
155+
}
156+
121157
return $common_settings;
122158
}
123159

@@ -133,7 +169,16 @@ public function sanitize_settings( array $new_settings ): array {
133169

134170
$new_settings[ static::ID ]['api_key'] = $api_key_settings[ static::ID ]['api_key'];
135171
$new_settings[ static::ID ]['authenticated'] = $api_key_settings[ static::ID ]['authenticated'];
136-
$new_settings[ static::ID ]['number_of_titles'] = sanitize_number_of_responses_field( 'number_of_titles', $new_settings[ static::ID ], $settings[ static::ID ] );
172+
173+
switch ( $this->feature_instance::ID ) {
174+
case TitleGeneration::ID:
175+
$new_settings[ static::ID ]['number_of_titles'] = sanitize_number_of_responses_field( 'number_of_titles', $new_settings[ static::ID ], $settings[ static::ID ] );
176+
break;
177+
178+
case ContentResizing::ID:
179+
$new_settings[ static::ID ]['number_of_suggestions'] = sanitize_number_of_responses_field( 'number_of_suggestions', $new_settings[ static::ID ], $settings[ static::ID ] );
180+
break;
181+
}
137182

138183
return $new_settings;
139184
}
@@ -412,7 +457,7 @@ public function resize_content( int $post_id, array $args = array() ) {
412457
$args = wp_parse_args(
413458
array_filter( $args ),
414459
[
415-
'num' => $settings['number_of_suggestions'] ?? 1,
460+
'num' => $settings[ static::ID ]['number_of_suggestions'] ?? 1,
416461
]
417462
);
418463

@@ -574,16 +619,16 @@ public function get_debug_information(): array {
574619

575620
if ( $this->feature_instance instanceof TitleGeneration ) {
576621
$debug_info[ __( 'No. of titles', 'classifai' ) ] = $provider_settings['number_of_titles'] ?? 1;
577-
$debug_info[ __( 'Generate title prompt', 'classifai' ) ] = wp_json_encode( $provider_settings['generate_title_prompt'] ?? [] );
622+
$debug_info[ __( 'Generate title prompt', 'classifai' ) ] = wp_json_encode( $settings['generate_title_prompt'] ?? [] );
578623
$debug_info[ __( 'Latest response', 'classifai' ) ] = $this->get_formatted_latest_response( get_transient( 'classifai_openai_chatgpt_title_generation_latest_response' ) );
579624
} elseif ( $this->feature_instance instanceof ExcerptGeneration ) {
580625
$debug_info[ __( 'Excerpt length', 'classifai' ) ] = $settings['length'] ?? 55;
581-
$debug_info[ __( 'Generate excerpt prompt', 'classifai' ) ] = wp_json_encode( $provider_settings['generate_excerpt_prompt'] ?? [] );
626+
$debug_info[ __( 'Generate excerpt prompt', 'classifai' ) ] = wp_json_encode( $settings['generate_excerpt_prompt'] ?? [] );
582627
$debug_info[ __( 'Latest response', 'classifai' ) ] = $this->get_formatted_latest_response( get_transient( 'classifai_openai_chatgpt_excerpt_generation_latest_response' ) );
583628
} elseif ( $this->feature_instance instanceof ContentResizing ) {
584629
$debug_info[ __( 'No. of suggestions', 'classifai' ) ] = $provider_settings['number_of_suggestions'] ?? 1;
585-
$debug_info[ __( 'Expand text prompt', 'classifai' ) ] = wp_json_encode( $provider_settings['expand_text_prompt'] ?? [] );
586-
$debug_info[ __( 'Condense text prompt', 'classifai' ) ] = wp_json_encode( $provider_settings['condense_text_prompt'] ?? [] );
630+
$debug_info[ __( 'Expand text prompt', 'classifai' ) ] = wp_json_encode( $settings['expand_text_prompt'] ?? [] );
631+
$debug_info[ __( 'Condense text prompt', 'classifai' ) ] = wp_json_encode( $settings['condense_text_prompt'] ?? [] );
587632
$debug_info[ __( 'Latest response', 'classifai' ) ] = $this->get_formatted_latest_response( get_transient( 'classifai_openai_chatgpt_content_resizing_latest_response' ) );
588633
}
589634

0 commit comments

Comments
 (0)