Skip to content

Commit b2975be

Browse files
committed
Added new raw modifier that can be used in the feed merge tags. This allows for fetching specific properties from the OpenAI response. Example usage: {:1:openai_feed_2,raw[usage/total_tokens]}
1 parent b26d8ed commit b2975be

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

class-gwiz-gf-openai.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1306,7 +1306,7 @@ public function replace_merge_tags( $text, $form, $entry, $url_encode, $esc_html
13061306
continue;
13071307
}
13081308

1309-
$replacement = $this->get_merge_tag_replacement( $form, $entry, $feed_id, $url_encode, $esc_html, $nl2br, $format );
1309+
$replacement = $this->get_merge_tag_replacement( $form, $entry, $feed_id, $url_encode, $esc_html, $nl2br, $format, $modifiers );
13101310
$text = str_replace( $match[0], $replacement, $text );
13111311
}
13121312

@@ -1319,14 +1319,14 @@ public function replace_merge_tags( $text, $form, $entry, $url_encode, $esc_html
13191319
continue;
13201320
}
13211321

1322-
$replacement = $this->get_merge_tag_replacement( $form, $entry, $feed_id, $url_encode, $esc_html, $nl2br, $format );
1322+
$replacement = $this->get_merge_tag_replacement( $form, $entry, $feed_id, $url_encode, $esc_html, $nl2br, $format, $modifiers );
13231323
$text = str_replace( $match[0], $replacement, $text );
13241324
}
13251325

13261326
return $text;
13271327
}
13281328

1329-
public function get_merge_tag_replacement( $form, $entry, $feed_id, $url_encode, $esc_html, $nl2br, $format ) {
1329+
public function get_merge_tag_replacement( $form, $entry, $feed_id, $url_encode, $esc_html, $nl2br, $format, $modifiers ) {
13301330
$feed = $this->get_feed( $feed_id );
13311331
$endpoint = rgars( $feed, 'meta/endpoint' );
13321332

@@ -1342,6 +1342,8 @@ public function get_merge_tag_replacement( $form, $entry, $feed_id, $url_encode,
13421342
return '';
13431343
}
13441344

1345+
$response_data = array();
1346+
13451347
switch ( $endpoint ) {
13461348
case 'completions':
13471349
$model = $feed['meta']['completions_model'];
@@ -1354,7 +1356,9 @@ public function get_merge_tag_replacement( $form, $entry, $feed_id, $url_encode,
13541356
'prompt' => $prompt,
13551357
), $feed );
13561358

1357-
$response_data = json_decode( $response['body'], true );
1359+
if ( ! is_wp_error( $response ) ) {
1360+
$response_data = json_decode( $response['body'], true );
1361+
}
13581362
break;
13591363

13601364
case 'edits':
@@ -1371,14 +1375,20 @@ public function get_merge_tag_replacement( $form, $entry, $feed_id, $url_encode,
13711375
'instruction' => $instruction,
13721376
), $feed );
13731377

1374-
$response_data = json_decode( $response['body'], true );
1378+
if ( ! is_wp_error( $response ) ) {
1379+
$response_data = json_decode( $response['body'], true );
1380+
}
13751381
break;
13761382

13771383
default:
13781384
return '';
13791385
}
13801386

1381-
$text = $this->get_text_from_response( $response_data );
1387+
if ( ! rgar( $modifiers, 'raw' ) ) {
1388+
$text = $this->get_text_from_response( $response_data );
1389+
} else {
1390+
$text = rgars( $response_data, rgar( $modifiers, 'raw' ) );
1391+
}
13821392

13831393
$text = $url_encode ? urlencode( $text ) : $text;
13841394
$text = $format === 'html' ? wp_kses_post( $text ) : wp_strip_all_tags( $text );

0 commit comments

Comments
 (0)