mirror of
https://codeberg.org/vlw/wp.git
synced 2026-04-13 12:59:39 +02:00
Compare commits
No commits in common. "1b0a9f385f542167a405f70412efe579ba5e213e" and "b14c14d9b320cd61d94e3dad58da857092eb2ca7" have entirely different histories.
1b0a9f385f
...
b14c14d9b3
5 changed files with 108 additions and 184 deletions
|
|
@ -76,19 +76,6 @@
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Return the current AUTO_INCREMENT value for a given table
|
|
||||||
*
|
|
||||||
* @param string $table
|
|
||||||
* @return int
|
|
||||||
*/
|
|
||||||
public function latest(string $table): int {
|
|
||||||
return $this->execute_query("SELECT AUTO_INCREMENT FROM information_schema.TABLES WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ?", [
|
|
||||||
self::$name,
|
|
||||||
self::get_table($table)
|
|
||||||
])->fetch_assoc()["AUTO_INCREMENT"];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fetch and set the WordPress siteurl from the options table
|
* Fetch and set the WordPress siteurl from the options table
|
||||||
*
|
*
|
||||||
|
|
|
||||||
|
|
@ -47,9 +47,6 @@
|
||||||
return self::from_name($title);
|
return self::from_name($title);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Current auto increment value will be the id for this entity
|
|
||||||
$id = Database::current()->latest(Posts::TABLE_NAME);
|
|
||||||
|
|
||||||
$values = [
|
$values = [
|
||||||
Posts::ID->value => null,
|
Posts::ID->value => null,
|
||||||
Posts::POST_AUTHOR->value => 0,
|
Posts::POST_AUTHOR->value => 0,
|
||||||
|
|
@ -62,7 +59,7 @@
|
||||||
Posts::COMMENT_STATUS->value => "closed",
|
Posts::COMMENT_STATUS->value => "closed",
|
||||||
Posts::PING_STATUS->value => "closed",
|
Posts::PING_STATUS->value => "closed",
|
||||||
Posts::POST_PASSWORD->value => "",
|
Posts::POST_PASSWORD->value => "",
|
||||||
Posts::POST_NAME->value => substr(slugify($title), 0, 200),
|
Posts::POST_NAME->value => slugify($title),
|
||||||
Posts::TO_PING->value => "",
|
Posts::TO_PING->value => "",
|
||||||
Posts::PINGED->value => "",
|
Posts::PINGED->value => "",
|
||||||
Posts::POST_MODIFIED->value => date(static::DATETIME_FORMAT),
|
Posts::POST_MODIFIED->value => date(static::DATETIME_FORMAT),
|
||||||
|
|
@ -80,7 +77,7 @@
|
||||||
throw new Exception("Failed to create database entity");
|
throw new Exception("Failed to create database entity");
|
||||||
}
|
}
|
||||||
|
|
||||||
return new static($id);
|
return self::from_name($title);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function __construct(public readonly int $id) {
|
public function __construct(public readonly int $id) {
|
||||||
|
|
@ -91,6 +88,48 @@
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get post meta fields for this Post. An array of all post meta is returned if no $key is provided
|
||||||
|
*
|
||||||
|
* @param string|null $key Return the value of this post meta key. Null for all post meta fields
|
||||||
|
* @return string|array|null Returns a string or null if a single key is selected. Array if all post meta fields are returned
|
||||||
|
*/
|
||||||
|
public function meta(?string $key = null): string|array|null {
|
||||||
|
return $key ? PostMeta::get_post_meta($this, $key) : PostMeta::get_all_post_meta($this);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns an array of all Terms assigned to this Post
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function terms(): array {
|
||||||
|
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);
|
||||||
|
|
@ -123,27 +162,27 @@
|
||||||
|
|
||||||
public string $post_status {
|
public string $post_status {
|
||||||
get => $this->get(Posts::POST_STATUS->value);
|
get => $this->get(Posts::POST_STATUS->value);
|
||||||
set (string $post_status) => $this->set(Posts::POST_STATUS->value, substr($post_status, 0, 20));
|
set (string $post_status) => $this->set(Posts::POST_STATUS->value, $post_status);
|
||||||
}
|
}
|
||||||
|
|
||||||
public string $comment_status {
|
public string $comment_status {
|
||||||
get => $this->get(Posts::COMMENT_STATUS->value);
|
get => $this->get(Posts::COMMENT_STATUS->value);
|
||||||
set (string $comment_status) => $this->set(Posts::COMMENT_STATUS->value, substr($comment_status, 0, 20));
|
set (string $comment_status) => $this->set(Posts::COMMENT_STATUS->value, $comment_status);
|
||||||
}
|
}
|
||||||
|
|
||||||
public string $ping_status {
|
public string $ping_status {
|
||||||
get => $this->get(Posts::PING_STATUS->value);
|
get => $this->get(Posts::PING_STATUS->value);
|
||||||
set (string $ping_status) => $this->set(Posts::PING_STATUS->value, substr($ping_status, 0, 20));
|
set (string $ping_status) => $this->set(Posts::PING_STATUS->value, $ping_status);
|
||||||
}
|
}
|
||||||
|
|
||||||
public string $post_password {
|
public string $post_password {
|
||||||
get => $this->get(Posts::POST_PASSWORD->value);
|
get => $this->get(Posts::POST_PASSWORD->value);
|
||||||
set (string $post_password) => $this->set(Posts::POST_PASSWORD->value, substr($post_password, 0, 255));
|
set (string $post_password) => $this->set(Posts::POST_PASSWORD->value, $post_password);
|
||||||
}
|
}
|
||||||
|
|
||||||
public string $post_name {
|
public string $post_name {
|
||||||
get => $this->get(Posts::POST_NAME->value);
|
get => $this->get(Posts::POST_NAME->value);
|
||||||
set (string $post_name) => $this->set(Posts::POST_NAME->value, substr($post_name, 0, 200));
|
set (string $post_name) => $this->set(Posts::POST_NAME->value, $post_name);
|
||||||
}
|
}
|
||||||
|
|
||||||
public string $to_ping {
|
public string $to_ping {
|
||||||
|
|
@ -183,78 +222,16 @@
|
||||||
|
|
||||||
public string $post_type {
|
public string $post_type {
|
||||||
get => $this->get(Posts::POST_TYPE->value);
|
get => $this->get(Posts::POST_TYPE->value);
|
||||||
set (string $post_type) => $this->set(Posts::POST_TYPE->value, substr($post_type, 0, 20));
|
set (string $post_type) => $this->set(Posts::POST_TYPE->value, $post_type);
|
||||||
}
|
}
|
||||||
|
|
||||||
public string $post_mime_type {
|
public string $post_mime_type {
|
||||||
get => $this->get(Posts::POST_MIME_TYPE->value);
|
get => $this->get(Posts::POST_MIME_TYPE->value);
|
||||||
set (string $post_mime_type) => $this->set(Posts::POST_MIME_TYPE->value, substr($post_mime_type, 0, 100));
|
set (string $post_mime_type) => $this->set(Posts::POST_MIME_TYPE->value, $post_mime_type);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int $comment_count {
|
public int $comment_count {
|
||||||
get => $this->get(Posts::COMMENT_COUNT->value);
|
get => $this->get(Posts::COMMENT_COUNT->value);
|
||||||
set (int $comment_count) => $this->set(Posts::COMMENT_COUNT->value, $comment_count);
|
set (int $comment_count) => $this->set(Posts::COMMENT_COUNT->value, $comment_count);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Get post meta fields for this Post. An array of all post meta is returned if no $key is provided
|
|
||||||
*
|
|
||||||
* @param string|null $key Return the value of this post meta key. Null for all post meta fields
|
|
||||||
* @return string|array|null Returns a string or null if a single key is selected. Array if all post meta fields are returned
|
|
||||||
*/
|
|
||||||
public function meta(?string $key = null): string|array|null {
|
|
||||||
return $key ? PostMeta::get_post_meta($this, $key) : PostMeta::get_all_post_meta($this);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns an array of all Terms assigned to this Post
|
|
||||||
*
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function terms(): array {
|
|
||||||
return Term::from_post($this);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Add a Term to this Post
|
|
||||||
*
|
|
||||||
* @param Term $term
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function add_term(Term $term): void {
|
|
||||||
$term->add_to_post($this);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Remove a Term from this Post
|
|
||||||
*
|
|
||||||
* @param Term $term
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function remove_term(Term $term): void {
|
|
||||||
$term->remove_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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -64,9 +64,6 @@
|
||||||
return $model;
|
return $model;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Current auto increment value will be the id for this entity
|
|
||||||
$id = Database::current()->latest(PostMetaTable::TABLE_NAME);
|
|
||||||
|
|
||||||
$values = [
|
$values = [
|
||||||
PostMetaTable::META_ID->value => null,
|
PostMetaTable::META_ID->value => null,
|
||||||
PostMetaTable::POST_ID->value => $post->id,
|
PostMetaTable::POST_ID->value => $post->id,
|
||||||
|
|
@ -78,7 +75,7 @@
|
||||||
throw new Exception("Failed to create database entity");
|
throw new Exception("Failed to create database entity");
|
||||||
}
|
}
|
||||||
|
|
||||||
return new static($id);
|
return new static(self::get_post_meta($post, $meta_key)->id);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function __construct(public readonly int $id) {
|
public function __construct(public readonly int $id) {
|
||||||
|
|
@ -89,21 +86,6 @@
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Post $post {
|
|
||||||
get => new Post($this->get(PostMetaTable::POST_ID->value));
|
|
||||||
set (Post $post) => $this->set(PostMetaTable::POST_ID->value, $post->id);
|
|
||||||
}
|
|
||||||
|
|
||||||
public string $meta_key {
|
|
||||||
get => $this->get(PostMetaTable::META_KEY->value);
|
|
||||||
set (string $meta_key) => $this->set(PostMetaTable::META_KEY->value, substr($meta_key, 0, 255));
|
|
||||||
}
|
|
||||||
|
|
||||||
public ?string $meta_value {
|
|
||||||
get => $this->get(PostMetaTable::META_VALUE->value);
|
|
||||||
set (?string $meta_value) => $this->set(PostMetaTable::META_VALUE->value, $meta_value);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Delete this post meta field from the database
|
* Delete this post meta field from the database
|
||||||
*
|
*
|
||||||
|
|
@ -111,7 +93,22 @@
|
||||||
*/
|
*/
|
||||||
public function delete(): void {
|
public function delete(): void {
|
||||||
$this->db
|
$this->db
|
||||||
->from(Database::get_table(PostMetaTable::TABLE_NAME))
|
->from(PostMetaTable::TABLE_NAME)
|
||||||
->delete([PostMetaTable::META_ID->value => $this->id]);
|
->delete([PostMetaTable::META_ID->value => $this->id]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Post $post {
|
||||||
|
get => new Post($this->get(PostMetaTable::POST_ID->value));
|
||||||
|
set (Post $post) => $this->set(PostMetaTable::POST_ID->value, $post->id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public string $meta_key {
|
||||||
|
get => $this->get(PostMetaTable::META_KEY->value);
|
||||||
|
set (string $meta_key) => $this->set(PostMetaTable::META_KEY->value, $meta_key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ?string $meta_value {
|
||||||
|
get => $this->get(PostMetaTable::META_VALUE->value);
|
||||||
|
set (?string $meta_value) => $this->set(PostMetaTable::META_VALUE->value, $meta_value);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,9 +7,7 @@
|
||||||
|
|
||||||
use vlw\WP\Database;
|
use vlw\WP\Database;
|
||||||
use vlw\WP\Tables\Taxonomies;
|
use vlw\WP\Tables\Taxonomies;
|
||||||
use vlw\WP\Posts\Taxonomy\Term;
|
|
||||||
|
|
||||||
require_once "Term.php";
|
|
||||||
require_once dirname(__DIR__, 2) . "/Tables/Taxonomies.php";
|
require_once dirname(__DIR__, 2) . "/Tables/Taxonomies.php";
|
||||||
|
|
||||||
class Taxonomy extends Model {
|
class Taxonomy extends Model {
|
||||||
|
|
@ -17,24 +15,16 @@
|
||||||
* Returns a Taxonomy by taxonomy name
|
* Returns a Taxonomy by taxonomy name
|
||||||
*
|
*
|
||||||
* @param string $name
|
* @param string $name
|
||||||
* @return array
|
* @return static|null
|
||||||
*/
|
*/
|
||||||
public static function from(string $taxonomy): array {
|
public function from(string $taxonomy): ?static {
|
||||||
$query = Database::current()
|
$query = Database::current()
|
||||||
->from(Database::get_table(Taxonomies::TABLE_NAME))
|
->from(Database::get_table(Taxonomies::TABLE_NAME))
|
||||||
->where([Taxonomies::TAXONOMY->value => $taxonomy])
|
->where([Taxonomies::TAXONOMY->value => $taxonomy])
|
||||||
|
->limit(1)
|
||||||
->select(Taxonomies::TERM_TAXONOMY_ID->value);
|
->select(Taxonomies::TERM_TAXONOMY_ID->value);
|
||||||
|
|
||||||
return array_map(fn(array $taxonomy): static => new static($taxonomy[Taxonomies::TERM_TAXONOMY_ID->value]), $query->fetch_all(MYSQLI_ASSOC));
|
return $query->num_rows === 1 ? new static($query->fetch_assoc()[Taxonomies::TERM_ID->value]) : null;
|
||||||
}
|
|
||||||
|
|
||||||
public static function from_term(Term $term): array {
|
|
||||||
$query = Database::current()
|
|
||||||
->from(Database::get_table(Taxonomies::TABLE_NAME))
|
|
||||||
->where([Taxonomies::TERM_ID->value => $term->id])
|
|
||||||
->select(Taxonomies::TERM_TAXONOMY_ID->value);
|
|
||||||
|
|
||||||
return array_map(fn(array $taxonomy): static => new static($taxonomy[Taxonomies::TERM_TAXONOMY_ID->value]), $query->fetch_all(MYSQLI_ASSOC));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -44,14 +34,15 @@
|
||||||
* @param string $type Post type
|
* @param string $type Post type
|
||||||
* @return static
|
* @return static
|
||||||
*/
|
*/
|
||||||
public static function new(string $taxonomy, Term $term): static {
|
public static function new(string $taxonomy): static {
|
||||||
// Current auto increment value will be the id for this entity
|
// Return existing instance of Taxonomy if it exists
|
||||||
$id = Database::current()->latest(Taxonomies::TABLE_NAME);
|
if (self::from($taxonomy)) {
|
||||||
|
return self::from($taxonomy);
|
||||||
|
}
|
||||||
|
|
||||||
$values = [
|
$values = [
|
||||||
Taxonomies::TERM_TAXONOMY_ID->value => null,
|
Taxonomies::TERM_TAXONOMY_ID->value => null,
|
||||||
Taxonomies::TERM_ID->value => $term->id,
|
Taxonomies::TERM_ID->value => 0,
|
||||||
Taxonomies::TAXONOMY->value => substr($taxonomy, 0, 32),
|
|
||||||
Taxonomies::DESCRIPTION->value => "",
|
Taxonomies::DESCRIPTION->value => "",
|
||||||
Taxonomies::PARENT->value => 0,
|
Taxonomies::PARENT->value => 0,
|
||||||
Taxonomies::COUNT->value => 0
|
Taxonomies::COUNT->value => 0
|
||||||
|
|
@ -61,8 +52,7 @@
|
||||||
throw new Exception("Failed to create database entity");
|
throw new Exception("Failed to create database entity");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Instance this Taxonomy from the last added taxonomy in the database
|
return self::from($taxonomy);
|
||||||
return new static($id);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function __construct(public readonly int $id) {
|
public function __construct(public readonly int $id) {
|
||||||
|
|
@ -80,7 +70,7 @@
|
||||||
|
|
||||||
public string $taxonomy {
|
public string $taxonomy {
|
||||||
get => $this->get(Taxonomies::TAXONOMY->value);
|
get => $this->get(Taxonomies::TAXONOMY->value);
|
||||||
set (string $taxonomy) => $this->set(Taxonomies::TAXONOMY->value, substr($taxonomy, 0, 32));
|
set (string $taxonomy) => $this->set(Taxonomies::TAXONOMY->value, $taxonomy);
|
||||||
}
|
}
|
||||||
|
|
||||||
public string $description {
|
public string $description {
|
||||||
|
|
@ -92,9 +82,4 @@
|
||||||
get => $this->get(Taxonomies::PARENT->value) ? new self($this->get(Taxonomies::PARENT->value)) : null;
|
get => $this->get(Taxonomies::PARENT->value) ? new self($this->get(Taxonomies::PARENT->value)) : null;
|
||||||
set (?self $parent) => $this->set(Taxonomies::PARENT->value, $parent->id);
|
set (?self $parent) => $this->set(Taxonomies::PARENT->value, $parent->id);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int $count {
|
|
||||||
get => (int) $this->get(Taxonomies::COUNT->value);
|
|
||||||
set (int $count) => $this->set(Taxonomies::COUNT->value, $count);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,11 +10,9 @@
|
||||||
use vlw\WP\Tables\Terms;
|
use vlw\WP\Tables\Terms;
|
||||||
use vlw\WP\Posts\Taxonomy\Taxonomy;
|
use vlw\WP\Posts\Taxonomy\Taxonomy;
|
||||||
use vlw\WP\Tables\TermRelationships;
|
use vlw\WP\Tables\TermRelationships;
|
||||||
use function vlw\WP\Support\slugify;
|
|
||||||
|
|
||||||
require_once dirname(__DIR__, 1) . "/Post.php";
|
require_once dirname(__DIR__, 1) . "/Post.php";
|
||||||
require_once dirname(__DIR__, 2) . "/Tables/Terms.php";
|
require_once dirname(__DIR__, 2) . "/Tables/Terms.php";
|
||||||
require_once dirname(__DIR__, 2) . "/Support/Slugify.php";
|
|
||||||
require_once dirname(__DIR__, 2) . "/Tables/TermRelationships.php";
|
require_once dirname(__DIR__, 2) . "/Tables/TermRelationships.php";
|
||||||
|
|
||||||
class Term extends Model {
|
class Term extends Model {
|
||||||
|
|
@ -57,13 +55,18 @@
|
||||||
* @return static
|
* @return static
|
||||||
*/
|
*/
|
||||||
public static function new(string $name, ?string $slug = null): static {
|
public static function new(string $name, ?string $slug = null): static {
|
||||||
// Current auto increment value will be the id for this entity
|
// Update and return meta key for existing id
|
||||||
$id = Database::current()->latest(Terms::TABLE_NAME);
|
if (self::from_name($name)) {
|
||||||
|
$model = self::from_name($name);
|
||||||
|
$model->slug = $slug;
|
||||||
|
|
||||||
|
return $model;
|
||||||
|
}
|
||||||
|
|
||||||
$values = [
|
$values = [
|
||||||
Terms::TERM_ID->value => null,
|
Terms::TERM_ID->value => null,
|
||||||
Terms::NAME->value => substr($name, 0, 200),
|
Terms::NAME->value => $name,
|
||||||
Terms::SLUG->value => substr($slug ? $slug : slugify($name), 0, 200),
|
Terms::SLUG->value => $slug,
|
||||||
Terms::TERM_GROUP->value => 0
|
Terms::TERM_GROUP->value => 0
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|
@ -71,7 +74,7 @@
|
||||||
throw new Exception("Failed to create database entity");
|
throw new Exception("Failed to create database entity");
|
||||||
}
|
}
|
||||||
|
|
||||||
return new static($id);
|
return self::from_name($name);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function __construct(public readonly int $id) {
|
public function __construct(public readonly int $id) {
|
||||||
|
|
@ -82,28 +85,13 @@
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public string $name {
|
|
||||||
get => $this->get(Terms::NAME->value);
|
|
||||||
set (string $name) => $this->set(Terms::NAME->value, substr($name, 0, 200));
|
|
||||||
}
|
|
||||||
|
|
||||||
public string $slug {
|
|
||||||
get => $this->get(Terms::SLUG->value);
|
|
||||||
set (string $slug) => $this->set(Terms::SLUG->value, substr($slug, 0, 200));
|
|
||||||
}
|
|
||||||
|
|
||||||
public int $term_group {
|
|
||||||
get => $this->get(Terms::TERM_GROUP->value);
|
|
||||||
set (int $term_group) => $this->set(Terms::TERM_GROUP->value, $term_group);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns an array of Taxonomies that implement this Term
|
* Returns the corresponding Taxonomy for this Term
|
||||||
*
|
*
|
||||||
* @return array
|
* @return Taxonomy
|
||||||
*/
|
*/
|
||||||
public function taxonomies(): array {
|
public function taxonomy(): Taxonomy {
|
||||||
return Taxonomy::from_term($this);
|
return new Taxonomy($this->id);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -114,7 +102,7 @@
|
||||||
*/
|
*/
|
||||||
public function add_to_post(Post $post): void {
|
public function add_to_post(Post $post): void {
|
||||||
// Bail out if this term has already been added to the target Post
|
// Bail out if this term has already been added to the target Post
|
||||||
if ($this->post_has_term($post)) {
|
if (self::from_post($post)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -129,11 +117,6 @@
|
||||||
if ($query === false) {
|
if ($query === false) {
|
||||||
throw new Exception("Failed to create database entity");
|
throw new Exception("Failed to create database entity");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Increase the post count for each Taxonomy implementing this Term
|
|
||||||
foreach ($this->taxonomies() as $taxonomy) {
|
|
||||||
$taxonomy->count++;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -143,11 +126,6 @@
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function remove_from_post(Post $post): void {
|
public function remove_from_post(Post $post): void {
|
||||||
// Bail out if this Term is not set on the target Post
|
|
||||||
if (!$this->post_has_term($post)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
$query = Database::current()
|
$query = Database::current()
|
||||||
->from(Database::get_table(TermRelationships::TABLE_NAME))
|
->from(Database::get_table(TermRelationships::TABLE_NAME))
|
||||||
->delete([
|
->delete([
|
||||||
|
|
@ -158,20 +136,20 @@
|
||||||
if ($query === false) {
|
if ($query === false) {
|
||||||
throw new Exception("Failed to create database entity");
|
throw new Exception("Failed to create database entity");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Decrease the post count for each Taxonomy implementing this Term
|
|
||||||
foreach ($this->taxonomies() as $taxonomy) {
|
|
||||||
$taxonomy->count--;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public string $name {
|
||||||
* Returns true if a given Post has this Term
|
get => $this->get(Terms::NAME->value);
|
||||||
*
|
set (string $name) => $this->set(Terms::NAME->value, $name);
|
||||||
* @param Post $post
|
}
|
||||||
* @return bool
|
|
||||||
*/
|
public string $slug {
|
||||||
private function post_has_term(Post $post): bool {
|
get => $this->get(Terms::SLUG->value);
|
||||||
return in_array($this->id, array_column(self::from_post($post), "id"));
|
set (string $slug) => $this->set(Terms::SLUG->value, $slug);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int $term_group {
|
||||||
|
get => $this->get(Terms::TERM_GROUP->value);
|
||||||
|
set (int $term_group) => $this->set(Terms::TERM_GROUP->value, $term_group);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue