upgrade-functions.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php if ( ! defined( 'ABSPATH' ) ) exit;
  2. /**
  3. * Update form settings to the new storage system when the form is viewed for the first time.
  4. *
  5. * @since 2.9
  6. * @return void
  7. */
  8. function nf_29_update_form_settings( $form_id ) {
  9. global $wpdb;
  10. // Check to see if the conversion has been reset.
  11. $is_reset = get_option( 'nf_converted_form_reset', 0 );
  12. // Check to see if an object exists with our form id.
  13. $type = nf_get_object_type($form_id);
  14. if ( $type ) {
  15. // We have an object with our form id.
  16. if ( $is_reset AND 'form' == $type ) {
  17. // Give precedence to the most recent form.
  18. // Set a new ID for the form being converted.
  19. $f_id = nf_insert_object('form');
  20. $fields = $wpdb->get_results("SELECT * FROM " . NINJA_FORMS_FIELDS_TABLE_NAME . " WHERE form_id = " . $form_id, ARRAY_A);
  21. foreach ($fields as $field) {
  22. unset($field['id']);
  23. $field['form_id'] = $f_id;
  24. // Copy the Fields to the new ID.
  25. $wpdb->insert(NINJA_FORMS_FIELDS_TABLE_NAME, $field);
  26. }
  27. $relationships = $wpdb->get_results("SELECT * FROM " . NF_OBJECT_RELATIONSHIPS_TABLE_NAME . " WHERE parent_id = " . $form_id, ARRAY_A);
  28. foreach ($relationships as $relationship) {
  29. unset($relationship['id']);
  30. // Copy the object related to the form.
  31. $object = $wpdb->get_results("SELECT * FROM " . NF_OBJECTS_TABLE_NAME . " WHERE id = " . $relationship['child_id'], ARRAY_A);
  32. unset($object['id']);
  33. $wpdb->insert(NF_OBJECTS_TABLE_NAME, $object);
  34. $relationship['child_id'] = $wpdb->insert_id;
  35. $relationship['parent_id'] = $f_id;
  36. // Copy the Relationships to the new ID.
  37. $wpdb->insert(NF_OBJECT_RELATIONSHIPS_TABLE_NAME, $relationship);
  38. }
  39. } else {
  40. // Give precedence to the converting form.
  41. // Insert a new object.
  42. $next_id = nf_insert_object($type);
  43. // Replace all instances of the conflicting object ID with our new one.
  44. $wpdb->update(NF_OBJECT_META_TABLE_NAME, array('object_id' => $next_id), array('object_id' => $form_id));
  45. $wpdb->update(NF_OBJECT_RELATIONSHIPS_TABLE_NAME, array('parent_id' => $next_id), array('parent_type' => $type, 'parent_id' => $form_id));
  46. $wpdb->update(NF_OBJECT_RELATIONSHIPS_TABLE_NAME, array('child_id' => $next_id), array('child_type' => $type, 'child_id' => $form_id));
  47. // Delete the original (conflicting) object
  48. $wpdb->query('DELETE FROM ' . NF_OBJECTS_TABLE_NAME . ' WHERE id = ' . $form_id);
  49. }
  50. }
  51. // Get the form from the old table.
  52. $form = $wpdb->get_row( 'SELECT * FROM ' . NINJA_FORMS_TABLE_NAME . ' WHERE id = ' . $form_id, ARRAY_A );
  53. // Set the insert form ID, if not already set.
  54. $f_id = isset ( $f_id ) ? $f_id : nf_insert_object( 'form', $form['id'] );
  55. // Unpack the converted form's settings
  56. $settings = maybe_unserialize( $form['data'] );
  57. $settings['date_updated'] = $form['date_updated'];
  58. foreach ( $settings as $meta_key => $value ) {
  59. nf_update_object_meta( $f_id, $meta_key, $value );
  60. }
  61. nf_update_object_meta( $f_id, 'status', '' );
  62. }
  63. /**
  64. * Check our option to see if we've updated all of our form settings.
  65. * If we haven't, then update the form currently being viewed.
  66. *
  67. * @since 2.9
  68. * @return void
  69. */
  70. function nf_29_update_form_settings_check( $form_id ) {
  71. // Bail if we are in the admin
  72. if ( is_admin() )
  73. return false;
  74. // Bail if this form was created in 2.9 or higher.
  75. if ( 'form' == nf_get_object_type( $form_id ) )
  76. return false;
  77. // Get a list of forms that we've already converted.
  78. $completed_forms = get_option( 'nf_converted_forms', array() );
  79. if ( ! is_array( $completed_forms ) )
  80. $completed_forms = array();
  81. // Bail if we've already converted the db for this form.
  82. if ( in_array( $form_id, $completed_forms ) )
  83. return false;
  84. nf_29_update_form_settings( $form_id );
  85. $completed_forms[] = $form_id;
  86. update_option( 'nf_converted_forms', $completed_forms );
  87. }
  88. add_action( 'nf_before_display_loading', 'nf_29_update_form_settings_check' );