Advertisement
Guest User

Untitled

a guest
Dec 4th, 2016
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.23 KB | None | 0 0
  1. /*
  2. * Function to create form shortcode
  3. */
  4. function request_form_func($atts, $content = null){
  5. extract(shortcode_atts(array(
  6. 'id' => false,
  7. 'class' => false,
  8. 'title' => false,
  9. 'subtitle' => false,
  10. 'action' => "/thanks",
  11. 'key' => false,
  12. 'button_text' => "Submit"
  13. ), $atts));
  14.  
  15. if( $class ) { $class = ' ' . $class; }
  16. else { $class = ''; }
  17.  
  18. if( empty($_GET) ){
  19. $next_step = false;
  20. $db_name = '';
  21. $db_email = '';
  22. }
  23. if(isset($_GET['hid'])){
  24. $email_hash = trim($_GET['hid']);
  25.  
  26. $table = "wp_new_user";
  27. $project_data = new wpdb('root','root','wordpress','localhost'); // Testserver
  28. $rows = $project_data->get_results( $project_data->prepare(
  29. "
  30. SELECT id,name, email, hash FROM " . $table . "
  31. WHERE hash = %d
  32. ",
  33. $email_hash
  34. ) );
  35.  
  36. $db_hash = $rows[0]->hash;
  37. if( $db_hash == $email_hash ) {
  38. $field_id = $rows[0]->id;
  39. $db_name = $rows[0]->name;
  40. $db_email = $rows[0]->email;
  41. $next_step = true;
  42. }
  43. }
  44.  
  45. $out = '';
  46.  
  47. if($id) { $id = '-'.$id; }
  48.  
  49. $out .= '<div class="request_form'.$class.'">';
  50. $out .= '<div class="form-wrap">';
  51. if($title) {
  52. $out .= '<span class="title">' . $title . '</span>';
  53. }
  54. $out .= '<form id="step-form" class="cf" method="post" action="'.$action.'">';
  55. $out .= '<div class="field-wrap"><label for="fullname'.$id.'"><span class="desc">Name</span>';
  56. $out .= '<input type="text" id="fullname'.$id.'" name="fullname" data-required="true" placeholder="Jon Doe" value="'.$db_name.'"></label></div>';
  57.  
  58. $out .= '<div class="field-wrap"><label for="email'.$id.'"><span class="desc">E-Mail</span>';
  59. $out .= '<input type="email" id="email'.$id.'" name="email" data-required="true" placeholder="name@domain.com" value="'.$db_email.'"></label></div>';
  60.  
  61. if($next_step){
  62. $out .= '<div class="field-wrap"><label for="username'.$id.'"><span class="desc">Username</span>';
  63. $out .= '<input type="text" id="username'.$id.'" name="username" data-required="true" placeholder="username"></label></div>';
  64.  
  65. $out .= '<div class="field-wrap"><label for="password'.$id.'"><span class="desc">Password</span>';
  66. $out .= '<input type="password" id="password'.$id.'" name="password" data-required="true" placeholder="password"></label></div>';
  67.  
  68. $out .= '<input type="hidden" id="field_id" name="field_id" value="'.$field_id.'" ></div>';
  69. }
  70. $out .= '<div class="field-wrap submit-wrap"><input type="submit" value="' .$button_text. '" name="submit"></div>';
  71.  
  72. $out .= wp_nonce_field('step_form', 'step_form_nonce'.$id, true, false);
  73. $out .= '</form>';
  74. $out .= '</div>';
  75. $out .= '</div>';
  76. return $out;
  77. }
  78. add_shortcode('request_form', 'request_form_func');
  79.  
  80. /*
  81. * Thanks shortcode to take care data
  82. */
  83. function thanks_func($atts, $content = null){
  84. $out = '';
  85.  
  86. if(!empty($_POST) || wp_verify_nonce($_POST['step_form_nonce'],'step_form')){
  87. }
  88. else {
  89. $out .= '<div class="content-area">';
  90. $out .= '<h2 class="h1 page-title">Something went wrong</h2>';
  91. $out .= '<p class="block">Please Re-Submit your form again</p>';
  92. $out .= '</div>';
  93.  
  94. return $out;
  95. }
  96.  
  97. if(isset($_POST['fullname'])){ $fullname = trim($_POST['fullname']); }
  98. if(isset($_POST['email'])){ $email = trim($_POST['email']); }
  99.  
  100. if(isset($_POST['username'])){ $username = trim($_POST['username']); }
  101. if(isset($_POST['password'])){ $password = trim($_POST['password']); }
  102. if(isset($_POST['field_id'])){ $field_id = trim($_POST['field_id']); }
  103. $hash = md5( rand(0,1000) ); // Generate random 32 character hash and assign it to a local variable.
  104.  
  105. $header .= "MIME-Version: 1.0\n";
  106. $header .= "Content-Type: text/html; charset=utf-8\n";
  107. $header .= "From:" . "admin@domain.com";
  108.  
  109. $confirmation_text = "Thanks for Submitting your first form";
  110. $confirmation_body_text = "/registration/?hid='.$hash.'";
  111.  
  112. $subject = "Please click the link below";
  113. $message = "Name: $fullname\n";
  114. $message .= "Email Address: $email\n";
  115. $message .= "Click the link: $confirmation_body_text\n";
  116.  
  117. if (!empty($username) && !empty($password) && !empty($field_id)){
  118. update_custom_user($username, $password, $field_id);
  119. } else if (create_custom_user($fullname, $email, $hash) && wp_mail($email, $subject, $message, $header)){
  120.  
  121. }
  122.  
  123. $out .= '<div class="content-area">';
  124. $content = do_shortcode($content);
  125. $out .= $content;
  126. $out .= '</div>';
  127.  
  128. return $out;
  129. }
  130. add_shortcode('thanks', 'thanks_func');
  131.  
  132. /*
  133. * Function to create new record in specified table
  134. */
  135. function create_custom_user($fullname, $email, $hash){
  136. global $wpdb;
  137. $table_name = $wpdb->prefix . "new_user";
  138.  
  139. $cur_date = new DateTime();
  140. $cur_date->setTimezone(new DateTimeZone('Europe/Berlin'));
  141. $cur_date = $cur_date->format('d.m.Y').', '.$cur_date->format('G:i');
  142.  
  143.  
  144. $wpdb->insert( $table_name, array(
  145. 'name' => $fullname,
  146. 'email' => $email,
  147. 'hash' => $hash,
  148. 'created_at' => $cur_date
  149.  
  150. ) );
  151.  
  152. return true;
  153. }
  154. /*
  155. * Function to update new record in specified table
  156. */
  157. function update_custom_user($username, $password, $field_id){
  158. global $wpdb;
  159. $table_name = $wpdb->prefix . "new_user";
  160.  
  161. $cur_date = new DateTime();
  162. $cur_date->setTimezone(new DateTimeZone('Europe/Berlin'));
  163. $cur_date = $cur_date->format('d.m.Y').', '.$cur_date->format('G:i');
  164.  
  165.  
  166. $wpdb->update( $table_name, array(
  167. 'username' => $username,
  168. 'password' => $password,
  169. 'updated_at' => $cur_date
  170. ),
  171. array(
  172. "id" => $field_id
  173. ) );
  174.  
  175. return true;
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement