Skip to content

Instantly share code, notes, and snippets.

@BhargavBhandari90
Created September 17, 2025 18:09
Show Gist options
  • Save BhargavBhandari90/8f9e80769ed50372910fd8b435c3abd4 to your computer and use it in GitHub Desktop.
Save BhargavBhandari90/8f9e80769ed50372910fd8b435c3abd4 to your computer and use it in GitHub Desktop.
Log image links to error log file
<?php
/**
* Log scanned image links to debug.log.
*
* Make sure to enable WP_DEBUG_LOG from wp-config.php to use this command.
*/
function process_image_batch_new($batch_size = 500, $reset = false)
{
$upload_dir = wp_get_upload_dir();
$batch = [];
$log_file = trailingslashit(WP_CONTENT_DIR) . 'debug.log';
if (!file_exists($log_file)) {
file_put_contents($log_file, "");
}
// Get all lines from log file.
$lines = preg_grep('/\[FP-SCAN\]/', file($log_file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) ?: []);
// Get already logged images
$logged = [];
foreach ($lines as $line) {
if (strpos($line, '[FP-SCAN]') !== false) {
$url = preg_replace('/^.*\[FP-SCAN\]\s*/', '', $line);
$logged[] = trim($url);
}
}
if(!is_array($logged)) {
$logged = [];
}
if($reset && empty($logged)) {
error_log("============= FP Image List - Start =============");
}
// Make a lookup for O(1) checks
$lookup = array_flip($logged);
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($upload_dir['basedir'], FilesystemIterator::SKIP_DOTS)
);
foreach($iterator as $file){
if($file->isFile() && in_array(strtolower($file->getExtension()), ['jpg', 'jpeg', 'png'], true)){
$link = str_replace($upload_dir['basedir'], $upload_dir['baseurl'], $file->getPathname());
// Skip already logged images
if (isset($lookup[$link])) {
continue;
}
$batch[] = '[FP-SCAN] ' . $link;
if (count($batch) >= $batch_size) {
break;
}
}
}
if(!empty($batch)){
$log_content = implode(PHP_EOL, $batch).PHP_EOL;
error_log($log_content);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment