Skip to content

Commit

Permalink
Added OpenAI feeds to form export/import. (Closes #17)
Browse files Browse the repository at this point in the history
  • Loading branch information
claygriffiths committed Jul 23, 2023
1 parent 99bea60 commit 59646a8
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions class-gwiz-gf-openai.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ public function pre_init() {

$this->setup_autoload();
$this->init_auto_updater();

add_filter( 'gform_export_form', array( $this, 'export_feeds_with_form' ) );
add_action( 'gform_forms_post_import', array( $this, 'import_feeds_with_form' ) );
}

/**
Expand Down Expand Up @@ -1773,4 +1776,52 @@ public function get_headers() {

return $headers;
}

/**
* Export OpenAI Add-On feeds when exporting forms.
*
* @param array $form The current form being exported.
*
* @return array
*/
public function export_feeds_with_form( $form ) {
$feeds = $this->get_feeds( $form['id'] );

if ( ! isset( $form['feeds'] ) ) {
$form['feeds'] = array();
}

$form['feeds'][ $this->get_slug() ] = $feeds;

return $form;
}

/**
* Import OpenAI Add-On feeds when importing forms.
*
* @param array $forms Imported forms.
*/
public function import_feeds_with_form( $forms ) {
foreach ( $forms as $import_form ) {
// Ensure the imported form is the latest.
$form = GFAPI::get_form( $import_form['id'] );

if ( ! rgars( $form, 'feeds/' . $this->get_slug() ) ) {
continue;
}

foreach ( rgars( $form, 'feeds/' . $this->get_slug() ) as $feed ) {
GFAPI::add_feed( $form['id'], $feed['meta'], $this->get_slug() );
}

// Remove feeds from the form array as it's no longer needed.
unset( $form['feeds'][ $this->get_slug() ] );

if ( empty( $form['feeds'] ) ) {
unset( $form['feeds'] );
}

GFAPI::update_form( $form );
}
}
}

0 comments on commit 59646a8

Please sign in to comment.