Compare commits

..

4 commits

Author SHA1 Message Date
0837975758 feat: add support for WP users tables (#10)
This PR adds support for manipulating the WordPress user tables through this library

Reviewed-on: https://codeberg.org/vlw/wp/pulls/10
2026-02-22 11:04:39 +01:00
22f7f26f75 fix: return types for Post property hooks (#11)
Reviewed-on: https://codeberg.org/vlw/wp/pulls/11
2026-02-22 11:04:25 +01:00
1ff1e1aabe feat: add new methods for Attachment type (#12)
Reviewed-on: https://codeberg.org/vlw/wp/pulls/12
2026-02-22 11:04:10 +01:00
1296208866 refactor: set site_url from method instead of static property (#13)
`site_url` is not needed for every operation, let's make it a method that can be called as needed instead of always setting it as static property on the `Database` class.

Reviewed-on: https://codeberg.org/vlw/wp/pulls/13
2026-02-22 11:03:54 +01:00
7 changed files with 345 additions and 20 deletions

View file

@ -9,7 +9,6 @@
class Database extends DatabaseFramework { class Database extends DatabaseFramework {
public static string $name = ""; public static string $name = "";
public static string $site_url = "";
private static string $prefix = "wp"; private static string $prefix = "wp";
private static string $hostname = ""; private static string $hostname = "";
@ -71,8 +70,6 @@
$database $database
); );
$this->set_site_url();
parent::__construct(); parent::__construct();
} }
@ -90,11 +87,11 @@
} }
/** /**
* Fetch and set the WordPress siteurl from the options table * Fetch the WordPress siteurl from the options table
* *
* @return void * @return void
*/ */
public function set_site_url() { public function site_url() {
$query = new DatabaseFramework() $query = new DatabaseFramework()
->from(static::$prefix . "_options") ->from(static::$prefix . "_options")
->where(["option_name" => "siteurl"]) ->where(["option_name" => "siteurl"])
@ -105,6 +102,6 @@
throw new Exception("Failed to fetch siteurl from options table"); throw new Exception("Failed to fetch siteurl from options table");
} }
self::$site_url = $query->fetch_assoc()["option_value"]; return $query->fetch_assoc()["option_value"];
} }
} }

View file

@ -98,12 +98,12 @@
public DateTimeImmutable $post_date { public DateTimeImmutable $post_date {
get => new DateTimeImmutable($this->get(Posts::POST_DATE->value)); get => new DateTimeImmutable($this->get(Posts::POST_DATE->value));
set (DateTimeImmutable $post_date) => $this->set(Posts::POST_DATE->value, $post_date->format(static::DATETIME_FORMAT)); set (DateTimeImmutable $post_date) => $this->set(Posts::POST_DATE->value, $post_date->format(static::DATETIME_FORMAT), $post_date);
} }
public DateTimeImmutable $post_date_gmt { public DateTimeImmutable $post_date_gmt {
get => new DateTimeImmutable($this->get(Posts::POST_DATE_GMT->value)); get => new DateTimeImmutable($this->get(Posts::POST_DATE_GMT->value));
set (DateTimeImmutable $post_date_gmt) => $this->set(Posts::POST_DATE_GMT->value, $post_date_gmt->format(static::DATETIME_FORMAT)); set (DateTimeImmutable $post_date_gmt) => $this->set(Posts::POST_DATE_GMT->value, $post_date_gmt->format(static::DATETIME_FORMAT), $post_date_gmt);
} }
public string $post_content { public string $post_content {
@ -158,17 +158,17 @@
public DateTimeImmutable $post_modified { public DateTimeImmutable $post_modified {
get => new DateTimeImmutable($this->get(Posts::POST_MODIFIED->value)); get => new DateTimeImmutable($this->get(Posts::POST_MODIFIED->value));
set (DateTimeImmutable $post_modified) => $this->set(Posts::POST_MODIFIED->value, $post_modified->format(static::DATETIME_FORMAT)); set (DateTimeImmutable $post_modified) => $this->set(Posts::POST_MODIFIED->value, $post_modified->format(static::DATETIME_FORMAT), $post_modified);
} }
public DateTimeImmutable $post_modified_gmt { public DateTimeImmutable $post_modified_gmt {
get => new DateTimeImmutable($this->get(Posts::POST_MODIFIED_GMT->value)); get => new DateTimeImmutable($this->get(Posts::POST_MODIFIED_GMT->value));
set (DateTimeImmutable $post_modified_gmt) => $this->set(Posts::POST_MODIFIED_GMT->value, $post_modified_gmt->format(static::DATETIME_FORMAT)); set (DateTimeImmutable $post_modified_gmt) => $this->set(Posts::POST_MODIFIED_GMT->value, $post_modified_gmt->format(static::DATETIME_FORMAT), $post_modified_gmt);
} }
public Post|false|null $post_parent { public Post|false|null $post_parent {
get => $this->get(Posts::POST_PARENT->value) !== 0 ? new self($this->get(Posts::POST_PARENT->value)) : null; get => $this->get(Posts::POST_PARENT->value) !== 0 ? new self($this->get(Posts::POST_PARENT->value)) : null;
set (Post|false|null $post_parent) => $this->set(Posts::POST_PARENT->value, $post_parent ? $post_parent->id : 0); set (Post|false|null $post_parent) => $this->set(Posts::POST_PARENT->value, $post_parent ? $post_parent->id : 0, $post_parent);
} }
public string $guid { public string $guid {

View file

@ -2,6 +2,9 @@
namespace vlw\WP\Posts\Type; namespace vlw\WP\Posts\Type;
use Exception;
use vlw\MimeTypes\MimeTypes;
use vlw\WP\Posts\Post; use vlw\WP\Posts\Post;
use vlw\WP\Posts\PostMeta; use vlw\WP\Posts\PostMeta;
use function vlw\WP\Support\slugify; use function vlw\WP\Support\slugify;
@ -13,8 +16,9 @@
class Attachment { class Attachment {
private const META_KEY_THUMBNAIL_ID = "_thumbnail_id"; private const META_KEY_THUMBNAIL_ID = "_thumbnail_id";
private const META_KEY_FEATURED_MEDIA = "_featured_media"; private const META_KEY_FEATURED_MEDIA = "_featured_media";
private const META_KEY_WP_ATTACHED_FILE = "_wp_attached_file";
private readonly Post $post; public private(set) readonly Post $post;
/** /**
* Return featured media for a Post if it exists * Return featured media for a Post if it exists
@ -73,6 +77,7 @@
$post->post_title = slugify($title); $post->post_title = slugify($title);
$post->guid = $url; $post->guid = $url;
$post->post_status = "inherit";
$post->post_type = "attachment"; $post->post_type = "attachment";
return new static($post->id); return new static($post->id);
@ -82,20 +87,45 @@
$this->post = new Post($id); $this->post = new Post($id);
} }
public string|false $content {
get => file_get_contents($this->post->guid);
}
/**
* Set the pathname for this Attachemnt to a file on disk
*
* @param string $pathname
* @return void
*/
public function set_attached_file(string $pathname): void {
PostMeta::new($this->post, self::META_KEY_WP_ATTACHED_FILE, $pathname);
}
/**
* Set Attachemnt data from a file on disk
*
* @param string $file
* @return void
*/
public function set_from_file(string $file): void {
if (!is_file($file)) {
throw new Exception("No file found at location '{$file}'");
}
$this->post->post_mime_type = new MimeTypes()->get_type_from_file($file);
$this->set_attached_file($file);
}
/** /**
* Make this Attachment the featured media of a Post * Make this Attachment the featured media of a Post
* *
* @param Post $post * @param Post $post
* @return object * @return object
*/ */
public function set_post_featured(Post $post): object { public function set_post_featured(Post $post): void {
return (object) [ PostMeta::new($post, self::META_KEY_THUMBNAIL_ID, $this->id);
"thumbnail" => PostMeta::new($post, self::META_KEY_THUMBNAIL_ID, $this->id), PostMeta::new($post, self::META_KEY_FEATURED_MEDIA, "a:1:{i:0;s:3:\"{$this->id}\";}");
"featured_media" => PostMeta::new($post, self::META_KEY_FEATURED_MEDIA, "a:1:{i:0;s:3:\"{$this->id}\";}")
];
}
public string|false $content { $this->post->post_parent = $post;
get => file_get_contents($this->post->guid);
} }
} }

16
src/Tables/UserMeta.php Normal file
View file

@ -0,0 +1,16 @@
<?php
namespace vlw\WP\Tables;
use vlw\xEnum;
enum UserMeta: string {
use xEnum;
public const TABLE_NAME = "usermeta";
case UMETA_ID = "umeta_id";
case USER_ID = "user_id";
case META_KEY = "meta_key";
case META_VALUE = "meta_value";
}

22
src/Tables/Users.php Normal file
View file

@ -0,0 +1,22 @@
<?php
namespace vlw\WP\Tables;
use vlw\xEnum;
enum Users: string {
use xEnum;
public const TABLE_NAME = "users";
case ID = "id";
case USER_LOGIN = "user_login";
case USER_PASS = "user_pass";
case USER_NICENAME = "user_nicename";
case USER_EMAIL = "user_email";
case USER_URL = "user_url";
case USER_REGISTERED = "user_registered";
case USER_ACTIVATION_KEY = "user_activation_key";
case USER_STATUS = "user_status";
case DISPLAY_NAME = "display_name";
}

162
src/Users/User.php Normal file
View file

@ -0,0 +1,162 @@
<?php
namespace vlw\WP\Users;
use Exception;
use DateTimeImmutable;
use vlw\Scaffold\Database\Model;
use vlw\WP\Database;
use vlw\WP\Tables\Users;
use function vlw\WP\Support\slugify;
require_once dirname(__DIR__, 1) . "/Tables/Users.php";
require_once dirname(__DIR__, 1) . "/Support/Slugify.php";
class User extends Model {
/**
* Return a User for a given login if that user exists
*
* @param string $email
* @return static|null
*/
public static function from_login(string $login): ?static {
$query = Database::current()
->from(Database::get_table(Users::TABLE_NAME))
->where([Users::USER_LOGIN->value => $login])
->limit(1)
->select(Users::ID->value);
return $query->num_rows === 1 ? new static($query->fetch_assoc()[Users::ID->value]) : null;
}
/**
* Return a User for a given email if that user exists
*
* @param string $email
* @return static|null
*/
public static function from_email(string $email): ?static {
$query = Database::current()
->from(Database::get_table(Users::TABLE_NAME))
->where([Users::USER_EMAIL->value => $email])
->limit(1)
->select(Users::ID->value);
return $query->num_rows === 1 ? new static($query->fetch_assoc()[Users::ID->value]) : null;
}
/**
* Create a new user
*
* @param string $title
* @param string $type
* @return static
*/
public static function new(string $login, string $email, ?string $display_name = null): static {
// Bail out with existing User for $login if exists
if (static::from_login($login)) {
return static::from_login($login);
}
// Bail out with existing User for $email if exists
if (static::from_email($email)) {
return static::from_email($email);
}
// Current auto increment value will be the id for this entity
$id = Database::current()->latest(Users::TABLE_NAME);
$values = [
Users::ID->value => null,
Users::USER_LOGIN->value => $login,
Users::USER_PASS->value => "",
Users::USER_NICENAME->value => slugify($login),
Users::USER_EMAIL->value => $email,
Users::USER_URL->value => "",
Users::USER_REGISTERED->value => date(static::DATETIME_FORMAT),
Users::USER_ACTIVATION_KEY->value => "",
Users::USER_STATUS->value => 0,
Users::DISPLAY_NAME->value => $display_name ?? $login
];
if (!parent::create(Database::get_table(Users::TABLE_NAME), $values)) {
throw new Exception("Failed to create database entity");
}
return new static($id);
}
public function __construct(public readonly int $id) {
parent::__construct(
Database::get_table(Users::TABLE_NAME),
Users::values(),
[Users::ID->value => (int) $id]
);
}
public string $user_login {
get => $this->get(Users::USER_LOGIN->value);
set (string $user_login) => $this->set(Users::USER_LOGIN->value, substr($user_login, 0, 60));
}
public string $user_pass {
get => $this->get(Users::USER_PASS->value);
set (string $user_pass) => $this->set(Users::USER_PASS->value, substr($user_pass, 0, 255));
}
public string $user_nicename {
get => $this->get(Users::USER_NICENAME->value);
set (string $user_nicename) => $this->set(Users::USER_NICENAME->value, substr($user_nicename, 0, 50));
}
public string $user_email {
get => $this->get(Users::USER_EMAIL->value);
set (string $user_email) => $this->set(Users::USER_EMAIL->value, substr($user_email, 0, 100));
}
public string $user_url {
get => $this->get(Users::USER_URL->value);
set (string $user_url) => $this->set(Users::USER_URL->value, substr($user_url, 0, 100));
}
public DateTimeImmutable $user_registered {
get => new DateTimeImmutable($this->get(Users::USER_REGISTERED->value));
set (DateTimeImmutable $user_registered) => $this->set(Users::USER_REGISTERED->value, $user_registered->format(static::DATETIME_FORMAT), $user_registered);
}
public string $user_activation_key {
get => $this->get(Users::USER_ACTIVATION_KEY->value);
set (string $user_activation_key) => $this->set(Users::USER_ACTIVATION_KEY->value, substr($user_activation_key, 0, 255));
}
public int $user_status {
get => $this->get(Users::USER_STATUS->value);
set (int $user_status) => $this->set(Users::USER_STATUS->value, $user_status);
}
public string $display_name {
get => $this->get(Users::DISPLAY_NAME->value);
set (string $display_name) => $this->set(Users::DISPLAY_NAME->value, substr($display_name, 0, 250));
}
/**
* Set the password for this User
*
* @param string $password
* @return void
*/
public function set_password(string $password): void {
$this->user_pass = password_hash($password, PASSWORD_DEFAULT);
}
/**
* Check if the password for this User is $password
*
* @param string $password
* @return bool
*/
public function validate_password(string $password): bool {
return password_verify($password, $this->user_pass);
}
}

98
src/Users/UserMeta.php Normal file
View file

@ -0,0 +1,98 @@
<?php
namespace vlw\WP\Users;
use Exception;
use vlw\Scaffold\Database\Model;
use vlw\WP\Database;
use vlw\WP\Users\User;
use vlw\WP\Tables\UserMeta as UserMetaTable;
require_once "User.php";
require_once dirname(__DIR__, 1) . "/Tables/UserMeta.php";
require_once dirname(__DIR__, 1) . "/Support/Slugify.php";
class UserMeta extends Model {
/**
* Get a user meta value for a User and a user meta key
*
* @param string $name
* @return static|false
*/
public static function get_user_meta(User $user, string $key): static|false {
$query = Database::current()
->from(Database::get_table(UserMetaTable::TABLE_NAME))
->where([
UserMetaTable::USER_ID->value => $user->id,
UserMetaTable::META_KEY->value => $key
])
->limit(1)
->select(UserMetaTable::UMETA_ID->value);
return $query->num_rows === 1 ? new static($query->fetch_assoc()[UserMetaTable::UMETA_ID->value]) : false;
}
/**
* Get all user meta entries for a given User
*
* @param string $name
* @return array
*/
public static function get_all_user_meta(User $user): array {
$query = Database::current()
->from(Database::get_table(UserMetaTable::TABLE_NAME))
->where([UserMetaTable::USER_ID->value => $user->id])
->select(UserMetaTable::UMETA_ID->value);
return array_map(fn(array $post_meta): static => new static($post_meta[UserMetaTable::UMETA_ID->value]), $query->fetch_all(MYSQLI_ASSOC));
}
/**
* Create a new user
*
* @param string $title
* @param string $type
* @return static
*/
public static function new(User $user, ?string $meta_key = null, ?string $meta_value = null): static {
// Current auto increment value will be the id for this entity
$id = Database::current()->latest(UserMetaTable::TABLE_NAME);
$values = [
UserMetaTable::UMETA_ID->value => null,
UserMetaTable::USER_ID->value => $user->id,
UserMetaTable::META_KEY->value => $meta_key,
UserMetaTable::META_VALUE->value => $meta_value
];
if (!parent::create(Database::get_table(UserMetaTable::TABLE_NAME), $values)) {
throw new Exception("Failed to create database entity");
}
return new static($id);
}
public function __construct(public readonly int $id) {
parent::__construct(
Database::get_table(UserMetaTable::TABLE_NAME),
UserMetaTable::values(),
[UserMetaTable::UMETA_ID->value => (int) $id]
);
}
public User $user {
get => new User($this->get(UserMetaTable::USER_ID->value));
set (User $user) => $this->set(UserMetaTable::USER_ID->value, $user->id, $user);
}
public string $meta_key {
get => $this->get(UserMetaTable::META_KEY->value);
set (string $meta_key) => $this->set(UserMetaTable::META_KEY->value, substr($meta_key, 0, 255));
}
public string $meta_value {
get => $this->get(UserMetaTable::META_VALUE->value);
set (string $meta_value) => $this->set(UserMetaTable::META_VALUE->value, $meta_value);
}
}