mirror of
https://codeberg.org/vlw/wp.git
synced 2026-02-26 03:51:58 +01:00
Compare commits
No commits in common. "08379757589aecb2ff1cba4efdcc7d457d8b7d52" and "1b0a9f385f542167a405f70412efe579ba5e213e" have entirely different histories.
0837975758
...
1b0a9f385f
7 changed files with 20 additions and 345 deletions
|
|
@ -9,6 +9,7 @@
|
|||
|
||||
class Database extends DatabaseFramework {
|
||||
public static string $name = "";
|
||||
public static string $site_url = "";
|
||||
|
||||
private static string $prefix = "wp";
|
||||
private static string $hostname = "";
|
||||
|
|
@ -70,6 +71,8 @@
|
|||
$database
|
||||
);
|
||||
|
||||
$this->set_site_url();
|
||||
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
|
|
@ -87,11 +90,11 @@
|
|||
}
|
||||
|
||||
/**
|
||||
* Fetch the WordPress siteurl from the options table
|
||||
* Fetch and set the WordPress siteurl from the options table
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function site_url() {
|
||||
public function set_site_url() {
|
||||
$query = new DatabaseFramework()
|
||||
->from(static::$prefix . "_options")
|
||||
->where(["option_name" => "siteurl"])
|
||||
|
|
@ -102,6 +105,6 @@
|
|||
throw new Exception("Failed to fetch siteurl from options table");
|
||||
}
|
||||
|
||||
return $query->fetch_assoc()["option_value"];
|
||||
self::$site_url = $query->fetch_assoc()["option_value"];
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -98,12 +98,12 @@
|
|||
|
||||
public DateTimeImmutable $post_date {
|
||||
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), $post_date);
|
||||
set (DateTimeImmutable $post_date) => $this->set(Posts::POST_DATE->value, $post_date->format(static::DATETIME_FORMAT));
|
||||
}
|
||||
|
||||
public DateTimeImmutable $post_date_gmt {
|
||||
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), $post_date_gmt);
|
||||
set (DateTimeImmutable $post_date_gmt) => $this->set(Posts::POST_DATE_GMT->value, $post_date_gmt->format(static::DATETIME_FORMAT));
|
||||
}
|
||||
|
||||
public string $post_content {
|
||||
|
|
@ -158,17 +158,17 @@
|
|||
|
||||
public DateTimeImmutable $post_modified {
|
||||
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), $post_modified);
|
||||
set (DateTimeImmutable $post_modified) => $this->set(Posts::POST_MODIFIED->value, $post_modified->format(static::DATETIME_FORMAT));
|
||||
}
|
||||
|
||||
public DateTimeImmutable $post_modified_gmt {
|
||||
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), $post_modified_gmt);
|
||||
set (DateTimeImmutable $post_modified_gmt) => $this->set(Posts::POST_MODIFIED_GMT->value, $post_modified_gmt->format(static::DATETIME_FORMAT));
|
||||
}
|
||||
|
||||
public Post|false|null $post_parent {
|
||||
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, $post_parent);
|
||||
set (Post|false|null $post_parent) => $this->set(Posts::POST_PARENT->value, $post_parent ? $post_parent->id : 0);
|
||||
}
|
||||
|
||||
public string $guid {
|
||||
|
|
|
|||
|
|
@ -2,9 +2,6 @@
|
|||
|
||||
namespace vlw\WP\Posts\Type;
|
||||
|
||||
use Exception;
|
||||
use vlw\MimeTypes\MimeTypes;
|
||||
|
||||
use vlw\WP\Posts\Post;
|
||||
use vlw\WP\Posts\PostMeta;
|
||||
use function vlw\WP\Support\slugify;
|
||||
|
|
@ -16,9 +13,8 @@
|
|||
class 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;
|
||||
private readonly Post $post;
|
||||
|
||||
/**
|
||||
* Return featured media for a Post if it exists
|
||||
|
|
@ -77,7 +73,6 @@
|
|||
|
||||
$post->post_title = slugify($title);
|
||||
$post->guid = $url;
|
||||
$post->post_status = "inherit";
|
||||
$post->post_type = "attachment";
|
||||
|
||||
return new static($post->id);
|
||||
|
|
@ -87,45 +82,20 @@
|
|||
$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
|
||||
*
|
||||
* @param Post $post
|
||||
* @return object
|
||||
*/
|
||||
public function set_post_featured(Post $post): void {
|
||||
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}\";}");
|
||||
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}\";}")
|
||||
];
|
||||
}
|
||||
|
||||
$this->post->post_parent = $post;
|
||||
public string|false $content {
|
||||
get => file_get_contents($this->post->guid);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,16 +0,0 @@
|
|||
<?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";
|
||||
}
|
||||
|
|
@ -1,22 +0,0 @@
|
|||
<?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";
|
||||
}
|
||||
|
|
@ -1,162 +0,0 @@
|
|||
<?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);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,98 +0,0 @@
|
|||
<?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);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue