Hi mitcho,
Here are the added codes on YARPP Version 3.3.2. to add the automatic back end timed caching feature on YARPP.
David
Step 1:
add an option
add following line in line 58 in includes.php (Deactivate and then reactive plugin to take effect.)
‘backend_caching_only’ => false,
Step 2:
add the switch to turn off front end cache building
changed line 224 in magic.php to
if (!$backend_caching_only) yarpp_cache_enforce($reference_ID);
Step 3:
add following ajax service function to the end of service.php
function do_cache_one() {
global $wpdb;
check_ajax_referer( “nonceyyycache” );
if (!$_POST[‘divpostid’]) {
$id=$wpdb->get_var(“select min(reference_ID) from wp_yarpp_related_cache where reference_ID>0” ,0);
if(!$id) $id = $wpdb->get_var(“select max(ID), count(ID) from $wpdb->posts as p where p.post_status = ‘publish'”,0);
else {$id = $wpdb->get_var(“select ID, post_title from $wpdb->posts where ID < $id and post_status = ‘publish’ and ifnull(post_title,”) != ” order by ID desc limit 1″,0);
}
} else {
$id = $_POST[‘divpostid’];
}
$sql = “select post_parent from $wpdb->posts where ID=’$id'”;
$parent_ID = $wpdb->get_var($sql);
if ($parent_ID != $id and $parent_ID)
$id = $parent_ID;
// $id must be numeric to be passed to this function
$id = intval($id);
$result = yarpp_cache_enforce($id,true);
$id = $wpdb->get_var(“select ID, post_title from $wpdb->posts where ID < $id and post_status = ‘publish’ and ifnull(post_title,”) != ” order by ID desc limit 1″,0);
echo $id;
die();
}
add_action( ‘wp_ajax_cache_one’, ‘do_cache_one’ );
step 4:
add Cache Building options and ajax code in the YARPP setting page:
add following codes in line 285 in options-meta-boxes.php
class YARPP_Meta_Box_Cache extends YARPP_Meta_Box {
function display() {
global $yarpp_myisam;
?>
<p><?php _e(‘By default (when unchecked), YARPP builds cache by both front end when users view posts and back end when a post is saved or updated. For most servers, the default setting is recommended.
</p><p>
For busy servers who can not use YARPP\’s default setting, we\’ve added a new back end only cache building option for you. Just check the check box below and click “Update options” button. Then, you build cache here using the server friendly caching method from the back end only. Click “Start Auto Process” button to automatically build cache one post per minute. The process will stop when all posts are processed. Or you can click “Stop Auto Process” button to stop the process and resume by clicking “Start Auto Process” button again. The cache table will be automatically built from the highest post ID to the lowest post ID.’,’yarpp’);?> </p>
<table class=”form-table” style=”margin-top: 0; clear:none;”>
<tbody>
<?php
$this->checkbox(‘backend_caching_only’,__(“Disable cache building from front end. Keep back end cache building.”,’yarpp’));
?>
</tbody>
</table>
<?php
$nonce = wp_create_nonce( ‘nonceyyycache’ );
?>
<script type=’text/javascript’>
function myajax() {
var myid=jQuery( ‘#divpostid’ ).val();
jQuery.ajax({
type: “post”,url: “admin-ajax.php”,data: { action: ‘cache_one’, divpostid: escape( jQuery( ‘#divpostid’ ).val() ), _ajax_nonce: ‘<?php echo $nonce; ?>’ },
success: function(data){
if (data <=0) {
alert(“process finished at “+ data);
stopCount();
jQuery(“#last”).html(myid);
jQuery(“#formstatus”).html(“Stoped at ID ” +data+” at ” +new Date());
jQuery(“#loading”).fadeOut(‘slow’);
return false;
}
jQuery(“#divpostid”).val(data);
var ycount=jQuery( ‘#ycount’ ).html();
var yycount= Number(ycount)+1;
if (yycount==1) jQuery(“#first”).html(data);
jQuery(“#ycount”).html(yycount);
}
});
return false;
}
var t;
var x;
var timer_is_on=0;
function timedCount() {
x=60000; // 60 seconds x1000 span time
t=setTimeout(“timedCount(), myajax()”, x);
}
function doTimer() {
if (!timer_is_on) {
jQuery(“#loading”).fadeIn(‘fast’);
jQuery(“#stop”).fadeIn(‘slow’);
jQuery(“#onepost”).fadeOut(‘slow’);
jQuery(“#start”).fadeOut(‘slow’);
timer_is_on=1;
timedCount();
}
}
function stopCount() {
clearTimeout(t);
timer_is_on=0;
jQuery(“#loading”).fadeOut(‘slow’);
jQuery(“#stop”).fadeOut(‘slow’);
jQuery(“#onepost”).fadeIn(‘slow’);
jQuery(“#start”).fadeIn(‘slow’);
jQuery(“#formstatus”).html(“Stoped at ” +new Date());
}
–>
</script>
<style type=’text/css’>
#loading { clear:both; background:url(images/loading.gif) center top no-repeat; text-align:center;padding:33px 0px 0px 0px; font-size:12px;display:none; font-family:Verdana, Arial, Helvetica, sans-serif; }
#stop { background:yellow;display:none; border-color:blue;-moz-border-radius: 3px; border-width:1px;border-style:solid; }
</style>
<div class=”wrap”>
<p>
<button type=”button” id=”onepost” onclick=”myajax()”>Process One Post</button>
<button type=”button” id=”start” onClick=”myajax(); doTimer()” />Start Auto Process</button>
<button type=”button” id=”stop” onClick=”stopCount()” />Stop Auto Process</button>
</p>
<p>Post ID: <input type=’text’ name=’divpostid’ id=’divpostid’ value=” /></p>
<span id=’first’></span> – <span id=’last’></span>
Item processed: <span id=’ycount’>0</span>
<div id=’formstatus’></div>
<div id=’loading’>Caching in Process. Click Stop button to stop!</div>
</div>
<?php
}
}
add_meta_box(‘yarpp_cache_building’, __(‘”Cache Building” options’,’yarpp’), array(new YARPP_Meta_Box_Cache, ‘display’), ‘settings_page_yarpp’, ‘normal’, ‘core’);