Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PHP `rename()` support for VirtualBox via a Must-Use plugin coupled with a "watcher" bash script.
<?php
/**
* Plugin Name: PHP <code>rename()</code> support for VirtualBox.
* Description: Enables support for the PHP native rename() function.
* Author: WordPress Core Contributors
* Author URI: https://make.wordpress.org/core
* License: GPLv2 or later
* Version: 1.0.0
*/
add_action(
'pre_move_dir',
function() {
global $wp_filesystem;
if ( ! $wp_filesystem ) {
require_once ABSPATH . '/wp-admin/includes/file.php';
WP_Filesystem();
}
$wp_filesystem->touch( trailingslashit( $wp_filesystem->wp_content_dir() ) . '.wpclearcache' );
}
);
add_action(
'post_move_dir',
function() {
global $wp_filesystem;
# Wait 200ms to ensure that the `rename()` has completed.
usleep( 200000 );
if ( ! $wp_filesystem ) {
require_once ABSPATH . '/wp-admin/includes/file.php';
WP_Filesystem();
}
$wp_filesystem->delete( trailingslashit( $wp_filesystem->wp_content_dir() ) . '.wpclearcache' );
}
);
#!/usr/bin/sh
# Name: PHP rename() support for VirtualBox.
# Description: Enables support for the PHP native rename() function.
# Author: WordPress Core Contributors
# Author URI: https://make.wordpress.org/core
# License: GPLv2 or later
# Version: 1.0.0
#
# Usage:
# 1. Set WP_CONTENT_DIR to the path for the wp-content directory,
# without a trailing slash.
#
# 2. Ensure the script can be executed by using the following command:
# chmod +x wpclearcache.sh
#
# 3. This script should be run continuously.
# It is advised to use the following command:
# ./wpclearcache.sh &
WP_CONTENT_DIR=.
while :
do
if [ -f "$WP_CONTENT_DIR/.wpclearcache" ]; then
# Free reclaimable slab objects (includes dentries and inodes).
sudo sh -c 'echo 2 > /proc/sys/vm/drop_caches'
fi
# Wait 200ms.
sleep 0.2s
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment