PluginProbe
OPcache Manager / trunk
OPcache Manager vtrunk
trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.2.0 1.3.0 1.3.1 1.3.2 2.0.0 2.1.0 2.10.0 2.11.0 2.12.0 2.13.0 2.13.1 2.14.0 2.2.0 2.3.0 2.3.1 2.3.2 2.4.0 2.5.0 2.6.0 All 36 releases
opcache-manager / includes / features / class-scripts.php

class-scripts.php in OPcache Manager trunk, at includes/features/class-scripts.php

697 lines 21.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Scripts list
4 *
5 * Lists all available scripts.
6 *
7 * @package Features
8 * @author Pierre Lannoy <https://pierre.lannoy.fr/>.
9 * @since 1.0.0
10 */
11
12 namespace OPcacheManager\Plugin\Feature;
13
14 use OPcacheManager\System\Conversion;
15 use OPcacheManager\System\Logger;
16 use OPcacheManager\System\Date;
17 use OPcacheManager\System\Timezone;
18 use OPcacheManager\System\OPcache;
19 use OPcacheManager\System\Environment;
20
21 if ( ! class_exists( 'WP_List_Table' ) ) {
22 require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
23 }
24
25 /**
26 * Define the scripts list functionality.
27 *
28 * Lists all available scripts.
29 *
30 * @package Features
31 * @author Pierre Lannoy <https://pierre.lannoy.fr/>.
32 * @since 1.0.0
33 */
34 class Scripts extends \WP_List_Table {
35
36 /**
37 * The scripts handler.
38 *
39 * @since 1.0.0
40 * @var array $scripts The scripts list.
41 */
42 private $scripts = [];
43
44 /**
45 * The number of lines to display.
46 *
47 * @since 1.0.0
48 * @var integer $limit The number of lines to display.
49 */
50 private $limit = 0;
51
52 /**
53 * The page to display.
54 *
55 * @since 1.0.0
56 * @var integer $limit The page to display.
57 */
58 private $paged = 1;
59
60 /**
61 * The order by of the list.
62 *
63 * @since 1.0.0
64 * @var string $orderby The order by of the list.
65 */
66 private $orderby = 'script';
67
68 /**
69 * The order of the list.
70 *
71 * @since 1.0.0
72 * @var string $order The order of the list.
73 */
74 private $order = 'desc';
75
76 /**
77 * The current url.
78 *
79 * @since 1.0.0
80 * @var string $url The current url.
81 */
82 private $url = '';
83
84 /**
85 * The form nonce.
86 *
87 * @since 1.0.0
88 * @var string $nonce The form nonce.
89 */
90 private $nonce = '';
91
92 /**
93 * The action to perform.
94 *
95 * @since 1.0.0
96 * @var string $action The action to perform.
97 */
98 private $action = '';
99
100 /**
101 * The bulk args.
102 *
103 * @since 1.0.0
104 * @var array $bulk The bulk args.
105 */
106 private $bulk = [];
107
108 /**
109 * Initialize the class and set its properties.
110 *
111 * @since 1.0.0
112 */
113 public function __construct() {
114 parent::__construct(
115 [
116 'singular' => 'script',
117 'plural' => 'scripts',
118 'ajax' => true,
119 ]
120 );
121 global $wp_version;
122 if ( version_compare( $wp_version, '4.2-z', '>=' ) && $this->compat_fields && is_array( $this->compat_fields ) ) {
123 array_push( $this->compat_fields, 'all_items' );
124 }
125 $this->process_args();
126 $this->process_action();
127 $this->scripts = [];
128 if ( function_exists( 'opcache_get_status' ) ) {
129 try {
130 $raw = opcache_get_status( true );
131 if ( array_key_exists( 'scripts', $raw ) ) {
132 foreach ( $raw['scripts'] as $script ) {
133 if ( false === strpos( $script['full_path'], OPCM_ABSPATH ) ) {
134 continue;
135 }
136 $item = [];
137 $item['script'] = str_replace( OPCM_ABSPATH, './', $script['full_path'] );
138 $item['hit'] = $script['hits'];
139 $item['memory'] = $script['memory_consumption'];
140 $item['timestamp'] = $script['timestamp'];
141 $item['used'] = $script['last_used_timestamp'];
142 $this->scripts[] = $item;
143 }
144 }
145 } catch ( \Throwable $e ) {
146 \DecaLog\Engine::eventsLogger( OPCM_SLUG )->error( sprintf( 'Unable to query OPcache status: %s.', $e->getMessage() ), [ 'code' => $e->getCode() ] );
147 }
148 }
149 }
150
151 /**
152 * Default column formatter.
153 *
154 * @param array $item The current item.
155 * @param string $column_name The current column name.
156 * @return string The cell formatted, ready to print.
157 * @since 1.0.0
158 */
159 protected function column_default( $item, $column_name ) {
160 return $item[ $column_name ];
161 }
162
163 /**
164 * Check box column formatter.
165 *
166 * @param array $item The current item.
167 * @return string The cell formatted, ready to print.
168 * @since 1.0.0
169 */
170 public function column_cb( $item ) {
171 return sprintf(
172 '<input type="checkbox" name="bulk[]" value="%s" />',
173 $item['script']
174 );
175 }
176
177 /**
178 * "hit" column formatter.
179 *
180 * @param array $item The current item.
181 * @return string The cell formatted, ready to print.
182 * @since 1.0.0
183 */
184 protected function column_hit( $item ) {
185 return Conversion::number_shorten( $item['hit'] );
186 }
187
188 /**
189 * "memory" column formatter.
190 *
191 * @param array $item The current item.
192 * @return string The cell formatted, ready to print.
193 * @since 1.0.0
194 */
195 protected function column_memory( $item ) {
196 return Conversion::data_shorten( $item['memory'] );
197 }
198
199 /**
200 * "used" column formatter.
201 *
202 * @param array $item The current item.
203 * @return string The cell formatted, ready to print.
204 * @since 1.0.0
205 */
206 protected function column_used( $item ) {
207 $time = new \DateTime();
208 $time->setTimestamp( $item['used'] );
209 return ucfirst( Date::get_positive_time_diff_from_mysql_utc( $time->format( 'Y-m-d H:i:s' ) ) );
210 }
211
212 /**
213 * "timestamp" column formatter.
214 *
215 * @param array $item The current item.
216 * @return string The cell formatted, ready to print.
217 * @since 1.0.0
218 */
219 protected function column_timestamp( $item ) {
220 $time = new \DateTime();
221 $time->setTimestamp( $item['timestamp'] );
222 return Date::get_date_from_mysql_utc( $time->format( 'Y-m-d H:i:s' ), Timezone::network_get()->getName(), 'Y-m-d H:i:s' );
223 }
224
225 /**
226 * Enumerates columns.
227 *
228 * @return array The columns.
229 * @since 1.0.0
230 */
231 public function get_columns() {
232 $columns = [
233 'cb' => '<input type="checkbox" />',
234 'script' => esc_html__( 'File', 'opcache-manager' ),
235 'timestamp' => esc_html__( 'Timestamp', 'opcache-manager' ),
236 'hit' => esc_html__( 'Hits', 'opcache-manager' ),
237 'memory' => esc_html__( 'Memory size', 'opcache-manager' ),
238 'used' => esc_html__( 'Used', 'opcache-manager' ),
239 ];
240 return $columns;
241 }
242
243 /**
244 * Enumerates hidden columns.
245 *
246 * @return array The hidden columns.
247 * @since 1.0.0
248 */
249 protected function get_hidden_columns() {
250 return [];
251 }
252
253 /**
254 * Enumerates sortable columns.
255 *
256 * @return array The sortable columns.
257 * @since 1.0.0
258 */
259 protected function get_sortable_columns() {
260 $sortable_columns = [
261 'script' => [ 'script', true ],
262 'hit' => [ 'hit', false ],
263 'memory' => [ 'memory', false ],
264 'timestamp' => [ 'timestamp', false ],
265 'used' => [ 'used', false ],
266 ];
267 return $sortable_columns;
268 }
269
270 /**
271 * Enumerates bulk actions.
272 *
273 * @return array The bulk actions.
274 * @since 1.0.0
275 */
276 public function get_bulk_actions() {
277 return [
278 'invalidate' => esc_html__( 'Invalidate', 'opcache-manager' ),
279 'force' => esc_html__( 'Force invalidate', 'opcache-manager' ),
280 'recompile' => esc_html__( 'Recompile', 'opcache-manager' ),
281 ];
282 }
283
284 /**
285 * Generate the table navigation above or below the table
286 *
287 * @param string $which Position of extra control.
288 * @since 1.0.0
289 */
290 protected function display_tablenav( $which ) {
291 if ( 'top' === $which ) {
292 wp_nonce_field( 'bulk-opcm-tools', '_wpnonce', false );
293 }
294 echo '<div class="tablenav ' . esc_attr( $which ) . '">';
295 if ( $this->has_items() ) {
296 echo '<div class="alignleft actions bulkactions">';
297 $this->bulk_actions( $which );
298 echo '</div>';
299 }
300 $this->extra_tablenav( $which );
301 $this->pagination( $which );
302 echo '<br class="clear" />';
303 echo '</div>';
304 }
305
306 /**
307 * Extra controls to be displayed between bulk actions and pagination.
308 *
309 * @param string $which Position of extra control.
310 * @since 1.0.0
311 */
312 public function extra_tablenav( $which ) {
313 $list = $this;
314 $args = compact( 'list', 'which' );
315 foreach ( $args as $key => $val ) {
316 $$key = $val;
317 }
318 if ( 'top' === $which || 'bottom' === $which ) {
319 include OPCM_ADMIN_DIR . 'partials/opcache-manager-admin-tools-lines.php';
320 }
321 }
322
323 /**
324 * Prepares the list to be displayed.
325 *
326 * @since 1.0.0
327 */
328 public function prepare_items() {
329 $this->set_pagination_args(
330 [
331 'total_items' => count( $this->scripts ),
332 'per_page' => $this->limit,
333 'total_pages' => ceil( count( $this->scripts ) / $this->limit ),
334 ]
335 );
336 $current_page = $this->get_pagenum();
337 $columns = $this->get_columns();
338 $hidden = $this->get_hidden_columns();
339 $sortable = $this->get_sortable_columns();
340 $this->_column_headers = [ $columns, $hidden, $sortable ];
341 $data = $this->scripts;
342 usort(
343 $data,
344 function ( $a, $b ) {
345 if ( 'script' === $this->orderby ) {
346 $result = strcmp( strtolower( $a[ $this->orderby ] ), strtolower( $b[ $this->orderby ] ) );
347 } else {
348 $result = intval( $a[ $this->orderby ] ) < intval( $b[ $this->orderby ] ) ? 1 : -1;
349 }
350 return ( 'asc' === $this->order ) ? -$result : $result;
351 }
352 );
353 $this->items = array_slice( $data, ( ( $current_page - 1 ) * $this->limit ), $this->limit );
354 }
355
356 /**
357 * Get available lines breakdowns.
358 *
359 * @since 1.0.0
360 */
361 public function get_line_number_select() {
362 $_disp = [ 50, 100, 250, 500 ];
363 $result = [];
364 foreach ( $_disp as $d ) {
365 $l = [];
366 $l['value'] = $d;
367 // phpcs:ignore
368 $l['text'] = sprintf( esc_html__( 'Display %d files per page', 'opcache-manager' ), $d );
369 $l['selected'] = ( intval( $d ) === intval( $this->limit ) ? 'selected="selected" ' : '' );
370 $result[] = $l;
371 }
372 return $result;
373 }
374
375 /**
376 * Pagination links.
377 *
378 * @param string $which Position of extra control.
379 * @since 1.0.0
380 */
381 protected function pagination( $which ) {
382 if ( empty( $this->_pagination_args ) ) {
383 return;
384 }
385 $total_items = (int) $this->_pagination_args['total_items'];
386 $total_pages = (int) $this->_pagination_args['total_pages'];
387 $infinite_scroll = false;
388 if ( isset( $this->_pagination_args['infinite_scroll'] ) ) {
389 $infinite_scroll = $this->_pagination_args['infinite_scroll'];
390 }
391 if ( 'top' === $which && $total_pages > 1 ) {
392 $this->screen->render_screen_reader_content( 'heading_pagination' );
393 }
394 // phpcs:ignore
395 $output = '<span class="displaying-num">' . sprintf( _n( '%s item', '%s items', $total_items ), number_format_i18n( $total_items ) ) . '</span>';
396 $current = (int) $this->get_pagenum();
397 $removable_query_args = wp_removable_query_args();
398 $current_url = $this->url;
399 $current_url = remove_query_arg( $removable_query_args, $current_url );
400 $page_links = [];
401 $total_pages_before = '<span class="paging-input">';
402 $total_pages_after = '</span></span>';
403 $disable_first = false;
404 $disable_last = false;
405 $disable_prev = false;
406 $disable_next = false;
407 if ( 1 === $current ) {
408 $disable_first = true;
409 $disable_prev = true;
410 }
411 if ( 2 === $current ) {
412 $disable_first = true;
413 }
414 if ( $current === $total_pages ) {
415 $disable_last = true;
416 $disable_next = true;
417 }
418 if ( $current === $total_pages - 1 ) {
419 $disable_last = true;
420 }
421 if ( $disable_first ) {
422 $page_links[] = '<span class="tablenav-pages-navspan button disabled" aria-hidden="true">&laquo;</span>';
423 } else {
424 $page_links[] = sprintf(
425 "<a class='first-page button' href='%s'><span class='screen-reader-text'>%s</span><span aria-hidden='true'>%s</span></a>",
426 $this->get_url( remove_query_arg( 'paged', $current_url ), true ),
427 __( 'First page' ),
428 '&laquo;'
429 );
430 }
431 if ( $disable_prev ) {
432 $page_links[] = '<span class="tablenav-pages-navspan button disabled" aria-hidden="true">&lsaquo;</span>';
433 } else {
434 $page_links[] = sprintf(
435 "<a class='prev-page button' href='%s'><span class='screen-reader-text'>%s</span><span aria-hidden='true'>%s</span></a>",
436 $this->get_url( add_query_arg( 'paged', max( 1, $current - 1 ), $current_url ), true ),
437 __( 'Previous page' ),
438 '&lsaquo;'
439 );
440 }
441 if ( 'bottom' === $which ) {
442 $html_current_page = $current;
443 $total_pages_before = '<span class="screen-reader-text">' . __( 'Current Page' ) . '</span><span id="table-paging" class="paging-input"><span class="tablenav-paging-text">';
444 } else {
445 $html_current_page = sprintf(
446 "%s<input class='current-page' id='current-page-selector' type='text' name='paged' value='%s' size='%d' aria-describedby='table-paging' /><span class='tablenav-paging-text'>",
447 '<label for="current-page-selector" class="screen-reader-text">' . __( 'Current Page' ) . '</label>',
448 $current,
449 strlen( $total_pages )
450 );
451 }
452 $html_total_pages = sprintf( "<span class='total-pages'>%s</span>", number_format_i18n( $total_pages ) );
453 // phpcs:ignore
454 $page_links[] = $total_pages_before . sprintf( _x( '%1$s of %2$s', 'paging' ), $html_current_page, $html_total_pages ) . $total_pages_after;
455 if ( $disable_next ) {
456 $page_links[] = '<span class="tablenav-pages-navspan button disabled" aria-hidden="true">&rsaquo;</span>';
457 } else {
458 $page_links[] = sprintf(
459 "<a class='next-page button' href='%s'><span class='screen-reader-text'>%s</span><span aria-hidden='true'>%s</span></a>",
460 $this->get_url( add_query_arg( 'paged', min( $total_pages, $current + 1 ), $current_url ), true ),
461 __( 'Next page' ),
462 '&rsaquo;'
463 );
464 }
465 if ( $disable_last ) {
466 $page_links[] = '<span class="tablenav-pages-navspan button disabled" aria-hidden="true">&raquo;</span>';
467 } else {
468 $page_links[] = sprintf(
469 "<a class='last-page button' href='%s'><span class='screen-reader-text'>%s</span><span aria-hidden='true'>%s</span></a>",
470 $this->get_url( add_query_arg( 'paged', $total_pages, $current_url ), true ),
471 __( 'Last page' ),
472 '&raquo;'
473 );
474 }
475 $pagination_links_class = 'pagination-links';
476 if ( ! empty( $infinite_scroll ) ) {
477 $pagination_links_class .= ' hide-if-js';
478 }
479 $output .= "\n<span class='$pagination_links_class'>" . join( "\n", $page_links ) . '</span>';
480 if ( $total_pages ) {
481 $page_class = $total_pages < 2 ? ' one-page' : '';
482 } else {
483 $page_class = ' no-pages';
484 }
485 $this->_pagination = "<div class='tablenav-pages{$page_class}'>$output</div>";
486 // phpcs:ignore
487 echo $this->_pagination;
488 }
489
490 /**
491 * Print column headers, accounting for hidden and sortable columns.
492 *
493 * @staticvar int $cb_counter.
494 * @param bool $with_id Whether to set the id attribute or not.
495 * @since 1.0.0
496 */
497 public function print_column_headers( $with_id = true ) {
498 list( $columns, $hidden, $sortable, $primary ) = $this->get_column_info();
499 if ( ! empty( $columns['cb'] ) ) {
500 static $cb_counter = 1;
501 $columns['cb'] = '<label class="screen-reader-text" for="cb-select-all-' . $cb_counter . '">' . __( 'Select All' ) . '</label><input id="cb-select-all-' . $cb_counter . '" type="checkbox" />';
502 $cb_counter++;
503 }
504 foreach ( $columns as $column_key => $column_display_name ) {
505 $class = [ 'manage-column', "column-$column_key" ];
506 if ( in_array( $column_key, $hidden, true ) ) {
507 $class[] = 'hidden';
508 }
509 if ( 'cb' === $column_key ) {
510 $class[] = 'check-column';
511 } elseif ( in_array( $column_key, [ 'posts', 'comments', 'links' ], true ) ) {
512 $class[] = 'num';
513 }
514 if ( $column_key === $primary ) {
515 $class[] = 'column-primary';
516 }
517 if ( isset( $sortable[ $column_key ] ) ) {
518 list( $orderby, $desc_first ) = $sortable[ $column_key ];
519 if ( $this->orderby === $orderby ) {
520 $order = 'asc' === $this->order ? 'desc' : 'asc';
521 $class[] = 'sorted';
522 $class[] = $this->order;
523 } else {
524 $order = $desc_first ? 'desc' : 'asc';
525 $class[] = 'sortable';
526 $class[] = $desc_first ? 'asc' : 'desc';
527 }
528 $column_display_name = '<a href="' . $this->get_url( add_query_arg( compact( 'orderby', 'order' ), $this->url ), true ) . '"><span>' . $column_display_name . '</span><span class="sorting-indicator"></span></a>';
529 }
530 $tag = ( 'cb' === $column_key ) ? 'td' : 'th';
531 $scope = ( 'th' === $tag ) ? 'scope="col"' : '';
532 $id = $with_id ? "id='$column_key'" : '';
533 if ( ! empty( $class ) ) {
534 $class = "class='" . join( ' ', $class ) . "'";
535 }
536 // phpcs:ignore
537 echo "<$tag $scope $id $class>$column_display_name</$tag>";
538 }
539 }
540
541 /**
542 * Display a warning if needed.
543 *
544 * @since 1.0.0
545 */
546 public function warning() {
547 $message = '';
548 if ( function_exists( 'opcache_get_status' ) ) {
549 $raw = opcache_get_status( false );
550 if ( ! (bool) $raw['opcache_enabled'] ) {
551 $message = esc_html__( 'OPcache is not enabled on this site. There\'s nothing to see here.', 'opcache-manager' );
552 }
553 if ( (bool) $raw['restart_pending'] ) {
554 $message = esc_html__( 'A full reset is currently pending. Displayed values may be inaccurate.', 'opcache-manager' );
555 }
556 if ( (bool) $raw['restart_in_progress'] ) {
557 $message = esc_html__( 'A full reset is currently in progress. Displayed values may be inaccurate.', 'opcache-manager' );
558 }
559 } else {
560 $message = esc_html__( 'OPcache is not enabled on this site. There\'s nothing to see here.', 'opcache-manager' );
561 }
562 if ( '' !== $message ) {
563 // phpcs:ignore
564 echo '<div id="opcm-warning" class="notice notice-warning"><p><strong>' . $message . '</strong></p></div>';
565 }
566 }
567
568 /**
569 * Get the cleaned url.
570 *
571 * @param boolean $url Optional. The url, false for current url.
572 * @param boolean $limit Optional. Has the limit to be in the url.
573 * @return string The url cleaned, ready to use.
574 * @since 1.0.0
575 */
576 public function get_url( $url = false, $limit = false ) {
577 global $wp;
578 $url = remove_query_arg( 'limit', $url );
579 if ( $limit ) {
580 $url .= ( false === strpos( $url, '?' ) ? '?' : '&' ) . 'limit=' . $this->limit;
581 }
582 return esc_url( $url );
583 }
584
585 /**
586 * Initializes all the list properties.
587 *
588 * @since 1.0.0
589 */
590 public function process_args() {
591 if ( ! ( $this->nonce = filter_input( INPUT_POST, '_wpnonce' ) ) ) {
592 $this->nonce = filter_input( INPUT_GET, '_wpnonce' );
593 }
594 $this->url = set_url_scheme( 'http://' . filter_input( INPUT_SERVER, 'HTTP_HOST' ) . filter_input( INPUT_SERVER, 'REQUEST_URI' ) );
595 $this->limit = filter_input( INPUT_GET, 'limit', FILTER_SANITIZE_NUMBER_INT );
596 foreach ( [ 'top', 'bottom' ] as $which ) {
597 if ( wp_verify_nonce( $this->nonce, 'bulk-opcm-tools' ) && array_key_exists( 'dolimit-' . $which, $_POST ) ) {
598 $this->limit = filter_input( INPUT_POST, 'limit-' . $which, FILTER_SANITIZE_NUMBER_INT );
599 }
600 }
601 if ( 0 === intval( $this->limit ) ) {
602 $this->limit = filter_input( INPUT_POST, 'limit-top', FILTER_SANITIZE_NUMBER_INT );
603 }
604 if ( 0 === intval( $this->limit ) ) {
605 $this->limit = 50;
606 }
607 $this->paged = filter_input( INPUT_GET, 'paged', FILTER_SANITIZE_NUMBER_INT );
608 if ( ! $this->paged ) {
609 $this->paged = filter_input( INPUT_POST, 'paged', FILTER_SANITIZE_NUMBER_INT );
610 if ( ! $this->paged ) {
611 $this->paged = 1;
612 }
613 }
614 $this->order = filter_input( INPUT_GET, 'order', FILTER_SANITIZE_FULL_SPECIAL_CHARS );
615 if ( ! $this->order ) {
616 $this->order = 'desc';
617 }
618 $this->orderby = filter_input( INPUT_GET, 'orderby', FILTER_SANITIZE_FULL_SPECIAL_CHARS );
619 if ( ! $this->orderby ) {
620 $this->orderby = 'script';
621 }
622 foreach ( [ 'top', 'bottom' ] as $which ) {
623 if ( wp_verify_nonce( $this->nonce, 'bulk-opcm-tools' ) && array_key_exists( 'dowarmup-' . $which, $_POST ) ) {
624 $this->action = 'warmup';
625 }
626 if ( wp_verify_nonce( $this->nonce, 'bulk-opcm-tools' ) && array_key_exists( 'doinvalidate-' . $which, $_POST ) ) {
627 $this->action = 'reset';
628 }
629 }
630 if ( array_key_exists( 'quick-action', $_GET ) && wp_verify_nonce( $this->nonce, 'quick-action-opcm-tools' ) ) {
631 $this->action = filter_input( INPUT_GET, 'quick-action', FILTER_SANITIZE_FULL_SPECIAL_CHARS );
632 }
633 if ( '' === $this->action ) {
634 $action = '-1';
635 if ( '-1' === $action && wp_verify_nonce( $this->nonce, 'bulk-opcm-tools' ) && array_key_exists( 'action', $_POST ) ) {
636 $action = filter_input( INPUT_POST, 'action', FILTER_SANITIZE_FULL_SPECIAL_CHARS );
637 }
638 if ( '-1' === $action && wp_verify_nonce( $this->nonce, 'bulk-opcm-tools' ) && array_key_exists( 'action2', $_POST ) ) {
639 $action = filter_input( INPUT_POST, 'action2', FILTER_SANITIZE_FULL_SPECIAL_CHARS );
640 }
641 if ( '-1' !== $action && wp_verify_nonce( $this->nonce, 'bulk-opcm-tools' ) && array_key_exists( 'bulk', $_POST ) ) {
642 $this->bulk = filter_input( INPUT_POST, 'bulk', FILTER_SANITIZE_FULL_SPECIAL_CHARS, FILTER_FORCE_ARRAY );
643 if ( 0 < count( $this->bulk ) ) {
644 $this->action = $action;
645 }
646 }
647 }
648 }
649
650 /**
651 * Processes the selected action.
652 *
653 * @since 1.0.0
654 */
655 public function process_action() {
656 switch ( $this->action ) {
657 case 'warmup':
658 if ( Environment::is_wordpress_multisite() ) {
659 // phpcs:ignore
660 $message = esc_html( sprintf( __( 'Network warm-up has been initiated. %d relevant files.', 'opcache-manager' ), OPcache::warmup( false, true ) ) );
661 } else {
662 // phpcs:ignore
663 $message = esc_html( sprintf( __( 'Site warm-up has been initiated. %d relevant files.', 'opcache-manager' ), OPcache::warmup( false, true ) ) );
664 }
665 $code = 0;
666 break;
667 case 'reset':
668 OPcache::reset( false );
669 $message = esc_html__( 'Site invalidation has been initiated. It may take a few seconds to complete.', 'opcache-manager' );
670 $code = 0;
671 break;
672 case 'invalidate':
673 // phpcs:ignore
674 $message = esc_html( sprintf( __( 'Invalidation done: %d file(s).', 'opcache-manager' ), OPcache::invalidate( $this->bulk, false ) ) );
675 $code = 0;
676 break;
677 case 'force':
678 // phpcs:ignore
679 $message = esc_html( sprintf( __( 'Forced invalidation done: %d file(s).', 'opcache-manager' ), OPcache::invalidate( $this->bulk, true ) ) );
680 $code = 0;
681 break;
682 case 'recompile':
683 // phpcs:ignore
684 $message = esc_html( sprintf( __( 'Recompilation done: %d file(s).', 'opcache-manager' ), OPcache::recompile( $this->bulk, true ) ) );
685 $code = 0;
686 break;
687 default:
688 return;
689 }
690 if ( 0 === $code ) {
691 add_settings_error( 'opcache_manager_no_error', $code, $message, 'updated' );
692 } else {
693 add_settings_error( 'opcache_manager_error', $code, $message, 'error' );
694 }
695 }
696 }
697