Passed
Push — master ( f8a28a...ab42dc )
by Mike
04:36
created

ImageAttachment::update_name()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Helper to upload files via the REST API.
4
 *
5
 * @package WooCommerce/Utilities
6
 */
7
8
namespace WooCommerce\RestApi\Utilities;
9
10
/**
11
 * ImageAttachment class.
12
 */
13
class ImageAttachment {
14
15
	/**
16
	 * Attachment ID.
17
	 *
18
	 * @var integer
19
	 */
20
	public $id = 0;
21
22
	/**
23
	 * Object attached to.
24
	 *
25
	 * @var integer
26
	 */
27
	public $object_id = 0;
28
29
	/**
30
	 * Constructor.
31
	 *
32
	 * @param integer $id Attachment ID.
33
	 * @param integer $object_id Object ID.
34
	 */
35
	public function __construct( $id = 0, $object_id = 0 ) {
36
		$this->id        = (int) $id;
37
		$this->object_id = (int) $object_id;
38
	}
39
40
	/**
41
	 * Upload an attachment file.
42
	 *
43
	 * @throws \WC_REST_Exception REST API exceptions.
44
	 * @param string $src URL to file.
45
	 */
46
	public function upload_image_from_src( $src ) {
47
		$upload = wc_rest_upload_image_from_url( esc_url_raw( $src ) );
48
49
		if ( is_wp_error( $upload ) ) {
50
			if ( ! apply_filters( 'woocommerce_rest_suppress_image_upload_error', false, $upload, $object->get_id(), $images ) ) {
51
				throw new \WC_REST_Exception( 'woocommerce_product_image_upload_error', $upload->get_error_message(), 400 );
52
			} else {
53
				return;
54
			}
55
		}
56
57
		$this->id = wc_rest_set_uploaded_image_as_attachment( $upload, $this->object_id );
0 ignored issues
show
Bug introduced by
It seems like $upload can also be of type WP_Error; however, parameter $upload of wc_rest_set_uploaded_image_as_attachment() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

57
		$this->id = wc_rest_set_uploaded_image_as_attachment( /** @scrutinizer ignore-type */ $upload, $this->object_id );
Loading history...
58
59
		if ( ! wp_attachment_is_image( $this->id ) ) {
60
			/* translators: %s: image ID */
61
			throw new \WC_REST_Exception( 'woocommerce_product_invalid_image_id', sprintf( __( '#%s is an invalid image ID.', 'woocommerce' ), $this->attachment_id ), 400 );
0 ignored issues
show
Bug Best Practice introduced by
The property attachment_id does not exist on WooCommerce\RestApi\Utilities\ImageAttachment. Did you maybe forget to declare it?
Loading history...
62
		}
63
	}
64
65
	/**
66
	 * Update attachment alt text.
67
	 *
68
	 * @param string $text Text to set.
69
	 */
70
	public function update_alt_text( $text ) {
71
		if ( ! $this->id ) {
72
			return;
73
		}
74
		update_post_meta( $this->id, '_wp_attachment_image_alt', wc_clean( $text ) );
75
	}
76
77
	/**
78
	 * Update attachment name.
79
	 *
80
	 * @param string $text Text to set.
81
	 */
82
	public function update_name( $text ) {
83
		if ( ! $this->id ) {
84
			return;
85
		}
86
		wp_update_post(
87
			array(
88
				'ID'         => $this->id,
89
				'post_title' => $text,
90
			)
91
		);
92
	}
93
}
94