prefix . 'fbv'; $fbv_att = $wpdb->prefix . 'fbv_attachment_folder'; foreach ($subfolders as $sub) { // Create or get subfolder $exists = $wpdb->get_var($wpdb->prepare("SELECT id FROM $fbv WHERE name = %s AND parent = %d", $sub, $parent_id)); if (!$exists) { $wpdb->query($wpdb->prepare("INSERT INTO $fbv (name, parent, type) VALUES (%s, %d, 0)", $sub, $parent_id)); $folder_id = $wpdb->insert_id; echo "Created: $sub (ID $folder_id)\n"; } else { $folder_id = $exists; echo "Exists: $sub (ID $folder_id)\n"; } $dir = $base_path . '/' . $sub; if (!is_dir($dir)) continue; $files = scandir($dir); $count = 0; foreach ($files as $f) { if ($f === '.' || $f === '..') continue; $filepath = $dir . '/' . $f; if (is_dir($filepath)) continue; // Check if exists $guid_search = '%libros/' . $sub . '/' . $f; $att_id = $wpdb->get_var($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE guid LIKE %s AND post_type = 'attachment'", $guid_search)); if (!$att_id) { $cmd = "wp media import " . escapeshellarg($filepath) . " --porcelain --allow-root"; $out = shell_exec($cmd); $att_id = trim($out); if (is_numeric($att_id)) { $wpdb->query("INSERT INTO $fbv_att (folder_id, attachment_id) VALUES ($folder_id, $att_id)"); $count++; } } } echo "Imported $count files to libros/$sub\n"; } echo "DONE\n";