v1.4.0
This commit is contained in:
parent
916d7845be
commit
cc1bca74fa
29 changed files with 770 additions and 204 deletions
182
includes/class-cli.php
Normal file
182
includes/class-cli.php
Normal file
|
|
@ -0,0 +1,182 @@
|
|||
<?php
|
||||
/**
|
||||
* WP-CLI commands for iDrivee2 Media Upload.
|
||||
*
|
||||
* @package iDrivee2Media
|
||||
* @since 1.4.0
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
namespace iDrivee2Media;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides WP-CLI commands for S3 operations and diagnostics.
|
||||
*
|
||||
* Commands:
|
||||
* wp idrivee2 test-connection — Test S3 bucket connectivity.
|
||||
* wp idrivee2 cleanup-local-files — Manually run the cron file cleanup.
|
||||
* wp idrivee2 stats — Show S3 operation statistics.
|
||||
*
|
||||
* @since 1.4.0
|
||||
*/
|
||||
class CLI {
|
||||
|
||||
/**
|
||||
* Configuration instance.
|
||||
*
|
||||
* @var Config
|
||||
*/
|
||||
private Config $config;
|
||||
|
||||
/**
|
||||
* S3 client factory.
|
||||
*
|
||||
* @var S3_Client_Factory
|
||||
*/
|
||||
private S3_Client_Factory $client_factory;
|
||||
|
||||
/**
|
||||
* Logger instance.
|
||||
*
|
||||
* @var Logger
|
||||
*/
|
||||
private Logger $logger;
|
||||
|
||||
/**
|
||||
* Media uploader instance.
|
||||
*
|
||||
* @var Media_Uploader
|
||||
*/
|
||||
private Media_Uploader $media_uploader;
|
||||
|
||||
/**
|
||||
* Constructor — instantiates dependencies.
|
||||
*
|
||||
* @since 1.4.0
|
||||
*/
|
||||
public function __construct() {
|
||||
$this->logger = new Logger();
|
||||
$this->config = new Config( $this->logger );
|
||||
$this->client_factory = new S3_Client_Factory( $this->config );
|
||||
$this->media_uploader = new Media_Uploader( $this->config, $this->client_factory, $this->logger );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the S3 connection by calling headBucket.
|
||||
*
|
||||
* ## EXAMPLES
|
||||
*
|
||||
* wp idrivee2 test-connection
|
||||
*
|
||||
* @since 1.4.0
|
||||
*
|
||||
* @param array<int, string> $args Positional arguments (unused).
|
||||
* @param array<string, string> $assoc_args Associative arguments (unused).
|
||||
* @return void
|
||||
*/
|
||||
public function test_connection( array $args, array $assoc_args ): void { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundBeforeLastUsed
|
||||
if ( ! $this->config->is_configured() ) {
|
||||
\WP_CLI::error( __( 'Plugin is not configured. Set S3 credentials in wp-config.php or Settings → iDrivee2.', 'idrivee2-media-upload' ) );
|
||||
}
|
||||
|
||||
\WP_CLI::log( __( 'Testing connection to bucket:', 'idrivee2-media-upload' ) . ' ' . $this->config->get_bucket() );
|
||||
|
||||
try {
|
||||
$client = $this->client_factory->create();
|
||||
$client->headBucket( array( 'Bucket' => $this->config->get_bucket() ) );
|
||||
\WP_CLI::success( __( 'Connection successful — bucket is accessible.', 'idrivee2-media-upload' ) );
|
||||
} catch ( \Aws\Exception\AwsException $e ) {
|
||||
\WP_CLI::error( __( 'AWS error:', 'idrivee2-media-upload' ) . ' ' . $e->getAwsErrorMessage() );
|
||||
} catch ( \Exception $e ) {
|
||||
\WP_CLI::error( __( 'Connection failed:', 'idrivee2-media-upload' ) . ' ' . $e->getMessage() );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Manually trigger the local file cleanup cron job.
|
||||
*
|
||||
* ## EXAMPLES
|
||||
*
|
||||
* wp idrivee2 cleanup-local-files
|
||||
*
|
||||
* @since 1.4.0
|
||||
*
|
||||
* @param array<int, string> $args Positional arguments (unused).
|
||||
* @param array<string, string> $assoc_args Associative arguments (unused).
|
||||
* @return void
|
||||
*/
|
||||
public function cleanup_local_files( array $args, array $assoc_args ): void { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundBeforeLastUsed
|
||||
$raw_queue = get_option( 'idrivee2_deletion_queue', array() );
|
||||
$before = is_array( $raw_queue ) ? count( $raw_queue ) : 0;
|
||||
|
||||
$this->media_uploader->cleanup_local_files();
|
||||
|
||||
$raw_queue = get_option( 'idrivee2_deletion_queue', array() );
|
||||
$after = is_array( $raw_queue ) ? count( $raw_queue ) : 0;
|
||||
$deleted = $before - $after;
|
||||
|
||||
if ( $deleted > 0 ) {
|
||||
\WP_CLI::success(
|
||||
sprintf(
|
||||
/* translators: 1: number of files deleted, 2: number remaining in queue. */
|
||||
_n( 'Cleanup complete: %1$d file deleted, %2$d remaining.', 'Cleanup complete: %1$d files deleted, %2$d remaining.', $deleted, 'idrivee2-media-upload' ),
|
||||
$deleted,
|
||||
$after
|
||||
)
|
||||
);
|
||||
} else {
|
||||
\WP_CLI::success(
|
||||
sprintf(
|
||||
/* translators: %d: number of files remaining in queue. */
|
||||
__( 'Cleanup complete: no files to delete, %d remaining in queue.', 'idrivee2-media-upload' ),
|
||||
$after
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Show S3 operation statistics.
|
||||
*
|
||||
* ## OPTIONS
|
||||
*
|
||||
* [--days=<days>]
|
||||
* : Number of days to show (default: 7, max: 30).
|
||||
*
|
||||
* ## EXAMPLES
|
||||
*
|
||||
* wp idrivee2 stats
|
||||
* wp idrivee2 stats --days=14
|
||||
*
|
||||
* @since 1.4.0
|
||||
*
|
||||
* @param array<int, string> $args Positional arguments (unused).
|
||||
* @param array<string, string> $assoc_args Associative arguments.
|
||||
* @return void
|
||||
*/
|
||||
public function stats( array $args, array $assoc_args ): void {
|
||||
$days = isset( $assoc_args['days'] ) ? max( 1, min( 30, (int) $assoc_args['days'] ) ) : 7;
|
||||
|
||||
$stats = $this->logger->get_s3_stats( $days );
|
||||
|
||||
if ( empty( $stats ) ) {
|
||||
\WP_CLI::success( __( 'No S3 operations recorded.', 'idrivee2-media-upload' ) );
|
||||
return;
|
||||
}
|
||||
|
||||
/* translators: %d: number of days. */
|
||||
\WP_CLI::log( sprintf( __( 'S3 operations (last %d days):', 'idrivee2-media-upload' ), $days ) );
|
||||
|
||||
foreach ( $stats as $date => $operations ) {
|
||||
$parts = array();
|
||||
foreach ( $operations as $op => $count ) {
|
||||
$parts[] = sprintf( '%s=%d', $op, $count );
|
||||
}
|
||||
\WP_CLI::log( sprintf( ' %s: %s', $date, implode( ', ', $parts ) ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue