-
Notifications
You must be signed in to change notification settings - Fork 0
/
PostTypeColumns.php
218 lines (182 loc) · 5.92 KB
/
PostTypeColumns.php
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<?php
namespace Adeliom\WP\Extensions\Utils\Types;
use WP_Query;
/**
* Class PostTypeColumns
*/
class PostTypeColumns
{
/**
* Post Type.
*
* @var null|string A custom post type slug.
*/
private ?string $post_type = null;
/**
* Column definitions.
*
* @var array An array of columns with args.
*/
private array $columns = [];
/**
* PostTypeColumns constructor.
*
* @param string $post_type A post type slug.
* @param array $columns An array of columns to be edited.
*/
public function __construct($post_type, $columns)
{
$this->post_type = $post_type;
foreach ($columns as $slug => $column) {
if (false !== $column) {
// Set defaults for thumbnail.
if ('thumbnail' === $slug) {
$column = wp_parse_args($column, [
'title' => __('Featured Image', 'mind/types'),
'width' => 80,
'height' => 80,
]);
}
// Set defaults for each field.
$column = wp_parse_args($column, [
'title' => '',
'type' => 'meta',
'transform' => null,
'sortable' => true,
'searchable' => false,
]);
}
$this->columns[$slug] = $column;
}
}
/**
* Inits hooks.
*/
public function init(): void
{
add_filter('manage_edit-' . $this->post_type . '_columns', fn(array $columns): array => $this->columns($columns));
add_filter('manage_edit-' . $this->post_type . '_sortable_columns', fn(array $columns): array => $this->columnsSortable($columns));
add_action('manage_' . $this->post_type . '_posts_custom_column', fn(string $column_name, int $post_id) => $this->columnContent($column_name, $post_id), 10, 2);
if (is_admin()) {
add_filter('pre_get_posts', fn(\WP_Query $query) => $this->searchCustomFields($query));
}
}
/**
* Filters columns for post list view.
*
* @param array $columns An array of existing columns.
*
* @return array Filtered array.
*/
public function columns(array $columns): array
{
foreach ($this->columns as $slug => $column) {
if (is_array($column)) {
$columns[$slug] = $column["title"] ?: $slug;
continue;
}
}
return $columns;
}
/**
* Filters sortable columns.
*
* @param array $columns An array of existing columns.
*
* @return array Filtered array.
*/
public function columnsSortable(array $columns): array
{
foreach ($this->columns as $slug => $column) {
// Remove column when it’s not sortable.
if (!isset($column['sortable']) || !$column['sortable']) {
unset($columns[$slug]);
continue;
} else {
$columns[$slug] = $slug;
}
}
return $columns;
}
/**
* Update column contents for post list view.
*
* @param string $column_name The column slug.
* @param int $post_id The post ID.
*/
public function columnContent(string $column_name, int $post_id): void
{
$value = null;
// Bail out.
if (
empty($this->columns)
|| !array_key_exists($column_name, $this->columns)
) {
return;
}
$column = $this->columns[$column_name];
if ('thumbnail' === $column_name) {
$src = get_the_post_thumbnail_url($post_id, 'thumbnail');
if (empty($src)) {
return;
}
$styles = '';
foreach (['width', 'height'] as $attr) {
if (isset($column[$attr])) {
$styles .= $attr . ':' . $column[$attr] . 'px;';
}
}
if (!empty($styles)) {
$styles = ' style="' . $styles . '"';
}
echo '<img src="' . esc_attr($src) . '"' . $styles . '>';
return;
}
if (isset($column['type'])) {
if ('acf' === $column['type']) {
$value = get_field($column_name, $post_id);
} elseif ('meta' === $column['type']) {
$value = get_post_meta($post_id, $column_name, true);
}
} else {
$value = get_post_meta($post_id, $column_name, true);
}
if (isset($column['transform']) && is_callable($column['transform'])) {
$value = call_user_func($column['transform'], $value, $post_id);
}
echo $value;
}
/**
* Includeds searchable custom fields in the search.
*
* @param WP_Query $query WordPress query object.
*/
public function searchCustomFields(WP_Query $query): void
{
global $typenow;
$searchterm = $query->query_vars['s'];
if (!$query->is_main_query() || $typenow !== $this->post_type || empty($searchterm)) {
return;
}
$meta_columns = array_filter($this->columns, function ($column): bool {
if (isset($column['type']) && isset($column['searchable'])) {
return 'meta' === $column['type'] && $column['searchable'];
}
return false;
});
$meta_query = ['relation' => 'OR'];
foreach (array_keys($meta_columns) as $key) {
$meta_query[] = [
'key' => $key,
'value' => $searchterm,
'compare' => 'LIKE',
];
}
/**
* The search parameter needs to be removed from the query, because it will prevent
* the proper posts from being found.
*/
//$query->set('s', '');
$query->set('meta_query', $meta_query);
}
}