class-wc-meta-box-product-images.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /**
  3. * Product Images
  4. *
  5. * Display the product images meta box.
  6. *
  7. * @author WooThemes
  8. * @category Admin
  9. * @package WooCommerce/Admin/Meta Boxes
  10. * @version 2.1.0
  11. */
  12. if ( ! defined( 'ABSPATH' ) ) {
  13. exit; // Exit if accessed directly
  14. }
  15. /**
  16. * WC_Meta_Box_Product_Images Class.
  17. */
  18. class WC_Meta_Box_Product_Images {
  19. /**
  20. * Output the metabox.
  21. *
  22. * @param WP_Post $post
  23. */
  24. public static function output( $post ) {
  25. global $thepostid, $product_object;
  26. $thepostid = $post->ID;
  27. $product_object = $thepostid ? wc_get_product( $thepostid ) : new WC_Product();
  28. wp_nonce_field( 'woocommerce_save_data', 'woocommerce_meta_nonce' );
  29. ?>
  30. <div id="product_images_container">
  31. <ul class="product_images">
  32. <?php
  33. $product_image_gallery = $product_object->get_gallery_image_ids( 'edit' );
  34. $attachments = array_filter( $product_image_gallery );
  35. $update_meta = false;
  36. $updated_gallery_ids = array();
  37. if ( ! empty( $attachments ) ) {
  38. foreach ( $attachments as $attachment_id ) {
  39. $attachment = wp_get_attachment_image( $attachment_id, 'thumbnail' );
  40. // if attachment is empty skip.
  41. if ( empty( $attachment ) ) {
  42. $update_meta = true;
  43. continue;
  44. }
  45. echo '<li class="image" data-attachment_id="' . esc_attr( $attachment_id ) . '">
  46. ' . $attachment . '
  47. <ul class="actions">
  48. <li><a href="#" class="delete tips" data-tip="' . esc_attr__( 'Delete image', 'woocommerce' ) . '">' . __( 'Delete', 'woocommerce' ) . '</a></li>
  49. </ul>
  50. </li>';
  51. // rebuild ids to be saved.
  52. $updated_gallery_ids[] = $attachment_id;
  53. }
  54. // need to update product meta to set new gallery ids
  55. if ( $update_meta ) {
  56. update_post_meta( $post->ID, '_product_image_gallery', implode( ',', $updated_gallery_ids ) );
  57. }
  58. }
  59. ?>
  60. </ul>
  61. <input type="hidden" id="product_image_gallery" name="product_image_gallery" value="<?php echo esc_attr( implode( ',', $updated_gallery_ids ) ); ?>" />
  62. </div>
  63. <p class="add_product_images hide-if-no-js">
  64. <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>
  65. </p>
  66. <?php
  67. }
  68. /**
  69. * Save meta box data.
  70. *
  71. * @param int $post_id
  72. * @param WP_Post $post
  73. */
  74. public static function save( $post_id, $post ) {
  75. $product_type = empty( $_POST['product-type'] ) ? WC_Product_Factory::get_product_type( $post_id ) : sanitize_title( stripslashes( $_POST['product-type'] ) );
  76. $classname = WC_Product_Factory::get_product_classname( $post_id, $product_type ? $product_type : 'simple' );
  77. $product = new $classname( $post_id );
  78. $attachment_ids = isset( $_POST['product_image_gallery'] ) ? array_filter( explode( ',', wc_clean( $_POST['product_image_gallery'] ) ) ) : array();
  79. $product->set_gallery_image_ids( $attachment_ids );
  80. $product->save();
  81. }
  82. }