PageRenderTime 20ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-email-user-newsletter.php

https://gitlab.com/rahulw.techspawn/wp-email-users
PHP | 157 lines | 139 code | 8 blank | 10 comment | 7 complexity | 6cac4a25f6154cc600de68cb4d1915cf MD5 | raw file
  1. <?php
  2. function weu_admin_send_newsletter(){
  3. global $current_user, $wpdb, $wp_roles;
  4. $user_roles = $current_user->roles;
  5. $roles = $wp_roles->get_names();
  6. if($user_roles[0]=='administrator') {
  7. wp_enqueue_script( 'wp-email-user-datatable-script', plugins_url('js/jquery.dataTables.min.js', __FILE__ ), array(), '1.0.0', false );
  8. wp_enqueue_script( 'wp-email-user-script', plugins_url('js/email-admin.js', __FILE__ ), array(), '1.0.0', false );
  9. wp_enqueue_style( 'wp-email-user-style', plugins_url('css/style.css', __FILE__ ) );
  10. wp_enqueue_style( 'wp-email-user-datatable-style', plugins_url('css/jquery.dataTables.min.css', __FILE__ ) );
  11. $wau_to = array();
  12. if( isset($_POST['ea_user_name'])){
  13. for($j=0;$j<count($_POST['ea_user_name']);$j++){
  14. $user= $_POST['ea_user_name'][$j];
  15. array_push($wau_to,$_POST[$user]);
  16. }
  17. }
  18. /* Send Mail to user using wp_mail */
  19. $wau_status = 2;
  20. $wau_too = array();
  21. $temp_key=isset($_POST['wau_sub'])?$_POST['wau_sub']:'';
  22. $chk_val=isset($_POST['temp'])?$_POST['temp']:'';
  23. $table_name = $wpdb->prefix.'email_user';
  24. if($chk_val==1)
  25. $wpdb->query($wpdb->prepare( "INSERT INTO `".$table_name."`(`template_key`, `template_value`, `status`) VALUES (%s,%s,%s)
  26. ",
  27. $temp_key,stripcslashes($_POST['wau_mailcontent']),'template'));
  28. for($j=0;$j<count($wau_to);$j++){
  29. $table_name = $wpdb->prefix.'weu_subscribers';
  30. //Fetching Values from weu_subscribers table with unique email id $wau_to[$j]
  31. $curr_email_data = $wpdb->get_results( "SELECT * FROM $table_name WHERE email = '$wau_to[$j]' ");
  32. foreach ($curr_email_data as $keyy) {
  33. //Calling function to generate link of unsubscribtion
  34. $list = 'default';
  35. $unsubscribe_link_for_email = weu_userUnsubscibe($keyy->id,$keyy->email,$list);
  36. //Replacing Tags like - [[unsubscribe]] with actual value from table like - $keyy->name (User Name)
  37. $replace= array(
  38. $unsubscribe_link_for_email,
  39. $keyy->name,
  40. $keyy->email,
  41. );
  42. $find = array(
  43. '[[unsubscribe]]',
  44. '[[subscriber-name]]',
  45. '[[subscriber-email]]'
  46. );
  47. }
  48. $mail_body = str_replace( $find, $replace, $_POST['wau_mailcontent'] );
  49. $subject = $_POST['wau_sub'];
  50. $body = stripslashes($mail_body);
  51. $from_email=sanitize_email($_POST['wau_from']);
  52. $from_name=sanitize_text_field($_POST['wau_from_name']);
  53. sanitize_text_field( $body );
  54. $headers[] = 'From: '.$from_name.' <'. $from_email.'>';
  55. $headers[] = 'Content-Type: text/html; charset="UTF-8"';
  56. $wau_to[$j]." - Echo wau_to</br>";
  57. $subject." - Subject</br>";
  58. $body." - Body</br>";
  59. $wau_status = wp_mail($wau_to[$j], $subject, $body, $headers);
  60. } // for ends
  61. if( $wau_status==1 ){
  62. echo '<div id="message" class="updated notice is-dismissible"><p>Your message has been sent successfully.</p><button type="button" class="notice-dismiss"><span class="screen-reader-text">Dismiss this notice.</span></button></div>';
  63. } elseif( $wau_status==0 ){
  64. echo '<div id="message" class="updated notice is-dismissible error"><p> Sorry,your message has not sent.</p><button type="button" class="notice-dismiss"><span class="screen-reader-text">Dismiss this notice.</span></button></div>';
  65. }
  66. $table_name = $wpdb->prefix.'weu_subscribers';
  67. $wau_subscriber = $wpdb->get_results( "SELECT * FROM $table_name" );
  68. echo "<div class='wrap'>";
  69. echo "<h2>Send Newsletter to Subscribers</h2>";
  70. echo "</div>";
  71. echo "<p>Send email to Subscribers who subscribed through WP Email Users Subscribe widget.</p>";
  72. echo '<form name="myform" class="wau_form" method="POST" action="#" onsubmit="return validation()" >';
  73. /* User role */
  74. echo '<table id="" class="form-table" >';
  75. echo '<tbody>';
  76. echo '<tr>';
  77. echo '<th>From Name</th> <td colspan="1"><input type="text" name="wau_from_name" value="'.$current_user->display_name.'" class="wau_boxlen" id="wau_from_name" required></td>';
  78. echo '</tr>';
  79. echo '<tr>';
  80. echo '<th>From Email</th> <td colspan="2"><input type="text" name="wau_from" value="'.$current_user->user_email.'" class="wau_boxlen" id="wau_from" required></td>';
  81. echo '</tr>';
  82. /**
  83. * Select Users
  84. **/
  85. echo '<tr class="wau_user_toggle">';
  86. echo "<th><b>Send Email To &nbsp; </b></th>";
  87. echo '<td colspan="3">';
  88. echo '<table id="example" class="display alluser_datatable" cellspacing="0" width="100%">
  89. <thead>
  90. <tr style="text-align:left"> <th style="text-align:center" ><input name="select_all" value="1" id="example-select-all" class="select-all" type="checkbox"></th>
  91. <th>Display name</th>
  92. <th>Email</th>
  93. </tr>
  94. </thead>
  95. <tbody>';
  96. foreach ( $wau_subscriber as $user ){
  97. echo '<tr style="text-align:left">';
  98. echo '<td style="text-align:center"><input type="checkbox" name="ea_user_name[]" value="'.$user->id.'" class="select-all"></td>';
  99. echo '<td><span id="getDetail">'. esc_html( $user->name ).'</span></td>';
  100. echo '<td><span >'.esc_html( $user->email ).'</span></td>';
  101. echo '</tr>';}
  102. echo'</tbody></table>'; // end user Data table for user
  103. foreach ( $wau_subscriber as $user ) {
  104. //echo '<input type="hidden" name="total_count[]" value="' . esc_html( $user->ID ) . '">';
  105. echo '<input type="hidden" name="' . esc_html( $user->id ) . '" value="'. esc_html( $user->email ) . '">';
  106. }
  107. /* select roles */
  108. $mail_content="";
  109. $table_name = $wpdb->prefix.'email_user';
  110. $myrows = $wpdb->get_results( "SELECT id, template_key, template_value FROM $table_name WHERE status = 'template'" );
  111. $template_path_one = plugins_url('template1.html', __FILE__ );
  112. $template_path_two = plugins_url('template2.html', __FILE__ );
  113. echo '<tr>';
  114. echo '<th>Template </th><td colspan="3"><select autocomplete="off" id="wau_template" name="mail_template[]" class="wau-template-selector" style="width:100%">
  115. <option selected="selected">- Select -</option>
  116. <option value="'.$template_path_one.'" id="wau_template_t1"> Default Template - 1 </option>
  117. <option value="'.$template_path_two.'" id="wau_template_t2"> Default Template - 2 </option>';
  118. for ($i=0;$i<count($myrows);$i++) {
  119. echo '<option value="'.$myrows[$i]->id.'" id="am" >'.$myrows[$i]->template_key.'</option>';
  120. }
  121. '</select></td>';
  122. echo '</tr>';
  123. echo '<tr>';
  124. echo '<th>Subject</th> <td colspan="3"><input type="text" name="wau_sub" class="wau_boxlen" id="sub" placeholder="write your email subject here" required></td>';
  125. echo '</tr>';
  126. echo '<tr>';
  127. echo '<th scope="row" valign="top"><label for="wau_mailcontent">Message</label></th>';
  128. echo '<td colspan="3">';
  129. echo '<div id="msg" class="wau_boxlen" name="wau_mailcontent">';
  130. wp_editor($mail_content, "wau_mailcontent",array('wpautop'=>false,'media_buttons' => true));
  131. echo '</div>';
  132. echo '<b> [[unsubscribe]] : </b>use this placeholder to add unsubscribe link. </br>
  133. <b> [[subscriber-name]] : </b>use this placeholder to display user name. </br>
  134. <b> [[subscriber-email]] : </b>use this placeholder to display user email.</br>';
  135. echo '<input type="checkbox" value="1" name="temp">Save template</br>';
  136. echo '</td>';
  137. echo'</tr>';
  138. echo '<tr>';
  139. echo '<th></th>';
  140. echo '<td colspan="3">';
  141. echo '<div><input type="submit" value="Send" class="button button-hero button-primary" id="weu_send" ></div>';
  142. echo '</td>';
  143. echo '</tr>';
  144. echo '</tbody>';
  145. echo '</table>';
  146. echo '</form>';
  147. }
  148. }
  149. ?>