wip: 2026-02-13T14:33:59+0100 (1770989639)

This commit is contained in:
Victor Westerlund 2026-02-13 14:33:59 +01:00
parent a49ab0aed9
commit d8cebde6a4
Signed by: vlw
GPG key ID: D0AD730E1057DFC6
3 changed files with 65 additions and 17 deletions

View file

@ -9,8 +9,10 @@
use vlw\WP\Database;
use vlw\WP\Tables\Posts;
use vlw\WP\Posts\PostMeta;
use vlw\WP\Posts\Taxonomy\Term;
use function vlw\WP\Support\slugify;
require_once "Taxonomy/Term.php";
require_once dirname(__DIR__, 1) . "/Tables/Posts.php";
require_once dirname(__DIR__, 1) . "/Support/Slugify.php";
@ -96,13 +98,12 @@
}
/**
* Get post meta fields for this Post. An array of all post meta is returned if no $key is provided
* Returns an array of all Terms assigned to this Post
*
* @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
* @return array
*/
public function taxonomies(?string $key = null): string|array|null {
return $key ? PostMeta::get_post_meta($this, $key) : PostMeta::get_all_post_meta($this);
public function terms(): array {
return Term::from_post($this);
}
public int $post_author {

View file

@ -1,16 +1,14 @@
<?php
namespace vlw\WP\Taxonomy;
namespace vlw\WP\Posts\Taxonomy;
use Exception;
use vlw\Scaffold\Database\Model;
use vlw\WP\Database;
use vlw\WP\Posts\Post;
use vlw\WP\Tables\Taxonomies;
require_once "Post.php";
require_once dirname(__DIR__, 1) . "/Tables/Taxonomies.php";
require_once dirname(__DIR__, 2) . "/Tables/Taxonomies.php";
class Taxonomy extends Model {
/**

View file

@ -1,6 +1,6 @@
<?php
namespace vlw\WP\Taxonomy;
namespace vlw\WP\Posts\Taxonomy;
use Exception;
use vlw\Scaffold\Database\Model;
@ -8,12 +8,12 @@
use vlw\WP\Database;
use vlw\WP\Posts\Post;
use vlw\WP\Tables\Terms;
use vlw\WP\Taxonomy\Taxonomy;
use vlw\WP\Posts\Taxonomy\Taxonomy;
use vlw\WP\Tables\TermRelationships;
require_once "Post.php";
require_once dirname(__DIR__, 1) . "/Tables/Terms.php";
require_once dirname(__DIR__, 1) . "/Tables/TermRelationships.php";
require_once dirname(__DIR__, 1) . "/Post.php";
require_once dirname(__DIR__, 2) . "/Tables/Terms.php";
require_once dirname(__DIR__, 2) . "/Tables/TermRelationships.php";
class Term extends Model {
/**
@ -22,7 +22,7 @@
* @param string $name
* @return static|null
*/
public function from_name(string $name): ?static {
public static function from_name(string $name): ?static {
$query = Database::current()
->from(Database::get_table(Terms::TABLE_NAME))
->where([Terms::NAME->value => $name])
@ -36,9 +36,9 @@
* Returns an array of all Terms associated with a Post
*
* @param Post $post
* @return void
* @return array
*/
public function from_post(Post $post) {
public static function from_post(Post $post): array {
$query = Database::current()
->from(Database::get_table(TermRelationships::TABLE_NAME))
->where([TermRelationships::OBJECT_ID->value => $post->id])
@ -85,10 +85,59 @@
);
}
/**
* Returns the corresponding Taxonomy for this Term
*
* @return Taxonomy
*/
public function taxonomy(): Taxonomy {
return new Taxonomy($this->id);
}
/**
* Add this Term to a target Post
*
* @param Post $post
* @return void
*/
public function add_to_post(Post $post): void {
// Bail out if this term has already been added to the target Post
if (self::from_post($post)) {
return;
}
$query = Database::current()
->from(Database::get_table(TermRelationships::TABLE_NAME))
->insert([
TermRelationships::OBJECT_ID->value => $post->id,
TermRelationships::TERM_TAXONOMY_ID->value => $this->id,
TermRelationships::TERM_ORDER->value => 0
]);
if ($query === false) {
throw new Exception("Failed to create database entity");
}
}
/**
* Remove this Term from a target Post
*
* @param Post $post
* @return void
*/
public function remove_from_post(Post $post): void {
$query = Database::current()
->from(Database::get_table(TermRelationships::TABLE_NAME))
->delete([
TermRelationships::OBJECT_ID->value => $post->id,
TermRelationships::TERM_TAXONOMY_ID->value => $this->id
]);
if ($query === false) {
throw new Exception("Failed to create database entity");
}
}
public string $name {
get => $this->get(Terms::NAME->value);
set (string $name) => $this->set(Terms::NAME->value, $name);