woocommerce/includes/admin/meta-boxes/class-wc-meta-box-product-i...

128 lines
4.0 KiB
PHP

<?php
/**
* Product Images
*
* Display the product images meta box.
*
* @author WooThemes
* @category Admin
* @package WooCommerce/Admin/Meta Boxes
* @version 2.1.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
/**
* WC_Meta_Box_Product_Images Class.
*/
class WC_Meta_Box_Product_Images {
/**
* Output the metabox.
*
* @param WP_Post $post
*/
public static function output( $post ) {
global $thepostid, $product_object;
// Restrict the markup of the new filter to these tags and attributes.
$allow_markup = array(
'a' => array(
'id' => array(),
'href' => array(),
'title' => array(),
'onclick' => array(),
'class' => array(),
),
'div' => array(
'id' => array(),
'title' => array(),
'onclick' => array(),
'class' => array(),
),
'input' => array(
'type' => array(),
'name' => array(),
'id' => array(),
'value' => array(),
'class' => array(),
),
);
$thepostid = $post->ID;
$product_object = $thepostid ? wc_get_product( $thepostid ) : new WC_Product();
wp_nonce_field( 'woocommerce_save_data', 'woocommerce_meta_nonce' );
?>
<div id="product_images_container">
<ul class="product_images">
<?php
$product_image_gallery = $product_object->get_gallery_image_ids( 'edit' );
$attachments = array_filter( $product_image_gallery );
$update_meta = false;
$updated_gallery_ids = array();
if ( ! empty( $attachments ) ) {
foreach ( $attachments as $attachment_id ) {
$attachment = wp_get_attachment_image( $attachment_id, 'thumbnail' );
// if attachment is empty skip.
if ( empty( $attachment ) ) {
$update_meta = true;
continue;
}
// Allow for extra info or action to be exposed for this attachment, but restrict it's output.
$attachment_info_action = wp_kses(
apply_filters( 'woocommerce_product_gallery_item_info_action', '', $thepostid, $attachment_id ),
$allow_markup
);
echo '<li class="image" data-attachment_id="' . esc_attr( $attachment_id ) . '">
' . $attachment . '
<ul class="actions">
<li><a href="#" class="delete tips" data-tip="' . esc_attr__( 'Delete image', 'woocommerce' ) . '">' . __( 'Delete', 'woocommerce' ) . '</a></li>
</ul>
' . $attachment_info_action . '
</li>';
// 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 ) );
}
}
?>
</ul>
<input type="hidden" id="product_image_gallery" name="product_image_gallery" value="<?php echo esc_attr( implode( ',', $updated_gallery_ids ) ); ?>" />
</div>
<p class="add_product_images hide-if-no-js">
<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>
</p>
<?php
}
/**
* Save meta box data.
*
* @param int $post_id
* @param WP_Post $post
*/
public static function save( $post_id, $post ) {
$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 );
$attachment_ids = isset( $_POST['product_image_gallery'] ) ? array_filter( explode( ',', wc_clean( $_POST['product_image_gallery'] ) ) ) : array();
$product->set_gallery_image_ids( $attachment_ids );
$product->save();
}
}