|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Database; |
|
6
|
|
|
use GeminiLabs\SiteReviews\Database\Query; |
|
7
|
|
|
use GeminiLabs\SiteReviews\Helpers\Arr; |
|
8
|
|
|
use GeminiLabs\SiteReviews\Helpers\Cast; |
|
9
|
|
|
|
|
10
|
|
|
class TaxonomyController extends AbstractController |
|
11
|
|
|
{ |
|
12
|
|
|
public const PRIORITY_META_KEY = 'term_priority'; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @param string[] $columns |
|
16
|
|
|
* |
|
17
|
|
|
* @return string[] |
|
18
|
|
|
* |
|
19
|
|
|
* @filter manage_edit-{glsr()->taxonomy}_columns |
|
20
|
|
|
*/ |
|
21
|
|
|
public function filterColumns(array $columns): array |
|
22
|
|
|
{ |
|
23
|
|
|
if ($this->termPriorityEnabled()) { |
|
24
|
|
|
$columns[static::PRIORITY_META_KEY] = _x('Priority', 'admin-text', 'site-reviews'); |
|
25
|
|
|
$columns['term_id'] = _x('TID', 'admin-text', 'site-reviews'); |
|
26
|
|
|
$columns['term_taxonomy_id'] = _x('TTID', 'admin-text', 'site-reviews'); |
|
27
|
|
|
} |
|
28
|
|
|
return $columns; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @filter manage_{glsr()->taxonomy}_custom_column |
|
33
|
|
|
*/ |
|
34
|
|
|
public function filterColumnValue(string $value, string $column, int $termId): string |
|
35
|
|
|
{ |
|
36
|
|
|
if ('term_id' === $column) { |
|
37
|
|
|
return (string) $termId; |
|
38
|
|
|
} |
|
39
|
|
|
if ('term_taxonomy_id' === $column) { |
|
40
|
|
|
return (string) get_term_by('term_id', $termId, glsr()->taxonomy)->term_taxonomy_id; |
|
41
|
|
|
} |
|
42
|
|
|
if (static::PRIORITY_META_KEY !== $column || !$this->termPriorityEnabled()) { |
|
43
|
|
|
return $value; |
|
44
|
|
|
} |
|
45
|
|
|
return (string) $this->termPriority($termId); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param string[] $hidden |
|
50
|
|
|
* |
|
51
|
|
|
* @return string[] |
|
52
|
|
|
* |
|
53
|
|
|
* @filter default_hidden_columns |
|
54
|
|
|
*/ |
|
55
|
|
|
public function filterDefaultHiddenColumns(array $hidden, \WP_Screen $screen): array |
|
56
|
|
|
{ |
|
57
|
|
|
if ('edit-'.glsr()->taxonomy !== Arr::get($screen, 'id')) { |
|
58
|
|
|
return $hidden; |
|
59
|
|
|
} |
|
60
|
|
|
return array_unique(array_merge($hidden, [ |
|
61
|
|
|
'term_id', |
|
62
|
|
|
'term_taxonomy_id', |
|
63
|
|
|
])); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param string[] $actions |
|
68
|
|
|
* |
|
69
|
|
|
* @return string[] |
|
70
|
|
|
* |
|
71
|
|
|
* @filter {glsr()->taxonomy}_row_actions |
|
72
|
|
|
*/ |
|
73
|
|
|
public function filterRowActions(array $actions, \WP_Term $term): array |
|
74
|
|
|
{ |
|
75
|
|
|
$action = ['id' => sprintf('<span>ID: %d</span>', $term->term_id)]; |
|
76
|
|
|
return array_merge($action, $actions); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param string[] $clauses |
|
81
|
|
|
* @param string[] $taxonomies |
|
82
|
|
|
* |
|
83
|
|
|
* @return string[] |
|
84
|
|
|
* |
|
85
|
|
|
* @filter terms_clauses |
|
86
|
|
|
*/ |
|
87
|
30 |
|
public function filterTermsClauses(array $clauses, array $taxonomies, array $args): array |
|
88
|
|
|
{ |
|
89
|
30 |
|
if (is_admin()) { |
|
90
|
9 |
|
return $clauses; |
|
91
|
|
|
} |
|
92
|
21 |
|
if ($taxonomies !== [glsr()->taxonomy]) { |
|
93
|
8 |
|
return $clauses; |
|
94
|
|
|
} |
|
95
|
20 |
|
if (!$this->termPriorityEnabled()) { |
|
96
|
|
|
return $clauses; |
|
97
|
|
|
} |
|
98
|
20 |
|
if (!$this->termPriorityExists()) { |
|
99
|
20 |
|
return $clauses; |
|
100
|
|
|
} |
|
101
|
|
|
$meta = new \WP_Meta_Query(); |
|
102
|
|
|
$meta->parse_query_vars($args); |
|
103
|
|
|
$meta->get_sql('term', 't', 'term_id'); |
|
104
|
|
|
if (empty($meta->get_clauses())) { |
|
105
|
|
|
global $wpdb; |
|
106
|
|
|
$clauses['join'] .= $wpdb->prepare(" LEFT JOIN {$wpdb->termmeta} AS tm ON (tm.term_id = t.term_id AND tm.meta_key = %s) ", static::PRIORITY_META_KEY); |
|
107
|
|
|
$clauses['where'] .= $wpdb->prepare(" AND (tm.term_id IS NULL OR tm.meta_key = %s) ", static::PRIORITY_META_KEY); |
|
108
|
|
|
$clauses['orderby'] = str_replace('ORDER BY', 'ORDER BY tm.meta_value+0 DESC, ', $clauses['orderby']); |
|
109
|
|
|
} |
|
110
|
|
|
return $clauses; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @action {glsr()->taxonomy}_add_form_fields |
|
115
|
|
|
*/ |
|
116
|
|
|
public function renderAddFields(): void |
|
117
|
|
|
{ |
|
118
|
|
|
if ($this->termPriorityEnabled()) { |
|
119
|
|
|
glsr()->render('views/partials/taxonomy/add-term_priority', [ |
|
120
|
|
|
'id' => static::PRIORITY_META_KEY, |
|
121
|
|
|
]); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @action {glsr()->taxonomy}_edit_form_fields |
|
127
|
|
|
*/ |
|
128
|
|
|
public function renderEditFields(\WP_Term $term): void |
|
129
|
|
|
{ |
|
130
|
|
|
if ($this->termPriorityEnabled()) { |
|
131
|
|
|
glsr()->render('views/partials/taxonomy/edit-term_priority', [ |
|
132
|
|
|
'id' => static::PRIORITY_META_KEY, |
|
133
|
|
|
'value' => $this->termPriority($term->term_id), |
|
134
|
|
|
]); |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @action quick_edit_custom_box |
|
140
|
|
|
*/ |
|
141
|
|
|
public function renderQuickEditFields(string $column, string $type, string $taxonomy): void |
|
142
|
|
|
{ |
|
143
|
|
|
if ('edit-tags' !== $type || $taxonomy !== glsr()->taxonomy || static::PRIORITY_META_KEY !== $column) { |
|
144
|
|
|
return; |
|
145
|
|
|
} |
|
146
|
|
|
if ($this->termPriorityEnabled()) { |
|
147
|
|
|
glsr()->render('views/partials/taxonomy/quickedit-term_priority', [ |
|
148
|
|
|
'id' => static::PRIORITY_META_KEY, |
|
149
|
|
|
]); |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* @action create_{glsr()->taxonomy} |
|
155
|
|
|
*/ |
|
156
|
|
|
public function termPriorityCreated(int $termId, int $ttId, array $args): void |
|
157
|
|
|
{ |
|
158
|
|
|
if (!$this->termPriorityEnabled()) { |
|
159
|
|
|
return; |
|
160
|
|
|
} |
|
161
|
|
|
$value = Arr::getAs('int', $args, static::PRIORITY_META_KEY); |
|
162
|
|
|
if (0 !== $value) { |
|
163
|
|
|
update_term_meta($termId, static::PRIORITY_META_KEY, $value); |
|
164
|
|
|
delete_transient(glsr()->prefix.static::PRIORITY_META_KEY); |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* @param string[] $metaIds |
|
170
|
|
|
* |
|
171
|
|
|
* @action deleted_term_meta |
|
172
|
|
|
*/ |
|
173
|
|
|
public function termPriorityDeleted(array $metaIds, int $termId, string $metaKey): void |
|
174
|
|
|
{ |
|
175
|
|
|
$term = get_term((int) $termId, glsr()->taxonomy); |
|
176
|
|
|
if (is_a($term, \WP_Term::class) && static::PRIORITY_META_KEY === $metaKey) { |
|
177
|
|
|
delete_transient(glsr()->prefix.static::PRIORITY_META_KEY); |
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* @action edit_{glsr()->taxonomy} |
|
183
|
|
|
*/ |
|
184
|
|
|
public function termPriorityUpdated(int $termId, int $ttId, array $args): void |
|
|
|
|
|
|
185
|
|
|
{ |
|
186
|
|
|
if (!$this->termPriorityEnabled()) { |
|
187
|
|
|
return; |
|
188
|
20 |
|
} |
|
189
|
|
|
$value = Arr::getAs('int', $args, static::PRIORITY_META_KEY); |
|
190
|
20 |
|
if (0 === $value) { |
|
191
|
|
|
delete_term_meta($termId, static::PRIORITY_META_KEY); // transient deleted with "deleted_term_meta" hook |
|
192
|
|
|
} else { |
|
193
|
20 |
|
update_term_meta($termId, static::PRIORITY_META_KEY, $value); |
|
194
|
|
|
delete_transient(glsr()->prefix.static::PRIORITY_META_KEY); |
|
195
|
20 |
|
} |
|
196
|
20 |
|
} |
|
197
|
20 |
|
|
|
198
|
|
|
protected function termPriority(int $termId): int |
|
199
|
|
|
{ |
|
200
|
|
|
return Cast::toInt(get_term_meta($termId, static::PRIORITY_META_KEY, true)); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
20 |
|
protected function termPriorityEnabled(): bool |
|
204
|
20 |
|
{ |
|
205
|
20 |
|
return !glsr()->filterBool('taxonomy/disable_term_priority', false); |
|
206
|
20 |
|
} |
|
207
|
20 |
|
|
|
208
|
|
|
protected function termPriorityExists(): bool |
|
209
|
20 |
|
{ |
|
210
|
|
|
$result = get_transient(glsr()->prefix.static::PRIORITY_META_KEY); |
|
211
|
|
|
if (false === $result) { |
|
212
|
|
|
$sql = " |
|
213
|
|
|
SELECT COUNT(*) |
|
214
|
|
|
FROM table|termmeta AS tm |
|
215
|
|
|
INNER JOIN table|term_taxonomy AS tt ON (tt.term_id = tm.term_id) |
|
216
|
|
|
WHERE tt.taxonomy = %s |
|
217
|
|
|
AND meta_key = %s |
|
218
|
|
|
"; |
|
219
|
|
|
$result = (int) glsr(Database::class)->dbGetVar( |
|
220
|
|
|
glsr(Query::class)->sql($sql, glsr()->taxonomy, static::PRIORITY_META_KEY) |
|
221
|
|
|
); |
|
222
|
|
|
set_transient(glsr()->prefix.static::PRIORITY_META_KEY, $result); |
|
223
|
|
|
} |
|
224
|
|
|
return (int) $result > 0; |
|
225
|
|
|
} |
|
226
|
|
|
} |
|
227
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.