mirror of
https://codeberg.org/vlw/wp.git
synced 2026-02-26 03:51:58 +01:00
wip: 2026-02-13T14:33:59+0100 (1770989639)
This commit is contained in:
parent
a49ab0aed9
commit
d8cebde6a4
3 changed files with 65 additions and 17 deletions
|
|
@ -9,8 +9,10 @@
|
||||||
use vlw\WP\Database;
|
use vlw\WP\Database;
|
||||||
use vlw\WP\Tables\Posts;
|
use vlw\WP\Tables\Posts;
|
||||||
use vlw\WP\Posts\PostMeta;
|
use vlw\WP\Posts\PostMeta;
|
||||||
|
use vlw\WP\Posts\Taxonomy\Term;
|
||||||
use function vlw\WP\Support\slugify;
|
use function vlw\WP\Support\slugify;
|
||||||
|
|
||||||
|
require_once "Taxonomy/Term.php";
|
||||||
require_once dirname(__DIR__, 1) . "/Tables/Posts.php";
|
require_once dirname(__DIR__, 1) . "/Tables/Posts.php";
|
||||||
require_once dirname(__DIR__, 1) . "/Support/Slugify.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 array
|
||||||
* @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 taxonomies(?string $key = null): string|array|null {
|
public function terms(): array {
|
||||||
return $key ? PostMeta::get_post_meta($this, $key) : PostMeta::get_all_post_meta($this);
|
return Term::from_post($this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int $post_author {
|
public int $post_author {
|
||||||
|
|
|
||||||
|
|
@ -1,16 +1,14 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace vlw\WP\Taxonomy;
|
namespace vlw\WP\Posts\Taxonomy;
|
||||||
|
|
||||||
use Exception;
|
use Exception;
|
||||||
use vlw\Scaffold\Database\Model;
|
use vlw\Scaffold\Database\Model;
|
||||||
|
|
||||||
use vlw\WP\Database;
|
use vlw\WP\Database;
|
||||||
use vlw\WP\Posts\Post;
|
|
||||||
use vlw\WP\Tables\Taxonomies;
|
use vlw\WP\Tables\Taxonomies;
|
||||||
|
|
||||||
require_once "Post.php";
|
require_once dirname(__DIR__, 2) . "/Tables/Taxonomies.php";
|
||||||
require_once dirname(__DIR__, 1) . "/Tables/Taxonomies.php";
|
|
||||||
|
|
||||||
class Taxonomy extends Model {
|
class Taxonomy extends Model {
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace vlw\WP\Taxonomy;
|
namespace vlw\WP\Posts\Taxonomy;
|
||||||
|
|
||||||
use Exception;
|
use Exception;
|
||||||
use vlw\Scaffold\Database\Model;
|
use vlw\Scaffold\Database\Model;
|
||||||
|
|
@ -8,12 +8,12 @@
|
||||||
use vlw\WP\Database;
|
use vlw\WP\Database;
|
||||||
use vlw\WP\Posts\Post;
|
use vlw\WP\Posts\Post;
|
||||||
use vlw\WP\Tables\Terms;
|
use vlw\WP\Tables\Terms;
|
||||||
use vlw\WP\Taxonomy\Taxonomy;
|
use vlw\WP\Posts\Taxonomy\Taxonomy;
|
||||||
use vlw\WP\Tables\TermRelationships;
|
use vlw\WP\Tables\TermRelationships;
|
||||||
|
|
||||||
require_once "Post.php";
|
require_once dirname(__DIR__, 1) . "/Post.php";
|
||||||
require_once dirname(__DIR__, 1) . "/Tables/Terms.php";
|
require_once dirname(__DIR__, 2) . "/Tables/Terms.php";
|
||||||
require_once dirname(__DIR__, 1) . "/Tables/TermRelationships.php";
|
require_once dirname(__DIR__, 2) . "/Tables/TermRelationships.php";
|
||||||
|
|
||||||
class Term extends Model {
|
class Term extends Model {
|
||||||
/**
|
/**
|
||||||
|
|
@ -22,7 +22,7 @@
|
||||||
* @param string $name
|
* @param string $name
|
||||||
* @return static|null
|
* @return static|null
|
||||||
*/
|
*/
|
||||||
public function from_name(string $name): ?static {
|
public static function from_name(string $name): ?static {
|
||||||
$query = Database::current()
|
$query = Database::current()
|
||||||
->from(Database::get_table(Terms::TABLE_NAME))
|
->from(Database::get_table(Terms::TABLE_NAME))
|
||||||
->where([Terms::NAME->value => $name])
|
->where([Terms::NAME->value => $name])
|
||||||
|
|
@ -36,9 +36,9 @@
|
||||||
* Returns an array of all Terms associated with a Post
|
* Returns an array of all Terms associated with a Post
|
||||||
*
|
*
|
||||||
* @param Post $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()
|
$query = Database::current()
|
||||||
->from(Database::get_table(TermRelationships::TABLE_NAME))
|
->from(Database::get_table(TermRelationships::TABLE_NAME))
|
||||||
->where([TermRelationships::OBJECT_ID->value => $post->id])
|
->where([TermRelationships::OBJECT_ID->value => $post->id])
|
||||||
|
|
@ -85,10 +85,59 @@
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the corresponding Taxonomy for this Term
|
||||||
|
*
|
||||||
|
* @return Taxonomy
|
||||||
|
*/
|
||||||
public function taxonomy(): Taxonomy {
|
public function taxonomy(): Taxonomy {
|
||||||
return new Taxonomy($this->id);
|
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 {
|
public string $name {
|
||||||
get => $this->get(Terms::NAME->value);
|
get => $this->get(Terms::NAME->value);
|
||||||
set (string $name) => $this->set(Terms::NAME->value, $name);
|
set (string $name) => $this->set(Terms::NAME->value, $name);
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue