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 $args Positional arguments (unused). * @param array $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 $args Positional arguments (unused). * @param array $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=] * : Number of days to show (default: 7, max: 30). * * ## EXAMPLES * * wp idrivee2 stats * wp idrivee2 stats --days=14 * * @since 1.4.0 * * @param array $args Positional arguments (unused). * @param array $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 ) ) ); } } }