NF_Upgrade_Notifications

The Ninja Forms NF Upgrade Notifications class.

Defined (1)

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

/deprecated/includes/admin/upgrades/convert-notifications.php  
  1. final class NF_Upgrade_Notifications extends NF_Upgrade 
  2. public $name = 'notifications'; 
  3.  
  4. public $priority = '2.9.2'; 
  5.  
  6. public $description = 'An update is necessary for the new Emails & Actions tab to function properly. This new section gives the user much more control over what happens when a form is submitted.'; 
  7.  
  8. public $completed_forms = array(); 
  9.  
  10. public $args = array(); 
  11.  
  12. public $errors = array(); 
  13.  
  14. public function loading() 
  15. global $wpdb; 
  16.  
  17. $this->removeOldEmailSettings(); 
  18.  
  19. $form_count = $this->getFormCount(); 
  20.  
  21. $forms = $this->getAllForms(); 
  22.  
  23. $x = 1; 
  24. if ( is_array( $forms ) ) { 
  25. foreach ( $forms as $form ) { 
  26. $this->args['forms'][$x] = $form['id']; 
  27. $x++; 
  28.  
  29. if( empty( $this->total_steps ) || $this->total_steps <= 1 ) { 
  30. $this->total_steps = $form_count; 
  31.  
  32. public function _beforeStep( $step ) 
  33. // Get a list of forms that we've already converted. 
  34. $this->completed_forms = get_option( 'nf_convert_notifications_forms', array() ); 
  35.  
  36. if ( ! is_array( $this->completed_forms ) ) { 
  37. $this->completed_forms = array(); 
  38.  
  39. public function step( $step ) 
  40. global $ninja_forms_fields; 
  41.  
  42. // Get our form ID 
  43. $form_id = $this->args['forms'][ $step ]; 
  44.  
  45. // Bail if we've already converted the notifications for this form. 
  46. if ( in_array( $form_id, $this->completed_forms ) ) 
  47. return false; 
  48.  
  49. // Grab our form from the database 
  50. $form_settings = Ninja_Forms()->form( $form_id )->settings; 
  51.  
  52. $fields = Ninja_Forms()->form( $form_id )->fields; 
  53.  
  54. $process_fields = array(); 
  55. foreach( $fields as $field_id => $field ) { 
  56. $label = strip_tags( nf_get_field_admin_label( $field_id ) ); 
  57.  
  58. if ( strlen( $label ) > 30 ) { 
  59. $tmp_label = substr( $label, 0, 30 ); 
  60. } else { 
  61. $tmp_label = $label; 
  62.  
  63. $tmp_array = array( 'value' => $field_id, 'label' => $tmp_label . ' - ID: ' . $field_id ); 
  64.  
  65. $admin_label = $label; 
  66.  
  67. $label = isset( $field['data']['label'] ) ? $field['data']['label'] : ''; 
  68.  
  69. // Check to see if this field is supposed to be "processed" 
  70. $type = $field['type']; 
  71. if ( isset ( $ninja_forms_fields[ $type ]['process_field'] ) && $ninja_forms_fields[ $type ]['process_field'] ) { 
  72. $process_fields[ $field_id ] = array( 'field_id' => $field_id, 'label' => $label, 'admin_label' => $admin_label ); 
  73.  
  74. // Create a notification for our admin email 
  75. if ( ( isset ( $form_settings['admin_email_msg'] ) && ! empty ( $form_settings['admin_email_msg'] ) ) || ( isset ( $form_settings['admin_email_fields'] ) && 1 == $form_settings['admin_email_fields'] ) ) { 
  76.  
  77. // Create a notification 
  78. $n_id = nf_insert_notification( $form_id ); 
  79.  
  80. // Update our notification type 
  81. nf_update_object_meta( $n_id, 'type', 'email' ); 
  82.  
  83. // Activate our notification 
  84. Ninja_Forms()->notification( $n_id )->activate(); 
  85.  
  86. // Update our notification name 
  87. Ninja_Forms()->notification( $n_id )->update_setting( 'name', __( 'Admin Email', 'ninja-forms' ) ); 
  88.  
  89. $admin_mailto = isset ( $form_settings['admin_mailto'] ) ? $form_settings['admin_mailto'] : array(); 
  90.  
  91. // Implode our admin email addresses 
  92. $to = implode('`', $admin_mailto ); 
  93. // Update our to setting 
  94. Ninja_Forms()->notification( $n_id )->update_setting( 'to', $to ); 
  95.  
  96. // Update our Format Setting 
  97. Ninja_Forms()->notification( $n_id )->update_setting( 'email_format', $form_settings['email_type'] ); 
  98.  
  99. // Update our attach csv option 
  100. Ninja_Forms()->notification( $n_id )->update_setting( 'attach_csv', $form_settings['admin_attach_csv'] ); 
  101.  
  102. // Update our subject 
  103. $subject = $this->replace_shortcodes( $form_settings['admin_subject'] ); 
  104.  
  105. Ninja_Forms()->notification( $n_id )->update_setting( 'email_subject', $subject ); 
  106.  
  107. // Update our From Name 
  108. if ( isset ( $form_settings['email_from_name'] ) ) { 
  109. Ninja_Forms()->notification( $n_id )->update_setting( 'from_name', $form_settings['email_from_name'] ); 
  110.  
  111. foreach ( $fields as $field ) { 
  112. if ( isset ( $field['data']['from_name'] ) && $field['data']['from_name'] == 1 ) { 
  113. // Update our From Name 
  114. Ninja_Forms()->notification( $n_id )->update_setting( 'from_name', 'field_' . $field['id'] ); 
  115. break; 
  116.  
  117. // Update our From Address 
  118. Ninja_Forms()->notification( $n_id )->update_setting( 'from_address', $form_settings['email_from'] ); 
  119.  
  120. // Get our reply-to address 
  121. foreach ( $fields as $field ) { 
  122. if ( isset ( $field['data']['replyto_email'] ) && $field['data']['replyto_email'] == 1 ) { 
  123. Ninja_Forms()->notification( $n_id )->update_setting( 'reply_to', 'field_' . $field['id'] ); 
  124. break; 
  125.  
  126. $email_message = $form_settings['admin_email_msg']; 
  127.  
  128. // Check to see if the "include list of fields" checkbox was checked. 
  129. if ( isset ( $form_settings['admin_email_fields'] ) && $form_settings['admin_email_fields'] == 1 ) { 
  130. $email_message .= '<br />[ninja_forms_all_fields]'; 
  131.  
  132. // Update our email message 
  133. Ninja_Forms()->notification( $n_id )->update_setting( 'email_message', $email_message ); 
  134.  
  135. Ninja_Forms()->notification( $n_id )->update_setting( 'admin_email', true ); 
  136.  
  137. // Create a notification for our user email 
  138. if ( ! empty ( $fields ) ) { 
  139. $addresses = array(); 
  140. foreach ( $fields as $field_id => $field ) { 
  141. if ( isset ( $field['data']['send_email'] ) && $field['data']['send_email'] == 1 ) { 
  142. // Add this field to our $addresses variable. 
  143. $addresses[] = 'field_' . $field_id; 
  144. unset( $field['data']['send_email'] ); 
  145. unset( $field['data']['replyto_email'] ); 
  146. unset( $field['data']['from_name'] ); 
  147.  
  148. $args = array( 
  149. 'update_array' => array( 
  150. 'data' => serialize( $field['data'] ),  
  151. ),  
  152. 'where' => array( 
  153. 'id' => $field_id,  
  154. ),  
  155. ); 
  156.  
  157. ninja_forms_update_field( $args ); 
  158.  
  159. if ( ! empty ( $addresses ) ) { 
  160. // We have a user email, so create a notification 
  161. $n_id = nf_insert_notification( $form_id ); 
  162.  
  163. // Update our notification type 
  164. nf_update_object_meta( $n_id, 'type', 'email' ); 
  165.  
  166. // Activate our notification 
  167. Ninja_Forms()->notification( $n_id )->activate(); 
  168.  
  169. // Update our notification name 
  170. Ninja_Forms()->notification( $n_id )->update_setting( 'name', __( 'User Email', 'ninja-forms' ) ); 
  171.  
  172. // Implode our admin email addresses 
  173. $n_var = count ( $addresses ) > 1 ? 'bcc' : 'to'; 
  174. $addresses = implode( '`', $addresses ); 
  175. Ninja_Forms()->notification( $n_id )->update_setting( $n_var, $addresses ); 
  176.  
  177. // Update our Format Setting 
  178. Ninja_Forms()->notification( $n_id )->update_setting( 'email_format', $form_settings['email_type'] ); 
  179.  
  180. // Update our subject 
  181. $subject = $this->replace_shortcodes( $form_settings['user_subject'] ); 
  182.  
  183. Ninja_Forms()->notification( $n_id )->update_setting( 'email_subject', $subject ); 
  184.  
  185. // Update our From Name 
  186. if ( isset ( $form_settings['email_from_name'] ) ) { 
  187. Ninja_Forms()->notification( $n_id )->update_setting( 'from_name', $form_settings['email_from_name'] ); 
  188.  
  189. // Update our From Address 
  190. Ninja_Forms()->notification( $n_id )->update_setting( 'from_address', $form_settings['email_from'] ); 
  191.  
  192. $email_message = $form_settings['user_email_msg']; 
  193.  
  194. // Check to see if the "include list of fields" checkbox was checked. If so, add our table to the end of the email message. 
  195. if ( isset ( $form_settings['user_email_fields'] ) && $form_settings['user_email_fields'] == 1 ) { 
  196. $email_message .= '<br />[ninja_forms_all_fields]'; 
  197.  
  198. // Update our email message 
  199. Ninja_Forms()->notification( $n_id )->update_setting( 'email_message', $email_message ); 
  200.  
  201. Ninja_Forms()->notification( $n_id )->update_setting( 'user_email', true ); 
  202.  
  203. if ( isset ( $form_settings['success_msg'] ) && ! empty( $form_settings['success_msg'] ) ) { 
  204. // Create a notification for our success message 
  205. $n_id = nf_insert_notification( $form_id ); 
  206. nf_update_object_meta( $n_id, 'name', __( 'Success Message', 'ninja-forms' ) ); 
  207. nf_update_object_meta( $n_id, 'type', 'success_message' ); 
  208. nf_update_object_meta( $n_id, 'active', 1 ); 
  209. nf_update_object_meta( $n_id, 'success_msg', $form_settings['success_msg'] ); 
  210.  
  211. if ( isset ( $form_settings['landing_page'] ) && ! empty( $form_settings['landing_page'] ) ) { 
  212. // Create a notification for our redirect 
  213. $n_id = nf_insert_notification( $form_id ); 
  214. nf_update_object_meta( $n_id, 'name', __( 'Redirect', 'ninja-forms' ) ); 
  215. nf_update_object_meta( $n_id, 'type', 'redirect' ); 
  216. nf_update_object_meta( $n_id, 'active', 1 ); 
  217. nf_update_object_meta( $n_id, 'redirect_url', $form_settings['landing_page'] ); 
  218.  
  219. $completed_forms = get_option( 'nf_convert_notifications_forms' ); 
  220. if ( ! is_array( $completed_forms ) || empty ( $completed_forms ) ) { 
  221. $completed_forms = array( $form_id ); 
  222. } else { 
  223. $completed_forms[] = $form_id; 
  224. update_option( 'nf_convert_notifications_forms', $completed_forms ); 
  225.  
  226. // Unset our admin email settings and save the form. 
  227. if ( isset ( $form_settings['admin_mailto'] ) ) { 
  228. unset( $form_settings['admin_mailto'] ); 
  229. unset( $form_settings['admin_attach_csv'] ); 
  230. unset( $form_settings['admin_subject'] ); 
  231. unset( $form_settings['admin_email_msg'] ); 
  232. unset( $form_settings['admin_email_fields'] ); 
  233. unset( $form_settings['success_msg'] ); 
  234. unset( $form_settings['user_email_msg'] ); 
  235. unset( $form_settings['user_subject'] ); 
  236. unset( $form_settings['landing_page'] ); 
  237.  
  238. $args = array( 
  239. 'update_array' => array( 
  240. 'data' => serialize( $form_settings ),  
  241. ),  
  242. 'where' => array( 
  243. 'id' => $form_id,  
  244. ),  
  245. ); 
  246.  
  247. ninja_forms_update_form( $args ); 
  248.  
  249. public function complete() 
  250. update_option( 'nf_convert_notifications_complete', true ); 
  251.  
  252. public function isComplete() 
  253. return get_option( 'nf_convert_notifications_complete', false ); 
  254.  
  255. /** 
  256. * PRIVATE METHODS 
  257. */ 
  258.  
  259. private function removeOldEmailSettings() 
  260. nf_change_email_fav(); 
  261. nf_remove_old_email_settings(); 
  262.  
  263. private function getFormCount() 
  264. $forms = Ninja_Forms()->forms()->get_all(); 
  265. return count( $forms ); 
  266.  
  267. private function getAllForms() 
  268. $forms = Ninja_Forms()->forms()->get_all(); 
  269.  
  270. $tmp_array = array(); 
  271. $x = 0; 
  272. foreach ( $forms as $form_id ) { 
  273. $tmp_array[ $x ]['id'] = $form_id; 
  274. $tmp_array[ $x ]['data'] = Ninja_Forms()->form( $form_id )->get_all_settings(); 
  275. $tmp_array[ $x ]['name'] = Ninja_Forms()->form( $form_id )->get_setting( 'form_title' ); 
  276. $x++; 
  277.  
  278. return $tmp_array; 
  279.  
  280. private function replace_shortcodes( $text ) { 
  281. $matches = array(); 
  282. $pattern = get_shortcode_regex(); 
  283. preg_match_all('/'.$pattern.'/s', $text, $matches); 
  284. if ( is_array( $matches ) && ! empty( $matches[2] ) ) { 
  285. foreach ( $matches[2] as $key => $shortcode ) { 
  286. if ( $shortcode == 'ninja_forms_field' ) { 
  287. if ( isset ( $matches[3][ $key ] ) ) { 
  288. $atts = shortcode_parse_atts( $matches[3][ $key ] ); 
  289. $id = $atts['id']; 
  290. $text = str_replace( $matches[0][ $key ], '`field_' . $id . '`', $text ); 
  291. return $text;