NF_Notification_Email

Class for our email notification type.

Defined (1)

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

/deprecated/classes/notification-email.php  
  1. class NF_Notification_Email extends NF_Notification_Base_Type 
  2.  
  3. /** 
  4. * Get things rolling 
  5. */ 
  6. function __construct() { 
  7. $this->name = __( 'Email', 'ninja-forms' ); 
  8.  
  9. /** 
  10. * Output our edit screen 
  11. * @access public 
  12. * @since 2.8 
  13. * @return void 
  14. */ 
  15. public function edit_screen( $id = '' ) { 
  16. $form_id = ( '' != $id ) ? Ninja_Forms()->notification( $id )->form_id : ''; 
  17.  
  18. if ( $id == '' ) { 
  19. $email_format = 'html'; 
  20. $from_name = get_bloginfo( 'name' ); 
  21. $from_address = get_bloginfo( 'admin_email' ); 
  22. $reply_to = ''; 
  23. $to = ''; 
  24. $cc = ''; 
  25. $bcc = ''; 
  26. $email_subject = ''; 
  27. $email_message = ''; 
  28. } else { 
  29. $email_format = Ninja_Forms()->notification( $id )->get_setting( 'email_format' ); 
  30. $from_name = Ninja_Forms()->notification( $id )->get_setting( 'from_name' ); 
  31. $from_address = Ninja_Forms()->notification( $id )->get_setting( 'from_address' ); 
  32. $reply_to = Ninja_Forms()->notification( $id )->get_setting( 'reply_to' ); 
  33. $to = Ninja_Forms()->notification( $id )->get_setting( 'to' ); 
  34. $cc = Ninja_Forms()->notification( $id )->get_setting( 'cc' ); 
  35. $bcc = Ninja_Forms()->notification( $id )->get_setting( 'bcc' ); 
  36. $email_subject = Ninja_Forms()->notification( $id )->get_setting( 'email_subject' ); 
  37. $email_message = Ninja_Forms()->notification( $id )->get_setting( 'email_message' ); 
  38.  
  39. ?> 
  40. <tr> 
  41. <th scope="row"><label for="settings-from_name"><?php _e( 'From Name', 'ninja-forms' ); ?></label></th> 
  42. <td> 
  43. <input name="settings[from_name]" type="text" id="settings-from_name" value="<?php echo $from_name; ?>" class="nf-tokenize" placeholder="<?php _e( 'Name or fields', 'ninja-forms' ); ?>" data-token-limit="0" data-key="from_name" data-type="all" /> 
  44. <span class="howto"><?php _e( 'Email will appear to be from this name.', 'ninja-forms' ) ?></span> 
  45. </td> 
  46. </tr> 
  47. <tr> 
  48. <th scope="row"><label for="settings-from_address"><?php _e( 'From Address', 'ninja-forms' ); ?></label></th> 
  49. <td> 
  50. <input name="settings[from_address]" type="text" id="settings-from_address" value="<?php echo $from_address; ?>" class="nf-tokenize" placeholder="<?php _e( 'One email address or field', 'ninja-forms' ); ?>" data-token-limit="1" data-key="from_address" data-type="all" /> 
  51. <span class="howto"><?php _e( 'Email will appear to be from this email address.', 'ninja-forms' ) ?></span> 
  52. </td> 
  53. </tr> 
  54. <tr> 
  55. <th scope="row"><label for="settings-to"><?php _e( 'To', 'ninja-forms' ); ?></label></th> 
  56. <td> 
  57. <input name="settings[to]" type="text" id="settings-to" value="<?php echo $to; ?>" class="nf-tokenize" placeholder="<?php _e( 'Email addresses or search for a field', 'ninja-forms' ); ?>" data-token-limit="0" data-key="to" data-type="all" /> 
  58. <span class="howto"><?php _e( 'Who should this email be sent to?', 'ninja-forms' ) ?></span> 
  59. </td> 
  60. </tr> 
  61. <tr> 
  62. <th scope="row"><label for="settings-email_subject"><?php _e( 'Subject', 'ninja-forms' ); ?></label></th> 
  63. <td> 
  64. <input name="settings[email_subject]" type="text" id="settings-email_subject" value="<?php echo $email_subject; ?>" class="nf-tokenize" placeholder="<?php _e( 'Subject Text or search for a field', 'ninja-forms' ); ?>" data-token-limit="0" data-key="email_subject" data-type="all" /> 
  65. <span class="howto"><?php _e( 'This will be the subject of the email.', 'ninja-forms' ) ?></span> 
  66. </td> 
  67. </tr> 
  68. <tr> 
  69. <th scope="row"><label for="settings-email_message"><?php _e( 'Email Message', 'ninja-forms' ); ?></label></th> 
  70. <td> 
  71. <?php 
  72. $settings = array( 
  73. 'textarea_name' => 'settings[email_message]',  
  74. ); 
  75. wp_editor( $email_message, 'email_message', $settings ); 
  76. ?> 
  77. </td> 
  78. </tr> 
  79. <tr> 
  80. <th scope="row"><label for="settings-attachments"><?php _e( 'Attachments', 'ninja-forms' ); ?></label></th> 
  81. <td> 
  82. <ul> 
  83. <?php 
  84. $attachment_types = apply_filters( 'nf_email_notification_attachment_types',  
  85. array( 
  86. 'attach_csv' => __( 'Submission CSV', 'ninja-forms' ),  
  87. ); 
  88.  
  89. foreach ( $attachment_types as $slug => $nicename ) { 
  90. if ( '' == $id ) { 
  91. $current_setting = 0; 
  92. } else { 
  93. $current_setting = Ninja_Forms()->notification( $id )->get_setting( $slug ); 
  94.  
  95. ?> 
  96. <li> 
  97. <input name="settings[<?php echo $slug; ?>]" type="hidden" value="0"> 
  98. <input name="settings[<?php echo $slug; ?>]" type="checkbox" id="settings-<?php echo $slug; ?>" value="1" <?php checked( $current_setting, 1 ); ?>/> 
  99. <label for="settings-<?php echo $slug; ?>"><?php echo $nicename; ?></label> 
  100. </li> 
  101.  
  102. <?php 
  103. ?> 
  104. </ul> 
  105. </td> 
  106. </tr> 
  107. <tr> 
  108. <th scope="row"><a href="#" id="toggle_email_advanced"><?php _e( 'Advanced Settings', 'ninja-forms' ); ?></a></th> 
  109. </tr> 
  110. <tbody id="email_advanced" style="display:none;"> 
  111. <tr> 
  112. <th scope="row"><label for="settings-email_format"><?php _e( 'Format', 'ninja-forms' ); ?></label></th> 
  113. <td> 
  114. <select name="settings[email_format]" id="settings-email_format" data-key="email_format"/> 
  115. <option value="html" <?php selected( $email_format, 'html' ); ?>><?php _e( 'HTML', 'ninja-forms' ); ?></option> 
  116. <option value="plain" <?php selected( $email_format, 'plain' ); ?>><?php _e( 'Plain Text', 'ninja-forms' ); ?></option> 
  117. </select> 
  118. </td> 
  119. </tr> 
  120. <tr> 
  121. <th scope="row"><label for="settings-reply_to"><?php _e( 'Reply To', 'ninja-forms' ); ?></label></th> 
  122. <td> 
  123. <input name="settings[reply_to]" type="text" id="settings-reply_to" value="<?php echo $reply_to; ?>" class="nf-tokenize" placeholder="<?php __( 'One email address or field', 'ninja-forms' ); ?>" data-token-limit="1" data-key="reply_to" data-type="all" /> 
  124. </td> 
  125. </tr> 
  126. <tr> 
  127. <th scope="row"><label for="settings-cc"><?php _e( 'Cc', 'ninja-forms' ); ?></label></th> 
  128. <td> 
  129. <input name="settings[cc]" type="text" id="settings-cc" value="<?php echo $cc; ?>" class="nf-tokenize" placeholder="<?php __( 'Email addresses or search for a field', 'ninja-forms' ); ?>" data-token-limit="0" data-key="cc" data-type="all" /> 
  130. </td> 
  131. </tr> 
  132. <tr> 
  133. <th scope="row"><label for="settings-bcc"><?php _e( 'Bcc', 'ninja-forms' ); ?></label></th> 
  134. <td> 
  135. <input name="settings[bcc]" type="text" id="settings-bcc" value="<?php echo $bcc; ?>" class="nf-tokenize" placeholder="<?php __( 'Email addresses or search for a field', 'ninja-forms' ); ?>" data-token-limit="0" data-key="bcc" data-type="all" /> 
  136. </td> 
  137. </tr> 
  138. </tbody> 
  139. <?php 
  140.  
  141. do_action( 'nf_email_notification_after_settings', $id ); 
  142.  
  143. if ( '' != $form_id ) { 
  144. $from_name = $this->get_value( $id, 'from_name', $form_id ); 
  145. $from_address = $this->get_value( $id, 'from_address', $form_id ); 
  146. $reply_to = $this->get_value( $id, 'reply_to', $form_id ); 
  147. $to = $this->get_value( $id, 'to', $form_id ); 
  148. $cc = $this->get_value( $id, 'cc', $form_id ); 
  149. $bcc = $this->get_value( $id, 'bcc', $form_id ); 
  150. $email_subject = $this->get_value( $id, 'email_subject', $form_id ); 
  151. } else { 
  152. $from_name = get_bloginfo( 'name' ); 
  153. $from_address = get_bloginfo( 'admin_email' ); 
  154. $reply_to = ''; 
  155. $to = ''; 
  156. $cc = ''; 
  157. $bcc = ''; 
  158. $email_subject = ''; 
  159.  
  160. ?> 
  161.  
  162. <script type="text/javascript"> 
  163. nf_notifications.tokens['from_name'] = <?php echo json_encode( $from_name ); ?>; 
  164. nf_notifications.tokens['from_address'] = <?php echo json_encode( $from_address ); ?>; 
  165. nf_notifications.tokens['reply_to'] = <?php echo json_encode( $reply_to ); ?>; 
  166. nf_notifications.tokens['to'] = <?php echo json_encode( $to ); ?>; 
  167. nf_notifications.tokens['cc'] = <?php echo json_encode( $cc ); ?>; 
  168. nf_notifications.tokens['bcc'] = <?php echo json_encode( $bcc ); ?>; 
  169. nf_notifications.tokens['email_subject'] = <?php echo json_encode( $email_subject ); ?>; 
  170. </script> 
  171.  
  172. <?php 
  173.  
  174. /** 
  175. * Get our input value labels 
  176. * @access public 
  177. * @since 2.8 
  178. * @return string $label 
  179. */ 
  180. public function get_value( $id, $meta_key, $form_id ) { 
  181. $meta_value = nf_get_object_meta_value( $id, $meta_key ); 
  182. $meta_value = explode( '`', $meta_value ); 
  183.  
  184. $return = array(); 
  185. foreach( $meta_value as $val ) { 
  186. if ( strpos( $val, 'field_' ) !== false ) { 
  187. $val = str_replace( 'field_', '', $val ); 
  188. $label = nf_get_field_admin_label( $val, $form_id ); 
  189. if ( strlen( $label ) > 30 ) { 
  190. $label = substr( $label, 0, 30 ); 
  191.  
  192. $return[] = array( 'value' => 'field_' . $val, 'label' => $label . ' - ID: ' . $val ); 
  193. } else { 
  194. $return[] = array( 'value' => $val, 'label' => $val ); 
  195.  
  196. return $return; 
  197.  
  198. /** 
  199. * Process our Email notification 
  200. * @access public 
  201. * @since 2.8 
  202. * @return void 
  203. */ 
  204. public function process( $id ) { 
  205. global $ninja_forms_processing; 
  206.  
  207. $form_id = $ninja_forms_processing->get_form_ID(); 
  208. $form_title = $ninja_forms_processing->get_form_setting( 'form_title' ); 
  209.  
  210. $email_format = Ninja_Forms()->notification( $id )->get_setting( 'email_format' ); 
  211. $attach_csv = Ninja_Forms()->notification( $id )->get_setting( 'attach_csv' ); 
  212.  
  213. $from_name = $this->process_setting( $id, 'from_name' ); 
  214. $from_name = implode( ' ', $from_name ); 
  215.  
  216. $from_address = $this->process_setting( $id, 'from_address' ); 
  217. $from_address = $from_address[0]; 
  218.  
  219. $reply_to = $this->process_setting( $id, 'reply_to' ); 
  220. $reply_to = $reply_to[0]; 
  221. $to = $this->process_setting( $id, 'to' ); 
  222. $cc = $this->process_setting( $id, 'cc' ); 
  223. // $cc = $cc[0]; 
  224. $bcc = $this->process_setting( $id, 'bcc' ); 
  225. // $bcc = $bcc[0]; 
  226.  
  227. if ( empty ( $from_name ) ) 
  228. $from_name = 'WordPress'; 
  229.  
  230. if ( empty ( $from_address ) ) { 
  231. $admin_email = get_bloginfo( 'admin_email' ); 
  232. $from_address = $admin_email; 
  233.  
  234. $email_from = $from_name.' <'.$from_address.'>'; 
  235.  
  236. $subject = $this->process_setting( $id, 'email_subject' ); 
  237. $subject = $this->flatten_array_recursive( ' ', $subject ); 
  238.  
  239. if ( empty( $subject ) ) { 
  240. $subject = $form_title; 
  241. } elseif( is_array( $subject ) ) { 
  242. $subject = implode( ', ', $subject ); 
  243.  
  244. $message = $this->process_setting( $id, 'email_message' ); 
  245. if ( is_array ( $message ) ) 
  246. $message = $message[0]; 
  247.  
  248. if ( $email_format != 'plain' ) 
  249. $message = apply_filters( 'ninja_forms_admin_email_message_wpautop', wpautop( $message ) ); 
  250.  
  251. if ( empty ( $message ) ) 
  252. $message = ' '; 
  253.  
  254. $headers = array(); 
  255. $headers[] = 'From: ' . $email_from; 
  256.  
  257. if( ! empty( $reply_to ) ) { 
  258. $headers[] = sprintf('Reply-To: "%s" <%s>', $from_name, $reply_to); 
  259. $headers[] = 'Content-Type: text/' . $email_format; 
  260. $headers[] = 'charset=utf-8'; 
  261.  
  262. if ( ! empty( $cc ) ) { 
  263. foreach ($cc as $ccemail) { 
  264. if ( ! empty( $ccemail ) ) { 
  265. $headers[] = 'Cc: ' . $ccemail; 
  266.  
  267. if ( ! empty( $bcc ) ) { 
  268. foreach ($bcc as $bccemail) { 
  269. if ( ! empty( $bccemail ) ) { 
  270. $headers[] = 'Bcc: ' . $bccemail; 
  271.  
  272. $csv_attachment = ''; 
  273. $attachments = apply_filters( 'nf_email_notification_attachments', array(), $id ); 
  274.  
  275. // Check to see if we need to attach a CSV 
  276. if ( 1 == $attach_csv ) { 
  277. // Create our attachment 
  278.  
  279. // Get our submission ID 
  280. $sub_id = $ninja_forms_processing->get_form_setting( 'sub_id' ); 
  281.  
  282. // create CSV content 
  283. $csv_content = Ninja_Forms()->sub( $sub_id )->export( true ); 
  284.  
  285. $upload_dir = wp_upload_dir(); 
  286. $path = trailingslashit( $upload_dir['path'] ); 
  287.  
  288. // create temporary file 
  289. $path = tempnam( $path, 'Sub' ); 
  290. $temp_file = fopen( $path, 'r+' ); 
  291.  
  292. // write to temp file 
  293. fwrite( $temp_file, $csv_content ); 
  294. fclose( $temp_file ); 
  295.  
  296. // find the directory we will be using for the final file 
  297. $path = pathinfo( $path ); 
  298. $dir = $path['dirname']; 
  299. $basename = $path['basename']; 
  300.  
  301. // create name for file 
  302. $new_name = apply_filters( 'ninja_forms_submission_csv_name', 'ninja-forms-submission' ); 
  303.  
  304. // remove a file if it already exists 
  305. if( file_exists( $dir.'/'.$new_name.'.csv' ) ) { 
  306. unlink( $dir.'/'.$new_name.'.csv' ); 
  307.  
  308. // move file 
  309. rename( $dir.'/'.$basename, $dir.'/'.$new_name.'.csv' ); 
  310. $csv_attachment = $dir.'/'.$new_name.'.csv'; 
  311.  
  312. $attachments[] = $csv_attachment; 
  313.  
  314. if ( is_array( $to ) AND !empty( $to ) ) { 
  315.  
  316. $to = explode( ", ", $this->flatten_array_recursive( ', ', $to ) ); 
  317.  
  318. wp_mail( $to, $subject, $message, $headers, $attachments ); 
  319.  
  320. // Delete our admin CSV if one is present. 
  321. if ( file_exists( $csv_attachment ) ) { 
  322. //unlink ( $csv_attachment ); 
  323.  
  324. public function flatten_array_recursive( $glue = ', ', array $array = array() ) { 
  325.  
  326. $return = array(); 
  327.  
  328. foreach ( $array as $value ) { 
  329.  
  330. if ( is_array( $value ) ) { 
  331.  
  332. $return[] = $this->flatten_array_recursive( $glue, $value ); 
  333.  
  334. } else { 
  335.  
  336. $return[] = $value; 
  337.  
  338.  
  339.  
  340. return implode( $glue, $return ); 
  341.  
  342.  
  343. /** 
  344. * Explode our settings by ` and extract each value. 
  345. * Check to see if the setting is a field; if it is, assign the value. 
  346. * Run shortcodes and return the result. 
  347. * @access public 
  348. * @since 2.8 
  349. * @return array $setting 
  350. */ 
  351. public function process_setting( $id, $setting, $html = 0 ) { 
  352.  
  353. // save the setting name 
  354. $setting_name = $setting; 
  355.  
  356. $format = Ninja_Forms()->notification( $id )->get_setting( 'email_format' ); 
  357. if ( 'html' == $format ) 
  358. $html = 1; 
  359.  
  360. // call parent process setting method 
  361. $setting = parent::process_setting( $id, $setting, $html ); 
  362.  
  363. // gotta keep the old filter in case anyone was using it. 
  364. return apply_filters( 'nf_email_notification_process_setting', $setting, $setting_name, $id ); 
  365.