Last active
March 14, 2019 22:06
-
-
Save corypratt/6a6ce38c3d945640d38f2b40f8569cd2 to your computer and use it in GitHub Desktop.
Wordpress function to get episode stats from Simplecast
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* This function can be used to pull total episode stats from Simplecast | |
* | |
*/ | |
function cp_get_episode_stats( $podcast_id ) { | |
if ( ! $podcast_id) { | |
return "-"; | |
} | |
$podcast = $podcast_id; | |
$url = 'https://api.simplecast.com/v1/podcasts/' . $podcast . '/statistics.json'; | |
$api_key = 'REPLACE WITH YOUR API KEY'; | |
$headers = array( 'X-API-KEY' => $api_key ); | |
$request = wp_remote_get( $url, array( 'headers' => $headers ) ); | |
if (is_wp_error($request)) { | |
return "-"; | |
} | |
$body = wp_remote_retrieve_body( $request ); | |
$data = json_decode( $body ); | |
$listens = $data->total_listens; | |
if ($listens > 999 && $listens <= 9999) { | |
$result = floor($listens / 1000) . 'K'; | |
} elseif ($listens > 9999 && $listens <= 999999) { | |
$result = round($listens/1000, 1) . 'K'; | |
} elseif ($listens > 999999) { | |
$result = floor($listens / 1000000) . 'M'; | |
} else { | |
$result = $listens; | |
} | |
return $result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment