notification-email.php 14 KB

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