MC4WP_MailChimp

Class MC4WP_MailChimp.

Defined (1)

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

/includes/class-mailchimp.php  
  1. class MC4WP_MailChimp { 
  2.  
  3. /** 
  4. * Get MailChimp lists 
  5. * Try cache first, then try API, then try fallback cache. 
  6. * @param bool $force_renewal 
  7. * @param bool $force_fallback 
  8. * @return array 
  9. */ 
  10. public function get_lists( $force_renewal = false, $force_fallback = false ) { 
  11.  
  12. if( $force_renewal ) { 
  13. delete_transient( 'mc4wp_mailchimp_lists' ); 
  14. delete_transient( 'mc4wp_mailchimp_lists_fallback' ); 
  15.  
  16. $cached_lists = get_transient( 'mc4wp_mailchimp_lists' ); 
  17.  
  18. // if force_fallback is true, get lists from older transient 
  19. if( $force_fallback ) { 
  20. $cached_lists = get_transient( 'mc4wp_mailchimp_lists_fallback' ); 
  21.  
  22. // got lists? if not, proceed with API call. 
  23. if( empty( $cached_lists ) ) { 
  24.  
  25. // make api request for lists 
  26. $api = mc4wp_get_api(); 
  27. $lists_data = $api->get_lists(); 
  28.  
  29. if ( is_array( $lists_data ) ) { 
  30.  
  31. $lists = array(); 
  32.  
  33. foreach ( $lists_data as $list ) { 
  34.  
  35. $lists["{$list->id}"] = (object) array( 
  36. 'id' => $list->id,  
  37. 'name' => $list->name,  
  38. 'subscriber_count' => $list->stats->member_count,  
  39. 'merge_vars' => array(),  
  40. 'interest_groupings' => array() 
  41. ); 
  42.  
  43. // get interest groupings 
  44. $groupings_data = $api->get_list_groupings( $list->id ); 
  45. if ( $groupings_data ) { 
  46. $lists["{$list->id}"]->interest_groupings = array_map( array( $this, 'strip_unnecessary_grouping_properties' ), $groupings_data ); 
  47.  
  48.  
  49. // get merge vars for all lists at once 
  50. $merge_vars_data = $api->get_lists_with_merge_vars( array_keys( $lists ) ); 
  51. if ( $merge_vars_data ) { 
  52. foreach ( $merge_vars_data as $list ) { 
  53. // add merge vars to list 
  54. $lists["{$list->id}"]->merge_vars = array_map( array( $this, 'strip_unnecessary_merge_vars_properties' ), $list->merge_vars ); 
  55.  
  56. // store lists in transients 
  57. set_transient( 'mc4wp_mailchimp_lists', $lists, ( 24 * 3600 ) ); // 1 day 
  58. set_transient( 'mc4wp_mailchimp_lists_fallback', $lists, 1209600 ); // 2 weeks 
  59.  
  60. return $lists; 
  61. } else { 
  62. // api request failed, get fallback data (with longer lifetime) 
  63. $cached_lists = get_transient( 'mc4wp_mailchimp_lists_fallback' ); 
  64.  
  65. if ( ! $cached_lists ) { 
  66. return array(); 
  67.  
  68.  
  69. return $cached_lists; 
  70.  
  71. /** 
  72. * Get a given MailChimp list 
  73. * @param int $list_id 
  74. * @param bool $force_renewal 
  75. * @param bool $force_fallback 
  76. * @return bool 
  77. */ 
  78. public function get_list( $list_id, $force_renewal = false, $force_fallback = false ) { 
  79. $lists = $this->get_lists( $force_renewal, $force_fallback ); 
  80.  
  81. if( isset( $lists[$list_id] ) ) { 
  82. return $lists[$list_id]; 
  83.  
  84. return false; 
  85.  
  86. /** 
  87. * Get the name of the MailChimp list with the given ID. 
  88. * @param int $id 
  89. * @return string 
  90. */ 
  91. public function get_list_name( $id ) { 
  92. $list = $this->get_list( $id ); 
  93.  
  94. if( is_object( $list ) && isset( $list->name ) ) { 
  95. return $list->name; 
  96.  
  97. return ''; 
  98.  
  99. /** 
  100. * Get the interest grouping object for a given list. 
  101. * @param string $list_id ID of MailChimp list that contains the grouping 
  102. * @param string $grouping_id ID of the Interest Grouping 
  103. * @return object|null 
  104. */ 
  105. public function get_list_grouping( $list_id, $grouping_id ) { 
  106. $list = $this->get_list( $list_id, false, true ); 
  107.  
  108. if( is_object( $list ) && isset( $list->interest_groupings ) ) { 
  109. foreach( $list->interest_groupings as $grouping ) { 
  110.  
  111. if( $grouping->id !== $grouping_id ) { 
  112. continue; 
  113.  
  114. return $grouping; 
  115.  
  116. return null; 
  117.  
  118. /** 
  119. * Get the name of a list grouping by its ID 
  120. * @param $list_id 
  121. * @param $grouping_id 
  122. * @return string 
  123. */ 
  124. public function get_list_grouping_name( $list_id, $grouping_id ) { 
  125.  
  126. $grouping = $this->get_list_grouping( $list_id, $grouping_id ); 
  127. if( $grouping ) { 
  128. return $grouping->name; 
  129.  
  130. return ''; 
  131.  
  132. /** 
  133. * Get the group object for a group in an interest grouping 
  134. * @param string $list_id ID of MailChimp list that contains the grouping 
  135. * @param string $grouping_id ID of the Interest Grouping containing the group 
  136. * @param string $group_id_or_name ID or name of the Group 
  137. * @return object|null 
  138. */ 
  139. public function get_list_grouping_group( $list_id, $grouping_id, $group_id_or_name ) { 
  140. $grouping = $this->get_list_grouping( $list_id, $grouping_id ); 
  141. if( is_object( $grouping ) && isset( $grouping->groups ) ) { 
  142. foreach( $grouping->groups as $group ) { 
  143.  
  144. if( $group->id == $group_id_or_name || $group->name === $group_id_or_name ) { 
  145. return $group; 
  146.  
  147.  
  148. return null; 
  149.  
  150. /** 
  151. * Returns number of subscribers on given lists. 
  152. * @param array $list_ids Array of list id's. 
  153. * @return int Sum of subscribers for given lists. 
  154. */ 
  155. public function get_subscriber_count( $list_ids ) { 
  156.  
  157. // don't count when $list_ids is empty or not an array 
  158. if( ! is_array( $list_ids ) || count( $list_ids ) === 0 ) { 
  159. return 0; 
  160.  
  161. $list_counts = get_transient( 'mc4wp_list_counts' ); 
  162.  
  163. if ( false === $list_counts ) { 
  164. // make api call 
  165. $api = mc4wp_get_api(); 
  166. $lists = $api->get_lists(); 
  167. $list_counts = array(); 
  168.  
  169. if ( is_array( $lists ) ) { 
  170.  
  171. foreach ( $lists as $list ) { 
  172. $list_counts["{$list->id}"] = $list->stats->member_count; 
  173.  
  174. $transient_lifetime = apply_filters( 'mc4wp_lists_count_cache_time', 1200 ); // 20 mins by default 
  175.  
  176. set_transient( 'mc4wp_list_counts', $list_counts, $transient_lifetime ); 
  177. set_transient( 'mc4wp_list_counts_fallback', $list_counts, 86400 ); // 1 day 
  178. } else { 
  179. // use fallback transient 
  180. $list_counts = get_transient( 'mc4wp_list_counts_fallback' ); 
  181. if ( false === $list_counts ) { 
  182. return 0; 
  183.  
  184. // start calculating subscribers count for all list combined 
  185. $count = 0; 
  186. foreach ( $list_ids as $id ) { 
  187. $count += ( isset( $list_counts[$id] ) ) ? $list_counts[$id] : 0; 
  188.  
  189. return apply_filters( 'mc4wp_subscriber_count', $count ); 
  190.  
  191. /** 
  192. * Build the group array object which will be stored in cache 
  193. * @param object $group 
  194. * @return object 
  195. */ 
  196. public function strip_unnecessary_group_properties( $group ) { 
  197. return (object) array( 
  198. 'name' => $group->name,  
  199. ); 
  200.  
  201. /** 
  202. * Build the groupings array object which will be stored in cache 
  203. * @param object $grouping 
  204. * @return object 
  205. */ 
  206. public function strip_unnecessary_grouping_properties( $grouping ) { 
  207. return (object) array( 
  208. 'id' => $grouping->id,  
  209. 'name' => $grouping->name,  
  210. 'groups' => array_map( array( $this, 'strip_unnecessary_group_properties' ), $grouping->groups ),  
  211. 'form_field' => $grouping->form_field,  
  212. ); 
  213.  
  214. /** 
  215. * Build the merge_var array object which will be stored in cache 
  216. * @param object $merge_var 
  217. * @return object 
  218. */ 
  219. public function strip_unnecessary_merge_vars_properties( $merge_var ) { 
  220. $array = array( 
  221. 'name' => $merge_var->name,  
  222. 'field_type' => $merge_var->field_type,  
  223. 'req' => $merge_var->req,  
  224. 'tag' => $merge_var->tag,  
  225. ); 
  226.  
  227. if ( isset( $merge_var->choices ) ) { 
  228. $array['choices'] = $merge_var->choices; 
  229.  
  230. return (object) $array; 
  231.  
  232.