WC_Meta_Box_Product_Images

WC_Meta_Box_Product_Images Class.

Defined (1)

The class is defined in the following location(s).

/includes/admin/meta-boxes/class-wc-meta-box-product-images.php  
  1. class WC_Meta_Box_Product_Images { 
  2.  
  3. /** 
  4. * Output the metabox. 
  5. * @param WP_Post $post 
  6. */ 
  7. public static function output( $post ) { 
  8. ?> 
  9. <div id="product_images_container"> 
  10. <ul class="product_images"> 
  11. <?php 
  12. if ( metadata_exists( 'post', $post->ID, '_product_image_gallery' ) ) { 
  13. $product_image_gallery = get_post_meta( $post->ID, '_product_image_gallery', true ); 
  14. } else { 
  15. // Backwards compat 
  16. $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' ); 
  17. $attachment_ids = array_diff( $attachment_ids, array( get_post_thumbnail_id() ) ); 
  18. $product_image_gallery = implode( ', ', $attachment_ids ); 
  19.  
  20. $attachments = array_filter( explode( ', ', $product_image_gallery ) ); 
  21. $update_meta = false; 
  22. $updated_gallery_ids = array(); 
  23.  
  24. if ( ! empty( $attachments ) ) { 
  25. foreach ( $attachments as $attachment_id ) { 
  26. $attachment = wp_get_attachment_image( $attachment_id, 'thumbnail' ); 
  27.  
  28. // if attachment is empty skip 
  29. if ( empty( $attachment ) ) { 
  30. $update_meta = true; 
  31. continue; 
  32.  
  33. echo '<li class="image" data-attachment_id="' . esc_attr( $attachment_id ) . '"> 
  34. ' . $attachment . ' 
  35. <ul class="actions"> 
  36. <li><a href="#" class="delete tips" data-tip="' . esc_attr__( 'Delete image', 'woocommerce' ) . '">' . __( 'Delete', 'woocommerce' ) . '</a></li> 
  37. </ul> 
  38. </li>'; 
  39.  
  40. // rebuild ids to be saved 
  41. $updated_gallery_ids[] = $attachment_id; 
  42.  
  43. // need to update product meta to set new gallery ids 
  44. if ( $update_meta ) { 
  45. update_post_meta( $post->ID, '_product_image_gallery', implode( ', ', $updated_gallery_ids ) ); 
  46. ?> 
  47. </ul> 
  48.  
  49. <input type="hidden" id="product_image_gallery" name="product_image_gallery" value="<?php echo esc_attr( $product_image_gallery ); ?>" /> 
  50.  
  51. </div> 
  52. <p class="add_product_images hide-if-no-js"> 
  53. <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> 
  54. </p> 
  55. <?php 
  56.  
  57. /** 
  58. * Save meta box data. 
  59. * @param int $post_id 
  60. * @param WP_Post $post 
  61. */ 
  62. public static function save( $post_id, $post ) { 
  63. $attachment_ids = isset( $_POST['product_image_gallery'] ) ? array_filter( explode( ', ', wc_clean( $_POST['product_image_gallery'] ) ) ) : array(); 
  64.  
  65. update_post_meta( $post_id, '_product_image_gallery', implode( ', ', $attachment_ids ) );