Skip to content

Commit

Permalink
CLI: Add a wp surge flush command
Browse files Browse the repository at this point in the history
Fixes #14
  • Loading branch information
kovshenin committed Apr 19, 2022
1 parent 4549a92 commit a2a5f68
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
52 changes: 52 additions & 0 deletions include/cli.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/**
* All things CLI
*
* @package Surge
*/

namespace Surge;

use WP_CLI;

include_once( __DIR__ . '/common.php' );

class CLI_Commands {

/**
* Flush all cached data.
*
* ## OPTIONS
*
* [--delete]
* : By default flushing cache will invalidate all existing entries. Using the --delete flag will also delete these entries from disk, which is slower.
* ---
* default: false
*/
public function flush( $args, $assoc_args ) {
$assoc_args = wp_parse_args( $assoc_args, [
'delete' => false,
] );

if ( ! $assoc_args['delete'] ) {
expire( '/' );
WP_CLI::success( 'Set all existing page cache entries as expired.' );
return;
}

require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php';
require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php';

$fs = new \WP_Filesystem_Direct( false );
$r = $fs->rmdir( CACHE_DIR, true );
if ( ! $r ) {
WP_CLI::error( sprintf( 'Could not recursively delete %s. Please check permissions.', CACHE_DIR ) );
}

WP_CLI::success( 'All page cache deleted successfully.' );
}
}

WP_CLI::add_command( 'surge', __NAMESPACE__ . '\\CLI_Commands', [
'shortdesc' => 'Control Surge page caching.',
] );
4 changes: 4 additions & 0 deletions surge.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
include_once( __DIR__ . '/include/cron.php' );
}

if ( defined( 'WP_CLI' ) && WP_CLI ) {
include_once( __DIR__ . '/include/cli.php' );
}

include_once( __DIR__ . '/include/invalidate.php' );
} );

Expand Down

0 comments on commit a2a5f68

Please sign in to comment.