Create a gist now

Instantly share code, notes, and snippets.

Embed
<?php
/**
* If we need to initialize it globally:
*
* $api_client = new_api_client();
* WP_Filesystem( $api_client, 'vip' );
*
* If needed, can also instantiate manually via:
*
* $api_client = Automattic\VIP\Files\new_api_client();
* $filesystem = new WP_Filesystem_VIP( $api_client );
*/
// Note: we're using `PHP_INT_MAX` for the priority because we want our `WP_Filesystem_VIP` class to always take precedence.
require( ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php' );
require_once( WPMU_PLUGIN_DIR . '/files/class-wp-filesystem-vip.php' );
require_once( WPMU_PLUGIN_DIR . '/files/class-api-client.php' );
// Stub class to match the format that `WP_Filesystem()` expects.
class WP_Filesystem_VIP extends Automattic\VIP\Files\WP_Filesystem_VIP {}
add_filter( 'filesystem_method', function( $method, $args, $context, $allow_relaxed_file_ownership ) {
return 'VIP'; // All VIP, all the time
}, PHP_INT_MAX, 4 );
add_filter( 'request_filesystem_credentials', function( $credentials, $form_post, $type, $error, $context, $extra_fields, $allow_relaxed_file_ownership ) {
if ( 'VIP' === $type ) {
$api_client = Automattic\VIP\Files\new_api_client();
$credentials = [
new Automattic\VIP\Files\WP_Filesystem_VIP_Uploads( $api_client ),
new WP_Filesystem_Direct( null ),
];
}
return $credentials;
}, PHP_INT_MAX, 7 );
// Should't need this because we `require`-ed the class already.
// But just in case :)
add_filter( 'filesystem_method_file', function( $file, $method ) {
if ( 'VIP' === $method ) {
$file = WPMU_PLUGIN_DIR . '/files/class-wp-filesystem-vip.php';
}
return $file;
}, PHP_INT_MAX, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment