Skip to content
This repository was archived by the owner on Oct 23, 2023. It is now read-only.

Commit e066f69

Browse files
authored
Merge pull request #13 from OM4/feature/qa
Fixes during QA
2 parents b40fbcd + 911e9e8 commit e066f69

File tree

6 files changed

+17
-2
lines changed

6 files changed

+17
-2
lines changed

.distignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
.git
66
.github
77
.gitignore
8+
.idea
9+
.vscode
810
.wp-env.json
911
*~
1012
/node_modules

assets/screenshot-1.png

-203 KB
Loading

assets/screenshot-2.png

-35.9 KB
Loading

assets/screenshot-3.png

-362 KB
Loading

includes/Modal/OpenAi_Generator.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@ public function __construct( Data $settings, OpenAiClient $client ) {
4646
* @param WC_Product $product The WooCommerce product to generate a description for.
4747
*
4848
* @return string The generated product description.
49-
* @throws Exception When an error occurs.
49+
*
50+
* @throws ClientException Is converted to an Exception.
51+
* @throws Exception When no API key exists.
52+
* @throws Exception When an API or moderation error occurs.
5053
*/
5154
public function generate( WC_Product $product ) {
5255
$settings = $this->settings->get_settings();
@@ -74,6 +77,9 @@ public function generate( WC_Product $product ) {
7477
$result = $this->client->moderations()->create(
7578
new ModerationsRequest( array( 'input' => $prompt ) )
7679
)->toArray();
80+
if ( array_key_exists( 'error', $result ) ) {
81+
throw new ClientException( $result['error']['message'] );
82+
}
7783

7884
$flagged = (int) $result['results'][0]['flagged'];
7985
set_transient( $key, $flagged, DAY_IN_SECONDS );
@@ -95,7 +101,7 @@ public function generate( WC_Product $product ) {
95101
*
96102
* @var array $result
97103
*/
98-
$result = $completions->create(
104+
$result = $completions->create(
99105
new CompletionsRequest(
100106
array(
101107
'model' => 'text-davinci-003',
@@ -105,6 +111,9 @@ public function generate( WC_Product $product ) {
105111
)
106112
)
107113
)->toArray();
114+
if ( array_key_exists( 'error', $result ) ) {
115+
throw new ClientException( $result['error']['message'] );
116+
}
108117
$new_description = $result['choices'][0]['text'];
109118

110119
// Moderate the result.
@@ -117,6 +126,9 @@ public function generate( WC_Product $product ) {
117126
$result = $this->client->moderations()->create(
118127
new ModerationsRequest( array( 'input' => $new_description ) )
119128
)->toArray();
129+
if ( array_key_exists( 'error', $result ) ) {
130+
throw new ClientException( $result['error']['message'] );
131+
}
120132

121133
if ( $result['results'][0]['flagged'] ) {
122134
throw new Exception(

includes/Modal/Screen.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ public function copycraft_modal_content() {
142142
echo '<button id="refresh" class="button button-primary button-large" title="' . esc_attr__( 'Generate a new description.', 'copycraft' ) . '">' . esc_html__( 'Try again', 'copycraft' ) . '</button>';
143143
echo '<button id="discard" class="button button-primary button-large" title="' . esc_attr__( 'Return to the Edit Produt screen.', 'copycraft' ) . '">' . esc_html__( 'Discard', 'copycraft' ) . '</button>';
144144
echo '</div>';
145+
exit;
145146
} catch ( Exception $e ) {
146147
$this->error( esc_html( $e->getMessage() ) );
147148
}

0 commit comments

Comments
 (0)