convert-forms.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php if ( ! defined( 'ABSPATH' ) ) exit;
  2. final class NF_Upgrade_Forms extends NF_Upgrade
  3. {
  4. public $name = 'forms';
  5. public $priority = '2.9.0';
  6. public $description = 'Form settings and information needs to be transferred to a new forms database. This new storage method will make it much easier effecient to interact with forms.';
  7. public $completed_forms = array();
  8. public $args = array();
  9. public $errors = array();
  10. public function loading()
  11. {
  12. global $wpdb;
  13. // Check that the table exists
  14. if( 0 == $wpdb->query( "SHOW TABLES LIKE '" . NINJA_FORMS_TABLE_NAME . "'" ) ) {
  15. //TODO Maybe change error to debug message instead
  16. $this->errors[] = NINJA_FORMS_TABLE_NAME . ' is not in the database';
  17. return;
  18. }
  19. // Get all our forms
  20. $forms = $wpdb->get_results( 'SELECT id FROM ' . NINJA_FORMS_TABLE_NAME, ARRAY_A );
  21. $x = 1;
  22. if ( is_array( $forms ) ) {
  23. foreach ( $forms as $form ) {
  24. $this->args['forms'][$x] = $form['id'];
  25. $x++;
  26. }
  27. }
  28. $this->total_steps = count( $this->args['forms'] );
  29. if( empty( $this->total_steps ) || $this->total_steps <= 1 ) {
  30. $this->total_steps = 1;
  31. }
  32. }
  33. public function _beforeStep( $step )
  34. {
  35. // Get a list of forms that we've already converted.
  36. $this->completed_forms = get_option( 'nf_converted_forms', array() );
  37. if ( ! is_array( $this->completed_forms ) ) {
  38. $this->completed_forms = array();
  39. }
  40. $this->form_id = $this->args['forms'][ $step ];
  41. Ninja_Forms()->form( $this->form_id )->dumpCache();
  42. // Bail if we've already converted the db for this form.
  43. if ( in_array( $this->form_id, $this->completed_forms ) ) {
  44. return false;
  45. }
  46. }
  47. public function step( $step )
  48. {
  49. $this->update_form_settings( $this->form_id );
  50. }
  51. public function _afterStep( $step )
  52. {
  53. $this->completed_forms[] = $this->form_id;
  54. update_option( 'nf_converted_forms', $this->completed_forms );
  55. Ninja_Forms()->form( $this->form_id )->dumpCache();
  56. }
  57. public function complete()
  58. {
  59. update_option( 'nf_convert_forms_complete', true );
  60. update_option( 'nf_converted_form_reset', false );
  61. }
  62. public function isComplete()
  63. {
  64. return get_option( 'nf_convert_forms_complete', false );
  65. }
  66. /*
  67. * CONVERSION METHODS
  68. */
  69. public function update_form_settings( $form_id )
  70. {
  71. global $wpdb;
  72. // Check to see if the conversion has been reset.
  73. $is_reset = get_option( 'nf_converted_form_reset', 0 );
  74. // Check to see if an object exists with our form id.
  75. $type = nf_get_object_type($form_id);
  76. if ( $type ) {
  77. // We have an object with our form id.
  78. if ( $is_reset AND 'form' == $type ) {
  79. // Give precedence to the most recent form.
  80. // Set a new ID for the form being converted.
  81. $f_id = nf_insert_object('form');
  82. $fields = $wpdb->get_results("SELECT * FROM " . NINJA_FORMS_FIELDS_TABLE_NAME . " WHERE form_id = " . $form_id, ARRAY_A);
  83. foreach ($fields as $field) {
  84. unset($field['id']);
  85. $field['form_id'] = $f_id;
  86. // Copy the Fields to the new ID.
  87. $wpdb->insert(NINJA_FORMS_FIELDS_TABLE_NAME, $field);
  88. }
  89. $relationships = $wpdb->get_results("SELECT * FROM " . NF_OBJECT_RELATIONSHIPS_TABLE_NAME . " WHERE parent_id = " . $form_id, ARRAY_A);
  90. foreach ($relationships as $relationship) {
  91. unset($relationship['id']);
  92. // Copy the object related to the form.
  93. $object = $wpdb->get_results("SELECT * FROM " . NF_OBJECTS_TABLE_NAME . " WHERE id = " . $relationship['child_id'], ARRAY_A);
  94. unset($object['id']);
  95. $wpdb->insert(NF_OBJECTS_TABLE_NAME, $object);
  96. $relationship['child_id'] = $wpdb->insert_id;
  97. $relationship['parent_id'] = $f_id;
  98. // Copy the Relationships to the new ID.
  99. $wpdb->insert(NF_OBJECT_RELATIONSHIPS_TABLE_NAME, $relationship);
  100. }
  101. } else {
  102. // Give precedence to the converting form.
  103. // Insert a new object.
  104. $next_id = nf_insert_object($type);
  105. // Replace all instances of the conflicting object ID with our new one.
  106. $wpdb->update(NF_OBJECT_META_TABLE_NAME, array('object_id' => $next_id), array('object_id' => $form_id));
  107. $wpdb->update(NF_OBJECT_RELATIONSHIPS_TABLE_NAME, array('parent_id' => $next_id), array('parent_type' => $type, 'parent_id' => $form_id));
  108. $wpdb->update(NF_OBJECT_RELATIONSHIPS_TABLE_NAME, array('child_id' => $next_id), array('child_type' => $type, 'child_id' => $form_id));
  109. // Delete the original (conflicting) object
  110. $wpdb->query('DELETE FROM ' . NF_OBJECTS_TABLE_NAME . ' WHERE id = ' . $form_id);
  111. }
  112. }
  113. // Get the form from the old table.
  114. $form = $wpdb->get_row( 'SELECT * FROM ' . NINJA_FORMS_TABLE_NAME . ' WHERE id = ' . $form_id, ARRAY_A );
  115. // Set the insert form ID, if not already set.
  116. $f_id = isset ( $f_id ) ? $f_id : nf_insert_object( 'form', $form['id'] );
  117. // Unpack the converted form's settings
  118. $settings = maybe_unserialize( $form['data'] );
  119. $settings['date_updated'] = $form['date_updated'];
  120. foreach ( $settings as $meta_key => $value ) {
  121. nf_update_object_meta( $f_id, $meta_key, $value );
  122. }
  123. nf_update_object_meta( $f_id, 'status', '' );
  124. }
  125. }