Skip to content

Instantly share code, notes, and snippets.

@JohnRDOrazio
Last active October 17, 2020 19:35
Show Gist options
  • Save JohnRDOrazio/4c7bb32387275caea61bf4348b49a3e2 to your computer and use it in GitHub Desktop.
Save JohnRDOrazio/4c7bb32387275caea61bf4348b49a3e2 to your computer and use it in GitHub Desktop.
WordPress AJAX example for Liturgical Calendar API
jQuery(document).ready(function($) {
var data = {
'action': 'call_litcal_api',
'nationalpreset': 'USA'
};
// since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
jQuery.get(ajaxurl, data, function(response) {
alert('Got this from the LitCal API: ' + response);
});
});
define("TRANSIENT_PREFIX","LitCal_");
function callLitCalAPI(){
//build the querystring from the GET or POST parameters, we're using GET here...
$querystring = "";
//see the allowed parameters here: https://johnromanodorazio.com/LiturgicalCalendar/dist/#/Main%20API%20endpoint/retrieveEventsGET
$allowedParameters = ["diocesanpreset","nationalpreset","locale","epiphany","ascension","corpuschristi","year","returntype"];
$parameters = [];
foreach($allowedParameters as $param){
if(isset($_GET[$param])){
$parameters[] = $param . "=" . $_GET[$param];
}
}
$querystring = implode("&",$parameters);
//if we don't already have the information stored in a transient,
//then we will call the API endpoint
if (false === ($output = get_transient(TRANSIENT_PREFIX.md5($querystring)))) {
//We will make a secure connection to the BibleGet service endpoint,
//if this server's OpenSSL and CURL versions support TLSv1.2
$curl_version = curl_version();
$ssl_version = str_replace('OpenSSL/', '', $curl_version['ssl_version']);
if (version_compare($curl_version['version'], '7.34.0', '>=') && version_compare($ssl_version, '1.0.1', '>=')) {
//we should be good to go for secure SSL communication supporting TLSv1_2
$ch = curl_init("https://johnromanodorazio.com/LiturgicalCalendar/LitCalEngine.php?" . $querystring);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
} else {
$ch = curl_init("http://johnromanodorazio.com/LiturgicalCalendar/LitCalEngine.php?" . $querystring);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
if (ini_get('safe_mode') || ini_get('open_basedir')) {
// if safe mode is on, we can't use some settings
} else {
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
}
$output = curl_exec($ch);
if ($output && !curl_errno($ch)) {
//we seem to have successfully retrieved our JSON data from the API
} else {
$errs = new stdClass();
$errs->error = "There was an error communicating with the Liturgical Calendar API, please wait a few minutes and try again" . ': '' . curl_error($ch) . '': ' . $finalquery;
$output = json_encode($errs);
}
curl_close($ch);
//this will ensure that the API endpoint will not be called more than once every 7 days
set_transient(TRANSIENT_PREFIX.md5($querystring), $output, 7 * 24 * HOUR_IN_SECONDS);
}
echo $output;
wp_die();
}
add_action('wp_ajax_call_litcal_api', 'callLitCalAPI');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment