This commit is contained in:
Javier Casares 2026-08-10 16:21:45 +00:00
commit cc1bca74fa
5 changed files with 770 additions and 204 deletions

View file

@ -180,8 +180,8 @@ class Admin_Page {
wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'idrivee2-media-upload' ) );
}
$raw_test_file = is_string( $_POST['test_file'] ) ? $_POST['test_file'] : '';
$this->handle_delete_test( sanitize_file_name( wp_unslash( $raw_test_file ) ) );
$raw_test_file = is_string( $_POST['test_file'] ) ? wp_unslash( $_POST['test_file'] ) : '';
$this->handle_delete_test( sanitize_file_name( $raw_test_file ) );
return;
}
}
@ -327,6 +327,7 @@ class Admin_Page {
'Bucket' => $this->config->get_bucket(),
'Key' => $file_name,
'Body' => $content,
// ACL matches the media uploader: files are served publicly via CDN/S3.
'ACL' => 'public-read',
)
);
@ -531,6 +532,9 @@ class Admin_Page {
$allowed_modes = array( 'all', 'thumbnail', 'none' );
$sanitized['subsizes_mode'] = in_array( $raw_mode, $allowed_modes, true ) ? $raw_mode : 'all';
// Data cleanup on uninstall: checkbox, default off (preserve data).
$sanitized['delete_on_uninstall'] = ! empty( $input['delete_on_uninstall'] ) ? '1' : '';
return $sanitized;
}
@ -577,6 +581,7 @@ class Admin_Page {
$region = $this->config->get_region();
$domain = $this->config->get_domain();
$subsizes_mode = $this->config->get_subsizes_mode();
$delete_on_uninstall = $this->config->is_delete_on_uninstall();
$is_configured = $this->config->is_configured();
@ -811,23 +816,34 @@ class Admin_Page {
</p>
</td>
</tr>
<tr>
<th scope="row"><?php esc_html_e( 'Data Cleanup', 'idrivee2-media-upload' ); ?></th>
<td>
<label for="idrivee2_delete_on_uninstall">
<input
type="checkbox"
id="idrivee2_delete_on_uninstall"
name="idrivee2_media_settings[delete_on_uninstall]"
value="1"
<?php checked( $delete_on_uninstall ); ?>
/>
<?php esc_html_e( 'Delete all plugin data when the plugin is uninstalled', 'idrivee2-media-upload' ); ?>
</label>
<p class="description">
<?php esc_html_e( 'By default, all data is preserved when the plugin is uninstalled. Check this to remove settings, statistics, and post meta on uninstall.', 'idrivee2-media-upload' ); ?>
</p>
</td>
</tr>
</tbody>
</table>
<?php if ( ! $any_in_config ) : ?>
<?php submit_button( __( 'Save Settings', 'idrivee2-media-upload' ) ); ?>
<?php else : ?>
<?php if ( $any_in_config ) : ?>
<p class="description">
<?php esc_html_e( 'To modify settings defined in wp-config.php, please edit your wp-config.php file directly.', 'idrivee2-media-upload' ); ?>
</p>
<?php
// Show submit button only if at least one field can be edited.
$can_edit = ! $host_in_config || ! $key_in_config || ! $secret_in_config || ! $bucket_in_config || ! $region_in_config || ! $domain_in_config || ! $subsizes_mode_in_config;
if ( $can_edit ) :
?>
<?php submit_button( __( 'Save Settings', 'idrivee2-media-upload' ) ); ?>
<?php endif; ?>
<?php endif; ?>
<?php submit_button( __( 'Save Settings', 'idrivee2-media-upload' ) ); ?>
</form>
<?php if ( $is_configured ) : ?>

182
includes/class-cli.php Normal file
View 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 ) ) );
}
}
}

View file

@ -240,6 +240,19 @@ class Config {
return in_array( $value, $allowed, true ) ? $value : 'all';
}
/**
* Check if the user opted in to delete all data on uninstall.
*
* @since 1.4.0
*
* @return bool True if all plugin data should be removed on uninstall.
*/
public function is_delete_on_uninstall(): bool {
$options = get_option( self::OPTION_NAME, array() );
return is_array( $options ) && isset( $options['delete_on_uninstall'] ) && '1' === $options['delete_on_uninstall'];
}
/**
* Update configuration options in WordPress database.
*
@ -268,6 +281,9 @@ class Config {
$allowed_modes = array( 'all', 'thumbnail', 'none' );
$options['subsizes_mode'] = in_array( $raw_mode, $allowed_modes, true ) ? $raw_mode : 'all';
// Data cleanup on uninstall: checkbox, default off (preserve data).
$options['delete_on_uninstall'] = ! empty( $data['delete_on_uninstall'] ) ? '1' : '';
// Log configuration changes.
if ( $this->logger ) {
foreach ( $options as $key => $new_value ) {

View file

@ -300,18 +300,21 @@ class Logger {
/**
* Get client IP address.
*
* Validates the address with filter_var() so malformed or spoofed
* REMOTE_ADDR values are rejected rather than sanitised into garbage.
*
* @since 0.3.1
*
* @return string Client IP address.
* @return string Client IP address, or empty string if invalid.
*/
private function get_client_ip(): string {
$ip = '';
if ( isset( $_SERVER['REMOTE_ADDR'] ) && is_string( $_SERVER['REMOTE_ADDR'] ) ) {
$ip = sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) );
if ( ! isset( $_SERVER['REMOTE_ADDR'] ) || ! is_string( $_SERVER['REMOTE_ADDR'] ) ) {
return '';
}
return $ip;
$valid_ip = filter_var( wp_unslash( $_SERVER['REMOTE_ADDR'] ), FILTER_VALIDATE_IP );
return is_string( $valid_ip ) ? $valid_ip : '';
}
/**

View file

@ -198,6 +198,9 @@ class Media_Uploader {
'Bucket' => $bucket,
'Key' => $object_key,
'Body' => $fh,
// ACL is public-read by design: this plugin serves media via a CDN
// or S3 URL. Files must be publicly accessible for WordPress to
// display them. Bucket policies should restrict list access separately.
'ACL' => 'public-read',
)
);
@ -377,17 +380,6 @@ class Media_Uploader {
return;
}
if ( ! function_exists( 'WP_Filesystem' ) ) {
require_once ABSPATH . 'wp-admin/includes/file.php';
}
WP_Filesystem();
global $wp_filesystem;
if ( ! ( $wp_filesystem instanceof \WP_Filesystem_Base ) ) {
$this->logger->error( 'WP_Filesystem not available, aborting cleanup' );
return;
}
$current_time = time();
$new_queue = array();
$deleted = 0;
@ -414,8 +406,11 @@ class Media_Uploader {
continue;
}
if ( $wp_filesystem->exists( $file_path ) ) {
$deleted_ok = $wp_filesystem->delete( $file_path );
if ( file_exists( $file_path ) ) {
// Use unlink directly: wp_delete_file() returns void on WP < 6.7,
// so the boolean return is unreliable across the declared WP range.
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read -- cron cleanup, WP_Filesystem unavailable without credentials
$deleted_ok = unlink( $file_path );
if ( $deleted_ok ) {
++$deleted;
} else {