Skip to content

Commit

Permalink
Merge pull request #1929 from benhuson/duplicate-product-images
Browse files Browse the repository at this point in the history
Add 'wpsc_duplicate_product_thumbnail' filter.
  • Loading branch information
JustinSainton committed Jun 26, 2015
2 parents 660d778 + e87b2a8 commit 133c6d2
Showing 1 changed file with 48 additions and 1 deletion.
49 changes: 48 additions & 1 deletion wpsc-admin/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -1411,6 +1411,9 @@ function wpsc_duplicate_product_process( $post, $new_parent_id = false ) {
// Finds children (Which includes product files AND product images), their meta values, and duplicates them.
wpsc_duplicate_children( $post->ID, $new_post_id );

// Copy product thumbnail (resetting duplicated meta value)
wpsc_duplicate_product_thumbnail( $post->ID, $new_post_id );

return $new_post_id;
}

Expand Down Expand Up @@ -1473,6 +1476,46 @@ function wpsc_duplicate_product_meta( $id, $new_id ) {

}

/**
* Duplicate Featured Image
*
* When a product is duplicated, the featured image ID is copied when the post
* meta is duplicated.
*
* When the featured image is attached to the duplicated product, if the image
* is duplicated the featured image ID is updated to the duplicated image ID
* otherwise the featured image ID is removed.
*
* If the featured image is not attached to the product the featured image ID
* remains the same as the original product.
*
* This function will remove the featured image if the image is not attached to
* the duplicated product and offers the opportunity to change the featured image
* of the duplicated product via the 'wpsc_duplicate_product_thumbnail' filter.
*
* @param integer $post_id Product ID.
* @param integer $new_post_id Duplicated product ID.
*/
function wpsc_duplicate_product_thumbnail( $post_id, $new_post_id ) {

$thumbnail_id = $original_thumbnail_id = has_post_thumbnail( $new_post_id ) ? get_post_thumbnail_id( $new_post_id ) : 0;

// If not duplicating product attachments, ensure featured image ID is zero
if ( ! apply_filters( 'wpsc_duplicate_product_attachment', true, get_post( $thumbnail_id ), $new_post_id ) ) {
$thumbnail_id = 0;
}

// Filter featured product image ID
$thumbnail_id = absint( apply_filters( 'wpsc_duplicate_product_thumbnail', $thumbnail_id, $original_thumbnail_id, $post_id, $new_post_id ) );

if ( $thumbnail_id > 0 ) {
set_post_thumbnail( $new_post_id, $thumbnail_id );
} else {
delete_post_thumbnail( $new_post_id );
}

}

/**
* Duplicates children product and children meta
*
Expand Down Expand Up @@ -1528,7 +1571,7 @@ function wpsc_duplicate_children( $old_parent_id, $new_parent_id ) {
*/
function wpsc_duplicate_product_image_process( $child_post, $new_parent_id ) {

if ( 'attachment' == get_post_type( $child_post ) && apply_filters( 'wpsc_duplicate_product_attachment', true, $child_post ) ) {
if ( 'attachment' == get_post_type( $child_post ) && apply_filters( 'wpsc_duplicate_product_attachment', true, $child_post, $new_parent_id ) ) {

$file = wp_get_attachment_url( $child_post->ID );

Expand Down Expand Up @@ -1583,6 +1626,10 @@ function wpsc_duplicate_product_image_process( $child_post, $new_parent_id ) {

}

} elseif ( has_post_thumbnail( $new_parent_id ) && $child_post->ID == get_post_thumbnail_id( $new_parent_id ) ) {

delete_post_meta( $new_parent_id, '_thumbnail_id' );

}

}
Expand Down

0 comments on commit 133c6d2

Please sign in to comment.