| 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 |
public function filterTermsClauses(array $clauses, array $taxonomies, array $args): array |
| 88 |
{ |
| 89 |
if (is_admin()) { |
| 90 |
return $clauses; |
| 91 |
} |
| 92 |
if ($taxonomies !== [glsr()->taxonomy]) { |
| 93 |
return $clauses; |
| 94 |
} |
| 95 |
if (!$this->termPriorityEnabled()) { |
| 96 |
return $clauses; |
| 97 |
} |
| 98 |
if (!$this->termPriorityExists()) { |
| 99 |
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 create_{glsr()->taxonomy} |
| 115 |
*/ |
| 116 |
public function onTermCreated(int $termId, int $ttId, array $args): void |
| 117 |
{ |
| 118 |
if (!$this->termPriorityEnabled()) { |
| 119 |
return; |
| 120 |
} |
| 121 |
$value = Arr::getAs('int', $args, static::PRIORITY_META_KEY); |
| 122 |
if (0 !== $value) { |
| 123 |
update_term_meta($termId, static::PRIORITY_META_KEY, $value); // transient deleted with "added_term_meta" hook |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* @param string[] $metaIds |
| 129 |
* |
| 130 |
* @action deleted_term_meta |
| 131 |
*/ |
| 132 |
public function onTermDeleted(array $metaIds, int $termId, string $metaKey): void |
| 133 |
{ |
| 134 |
$this->forgetTermPriorityExists($termId, $metaKey); |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* @action added_term_meta |
| 139 |
* @action updated_term_meta |
| 140 |
*/ |
| 141 |
public function onTermPriorityChanged(int $metaId, int $termId, string $metaKey): void |
| 142 |
{ |
| 143 |
$this->forgetTermPriorityExists($termId, $metaKey); |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* @action edit_{glsr()->taxonomy} |
| 148 |
*/ |
| 149 |
public function onTermUpdated(int $termId, int $ttId, array $args): void |
| 150 |
{ |
| 151 |
if (!$this->termPriorityEnabled()) { |
| 152 |
return; |
| 153 |
} |
| 154 |
$value = Arr::getAs('int', $args, static::PRIORITY_META_KEY); |
| 155 |
if (0 === $value) { |
| 156 |
delete_term_meta($termId, static::PRIORITY_META_KEY); // transient deleted with "deleted_term_meta" hook |
| 157 |
} else { |
| 158 |
update_term_meta($termId, static::PRIORITY_META_KEY, $value); // transient deleted with "added_term_meta"/"updated_term_meta" hook |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* @action {glsr()->taxonomy}_add_form_fields |
| 164 |
*/ |
| 165 |
public function renderAddFields(): void |
| 166 |
{ |
| 167 |
if ($this->termPriorityEnabled()) { |
| 168 |
glsr()->render('views/partials/taxonomy/add-term_priority', [ |
| 169 |
'id' => static::PRIORITY_META_KEY, |
| 170 |
]); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* @action {glsr()->taxonomy}_edit_form_fields |
| 176 |
*/ |
| 177 |
public function renderEditFields(\WP_Term $term): void |
| 178 |
{ |
| 179 |
if ($this->termPriorityEnabled()) { |
| 180 |
glsr()->render('views/partials/taxonomy/edit-term_priority', [ |
| 181 |
'id' => static::PRIORITY_META_KEY, |
| 182 |
'value' => $this->termPriority($term->term_id), |
| 183 |
]); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* @action quick_edit_custom_box |
| 189 |
*/ |
| 190 |
public function renderQuickEditFields(string $column, string $type, string $taxonomy): void |
| 191 |
{ |
| 192 |
if ('edit-tags' !== $type || $taxonomy !== glsr()->taxonomy || static::PRIORITY_META_KEY !== $column) { |
| 193 |
return; |
| 194 |
} |
| 195 |
if ($this->termPriorityEnabled()) { |
| 196 |
glsr()->render('views/partials/taxonomy/quickedit-term_priority', [ |
| 197 |
'id' => static::PRIORITY_META_KEY, |
| 198 |
]); |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
protected function forgetTermPriorityExists(int $termId, string $metaKey): void |
| 203 |
{ |
| 204 |
if (static::PRIORITY_META_KEY !== $metaKey) { |
| 205 |
return; |
| 206 |
} |
| 207 |
if (!is_a(get_term($termId, glsr()->taxonomy), \WP_Term::class)) { |
| 208 |
return; // somebody else's taxonomy has a term_priority of its own |
| 209 |
} |
| 210 |
delete_transient(glsr()->prefix.static::PRIORITY_META_KEY); |
| 211 |
} |
| 212 |
|
| 213 |
protected function termPriority(int $termId): int |
| 214 |
{ |
| 215 |
return Cast::toInt(get_term_meta($termId, static::PRIORITY_META_KEY, true)); |
| 216 |
} |
| 217 |
|
| 218 |
protected function termPriorityEnabled(): bool |
| 219 |
{ |
| 220 |
return !glsr()->filterBool('taxonomy/disable_term_priority', false); |
| 221 |
} |
| 222 |
|
| 223 |
protected function termPriorityExists(): bool |
| 224 |
{ |
| 225 |
$result = get_transient(glsr()->prefix.static::PRIORITY_META_KEY); |
| 226 |
if (false === $result) { |
| 227 |
$sql = " |
| 228 |
SELECT COUNT(*) |
| 229 |
FROM table|termmeta AS tm |
| 230 |
INNER JOIN table|term_taxonomy AS tt ON (tt.term_id = tm.term_id) |
| 231 |
WHERE tt.taxonomy = %s |
| 232 |
AND meta_key = %s |
| 233 |
"; |
| 234 |
$result = (int) glsr(Database::class)->dbGetVar( |
| 235 |
glsr(Query::class)->sql($sql, glsr()->taxonomy, static::PRIORITY_META_KEY) |
| 236 |
); |
| 237 |
set_transient(glsr()->prefix.static::PRIORITY_META_KEY, $result); |
| 238 |
} |
| 239 |
return (int) $result > 0; |
| 240 |
} |
| 241 |
} |
| 242 |
|