Revised, roduct gallery panel, images stored in meta = more flexibility.

This commit is contained in:
Mike Jolley 2012-12-20 01:13:06 +00:00
parent 7bd81503b5
commit c8852bcf40
9 changed files with 147 additions and 635 deletions

View File

@ -651,6 +651,10 @@ function woocommerce_process_product_meta( $post_id, $post ) {
// Set transient for product type
set_transient( 'wc_product_type_' . $post_id, $product_type );
// Gallery Images
$attachment_ids = array_filter( explode( ',', woocommerce_clean( $_POST['product_image_gallery'] ) ) );
update_post_meta( $post_id, '_product_image_gallery', implode( ',', $attachment_ids ) );
// Update post meta
update_post_meta( $post_id, '_regular_price', stripslashes( $_POST['_regular_price'] ) );
update_post_meta( $post_id, '_sale_price', stripslashes( $_POST['_sale_price'] ) );

View File

@ -24,259 +24,138 @@ function woocommerce_product_images_box() {
<div id="product_images_container">
<ul class="product_images">
<?php
$thumbnail_id = get_post_thumbnail_id( $post->ID );
if ( $thumbnail_id )
echo '<li class="image" data-post_id="' . $thumbnail_id . '">
' . wp_get_attachment_image( $thumbnail_id, 'full' ) . '
<span class="loading"></span>
<ul class="actions">
<li><a href="#" class="delete" title="' . __( 'Delete image', 'woocommerce' ) . '">' . __( 'Delete', 'woocommerce' ) . '</a></li>
<li><a href="' . admin_url( 'media-upload.php?post_id=' . $post->ID . '&attachment_id=' . $thumbnail_id . '&tab=library&width=640&height=553&TB_iframe=1' ) . '" class="view thickbox" onclick="return false;" title="' . __( 'Edit image', 'woocommerce' ) . '">' . __( 'View', 'woocommerce' ) . '</a></li>
</ul>
</li>';
$attachments =& get_children( 'post_parent=' . $post->ID . '&numberposts=-1&post_type=attachment&orderby=menu_order&order=ASC&post_mime_type=image' );
foreach ( $attachments as $attachment_id => $attachment ) {
if ( $thumbnail_id == $attachment_id )
continue;
$exclude_class = get_post_meta( $attachment_id, '_woocommerce_exclude_image', true ) == 1 ? 'excluded' : '';
echo '<li class="image ' . $exclude_class . '" data-post_id="' . $attachment_id . '">
' . wp_get_attachment_image( $attachment_id, 'full' ) . '
<span class="loading"></span>
<ul class="actions">
<li><a href="#" class="delete" title="' . __( 'Delete image', 'woocommerce' ) . '">' . __( 'Delete', 'woocommerce' ) . '</a></li>
<li><a href="' . admin_url( 'media-upload.php?post_id=' . $post->ID . '&attachment_id=' . $attachment_id . '&tab=library&width=640&height=553&TB_iframe=1' ) . '" class="view thickbox" onclick="return false;" title="' . __( 'Edit image', 'woocommerce' ) . '">' . __( 'View', 'woocommerce' ) . '</a></li>
</ul>
</li>';
if ( metadata_exists( 'post', $post->ID, '_product_image_gallery' ) ) {
$product_image_gallery = get_post_meta( $post->ID, '_product_image_gallery', true );
} else {
// Backwards compat
$attachment_ids = array_filter( array_diff( get_posts( 'post_parent=' . $post->ID . '&numberposts=-1&post_type=attachment&orderby=menu_order&order=ASC&post_mime_type=image&fields=ids' ), array( get_post_thumbnail_id() ) ) );
$product_image_gallery = implode( ',', $attachment_ids );
}
$attachments = array_filter( explode( ',', $product_image_gallery ) );
if ( $attachments )
foreach ( $attachments as $attachment_id ) {
echo '<li class="image" data-attachment_id="' . $attachment_id . '">
' . wp_get_attachment_image( $attachment_id, 'full' ) . '
<ul class="actions">
<li><a href="#" class="delete" title="' . __( 'Delete image', 'woocommerce' ) . '">' . __( 'Delete', 'woocommerce' ) . '</a></li>
</ul>
</li>';
}
?>
</ul>
</div>
<!-- Uploader section -->
<div id="plupload-upload-ui" class="hide-if-no-js">
<div id="drag-drop-area">
<p class="drag-drop-info"><?php _e('Drop files here'); ?></p>
<p><?php _ex('or', 'Uploader: Drop files here - or - Select Files'); ?></p>
<p class="drag-drop-buttons"><input id="plupload-browse-button" type="button" value="<?php esc_attr_e('Select Files'); ?>" class="button" /></p>
</div>
</div>
<?php
// Drag and drop code adapted from Drag & Drop Featured Image by Jonathan Lundström
$plupload_init = array(
'runtimes' => 'html5,silverlight,flash,html4',
'browse_button' => 'plupload-browse-button',
'container' => 'plupload-upload-ui',
'drop_element' => 'drag-drop-area',
'file_data_name' => 'async-upload',
'multiple_queues' => true,
'max_file_size' => wp_max_upload_size() . 'b',
'url' => admin_url('admin-ajax.php'),
'flash_swf_url' => includes_url('js/plupload/plupload.flash.swf'),
'silverlight_xap_url' => includes_url('js/plupload/plupload.silverlight.xap'),
'filters' => array( array( 'title' => __( 'Allowed Files' ), 'extensions' => '*') ),
'multipart' => true,
'urlstream_upload' => true,
'multipart_params' => array(
'_ajax_nonce' => wp_create_nonce( 'product-images-box-upload' ),
'action' => 'woocommerce_product_images_box_upload',
'post_id' => $post->ID
)
);
<input type="hidden" id="product_image_gallery" name="product_image_gallery" value="<?php echo esc_attr( $product_image_gallery ); ?>" />
// Apply filters to initiate plupload:
$plupload_init = apply_filters( 'plupload_init', $plupload_init );
?>
</div>
<p class="add_product_images hide-if-no-js">
<a href="#"><?php _e( 'Add product gallery images', 'woocommerce' ); ?></a>
</p>
<script type="text/javascript">
jQuery(document).ready(function($){
function product_images_container_init() {
// Attribute ordering
$('#product_images_container ul.product_images').sortable({
items: 'li.image',
cursor: 'move',
scrollSensitivity:40,
forcePlaceholderSize: true,
forceHelperSize: false,
helper: 'clone',
opacity: 0.65,
placeholder: 'wc-metabox-sortable-placeholder',
start:function(event,ui){
ui.item.css('background-color','#f6f6f6');
},
stop:function(event,ui){
ui.item.removeAttr('style');
},
update: function(event, ui) {
$('#product_images_container ul li.image').css('cursor','default');
$('#product_images_container ul.product_images').sortable('disable');
// Uploading files
var product_gallery_frame;
var $image_gallery_ids = $('#product_image_gallery');
var $product_images = $('#product_images_container ul.product_images');
var post_id = <?php echo $post->ID; ?>;
var attachment_id = ui.item.attr( 'data-post_id' );
var prev_attachment_id = ui.item.prev().attr( 'data-post_id' );
var next_attachment_id = ui.item.next().attr( 'data-post_id' );
jQuery('.add_product_images a').live('click', function( event ){
// show spinner
ui.item.addClass('loading');
var $el = $(this);
var attachment_ids = $image_gallery_ids.val();
// go do the sorting stuff via ajax
jQuery.post( ajaxurl, {
action: 'woocommerce_product_image_ordering',
post_id: post_id,
attachment_id: attachment_id,
prev_attachment_id: prev_attachment_id,
next_attachment_id: next_attachment_id,
_ajax_nonce: '<?php echo wp_create_nonce( 'product-image-ordering' ); ?>'
}, function( response ) {
ui.item.removeClass('loading');
$('#product_images_container ul li.image').css('cursor','move');
$('#product_images_container ul.product_images').sortable('enable');
}
);
}
});
}
event.preventDefault();
product_images_container_init();
// Delete images
$('#product_images_container').on( 'click', 'a.delete', function() {
$image = $(this).closest('li.image');
var attachment_id = $image.attr('data-post_id');
if ( attachment_id ) {
$image.addClass('loading');
var answer = confirm('<?php _e( 'Are you sure you want to remove this attachment?', 'woocommerce' ); ?>');
if ( answer ) {
jQuery.post( ajaxurl, {
action: 'woocommerce_product_image_delete',
post_id: <?php echo $post->ID; ?>,
attachment_id: attachment_id,
_ajax_nonce: '<?php echo wp_create_nonce( 'product-image-delete' ); ?>'
}, function( response ) {
$image.remove();
}
);
} else {
$image.removeClass('loading');
}
}
return false;
} );
// Drag and drop uploading of images
var uploader = new plupload.Uploader(<?php echo json_encode( $plupload_init ); ?>);
// Check for drag'n'drop functionality:
uploader.bind('Init', function(up){
var uploaddiv = $('#plupload-upload-ui');
// Add classes and bind actions:
if(up.features.dragdrop){
uploaddiv.addClass('drag-drop');
$('#drag-drop-area')
.bind('dragover.wp-uploader', function() { uploaddiv.addClass('drag-over'); })
.bind('dragleave.wp-uploader, drop.wp-uploader', function(){ uploaddiv.removeClass('drag-over'); });
} else{
uploaddiv.removeClass('drag-drop');
$('#drag-drop-area').unbind('.wp-uploader');
}
});
// Initiate uploading script:
uploader.init();
// File queue handler
uploader.bind('FilesAdded', function(up, files){
var hundredmb = 100 * 1024 * 1024, max = parseInt(up.settings.max_file_size, 10);
// Loop through files:
plupload.each(files, function(file){
if ( max > hundredmb && file.size > hundredmb && up.runtime != 'html5' ) {
alert( "<?php _e( 'The file you selected exceeds the maximum filesize specified in this installation.', 'woocommerce' ); ?>" );
}
});
// Refresh and start:
up.refresh();
up.start();
// Block the UI
$('#product_images_container').block({ message: null, overlayCSS: { background: '#fff url(' + woocommerce_writepanel_params.plugin_url + '/assets/images/ajax-loader.gif) no-repeat center', opacity: 0.6 } });
});
// Handle new uploads
uploader.bind( 'FileUploaded', function( up, file, response ) {
response = $.parseJSON( response.response );
if ( response.error ) {
alert( response.error );
} else {
$('#product_images_container ul').append('<li class="image" data-post_id="' + response.post_id + '">\
<img src="' + response.src + '" />\
<span class="loading"></span>\
<ul class="actions">\
<li><a href="#" class="delete" title="<?php _e( 'Delete image', 'woocommerce' ); ?>"><?php _e( 'Delete', 'woocommerce' ) ?></a></li>\
<li><a href="' + response.edit_url + '" class="view thickbox" onclick="return false;" title="<?php _e( 'Edit image', 'woocommerce' ); ?>"><?php _e( 'View', 'woocommerce' ) ?></a></li>\
</ul>\
</li>');
}
$('#product_images_container').unblock();
});
// Refresh images when a thickbox (images) closes
var loading_product_images = false;
jQuery(document).bind( 'tb_unload', function() {
if ( loading_product_images )
// If the media frame already exists, reopen it.
if ( product_gallery_frame ) {
product_gallery_frame.open();
return;
}
loading_product_images = true;
// Create the media frame.
product_gallery_frame = wp.media.frames.downloadable_file = wp.media({
// Set the title of the modal.
title: '<?php _e( 'Add Images to Product Gallery', 'woocommerce' ); ?>',
button: {
text: '<?php _e( 'Add to gallery', 'woocommerce' ); ?>',
},
multiple: true
});
// Block
$('#product_images_container').block({ message: null, overlayCSS: { background: '#fff url(' + woocommerce_writepanel_params.plugin_url + '/assets/images/ajax-loader.gif) no-repeat center', opacity: 0.6 } });
// When an image is selected, run a callback.
product_gallery_frame.on( 'select', function() {
// Re-load images
jQuery.post( ajaxurl, {
action: 'woocommerce_product_image_refresh',
post_id: <?php echo $post->ID; ?>,
_ajax_nonce: '<?php echo wp_create_nonce( 'product-image-refresh' ); ?>'
}, function( response ) {
if ( response ) {
$("#product_images_container").html(response);
var selection = product_gallery_frame.state().get('selection');
selection.map( function( attachment ) {
attachment = attachment.toJSON();
if ( attachment.id ) {
attachment_ids = attachment_ids ? attachment_ids + "," + attachment.id : attachment.id;
$product_images.append('\
<li class="image" data-attachment_id="' + attachment.id + '">\
<img src="' + attachment.url + '" />\
<ul class="actions">\
<li><a href="#" class="delete" title="<?php _e( 'Delete image', 'woocommerce' ); ?>"><?php _e( 'Delete', 'woocommerce' ); ?></a></li>\
</ul>\
</li>');
}
// Re-init
product_images_container_init();
} );
// Unblock
$('#product_images_container').unblock();
$image_gallery_ids.val( attachment_ids );
});
loading_product_images = false;
}
);
// Finally, open the modal.
product_gallery_frame.open();
});
// Image ordering
$product_images.sortable({
items: 'li.image',
cursor: 'move',
scrollSensitivity:40,
forcePlaceholderSize: true,
forceHelperSize: false,
helper: 'clone',
opacity: 0.65,
placeholder: 'wc-metabox-sortable-placeholder',
start:function(event,ui){
ui.item.css('background-color','#f6f6f6');
},
stop:function(event,ui){
ui.item.removeAttr('style');
},
update: function(event, ui) {
var attachment_ids = '';
$('#product_images_container ul li.image').css('cursor','default').each(function() {
var attachment_id = jQuery(this).attr( 'data-attachment_id' );
attachment_ids = attachment_ids + attachment_id + ',';
});
$image_gallery_ids.val( attachment_ids );
}
});
// Remove images
$('#product_images_container').on( 'click', 'a.delete', function() {
$(this).closest('li.image').remove();
var attachment_ids = '';
$('#product_images_container ul li.image').css('cursor','default').each(function() {
var attachment_id = jQuery(this).attr( 'data-attachment_id' );
attachment_ids = attachment_ids + attachment_id + ',';
});
$image_gallery_ids.val( attachment_ids );
return false;
} );
});

View File

@ -44,11 +44,10 @@ function woocommerce_meta_boxes() {
// Products
add_meta_box( 'woocommerce-product-data', __( 'Product Data', 'woocommerce' ), 'woocommerce_product_data_box', 'product', 'normal', 'high' );
//add_meta_box( 'woocommerce-product-images', __( 'Product Images', 'woocommerce' ), 'woocommerce_product_images_box', 'product', 'side' );
add_meta_box( 'woocommerce-product-images', __( 'Product Gallery', 'woocommerce' ), 'woocommerce_product_images_box', 'product', 'side' );
remove_meta_box( 'product_shipping_classdiv', 'product', 'side' );
remove_meta_box( 'pageparentdiv', 'product', 'side' );
remove_meta_box( 'postimagediv', 'product', 'side' );
// Excerpt
if ( function_exists('wp_editor') ) {

View File

@ -577,79 +577,6 @@ function woocommerce_admin_head() {
add_action('admin_head', 'woocommerce_admin_head');
/**
* Add functionality to the image uploader on product pages to exclude an image
*
* @access public
* @param mixed $fields
* @param mixed $object
* @return void
*/
function woocommerce_exclude_image_from_product_page_field( $fields, $object ) {
if (!$object->post_parent) return $fields;
$parent = get_post( $object->post_parent );
if ( $parent->post_type !== 'product' )
return $fields;
$exclude_image = get_post_meta( absint( $object->ID ), '_woocommerce_exclude_image', true );
$label = __( 'Exclude image', 'woocommerce' );
$html = '<input type="checkbox" '.checked($exclude_image, 1, false).' name="attachments['.$object->ID.'][woocommerce_exclude_image]" id="attachments['.$object->ID.'][woocommerce_exclude_image" />';
$fields['woocommerce_exclude_image'] = array(
'label' => $label,
'input' => 'html',
'html' => $html,
'value' => '',
'helps' => __( 'Enabling this option will hide it from the product page image gallery.', 'woocommerce' )
);
return $fields;
}
add_filter('attachment_fields_to_edit', 'woocommerce_exclude_image_from_product_page_field', 1, 2);
add_filter('attachment_fields_to_save', 'woocommerce_exclude_image_from_product_page_field_save', 1, 2);
add_action('add_attachment', 'woocommerce_exclude_image_from_product_page_field_add');
/**
* Save the meta for exlcuding images from galleries.
*
* @access public
* @param mixed $post
* @param mixed $attachment
* @return void
*/
function woocommerce_exclude_image_from_product_page_field_save( $post, $attachment ) {
if (isset($_REQUEST['attachments'][$post['ID']]['woocommerce_exclude_image'])) :
delete_post_meta( (int) $post['ID'], '_woocommerce_exclude_image' );
update_post_meta( (int) $post['ID'], '_woocommerce_exclude_image', 1);
else :
delete_post_meta( (int) $post['ID'], '_woocommerce_exclude_image' );
update_post_meta( (int) $post['ID'], '_woocommerce_exclude_image', 0);
endif;
return $post;
}
/**
* Add the meta for exlcuding images from galleries.
*
* @access public
* @param mixed $post_id
* @return void
*/
function woocommerce_exclude_image_from_product_page_field_add( $post_id ) {
add_post_meta( $post_id, '_woocommerce_exclude_image', 0, true );
}
/**
* Duplicate a product action
*

File diff suppressed because one or more lines are too long

View File

@ -1611,8 +1611,11 @@ img.help_tip {
.inside {
margin: 0;
padding: 0;
.add_product_images {
padding: 0 9px;
}
#product_images_container {
padding: 9px 0 0 9px;
padding: 0 0 0 9px;
ul {
overflow: hidden;
margin: 0;
@ -1624,7 +1627,7 @@ img.help_tip {
float: left;
cursor: move;
border: 1px solid #d5d5d5;
margin: 0 9px 9px 0;
margin: 9px 9px 0 0;
background: #f7f7f7;
-webkit-border-radius: 2px;
-moz-border-radius: 2px;
@ -1638,41 +1641,11 @@ img.help_tip {
height: auto;
display: block;
}
.loading {
background: #fff url(../images/ajax-loader.gif) no-repeat center;
width: 100%;
height: 100%;
opacity: 0.5;
position: absolute;
display: none;
top:0;
left: 0;
}
}
li.excluded {
opacity: 0.2;
}
li.loading {
.loading {
display: block;
}
}
li.image:first-child {
margin: 0 0 9px 0;
width: 258px;
height: auto;
overflow: visible;
opacity: 1;
}
li.wc-metabox-sortable-placeholder {
border: 4px dashed #dddddd;
background: #f7f7f7 url(../images/image_watermark.png) no-repeat center;
}
li.wc-metabox-sortable-placeholder:first-child {
background: #f7f7f7 url(../images/star_watermark.png) no-repeat center;
width: 258px;
height: 258px;
}
ul.actions {
position: absolute;
top:0;
@ -1709,36 +1682,6 @@ img.help_tip {
}
}
}
#plupload-upload-ui {
text-align: center;
color: #999;
background: none;
cursor: default !important;
border: 0;
width: 258px;
height: auto;
margin: 0;
padding: 0 0 9px 9px;
#drag-drop-area {
padding: 20px 0;
height: auto;
-webkit-border-radius: 2px;
-moz-border-radius: 2px;
border-radius: 2px;
-webkit-box-sizing: border-box; /* Safari/Chrome, other WebKit */
-moz-box-sizing: border-box; /* Firefox, other Gecko */
box-sizing: border-box; /* Opera/IE 8+ */
line-height: 1.4em;
p {
color: #AAA;
font-size: 14px;
margin: 5px 0;
}
p.drag-drop-info {
font-size: 20px;
}
}
}
}
}
#woocommerce-product-data {
@ -2696,20 +2639,6 @@ table.bar_chart {
background-image: url(../images/product_types-x2.png) !important;
background-size:96px 16px !important;
}
#woocommerce-product-images {
.inside {
#product_images_container {
ul {
li.image, li.add, li.wc-metabox-sortable-placeholder {
.loading {
background-image: url(../images/ajax-loader@2x.gif);
background-size: 16px 16px;
}
}
}
}
}
}
#woocommerce-product-data ul.product_data_tabs li {
&.general_options, &.tax_options, &.inventory_options, &.shipping_options, &.linked_product_options, &.attribute_options, &.variation_options, &.advanced_options {
a {

View File

@ -85,6 +85,22 @@ abstract class WC_Product {
return $value;
}
/**
* get_images function.
*
* @access public
* @return array
*/
function get_images() {
if ( ! isset( $this->product_image_gallery ) ) {
// Backwards compat
$attachment_ids = array_diff( get_posts( 'post_parent=' . $this->id . '&numberposts=-1&post_type=attachment&orderby=menu_order&order=ASC&post_mime_type=image&fields=ids' ), array( get_post_thumbnail_id() ) );
$this->product_image_gallery = implode( ',', $attachment_ids );
}
return (array) explode( ',', $this->product_image_gallery );
}
/**
* Get SKU (Stock-keeping unit) - product unique ID.
*

View File

@ -9,28 +9,20 @@
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
global $post, $woocommerce;
global $post, $product, $woocommerce;
?>
<div class="thumbnails"><?php
$attachments = get_posts( array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => null,
'post_parent' => $post->ID,
'post__not_in' => array( get_post_thumbnail_id() ),
'post_mime_type'=> 'image',
'orderby' => 'menu_order',
'order' => 'ASC'
) );
if ($attachments) {
$attachment_ids = $product->get_images();
if ( $attachment_ids ) {
$loop = 0;
$columns = apply_filters( 'woocommerce_product_thumbnails_columns', 3 );
foreach ( $attachments as $key => $attachment ) {
foreach ( $attachment_ids as $id ) {
if ( get_post_meta( $attachment->ID, '_woocommerce_exclude_image', true ) == 1 )
continue;
$attachment = get_post( $id );
$classes = array( 'zoom' );
@ -43,7 +35,6 @@ global $post, $woocommerce;
printf( '<a href="%s" title="%s" rel="thumbnails" class="%s">%s</a>', wp_get_attachment_url( $attachment->ID ), esc_attr( $attachment->post_title ), implode(' ', $classes), wp_get_attachment_image( $attachment->ID, apply_filters( 'single_product_small_thumbnail_size', 'shop_thumbnail' ) ) );
$loop++;
}
}

View File

@ -1852,237 +1852,4 @@ function woocommerce_product_ordering() {
die( json_encode( $new_pos ) );
}
add_action( 'wp_ajax_woocommerce_product_ordering', 'woocommerce_product_ordering' );
/**
* woocommerce_product_images_box_upload function.
*
* @access public
* @return void
*/
function woocommerce_product_images_box_upload() {
check_ajax_referer( 'product-images-box-upload' );
// Get posted data
$file = $_FILES['async-upload'];
$post_id = absint( $_POST['post_id'] );
// Get size
$attachments =& get_children( 'post_parent=' . $post_id . '&numberposts=-1&post_type=attachment&post_mime_type=image' );
$gallery_size = absint( sizeof( $attachments ) );
// Upload
$upload = wp_handle_upload( $file, array( 'test_form' => false ) );
if( ! isset( $upload['error'] ) && isset( $upload['file'] ) ) {
// Create attachment
require_once( ABSPATH . 'wp-admin/includes/image.php' );
$attachment_id = wp_insert_attachment( array(
'post_mime_type' => $upload['type'],
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $upload['file'] ) ),
'post_content' => '',
'post_status' => 'inherit',
'menu_order' => ( $gallery_size + 1 )
), $upload['file'], $post_id );
$attachment_data = wp_generate_attachment_metadata( $attachment_id, $upload['file'] );
wp_update_attachment_metadata( $attachment_id, $attachment_data );
// Set featured image
$post_thumb_id = get_post_meta( $post_id, '_thumbnail_id', true );
if ( empty( $post_thumb_id ) )
update_post_meta( $post_id, '_thumbnail_id', $attachment_id );
// Return the result
$image = wp_get_attachment_image_src( $attachment_id, 'full' );
$result = array(
'src' => $image[0],
'post_id' => $attachment_id,
'edit_url' => admin_url( 'media-upload.php?post_id=' . $post_id . '&attachment_id=' . $attachment_id . '&tab=library&width=640&height=553&TB_iframe=1' )
);
} else {
$result = array(
'error' => $upload['error']
);
}
echo json_encode( $result );
die();
}
add_action( 'wp_ajax_woocommerce_product_images_box_upload', 'woocommerce_product_images_box_upload' );
/**
* woocommerce_product_image_ordering function.
*
* @access public
* @return void
*/
function woocommerce_product_image_ordering() {
check_ajax_referer( 'product-image-ordering' );
$post_id = isset( $_POST['post_id'] ) ? absint( $_POST['post_id'] ) : false;
$attachment_id = isset( $_POST['attachment_id'] ) ? absint( $_POST['attachment_id'] ) : false;
$prev_attachment_id = isset( $_POST['prev_attachment_id'] ) ? absint( $_POST['prev_attachment_id'] ) : false;
$next_attachment_id = isset( $_POST['next_attachment_id'] ) ? absint( $_POST['next_attachment_id'] ) : false;
if ( ! $post_id || ! $attachment_id )
die( -1 );
$siblings = get_children( 'post_parent=' . $post_id . '&numberposts=-1&post_type=attachment&orderby=menu_order&order=ASC&post_mime_type=image' );
$new_positions = array(); // store new positions for ajax
$menu_order = 1;
foreach( $siblings as $sibling_id => $sibling ) {
if ( $sibling_id == $attachment_id )
continue;
// if this is the post that comes after our repositioned post, set our repositioned post position and increment menu order
if ( $next_attachment_id && $next_attachment_id == $sibling_id ) {
$attachment = array();
$attachment['ID'] = $attachment_id;
$attachment['menu_order'] = $menu_order;
wp_update_post( $attachment );
$new_positions[ $attachment_id ] = $menu_order;
$menu_order++;
}
// if repositioned post has been set, and new items are already in the right order, we can stop
if ( isset( $new_positions[ $attachment_id ] ) && $sibling->menu_order >= $menu_order )
break;
// set the menu order of the current sibling and increment the menu order
$attachment = array();
$attachment['ID'] = $sibling_id;
$attachment['menu_order'] = $menu_order;
wp_update_post( $attachment );
$new_positions[ $sibling_id ] = $menu_order;
$menu_order++;
if ( ! $next_attachment_id && $prev_attachment_id == $sibling_id ) {
$attachment = array();
$attachment['ID'] = $attachment_id;
$attachment['menu_order'] = $menu_order;
wp_update_post( $attachment );
$new_positions[ $attachment_id ] = $menu_order;
$menu_order++;
}
}
// Set featured image
$new_positions = array_flip( $new_positions );
if ( isset( $new_positions[1] ) )
update_post_meta( $post_id, '_thumbnail_id', $new_positions[1] );
die();
}
add_action( 'wp_ajax_woocommerce_product_image_ordering', 'woocommerce_product_image_ordering' );
/**
* woocommerce_product_image_delete function.
*
* @access public
* @return void
*/
function woocommerce_product_image_delete() {
check_ajax_referer( 'product-image-delete' );
$post_id = isset( $_POST['post_id'] ) ? absint( $_POST['post_id'] ) : false;
$attachment_id = isset( $_POST['attachment_id'] ) ? absint( $_POST['attachment_id'] ) : false;
if ( ! $post_id || ! $attachment_id )
die( -1 );
wp_delete_attachment( $attachment_id );
$thumbnail_id = get_post_thumbnail_id( $post_id );
if ( $thumbnail_id == $attachment_id ) {
$attachments = get_children( 'post_parent=' . $post_id . '&numberposts=1&post_type=attachment&orderby=menu_order&order=ASC&post_mime_type=image' );
foreach ( $attachments as $attachment_id => $attachment ) {
update_post_meta( $post_id, '_thumbnail_id', $attachment_id );
}
}
die();
}
add_action( 'wp_ajax_woocommerce_product_image_delete', 'woocommerce_product_image_delete' );
/**
* woocommerce_product_image_refresh function.
*
* @access public
* @return void
*/
function woocommerce_product_image_refresh() {
check_ajax_referer( 'product-image-refresh' );
$post_id = isset( $_POST['post_id'] ) ? absint( $_POST['post_id'] ) : false;
if ( ! $post_id )
die();
?>
<ul class="product_images">
<?php
$thumbnail_id = get_post_thumbnail_id( $post_id );
if ( $thumbnail_id )
echo '<li class="image" data-post_id="' . $thumbnail_id . '">
' . wp_get_attachment_image( $thumbnail_id, 'full' ) . '
<span class="loading"></span>
<ul class="actions">
<li><a href="#" class="delete">' . __( 'Delete', 'woocommerce' ) . '</a></li>
<li><a href="' . admin_url( 'media-upload.php?post_id=' . $post_id . '&attachment_id=' . $thumbnail_id . '&tab=library&width=640&height=553&TB_iframe=1' ) . '" class="view thickbox" onclick="return false;">' . __( 'View', 'woocommerce' ) . '</a></li>
</ul>
</li>';
$attachments =& get_children( 'post_parent=' . $post_id . '&numberposts=-1&post_type=attachment&orderby=menu_order&order=ASC&post_mime_type=image' );
foreach ( $attachments as $attachment_id => $attachment ) {
if ( $thumbnail_id == $attachment_id )
continue;
$exclude_class = get_post_meta( $attachment_id, '_woocommerce_exclude_image', true ) == 1 ? 'excluded' : '';
echo '<li class="image ' . $exclude_class . '" data-post_id="' . $attachment_id . '">
' . wp_get_attachment_image( $attachment_id, 'full' ) . '
<span class="loading"></span>
<ul class="actions">
<li><a href="#" class="delete">' . __( 'Delete', 'woocommerce' ) . '</a></li>
<li><a href="' . admin_url( 'media-upload.php?post_id=' . $post_id . '&attachment_id=' . $attachment_id . '&tab=library&width=640&height=553&TB_iframe=1' ) . '" class="view thickbox" onclick="return false;">' . __( 'View', 'woocommerce' ) . '</a></li>
</ul>
</li>';
}
?>
</ul>
<?php
die();
}
add_action( 'wp_ajax_woocommerce_product_image_refresh', 'woocommerce_product_image_refresh' );
add_action( 'wp_ajax_woocommerce_product_ordering', 'woocommerce_product_ordering' );