-
Notifications
You must be signed in to change notification settings - Fork 1
/
Base.php
85 lines (65 loc) · 1.74 KB
/
Base.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
namespace Expresser\Taxonomy;
use Exception;
use Expresser\Support\Model;
use WP_Term;
use WP_Term_Query;
abstract class Base extends Model
{
public $taxonomy;
protected $term;
public function __construct(WP_Term $term = null)
{
$this->term = $term ?: new WP_Term((object) [
'taxonomy' => $this->taxonomy,
]);
parent::__construct($this->term->to_array());
}
public function newQuery()
{
$query = (new Builder(new Query(new WP_Term_Query)))->setModel($this);
return $query->taxonomy($this->taxonomy)->hideEmpty(false);
}
public function getCacheableAccessors()
{
return array_merge(parent::getCacheableAccessors(), [
'permalink',
]);
}
public function getIdAttribute($value)
{
return (int)$value;
}
public function getPermalinkAttribute()
{
$permalink = get_term_link($this->term_id, $this->taxonomy);
if (!is_wp_error($permalink)) {
return $permalink;
}
}
public function getUrlAttribute()
{
return $this->permalink;
}
public static function getTaxonomy()
{
static $taxonomy;
if (is_null($taxonomy)) {
$term = new static;
$taxonomy = $term->taxonomy;
}
return $taxonomy;
}
public static function in(array $ids)
{
return count(array_intersect($ids, self::get()->lists('term_id'))) > 0;
}
public static function registerHooks($class)
{
add_action('init', [$class, 'registerTaxonomy'], -PHP_INT_MAX);
}
public static function registerTaxonomy()
{
throw new Exception('A new taxonomy must override registerTaxonomy.');
}
}