MailchimppopupMailmunchApi

The MailChimp Popup with Exit Support MailchimppopupMailmunchApi class.

Defined (1)

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

/inc/mailmunchapi.php  
  1. class MailchimppopupMailmunchApi { 
  2. protected $base_url = ''; 
  3. protected $email = ''; 
  4. protected $password = ''; 
  5. protected $headers = array('Accept' => 'application/json'); 
  6. protected $requestType = 'get'; 
  7.  
  8. function __construct($email, $password, $url) { 
  9. $this->email = $email; 
  10. $this->password = $password; 
  11. $this->base_url = $url; 
  12.  
  13. function sites() { 
  14. $this->requestType = 'get'; 
  15. return $this->ping('/sites'); 
  16.  
  17. function widgets($site_id, $widget_type_name) { 
  18. $this->requestType = 'get'; 
  19. if (!empty($widget_type_name)) { 
  20. return $this->ping('/sites/'.$site_id.'/widgets?widget_type_name='.$widget_type_name); 
  21. } else { 
  22. return $this->ping('/sites/'.$site_id.'/widgets'); 
  23.  
  24. function getWidgetsHtml($site_id) { 
  25. $this->requestType = 'get'; 
  26. return $this->ping('/sites/'.$site_id.'/widgets/wordpress?plugin=mailchimp-popup'); 
  27.  
  28. function deleteWidget($site_id, $widget_id) { 
  29. $this->requestType = 'post'; 
  30. return $this->ping('/sites/'.$site_id.'/widgets/'.$widget_id.'/delete'); 
  31.  
  32. function hasSite() { 
  33. $request = $this->sites(); 
  34. $sites = $request['body']; 
  35. $result = json_decode($sites); 
  36.  
  37. return (sizeof($result) > 0); 
  38.  
  39. function createSite($sitename, $domain) { 
  40. $this->requestType = 'post'; 
  41. return $this->ping('/sites', array( 
  42. 'site' => array( 
  43. 'name' => $sitename,  
  44. 'domain' => $domain,  
  45. 'wordpress' => true,  
  46. 'external_id' => get_option("mc_pop_mm_wordpress_instance_id") 
  47. )); 
  48.  
  49. function updateSite($sitename, $domain) { 
  50. $this->requestType = 'post'; 
  51. return $this->ping('/wordpress/update_site', array( 
  52. 'external_id' => get_option("mc_pop_mm_wordpress_instance_id"),  
  53. 'site' => array( 
  54. 'name' => $sitename,  
  55. 'domain' => $domain 
  56. )); 
  57.  
  58. function createGuestUser() { 
  59. $this->requestType = 'post'; 
  60. return $this->ping('/users', array( 
  61. 'user' => array( 
  62. 'email' => $this->email,  
  63. 'password' => $this->password,  
  64. 'guest_user' => true,  
  65. 'referral' => "mailchimp-popup-wordpress-plugin" 
  66. ), true); 
  67.  
  68. function signUp() { 
  69. $this->requestType = 'post'; 
  70. return $this->ping('/users', array( 
  71. 'user' => array( 
  72. 'email' => $this->email,  
  73. 'password' => $this->password,  
  74. 'referral' => "mailchimp-popup-wordpress-plugin" 
  75. ), true); 
  76.  
  77. function updateGuest($new_email, $new_password) { 
  78. $this->requestType = 'post'; 
  79. return $this->ping('/wordpress/update_guest', array( 
  80. 'user' => array( 
  81. 'email' => $new_email,  
  82. 'password' => $new_password,  
  83. 'guest_user' => false 
  84. ), true); 
  85.  
  86. function signIn() { 
  87. $this->requestType = 'post'; 
  88. return $this->ping('/users/sign_in'); 
  89.  
  90. function validPassword() { 
  91. $this->requestType = 'get'; 
  92. $request = $this->ping('/sites'); 
  93. if( is_wp_error( $request ) ) { 
  94. return new WP_Error( 'broke', "Unable to connect to MailMunch. Please try again later." ); 
  95.  
  96. if ($request['response']['code'] == 200) { 
  97. return true; 
  98. else { 
  99. return false; 
  100.  
  101. function isNewUser($email) { 
  102. if (empty($email)) { 
  103. $email = $this->email; 
  104. $this->requestType = 'get'; 
  105. $result = $this->ping('/users/exists?user[email]='. $email, array(), true); 
  106. return $result['body'] == 'false'; 
  107.  
  108. function ping($path, $options=array(), $skipAuth=false) { 
  109. $auth = array('auth' => array($this->email, $this->password)); 
  110. $type = $this->requestType; 
  111. $url = $this->base_url. $path; 
  112. $args = array( 
  113. 'headers' => array_merge($this->headers, array( 
  114. 'Authorization' => 'Basic ' . base64_encode( $this->email . ':' . $this->password ) 
  115. ),  
  116. 'timeout' => 120 
  117. ); 
  118.  
  119. if ($type != 'post') { 
  120. $request = wp_remote_get($url, $args); 
  121. else { 
  122. $args = array_merge($args, array('method' => 'POST', 'body' => $options)); 
  123. $request = wp_remote_post($url, $args); 
  124.  
  125. if ( !is_wp_error( $request ) && ( $request['response']['code'] == 500 || $request['response']['code'] == 503 ) ) { 
  126. return new WP_Error( 'broke', "Internal Server Error" ); 
  127.  
  128. return $request;