from pathlib import Path
src = Path("/mnt/data/Pasted text(20260907-153732).txt")
text = src.read_text(encoding="utf-8")
old_featured = """ private static function set_product_image_from_url($product_id, $image_url)
{
if (empty($image_url)) return false;
require_once ABSPATH . 'wp-admin/includes/media.php';
require_once ABSPATH . 'wp-admin/includes/file.php';
require_once ABSPATH . 'wp-admin/includes/image.php';
$attachment_id = self::with_filename_shortener(static function () use ($image_url, $product_id) {
return media_sideload_image($image_url, $product_id, null, 'id');
});
if (is_wp_error($attachment_id)) {
error_log(sprintf(
'Symphonya: media_sideload_image failed for product %d, URL "%s" — [%s] %s',
$product_id,
$image_url,
$attachment_id->get_error_code(),
$attachment_id->get_error_message()
));
return false;
}
set_post_thumbnail($product_id, $attachment_id);
return $attachment_id;
}
"""
new_featured = """ /**
* Find an existing Media Library attachment for a Symphonya image URL.
*
* WordPress stores the original sideload URL in _source_url. We also keep
* our own _symphonya_source_url marker for future lookups.
*
* @param string $image_url
* @return int Attachment ID, or 0 when no usable attachment exists.
*/
private static function find_existing_attachment_by_url($image_url)
{
$image_url = esc_url_raw((string) $image_url);
if ($image_url === '') {
return 0;
}
global $wpdb;
$attachment_id = (int) $wpdb->get_var(
$wpdb->prepare(
"
SELECT pm.post_id
FROM {$wpdb->postmeta} pm
INNER JOIN {$wpdb->posts} p
ON p.ID = pm.post_id
WHERE pm.meta_key IN ('_symphonya_source_url', '_source_url')
AND pm.meta_value = %s
AND p.post_type = 'attachment'
AND p.post_status != 'trash'
ORDER BY
CASE WHEN pm.meta_key = '_symphonya_source_url' THEN 0 ELSE 1 END,
p.ID ASC
LIMIT 1
",
$image_url
)
);
if (!$attachment_id) {
return 0;
}
$file = get_attached_file($attachment_id);
if (!$file || !file_exists($file)) {
return 0;
}
// Backfill our own marker on older attachments found via _source_url.
if (get_post_meta($attachment_id, '_symphonya_source_url', true) !== $image_url) {
update_post_meta($attachment_id, '_symphonya_source_url', $image_url);
}
return $attachment_id;
}
private static function set_product_image_from_url($product_id, $image_url)
{
$image_url = esc_url_raw((string) $image_url);
if ($image_url === '') {
return false;
}
// Reuse an attachment that WordPress/Symphonya already downloaded.
$existing_attachment_id = self::find_existing_attachment_by_url($image_url);
if ($existing_attachment_id) {
set_post_thumbnail($product_id, $existing_attachment_id);
return $existing_attachment_id;
}
require_once ABSPATH . 'wp-admin/includes/media.php';
require_once ABSPATH . 'wp-admin/includes/file.php';
require_once ABSPATH . 'wp-admin/includes/image.php';
$attachment_id = self::with_filename_shortener(static function () use ($image_url, $product_id) {
return media_sideload_image($image_url, $product_id, null, 'id');
});
if (is_wp_error($attachment_id)) {
error_log(sprintf(
'Symphonya: media_sideload_image failed for product %d, URL "%s" — [%s] %s',
$product_id,
$image_url,
$attachment_id->get_error_code(),
$attachment_id->get_error_message()
));
return false;
}
$attachment_id = (int) $attachment_id;
// Keep a Symphonya-specific source marker for fast, reliable reuse.
update_post_meta($attachment_id, '_symphonya_source_url', $image_url);
set_post_thumbnail($product_id, $attachment_id);
return $attachment_id;
}
"""
if old_featured not in text:
raise RuntimeError("Featured-image function not found exactly; refusing to create a partial patch.")
text = text.replace(old_featured, new_featured, 1)
old_gallery = """ $new_ids = self::with_filename_shortener(static function () use ($urls, $product_id) {
$ids = array();
foreach ($urls as $url) {
$url = esc_url_raw($url);
if (empty($url)) {
continue;
}
$attachment_id = media_sideload_image($url, $product_id, null, 'id');
if (is_wp_error($attachment_id)) {
error_log(sprintf(
'Symphonya: gallery sideload failed for product %d, URL "%s" — [%s] %s',
$product_id,
$url,
$attachment_id->get_error_code(),
$attachment_id->get_error_message()
));
continue;
}
$ids[] = intval($attachment_id);
}
return $ids;
});
"""
new_gallery = """ $new_ids = self::with_filename_shortener(static function () use ($urls, $product_id) {
$ids = array();
foreach ($urls as $url) {
$url = esc_url_raw($url);
if (empty($url)) {
continue;
}
// Reuse an attachment that already exists for this exact source URL.
$existing_attachment_id = self::find_existing_attachment_by_url($url);
if ($existing_attachment_id) {
$ids[] = intval($existing_attachment_id);
continue;
}
$attachment_id = media_sideload_image($url, $product_id, null, 'id');
if (is_wp_error($attachment_id)) {
error_log(sprintf(
'Symphonya: gallery sideload failed for product %d, URL "%s" — [%s] %s',
$product_id,
$url,
$attachment_id->get_error_code(),
$attachment_id->get_error_message()
));
continue;
}
$attachment_id = intval($attachment_id);
// Keep a Symphonya-specific source marker for future reuse.
update_post_meta($attachment_id, '_symphonya_source_url', $url);
$ids[] = $attachment_id;
}
return $ids;
});
"""
if old_gallery not in text:
raise RuntimeError("Gallery sideload block not found exactly; refusing to create a partial patch.")
text = text.replace(old_gallery, new_gallery, 1)
out = Path("/mnt/data/index-symphonya-afbeeldingen-fix.php")
out.write_text(text, encoding="utf-8")
print(f"Bestand gemaakt: {out}")
print("Aanpassingen:")
print("- bestaande attachment wordt hergebruikt op basis van _source_url of _symphonya_source_url")
print("- featured image wordt niet opnieuw gedownload als die al bestaat")
print("- gallery-afbeeldingen worden niet opnieuw gedownload als die al bestaan")
print("- nieuw gedownloade Symphonya-afbeeldingen krijgen _symphonya_source_url als marker")
Warning: Cannot modify header information - headers already sent by (output started at /home/mvghaars/public_html/wp-content/plugins/symphonya/src/modules/products/index.php:1) in /home/mvghaars/public_html/wp-includes/pluggable.php on line 1539
Warning: Cannot modify header information - headers already sent by (output started at /home/mvghaars/public_html/wp-content/plugins/symphonya/src/modules/products/index.php:1) in /home/mvghaars/public_html/wp-includes/pluggable.php on line 1542