Skip to content

Commit

Permalink
Feedback fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
barthc committed Mar 4, 2024
1 parent f33efe0 commit 79906a3
Showing 1 changed file with 46 additions and 11 deletions.
57 changes: 46 additions & 11 deletions class-gwiz-gf-openai.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public function pre_init() {

add_filter( 'gform_export_form', array( $this, 'export_feeds_with_form' ) );
add_action( 'gform_forms_post_import', array( $this, 'import_feeds_with_form' ) );
add_action( 'admin_head', array( $this, 'disable_notice_css' ) );
add_action( 'admin_footer', array( $this, 'add_warning_css' ) );
}

/**
Expand Down Expand Up @@ -627,9 +627,12 @@ public function feed_settings_fields() {
),
array(
'title' => 'Advanced Settings: Moderations',

Check warning on line 629 in class-gwiz-gf-openai.php

View workflow job for this annotation

GitHub Actions / PHPCS

Array double arrow not aligned correctly; expected 8 space(s) between "'title'" and double arrow, but found 6.
'id' => 'advanced_settings_moderations',

Check warning on line 630 in class-gwiz-gf-openai.php

View workflow job for this annotation

GitHub Actions / PHPCS

Array double arrow not aligned correctly; expected 11 space(s) between "'id'" and double arrow, but found 9.
'fields' => array(

Check warning on line 631 in class-gwiz-gf-openai.php

View workflow job for this annotation

GitHub Actions / PHPCS

Array double arrow not aligned correctly; expected 7 space(s) between "'fields'" and double arrow, but found 5.
$this->feed_advanced_setting_timeout( 'moderations' ),
),
'collapsible' => true,
'is_collapsed' => ! isset( $_POST['gform_settings_section_collapsed_advanced_settings_moderations'] ),
'dependency' => array(

Check warning on line 636 in class-gwiz-gf-openai.php

View workflow job for this annotation

GitHub Actions / PHPCS

Array double arrow not aligned correctly; expected 3 space(s) between "'dependency'" and double arrow, but found 1.
'live' => true,
'fields' => array(
Expand Down Expand Up @@ -1447,19 +1450,27 @@ public function get_headers() {
}

public function get_feed( $id ) {
$feed = parent::get_feed( $id );
$endpoint = rgars( $feed, 'meta/endpoint' );
$feed = parent::get_feed( $id );

if ( ! in_array( $endpoint, array( 'completions', 'edits' ) ) ) {
return $feed;
if ( $this->should_transform_feed( $feed ) ) {
return $this->transform_feed( $feed );
}

if ( $this->is_feed_edit_page() ) {
$message = $this->get_transform_message( $endpoint );
GFCommon::add_dismissible_message( $message, $id . '_transform_feed_notice' );
return $feed;
}

public function feed_edit_page( $form, $feed_id ) {

Check failure on line 1462 in class-gwiz-gf-openai.php

View workflow job for this annotation

GitHub Actions / PHPCS

Tabs must be used to indent lines; spaces are not allowed
$feed = parent::get_feed( $feed_id );

if ( $this->should_transform_feed( $feed ) ) {
$endpoint = rgars( $feed, 'meta/endpoint' );
$notice = $this->get_transform_message( $endpoint );
$this->get_settings_renderer()->set_postback_message_callback( function( $message ) use( $notice ) {

Check failure on line 1468 in class-gwiz-gf-openai.php

View workflow job for this annotation

GitHub Actions / PHPCS

Space after opening control structure is required

Check failure on line 1468 in class-gwiz-gf-openai.php

View workflow job for this annotation

GitHub Actions / PHPCS

No space before opening parenthesis is prohibited
return $notice;
} );
}

return $this->transform_feed( $feed );
parent::feed_edit_page( $form, $feed_id );
}

public function get_feeds( $form_id = null ) {
Expand Down Expand Up @@ -1533,10 +1544,34 @@ public function get_transform_message( $endpoint ) {
return sprintf( __( 'This feed has been updated to use the "Chat Completions" endpoint, replacing the "%s" endpoint which has been deprecated by OpenAI. Save this feed to dismiss this message.', 'gravityforms-openai' ), ucfirst( $endpoint ) );
}

public function disable_notice_css() {
public function should_transform_feed( $feed ) {
$endpoint = rgars( $feed, 'meta/endpoint' );

if ( in_array( $endpoint, array( 'completions', 'edits' ) ) ) {
return true;
}

return false;
}

public function add_warning_css() {
$messages = json_encode(
array(

Check failure on line 1559 in class-gwiz-gf-openai.php

View workflow job for this annotation

GitHub Actions / PHPCS

Whitespace found at end of line
$this->get_transform_message( 'completions' ),
$this->get_transform_message( 'edits' ),
)
);

if ( $this->is_feed_edit_page() ) {
?>
<style>.notice-dismiss { display: none; }</style>
<script>
const notice = document.getElementsByClassName("gforms_note_success")[0];
const messages = <?php echo $messages; ?>;
if (messages.includes(notice.innerText)){
notice.classList.remove("gforms_note_success");
notice.classList.add("gforms_note_warning");
}
</script>
<?php
}
}
Expand Down

0 comments on commit 79906a3

Please sign in to comment.