Skip to content

Commit a547f51

Browse files
committed
Add Term and Category classes for dealing with taxonomies
1 parent 2f7722d commit a547f51

File tree

4 files changed

+80
-3
lines changed

4 files changed

+80
-3
lines changed

src/PostTypes/Post.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
class Post
88
{
9-
public $id;
9+
public int $id;
1010

1111
protected \WP_Post $WP_Post;
1212

@@ -22,7 +22,7 @@ class Post
2222
public \WP_Post $parent;
2323
public string $thumbnailHtml = '';
2424

25-
public function __construct(int $id = 0)
25+
public function __construct(int $id)
2626
{
2727
$this->id = $id;
2828

src/Terms/Category.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace OOPWP\Terms;
4+
5+
class Category extends Term
6+
{
7+
public string $taxonomyName = 'category';
8+
9+
public function __construct(int $id)
10+
{
11+
parent::__construct($id, $this->taxonomyName);
12+
}
13+
}

src/Terms/Term.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
namespace OOPWP\Terms;
4+
5+
class Term
6+
{
7+
public int $id;
8+
9+
public string $taxonomy;
10+
11+
protected \WP_Term $WP_Term;
12+
13+
public string $name = '';
14+
public string $slug = '';
15+
public string $description = '';
16+
public self $parent;
17+
18+
public function __construct(int $id, string $taxonomy)
19+
{
20+
$this->id = $id;
21+
$this->taxonomy = $taxonomy;
22+
23+
$this->WP_Term = \get_term($this->id, $this->taxonomy);
24+
}
25+
26+
public function withAll()
27+
{
28+
$this
29+
->withName()
30+
->withSlug()
31+
->withDescription()
32+
->withParent();
33+
34+
return $this;
35+
}
36+
37+
public function withName()
38+
{
39+
$this->name = $this->WP_Term->name;
40+
41+
return $this;
42+
}
43+
44+
public function withSlug()
45+
{
46+
$this->slug = $this->WP_Term->slug;
47+
48+
return $this;
49+
}
50+
51+
public function withDescription()
52+
{
53+
$this->description = $this->WP_Term->description;
54+
55+
return $this;
56+
}
57+
58+
public function withParent()
59+
{
60+
$this->parent = new self($this->WP_Term->parent, $this->taxonomy);
61+
62+
return $this;
63+
}
64+
}

src/User.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class User
2222
public string $nickname = '';
2323
public string $description = '';
2424

25-
public function __construct(int $id = 0)
25+
public function __construct(int $id)
2626
{
2727
$this->id = $id;
2828

0 commit comments

Comments
 (0)