Skip to content

Instantly share code, notes, and snippets.

@pagelab
Created April 7, 2014 21:06
Show Gist options
  • Save pagelab/10056056 to your computer and use it in GitHub Desktop.
Save pagelab/10056056 to your computer and use it in GitHub Desktop.
<?php
/**
* Show Mad Mimi subscriber count w/cache.
*/
function jm_madmimi_subscribers() {
$email = '';
$api = '';
// check and get cached subscriber count
if ( false === ( $subscribers = get_transient( 'mm_subscriber_count' ) ) ) {
// if no cache set pull from MadMimi API
$response = wp_remote_retrieve_body( wp_remote_get( "https://api.madmimi.com/audience_lists/lists.json?username={$email}&api_key={$api}" ) );
// if error
if ( is_wp_error( $response ) ) {
// fallback count
$subscribers = 42;
} else {
// decode and format MadMimi API data
$json = json_decode( $response, true );
$subscribers = number_format( $json[0][list_size] );
}
// store count to expire in 24 hours
set_transient( 'mm_subscriber_count', $subscribers, 60*60*24 );
}
return $subscribers;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment