PluginProbe
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments / 4.2.2
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments v4.2.2
4.7.2 4.7.1 4.7.0 4.6.6 4.6.5 4.6.4 4.6.3 4.6.2 4.6.1 4.6.0 4.5.1 4.5.0 4.4.2 4.4.1 4.4.0 4.3.3 4.3.2 4.3.1 4.3.0 4.2.3 4.2.2 4.2.1 1.0.3 1.0.4 1.0.5 All 281 releases
surecart / app / src / Models / ProductMedia.php
ProductMedia.php
94 lines 2.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace SureCart\Models;
4
5 /**
6 * ProductMedia model
7 */
8 class ProductMedia extends Model {
9 /**
10 * Rest API endpoint
11 *
12 * @var string
13 */
14 protected $endpoint = 'product_medias';
15
16 /**
17 * Object name
18 *
19 * @var string
20 */
21 protected $object_name = 'product_media';
22
23 /**
24 * Does an update clear account cache?
25 *
26 * @var boolean
27 */
28 protected $clears_account_cache = true;
29
30 /**
31 * Set the media attribute
32 *
33 * @param string $value Media properties.
34 * @return void
35 */
36 public function setMediaAttribute( $value ) {
37 $this->setRelation( 'media', $value, Media::class );
38 }
39
40 /**
41 * Get the url for the product media.
42 * We do this because the media object is not always set.
43 *
44 * @param integer $size The size.
45 *
46 * @return string
47 */
48 public function getUrl( $size ) {
49 return ! empty( $this->media ) ? $this->media->getUrl( $size ) : $this->url;
50 }
51
52 /**
53 * Get the width for the product media.
54 *
55 * @return integer|null
56 */
57 public function getWidthAttribute() {
58 return ! empty( $this->media ) && isset( $this->media->width ) ? $this->media->width : null;
59 }
60
61 /**
62 * Get the height for the product media.
63 *
64 * @return integer|null
65 */
66 public function getHeightAttribute() {
67 return ! empty( $this->media ) && isset( $this->media->height ) ? $this->media->height : null;
68 }
69
70 /**
71 * Get the srcset for the product media.
72 * We do this because the media object is not always set.
73 *
74 * @param array[integer] $sizes The sizes.
75 *
76 * @return string
77 */
78 public function getSrcset( $sizes ) {
79 return ! empty( $this->media ) ? $this->media->withImageSizes( $sizes )->srcset : '';
80 }
81
82 /**
83 * Download the media to the wp install.
84 *
85 * @return integer The attachment id.
86 */
87 public function download() {
88 require_once ABSPATH . 'wp-admin/includes/media.php';
89 require_once ABSPATH . 'wp-admin/includes/file.php';
90 require_once ABSPATH . 'wp-admin/includes/image.php';
91 return \media_sideload_image( ! empty( $this->media->url ) ? $this->media->url . '?' . $this->media->filename : $this->url, 0, null, 'id' );
92 }
93 }
94