mirror of
https://codeberg.org/vlw/wp.git
synced 2026-02-26 03:51:58 +01:00
feat: add methods for Post featured media Attachments (#4)
In this PR we add methods for setting and getting featured media attachments for posts. Reviewed-on: https://codeberg.org/vlw/wp/pulls/4
This commit is contained in:
parent
ff9b5ca42c
commit
b14c14d9b3
2 changed files with 90 additions and 2 deletions
|
|
@ -107,6 +107,29 @@
|
||||||
return Term::from_post($this);
|
return Term::from_post($this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get, set, or unset featured media for this Post
|
||||||
|
*
|
||||||
|
* @param Attachment|false|null $attachment Pass Attachment to set featured media, false to unset, and null (or nothing) to get featured media
|
||||||
|
* @return Attachment|null Returns an Attachment if featured media is set. Null if no featured media is set
|
||||||
|
*/
|
||||||
|
public function featured_media(Attachment|false|null $featured_media = null): ?Attachment {
|
||||||
|
// Remove featured media from this Post
|
||||||
|
if ($featured_media === false) {
|
||||||
|
Attachment::remove_post_featured_media($this);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set Attachment as the featured media for this Post
|
||||||
|
if ($featured_media instanceof Attachment) {
|
||||||
|
$featured_media->set_post_featured($this);
|
||||||
|
|
||||||
|
return $featured_media;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Attachment::from_post_featured($this);
|
||||||
|
}
|
||||||
|
|
||||||
public int $post_author {
|
public int $post_author {
|
||||||
get => $this->get(Posts::POST_AUTHOR->value);
|
get => $this->get(Posts::POST_AUTHOR->value);
|
||||||
set (int $post_author) => $this->set(Posts::POST_AUTHOR->value, $post_author);
|
set (int $post_author) => $this->set(Posts::POST_AUTHOR->value, $post_author);
|
||||||
|
|
|
||||||
|
|
@ -3,14 +3,64 @@
|
||||||
namespace vlw\WP\Posts\Type;
|
namespace vlw\WP\Posts\Type;
|
||||||
|
|
||||||
use vlw\WP\Posts\Post;
|
use vlw\WP\Posts\Post;
|
||||||
|
use vlw\WP\Posts\PostMeta;
|
||||||
use function vlw\WP\Support\slugify;
|
use function vlw\WP\Support\slugify;
|
||||||
|
|
||||||
require_once dirname(__DIR__, 1) . "/Post.php";
|
require_once dirname(__DIR__, 1) . "/Post.php";
|
||||||
|
require_once dirname(__DIR__, 1) . "/PostMeta.php";
|
||||||
require_once dirname(__DIR__, 2) . "/Support/Slugify.php";
|
require_once dirname(__DIR__, 2) . "/Support/Slugify.php";
|
||||||
|
|
||||||
class Attachment {
|
class Attachment {
|
||||||
|
private const META_KEY_THUMBNAIL_ID = "_thumbnail_id";
|
||||||
|
private const META_KEY_FEATURED_MEDIA = "_featured_media";
|
||||||
|
|
||||||
private readonly Post $post;
|
private readonly Post $post;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return featured media for a Post if it exists
|
||||||
|
*
|
||||||
|
* @param Post $post
|
||||||
|
* @param bool $thumbnail Thumbnail is the same as the featured meta. Default to true
|
||||||
|
* @return Attachment|null
|
||||||
|
*/
|
||||||
|
public static function from_post_featured(Post $post, bool $thumbnail = true): ?Attachment {
|
||||||
|
// Check the thumbnail for an attachment ID. This operation is slightly less demanding than unserialize
|
||||||
|
if ($thumbnail) {
|
||||||
|
$post_meta = PostMeta::get_post_meta($post, self::META_KEY_THUMBNAIL_ID);
|
||||||
|
|
||||||
|
if ($post_meta) {
|
||||||
|
return new static($post_meta->id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$post_meta = PostMeta::get_post_meta($post, self::META_KEY_FEATURED_MEDIA);
|
||||||
|
|
||||||
|
// Bail out, the target Post does not have any featured media
|
||||||
|
if (!$post_meta) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// New instance from post ID in featured media serialized object
|
||||||
|
return new static((int) unserialize($post_meta->meta_value)[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove featured media from a Post
|
||||||
|
*
|
||||||
|
* @param Post $post
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public static function remove_post_featured_media(Post $post): void {
|
||||||
|
foreach ([
|
||||||
|
PostMeta::get_post_meta($post, self::META_KEY_THUMBNAIL_ID),
|
||||||
|
PostMeta::get_post_meta($post, self::META_KEY_FEATURED_MEDIA)
|
||||||
|
] as $post_meta) {
|
||||||
|
if ($post_meta) {
|
||||||
|
$post_meta->delete();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new post
|
* Create a new post
|
||||||
*
|
*
|
||||||
|
|
@ -20,8 +70,10 @@
|
||||||
*/
|
*/
|
||||||
public static function new(string $title, string $url): static {
|
public static function new(string $title, string $url): static {
|
||||||
$post = Post::new($title);
|
$post = Post::new($title);
|
||||||
|
|
||||||
$post->post_title = slugify($title);
|
$post->post_title = slugify($title);
|
||||||
$post->guid = $url;
|
$post->guid = $url;
|
||||||
|
$post->post_type = "attachment";
|
||||||
|
|
||||||
return new static($post->id);
|
return new static($post->id);
|
||||||
}
|
}
|
||||||
|
|
@ -30,7 +82,20 @@
|
||||||
$this->post = new Post($id);
|
$this->post = new Post($id);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function get_attachment(): string {
|
/**
|
||||||
return file_get_contents($this->post->guid);
|
* Make this Attachment the featured media of a Post
|
||||||
|
*
|
||||||
|
* @param Post $post
|
||||||
|
* @return object
|
||||||
|
*/
|
||||||
|
public function set_post_featured(Post $post): object {
|
||||||
|
return (object) [
|
||||||
|
"thumbnail" => PostMeta::new($post, self::META_KEY_THUMBNAIL_ID, $this->id),
|
||||||
|
"featured_media" => PostMeta::new($post, self::META_KEY_FEATURED_MEDIA, "a:1:{i:0;s:3:\"{$this->id}\";}")
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public string|false $content {
|
||||||
|
get => file_get_contents($this->post->guid);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue