Repository navigation
Add hook to expose changed files for OPCache invalidation - #501
Conversation
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
…logic Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
Failed updates made `update_many()` exit before the hook was fired, even though files of the other items had changed. The hook now fires right after the bulk upgrade, and only when the upgrader tracks changed files, so that subclasses using another upgrader class keep working. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UWTd5pwpi2xQfDA6mUYkkx
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
|
@coderabbitai review |
Action performed
|
|
Sample cronjob I intend to use to take advantage of this hook: #!/bin/bash
PLUGINS_NO_AUTO_UPDATE=foo,bar,advanced-custom-fields-pro
WP=/usr/local/bin/wp
path="$1" && shift
# Remaining arguments, if any, are forwarded to cachetool (eg. --fcgi=/run/php/<pool>.sock)
[[ ! -d $path ]] && exit 1
update=(--path="$path" plugin update --all --patch --quiet --no-color --exclude="$PLUGINS_NO_AUTO_UPDATE")
output=$($WP "${update[@]}" --dry-run)
[[ -z $output ]] && exit 0
# wp-cli/extension-command >= 3.1.0 reports the files it rewrote through the
# `plugin_update_finished` hook (https://github.com/wp-cli/extension-command/pull/501,
# https://github.com/wp-cli/extension-command/issues/444). Older versions never fire it:
# $updated then stays empty and we fall back to a full reset, as before.
updated=$(mktemp) hook=$(mktemp --suffix=.php)
trap 'rm -f "$updated" "$hook"' EXIT
cat > "$hook" <<'PHP'
<?php
// Report the directory of each updated plugin, one per line.
WP_CLI::add_hook('plugin_update_finished', function ($files) {
$dirs = [];
foreach ((array) $files as $file) {
if (0 === strpos($file, WP_PLUGIN_DIR . '/')) {
$dirs[] = WP_PLUGIN_DIR . '/' . strtok(substr($file, strlen(WP_PLUGIN_DIR) + 1), '/');
}
}
if ($dirs) {
file_put_contents(getenv('WP_UPDATED_PLUGINS_DIRS'), implode("\n", array_unique($dirs)) . "\n");
}
});
PHP
WP_UPDATED_PLUGINS_DIRS="$updated" $WP "${update[@]}" --require="$hook"
if [[ -s $updated ]] && type -P cachetool &> /dev/null; then
# Selective invalidation, at the PHP-FPM level. `opcache:invalidate:scripts` expects a
# single regexp matched against the cached scripts (so: one call per updated plugin, and
# files dropped by the update are invalidated too, unlike with `wp opcache invalidate`).
while read -r dir; do
sudo cachetool "$@" opcache:invalidate:scripts "^$dir/"
done < "$updated"
elif $WP --path="$path" cli has-command opcache; then
$WP --path="$path" opcache clear;
elif type -P cachetool &> /dev/null; then
sudo cachetool "$@" opcache:reset
fi
find "$path/../app/plugins" -maxdepth 1 -type d -empty
if type -P yq &>/dev/null; then
p=$(realpath "$path/../../")
core_version=$(yq -r '.require["roots/wordpress-no-content"]|sub("[^0-9]"; "")' "$p/composer.json")
composer --working-dir="$p" update "roots/wordpress-no-content:^${core_version}"
fi
true; |
WP-CLI runs in CLI SAPI context, so WordPress core's
wp_opcache_invalidate_directory()calls during plugin/theme updates don't affect PHP-FPM opcache. Users need the list of changed files to invalidate opcache using external tools like cachetool.The custom plugin and theme upgraders are updated to hook into
wp_opcache_invalidate_fileto get that list. Then, it is exposed via a custom WP-CLI hook (WP_CLI::do_hook()) so that other packages such as https://github.com/wearerequired/wp-cli-clear-opcache could be notified of updates and invalidate all the files in PHP-FPM.Usage:
Notes:
wp_opcache_invalidate_filefilter whenopcache_invalidate()is available in the CLI (the OPcache extension is loaded, andopcache.restrict_apiallows it). Otherwise the list is empty.wp plugin updateandwp theme update. Updates with--version, which go throughinstall --force, don't fire it.Summary by CodeRabbit