forked from Automattic/batcache
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbatcache-cli.php
More file actions
70 lines (62 loc) · 1.79 KB
/
batcache-cli.php
File metadata and controls
70 lines (62 loc) · 1.79 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
<?php
/**
* WP-CLI integration for Batcache Manager.
*
* @package Batcache
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Manage Batcache.
*/
class Batcache_CLI_Command extends WP_CLI_Command {
/**
* Flush the object cache, or a single object cache group.
*
* By default, runs `wp_cache_flush()` (entire object cache).
*
* When `[--group=<name>]` is set, only that group is flushed via `wp_cache_flush_group()`
* (Redis or another backend that advertises `flush_group` support).
*
* ## OPTIONS
*
* [--group=<name>]
* : Object cache group to flush only. Omit to flush the whole object cache.
*
* ## EXAMPLES
*
* # Flush the entire object cache.
* wp batcache flush
*
* # Flush one object cache group by name (Redis).
* wp batcache flush --group=batcache --url=https://example.com/
*
* @param array $args Positional arguments (unused).
* @param array $assoc_args {
* Optional. Associative arguments.
*
* @type string $group Object cache group name passed as `--group=<name>`.
* }
* @return void
*/
public function flush( $args = [], $assoc_args = [] ): void {
$group = '';
if ( isset( $assoc_args['group'] ) && is_string( $assoc_args['group'] ) ) {
$group = trim( $assoc_args['group'] );
}
if ( '' !== $group ) {
if ( ! function_exists( 'wp_cache_flush_group' ) || ! wp_cache_supports( 'flush_group' ) ) {
WP_CLI::error( 'Object cache does not support flushing a single group (flush_group).' );
}
$ok = wp_cache_flush_group( $group );
if ( $ok ) {
WP_CLI::success( sprintf( 'Object cache group "%s" flushed.', $group ) );
return;
}
WP_CLI::error( sprintf( 'Failed to flush object cache group "%s".', $group ) );
}
wp_cache_flush();
WP_CLI::success( 'Object cache flushed.' );
}
}