You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
1.5 KiB

2 years ago
2 years ago
  1. <?php namespace WpsMcloud\Models;
  2. class PostAttachment
  3. {
  4. const META_KEY_FLAGGED_AS_404 = '_wp_media_cloud_transform_404';
  5. const META_KEY_ATTACHMENT_METADATA = '_wp_attachment_metadata';
  6. public int $id;
  7. public string $meta_value;
  8. public ?array $meta;
  9. public string $guid;
  10. public string $post_mime_type;
  11. public function __construct($post)
  12. {
  13. $this->id = $post->ID;
  14. $this->meta_value = $post->meta_value;
  15. $this->guid = $post->guid;
  16. $this->post_mime_type = $post->post_mime_type;
  17. }
  18. public static function get(array $posts): array
  19. {
  20. return array_map(fn ($post) => new self($post), $posts);
  21. }
  22. public function getId(): string
  23. {
  24. return $this->id;
  25. }
  26. public function getMeta(): array
  27. {
  28. if (!isset($this->meta)) {
  29. $this->meta = unserialize($this->meta_value);
  30. }
  31. return $this->meta;
  32. }
  33. public function getUrl(): string
  34. {
  35. return $this->guid;
  36. }
  37. public function getBasename(): string
  38. {
  39. return mb_basename($this->getUrl());
  40. }
  41. public function getPostMimeType(): string
  42. {
  43. return $this->post_mime_type;
  44. }
  45. public function setAttachmentMetaData(array $meta)
  46. {
  47. update_post_meta($this->getId(), self::META_KEY_ATTACHMENT_METADATA, $meta);
  48. }
  49. public function setFlaggedAs404()
  50. {
  51. add_post_meta($this->getId(), self::META_KEY_FLAGGED_AS_404, 1);
  52. }
  53. }