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