class-godaddy-em.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <?php
  2. /**
  3. * GoDaddy Email Marketing Dispatcher.
  4. *
  5. * A modified version from GoDaddy Email Marketing plugin
  6. * (https://wordpress.org/plugins/godaddy-email-marketing-sign-up-forms/)
  7. *
  8. * @since 1.0
  9. */
  10. class GoDaddyEM {
  11. /**
  12. * API Credentials
  13. * @var string
  14. */
  15. private static $api_username;
  16. private static $api_key;
  17. /**
  18. * Base API URI
  19. */
  20. private static $api_base_url = 'https://gem.godaddy.com/';
  21. /**
  22. * HTTP response codes
  23. *
  24. * @var array
  25. */
  26. private static $ok_codes = array( 200, 304 );
  27. /**
  28. * Constructor
  29. *
  30. * @param string $email The username.
  31. * @param string $api_key The API key.
  32. */
  33. public function __construct($username, $api_key) {
  34. self::$api_username = $username;
  35. self::$api_key = $api_key;
  36. }
  37. /**
  38. * Gets and sets the forms.
  39. *
  40. * @param string $username The username.
  41. * @param string $api_key
  42. *
  43. * @return string $api_key The API key.
  44. */
  45. public static function fetch_forms( $username = '', $api_key = '' ) {
  46. if ( ! $username && ! $api_key ) {
  47. $username = self::$api_username;
  48. $api_key = self::$api_key;
  49. }
  50. if ( ! $username || ! $api_key ) {
  51. return false;
  52. }
  53. $auth = array(
  54. 'username' => $username,
  55. 'api_key' => $api_key,
  56. );
  57. // Prepare the URL that includes our credentials.
  58. $response = wp_remote_get( self::get_method_url( 'forms', false, $auth ), array(
  59. 'timeout' => 10,
  60. ) );
  61. // Credentials are incorrect.
  62. if ( ! in_array( wp_remote_retrieve_response_code( $response ), self::$ok_codes, true ) ) {
  63. return false;
  64. }
  65. $data = json_decode( wp_remote_retrieve_body( $response ) );
  66. return $data;
  67. }
  68. /**
  69. * Gets the forms.
  70. *
  71. * @return array|false The form fields array or false.
  72. */
  73. public static function get_forms() {
  74. if ( ! self::$api_username ) {
  75. return false;
  76. }
  77. $data = self::fetch_forms();
  78. return $data;
  79. }
  80. /**
  81. * Gets and sets the form fields.
  82. *
  83. * @param string $form_id Form ID.
  84. * @return false|object The form fields JSON object or false.
  85. */
  86. public static function get_fields( $form_id ) {
  87. // Fields are not cached. fetch and cache.
  88. $response = wp_remote_get( self::get_method_url( 'fields', array(
  89. 'id' => $form_id,
  90. ) ) );
  91. // Was there an error, connection is down? bail and try again later.
  92. if ( ! self::is_response_ok( $response ) ) {
  93. return false;
  94. }
  95. $data = json_decode( wp_remote_retrieve_body( $response ) );
  96. return $data;
  97. }
  98. /**
  99. * Return the API base URL.
  100. *
  101. * @param string $path (optional)
  102. *
  103. * @return string
  104. */
  105. public static function get_api_base_url( $path = '' ) {
  106. return self::$api_base_url . $path;
  107. }
  108. /**
  109. * Utility function for getting a URL for various API methods
  110. *
  111. * @param string $method The short of the API method.
  112. * @param array $params Extra parameters to pass on with the request.
  113. * @param bool $auth Autentication array including API key and username.
  114. *
  115. * @return string The final URL to use for the request
  116. */
  117. public static function get_method_url( $method, $params = array(), $auth = false ) {
  118. $auth = $auth ? $auth : array(
  119. 'username' => self::$api_username,
  120. 'api_key' => self::$api_key
  121. );
  122. $path = '';
  123. switch ( $method ) {
  124. case 'forms' :
  125. $path = add_query_arg( $auth, 'signups.json' );
  126. break;
  127. case 'fields' :
  128. $path = add_query_arg( $auth, 'signups/' . $params['id'] . '.json' );
  129. break;
  130. case 'account' :
  131. $path = add_query_arg( $auth, 'user/account_status' );
  132. break;
  133. }
  134. return self::get_api_base_url( $path );
  135. }
  136. /**
  137. * Check for an OK response.
  138. *
  139. * @param array $request HTTP response by reference.
  140. * @return bool
  141. */
  142. public static function is_response_ok( $request ) {
  143. return ( ! is_wp_error( $request ) && in_array( wp_remote_retrieve_response_code( $request ), self::$ok_codes, true ) );
  144. }
  145. /**
  146. * Check if an account exists from GoDaddy
  147. *
  148. * @return bool
  149. */
  150. public static function is_account_ok() {
  151. // Request account details
  152. $response = wp_remote_get( self::get_method_url( 'account' ) );
  153. // Was there an error, connection is down? bail and try again later.
  154. if ( ! self::is_response_ok( $response ) ) {
  155. return false;
  156. }
  157. return true;
  158. }
  159. /**
  160. * Add new subscriber
  161. *
  162. * @param array $data An array of subscriber data
  163. */
  164. public static function add_subscriber( $data ) {
  165. if ( empty( $data ) ) {
  166. return false;
  167. }
  168. if ( ! self::$api_username || ! self::$api_key ) {
  169. return false;
  170. }
  171. if ( isset( $data[ 'form_id' ] ) && isset( $data[ 'email' ] ) ) {
  172. $form = self::get_fields( $data[ 'form_id' ] );
  173. $submit_data = array(
  174. 'username' => self::$api_username,
  175. 'api_key' => self::$api_key,
  176. 'integration' => 'WordPress',
  177. 'signup[email]' => $data['email']
  178. );
  179. if ( isset( $data[ 'first_name' ] ) ) {
  180. $submit_data[ 'signup[first_name]' ] = $data['first_name'];
  181. }
  182. if ( isset( $data[ 'last_name' ] ) ) {
  183. $submit_data[ 'signup[last_name]' ] = $data['last_name'];
  184. }
  185. // Prepare the URL that includes our credentials.
  186. $response = wp_remote_post( $form->submit, array(
  187. 'method' => 'POST',
  188. 'timeout' => 10,
  189. 'body' => $submit_data
  190. ) );
  191. // Credentials are correct.
  192. if ( self::is_response_ok( $response ) ) {
  193. return true;
  194. }
  195. }
  196. return false;
  197. }
  198. }