|
<?php |
|
|
|
$updatedImageIds = array(); |
|
$productsUpdated = array(); |
|
|
|
////////////////////////// |
|
// Update Images where |
|
// post is assigned |
|
////////////////////////// |
|
$parent_images_args = array( |
|
'post_type' => 'attachment', |
|
'post_mime_type' => 'image', |
|
'post_status' => 'inherit', |
|
'posts_per_page' => -1, |
|
); |
|
|
|
$parent_images = get_posts( $parent_images_args ); |
|
foreach ($parent_images as $parent_image) { |
|
|
|
if(!isset($parent_image->post_parent) || empty($parent_image->post_parent)) { |
|
continue; |
|
} |
|
$parent_id = $parent_image->post_parent; |
|
$productsUpdated[] = $parent_id; |
|
$parent_title = get_the_title($parent_id); |
|
|
|
$image_id = $parent_image->ID; |
|
|
|
$updatedImageIds[] = $image_id; |
|
update_post_meta($image_id, '_wp_attachment_image_alt', $parent_title); |
|
} |
|
|
|
////////////////////////// |
|
// Update Images by Looping |
|
// through products |
|
////////////////////////// |
|
$products_query = array( |
|
'post_type' => 'product', |
|
'posts_per_page' => -1, |
|
'post__not_in' => $productsUpdated |
|
|
|
); |
|
$products = get_posts($products_query); |
|
|
|
foreach ($products as $product) { |
|
$product_name = str_replace(array('..', '...'), '', $product->post_title); |
|
$product_image_id = get_post_thumbnail_id($product->ID); |
|
|
|
if(!empty($product_image_id) && !in_array($product_image_id, $updatedImageIds)) { |
|
$updatedImageIds[] = $product_image_id; |
|
update_post_meta($product_image_id, '_wp_attachment_image_alt', $product_name); |
|
} |
|
|
|
$product_gallery_images = get_post_meta($product_image_id, '_product_image_gallery', true); |
|
if($product_gallery_images) { |
|
|
|
$product_gallery_images = explode(',', $product_gallery_images); |
|
$i = 1; |
|
foreach ($product_gallery_images as $product_gallery_image) { |
|
|
|
if(in_array($product_gallery_image, $updatedImageIds)) { |
|
continue; |
|
} |
|
|
|
$updatedImageIds[] = $product_gallery_image; |
|
update_post_meta($product_gallery_image, '_wp_attachment_image_alt', $product_name . ' – Detailbild ' . $i); |
|
$i++; |
|
|
|
} |
|
} |
|
} |
|
|
|
////////////////////////// |
|
// Update restly images |
|
////////////////////////// |
|
$other_images_args = array( |
|
'post_type' => 'attachment', |
|
'post_mime_type' => 'image', |
|
'post_status' => 'inherit', |
|
'posts_per_page' => -1, |
|
'post__not_in' => $updatedImageIds, |
|
); |
|
$other_images = get_posts( $other_images_args ); |
|
foreach ($other_images as $other_image) { |
|
|
|
$other_image_id = $other_image->ID; |
|
$other_image_title = get_the_title($other_image_id); |
|
$new_image_alt = str_replace(array('-', '.'), ' ', $other_image_title); |
|
|
|
update_post_meta($other_image_id, '_wp_attachment_image_alt', $new_image_alt); |
|
} |