Skip to content

Commit 388c01a

Browse files
committed
Merge branch 'release-2.3.0'
2 parents 734252c + 7faddf3 commit 388c01a

File tree

15 files changed

+219
-80
lines changed

15 files changed

+219
-80
lines changed

.github/workflows/php.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ jobs:
3131
runs-on: ubuntu-latest
3232

3333
steps:
34-
- uses: actions/checkout@v1
34+
- name: Checkout the project
35+
uses: actions/checkout@v3
3536

3637
- name: Set up PHP versions
3738
uses: shivammathur/setup-php@v2

CHANGELOG.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
# BC Cache Changelog
22

3+
## Version 2.3.0 (2022-09-06)
4+
5+
This release has been tested with WordPress 6.0.
6+
7+
### Added
8+
9+
* Add `msclkid` to ignored query string parameters [#79](https://github.com/chesio/bc-cache/issues/79).
10+
* Add `bc-cache list` WP-CLI command [#38](https://github.com/chesio/bc-cache/issues/38).
11+
312
## Version 2.2.2 (2022-02-04)
413

514
### Fixed
@@ -130,4 +139,4 @@ Fixes problem when cache has not been flushed when a post of public post type ha
130139

131140
## Older releases
132141

133-
Changelog for older releases can be found (here)[https://github.com/chesio/bc-cache/releases].
142+
Notes on changes in all releases can be found [here](https://github.com/chesio/bc-cache/releases).

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ BC Cache has no settings page - it is intended for webmasters who are familiar w
1515

1616
## Limitations
1717

18-
* BC Cache has not been tested with [WordPress block editor](https://wordpress.org/support/article/wordpress-editor/).
1918
* BC Cache has not been tested on WordPress multisite installation.
2019
* BC Cache has not been tested on Windows servers.
2120
* BC Cache can only serve requests without filename in path, ie. `/some/path` or `some/other/path/`, but not `/some/path/to/filename.html`.
21+
* BC Cache does not support [content negotation](https://developer.mozilla.org/en-US/docs/Web/HTTP/Content_negotiation#the_accept_header). If contents of URL are deemed cacheable, content is always returned as `text/html` regardless of `Accept` header send by client.
2222

2323
## Installation
2424

@@ -104,7 +104,7 @@ AddDefaultCharset utf-8
104104
105105
# Main rules: serve only GET requests with whitelisted query string fields coming from anonymous users.
106106
RewriteCond %{REQUEST_METHOD} GET
107-
RewriteCond %{QUERY_STRING} ^(?:(?:_gl|gclid|gclsrc|fbclid|utm_(?:source|medium|campaign|term|content))=[\w\-]*(?:&|$))*$
107+
RewriteCond %{QUERY_STRING} ^(?:(?:_gl|gclid|gclsrc|fbclid|msclkid|utm_(?:source|medium|campaign|term|content))=[\w\-]*(?:&|$))*$
108108
RewriteCond %{HTTP_COOKIE} !(wp-postpass|wordpress_logged_in|comment_author)_
109109
RewriteCond %{ENV:BC_CACHE_ROOT}/wp-content/cache/bc-cache/%{ENV:BC_CACHE_SCHEME}_%{ENV:BC_CACHE_HOST}%{ENV:BC_CACHE_PATH}%{ENV:BC_CACHE_FILE} -f
110110
RewriteRule .* %{ENV:BC_CACHE_ROOT}/wp-content/cache/bc-cache/%{ENV:BC_CACHE_SCHEME}_%{ENV:BC_CACHE_HOST}%{ENV:BC_CACHE_PATH}%{ENV:BC_CACHE_FILE} [L,NS]
@@ -370,13 +370,14 @@ If you see 403 errors instead of cached pages, you have to either remove the `|g
370370

371371
## WP-CLI integration
372372

373-
You might use [WP-CLI](https://wp-cli.org/) to delete specific posts/pages form cache, flush entire cache, run cache warm up or get size information. BC Cache registers `bc-cache` command with following subcommands:
373+
You might use [WP-CLI](https://wp-cli.org/) to delete specific posts/pages form cache, flush entire cache, run cache warm up, get size information or even list all cache entries. BC Cache registers `bc-cache` command with following subcommands:
374374

375375
* `delete <post-id>` - deletes cache data (all request variants) of post/page with given ID
376376
* `remove <url>` - deletes cache data (all request variants) of given URL
377377
* `flush` - flushes entire cache
378-
* `size [--human-readable]` - retrieves cache directory apparent size, optionally in human readable format
379378
* `warm-up` - runs cache warm up
379+
* `size [--human-readable]` - retrieves cache directory apparent size, optionally in human readable format
380+
* `list [<column>...] [--format=<format>] [--plain] [--sort-by=<column>]` - list cache entries, optionally in specified format or sorted
380381

381382
## Credits
382383

bc-cache.php

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
* Plugin Name: BC Cache
55
* Plugin URI: https://github.com/chesio/bc-cache
66
* Description: Simple full page cache plugin inspired by Cachify.
7-
* Version: 2.2.2
7+
* Version: 2.3.0
88
* Author: Česlav Przywara <ceslav@przywara.cz>
99
* Author URI: https://www.chesio.com
1010
* Requires PHP: 7.3
1111
* Requires WP: 5.5
12-
* Tested up to: 5.9
12+
* Tested up to: 6.0
1313
* Text Domain: bc-cache
1414
* GitHub Plugin URI: https://github.com/chesio/bc-cache
1515
* Update URI: https://github.com/chesio/bc-cache
@@ -82,18 +82,22 @@ function () {
8282
// Register autoloader for this plugin.
8383
require_once __DIR__ . '/autoload.php';
8484

85-
// Construct plugin instance.
86-
$bc_cache = new \BlueChip\Cache\Plugin(
87-
__FILE__,
88-
defined('BC_CACHE_FILE_LOCKING_ENABLED') ? BC_CACHE_FILE_LOCKING_ENABLED : true,
89-
defined('BC_CACHE_WARM_UP_ENABLED') ? BC_CACHE_WARM_UP_ENABLED : true
90-
);
85+
return call_user_func(function () {
86+
// Construct plugin instance.
87+
$bc_cache = new \BlueChip\Cache\Plugin(
88+
__FILE__,
89+
defined('BC_CACHE_FILE_LOCKING_ENABLED') ? BC_CACHE_FILE_LOCKING_ENABLED : true,
90+
defined('BC_CACHE_WARM_UP_ENABLED') ? BC_CACHE_WARM_UP_ENABLED : true
91+
);
9192

92-
// Register activation hook.
93-
register_activation_hook(__FILE__, [$bc_cache, 'activate']);
94-
// Register deactivation hook.
95-
register_deactivation_hook(__FILE__, [$bc_cache, 'deactivate']);
96-
// Ideally, uninstall hook would be registered here, but WordPress allows only static method in uninstall hook...
93+
// Register activation hook.
94+
register_activation_hook(__FILE__, [$bc_cache, 'activate']);
95+
// Register deactivation hook.
96+
register_deactivation_hook(__FILE__, [$bc_cache, 'deactivate']);
9797

98-
// Load the plugin.
99-
$bc_cache->load();
98+
// Boot up the plugin after all plugins are loaded.
99+
add_action('plugins_loaded', [$bc_cache, 'load'], 10, 0);
100+
101+
// Return the instance.
102+
return $bc_cache;
103+
});

classes/BlueChip/Cache/AdminNotices.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,17 @@ abstract class AdminNotices
1717
*
1818
* @link https://make.wordpress.org/core/2015/04/23/spinners-and-dismissible-admin-notices-in-4-2/
1919
*
20-
* @param string[]|string $message Single message or array of messages.
20+
* @param string $message Message to display in admin notice.
2121
* @param string $type [optional] Type: 'notice-error', 'notice-warning', 'notice-success' or 'notice-info] (default).
2222
* @param bool $is_dismissible [optional] Should the notice be dismissible? Default is true.
2323
* @param bool $escape_html [optional] Should the content of message be HTML escaped? Default is true.
2424
*/
25-
public static function add($message, string $type = self::INFO, bool $is_dismissible = true, bool $escape_html = true): void
25+
public static function add(string $message, string $type = self::INFO, bool $is_dismissible = true, bool $escape_html = true): void
2626
{
2727
$classes = \implode(' ', \array_filter(['notice', $type, $is_dismissible ? 'is-dismissible' : '']));
2828
add_action('admin_notices', function () use ($message, $classes, $escape_html) {
2929
echo '<div class="' . $classes . '">';
30-
$messages = \is_array($message) ? $message : [$message];
31-
\array_walk($messages, function ($msg) use ($escape_html) {
32-
echo '<p>' . ($escape_html ? esc_html($msg) : $msg) . '</p>';
33-
});
30+
echo '<p>' . ($escape_html ? esc_html($message) : $message) . '</p>';
3431
echo '</div>';
3532
});
3633
}

classes/BlueChip/Cache/Cli.php

Lines changed: 128 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,30 @@
1010
class Cli
1111
{
1212
/**
13-
* @var \BlueChip\Cache\Core
13+
* @var string
14+
*/
15+
private const UNKNOWN_VALUE = '???';
16+
17+
/**
18+
* @var Core
1419
*/
1520
private $cache;
1621

1722
/**
18-
* @var \BlueChip\Cache\Crawler|null
23+
* @var Crawler|null
1924
*/
2025
private $cache_crawler;
2126

2227
/**
23-
* @var \BlueChip\Cache\Feeder|null
28+
* @var Feeder|null
2429
*/
2530
private $cache_feeder;
2631

2732

2833
/**
29-
* @param \BlueChip\Cache\Core $cache
30-
* @param \BlueChip\Cache\Crawler|null $cache_crawler Null value signals that cache warm up is disabled.
31-
* @param \BlueChip\Cache\Feeder|null $cache_feeder Null value signals that cache warm up is disabled.
34+
* @param Core $cache
35+
* @param Crawler|null $cache_crawler Null value signals that cache warm up is disabled.
36+
* @param Feeder|null $cache_feeder Null value signals that cache warm up is disabled.
3237
*/
3338
public function __construct(Core $cache, ?Crawler $cache_crawler, ?Feeder $cache_feeder)
3439
{
@@ -171,6 +176,123 @@ private function erase(string $url, ?int $post_id = null): void
171176
}
172177

173178

179+
/**
180+
* List cache entries.
181+
*
182+
* By default following columns are printed:
183+
* - URL (url)
184+
* - Request variant (request_variant)
185+
* - Created (created)
186+
* - Size (size)
187+
*
188+
* Note: Request variant column is printed only if there are multiple request variants configured or column is explicitly requested (see options below).
189+
*
190+
* ## OPTIONS
191+
*
192+
* [<column>...]
193+
* : Explicitly set columns (incl. their order) to print: proper column keys has to be given (see the list above).
194+
*
195+
* [--format=<format>]
196+
* : Output format to use. Can be 'table', 'json', 'csv', 'yaml' or 'count'. Default is 'table'.
197+
*
198+
* [--plain]
199+
* : Print URL including scheme and host, request variant as a key only, creation time as Unix timestamp and size as number of bytes without unit.
200+
*
201+
* [--sort-by=<column>]
202+
* : Sort by given column in ascending order. It is possible to sort by column that is not being printed out.
203+
*
204+
* ## EXAMPLES
205+
*
206+
* wp bc-cache list url request-variant size
207+
*/
208+
public function list(array $args, array $assoc_args): void
209+
{
210+
$available_columns = [
211+
'url',
212+
'request_variant',
213+
'created',
214+
'size',
215+
];
216+
217+
// Explicitly set columns to display?
218+
if ($args !== []) {
219+
// Set and validate columns to display.
220+
$columns_to_display = [];
221+
foreach ($args as $arg) {
222+
if (\array_search($arg, $available_columns, true) === false) {
223+
\WP_CLI::error(sprintf('Unknown column key given: "%s". Exiting ...', $arg));
224+
}
225+
226+
$columns_to_display[] = $arg;
227+
}
228+
}
229+
230+
$format = $assoc_args['format'] ?? 'table';
231+
$plain = $assoc_args['plain'] ?? false;
232+
$sort_by = $assoc_args['sort-by'] ?? '';
233+
234+
// Validate sort by value.
235+
if ($sort_by) {
236+
if (\array_search($sort_by, $available_columns, true) === false) {
237+
\WP_CLI::error(sprintf('Unknown column key given for --sort-by argument: "%s". Exiting ...', $sort_by));
238+
}
239+
}
240+
241+
$request_variants = $this->cache->getRequestVariants();
242+
243+
if (!isset($columns_to_display)) {
244+
// Columns to display have not been set explicitly, so use all available columns...
245+
$columns_to_display = $available_columns;
246+
// ...but unset request variant if there is only single (default) variant configured.
247+
if (\count($request_variants) === 1) {
248+
$columns_to_display = \array_diff($columns_to_display, ['request_variant']);
249+
}
250+
}
251+
252+
$cache_items = $this->cache->inspect();
253+
254+
if ($cache_items === null) {
255+
\WP_CLI::error('Cache items could not be fetched due to I/O error. Exiting ...');
256+
}
257+
258+
// Prepare items.
259+
$items = array_map(
260+
function (ListTableItem $item) use ($plain, $request_variants): array {
261+
$request_variant = $item->getRequestVariant();
262+
$timestamp = $item->getTimestamp();
263+
$total_size = $item->getTotalSize();
264+
$url = $item->getUrl();
265+
266+
return [
267+
'url' => $plain ? $url : \parse_url($url, PHP_URL_PATH),
268+
'request_variant' => $plain ? $request_variant : $request_variants[$request_variant],
269+
'created' => $timestamp ? ($plain ? $timestamp : wp_date('Y-m-d H:i:s', $timestamp)) : self::UNKNOWN_VALUE,
270+
'size' => $plain ? $total_size : size_format($total_size),
271+
];
272+
},
273+
$cache_items
274+
);
275+
276+
// Sort items?
277+
if ($sort_by) {
278+
usort(
279+
$items,
280+
function (array $a, array $b) use ($sort_by): int {
281+
if ($a[$sort_by] < $b[$sort_by]) {
282+
return -1;
283+
}
284+
if ($a[$sort_by] > $b[$sort_by]) {
285+
return 1;
286+
}
287+
return 0;
288+
}
289+
);
290+
}
291+
292+
\WP_CLI\Utils\format_items($format, $items, $columns_to_display);
293+
}
294+
295+
174296
/**
175297
* Warm up cache.
176298
*

0 commit comments

Comments
 (0)