Skip to content

Commit 1f0d943

Browse files
committed
Fixed issue where results in mapped fields would not show in notifications.
1 parent 68f7f79 commit 1f0d943

File tree

1 file changed

+30
-12
lines changed

1 file changed

+30
-12
lines changed

class-gwiz-gf-openai.php

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,12 @@ class GWiz_GF_OpenAI extends GFFeedAddOn {
5656
protected $_title = 'Gravity Forms OpenAI';
5757
protected $_short_title = 'OpenAI';
5858

59-
// Use async feed processing to speed up form submission. Moderations endpoint isn't handled via process_feed().
60-
protected $_async_feed_processing = true;
59+
/**
60+
* Disable async feed processing for now as it can prevent results mapped to fields from working in notifications.
61+
*
62+
* @var bool
63+
*/
64+
protected $_async_feed_processing = false;
6165

6266
public static function get_instance() {
6367
if ( self::$instance === null ) {
@@ -790,11 +794,11 @@ public function process_feed( $feed, $entry, $form ) {
790794

791795
switch ( $endpoint ) {
792796
case 'completions':
793-
$this->process_endpoint_completions( $feed, $entry, $form );
797+
$entry = $this->process_endpoint_completions( $feed, $entry, $form );
794798
break;
795799

796800
case 'edits':
797-
$this->process_endpoint_edits( $feed, $entry, $form );
801+
$entry = $this->process_endpoint_edits( $feed, $entry, $form );
798802
break;
799803

800804
case 'images/generations':
@@ -810,6 +814,8 @@ public function process_feed( $feed, $entry, $form ) {
810814
$this->add_feed_error( sprintf( __( 'Unknown endpoint: %s' ), $endpoint ), $feed, $entry, $form );
811815
break;
812816
}
817+
818+
return $entry;
813819
}
814820

815821
/**
@@ -818,6 +824,8 @@ public function process_feed( $feed, $entry, $form ) {
818824
* @param $feed array The current feed being processed.
819825
* @param $entry array The current entry being processed.
820826
* @param $form array The current form being processed.
827+
*
828+
* @return array Modified entry.
821829
*/
822830
public function process_endpoint_completions( $feed, $entry, $form ) {
823831
$model = $feed['meta']['completions_model'];
@@ -839,27 +847,29 @@ public function process_endpoint_completions( $feed, $entry, $form ) {
839847
if ( is_wp_error( $response ) ) {
840848
// If there was an error, log it and return.
841849
$this->add_feed_error( $response->get_error_message(), $feed, $entry, $form );
842-
return;
850+
return $entry;
843851
}
844852

845853
// Parse the response and add it as an entry note.
846854
$response_data = json_decode( $response['body'], true );
847855

848856
if ( rgar( $response_data, 'error' ) ) {
849857
$this->add_feed_error( $response_data['error']['message'], $feed, $entry, $form );
850-
return;
858+
return $entry;
851859
}
852860

853861
$text = $this->get_text_from_response( $response_data );
854862

855863
if ( ! is_wp_error( $text ) ) {
856864
GFAPI::add_note( $entry['id'], 0, 'OpenAI Response (' . $feed['meta']['feed_name'] . ')', $text );
857-
$this->maybe_save_result_to_field( $feed, $entry, $form, $text );
865+
$entry = $this->maybe_save_result_to_field( $feed, $entry, $form, $text );
858866
} else {
859867
$this->add_feed_error( $text->get_error_message(), $feed, $entry, $form );
860868
}
861869

862870
gform_add_meta( $entry['id'], 'openai_response_' . $feed['id'], $response['body'] );
871+
872+
return $entry;
863873
}
864874

865875
/**
@@ -868,6 +878,8 @@ public function process_endpoint_completions( $feed, $entry, $form ) {
868878
* @param $feed array The current feed being processed.
869879
* @param $entry array The current entry being processed.
870880
* @param $form array The current form being processed.
881+
*
882+
* @return array Modified entry.
871883
*/
872884
public function process_endpoint_edits( $feed, $entry, $form ) {
873885
$model = $feed['meta']['edits_model'];
@@ -892,42 +904,48 @@ public function process_endpoint_edits( $feed, $entry, $form ) {
892904
if ( is_wp_error( $response ) ) {
893905
// If there was an error, log it and return.
894906
$this->add_feed_error( $response->get_error_message(), $feed, $entry, $form );
895-
return;
907+
return $entry;
896908
}
897909

898910
// Parse the response and add it as an entry note.
899911
$response_data = json_decode( $response['body'], true );
900912

901913
if ( rgar( $response_data, 'error' ) ) {
902914
$this->add_feed_error( $response_data['error']['message'], $feed, $entry, $form );
903-
return;
915+
return $entry;
904916
}
905917

906918
$text = $this->get_text_from_response( $response_data );
907919

908920
if ( ! is_wp_error( $text ) ) {
909921
GFAPI::add_note( $entry['id'], 0, 'OpenAI Response (' . $feed['meta']['feed_name'] . ')', $text );
910-
$this->maybe_save_result_to_field( $feed, $entry, $form, $text );
922+
$entry = $this->maybe_save_result_to_field( $feed, $entry, $form, $text );
911923
} else {
912924
$this->add_feed_error( $text->get_error_message(), $feed, $entry, $form );
913925
}
914926

915927
gform_add_meta( $entry['id'], 'openai_response_' . $feed['id'], $response['body'] );
928+
929+
return $entry;
916930
}
917931

918932

919933
/**
920934
* Saves the result to the selected field if configured.
935+
*
936+
* @return array Modified entry.
921937
*/
922938
public function maybe_save_result_to_field( $feed, $entry, $form, $text ) {
923939
$endpoint = rgars( $feed, 'meta/endpoint' );
924940
$map_result_to_field = rgars( $feed, 'meta/' . $endpoint . '_map_result_to_field' );
925941

926942
if ( ! is_numeric( $map_result_to_field ) ) {
927-
return;
943+
return $entry;
928944
}
929945

930-
GFAPI::update_entry_field( $entry['id'], $map_result_to_field, $text );
946+
$entry[ $map_result_to_field ] = $text;
947+
948+
return $entry;
931949
}
932950

933951
/**

0 commit comments

Comments
 (0)