Last active
May 6, 2016 00:37
-
-
Save bryanmonzon/68035936bd0890e1fe7a to your computer and use it in GitHub Desktop.
If you're using WordPress, you can easily cache your donations using these two functions. Make sure to enter your API key and SUBDOMAIN
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 | |
/** | |
* Get and store API call in a transient for better | |
* performance. | |
* @return object from either a transient or the API directly | |
*/ | |
function notforsale_get_donations() | |
{ | |
$donations = get_transient( 'dntly_donations' ); //Get's the value stored in the transient | |
//Check to see if anything exists | |
if( false === $donations ) { | |
$donations = notforsale_get_donations_api_call(); //Make the API call | |
if( $donations->success === true ){ | |
set_transient( 'dntly_donations', $donations, HOUR_IN_SECONDS ); //If successfull, set the transient for use for the next hour | |
} | |
} | |
return $donations; | |
} | |
/** | |
* Makes the call to get donations. | |
* @return object returns the API object from Donately | |
*/ | |
function notforsale_get_donations_api_call() | |
{ | |
$url = 'https://YOUR_SUB_DOMAIN.dntly.com/api/v1/admin/donations.json'; | |
$count = 10; | |
$minutes = HOUR_IN_SECONDS; | |
$time = time(); | |
$hour_ago = $minutes - $time; | |
$token = 'ENTER API KEY HERE'; | |
$authorization = 'Basic ' . base64_encode("{$token}:"); | |
$headers = array( 'Authorization' => $authorization, 'sslverify' => false ); | |
$payload = array( | |
'count' => $count, | |
'order' => 'desc', | |
'created[gt]' => $hour_ago, | |
// 'order_by' => 'amount_in_cents', | |
); | |
$response = wp_safe_remote_get( $url, array( | |
'method' => 'GET', | |
'timeout' => 45, | |
'redirection' => 5, | |
'httpversion' => '1.0', | |
'blocking' => true, | |
'headers' => $headers, | |
'body' => http_build_query( $payload ), | |
'cookies' => array() | |
)); | |
$body = wp_remote_retrieve_body( $response ); | |
$donors = json_decode( $body ); | |
return $donors; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment