/2026/09/photo-300x200.jpg. * - A resumable background sync walks the uploads folder one directory at a time and * uploads new/changed files in parallel. What's been uploaded is tracked in a DB table. * - New media is uploaded automatically right after WordPress creates its sizes. * - Exported pages point media URLs at the public R2 domain, and uploads can be left * out of the static site. */ if ( ! defined( 'ABSPATH' ) ) { exit; } class CFSP_Media { const DB_VERSION = '1'; const JOB = 'cfsp_media_job'; const QUEUE = 'cfsp_media_queue'; const STATS = 'cfsp_media_stats'; const LOCK = 'cfsp_media_lock'; const PAUSED = 'cfsp_media_paused'; // Survives plugin updates (stored in the database). const PLACEHOLDER = '__CFSP_R2_MEDIA__'; public static function init() { if ( get_option( 'cfsp_media_db' ) !== self::DB_VERSION ) { self::install(); } add_action( 'wp_ajax_cfsp_media_worker', array( __CLASS__, 'handle_worker' ) ); add_action( 'wp_ajax_nopriv_cfsp_media_worker', array( __CLASS__, 'handle_worker' ) ); add_action( 'cfsp_watchdog', array( __CLASS__, 'watchdog' ) ); add_action( 'cfsp_media_resync', array( __CLASS__, 'scheduled_sync' ) ); add_action( 'cfsp_settings_updated', array( __CLASS__, 'reschedule' ) ); if ( self::enabled() ) { if ( CFSP_Settings::get( 'r2_auto_upload' ) ) { add_filter( 'wp_generate_attachment_metadata', array( __CLASS__, 'on_metadata' ), 99, 2 ); add_filter( 'wp_update_attachment_metadata', array( __CLASS__, 'on_metadata' ), 99, 2 ); } if ( CFSP_Settings::get( 'r2_delete' ) ) { add_action( 'delete_attachment', array( __CLASS__, 'on_delete' ), 10, 1 ); } } } public static function enabled() { return CFSP_Settings::get( 'r2_enabled' ) && CFSP_R2::is_configured(); } /* ------------------------------------------------------------------ * * Database * ------------------------------------------------------------------ */ public static function table() { global $wpdb; return $wpdb->prefix . 'cfsp_media'; } public static function install() { global $wpdb; require_once ABSPATH . 'wp-admin/includes/upgrade.php'; $table = self::table(); $charset = $wpdb->get_charset_collate(); dbDelta( "CREATE TABLE {$table} ( id char(32) NOT NULL, dir varchar(191) NOT NULL DEFAULT '', name varchar(255) NOT NULL, size bigint(20) unsigned NOT NULL DEFAULT 0, mtime int(10) unsigned NOT NULL DEFAULT 0, uploaded int(10) unsigned NOT NULL DEFAULT 0, PRIMARY KEY (id), KEY dir (dir) ) {$charset};" ); update_option( 'cfsp_media_db', self::DB_VERSION ); } private static function row_id( $rel ) { return md5( $rel ); } /** Known files of one directory: name => [ size, mtime ]. */ private static function known( $dir ) { global $wpdb; // The plugin's own table of files uploaded to R2; always read fresh, since batches run in parallel requests. $rows = $wpdb->get_results( $wpdb->prepare( "SELECT name, size, mtime FROM {$wpdb->prefix}cfsp_media WHERE dir = %s", $dir ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery $out = array(); foreach ( (array) $rows as $r ) { $out[ $r->name ] = array( (int) $r->size, (int) $r->mtime ); } return $out; } private static function remember( $rel, $size, $mtime ) { global $wpdb; $dir = dirname( $rel ); $wpdb->replace( // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- The plugin's own table. self::table(), array( 'id' => self::row_id( $rel ), 'dir' => '.' === $dir ? '' : $dir, 'name' => basename( $rel ), 'size' => $size, 'mtime' => $mtime, 'uploaded' => time(), ), array( '%s', '%s', '%s', '%d', '%d', '%d' ) ); } private static function forget( $rel ) { global $wpdb; $wpdb->delete( self::table(), array( 'id' => self::row_id( $rel ) ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- The plugin's own table. } public static function totals() { global $wpdb; $row = $wpdb->get_row( "SELECT COUNT(*) AS files, COALESCE(SUM(size), 0) AS bytes, MAX(uploaded) AS last FROM {$wpdb->prefix}cfsp_media" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- The plugin's own table; the totals are stored in an option afterwards. return array( 'files' => $row ? (int) $row->files : 0, 'bytes' => $row ? (int) $row->bytes : 0, 'last' => $row ? (int) $row->last : 0, ); } /* ------------------------------------------------------------------ * * Paths * ------------------------------------------------------------------ */ public static function uploads() { static $u = null; if ( null === $u ) { $u = wp_upload_dir( null, false ); } return $u; } /** Object key for an uploads-relative path. */ public static function key( $rel ) { $prefix = trim( (string) CFSP_Settings::get( 'r2_prefix' ), '/' ); return ( '' === $prefix ? '' : $prefix . '/' ) . ltrim( $rel, '/' ); } /** Public URL prefix that replaces the uploads base URL, e.g. https://cdn.example.com/wp-content/uploads */ public static function public_base() { $prefix = trim( (string) CFSP_Settings::get( 'r2_prefix' ), '/' ); return untrailingslashit( (string) CFSP_Settings::get( 'r2_public_url' ) ) . ( '' === $prefix ? '' : '/' . $prefix ); } private static function excluded_dirs() { // Private folders other plugins keep inside uploads (backups, protected downloads, logs). // They must never end up in a public bucket. $private = array( 'wp-file-manager-pro/fm_backup', 'wp-file-manager/fm_backup', 'woocommerce_uploads', 'wc-logs', 'edd', 'backwpup-logs', 'backwpup-temp', 'backups-dup-lite', 'ai1wm-backups', 'updraft', ); return array_merge( array( CFSP_Settings::DATA_DIR, 'cfsp-static', 'cfsp-work' ), $private, CFSP_Settings::lines( 'r2_exclude_dirs' ) ); } /** * Should this uploads-relative path be skipped? * * @param string $rel Path relative to the uploads folder. * @param bool $is_dir Folders are only checked against "Skip folders", not name rules. */ public static function skip( $rel, $is_dir = false ) { $rel = ltrim( str_replace( '\\', '/', $rel ), '/' ); if ( preg_match( '#(^|/)\.|\.(php\d?|phtml|phar|sql|log|ini|htaccess)$#i', $rel ) ) { return true; } foreach ( self::excluded_dirs() as $dir ) { $dir = trim( str_replace( '\\', '/', $dir ), '/' ); if ( '' !== $dir && ( $rel === $dir || 0 === strpos( $rel, $dir . '/' ) ) ) { return true; } } if ( ! $is_dir ) { static $rules = null; if ( null === $rules ) { $rules = self::name_rules( CFSP_Settings::get( 'r2_exclude_names' ) ); } return null !== self::matching_rule( $rel, $rules ); } return false; } /** Parse "Skip files named" text into rules (lowercased, one per line). */ public static function name_rules( $text ) { $rules = array(); foreach ( preg_split( '/\r\n|\r|\n/', (string) $text ) as $line ) { $line = strtolower( trim( $line ) ); if ( '' !== $line ) { $rules[] = $line; } } return array_values( array_unique( $rules ) ); } /** * The first rule that matches a file, or null. * plain text file name contains it (case-insensitive), e.g. "-150x150" * * and ? wildcards whole file name must match, e.g. "*.bak", "*-scaled.*" * a rule with "/" matched against the path inside uploads instead, e.g. "2019/*" */ public static function matching_rule( $rel, array $rules ) { $rel = strtolower( $rel ); $name = basename( $rel ); foreach ( $rules as $rule ) { $subject = false !== strpos( $rule, '/' ) ? $rel : $name; if ( false !== strpbrk( $rule, '*?' ) ) { if ( fnmatch( ltrim( $rule, '/' ), $subject ) ) { return $rule; } } elseif ( false !== strpos( $subject, ltrim( $rule, '/' ) ) ) { return $rule; } } return null; } /** * Count the files a set of name rules would skip (for the Preview button). * * @return array [ 'checked' => int, 'skipped' => int, 'bytes' => int, 'examples' => string[], 'per_rule' => [ rule => count ], 'truncated' => bool ] */ public static function preview_skips( $text, $max_files = 400000 ) { $rules = self::name_rules( $text ); $out = array( 'checked' => 0, 'skipped' => 0, 'bytes' => 0, 'examples' => array(), 'per_rule' => array_fill_keys( $rules, 0 ), 'truncated' => false ); $base = self::uploads()['basedir']; if ( ! $rules || ! is_dir( $base ) ) { return $out; } $it = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $base, FilesystemIterator::SKIP_DOTS ) ); foreach ( $it as $file ) { if ( ! $file->isFile() ) { continue; } $rel = ltrim( str_replace( '\\', '/', substr( $file->getPathname(), strlen( $base ) ) ), '/' ); // Files the other rules already skip don't count. if ( preg_match( '#(^|/)\.|\.(php\d?|phtml|phar|sql|log|ini|htaccess)$#i', $rel ) || self::skip( dirname( $rel ), true ) ) { continue; } $out['checked']++; $rule = self::matching_rule( $rel, $rules ); if ( null !== $rule ) { $out['skipped']++; $out['bytes'] += $file->getSize(); $out['per_rule'][ $rule ]++; if ( count( $out['examples'] ) < 8 ) { $out['examples'][] = $rel; } } if ( $out['checked'] >= $max_files ) { $out['truncated'] = true; break; } } return $out; } private static function content_type( $rel ) { $ext = strtolower( pathinfo( $rel, PATHINFO_EXTENSION ) ); if ( isset( CFSP_Exporter::MIME[ $ext ] ) ) { return CFSP_Exporter::MIME[ $ext ]; } $check = wp_check_filetype( $rel ); return $check['type'] ? $check['type'] : 'application/octet-stream'; } /* ------------------------------------------------------------------ * * Jobs * ------------------------------------------------------------------ */ public static function job() { wp_cache_delete( self::JOB, 'options' ); $job = get_option( self::JOB ); return is_array( $job ) ? $job : null; } public static function is_running( $job = null ) { $job = $job ? $job : self::job(); return $job && 'running' === $job['phase']; } /** * Whether the job a batch started with is still the running one. False once it is cancelled, * paused or replaced by a new sync, so a batch still busy from before stops and never writes * its old counters over the new job. */ private static function still_current( $started ) { $current = self::job(); return $current && (int) $current['started'] === (int) $started && 'running' === $current['phase']; } /** True when the stored job is no longer the one this batch loaded (cancelled and restarted). */ private static function replaced( array $job ) { $current = self::job(); return ! $current || (int) $current['started'] !== (int) $job['started']; } /** Start a full library sync (uploads everything new or changed). */ public static function start_sync( $reason = 'Manual sync' ) { if ( ! self::enabled() ) { return new WP_Error( 'cfsp_r2', 'Turn on Media offload and fill in the R2 connection first.' ); } if ( self::is_running() ) { return new WP_Error( 'cfsp_r2', 'A media sync is already running.' ); } if ( self::is_paused() ) { return self::resume(); } $base = self::uploads()['basedir']; $dirs = array( '' ); if ( is_dir( $base ) ) { $it = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $base, FilesystemIterator::SKIP_DOTS ), RecursiveIteratorIterator::SELF_FIRST ); foreach ( $it as $item ) { if ( $item->isDir() ) { $rel = ltrim( str_replace( '\\', '/', substr( $item->getPathname(), strlen( $base ) ) ), '/' ); if ( ! self::skip( $rel, true ) ) { $dirs[] = $rel; } } } } sort( $dirs ); update_option( self::JOB, array( 'phase' => 'running', 'reason' => $reason, 'dirs' => $dirs, 'dir_i' => 0, 'checked' => 0, 'uploaded' => 0, 'bytes' => 0, 'errors' => 0, 'started' => time(), 'updated' => time(), 'message' => '', ), false ); CFSP_Logger::log( sprintf( 'Media sync started: %d folders to check. %s', count( $dirs ), $reason ) ); self::spawn(); return true; } /** Short "where we are" text for a sync or cleanup job. */ private static function progress_note( $job ) { if ( 'cleanup' === ( $job['kind'] ?? 'sync' ) ) { return sprintf( 'checked %d files in R2, removed %d', $job['scanned'], $job['deleted'] ); } return sprintf( 'folder %d of %d', min( $job['dir_i'] + 1, count( $job['dirs'] ) ), count( $job['dirs'] ) ); } /** * Delete files from R2 that match the current skip rules (Skip folders / Skip files named), * e.g. sizes that were uploaded before a rule was added. Walks the bucket itself, 1,000 keys * per page, so it also finds files the plugin has no record of. */ public static function start_cleanup() { if ( ! self::enabled() ) { return new WP_Error( 'cfsp_r2', 'Turn on Media offload and fill in the R2 connection first.' ); } $job = self::job(); if ( $job && ( self::is_running( $job ) || self::is_paused( $job ) ) ) { return new WP_Error( 'cfsp_r2', 'Finish, cancel or resume the current media job first.' ); } delete_option( self::PAUSED ); update_option( self::JOB, array( 'kind' => 'cleanup', 'phase' => 'running', 'reason' => 'Remove skipped files', 'dirs' => array(), 'dir_i' => 0, 'token' => null, 'scanned' => 0, 'deleted' => 0, 'checked' => 0, 'uploaded' => 0, 'bytes' => 0, 'errors' => 0, 'started' => time(), 'updated' => time(), 'message' => '', ), false ); CFSP_Logger::log( 'Media: removing files that match the skip rules from R2.' ); self::spawn(); return true; } /** One page (up to 1,000 keys) at a time until the bucket has been walked or time runs out. */ private static function cleanup_step( CFSP_R2 $r2, array $job, $deadline ) { $prefix = trim( (string) CFSP_Settings::get( 'r2_prefix' ), '/' ); $prefix = '' === $prefix ? '' : $prefix . '/'; do { $page = $r2->list_objects( $prefix, $job['token'] ); if ( is_wp_error( $page ) ) { $job['errors']++; CFSP_Logger::error( 'Media cleanup: ' . $page->get_error_message() ); if ( $job['errors'] >= 5 ) { $job['phase'] = 'failed'; $job['message'] = $page->get_error_message(); } return $job; } $doomed = array(); foreach ( $page['keys'] as $key ) { $job['scanned']++; $rel = substr( $key, strlen( $prefix ) ); if ( '' !== $rel && '/' !== substr( $key, -1 ) && self::skip( $rel ) ) { $doomed[ $key ] = $rel; } } if ( $doomed ) { $result = $r2->delete_many( array_keys( $doomed ) ); if ( is_wp_error( $result ) ) { $job['errors']++; CFSP_Logger::error( 'Media cleanup: ' . $result->get_error_message() ); if ( $job['errors'] >= 5 ) { $job['phase'] = 'failed'; $job['message'] = $result->get_error_message(); } return $job; // Same page is retried next batch (token unchanged). } foreach ( $doomed as $key => $rel ) { if ( ! isset( $result['errors'][ $key ] ) ) { self::forget( $rel ); } else { CFSP_Logger::error( 'Media cleanup: could not delete ' . $rel . ' (' . $result['errors'][ $key ] . ')' ); } } $job['deleted'] += $result['deleted']; $job['errors'] += count( $result['errors'] ); CFSP_Logger::log( sprintf( 'Media cleanup: removed %d file(s) from R2 (%d checked so far).', $result['deleted'], $job['scanned'] ) ); } $job['token'] = $page['next']; if ( ! $page['next'] ) { $job['phase'] = 'done'; $job['message'] = sprintf( 'Checked %d files in R2 and removed %d that match the skip rules.', $job['scanned'], $job['deleted'] ); CFSP_Logger::log( 'Media cleanup finished. ' . $job['message'] ); return $job; } } while ( time() < $deadline && ! self::pause_requested() ); return $job; } public static function is_paused( $job = null ) { $job = $job ? $job : self::job(); return $job && 'paused' === $job['phase']; } /** True while uploads should stop: a paused sync also holds back queued new media. */ public static function pause_requested() { wp_cache_delete( self::PAUSED, 'options' ); return (bool) get_option( self::PAUSED ); } /** * Pause the running sync. The batch in progress stops after its current upload round * and remembers the folder it was in; nothing already uploaded is sent again. */ public static function pause() { $job = self::job(); if ( ! $job || ! self::is_running( $job ) ) { return new WP_Error( 'cfsp_r2', 'No media sync is running.' ); } update_option( self::PAUSED, 1, false ); $job['phase'] = 'paused'; $job['message'] = 'Paused at ' . self::progress_note( $job ) . '.'; update_option( self::JOB, $job, false ); CFSP_Logger::log( 'Media job paused. ' . $job['message'] ); return true; } public static function resume() { $job = self::job(); delete_option( self::PAUSED ); if ( ! $job || ! self::is_paused( $job ) ) { self::spawn(); // Still process any queued new media. return new WP_Error( 'cfsp_r2', 'There is no paused media sync.' ); } $job['phase'] = 'running'; $job['message'] = ''; $job['updated'] = time(); update_option( self::JOB, $job, false ); CFSP_Logger::log( 'Media job resumed at ' . self::progress_note( $job ) . '.' ); self::spawn(); return true; } public static function cancel() { delete_option( self::PAUSED ); $job = self::job(); if ( $job && ( self::is_running( $job ) || self::is_paused( $job ) ) ) { $job['phase'] = 'cancelled'; $job['message'] = 'Cancelled by user.'; update_option( self::JOB, $job, false ); CFSP_Logger::log( 'Media sync cancelled.' ); } self::release_lock(); } /** Queue specific files (auto-upload of new media). */ public static function enqueue( array $rels ) { $queue = get_option( self::QUEUE, array() ); $queue = is_array( $queue ) ? $queue : array(); foreach ( $rels as $rel ) { if ( ! self::skip( $rel ) ) { $queue[ $rel ] = 1; } } update_option( self::QUEUE, $queue, false ); self::spawn(); } public static function spawn() { wp_remote_post( admin_url( 'admin-ajax.php' ), CFSP_Exporter::loopback_args( array( 'blocking' => false, 'timeout' => 0.01, 'sslverify' => false, 'body' => array( 'action' => 'cfsp_media_worker', 'key' => get_option( 'cfsp_worker_key' ), ), ) ) ); } public static function handle_worker() { $key = isset( $_POST['key'] ) ? sanitize_text_field( wp_unslash( $_POST['key'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification if ( ! $key || ! hash_equals( (string) get_option( 'cfsp_worker_key' ), $key ) ) { wp_die( '', '', array( 'response' => 403 ) ); } ignore_user_abort( true ); self::run_batch(); wp_die( '', '', array( 'response' => 200 ) ); } public static function watchdog() { if ( self::pause_requested() ) { return; } $job = self::job(); $queue = get_option( self::QUEUE, array() ); if ( ( $job && self::is_running( $job ) && $job['updated'] < time() - 45 ) || ! empty( $queue ) ) { self::run_batch(); } } private static function acquire_lock( $ttl ) { global $wpdb; $now = time(); // An atomic lock: INSERT IGNORE succeeds for exactly one request, which add_option() can't guarantee. // phpcs:disable WordPress.DB.DirectDatabaseQuery $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->options} WHERE option_name = %s AND CAST(option_value AS UNSIGNED) < %d", self::LOCK, $now ) ); $inserted = $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO {$wpdb->options} (option_name, option_value, autoload) VALUES (%s, %s, 'no')", self::LOCK, (string) ( $now + $ttl ) ) ); // phpcs:enable wp_cache_delete( self::LOCK, 'options' ); return 1 === (int) $inserted; } private static function release_lock() { global $wpdb; $wpdb->delete( $wpdb->options, array( 'option_name' => self::LOCK ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- Paired with acquire_lock(). wp_cache_delete( self::LOCK, 'options' ); } /** * One time-boxed batch: first queued files (new media), then the running full sync. * * @param bool $chain Spawn the next batch if work remains. */ public static function run_batch( $chain = true ) { if ( ! self::enabled() || self::pause_requested() ) { return false; } $seconds = (int) CFSP_Settings::get( 'batch_seconds' ); if ( ! self::acquire_lock( $seconds + 180 ) ) { return false; } if ( function_exists( 'set_time_limit' ) ) { @set_time_limit( $seconds + 180 ); // phpcs:ignore WordPress.PHP.NoSilencedErrors, Squiz.PHP.DiscouragedFunctions.Discouraged -- A media batch runs up to "Batch length" seconds plus uploads in progress; hosts may disable the function, hence the check and @. } wp_raise_memory_limit( 'admin' ); $deadline = time() + $seconds; $r2 = new CFSP_R2(); $base = trailingslashit( self::uploads()['basedir'] ); // 1) Queued files. wp_cache_delete( self::QUEUE, 'options' ); $queue = get_option( self::QUEUE, array() ); if ( is_array( $queue ) && $queue ) { $files = array(); foreach ( array_keys( $queue ) as $rel ) { if ( is_file( $base . $rel ) && ! self::skip( (string) $rel ) ) { $files[ $rel ] = array( filesize( $base . $rel ), filemtime( $base . $rel ) ); } } self::upload_files( $r2, $files, $deadline, $stats ); wp_cache_delete( self::QUEUE, 'options' ); $fresh = get_option( self::QUEUE, array() ); // New items may have arrived meanwhile. $fresh = is_array( $fresh ) ? $fresh : array(); // Attempted files leave the queue even if they failed (logged); the next full sync retries them. foreach ( $stats['attempted'] as $rel ) { unset( $fresh[ $rel ] ); } // Files that no longer exist (deleted, or replaced by WordPress, e.g. "-scaled" images) or // that now match a skip rule leave the queue too; otherwise they would stay queued forever. foreach ( array_keys( $queue ) as $rel ) { if ( ! is_file( $base . $rel ) || self::skip( (string) $rel ) ) { unset( $fresh[ $rel ] ); } } update_option( self::QUEUE, $fresh, false ); if ( $stats['uploaded'] ) { CFSP_Logger::log( sprintf( 'Media: uploaded %d new file(s) to R2.', $stats['uploaded'] ) ); } } // 2) Removing skipped files, or a full sync one directory at a time. $job = self::job(); if ( $job && self::is_running( $job ) && 'cleanup' === ( $job['kind'] ?? 'sync' ) ) { $job = self::cleanup_step( $r2, $job, $deadline ); $current = self::job(); if ( $current && in_array( $current['phase'], array( 'paused', 'cancelled' ), true ) && 'running' === $job['phase'] ) { $job['phase'] = $current['phase']; $job['message'] = 'paused' === $current['phase'] ? 'Paused at ' . self::progress_note( $job ) . '.' : $current['message']; } $job['updated'] = time(); if ( ! self::replaced( $job ) ) { update_option( self::JOB, $job, false ); } } elseif ( $job && self::is_running( $job ) ) { while ( time() < $deadline && $job['dir_i'] < count( $job['dirs'] ) && ! self::pause_requested() && self::still_current( $job['started'] ) ) { $dir = $job['dirs'][ $job['dir_i'] ]; $path = $base . ( '' === $dir ? '' : $dir . '/' ); $known = self::known( $dir ); $pending = array(); $found = 0; $entries = is_dir( $path ) ? scandir( $path ) : array(); foreach ( (array) $entries as $name ) { if ( '.' === $name || '..' === $name || ! is_file( $path . $name ) ) { continue; } $rel = ( '' === $dir ? '' : $dir . '/' ) . $name; if ( self::skip( $rel ) ) { continue; } $found++; $size = filesize( $path . $name ); $mtime = filemtime( $path . $name ); if ( ! isset( $known[ $name ] ) || $known[ $name ][0] !== $size || $known[ $name ][1] !== $mtime ) { $pending[ $rel ] = array( $size, $mtime ); } } self::upload_files( $r2, $pending, $deadline, $stats, $job['started'] ); $job['uploaded'] += $stats['uploaded']; $job['bytes'] += $stats['bytes']; $job['errors'] += $stats['errors']; if ( '' !== $stats['last_error'] ) { $job['last_error'] = $stats['last_error']; } if ( count( $stats['attempted'] ) < count( $pending ) ) { break; // Out of time mid-folder; the next batch re-checks this folder. } // Counted once the folder is finished, so a large folder spread over several // batches isn't counted again by each one. $job['checked'] += $found; $job['dir_i']++; if ( $stats['uploaded'] ) { CFSP_Logger::log( sprintf( 'Media: %s: uploaded %d file(s) (%s).', '' === $dir ? 'uploads/' : $dir, $stats['uploaded'], size_format( $stats['bytes'] ) ) ); } } if ( $job['dir_i'] >= count( $job['dirs'] ) ) { $job['phase'] = 'done'; $job['message'] = sprintf( 'Checked %d files, uploaded %d (%s), %d errors.', $job['checked'], $job['uploaded'], size_format( $job['bytes'] ), $job['errors'] ); CFSP_Logger::log( 'Media sync finished. ' . $job['message'] ); } // A pause or cancel made while this batch ran wins; the progress made is still saved. // If the sync was cancelled and a new one started meanwhile, leave the new one alone. $current = self::job(); if ( $current && in_array( $current['phase'], array( 'paused', 'cancelled' ), true ) && 'done' !== $job['phase'] ) { $job['phase'] = $current['phase']; $job['message'] = 'paused' === $current['phase'] ? 'Paused at ' . self::progress_note( $job ) . '.' : $current['message']; } $job['updated'] = time(); if ( ! self::replaced( $job ) ) { update_option( self::JOB, $job, false ); } } update_option( self::STATS, self::totals(), false ); self::release_lock(); wp_cache_delete( self::QUEUE, 'options' ); if ( $chain && ( self::is_running() || get_option( self::QUEUE, array() ) ) ) { self::spawn(); } return true; } /** * Upload files, several at once, until done or out of time: rounds of up to the "r2_parallel" * setting (see CFSP_R2::put_rolling). * * @param array $files [ rel => [ size, mtime ] ] * @return string[] Uploaded rel paths. */ private static function upload_files( CFSP_R2 $r2, array $files, $deadline, &$stats, $started = null ) { $stats = array( 'uploaded' => 0, 'bytes' => 0, 'errors' => 0, 'attempted' => array(), 'last_error' => '' ); $done = array(); $base = trailingslashit( self::uploads()['basedir'] ); $send = array(); foreach ( $files as $rel => $info ) { if ( $info[0] > 300 * MB_IN_BYTES ) { $stats['attempted'][] = (string) $rel; $stats['last_error'] = $rel . ': over 300 MB; upload it with rclone or the dashboard.'; CFSP_Logger::error( 'Media: skipped ' . $stats['last_error'] ); $stats['errors']++; continue; } $send[ $rel ] = array( 'key' => self::key( $rel ), 'path' => $base . $rel, 'type' => self::content_type( $rel ), 'size' => $info[0], ); } if ( ! $send ) { return $done; } $checked = 0; $started_ids = $r2->put_rolling( $send, (int) CFSP_Settings::get( 'r2_parallel' ), function () use ( $deadline, $started, &$checked ) { if ( time() >= $deadline || self::pause_requested() ) { return false; } // Every few seconds, make sure the sync wasn't cancelled or replaced meanwhile. if ( null !== $started && time() - $checked >= 3 ) { $checked = time(); return self::still_current( $started ); } return true; }, function ( $rel, $result ) use ( $files, &$done, &$stats ) { if ( true === $result ) { self::remember( $rel, $files[ $rel ][0], $files[ $rel ][1] ); $done[] = $rel; $stats['uploaded']++; $stats['bytes'] += $files[ $rel ][0]; } else { $stats['errors']++; $stats['last_error'] = $rel . ': ' . $result->get_error_message(); CFSP_Logger::error( 'Media: ' . $stats['last_error'] ); } } ); $stats['attempted'] = array_merge( $stats['attempted'], $started_ids ); return $done; } public static function scheduled_sync() { // Never auto-resume something the user paused. if ( self::enabled() && ! self::is_running() && ! self::is_paused() && ! self::pause_requested() ) { self::start_sync( 'Scheduled re-sync' ); } } public static function reschedule() { $recurrence = CFSP_Settings::get( 'r2_resync' ); $current = wp_get_schedule( 'cfsp_media_resync' ); if ( 'off' === $recurrence || ! CFSP_Settings::get( 'r2_enabled' ) ) { wp_clear_scheduled_hook( 'cfsp_media_resync' ); } elseif ( $current !== $recurrence ) { wp_clear_scheduled_hook( 'cfsp_media_resync' ); wp_schedule_event( time() + HOUR_IN_SECONDS, $recurrence, 'cfsp_media_resync' ); } } /* ------------------------------------------------------------------ * * Attachment hooks * ------------------------------------------------------------------ */ /** Every file of an attachment, relative to uploads: original, -scaled, all sizes, edit backups. */ public static function attachment_files( $attachment_id, $meta = null ) { $meta = is_array( $meta ) ? $meta : wp_get_attachment_metadata( $attachment_id ); $file = get_post_meta( $attachment_id, '_wp_attached_file', true ); if ( ! $file ) { return array(); } $dir = dirname( $file ); $dir = '.' === $dir ? '' : $dir . '/'; $files = array( $file ); if ( is_array( $meta ) ) { if ( ! empty( $meta['original_image'] ) ) { $files[] = $dir . $meta['original_image']; } foreach ( (array) ( $meta['sizes'] ?? array() ) as $size ) { if ( ! empty( $size['file'] ) ) { $files[] = $dir . $size['file']; } foreach ( (array) ( $size['sources'] ?? array() ) as $source ) { // WebP/AVIF sub-sizes (WP 6.1+ / Performance Lab). if ( ! empty( $source['file'] ) ) { $files[] = $dir . $source['file']; } } } foreach ( (array) ( $meta['sources'] ?? array() ) as $source ) { if ( ! empty( $source['file'] ) ) { $files[] = $dir . $source['file']; } } } foreach ( (array) get_post_meta( $attachment_id, '_wp_attachment_backup_sizes', true ) as $backup ) { if ( is_array( $backup ) && ! empty( $backup['file'] ) ) { $files[] = $dir . $backup['file']; } } return array_values( array_unique( $files ) ); } public static function on_metadata( $meta, $attachment_id ) { self::enqueue( self::attachment_files( $attachment_id, $meta ) ); return $meta; } public static function on_delete( $attachment_id ) { $r2 = new CFSP_R2(); foreach ( self::attachment_files( $attachment_id ) as $rel ) { $result = $r2->delete( self::key( $rel ) ); if ( true === $result ) { self::forget( $rel ); } else { CFSP_Logger::error( 'Media: could not delete ' . $rel . ' from R2: ' . $result->get_error_message() ); } } } /* ------------------------------------------------------------------ * * Export integration * ------------------------------------------------------------------ */ /** Rewrite media URLs to the R2 domain in exported pages? */ public static function rewrite_enabled() { return CFSP_Settings::get( 'r2_enabled' ) && CFSP_Settings::get( 'r2_rewrite' ) && CFSP_Settings::get( 'r2_public_url' ); } /** Leave uploads out of the static export? */ public static function exclude_uploads() { return self::rewrite_enabled() && CFSP_Settings::get( 'r2_exclude_export' ); } /** Uploads path relative to the WordPress home, e.g. "/wp-content/uploads/". */ public static function uploads_rel_path() { return trailingslashit( CFSP_Exporter::relative_path( self::uploads()['baseurl'] . '/' ) ); } /** * Step 1 of the rewrite (before origin replacement): swap every form of the uploads base URL * for a placeholder, so later origin/path rewrites can't touch it. */ public static function protect( $content ) { if ( ! self::rewrite_enabled() ) { return $content; } $baseurl = self::uploads()['baseurl']; $parts = wp_parse_url( $baseurl ); $hp = $parts['host'] . ( isset( $parts['port'] ) ? ':' . $parts['port'] : '' ) . rtrim( $parts['path'] ?? '', '/' ) . '/'; $path = rtrim( $parts['path'] ?? '', '/' ) . '/'; $map = array(); foreach ( array( 'https://' . $hp, 'http://' . $hp, '//' . $hp ) as $from ) { $map[ $from ] = self::PLACEHOLDER . '/'; $map[ str_replace( '/', '\\/', $from ) ] = self::PLACEHOLDER . '\\/'; } $content = strtr( $content, $map ); // Root-relative references: src="/wp-content/uploads/…", url(/wp-content/uploads/…). $content = preg_replace( '#(["\'(\s,=])' . preg_quote( $path, '#' ) . '#', '$1' . self::PLACEHOLDER . '/', $content ); return $content; } /** Step 2 (after all other rewrites): placeholder → public R2 URL, then fix links to skipped files. */ public static function restore( $content ) { if ( false !== strpos( $content, self::PLACEHOLDER ) ) { $public = self::public_base(); $content = strtr( $content, array( self::PLACEHOLDER . '\\/' => str_replace( '/', '\\/', $public ) . '\\/', self::PLACEHOLDER . '/' => $public . '/', ) ); } return self::fix_skipped( $content ); } /** * Files kept out of R2 by the skip rules (e.g. "*-307x200.png") would be broken on the CDN, but * WordPress still writes them into src/srcset. Drop them from srcset, and point any other reference * to a skipped size at the original upload (photo-300x200.png → photo.png, or photo-scaled.png). */ public static function fix_skipped( $content ) { if ( ! self::rewrite_enabled() || '' === trim( (string) CFSP_Settings::get( 'r2_exclude_names' ) ) ) { return $content; } $base = self::public_base() . '/'; if ( false === strpos( $content, $base ) ) { return $content; } $qbase = preg_quote( $base, '#' ); // 1) srcset / data-srcset: remove candidates that point at skipped files. $content = preg_replace_callback( '#(\b(?:data-)?srcset\s*=\s*)(["\'])(.*?)\2#is', function ( $m ) use ( $base ) { $kept = array(); $changed = false; foreach ( preg_split( '/,\s+(?=\S)/', trim( $m[3] ) ) as $candidate ) { $url = strtok( trim( $candidate ), " \t\n" ); if ( $url && 0 === strpos( $url, $base ) && self::skipped_url( substr( $url, strlen( $base ) ) ) ) { $changed = true; continue; } $kept[] = trim( $candidate ); } if ( ! $changed ) { return $m[0]; } return $kept ? $m[1] . $m[2] . implode( ', ', $kept ) . $m[2] : ''; }, $content ); // 1b) An whose srcset is down to one image (or none) just repeats src: drop srcset and sizes. $content = preg_replace_callback( '#]*>#i', function ( $m ) { $tag = $m[0]; if ( preg_match( '#\ssrcset\s*=\s*(["\'])(.*?)\1#is', $tag, $s ) && count( preg_split( '/,\s+(?=\S)/', trim( $s[2] ) ) ) > 1 ) { return $tag; } return preg_replace( '#\s+(?:srcset|sizes)\s*=\s*(["\']).*?\1#is', '', $tag ); }, $content ); // 2) Every other reference (src, data-src, og:image, CSS url(), JSON-LD): use the original. return preg_replace_callback( '#' . $qbase . '([^"\'\s)<>,?\#]+)#', function ( $m ) use ( $base ) { if ( ! self::skipped_url( $m[1] ) ) { return $m[0]; } $original = self::original_of( $m[1] ); return $original ? $base . $original : $m[0]; }, $content ); } /** Is this uploads-relative path (as it appears in a URL) kept out of R2? Cached per request. */ private static function skipped_url( $rel ) { static $cache = array(); $rel = rawurldecode( $rel ); if ( ! isset( $cache[ $rel ] ) ) { $cache[ $rel ] = self::skip( $rel ); } return $cache[ $rel ]; } /** The original upload behind a WordPress size ("2021/10/photo-300x200.png" → "2021/10/photo.png"), or null. */ private static function original_of( $rel ) { static $cache = array(); if ( array_key_exists( $rel, $cache ) ) { return $cache[ $rel ]; } $cache[ $rel ] = null; if ( preg_match( '#^(.+)-\d+x\d+(\.[a-z0-9]+)$#i', rawurldecode( $rel ), $m ) ) { $dir = trailingslashit( self::uploads()['basedir'] ); foreach ( array( $m[1] . $m[2], $m[1] . '-scaled' . $m[2] ) as $candidate ) { if ( is_file( $dir . $candidate ) && ! self::skip( $candidate ) ) { $cache[ $rel ] = str_replace( '%2F', '/', rawurlencode( $candidate ) ); break; } } } return $cache[ $rel ]; } }