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-03 10:37:07 +00:00
|
|
|
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
|
|
|
|
if ( metadata_exists( 'post', $post->ID, '_product_image_gallery' ) ) {
|
|
|
|
$product_image_gallery = get_post_meta( $post->ID, '_product_image_gallery', true );
|
|
|
|
} else {
|
2017-07-17 10:10:52 +00:00
|
|
|
// Backwards compatibility.
|
2013-08-06 10:41:20 +00:00
|
|
|
$attachment_ids = get_posts( 'post_parent=' . $post->ID . '&numberposts=-1&post_type=attachment&orderby=menu_order&order=ASC&post_mime_type=image&fields=ids&meta_key=_woocommerce_exclude_image&meta_value=0' );
|
|
|
|
$attachment_ids = array_diff( $attachment_ids, array( get_post_thumbnail_id() ) );
|
|
|
|
$product_image_gallery = implode( ',', $attachment_ids );
|
|
|
|
}
|
|
|
|
|
2016-04-05 12:42:03 +00:00
|
|
|
$attachments = array_filter( explode( ',', $product_image_gallery ) );
|
|
|
|
$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 ) {
|
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
|
|
|
|
|
|
|
update_post_meta( $post_id, '_product_image_gallery', implode( ',', $attachment_ids ) );
|
|
|
|
}
|
2014-08-31 07:18:21 +00:00
|
|
|
}
|