Subscriber.php
5.63 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
<?php
namespace WP_Rocket\Engine\Admin\Database;
use WP_Rocket\Event_Management\Subscriber_Interface;
use WP_Rocket\Admin\Options_Data;
/**
* Subscriber for the database optimization
*/
class Subscriber implements Subscriber_Interface {
/**
* Optimization process instance.
*
* @since 3.4
*
* @var Optimization
*/
private $optimize;
/**
* WP Rocket Options instance.
*
* @since 3.4
*
* @var Options_Data
*/
private $options;
/**
* Constructor
*
* @param Optimization $optimize Optimize instance.
* @param Options_Data $options WP Rocket options.
*/
public function __construct( Optimization $optimize, Options_Data $options ) {
$this->optimize = $optimize;
$this->options = $options;
}
/**
* Return an array of events that this subscriber wants to listen to.
*
* @since 3.3
*
* @return array
*/
public static function get_subscribed_events() {
return [
'cron_schedules' => 'add_cron_schedule',
'init' => 'database_optimization_scheduled',
'rocket_database_optimization_time_event' => 'cron_optimize',
'pre_update_option_' . WP_ROCKET_SLUG => 'save_optimize',
'admin_notices' => [
[ 'notice_process_running' ],
[ 'notice_process_complete' ],
],
];
}
/**
* Add a new interval for the cron job.
* This adds a weekly/monthly interval for database optimization.
*
* @since 3.4
*
* @param array $schedules An array of intervals used by cron jobs.
* @return array Updated array of intervals.
*/
public function add_cron_schedule( $schedules ) {
if ( ! $this->options->get( 'schedule_automatic_cleanup', false ) ) {
return $schedules;
}
switch ( $this->options->get( 'automatic_cleanup_frequency', 'weekly' ) ) {
case 'weekly':
$schedules['weekly'] = [
'interval' => 604800,
'display' => __( 'weekly', 'rocket' ),
];
break;
case 'monthly':
$schedules['monthly'] = [
'interval' => 2592000,
'display' => __( 'monthly', 'rocket' ),
];
break;
}
return $schedules;
}
/**
* Plans database optimization cron
* If the task is not programmed, it is automatically triggered
*
* @since 2.8
*
* @see process_handler()
*/
public function database_optimization_scheduled() {
if ( ! $this->options->get( 'schedule_automatic_cleanup', false ) ) {
return;
}
if ( ! wp_next_scheduled( 'rocket_database_optimization_time_event' ) ) {
wp_schedule_event( time(), $this->options->get( 'automatic_cleanup_frequency', 'weekly' ), 'rocket_database_optimization_time_event' );
}
}
/**
* Database Optimization cron callback
*
* @since 3.0.4
*/
public function cron_optimize() {
$items = array_filter( array_keys( $this->optimize->get_options() ), [ $this->options, 'get' ] );
if ( empty( $items ) ) {
return;
}
$this->optimize->process_handler( $items );
}
/**
* Launches the database optimization when the settings are saved with optimize button
*
* @since 2.8
*
* @see process_handler()
*
* @param array $value The new, unserialized option value.
* @return array
*/
public function save_optimize( $value ) {
if ( empty( $_POST ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
return $value;
}
if ( empty( $value ) || ! isset( $value['submit_optimize'] ) ) {
return $value;
}
unset( $value['submit_optimize'] );
if ( ! current_user_can( 'rocket_manage_options' ) ) {
return $value;
}
$items = [];
$db_options = $this->optimize->get_options();
foreach ( $value as $key => $option_value ) {
if ( isset( $db_options[ $key ] ) && 1 === $option_value ) {
$items[] = $key;
}
}
if ( empty( $items ) ) {
return $value;
}
$this->optimize->process_handler( $items );
return $value;
}
/**
* This notice is displayed after launching the database optimization process
*
* @since 2.11
*/
public function notice_process_running() {
$screen = get_current_screen();
if ( ! current_user_can( 'rocket_manage_options' ) ) {
return;
}
if ( 'settings_page_wprocket' !== $screen->id ) {
return;
}
$notice = get_transient( 'rocket_database_optimization_process' );
if ( ! $notice ) {
return;
}
\rocket_notice_html(
[
'status' => 'info',
'message' => esc_html__( 'Database optimization process is running', 'rocket' ),
]
);
}
/**
* This notice is displayed when the database optimization process is complete
*
* @since 2.11
*/
public function notice_process_complete() {
$screen = get_current_screen();
if ( ! current_user_can( 'rocket_manage_options' ) ) {
return;
}
if ( 'settings_page_wprocket' !== $screen->id ) {
return;
}
$optimized = get_transient( 'rocket_database_optimization_process_complete' );
if ( false === $optimized ) {
return;
}
$db_options = $this->optimize->get_options();
delete_transient( 'rocket_database_optimization_process_complete' );
$message = esc_html__( 'Database optimization process is complete. Everything was already optimized!', 'rocket' );
if ( ! empty( $optimized ) ) {
$message = esc_html__( 'Database optimization process is complete. List of optimized items below:', 'rocket' );
}
if ( ! empty( $optimized ) ) {
$message .= '<ul>';
foreach ( $optimized as $key => $number ) {
$message .= '<li>' .
/* translators: %1$d = number of items optimized, %2$s = type of optimization */
sprintf( esc_html__( '%1$d %2$s optimized.', 'rocket' ), $number, $db_options[ $key ] )
. '</li>';
}
$message .= '</ul>';
}
\rocket_notice_html(
[
'message' => $message,
]
);
}
}