diff --git a/src/Posts/Post.php b/src/Posts/Post.php index a64b7ca..43ffd91 100644 --- a/src/Posts/Post.php +++ b/src/Posts/Post.php @@ -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 { diff --git a/src/Posts/Taxonomy/Taxonomy.php b/src/Posts/Taxonomy/Taxonomy.php index bd0c7c9..548fac6 100644 --- a/src/Posts/Taxonomy/Taxonomy.php +++ b/src/Posts/Taxonomy/Taxonomy.php @@ -1,16 +1,14 @@ 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);