Skip to content

Instantly share code, notes, and snippets.

@BhargavBhandari90
Last active September 18, 2025 10:11
Show Gist options
  • Save BhargavBhandari90/4f44aebed235ad415fbf0e26fb596deb to your computer and use it in GitHub Desktop.
Save BhargavBhandari90/4f44aebed235ad415fbf0e26fb596deb to your computer and use it in GitHub Desktop.
<?php
// Log files in oneshot - NO generator
function fp_scan_images_oneshot($only_non_optimized = false)
{
$base_directory = wp_upload_dir();
$directory = $base_directory['basedir'];
$optimized_directory = WP_CONTENT_DIR . '/flying-press-images';
// Recursive scan with FileSystemIterator
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($directory, FilesystemIterator::SKIP_DOTS)
);
if (empty($iterator)) {
return [];
}
$batch = [];
foreach ($iterator as $file) {
if (!$file->isFile() || !in_array(strtolower($file->getExtension()), ['jpg', 'jpeg', 'png'], true)) {
continue;
}
if ($only_non_optimized) {
$optimized_file = str_replace($directory, $optimized_directory, $file->getPathname());
if (is_file($optimized_file)) {
continue;
}
}
$batch[] = str_replace($directory, $base_directory['baseurl'], $file->getPathname());
if (count($batch) >= 100) {
error_log(print_r($batch, true));
$batch = [];
}
}
// If batch still contains images, log them
if ($batch) {
error_log(print_r($batch, true));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment