Skip to content

Commit

Permalink
better handling of bible quotes cache by prefixing transients, added …
Browse files Browse the repository at this point in the history
…option in settings page to flush bible quotes cache
  • Loading branch information
Lwangaman authored and Lwangaman committed May 21, 2020
1 parent 8174197 commit 39b151c
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 15 deletions.
26 changes: 24 additions & 2 deletions bibleget-io.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/*
* Plugin Name: BibleGet I/O
* Version: 5.6
* Version: 5.7
* Plugin URI: https://www.bibleget.io/
* Description: Easily insert Bible quotes from a choice of Bible versions into your articles or pages with the "Bible quote" block or with the shortcode [bibleget].
* Author: John Romano D'Orazio
Expand Down Expand Up @@ -29,7 +29,7 @@

//TODO: better ui for the customizer, use sliders

define("BIBLEGETPLUGINVERSION", "v5_6");
define("BIBLEGETPLUGINVERSION", "v5_7");

if (!defined('ABSPATH')) {
header('Status: 403 Forbidden');
Expand Down Expand Up @@ -1465,6 +1465,28 @@ function bibleGetSetOptions()
}
add_action('wp_ajax_refresh_bibleget_server_data', 'bibleGetSetOptions');

function flushBibleQuotesCache(){
global $wpdb;
//The following SELECT should select both the transient and the transient_timeout
//This will also remove the Google Fonts API key transient if it uses the same prefix...
//I guess we'll just have to not use our defined prefix on the Google Fonts API key transient
//in order avoid this
$sql = "DELETE
FROM $wpdb->options
WHERE `option_name` LIKE '%transient_%".TRANSIENT_PREFIX."%'
";
//We shouldn't have to do a $wpdb->prepare here because there is no kind of user input anywhere
if($wpdb->query( $sql ) !== false){
echo 'cacheflushed';
}
else{
echo 'cacheNotFlushed';
}
wp_die();
}

add_action('wp_ajax_flush_bible_quotes_cache', 'flushBibleQuotesCache');

function searchByKeyword(){
$keyword = $_POST['keyword'];
$version = $_POST['version'];
Expand Down
30 changes: 30 additions & 0 deletions css/admin.css
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,30 @@ form input[type=color] {
left: 0;
}

#bibleget-settings-contents .flexcontainer {
display: flex;
justify-content: space-evenly;
padding: 15px;
margin: 15px;
border: 1px solid lightgray;
border-radius: 6px;
}

#bibleget-settings-contents .flexcontainer .flexitem {
flex-grow: 1;
margin: 15px;
border-right: 1px solid gray;
}

#bibleget-settings-contents .flexcontainer .flexitem:last-child {
border-right: 0px;
}

#bibleget-settings-contents .flexcontainer .flexitem button {
display: block;
margin: 0px auto;
}

#bibleget_ajax_spinner {
position: absolute;
background-color: rgba(0, 0, 0, 0.7);
Expand Down Expand Up @@ -180,3 +204,9 @@ form input[type=color] {
#biblegetForceRefreshGFapiResults:hover,#biblegetGFapiKeyRetest:hover {
text-decoration: underline;
}

#versionselect {
overflow-y: hidden;
max-width: 100%;
width: 30em;
}
42 changes: 42 additions & 0 deletions js/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,48 @@ jQuery(document).ready(function($) {
});
});

jQuery('#bibleget-cache-flush-btn').on('click', function(){
jQuery.ajax({
type: 'POST',
url: bibleGetOptionsFromServer.ajax_url,
data: { action: 'flush_bible_quotes_cache' },
beforeSend : function() {
jQuery('#bibleget_ajax_spinner').show();
},
complete : function() {
jQuery('#bibleget_ajax_spinner').hide();
},
success : function(returndata) {
if (returndata == 'cacheflushed') {
jQuery(
"#bibleget-settings-notification")
.append(
'Bible quotes cache emptied successfully')
.fadeIn("slow");
} else {
jQuery("#bibleget-settings-notification").append(
'There was an error while attempting to flush the Bible quotes cache... Perhaps try again?')
.fadeIn("slow");
}
jQuery(".bibleget-settings-notification-dismiss")
.click(function() {
jQuery("#bibleget-settings-notification").fadeOut("slow");
});

},
error: function(xhr, ajaxOptions, thrownError){
jQuery("#bibleget-settings-notification")
.fadeIn("slow")
.append('Emptying of Bible quotes cache was not successful... ERROR: ' + xhr.responseText);
jQuery(".bibleget-settings-notification-dismiss")
.click(function() {
jQuery("#bibleget-settings-notification").fadeOut("slow");
});

}
});
});

if(typeof gfontsBatch !== 'undefined' && typeof gfontsBatch === 'object' && gfontsBatch.hasOwnProperty('job') && gfontsBatch.job.hasOwnProperty('gfontsPreviewJob') && gfontsBatch.job.gfontsPreviewJob === true && gfontsBatch.job.hasOwnProperty('gfontsWeblist') && typeof gfontsBatch.job.gfontsWeblist == 'object' && gfontsBatch.job.gfontsWeblist.hasOwnProperty('items') ){
//console.log('We have a gfontsPreviewJob to do! gfontsBatch: ');
//console.log(gfontsBatch);
Expand Down
31 changes: 20 additions & 11 deletions options.php
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ public function admin_print_scripts($hook)
if ($this->gfontsAPIkeyCheckResult == "SUCCESS") {
//We only want the transient to be set from the bibleget settings page, so we wait until now
// instead of doing it in the gfontsAPIkeyCheck (which is called on any admin interface)
set_transient(TRANSIENT_PREFIX.md5($this->options['googlefontsapi_key']), $this->gfontsAPIkeyCheckResult, 90 * 24 * HOUR_IN_SECONDS); // 90 giorni
set_transient(md5($this->options['googlefontsapi_key']), $this->gfontsAPIkeyCheckResult, 90 * 24 * HOUR_IN_SECONDS); // 90 giorni

$plugin_path = "";
// bibleGetWriteLog("about to initialize creation of admin page...");
Expand Down Expand Up @@ -543,8 +543,16 @@ public function create_admin_page()
echo "<div class=\"bibleget-dynamic-data-wrapper\">" . implode(", ", $this->biblebookslangs) . "</div>";
?></li>
</ol>
<p><?php _e("This information from the BibleGet server is cached locally to improve performance. If new versions have been added to the BibleGet server or new languages are supported, this information might be outdated. In that case you can click on the button below to renew the information.", "bibleget-io"); ?></p>
<button id="bibleget-server-data-renew-btn" class="button button-secondary"><?php _e("RENEW INFORMATION FROM BIBLEGET SERVER", "bibleget-io") ?></button>
<div class="flexcontainer">
<div class="flexitem">
<p><?php _e("This information from the BibleGet server is cached locally to improve performance. If new versions have been added to the BibleGet server or new languages are supported, this information might be outdated. In that case you can click on the button below to renew the information.", "bibleget-io"); ?></p>
<button id="bibleget-server-data-renew-btn" class="button button-secondary"><?php _e("RENEW INFORMATION FROM BIBLEGET SERVER", "bibleget-io") ?></button>
</div>
<div class="flexitem">
<p><?php _e("If there has been a recent update to the plugin with new functionality, or a recent update to the BibleGet endpoint engine, you may have to flush the cached Bible quotes in order for any new functionalities to work correctly. The cached Bible quotes will be emptied within a week, click here to flush them immediately.", "bibleget-io"); ?></p>
<button id="bibleget-cache-flush-btn" class="button button-secondary"><?php _e("FLUSH CACHED BIBLE QUOTES", "bibleget-io") ?></button>
</div>
</div>
</div>
<div id="bibleget_ajax_spinner"><img src="<?php echo admin_url(); ?>images/wpspin_light-2x.gif" /></div>
</div>
Expand Down Expand Up @@ -632,6 +640,7 @@ public function favorite_version_callback()
echo '</optgroup>';
}
echo '</select>';
echo '<br /><i>'.__("In order to select multiple items, hold down CTRL key (Command key on Mac) while clicking items.","bibleget-io").'</i>';
echo '<input type="hidden" id="favorite_version" name="bibleget_settings[favorite_version]" value="" />';
}

Expand Down Expand Up @@ -662,14 +671,14 @@ public function googlefontsapikey_callback()
$gfontsAPIkeyTimeLeft = (count($timeLeft) > 0) ? "[" . implode(", ", $timeLeft) . "]" : "";

/* translators: refers to the outcome of the validity check of the Google Fonts API key */
echo '<span style="color:Green;font-weight:bold;margin-left:12px;">' . __("VALID", "bibleget-io") . '</span>';
echo '<span style="color:Green;font-weight:bold;margin-left:12px;">' . __("VALID", "bibleget-io") . '</span><br />';
echo ' <i>' . sprintf(__("Google Fonts API refresh scheduled in: %s", "bibleget-io"), $gfontsAPIkeyTimeLeft);
echo ' ' . sprintf(__("OR %s Click here %s to force refresh the list of fonts from the Google Fonts API", "bibleget-io"), '<span id="biblegetForceRefreshGFapiResults">', '</span>');
echo '</i>';
break;
case "CURL_ERROR":
/* translators: refers to the outcome of the validity check of the Google Fonts API key */
echo '<span style="color:DarkViolet;font-weight:bold;margin-left:12px;">' . __("CURL ERROR WHEN SENDING REQUEST", "bibleget-io") . '</span>';
echo '<span style="color:DarkViolet;font-weight:bold;margin-left:12px;">' . __("CURL ERROR WHEN SENDING REQUEST", "bibleget-io") . '</span><br />';
foreach ($this->gfontsAPI_errors as $er) {
if ($er == 403) {
echo '<br /><i style="color:DarkViolet;margin-left:12px;">';
Expand All @@ -683,11 +692,11 @@ public function googlefontsapikey_callback()
break;
case "JSON_ERROR":
/* translators: refers to the outcome of the validity check of the Google Fonts API key */
echo '<span style="color:Orange;font-weight:bold;margin-left:12px;">' . __("NO VALID JSON RESPONSE", "bibleget-io") . '</span>';
echo '<span style="color:Orange;font-weight:bold;margin-left:12px;">' . __("NO VALID JSON RESPONSE", "bibleget-io") . '</span><br />';
break;
case "REQUEST_NOT_SENT":
/* translators: refers to the outcome of the validity check of the Google Fonts API key */
echo '<span style="color:Red;font-weight:bold;margin-left:12px;">' . __("SERVER UNABLE TO MAKE REQUESTS", "bibleget-io") . '</span>';
echo '<span style="color:Red;font-weight:bold;margin-left:12px;">' . __("SERVER UNABLE TO MAKE REQUESTS", "bibleget-io") . '</span><br />';
break;
}
} else {
Expand Down Expand Up @@ -717,7 +726,7 @@ public function gfontsAPIkeyCheck()
$this->gfontsAPIkey = $this->options['googlefontsapi_key'];

//has this key been tested in the past 3 months at least?
if (false === ($result = get_transient(TRANSIENT_PREFIX.md5($this->options['googlefontsapi_key'])))) {
if (false === ($result = get_transient(md5($this->options['googlefontsapi_key'])))) {

//We will make a secure connection to the Google Fonts API endpoint
$curl_version = curl_version();
Expand Down Expand Up @@ -778,7 +787,7 @@ public function gfontsAPIkeyCheck()
//we have a previously saved api key which has been tested
//$result is not false
global $wpdb;
$transientKey = TRANSIENT_PREFIX.md5($this->options['googlefontsapi_key']);
$transientKey = md5($this->options['googlefontsapi_key']);
$transient_timeout = $wpdb->get_col("
SELECT option_value
FROM $wpdb->options
Expand Down Expand Up @@ -979,8 +988,8 @@ public function bibleGetForceRefreshGFontsResults()
{
check_ajax_referer('refresh_gfonts_results_nonce', 'security', TRUE); //no need for an "if", it will die if not valid
if (isset($_POST["gfontsApiKey"]) && $_POST["gfontsApiKey"] != "") {
if (get_transient(TRANSIENT_PREFIX.md5($_POST["gfontsApiKey"]))) {
delete_transient(TRANSIENT_PREFIX.md5($_POST["gfontsApiKey"]));
if (get_transient(md5($_POST["gfontsApiKey"]))) {
delete_transient(md5($_POST["gfontsApiKey"]));
echo 'TRANSIENT_DELETED';
wp_die();
}
Expand Down
11 changes: 9 additions & 2 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
Contributors: Lwangaman
Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=HDS7XQKGFHJ58
Tags: bible,shortcode,quote,citation,verses,bibbia,citazione,versetti,biblia,cita,versiculos,versets,citation
Requires at least: 3.3
Requires at least: 5.0
Tested up to: 5.4.1
Stable tag: 5.6
Stable tag: 5.7
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html

Expand Down Expand Up @@ -94,6 +94,10 @@ If you receive a 504 http status error it means that the connection with the Goo

== Changelog ==

= 5.7 =
* better handling of bible quotes cache by prefixing the transients
* added option to flush bible quotes cache from settings page

= 5.6 =
* turn off PHP error reporting!

Expand Down Expand Up @@ -261,6 +265,9 @@ If you receive a 504 http status error it means that the connection with the Goo

== Upgrade Notice ==

= 5.7 =
Versions prior to 5.1 must be updated. v5.4 adds a Gutenberg block

= 5.6 =
Versions prior to 5.1 must be updated. v5.4 adds a Gutenberg block

Expand Down

0 comments on commit 39b151c

Please sign in to comment.