-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
plugin.php
70 lines (57 loc) · 1.89 KB
/
plugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
/*
Plugin Name: Cache stat pages
Plugin URI: https://github.com/YOURLS/cache-stats-pages
Description: Cache stat pages. Needs YOURLS 1.5.1+
Version: 1.0.2
Author: Ozh
Author URI: http://ozh.org/
*/
define ( 'OZH_YCACHE_DURATION', 21600 ); // cache page for 6 hours
/********** DO NOT EDIT FURTHER ************/
// No direct call
if( !defined( 'YOURLS_ABSPATH' ) ) die();
// Cache path
function ozh_ycache_get_cachefile() {
return dirname( __FILE__) .'/cache/'. trim( yourls_get_request(), '+' ) . '.html';
}
// Cache pages
yourls_add_action( 'pre_html_head', 'ozh_ycache_start' );
function ozh_ycache_start( $args ) {
// Only if we're viewing a stat page
$context = $args[0];
if( !defined( 'YOURLS_INFOS' ) or !YOURLS_INFOS or $context != 'infos' )
return;
$cachefile = ozh_ycache_get_cachefile();
// Serve from cache if younger than OZH_YCACHE_DURATION
if ( file_exists( $cachefile ) && filesize( $cachefile ) > 1000
&& ( time() - OZH_YCACHE_DURATION < filemtime( $cachefile ) ) ) {
$data = file_get_contents( $cachefile );
echo $data;
echo "<!-- Cached ".date('jS F Y H:i', filemtime($cachefile)).". Current time: ".date('jS F Y H:i')." -->\n";
exit;
}
// Serve fresh otherwise
yourls_add_action( 'shutdown', 'ozh_ycache_shutdown' );
ob_start(); // start the output buffer
}
// Create cache directory on activation
yourls_add_action( 'activated_'.yourls_plugin_basename( __FILE__ ), 'ozh_ycache_setup' );
function ozh_ycache_setup() {
$dir = dirname( __FILE__ );
if( !is_dir( $dir.'/cache' ) ) {
mkdir( $dir.'/cache', 0600 );
}
}
function ozh_ycache_shutdown() {
// save the contents of output buffer to the file
$cachefile = ozh_ycache_get_cachefile();
$fp = fopen( $cachefile, 'w' );
$data = ob_get_contents();
fwrite($fp, $data);
fclose($fp);
// Send the output to the browser
ob_end_clean();
echo $data;
echo "<!-- Fresh ".date('jS F Y H:i')." -->\n";
}