Post.php
1.81 KB
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
86
87
88
89
90
91
92
93
<?php
namespace ACP\Search\Comparison\Meta;
use AC;
use ACP\Helper\Select;
use ACP\Helper\Select\Formatter;
use ACP\Search\Comparison\Meta;
use ACP\Search\Comparison\SearchableValues;
use ACP\Search\Operators;
use ACP\Search\Value;
use WP_Term;
class Post extends Meta
implements SearchableValues {
/**
* @var string|array
*/
private $post_type = 'any';
/**
* @var WP_Term[]
*/
private $terms = [];
public function __construct( $meta_key, $meta_type, $post_type = false, array $terms = [], $labels = null ) {
$this->set_post_type( $post_type );
$this->set_terms( $terms );
parent::__construct( $this->get_meta_operators(), $meta_key, $meta_type, Value::STRING, $labels );
}
protected function get_meta_operators() {
return new Operators( [
Operators::EQ,
Operators::NEQ,
Operators::IS_EMPTY,
Operators::NOT_IS_EMPTY,
] );
}
public function get_values( $search, $page ) {
$entities = new Select\Entities\Post( [
's' => $search,
'paged' => $page,
'post_type' => $this->post_type,
'tax_query' => $this->get_tax_query(),
'search_fields' => [ 'post_title', 'ID' ],
] );
return new AC\Helper\Select\Options\Paginated(
$entities,
new Select\Group\PostType(
new Formatter\PostTitle( $entities )
)
);
}
/**
* @param string|array $post_type
*/
private function set_post_type( $post_type ) {
if ( $post_type ) {
$this->post_type = $post_type;
}
}
/**
* @param WP_Term[] $terms
*/
private function set_terms( array $terms ) {
$this->terms = $terms;
}
/**
* @return array
*/
protected function get_tax_query() {
$tax_query = [];
foreach ( $this->terms as $term ) {
$tax_query[] = [
'taxonomy' => $term->taxonomy,
'field' => 'slug',
'terms' => $term->slug,
];
}
return $tax_query;
}
}