Puslapis nerastas
The link you followed may be broken, or the page may have been removed.
Batch {$paged} of {$total_pages} ({$per_page} posts per batch, {$total_posts} total posts)
"; echo ""; if(empty($posts)){ echo "No posts found in this batch.
No images.
"; continue; } foreach($m[1] as $url){ $path = parse_url($url, PHP_URL_PATH); if(strpos($path,'/uploads/')===false) continue; $file = basename($path); // year/month if(preg_match('#/uploads/(\d{4})/(\d{2})/#',$path,$d)){ list(,$year,$month) = $d; $orig_filename = $file; $orig_folder = "{$year}/{$month}/"; } elseif(preg_match('#/uploads/(\d{4})-(\d{2})-#',$path,$d2)){ list(,$year,$month) = $d2; $orig_filename = preg_replace('/^(?:'.$year.'-'.$month.'-)+/', '', $file); $orig_folder = "{$year}/{$month}/"; } else { continue; } $base = pathinfo($file, PATHINFO_FILENAME); $ext = pathinfo($file, PATHINFO_EXTENSION); $prefix = "{$year}-{$month}-"; $pattern = '/^(?:' . preg_quote($prefix,'/') . '){2,}/'; $base = preg_replace($pattern, $prefix, $base); if(strpos($base, $prefix) === 0){ $search = $base; } else { $search = $prefix . $base; } $like = "%/uploads/{$year}/{$month}/{$search}%." . $ext; $att = $wpdb->get_var($wpdb->prepare(" SELECT ID FROM {$wpdb->posts} WHERE post_type='attachment' AND post_mime_type LIKE 'image%%' AND guid LIKE %s LIMIT 1 ", $like )); if($att && ($fp = get_attached_file($att)) && file_exists($fp)){ $real = basename($fp); $new = "/wp-content/uploads/{$real}"; $upd = str_replace($url,$new,$upd); $filename_log[] = [ 'old' => $file, 'new' => $real, 'status' => 'found' ]; continue; } // Check if file is in uploads (not in media library) $upload_dir = wp_upload_dir(); $new_filename = $search . '.' . $ext; $new_path = trailingslashit($upload_dir['path']) . $new_filename; if(file_exists($new_path)){ $filename_log[] = [ 'old' => $file, 'new' => $new_filename, 'status' => 'file_exists_but_not_registered' ]; // Optionally: register as attachment here. continue; } // Try to download from backup $download_url = $backup_base . $orig_folder . $orig_filename; require_once(ABSPATH . 'wp-admin/includes/file.php'); require_once(ABSPATH . 'wp-admin/includes/media.php'); require_once(ABSPATH . 'wp-admin/includes/image.php'); $tmp = download_url($download_url, 10); if( is_wp_error($tmp) ) { $filename_log[] = [ 'old' => $file, 'new' => '', 'status' => 'download_failed' ]; continue; } if(!@rename($tmp, $new_path)){ @unlink($tmp); $filename_log[] = [ 'old' => $file, 'new' => '', 'status' => 'move_failed' ]; continue; } $file_array = [ 'name' => $new_filename, 'tmp_name' => $new_path ]; $attach_id = media_handle_sideload($file_array, $post->ID); if(is_wp_error($attach_id)){ @unlink($new_path); $filename_log[] = [ 'old' => $file, 'new' => '', 'status' => 'media_failed' ]; continue; } $real = basename(get_attached_file($attach_id)); $new = "/wp-content/uploads/{$real}"; $upd = str_replace($url, $new, $upd); $filename_log[] = [ 'old' => $file, 'new' => $real, 'status' => 'downloaded' ]; } if($upd !== $orig){ wp_update_post(['ID'=>$post->ID,'post_content'=>$upd]); echo " Updated post.
No changes.
Old | New | Status |
---|---|---|
{$row['old']} | {$row['new']} | {$row['status']} |