-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathExtension_AlwaysCached_Environment.php
141 lines (123 loc) · 2.8 KB
/
Extension_AlwaysCached_Environment.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
/**
* File: Extension_AlwaysCached_Environment.php
*
* Controller for AlwaysCached extension environment.
*
* @since 2.8.0
*
* @package W3TC
*/
namespace W3TC;
/**
* AlwaysCached Admin Environment.
*
* @since 2.8.0
*/
class Extension_AlwaysCached_Environment {
/**
* Fixes environment on admin request.
*
* @since 2.8.0
*
* @return void|null
*/
public static function fix_on_wpadmin_request() {
if ( ! Extension_AlwaysCached_Plugin::is_enabled() ) {
return null;
}
Extension_AlwaysCached_Queue::create_table();
}
/**
* Fixes environment once event occurs.
*
* @since 2.8.0
*
* @param Config $config Config data.
* @param string $event Event key.
* @param Config|null $old_config Old config data.
*
* @throws Util_Environment_Exceptions Exception.
*
* @return void|null
*/
public static function fix_on_event( $config, $event, $old_config = null ) {
if ( ! Extension_AlwaysCached_Plugin::is_enabled() ) {
return null;
}
$exs = new Util_Environment_Exceptions();
try {
// Drop AlwaysCached DB table on activation and recreate.
self::handle_tables(
'activate' === $event,
true
);
} catch ( \Exception $ex ) {
$exs->push( $ex );
}
if ( count( $exs->exceptions() ) > 0 ) {
throw $exs;
}
}
/**
* Fixes environment after plugin deactivation.
*
* @since 2.8.0
*
* @throws Util_Environment_Exceptions Exception.
*
* @return void
*/
public static function fix_after_deactivation() {
$exs = new Util_Environment_Exceptions();
try {
// Drop AlwaysCached DB table on deactivation.
self::handle_tables( true, false );
} catch ( \Exception $ex ) {
$exs->push( $ex );
}
if ( count( $exs->exceptions() ) > 0 ) {
throw $exs;
}
}
/**
* Sets requirment instructions for AlwaysCached on install page.
*
* @since 2.8.0
*
* @param Config $config Config data.
*
* @return array|null
*/
public function get_instructions( $config ) {
if ( ! Extension_AlwaysCached_Plugin::is_enabled() ) {
return null;
}
$instructions = array();
$instructions[] = array(
'title' => __( 'Always Cached module: Required Database SQL', 'w3-total-cache' ),
'content' => Extension_AlwaysCached_Queue::drop_table_sql() . ";\n" . Extension_AlwaysCached_Queue::create_table_sql() . ';',
'area' => 'database',
);
return $instructions;
}
/**
* Create table
*
* @param bool $drop Drop flag.
* @param bool $create Create flag.
*
* @throws Util_Environment_Exception Exception.
*/
private static function handle_tables( $drop, $create ) {
if ( ! Extension_AlwaysCached_Plugin::is_enabled() ) {
return null;
}
if ( $drop ) {
$sql = Extension_AlwaysCached_Queue::drop_table();
}
if ( $create ) {
Extension_AlwaysCached_Queue::create_table();
}
}
}