PluginProbe
Datafeedr API / 1.4.3
Datafeedr API v1.4.3
1.4.3 1.4.2 1.0.125 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.19 1.0.2 1.0.20 1.0.21 1.0.22 1.0.23 1.0.24 1.0.25 1.0.26 1.0.27 1.0.28 1.0.29 1.0.3 1.0.30 1.0.31 1.0.32 All 182 releases
datafeedr-api / classes / class-dfrapi-image-data.php

class-dfrapi-image-data.php in Datafeedr API 1.4.3, at classes/class-dfrapi-image-data.php

171 lines 4.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 defined( 'ABSPATH' ) || exit;
4
5
6 class Dfrapi_Image_Data {
7
8 /**
9 * @var string $image_url The URL of the image to import into the Media Library.
10 */
11 private $image_url;
12
13 /**
14 * @var string $filename Name of the image file.
15 */
16 private $filename = '';
17
18 /**
19 * @var string $post_title Maps to Media Title field.
20 */
21 private $post_title = '';
22
23 /**
24 * @var string $post_content Maps to Media Description field.
25 */
26 private $post_content = '';
27
28 /**
29 * @var string $alternative_text Maps to postmeta "_wp_attachment_image_alt" field.
30 */
31 private $alternative_text = '';
32
33 /**
34 * @var string $post_excerpt Maps to Media Caption field.
35 */
36 private $post_excerpt = '';
37
38 /**
39 * @var int $post_author Maps to Media Author ID.
40 */
41 private $post_author = 0;
42
43 /**
44 * @var int $post_parent The Post ID the media is associated with.
45 */
46 private $post_parent = 0;
47
48 /**
49 * @var bool $is_post_thumbnail Whether or not this image should be set as the post thumbnail.
50 */
51 private $is_post_thumbnail = false;
52
53 /**
54 * Dfrapi_Image_Data constructor.
55 *
56 * @param string $image_url
57 */
58 public function __construct( string $image_url ) {
59 $this->set_image_url( $image_url );
60 }
61
62 public function get_image_url() {
63 return $this->image_url;
64 }
65
66 public function get_filename() {
67 return ! empty( $this->filename ) ? $this->filename : $this->get_original_filename();
68 }
69
70 public function get_title() {
71 return ! empty( $this->post_title ) ? $this->post_title : $this->get_original_filename();
72 }
73
74 public function get_description() {
75 return ! empty( $this->post_content ) ? $this->post_content : '';
76 }
77
78 public function get_caption() {
79 return ! empty( $this->post_excerpt ) ? $this->post_excerpt : '';
80 }
81
82 public function get_author_id() {
83 return absint( $this->post_author );
84 }
85
86 public function get_post_parent_id() {
87 return absint( $this->post_parent );
88 }
89
90 public function get_alternative_text() {
91 return ! empty( $this->alternative_text ) ? $this->alternative_text : '';
92 }
93
94 public function is_post_thumbnail() {
95 return $this->is_post_thumbnail;
96 }
97
98 public function set_image_url( string $image_url ) {
99 $this->image_url = trim( $image_url );
100 }
101
102 public function set_filename( string $filename ) {
103 $this->filename = trim( $filename );
104 }
105
106 public function set_title( string $title ) {
107 $this->post_title = trim( $title );
108 }
109
110 public function set_description( string $content ) {
111 $this->post_content = trim( $content );
112 }
113
114 public function set_caption( string $excerpt ) {
115 $this->post_excerpt = trim( $excerpt );
116 }
117
118 public function set_author_id( int $author_id ) {
119 $this->post_author = absint( $author_id );
120 }
121
122 public function set_post_parent_id( int $post_parent_id ) {
123 $this->post_parent = absint( $post_parent_id );
124 }
125
126 public function set_alternative_text( string $alternative_text ) {
127 $this->alternative_text = trim( $alternative_text );
128 }
129
130 public function set_post_thumbnail( bool $is_post_thumbnail ) {
131 $this->is_post_thumbnail = boolval( $is_post_thumbnail );
132 }
133
134 /**
135 * Return an array structured similar to a WP_Post array.
136 *
137 * @return array
138 */
139 public function get_post_array() {
140 return [
141 'post_title' => $this->get_title(),
142 'post_content' => $this->get_description(),
143 'post_excerpt' => $this->get_caption(),
144 'post_author' => $this->get_author_id(),
145 'post_parent' => $this->get_post_parent_id(),
146 ];
147 }
148
149 /**
150 * Returns just the name of the image without an extension, paths, domain name, etc...
151 *
152 * Examples:
153 *
154 * Original: https://images.asos-media.com/products/new-look-snake-print-v-shaped-bikini-bottoms-in-bright-yellow/11880433-1-brightyellow?$XXLrmbnrbtm$
155 * Filename: 11880433-1-brightyellow
156 *
157 * Original: https://www.rei.com/media/3c8c2c5f-5c2c-4319-b536-1a9caefb8514
158 * Filename: 3c8c2c5f-5c2c-4319-b536-1a9caefb8514
159 *
160 * Original: https://www.patagonia.com/dw/image/v2/BDJB_PRD/on/demandware.static/-/Sites-patagonia-master/default/dwa72917ec/images/hi-res/11193_950.jpg?sw=1000&sh=1000&sfrm=png&q=95&bgcolor=f6f6f6
161 * Filename: 11193_950
162 *
163 * @return string
164 */
165 public function get_original_filename() {
166 $path = parse_url( $this->get_image_url(), PHP_URL_PATH );
167
168 return pathinfo( $path, PATHINFO_FILENAME );
169 }
170 }
171