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
This commit is contained in:
Victor Westerlund 2026-02-24 11:51:14 +01:00
parent 0837975758
commit 2bfc93ceda
3 changed files with 23 additions and 26 deletions

6
composer.lock generated
View file

@ -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",

View file

@ -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();
}
/**

View file

@ -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]);
}
/**