2013-08-06 10:41:20 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2015-11-03 13:53:50 +00:00
|
|
|
* Product Images
|
2013-08-06 10:41:20 +00:00
|
|
|
*
|
|
|
|
* Display the product images meta box.
|
|
|
|
*
|
2014-08-31 07:18:21 +00:00
|
|
|
* @author WooThemes
|
|
|
|
* @category Admin
|
|
|
|
* @package WooCommerce/Admin/Meta Boxes
|
2013-08-06 10:41:20 +00:00
|
|
|
* @version 2.1.0
|
|
|
|
*/
|
|
|
|
|
2014-09-20 20:05:06 +00:00
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
|
|
exit; // Exit if accessed directly
|
|
|
|
}
|
2013-08-06 10:41:20 +00:00
|
|
|
|
|
|
|
/**
|
2015-11-03 12:28:01 +00:00
|
|
|
* WC_Meta_Box_Product_Images Class.
|
2013-08-06 10:41:20 +00:00
|
|
|
*/
|
|
|
|
class WC_Meta_Box_Product_Images {
|
|
|
|
|
|
|
|
/**
|
2015-11-03 12:28:01 +00:00
|
|
|
* Output the metabox.
|
2016-01-04 21:31:36 +00:00
|
|
|
*
|
|
|
|
* @param WP_Post $post
|
2013-08-06 10:41:20 +00:00
|
|
|
*/
|
|
|
|
public static function output( $post ) {
|
2018-01-18 09:37:03 +00:00
|
|
|
global $thepostid, $product_object;
|
|
|
|
|
|
|
|
$thepostid = $post->ID;
|
|
|
|
$product_object = $thepostid ? wc_get_product( $thepostid ) : new WC_Product;
|
|
|
|
wp_nonce_field( 'woocommerce_save_data', 'woocommerce_meta_nonce' );
|
2013-08-06 10:41:20 +00:00
|
|
|
?>
|
|
|
|
<div id="product_images_container">
|
|
|
|
<ul class="product_images">
|
|
|
|
<?php
|
2018-01-18 09:37:03 +00:00
|
|
|
$product_image_gallery = $product_object->get_gallery_image_ids( 'edit' );
|
2013-08-06 10:41:20 +00:00
|
|
|
|
2018-01-18 09:37:03 +00:00
|
|
|
$attachments = array_filter( $product_image_gallery );
|
2016-04-05 12:42:03 +00:00
|
|
|
$update_meta = false;
|
|
|
|
$updated_gallery_ids = array();
|
2015-11-24 06:01:30 +00:00
|
|
|
|
2015-04-01 13:33:56 +00:00
|
|
|
if ( ! empty( $attachments ) ) {
|
2013-08-06 10:41:20 +00:00
|
|
|
foreach ( $attachments as $attachment_id ) {
|
2015-11-24 06:01:30 +00:00
|
|
|
$attachment = wp_get_attachment_image( $attachment_id, 'thumbnail' );
|
|
|
|
|
|
|
|
// if attachment is empty skip
|
|
|
|
if ( empty( $attachment ) ) {
|
|
|
|
$update_meta = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-11-20 19:11:59 +00:00
|
|
|
echo '<li class="image" data-attachment_id="' . esc_attr( $attachment_id ) . '">
|
2015-11-24 06:01:30 +00:00
|
|
|
' . $attachment . '
|
2013-08-06 10:41:20 +00:00
|
|
|
<ul class="actions">
|
2015-08-05 19:17:52 +00:00
|
|
|
<li><a href="#" class="delete tips" data-tip="' . esc_attr__( 'Delete image', 'woocommerce' ) . '">' . __( 'Delete', 'woocommerce' ) . '</a></li>
|
2013-08-06 10:41:20 +00:00
|
|
|
</ul>
|
|
|
|
</li>';
|
2015-11-24 06:01:30 +00:00
|
|
|
|
|
|
|
// rebuild ids to be saved
|
|
|
|
$updated_gallery_ids[] = $attachment_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
// need to update product meta to set new gallery ids
|
|
|
|
if ( $update_meta ) {
|
|
|
|
update_post_meta( $post->ID, '_product_image_gallery', implode( ',', $updated_gallery_ids ) );
|
2013-08-06 10:41:20 +00:00
|
|
|
}
|
2014-08-31 07:18:21 +00:00
|
|
|
}
|
2013-08-06 10:41:20 +00:00
|
|
|
?>
|
|
|
|
</ul>
|
|
|
|
|
|
|
|
<input type="hidden" id="product_image_gallery" name="product_image_gallery" value="<?php echo esc_attr( $product_image_gallery ); ?>" />
|
|
|
|
|
|
|
|
</div>
|
|
|
|
<p class="add_product_images hide-if-no-js">
|
2016-10-12 10:16:30 +00:00
|
|
|
<a href="#" data-choose="<?php esc_attr_e( 'Add images to product gallery', 'woocommerce' ); ?>" data-update="<?php esc_attr_e( 'Add to gallery', 'woocommerce' ); ?>" data-delete="<?php esc_attr_e( 'Delete image', 'woocommerce' ); ?>" data-text="<?php esc_attr_e( 'Delete', 'woocommerce' ); ?>"><?php _e( 'Add product gallery images', 'woocommerce' ); ?></a>
|
2013-08-06 10:41:20 +00:00
|
|
|
</p>
|
|
|
|
<?php
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 12:28:01 +00:00
|
|
|
* Save meta box data.
|
2016-01-04 21:31:36 +00:00
|
|
|
*
|
|
|
|
* @param int $post_id
|
|
|
|
* @param WP_Post $post
|
2013-08-06 10:41:20 +00:00
|
|
|
*/
|
|
|
|
public static function save( $post_id, $post ) {
|
2018-01-18 09:37:03 +00:00
|
|
|
$product_type = empty( $_POST['product-type'] ) ? WC_Product_Factory::get_product_type( $post_id ) : sanitize_title( stripslashes( $_POST['product-type'] ) );
|
|
|
|
$classname = WC_Product_Factory::get_product_classname( $post_id, $product_type ? $product_type : 'simple' );
|
|
|
|
$product = new $classname( $post_id );
|
2014-11-28 04:42:58 +00:00
|
|
|
$attachment_ids = isset( $_POST['product_image_gallery'] ) ? array_filter( explode( ',', wc_clean( $_POST['product_image_gallery'] ) ) ) : array();
|
2013-08-06 10:41:20 +00:00
|
|
|
|
2018-01-18 09:37:03 +00:00
|
|
|
$product->set_gallery_image_ids( $attachment_ids );
|
|
|
|
$product->save();
|
2013-08-06 10:41:20 +00:00
|
|
|
}
|
2014-08-31 07:18:21 +00:00
|
|
|
}
|