@@ -56,8 +56,12 @@ class GWiz_GF_OpenAI extends GFFeedAddOn {
56
56
protected $ _title = 'Gravity Forms OpenAI ' ;
57
57
protected $ _short_title = 'OpenAI ' ;
58
58
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 ;
61
65
62
66
public static function get_instance () {
63
67
if ( self ::$ instance === null ) {
@@ -790,11 +794,11 @@ public function process_feed( $feed, $entry, $form ) {
790
794
791
795
switch ( $ endpoint ) {
792
796
case 'completions ' :
793
- $ this ->process_endpoint_completions ( $ feed , $ entry , $ form );
797
+ $ entry = $ this ->process_endpoint_completions ( $ feed , $ entry , $ form );
794
798
break ;
795
799
796
800
case 'edits ' :
797
- $ this ->process_endpoint_edits ( $ feed , $ entry , $ form );
801
+ $ entry = $ this ->process_endpoint_edits ( $ feed , $ entry , $ form );
798
802
break ;
799
803
800
804
case 'images/generations ' :
@@ -810,6 +814,8 @@ public function process_feed( $feed, $entry, $form ) {
810
814
$ this ->add_feed_error ( sprintf ( __ ( 'Unknown endpoint: %s ' ), $ endpoint ), $ feed , $ entry , $ form );
811
815
break ;
812
816
}
817
+
818
+ return $ entry ;
813
819
}
814
820
815
821
/**
@@ -818,6 +824,8 @@ public function process_feed( $feed, $entry, $form ) {
818
824
* @param $feed array The current feed being processed.
819
825
* @param $entry array The current entry being processed.
820
826
* @param $form array The current form being processed.
827
+ *
828
+ * @return array Modified entry.
821
829
*/
822
830
public function process_endpoint_completions ( $ feed , $ entry , $ form ) {
823
831
$ model = $ feed ['meta ' ]['completions_model ' ];
@@ -839,27 +847,29 @@ public function process_endpoint_completions( $feed, $entry, $form ) {
839
847
if ( is_wp_error ( $ response ) ) {
840
848
// If there was an error, log it and return.
841
849
$ this ->add_feed_error ( $ response ->get_error_message (), $ feed , $ entry , $ form );
842
- return ;
850
+ return $ entry ;
843
851
}
844
852
845
853
// Parse the response and add it as an entry note.
846
854
$ response_data = json_decode ( $ response ['body ' ], true );
847
855
848
856
if ( rgar ( $ response_data , 'error ' ) ) {
849
857
$ this ->add_feed_error ( $ response_data ['error ' ]['message ' ], $ feed , $ entry , $ form );
850
- return ;
858
+ return $ entry ;
851
859
}
852
860
853
861
$ text = $ this ->get_text_from_response ( $ response_data );
854
862
855
863
if ( ! is_wp_error ( $ text ) ) {
856
864
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 );
858
866
} else {
859
867
$ this ->add_feed_error ( $ text ->get_error_message (), $ feed , $ entry , $ form );
860
868
}
861
869
862
870
gform_add_meta ( $ entry ['id ' ], 'openai_response_ ' . $ feed ['id ' ], $ response ['body ' ] );
871
+
872
+ return $ entry ;
863
873
}
864
874
865
875
/**
@@ -868,6 +878,8 @@ public function process_endpoint_completions( $feed, $entry, $form ) {
868
878
* @param $feed array The current feed being processed.
869
879
* @param $entry array The current entry being processed.
870
880
* @param $form array The current form being processed.
881
+ *
882
+ * @return array Modified entry.
871
883
*/
872
884
public function process_endpoint_edits ( $ feed , $ entry , $ form ) {
873
885
$ model = $ feed ['meta ' ]['edits_model ' ];
@@ -892,42 +904,48 @@ public function process_endpoint_edits( $feed, $entry, $form ) {
892
904
if ( is_wp_error ( $ response ) ) {
893
905
// If there was an error, log it and return.
894
906
$ this ->add_feed_error ( $ response ->get_error_message (), $ feed , $ entry , $ form );
895
- return ;
907
+ return $ entry ;
896
908
}
897
909
898
910
// Parse the response and add it as an entry note.
899
911
$ response_data = json_decode ( $ response ['body ' ], true );
900
912
901
913
if ( rgar ( $ response_data , 'error ' ) ) {
902
914
$ this ->add_feed_error ( $ response_data ['error ' ]['message ' ], $ feed , $ entry , $ form );
903
- return ;
915
+ return $ entry ;
904
916
}
905
917
906
918
$ text = $ this ->get_text_from_response ( $ response_data );
907
919
908
920
if ( ! is_wp_error ( $ text ) ) {
909
921
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 );
911
923
} else {
912
924
$ this ->add_feed_error ( $ text ->get_error_message (), $ feed , $ entry , $ form );
913
925
}
914
926
915
927
gform_add_meta ( $ entry ['id ' ], 'openai_response_ ' . $ feed ['id ' ], $ response ['body ' ] );
928
+
929
+ return $ entry ;
916
930
}
917
931
918
932
919
933
/**
920
934
* Saves the result to the selected field if configured.
935
+ *
936
+ * @return array Modified entry.
921
937
*/
922
938
public function maybe_save_result_to_field ( $ feed , $ entry , $ form , $ text ) {
923
939
$ endpoint = rgars ( $ feed , 'meta/endpoint ' );
924
940
$ map_result_to_field = rgars ( $ feed , 'meta/ ' . $ endpoint . '_map_result_to_field ' );
925
941
926
942
if ( ! is_numeric ( $ map_result_to_field ) ) {
927
- return ;
943
+ return $ entry ;
928
944
}
929
945
930
- GFAPI ::update_entry_field ( $ entry ['id ' ], $ map_result_to_field , $ text );
946
+ $ entry [ $ map_result_to_field ] = $ text ;
947
+
948
+ return $ entry ;
931
949
}
932
950
933
951
/**
0 commit comments