NF_Upgrade_Email_Settings

The Ninja Forms NF Upgrade Email Settings class.

Defined (1)

The class is defined in the following location(s).

/deprecated/includes/admin/upgrades/update-email-settings.php  
  1. final class NF_Upgrade_Email_Settings extends NF_Upgrade 
  2. public $name = 'email_settings'; 
  3.  
  4. public $priority = '2.9.3'; 
  5.  
  6. public $description = 'Email settings need to be transferred to a new storage method.'; 
  7.  
  8. public $args = array(); 
  9.  
  10. public $errors = array(); 
  11.  
  12. public function loading() 
  13. // Remove old email settings. 
  14. $this->changeEmailFav(); 
  15.  
  16. // Get our total number of forms. 
  17. $form_count = $this->getFormCount(); 
  18.  
  19. // Get all our forms 
  20. $forms = $this->getAllForms(); 
  21.  
  22. $x = 1; 
  23. if ( is_array( $forms ) ) { 
  24. foreach ( $forms as $form ) { 
  25. $this->args['forms'][$x] = $form['id']; 
  26. $x++; 
  27.  
  28. if( empty( $this->total_steps ) || $this->total_steps <= 1 ) { 
  29. $this->total_steps = $form_count; 
  30.  
  31. $args = array( 
  32. 'total_steps' => $this->total_steps,  
  33. 'step' => 1,  
  34. ); 
  35.  
  36. $this->redirect = admin_url( 'admin.php?page=ninja-forms' ); 
  37.  
  38. return $args; 
  39.  
  40. public function step( $step ) 
  41. // Get our form ID 
  42. $form_id = $this->args['forms'][ $step ]; 
  43. $this->removeOldEmailSettings( $form_id ); 
  44.  
  45. public function complete() 
  46. update_option( 'nf_update_email_settings_complete', true ); 
  47.  
  48. public function isComplete() 
  49. return get_option( 'nf_update_email_settings_complete', false ); 
  50.  
  51. /** 
  52. * PRIVATE METHODS 
  53. */ 
  54.  
  55. private function changeEmailFav() { 
  56. global $wpdb; 
  57.  
  58. $email_address = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM ".NINJA_FORMS_FAV_FIELDS_TABLE_NAME." WHERE name = %s AND row_type = 0", 'Email' ), ARRAY_A ); 
  59.  
  60. $sql = 'DELETE FROM `' . NINJA_FORMS_FAV_FIELDS_TABLE_NAME . '` WHERE name = "Email Address"'; 
  61.  
  62. $wpdb->query( $sql ); 
  63.  
  64. $sql = 'DELETE FROM `' . NINJA_FORMS_FAV_FIELDS_TABLE_NAME . '` WHERE name = "Email"'; 
  65.  
  66. $wpdb->query( $sql ); 
  67.  
  68. if ( isset ( $email_address['id'] ) && ! empty ( $email_address['id'] ) ) { 
  69. $sql = 'INSERT INTO `'.NINJA_FORMS_FAV_FIELDS_TABLE_NAME.'` (`id`, `row_type`, `type`, `order`, `data`, `name`) VALUES 
  70. (' . $email_address['id'] . ', 0, \'_text\', 0, \'a:25:{s:5:"label";s:5:"Email";s:9:"label_pos";s:5:"above";s:13:"default_value";s:0:"";s:4:"mask";s:0:"";s:10:"datepicker";s:1:"0";s:5:"email";s:1:"1";s:10:"send_email";s:1:"0";s:10:"from_email";s:1:"0";s:10:"first_name";s:1:"0";s:9:"last_name";s:1:"0";s:9:"from_name";s:1:"0";s:14:"user_address_1";s:1:"0";s:14:"user_address_2";s:1:"0";s:9:"user_city";s:1:"0";s:8:"user_zip";s:1:"0";s:10:"user_phone";s:1:"0";s:10:"user_email";s:1:"1";s:21:"user_info_field_group";s:1:"1";s:3:"req";s:1:"0";s:5:"class";s:0:"";s:9:"show_help";s:1:"0";s:9:"help_text";s:0:"";s:17:"calc_auto_include";s:1:"0";s:11:"calc_option";s:1:"0";s:11:"conditional";s:0:"";}\', \'Email\')'; 
  71. $wpdb->query($sql); 
  72.  
  73.  
  74. private function getFormCount() { 
  75. global $wpdb; 
  76.  
  77. $forms = Ninja_Forms()->forms()->get_all(); 
  78. return count( $forms ); 
  79.  
  80. private function getAllForms() { 
  81. $forms = Ninja_Forms()->forms()->get_all(); 
  82.  
  83. $tmp_array = array(); 
  84. $x = 0; 
  85. foreach ( $forms as $form_id ) { 
  86. $tmp_array[ $x ]['id'] = $form_id; 
  87. $tmp_array[ $x ]['data'] = Ninja_Forms()->form( $form_id )->get_all_settings(); 
  88. $tmp_array[ $x ]['name'] = Ninja_Forms()->form( $form_id )->get_setting( 'form_title' ); 
  89. $x++; 
  90.  
  91. return $tmp_array; 
  92.  
  93. private function removeOldEmailSettings( $form_id = '' ) { 
  94.  
  95. if ( '' == $form_id ) { 
  96. $forms = $this->getAllForms(); 
  97.  
  98. if ( is_array( $forms ) ) { 
  99. foreach ( $forms as $form ) { 
  100. $this->removeOldEmailSendTo( $form['id'] ); 
  101. } else { 
  102. $this->removeOldEmailSendTo( $form_id ); 
  103.  
  104. function removeOldEmailSendTo( $form_id ) { 
  105. if ( empty ( $form_id ) ) 
  106. return false; 
  107.  
  108. // Remove any "Admin mailto" settings we might have. 
  109. $form_row = ninja_forms_get_form_by_id( $form_id ); 
  110.  
  111. if ( isset ( $form_row['data']['admin_mailto'] ) ) { 
  112. unset ( $form_row['data']['admin_mailto'] ); 
  113.  
  114. $args = array( 
  115. 'update_array' => array( 
  116. 'data' => serialize( $form_row['data'] ),  
  117. ),  
  118. 'where' => array( 
  119. 'id' => $form_id,  
  120. ),  
  121. ); 
  122.  
  123. ninja_forms_update_form( $args ); 
  124.  
  125.  
  126. // Update any old email settings we have. 
  127. $fields = Ninja_Forms()->form( $form_id )->fields; 
  128.  
  129. // Create a notification for our user email 
  130. if ( ! empty ( $fields ) ) { 
  131. foreach ( $fields as $field_id => $field ) { 
  132. if ( isset ( $field['data']['send_email'] ) && $field['data']['send_email'] == 1 ) { 
  133. // Add this field to our $addresses variable. 
  134. unset( $field['data']['send_email'] ); 
  135. unset( $field['data']['replyto_email'] ); 
  136. unset( $field['data']['from_name'] ); 
  137.  
  138. $args = array( 
  139. 'update_array' => array( 
  140. 'data' => serialize( $field['data'] ),  
  141. ),  
  142. 'where' => array( 
  143. 'id' => $field_id,  
  144. ),  
  145. ); 
  146.  
  147. ninja_forms_update_field( $args );