PostVisibility.php
1.5 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
<?php
namespace ACP\Search\Comparison\Post;
use AC;
use ACP\Search\Comparison;
use ACP\Search\Helper\Sql\ComparisonFactory;
use ACP\Search\Operators;
use ACP\Search\Query\Bindings;
use ACP\Search\Value;
class PostVisibility extends Comparison
implements Comparison\Values {
public function __construct() {
$operators = new Operators( [
Operators::EQ,
] );
parent::__construct( $operators );
}
protected function create_query_bindings( $operator, Value $value ) {
global $wpdb;
$binding = new Bindings();
switch ( $value->get_value() ) {
case 'private':
$value = new Value( 'private', Value::STRING );
$binding->where(
$this->create_where( 'post_status', $operator, $value )
);
break;
case 'protected':
$value = new Value( null, Value::STRING );
$binding->where(
$this->create_where( 'post_password', Operators::NOT_IS_EMPTY, $value )
);
break;
case 'public':
$binding->where( "(
{$wpdb->posts}.post_password = '' AND
{$wpdb->posts}.post_status != 'private'
)" );
break;
}
return $binding;
}
private function create_where( $field, $operator, $value ) {
global $wpdb;
return ComparisonFactory::create(
$wpdb->posts . '.' . $field,
$operator,
$value
)->prepare();
}
public function get_values() {
return AC\Helper\Select\Options::create_from_array( [
'private' => _x( 'Private', 'post status' ),
'protected' => _x( 'Password protected', 'post status' ),
'public' => __( 'Public' ),
] );
}
}