/*
 Theme Name:   Woodmart Child
 Description:  Woodmart Child Theme
 Author:       XTemos
 Author URI:   http://xtemos.com
 Template:     woodmart
 Version:      1.0.0
 Text Domain:  woodmart
*/



add_filter( 'wp_delete_post', 'delete_product_images_reliably', 10, 1 );

function delete_product_images_reliably( $post_id ) {

    if ( get_post_type( $post_id ) !== 'product' ) {
        return $post_id;
    }

    // Captura direto do banco (antes do WooCommerce limpar)
    $featured_image_id = get_post_meta( $post_id, '_thumbnail_id', true );
    $gallery_image_ids = get_post_meta( $post_id, '_product_image_gallery', true );

    if ( $featured_image_id ) {
        wp_delete_attachment( (int) $featured_image_id, true );
    }

    if ( ! empty( $gallery_image_ids ) ) {
        $ids = array_map( 'intval', explode( ',', $gallery_image_ids ) );
        foreach ( $ids as $image_id ) {
            wp_delete_attachment( $image_id, true );
        }
    }

    return $post_id;
}
