From 2bfc93ceda887181685c1403999b6ec1c428e83c Mon Sep 17 00:00:00 2001 From: Victor Westerlund Date: Tue, 24 Feb 2026 11:51:14 +0100 Subject: [PATCH] refactor: use `Database` instance from scaffold library (#16) Bumped vlw/scaffold to 1.7.6 and make use of the `Database::instance()` method. This is an almost-fix for issue #15. It will not close it though. The Database class is still a hot mess. Reviewed-on: https://codeberg.org/vlw/wp/pulls/16 --- composer.lock | 6 +++--- src/Database.php | 8 +------- src/Posts/Type/Attachment.php | 35 +++++++++++++++++++---------------- 3 files changed, 23 insertions(+), 26 deletions(-) diff --git a/composer.lock b/composer.lock index 69292ff..3d573f8 100644 --- a/composer.lock +++ b/composer.lock @@ -35,11 +35,11 @@ }, { "name": "vlw/scaffold", - "version": "1.7.0", + "version": "1.7.6", "source": { "type": "git", "url": "https://codeberg.org/vlw/scaffold", - "reference": "1536079fe384c0cdc8b3814b67318140dbc339ff" + "reference": "ffd809f76dd50d7a6ee631f21715dca40ec40f44" }, "require": { "vlw/mysql": "3.5.*" @@ -61,7 +61,7 @@ } ], "description": "Project scaffolding (primarily) for Reflect and Vegvisir projects", - "time": "2026-02-11T16:25:22+00:00" + "time": "2026-02-24T09:44:05+00:00" }, { "name": "vlw/xenum", diff --git a/src/Database.php b/src/Database.php index e85f780..b0481b0 100644 --- a/src/Database.php +++ b/src/Database.php @@ -31,13 +31,7 @@ * @return static */ public static function current(): static { - return new static( - self::$hostname, - self::$username, - self::$password, - self::$name, - self::$prefix - ); + return static::instance(); } /** diff --git a/src/Posts/Type/Attachment.php b/src/Posts/Type/Attachment.php index 4257a61..fa58493 100644 --- a/src/Posts/Type/Attachment.php +++ b/src/Posts/Type/Attachment.php @@ -5,17 +5,21 @@ use Exception; use vlw\MimeTypes\MimeTypes; + use vlw\WP\Database; use vlw\WP\Posts\Post; + use vlw\WP\Tables\Posts; use vlw\WP\Posts\PostMeta; use function vlw\WP\Support\slugify; 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) . "/Tables/Posts.php"; class Attachment { - private const META_KEY_THUMBNAIL_ID = "_thumbnail_id"; - private const META_KEY_FEATURED_MEDIA = "_featured_media"; + private const POST_TYPE = "attachment"; + private const META_KEY_THUMBNAIL_ID = "_thumbnail_id"; + private const META_KEY_FEATURED_MEDIA = "_featured_media"; private const META_KEY_WP_ATTACHED_FILE = "_wp_attached_file"; public private(set) readonly Post $post; @@ -28,24 +32,23 @@ * @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); + // We didn't find any post meta, let's search the post table for an attachment with this Post as its parent + $query = Database::current() + ->from(Database::get_table("posts")) + ->where([ + Posts::POST_PARENT->value => $post->id, + Posts::POST_TYPE->value => self::POST_TYPE + ]) + ->limit(1) + ->select(Posts::ID->value); - 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) { + // Bail out, we didn't find any valid featured media for this Post + if ($query->num_rows !== 1) { return null; } - // New instance from post ID in featured media serialized object - return new static((int) unserialize($post_meta->meta_value)[0]); + // We found an attachment + return new static($query->fetch_assoc()[Posts::ID->value]); } /**