Skip to content

Instantly share code, notes, and snippets.

@drzraf
Created July 27, 2018 15:44
Show Gist options
  • Save drzraf/d0f3f03aeb2fac42e0e615516738f459 to your computer and use it in GitHub Desktop.
Save drzraf/d0f3f03aeb2fac42e0e615516738f459 to your computer and use it in GitHub Desktop.
diff --git a/forms_model.php b/forms_model.php
index d872428403f9d98d16dc5184c9aa8b4e04a5b1bc..c261bbcf4d141995ca9c7bdeb8a08507f3e366dc 100644
--- a/forms_model.php
+++ b/forms_model.php
@@ -965,21 +965,22 @@ class GFFormsModel {
}
$results = $wpdb->get_results(
- " SELECT display_meta, confirmations, notifications FROM {$form_table_name} f
+ " SELECT id, display_meta, confirmations, notifications FROM {$form_table_name} f
INNER JOIN {$meta_table_name} m ON f.id = m.form_id
WHERE id in({$ids})", ARRAY_A
);
+ $forms = array();
foreach ( $results as &$result ) {
$form = self::unserialize( $result['display_meta'] );
$form['confirmations'] = self::unserialize( $result['confirmations'] );
$form['notifications'] = self::unserialize( $result['notifications'] );
// Creating field objects and copying some form variables down to fields for easier access
$form = self::convert_field_objects( $form );
- $result = $form;
+ $forms[ $result['id'] ] = $form;
}
- return $results;
+ return $forms;
}
@@ -1739,12 +1740,18 @@ class GFFormsModel {
}
}
- public static function insert_form( $form_title ) {
+ public static function insert_form( $form_title, $desired_id = FALSE ) {
global $wpdb;
$form_table_name = self::get_form_table_name();
//creating new form
- $wpdb->query( $wpdb->prepare( "INSERT INTO $form_table_name(title, date_created) VALUES(%s, utc_timestamp())", $form_title ) );
+ //test whether a specific id is desired *and* available
+ if ( $desired_id && !self::get_form( $desired_id, true ) ) {
+ $wpdb->query( $wpdb->prepare( "INSERT INTO $form_table_name(id, title, date_created) VALUES(%d, %s, utc_timestamp())", $desired_id, $form_title ) );
+ }
+ else {
+ $wpdb->query( $wpdb->prepare( "INSERT INTO $form_table_name(title, date_created) VALUES(%s, utc_timestamp())", $form_title ) );
+ }
//returning newly created form id
return $wpdb->insert_id;
diff --git a/includes/api.php b/includes/api.php
index 8e135eec96aed1cd7dd6fba85f0c9e42d6511bc4..c97b484a4eca5493fce68303c3e25f477d2997c9 100644
--- a/includes/api.php
+++ b/includes/api.php
@@ -360,8 +360,8 @@ class GFAPI {
return new WP_Error( 'invalid', __( 'Invalid form objects', 'gravityforms' ) );
}
$form_ids = array();
- foreach ( $forms as $form ) {
- $result = self::add_form( $form );
+ foreach ( $forms as $key => $form ) {
+ $result = self::add_form( $form, $key );
if ( is_wp_error( $result ) ) {
return $result;
}
@@ -384,10 +384,11 @@ class GFAPI {
* @uses GFFormsModel::update_form_meta()
*
* @param array $form_meta The Form object.
+ * @param array $desired_id Form ID to use (if available)
*
* @return int|WP_Error Either the new Form ID or a WP_Error instance.
*/
- public static function add_form( $form_meta ) {
+ public static function add_form( $form_meta, $desired_id = FALSE ) {
global $wpdb;
if ( gf_upgrade()->get_submissions_block() ) {
@@ -410,7 +411,7 @@ class GFAPI {
}
// Inserting form.
- $form_id = RGFormsModel::insert_form( $title );
+ $form_id = RGFormsModel::insert_form( $title, $desired_id );
// Updating form meta.
$form_meta['title'] = $title;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment