mc_pop_mm_setup

The MailChimp Popup with Exit Support mc pop mm setup function.

Description

mc_pop_mm_setup(); 

Usage

  1. if ( !function_exists( 'mc_pop_mm_setup' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'mailchimp-popup-with-exit-support/mailchimp-popup-mailmunch.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = mc_pop_mm_setup(); 
  7.  

Defined (1)

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

/mailchimp-popup-mailmunch.php  
  1. function mc_pop_mm_setup() { 
  2. $mm_helpers = new MailchimppopupMailmunchHelpers(); 
  3. $mc_pop_mm_data = unserialize(get_option("mc_pop_mm_data")); 
  4. $mc_pop_mm_data["site_url"] = home_url(); 
  5. $mc_pop_mm_data["site_name"] = get_bloginfo(); 
  6. update_option("mc_pop_mm_data", serialize($mc_pop_mm_data)); 
  7.  
  8. // This is a POST request. Let's save data first. 
  9. if ($_POST) { 
  10. $post_data = (isset($_POST["mc_pop_mm_data"]) ? $_POST["mc_pop_mm_data"] : array()); 
  11. $post_action = $_POST["action"]; 
  12.  
  13. if ($post_action == "save_settings") {  
  14.  
  15. $mc_pop_mm_data = array_merge(unserialize(get_option('mc_pop_mm_data')), $post_data); 
  16. update_option("mc_pop_mm_data", serialize($mc_pop_mm_data)); 
  17.  
  18. } else if ($post_action == "sign_in") { 
  19.  
  20. $mm = new MailchimppopupMailmunchApi($_POST["email"], $_POST["password"], "http://".MAILCHIMP_POPUP_MAILMUNCH_URL); 
  21. if ($mm->validPassword()) { 
  22. update_option("mc_pop_mm_user_email", $_POST["email"]); 
  23. update_option("mc_pop_mm_user_password", base64_encode($_POST["password"])); 
  24. delete_option("mc_pop_mm_guest_user"); 
  25.  
  26. } else if ($post_action == "sign_up") { 
  27.  
  28. if (empty($_POST["email"]) || empty($_POST["password"])) { 
  29. $invalid_email_password = true; 
  30. } else { 
  31. $account_info = $mm_helpers->getEmailPassword(); 
  32. $mc_pop_mm_email = $account_info['email']; 
  33. $mc_pop_mm_password = $account_info['password']; 
  34.  
  35. $mm = new MailchimppopupMailmunchApi($mc_pop_mm_email, $mc_pop_mm_password, "http://".MAILCHIMP_POPUP_MAILMUNCH_URL); 
  36. if ($mm->isNewUser($_POST['email'])) { 
  37. $update_result = $mm->updateGuest($_POST['email'], $_POST['password']); 
  38. $result = json_decode($update_result['body']); 
  39. update_option("mc_pop_mm_user_email", $result->email); 
  40. update_option("mc_pop_mm_user_password", base64_encode($_POST['password'])); 
  41. if (!$result->guest_user) { delete_option("mc_pop_mm_guest_user"); } 
  42. $mc_pop_mm_email = $result->email; 
  43. $mc_pop_mm_password = $_POST['password']; 
  44.  
  45. // We have update the guest with real email address, let's create a site now 
  46. $mm = new MailchimppopupMailmunchApi($mc_pop_mm_email, $mc_pop_mm_password, "http://".MAILCHIMP_POPUP_MAILMUNCH_URL); 
  47.  
  48. $update_result = $mm->updateSite($mc_pop_mm_data["site_name"], $mc_pop_mm_data["site_url"]); 
  49. $result = json_decode($update_result['body']); 
  50. $mc_pop_mm_data = unserialize(get_option("mc_pop_mm_data")); 
  51. $mc_pop_mm_data["site_url"] = $result->domain; 
  52. $mc_pop_mm_data["site_name"] = $result->name; 
  53. update_option("mc_pop_mm_data", serialize($mc_pop_mm_data)); 
  54. } else { 
  55. $user_exists = true; 
  56.  
  57. } else if ($post_action == "unlink_account") { 
  58.  
  59. $mc_pop_mm_data = array(); 
  60. $mc_pop_mm_data["site_url"] = home_url(); 
  61. $mc_pop_mm_data["site_name"] = get_bloginfo(); 
  62. update_option("mc_pop_mm_data", serialize($mc_pop_mm_data)); 
  63. delete_option("mc_pop_mm_user_email"); 
  64. delete_option("mc_pop_mm_user_password"); 
  65.  
  66. } else if ($post_action == "delete_widget") { 
  67.  
  68. if ($_POST["site_id"] && $_POST["widget_id"]) { 
  69. $account_info = $mm_helpers->getEmailPassword(); 
  70. $mc_pop_mm_email = $account_info['email']; 
  71. $mc_pop_mm_password = $account_info['password']; 
  72. $mm = new MailchimppopupMailmunchApi($account_info['email'], $account_info["password"], "http://".MAILCHIMP_POPUP_MAILMUNCH_URL); 
  73. $request = $mm->deleteWidget($_POST["site_id"], $_POST["widget_id"]); 
  74.  
  75. } else if ($post_action == "create_site") {  
  76. $site_url = (empty($_POST["site_url"]) ? get_bloginfo() : $_POST["site_url"]); 
  77. $site_name = (empty($_POST["site_name"]) ? home_url() : $_POST["site_name"]); 
  78.  
  79. $account_info = $mm_helpers->getEmailPassword(); 
  80. $mm = new MailchimppopupMailmunchApi($account_info['email'], $account_info["password"], "http://".MAILCHIMP_POPUP_MAILMUNCH_URL); 
  81. $request = $mm->createSite($site_name, $site_url); 
  82. $site = json_decode($request['body']); 
  83.  
  84. if (!empty($site->id)) { 
  85. $mc_pop_mm_data = unserialize(get_option("mc_pop_mm_data")); 
  86. $mc_pop_mm_data["site_id"] = $site->id; 
  87. $mc_pop_mm_data["script_src"] = $site->javascript_url; 
  88. update_option("mc_pop_mm_data", serialize($mc_pop_mm_data)); 
  89.  
  90. // If the user does not exists, create a GUEST user 
  91. if (get_option("mc_pop_mm_user_email") == "") { 
  92. $mc_pop_mm_email = "guest_".uniqid()."@mailmunch.co"; 
  93. $mc_pop_mm_password = uniqid(); 
  94. $mm = new MailchimppopupMailmunchApi($mc_pop_mm_email, $mc_pop_mm_password, "http://".MAILCHIMP_POPUP_MAILMUNCH_URL); 
  95. $mm->createGuestUser(); 
  96. update_option("mc_pop_mm_user_email", $mc_pop_mm_email); 
  97. update_option("mc_pop_mm_user_password", base64_encode($mc_pop_mm_password)); 
  98. update_option("mc_pop_mm_guest_user", true); 
  99.  
  100. // If we already have the user's email stored, let's create the API instance 
  101. // If we don't have it yet, make sure NOT to phone home any user data 
  102. if (get_option("mc_pop_mm_user_email") != "") { 
  103. $account_info = $mm_helpers->getEmailPassword(); 
  104. $mc_pop_mm_email = $account_info['email']; 
  105. $mc_pop_mm_password = $account_info['password']; 
  106.  
  107. $mm = new MailchimppopupMailmunchApi($mc_pop_mm_email, $mc_pop_mm_password, "http://".MAILCHIMP_POPUP_MAILMUNCH_URL); 
  108. $pass_check = $mm->validPassword(); 
  109.  
  110. if( is_wp_error( $pass_check ) ) { 
  111. echo $pass_check->get_error_message(); 
  112. return; 
  113.  
  114. if (!$pass_check) { 
  115. // Invalid user, create a GUEST user 
  116. $mc_pop_mm_email = "guest_".uniqid()."@mailmunch.co"; 
  117. $mc_pop_mm_password = uniqid(); 
  118. $mm = new MailchimppopupMailmunchApi($mc_pop_mm_email, $mc_pop_mm_password, "http://".MAILCHIMP_POPUP_MAILMUNCH_URL); 
  119. $mm->createGuestUser(); 
  120. update_option("mc_pop_mm_user_email", $mc_pop_mm_email); 
  121. update_option("mc_pop_mm_user_password", base64_encode($mc_pop_mm_password)); 
  122. update_option("mc_pop_mm_guest_user", true); 
  123.  
  124. $mc_pop_mm_guest_user = get_option("mc_pop_mm_guest_user"); 
  125.  
  126.  
  127. if ($mc_pop_mm_guest_user) { 
  128. // This is a Guest USER. Do not collect any user data. 
  129. $sites = $mm_helpers->createAndGetGuestSites($mm); 
  130. } else { 
  131. $sites = $mm_helpers->createAndGetSites($mm); 
  132.  
  133. if (isset($mc_pop_mm_data["site_id"])) { 
  134. // If there's a site already chosen, we need to get and save it's script_src in WordPress 
  135. $site = $mm_helpers->getSite($sites, $mc_pop_mm_data["site_id"]); 
  136.  
  137. if ($site) { 
  138. $mc_pop_mm_data = array_merge(unserialize(get_option('mc_pop_mm_data')), array("script_src" => $site->javascript_url)); 
  139. update_option("mc_pop_mm_data", serialize($mc_pop_mm_data)); 
  140. } else { 
  141. // The chosen site does not exist in the mailmunch account any more, remove it locally 
  142. $site_not_found = true; 
  143. $mc_pop_mm_data = unserialize(get_option('mc_pop_mm_data')); 
  144. unset($mc_pop_mm_data["site_id"]); 
  145. unset($mc_pop_mm_data["script_src"]); 
  146. update_option("mc_pop_mm_data", serialize($mc_pop_mm_data)); 
  147.  
  148. if (!isset($mc_pop_mm_data["site_id"])) { 
  149. // If there's NO chosen site yet 
  150.  
  151. if (sizeof($sites) == 1 && ($sites[0]->name == get_bloginfo() || $sites[0]->name == "WordPress")) { 
  152. // If this mailmunch account only has 1 site and its name matches this WordPress blogs 
  153.  
  154. $site = $sites[0]; 
  155.  
  156. if ($site) { 
  157. $mc_pop_mm_data = array_merge(unserialize(get_option('mc_pop_mm_data')), array("site_id" => $site->id, "script_src" => $site->javascript_url)); 
  158. update_option("mc_pop_mm_data", serialize($mc_pop_mm_data)); 
  159. } else if (sizeof($sites) > 0) { 
  160. // If this mailmunch account has one or more sites, let the user choose one 
  161. ?> 
  162. <div class="container"> 
  163. <div class="page-header"> 
  164. <h1>Choose Your Site</h1> 
  165. </div> 
  166.  
  167. <p>Choose the site that you would like to link with your WordPress.</p> 
  168.  
  169. <div id="choose-site-form"> 
  170. <form action="" method="POST"> 
  171. <div class="form-group"> 
  172. <input type="hidden" name="action" value="save_settings" /> 
  173.  
  174. <select name="mc_pop_mm_data[site_id]"> 
  175. <?php foreach ($sites as $site) { ?> 
  176. <option value="<?php echo $site->id ?>"><?php echo $site->name ?></option> 
  177. <?php } ?> 
  178. </select> 
  179. </div> 
  180.  
  181. <div class="form-group"> 
  182. <input type="submit" value="Save Settings" class="button-primary" /> 
  183. </div> 
  184. </form> 
  185.  
  186. <p> 
  187. Don't see this site above? <a href="" onclick="document.getElementById('create-site-form').style.display = 'block'; document.getElementById('choose-site-form').style.display = 'none'; return false;">Create New Site</a> 
  188. </p> 
  189. </div> 
  190.  
  191. <div id="create-site-form" style="display: none;"> 
  192. <form action="" method="POST"> 
  193. <input type="hidden" name="action" value="create_site" /> 
  194.  
  195. <div class="form-group"> 
  196. <label>Site Name</label> 
  197. <input type="text" name="site_name" value="<?php echo get_bloginfo() ?>" /> 
  198. </div> 
  199.  
  200. <div class="form-group"> 
  201. <label>Site URL</label> 
  202. <input type="text" name="site_url" value="<?php echo home_url() ?>" /> 
  203. </div> 
  204.  
  205. <div class="form-group"> 
  206. <input type="submit" value="Create Site" class="button-primary" /> 
  207. </div> 
  208. </form> 
  209.  
  210. <p> 
  211. Already have a site in your MailMunch account? <a href="" onclick="document.getElementById('create-site-form').style.display = 'none'; document.getElementById('choose-site-form').style.display = 'block'; return false;">Choose Site</a> 
  212. </p> 
  213. </div> 
  214. </div> 
  215. <?php 
  216. return; 
  217.  
  218. $request = $mm->getWidgetsHtml($mc_pop_mm_data["site_id"]); 
  219. $widgets = $request['body']; 
  220. $widgets = str_replace("{{EMAIL}}", $mc_pop_mm_email, $widgets); 
  221. $widgets = str_replace("{{PASSWORD}}", $mc_pop_mm_password, $widgets); 
  222. echo $widgets; 
  223.  
  224. if ($mc_pop_mm_guest_user) { 
  225. $current_user = wp_get_current_user(); 
  226. ?> 
  227.  
  228. <div id="signup-signin-box-overlay" onclick="hideSignupBox();" style="display: none;"></div> 
  229.  
  230. <div id="signup-signin-box" style="display:none;"> 
  231. <a id="signup-signin-close" onclick="hideSignupBox();"> 
  232. <img src="<?php echo plugins_url( 'img/close.png', __FILE__ ) ?>" /> 
  233. </a> 
  234.  
  235. <div id="sign-up-form" class="<?php if (!$_POST || ($_POST["action"] != "sign_in" && $_POST["action"] != "unlink_account")) { ?> active<?php } ?>"> 
  236. <div class="form-container"> 
  237. <h2 class="modal-header">Sign Up</h2> 
  238. <p>To activate your MailChimp forms, we will now create your account on MailMunch (<a onclick="showWhyAccount();" id="why-account-btn">Why?</a>).</p> 
  239.  
  240. <div id="why-account" class="alert alert-warning" style="display: none;"> 
  241. <h4>Why do I need a MailMunch account?</h4> 
  242.  
  243. <p> 
  244. MailMunch is a not just a WordPress plugin but a standalone service. An account is required to identify your WordPress and serve your MailChimp forms. 
  245. </p> 
  246. </div> 
  247.  
  248. <?php if (isset($user_exists)) { ?> 
  249. <div id="invalid-alert" class="alert alert-danger signup-alert" role="alert">Account with this email already exists. Please sign in using your password.</div> 
  250. <?php } else if (isset($invalid_email_password)) { ?> 
  251. <div id="invalid-alert" class="alert alert-danger signup-alert" role="alert">Invalid email or password. Please enter a valid email and password below.</div> 
  252. <?php } ?> 
  253.  
  254. <form action="" method="POST"> 
  255. <input type="hidden" name="action" value="sign_up" /> 
  256.  
  257. <div class="form-group"> 
  258. <label>Wordpress Name</label> 
  259. <input type="text" placeholder="Site Name" name="site_name" value="<?php echo $mc_pop_mm_data["site_name"] ?>" class="form-control"> 
  260. </div> 
  261.  
  262. <div class="form-group"> 
  263. <label>Wordpress URL</label> 
  264. <input type="text" placeholder="Site URL" name="site_url" value="<?php echo $mc_pop_mm_data["site_url"] ?>" class="form-control"> 
  265. </div> 
  266.  
  267. <div class="form-group"> 
  268. <label>Email Address</label> 
  269. <input type="email" placeholder="Email Address" name="email" value="<?php echo $current_user->user_email ?>" class="form-control"> 
  270. </div> 
  271.  
  272. <div class="form-group"> 
  273. <label>Password</label> 
  274. <input type="password" placeholder="Password" name="password" class="form-control" /> 
  275. </div> 
  276.  
  277. <div class="form-group"> 
  278. <input type="submit" value="Sign Up »" class="btn btn-success btn-lg" /> 
  279. </div> 
  280. </form> 
  281. </div> 
  282.  
  283. <p>Already have an account? <a id="show-sign-in" onclick="showSignInForm();">Sign In</a></p> 
  284. </div> 
  285.  
  286. <div id="sign-in-form" class="<?php if ($_POST && ($_POST["action"] == "sign_in" || $_POST["action"] == "unlink_account")) { ?> active<?php } ?>"> 
  287. <h2 class="modal-header">Sign In</h2> 
  288. <p>Sign in using your email and password below.</p> 
  289.  
  290. <?php if ($_POST && $_POST["action"] == "sign_in") { ?> 
  291. <div id="invalid-alert" class="alert alert-danger signin-alert" role="alert">Invalid Email or Password. Please try again.</div> 
  292. <?php } ?> 
  293.  
  294. <div class="form-container"> 
  295. <form action="" method="POST"> 
  296. <input type="hidden" name="action" value="sign_in" /> 
  297.  
  298. <div class="form-group"> 
  299. <label>Email Address</label> 
  300. <input type="email" placeholder="Email Address" name="email" class="form-control" value="<?php if (isset($_POST["email"])) { echo $_POST["email"]; } ?>" /> 
  301. </div> 
  302. <div class="form-group"> 
  303. <label>Password</label> 
  304. <input type="password" placeholder="Password" name="password" class="form-control" /> 
  305. </div> 
  306.  
  307. <div class="form-group"> 
  308. <input type="submit" value="Sign In »" class="btn btn-success btn-lg" /> 
  309. </div> 
  310. </form> 
  311. </div> 
  312.  
  313. <p>Forgot your password? <a href="http://<?php echo MAILCHIMP_POPUP_MAILMUNCH_URL; ?>/users/password/new" target="_blank">Click here</a> to retrieve it.</p> 
  314. <p>Don't have an account? <a id="show-sign-up" onclick="showSignUpForm();">Sign Up</a></p> 
  315. </div> 
  316. </div> 
  317.  
  318. <?php 
  319. if ($_POST) {  
  320. ?> 
  321. <script> 
  322. jQuery(window).load(function() { 
  323. <?php if ($_POST && ($_POST["action"] == "sign_in" || $_POST["action"] == "unlink_account")) { ?> 
  324. showSignInForm(); 
  325. <?php } else { ?> 
  326. showSignUpForm(); 
  327. <?php } ?> 
  328. }); 
  329. </script> 
  330. <?php