/wp-content/plugins/divi-builder/framework/subscription/mailchimp/mailchimp.php

https://gitlab.com/gregtyka/lfmawordpress · PHP · 97 lines · 33 code · 8 blank · 56 comment · 1 complexity · 779eedebf5bedee5a78a49324545a718 MD5 · raw file

  1. <?php
  2. /**
  3. * Super-simple, minimum abstraction MailChimp API v2 wrapper
  4. * Class name was renamed from MailChimp to MailChimp_Divi to avoid conflicts with some plugins.
  5. * The use of curl and file_get_content has been replaced with WordPress' HTTP API
  6. *
  7. * Contributors:
  8. * Michael Minor <me@pixelbacon.com>
  9. * Lorna Jane Mitchell, github.com/lornajane
  10. *
  11. * @author Drew McLellan <drew.mclellan@gmail.com>
  12. * @version 1.1.1
  13. *
  14. * The MIT License (MIT)
  15. *
  16. * Copyright (c) 2013 Drew McLellan
  17. *
  18. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  19. * this software and associated documentation files (the "Software"), to deal in
  20. * the Software without restriction, including without limitation the rights to
  21. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  22. * the Software, and to permit persons to whom the Software is furnished to do so,
  23. * subject to the following conditions:
  24. *
  25. * The above copyright notice and this permission notice shall be included in all
  26. * copies or substantial portions of the Software.
  27. *
  28. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  29. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  30. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  31. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  32. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  33. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  34. *
  35. */
  36. class MailChimp_Divi
  37. {
  38. private $api_key;
  39. private $api_endpoint = 'https://<dc>.api.mailchimp.com/2.0';
  40. private $verify_ssl = false;
  41. /**
  42. * Create a new instance
  43. * @param string $api_key Your MailChimp API key
  44. */
  45. function __construct($api_key)
  46. {
  47. $this->api_key = $api_key;
  48. list(, $datacentre) = explode('-', $this->api_key);
  49. $this->api_endpoint = str_replace('<dc>', $datacentre, $this->api_endpoint);
  50. }
  51. /**
  52. * Call an API method. Every request needs the API key, so that is added automatically -- you don't need to pass it in.
  53. * @param string $method The API method to call, e.g. 'lists/list'
  54. * @param array $args An array of arguments to pass to the method. Will be json-encoded for you.
  55. * @return array Associative array of json decoded API response.
  56. */
  57. public function call($method, $args=array(), $timeout = 10)
  58. {
  59. return $this->makeRequest($method, $args, $timeout);
  60. }
  61. /**
  62. * Performs the underlying HTTP request. Not very exciting
  63. * @param string $method The API method to be called
  64. * @param array $args Assoc array of parameters to be passed
  65. * @param int $timeout Time allocated before timeout
  66. * @return array Assoc array of decoded result
  67. */
  68. private function makeRequest( $method, $args_body = array(), $timeout = 10 )
  69. {
  70. // Prepare argument
  71. $args = array(
  72. 'timeout' => $timeout,
  73. 'sslverify' => $this->verify_ssl,
  74. 'body' => array(
  75. 'apikey' => $this->api_key,
  76. ),
  77. );
  78. // Merge $args_body into $args['body']
  79. if ( ! empty( $args_body ) ) {
  80. $args['body'] = array_merge( $args['body'], $args_body );
  81. }
  82. // Setup URL
  83. $url = $this->api_endpoint.'/'.$method.'.json';
  84. // Request to MailChimp API and get result
  85. $result = wp_remote_post( $url, $args );
  86. // Return result
  87. return $result;
  88. }
  89. }