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.

68 lines
1.5 KiB

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