1
|
|
|
<?php
|
2
|
|
|
|
3
|
|
|
namespace Rarst\Fragment_Cache;
|
4
|
|
|
|
5
|
|
|
use Pimple\Container;
|
6
|
|
|
|
7
|
|
|
/**
|
8
|
|
|
* Main plugin's class.
|
9
|
|
|
*/
|
10
|
|
|
class Plugin extends Container {
|
11
|
|
|
|
12
|
|
|
/** @var array $handlers Set of registered fragment handlers. */
|
13
|
|
|
protected $handlers = array();
|
14
|
|
|
|
15
|
|
|
/**
|
16
|
|
|
* Start the plugin after initial setup.
|
17
|
|
|
*/
|
18
|
|
|
public function run() {
|
19
|
|
|
|
20
|
|
|
add_action( 'init', array( $this, 'init' ) );
|
21
|
|
|
add_filter( 'update_blocker_blocked', array( $this, 'update_blocker_blocked' ) );
|
22
|
|
|
}
|
23
|
|
|
|
24
|
|
|
/**
|
25
|
|
|
* Enable registered fragment handlers on init.
|
26
|
|
|
*/
|
27
|
|
|
public function init() {
|
28
|
|
|
|
29
|
|
|
if (
|
30
|
|
|
'on' === filter_input( INPUT_POST, 'wp_customize' )
|
31
|
|
|
&& empty( filter_input( INPUT_POST, 'action' ) )
|
32
|
|
|
&& current_user_can( 'customize' )
|
33
|
|
|
) {
|
34
|
|
|
return; // We don’t want cache running in Customizer previews.
|
35
|
|
|
}
|
36
|
|
|
|
37
|
|
|
foreach ( $this->handlers as $key => $type ) {
|
38
|
|
|
if ( isset( $this[ $type ] ) ) {
|
39
|
|
|
/** @var Fragment_Cache $handler */
|
40
|
|
|
$handler = $this[ $type ];
|
41
|
|
|
$handler->enable();
|
42
|
|
|
} else {
|
43
|
|
|
unset( $this->handlers[ $key ] );
|
44
|
|
|
}
|
45
|
|
|
}
|
46
|
|
|
}
|
47
|
|
|
|
48
|
|
|
/**
|
49
|
|
|
* @see https://github.com/Rarst/update-blocker
|
50
|
|
|
*
|
51
|
|
|
* @param array $blocked Configuration data for blocked items.
|
52
|
|
|
*
|
53
|
|
|
* @return array
|
54
|
|
|
*/
|
55
|
|
|
public function update_blocker_blocked( $blocked ) {
|
56
|
|
|
|
57
|
|
|
$blocked['plugins'][] = plugin_basename( dirname( __DIR__ ) . '/fragment-cache.php' );
|
58
|
|
|
|
59
|
|
|
return $blocked;
|
60
|
|
|
}
|
61
|
|
|
|
62
|
|
|
/**
|
63
|
|
|
* Add (or override) cache handler and enable it.
|
64
|
|
|
*
|
65
|
|
|
* @param string $type Handler type name.
|
66
|
|
|
* @param string $class_name Handler class name to instance.
|
67
|
|
|
*/
|
68
|
|
|
public function add_fragment_handler( $type, $class_name ) {
|
69
|
|
|
|
70
|
|
|
if ( isset( $this[ $type ] ) ) {
|
71
|
|
|
/** @var Fragment_Cache $handler */
|
72
|
|
|
$handler = $this[ $type ];
|
73
|
|
|
$handler->disable();
|
74
|
|
|
unset( $this[ $type ] );
|
75
|
|
|
}
|
76
|
|
|
|
77
|
|
|
$this[ $type ] = function ( $plugin ) use ( $type, $class_name ) {
|
78
|
|
|
return new $class_name( array( 'type' => $type, 'timeout' => $plugin['timeout'] ) );
|
79
|
|
|
};
|
80
|
|
|
|
81
|
|
|
if ( ! in_array( $type, $this->handlers, true ) ) {
|
82
|
|
|
$this->handlers[] = $type;
|
83
|
|
|
}
|
84
|
|
|
}
|
85
|
|
|
}
|
86
|
|
|
|