| 1 |
<?php |
| 2 |
namespace Depicter\Services; |
| 3 |
|
| 4 |
|
| 5 |
use Averta\Core\Utility\Arr; |
| 6 |
use Averta\WordPress\Handler\Error; |
| 7 |
use Averta\WordPress\Utility\Sanitize; |
| 8 |
use Depicter; |
| 9 |
use Depicter\GuzzleHttp\Exception\GuzzleException; |
| 10 |
use Depicter\GuzzleHttp\TransferStats; |
| 11 |
use Depicter\Media\Image\ImageEditor; |
| 12 |
use SimpleXMLElement; |
| 13 |
|
| 14 |
class MediaLibraryService |
| 15 |
{ |
| 16 |
/** |
| 17 |
* Query the database |
| 18 |
* |
| 19 |
* @param array $queryParams |
| 20 |
* |
| 21 |
* @return \WP_Query |
| 22 |
*/ |
| 23 |
public function query( $queryParams = [] ){ |
| 24 |
return new \WP_Query( $queryParams ); |
| 25 |
} |
| 26 |
|
| 27 |
|
| 28 |
/** |
| 29 |
* Generate output for attachments |
| 30 |
* |
| 31 |
* @param \WP_Query $attachments |
| 32 |
* |
| 33 |
* @param string $assetType |
| 34 |
* |
| 35 |
* @return array |
| 36 |
*/ |
| 37 |
public function getQueryOutput( $attachments, $assetType = '' ){ |
| 38 |
$result = []; |
| 39 |
|
| 40 |
if ( $attachments->have_posts() ) { |
| 41 |
$hits = []; |
| 42 |
|
| 43 |
foreach ( $attachments->posts as $key => $attachment ) { |
| 44 |
$attachmentMeta = wp_get_attachment_metadata( $attachment->ID ); |
| 45 |
$mimType = get_post_mime_type( $attachment->ID ); |
| 46 |
$hits[ $key ] = [ |
| 47 |
"id" => $attachment->ID . "", |
| 48 |
"type" => $assetType, |
| 49 |
"mimeType" => $mimType, |
| 50 |
"sourceType" => "library", |
| 51 |
"title" => $attachment->post_title, |
| 52 |
"description" => $attachment->post_content, |
| 53 |
]; |
| 54 |
|
| 55 |
if ( $mimType == 'image/svg+xml' ) { |
| 56 |
$fileContent = @file_get_contents(get_attached_file( $attachment->ID )); |
| 57 |
if( empty( trim( $fileContent ) ) ){ |
| 58 |
continue; |
| 59 |
} |
| 60 |
$svg = new SimpleXMLElement( $fileContent ); |
| 61 |
$hits[ $key ] = Arr::merge( $hits[ $key ], [ |
| 62 |
'width' => (int) $svg['width'], |
| 63 |
'height' => (int) $svg['height'], |
| 64 |
]); |
| 65 |
} else if ( strpos( $mimType, 'audio' ) !== 0 ) { |
| 66 |
$hits[ $key ] = Arr::merge( $hits[ $key ], [ |
| 67 |
'width' => $attachmentMeta['width'], |
| 68 |
'height' => $attachmentMeta['height'], |
| 69 |
"thumb" => wp_get_attachment_image_src( $attachment->ID, 'depicter-thumbnail' )[0] |
| 70 |
]); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
$result = [ |
| 75 |
'page' => $attachments->query['paged'], |
| 76 |
'perpage' => $attachments->query['posts_per_page'], |
| 77 |
'totalPages' => $attachments->max_num_pages, |
| 78 |
'total' => $attachments->found_posts, |
| 79 |
'hasMore' => $attachments->query['paged'] < $attachments->max_num_pages, |
| 80 |
'hits' => $hits |
| 81 |
]; |
| 82 |
|
| 83 |
} |
| 84 |
|
| 85 |
return $result; |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
/** |
| 90 |
* Get the direct link to media source |
| 91 |
* |
| 92 |
* @param $id |
| 93 |
* @param $size |
| 94 |
* @param $args |
| 95 |
* |
| 96 |
* @return false|string |
| 97 |
* @throws \Exception |
| 98 |
*/ |
| 99 |
public function getSourceURL( $id, $size = 'full', $args = [] ) { |
| 100 |
|
| 101 |
if( empty( $id ) ){ |
| 102 |
throw new \Exception('Media ID is required.'); |
| 103 |
} |
| 104 |
|
| 105 |
$available_sizes = [ |
| 106 |
'screen' => 'full', |
| 107 |
'full' => 'full', |
| 108 |
'large' => 'full', |
| 109 |
'medium' => 'medium_large', |
| 110 |
'small' => 'medium', |
| 111 |
'thumb' => 'thumbnail' |
| 112 |
]; |
| 113 |
|
| 114 |
if ( ! is_array( $size ) ) { |
| 115 |
$mediaSize = ! in_array( $size, array_keys( $available_sizes ) ) ? 'full' : $available_sizes[ $size ]; |
| 116 |
} else { |
| 117 |
$mediaSize = 'full'; |
| 118 |
} |
| 119 |
|
| 120 |
$mime_type = get_post_mime_type( $id ); |
| 121 |
|
| 122 |
// If media not found |
| 123 |
if( false === $mime_type ){ |
| 124 |
throw new \Exception('Media does not exists.'); |
| 125 |
} |
| 126 |
// If mime type was not detected try to retrieve original attachment url |
| 127 |
if( empty( $mime_type ) && $attachment_url = wp_get_attachment_url( $id ) ){ |
| 128 |
return $attachment_url; |
| 129 |
} |
| 130 |
|
| 131 |
// If it was video mime type |
| 132 |
if( 0 === strpos( $mime_type, 'video' ) ){ |
| 133 |
$attachment_url = wp_get_attachment_url( $id ); |
| 134 |
return $attachment_url; |
| 135 |
} |
| 136 |
|
| 137 |
// If it was image mime type |
| 138 |
if ( $media = wp_get_attachment_image_src( $id, $mediaSize ) ) { |
| 139 |
if ( is_array( $size ) ) { |
| 140 |
if ( $media[1] < $size[0] * 2 ){ |
| 141 |
return $media[0]; |
| 142 |
} |
| 143 |
$url = ImageEditor::resize( $media[0], $size[0], $size[1], $args ); |
| 144 |
return $url ? $url : $media[0]; |
| 145 |
} |
| 146 |
return $media[0]; |
| 147 |
} |
| 148 |
|
| 149 |
throw new \Exception('Media not found.'); |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Imports an asset to media library |
| 154 |
* |
| 155 |
* @param $assetID |
| 156 |
* |
| 157 |
* @param bool $forceToDownloadAgain |
| 158 |
* |
| 159 |
* @return false|int Attachment ID or false on failure |
| 160 |
* @throws GuzzleException |
| 161 |
*/ |
| 162 |
public function importAsset( $assetID, $forceToDownloadAgain = false ) { |
| 163 |
|
| 164 |
// Check if this asset id is imported before or not |
| 165 |
// Useful while user publishes document during edit process multiple times |
| 166 |
$attachmentId = $this->getAttachmentForImportedAsset( $assetID ); |
| 167 |
if( ! $forceToDownloadAgain && $attachmentId && get_attached_file( $attachmentId ) ){ |
| 168 |
return $attachmentId; |
| 169 |
} |
| 170 |
|
| 171 |
$args = [ |
| 172 |
'forcePreview' => false, |
| 173 |
'event' => 'download' |
| 174 |
]; |
| 175 |
$mediaHotlinkUrl = AssetsAPIService::getHotlink( $assetID, 'large', $args ); |
| 176 |
|
| 177 |
$assetFileName = Sanitize::fileName( $assetID ); |
| 178 |
|
| 179 |
$response = Depicter::remote()->get( $mediaHotlinkUrl, [ |
| 180 |
'on_stats' => function (TransferStats $stats) use (&$url) { |
| 181 |
$url = $stats->getEffectiveUri(); |
| 182 |
} |
| 183 |
]); |
| 184 |
|
| 185 |
$type = $response->getHeaderLine('content-type'); // 'application/json; charset=utf8' |
| 186 |
|
| 187 |
if ( ! $type || $response->getStatusCode() == 404 ){ |
| 188 |
return false; |
| 189 |
} |
| 190 |
|
| 191 |
if ( $type == 'image/jpeg' ){ |
| 192 |
$filename = $assetFileName . '.jpg'; |
| 193 |
} elseif ( $type == 'image/png' ) { |
| 194 |
$filename = $assetFileName . '.png'; |
| 195 |
} elseif ( $type == 'image/bmp' ) { |
| 196 |
$filename = $assetFileName . '.bmp'; |
| 197 |
} elseif ( $type == 'image/svg+xml' ) { |
| 198 |
$filename = basename( $url ); |
| 199 |
} else { |
| 200 |
// $parts = parse_url( $url ); |
| 201 |
// parse_str( $parts['query'], $query); |
| 202 |
// $filename = isset( $query['filename'] ) ? $query['filename'] : $assetFileName . '.mp4'; |
| 203 |
$filename = $assetFileName . '.mp4'; |
| 204 |
} |
| 205 |
|
| 206 |
$fileSystem = Depicter::storage()->filesystem(); |
| 207 |
|
| 208 |
$file = Depicter::storage()->uploads()->getPath() . '/' . $filename; |
| 209 |
$fileType = wp_check_filetype( $filename, null ); |
| 210 |
|
| 211 |
if( $fileSystem->exists( $file ) ){ |
| 212 |
$fileUrl = Depicter::storage()->uploads()->getUrl() . '/' . $filename; |
| 213 |
$attachmentId = attachment_url_to_postid( $fileUrl ); |
| 214 |
if ( $attachmentId ) { |
| 215 |
$this->registerImportedAsset( $assetID, (int) $attachmentId ); |
| 216 |
return $attachmentId; |
| 217 |
} |
| 218 |
|
| 219 |
return $this->insertAttachment( $assetID, $file, $fileType['type'], $assetFileName ); |
| 220 |
} |
| 221 |
|
| 222 |
$isFileDownloaded = $fileSystem->write( |
| 223 |
$file, |
| 224 |
$response->getBody()->getContents() |
| 225 |
); |
| 226 |
|
| 227 |
if ( ! $isFileDownloaded ) { |
| 228 |
return false; |
| 229 |
} |
| 230 |
|
| 231 |
return $this->insertAttachment( $assetID, $file, $fileType['type'], $assetFileName ); |
| 232 |
} |
| 233 |
|
| 234 |
/** |
| 235 |
* Insert Attachment |
| 236 |
* |
| 237 |
* @param string $assetID |
| 238 |
* @param string $filePath |
| 239 |
* @param string $fileType |
| 240 |
* @param string $fileName |
| 241 |
* |
| 242 |
* @return false|int |
| 243 |
*/ |
| 244 |
public function insertAttachment( string $assetID, string $filePath, string $fileType, string $fileName = '') { |
| 245 |
$attachment = array( |
| 246 |
'post_mime_type' => $fileType, |
| 247 |
'post_title' => !empty( $fileName ) ? $fileName : basename( $filePath ), |
| 248 |
'post_content' => '', |
| 249 |
'post_status' => 'inherit' |
| 250 |
); |
| 251 |
|
| 252 |
$attachmentId = wp_insert_attachment( $attachment, $filePath ); |
| 253 |
|
| 254 |
if( is_wp_error( $attachmentId ) || ! $attachmentId ){ |
| 255 |
error_log( 'Error while inserting asset with ID of ' . $assetID, 0 ); |
| 256 |
return false; |
| 257 |
} |
| 258 |
|
| 259 |
wp_update_attachment_metadata( $attachmentId, wp_generate_attachment_metadata( $attachmentId, $filePath ) ); |
| 260 |
|
| 261 |
$this->registerImportedAsset( $assetID, (int) $attachmentId ); |
| 262 |
|
| 263 |
return $attachmentId; |
| 264 |
} |
| 265 |
/** |
| 266 |
* Imports list of assets to media library |
| 267 |
* |
| 268 |
* @param $assetIDs |
| 269 |
* @param $forceToDownloadAgain |
| 270 |
* |
| 271 |
* @throws GuzzleException |
| 272 |
*/ |
| 273 |
public function importAssets( $assetIDs, $forceToDownloadAgain = false ) |
| 274 |
{ |
| 275 |
foreach( $assetIDs as $ID ){ |
| 276 |
$this->importAsset( $ID, $forceToDownloadAgain ); |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
/** |
| 281 |
* Retrieves an attachment ID for an asset if it was imported and registered before. |
| 282 |
* |
| 283 |
* @param string $assetId |
| 284 |
* |
| 285 |
* @return string|bool False if attachment ID does not exists for asset ID |
| 286 |
*/ |
| 287 |
public function getAttachmentForImportedAsset( $assetId ) |
| 288 |
{ |
| 289 |
if( empty( $assetId ) ){ |
| 290 |
Error::trigger('Asset ID is not valid.'); |
| 291 |
return false; |
| 292 |
} |
| 293 |
|
| 294 |
$dictionary = $this->getImportedAssetsDictionary(); |
| 295 |
return isset( $dictionary[ $assetId ] ) ? $dictionary[ $assetId ] : false; |
| 296 |
} |
| 297 |
|
| 298 |
/** |
| 299 |
* Retrieves supported mime types for a media type |
| 300 |
* |
| 301 |
* @param string $mediaType Media type. image, photo, video, audio, vector, svg |
| 302 |
* |
| 303 |
* @return mixed|string |
| 304 |
*/ |
| 305 |
public function getSupportedMimeTypes( $mediaType = '' ){ |
| 306 |
$mimeTypes = [ |
| 307 |
'image' => ['image/jpeg', 'image/gif', 'image/png', 'image/bmp', 'image/tiff', 'image/x-icon', 'image/webp'], |
| 308 |
'photo' => ['image/jpeg', 'image/gif', 'image/png', 'image/bmp', 'image/tiff', 'image/x-icon', 'image/webp'], |
| 309 |
'video' => 'video', |
| 310 |
'audio' => 'audio', |
| 311 |
'vector' => 'image/svg+xml', |
| 312 |
'svg' => 'image/svg+xml' |
| 313 |
]; |
| 314 |
|
| 315 |
if( ! empty( $mimeTypes[ $mediaType ] ) ){ |
| 316 |
return $mimeTypes[ $mediaType ]; |
| 317 |
} |
| 318 |
|
| 319 |
return ''; |
| 320 |
} |
| 321 |
|
| 322 |
/** |
| 323 |
* Registers the attachment ID and belonging asset ID in a dictionary. |
| 324 |
* |
| 325 |
* @param string $assetId |
| 326 |
* @param int $attachmentId |
| 327 |
* |
| 328 |
* @return bool False on failure |
| 329 |
*/ |
| 330 |
private function registerImportedAsset( $assetId, $attachmentId ) |
| 331 |
{ |
| 332 |
if( empty( $assetId ) || empty( $attachmentId ) ){ |
| 333 |
Error::trigger('Asset ID or attachment ID is not valid.'); |
| 334 |
return false; |
| 335 |
} |
| 336 |
|
| 337 |
$dictionary = $this->getImportedAssetsDictionary(); |
| 338 |
$dictionary[ $assetId ] = $attachmentId; |
| 339 |
|
| 340 |
return \Depicter::options()->set( 'imported_assets', $dictionary ); |
| 341 |
} |
| 342 |
|
| 343 |
/** |
| 344 |
* Retrieves list of all assets which were imported and registered before. |
| 345 |
* |
| 346 |
* @return array |
| 347 |
*/ |
| 348 |
private function getImportedAssetsDictionary(){ |
| 349 |
return \Depicter::options()->get('imported_assets', []); |
| 350 |
} |
| 351 |
|
| 352 |
} |
| 353 |
|