1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* From Bulk Delete v6.1.0, the minimum required PHP version is v5.6 |
4
|
|
|
* This opens the possibility of using Traits. |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace BulkWP\BulkDelete\Core\Base\Mixin; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Operator related helpers. |
11
|
|
|
* |
12
|
|
|
* Used in Renderer. |
13
|
|
|
* |
14
|
|
|
* @since 6.1.0 |
15
|
|
|
*/ |
16
|
|
|
trait OperatorHelpers { |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Get the master list of operators. |
20
|
|
|
* |
21
|
|
|
* The operators are cached using static variable for performance. |
22
|
|
|
* |
23
|
|
|
* @return array Master list of operators. |
24
|
|
|
*/ |
25
|
|
|
protected function get_operator_master_list() { |
26
|
|
|
static $operator_with_labels = []; |
27
|
|
|
|
28
|
|
|
if ( empty( $operator_with_labels ) ) { |
29
|
|
|
$operator_with_labels = [ |
30
|
|
|
'=' => __( 'equal to', 'bulk-delete' ), |
31
|
|
|
'!=' => __( 'not equal to', 'bulk-delete' ), |
32
|
|
|
'<' => __( 'less than', 'bulk-delete' ), |
33
|
|
|
'<=' => __( 'less than or equal to', 'bulk-delete' ), |
34
|
|
|
'>' => __( 'greater than', 'bulk-delete' ), |
35
|
|
|
'>=' => __( 'greater than or equal to', 'bulk-delete' ), |
36
|
|
|
'IN' => __( 'in', 'bulk-delete' ), |
37
|
|
|
'NOT IN' => __( 'not in', 'bulk-delete' ), |
38
|
|
|
'BETWEEN' => __( 'between', 'bulk-delete' ), |
39
|
|
|
'NOT BETWEEN' => __( 'not between', 'bulk-delete' ), |
40
|
|
|
'EXISTS' => __( 'exists', 'bulk-delete' ), |
41
|
|
|
'NOT EXISTS' => __( 'not exists', 'bulk-delete' ), |
42
|
|
|
'LIKE' => __( 'contains', 'bulk-delete' ), |
43
|
|
|
'NOT LIKE' => __( 'not contains', 'bulk-delete' ), |
44
|
|
|
'STARTS_WITH' => __( 'starts with', 'bulk-delete' ), |
45
|
|
|
'ENDS_WITH' => __( 'ends with', 'bulk-delete' ), |
46
|
|
|
'CONTAINS' => __( 'contains', 'bulk-delete' ), |
47
|
|
|
]; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return $operator_with_labels; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Resolve the operators array. |
55
|
|
|
* |
56
|
|
|
* @param array $operators Operators array. |
57
|
|
|
* |
58
|
|
|
* @return array Resolved operators. |
59
|
|
|
*/ |
60
|
|
|
protected function resolve_operators( $operators ) { |
61
|
|
|
$operator_list_to_render = []; |
62
|
|
|
|
63
|
|
|
$operator_master_list = $this->get_operator_master_list(); |
64
|
|
|
|
65
|
|
|
foreach ( $operators as $operator ) { |
66
|
|
|
if ( array_key_exists( $operator, $operator_master_list ) ) { |
67
|
|
|
$operator_master_list[] = $operator; |
68
|
|
|
} else { |
69
|
|
|
$operator_list_to_render = array_merge( $operator_list_to_render, $this->resolve_operator( $operator ) ); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $operator_list_to_render; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Resolve the operator. |
78
|
|
|
* |
79
|
|
|
* Users can specify placeholders for operators. |
80
|
|
|
* The following placeholders are currently supported |
81
|
|
|
* - equals |
82
|
|
|
* - numeric |
83
|
|
|
* - ins |
84
|
|
|
* - betweens |
85
|
|
|
* - exists-all |
86
|
|
|
* - likes |
87
|
|
|
* - string-start-end |
88
|
|
|
* - string-all |
89
|
|
|
* |
90
|
|
|
* @param string $operator Operator. |
91
|
|
|
* |
92
|
|
|
* @return array Resolved operators. |
93
|
|
|
*/ |
94
|
|
|
protected function resolve_operator( $operator ) { |
95
|
|
|
switch ( $operator ) { |
96
|
|
|
case 'equals': |
97
|
|
|
return [ '=', '!=' ]; |
98
|
|
|
|
99
|
|
|
case 'numeric': |
100
|
|
|
return [ '<', '<=', '>', '>=' ]; |
101
|
|
|
|
102
|
|
|
case 'ins': |
103
|
|
|
return [ 'IN', 'NOT IN' ]; |
104
|
|
|
|
105
|
|
|
case 'betweens': |
106
|
|
|
return [ 'BETWEEN', 'NOT BETWEEN' ]; |
107
|
|
|
|
108
|
|
|
case 'exist-all': |
109
|
|
|
return [ 'EXISTS', 'NOT EXISTS' ]; |
110
|
|
|
|
111
|
|
|
case 'likes': |
112
|
|
|
return [ 'LIKE', 'NOT LIKE' ]; |
113
|
|
|
|
114
|
|
|
case 'string-start-end': |
115
|
|
|
return [ 'STARTS_WITH', 'ENDS_WITH' ]; |
116
|
|
|
|
117
|
|
|
case 'string-all': |
118
|
|
|
return [ 'STARTS_WITH', 'ENDS_WITH', 'CONTAINS' ]; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Get the label for an operator. |
124
|
|
|
* |
125
|
|
|
* @param string $operator Operator. |
126
|
|
|
* |
127
|
|
|
* @return string Operator label. |
128
|
|
|
*/ |
129
|
|
|
protected function get_operator_label( $operator ) { |
130
|
|
|
$operator_with_labels = $this->get_operator_master_list(); |
131
|
|
|
|
132
|
|
|
if ( ! array_key_exists( $operator, $operator_with_labels ) ) { |
133
|
|
|
return ''; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return $operator_with_labels[ $operator ]; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|