Completed
Push — master ( 15aa29...17da96 )
by Claudio
18:39 queued 11s
created

meta-boxes/class-wc-meta-box-product-images.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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