-
Notifications
You must be signed in to change notification settings - Fork 6
/
CPT_Columns.php
395 lines (372 loc) · 10.7 KB
/
CPT_Columns.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
<?php
//Based on Ohad Raz - https://en.bainternet.info/custom-post-types-columns/
//Some info:
// The classes read the meta field of the CPT ID and enable the sorting
// Is possible remove other column
// Support 5 type of object natively: Title, Thumbnail, Author, Custom taxonomy, Custom Field
// Add a filter cpt_columns_text_{column_name_id} in Title type
// prefix/suffix values are for all the objects except post_thumb
// New features by Mte90:
// Fix for def value
// New order columns value
// Custom callback for the value
// Support for author type
// Support for sort for custom_value
//Check the examples!
/* Example
//create an instance
$post_columns = new CPT_columns('post'); // if you want to replace and reorder columns then pass a second parameter as true
//add native column
$post_columns->add_column('title',
array(
'label' => __('Title'),
'type' => 'native',
'sortable' => true
)
);
//add thumbnail column
$post_columns->add_column('post_thumb',
array(
'label' => __('Thumb'),
'type' => 'thumb',
'size' => array('80,80') //size accepted by the_post_thumbnail as array or string
)
);
//add taxonomy
$post_columns->add_column('custom_tax_id',
array(
'label' => __('Custom Taxonomy'),
'type' => 'custom_tax',
'taxonomy' => 'category' //taxonomy name
)
);
//custom field column
$post_columns->add_column('price',
array(
'label' => __('Custom Field'),
'type' => 'post_meta',
'meta_key' => 'price', //meta_key
'orderby' => 'meta_value', //meta_value,meta_value_num
'sortable' => true,
'prefix' => "$",
'suffix' => "",
'def' => "", // default value in case post meta not found
'order' => "-1" //before the date column
)
);
//custom callback column
$post_columns->add_column('price',
array(
'label' => __('Custom Callback'),
'type' => 'custom_value',
'meta_key' => 'your_meta_value',
'callback' => 'your_callback_function', // array( $this, 'your_callback_method')in a class
'orderby' => 'meta_value', //meta_value,meta_value_num
'sortable' => true,
'prefix' => "$",
'suffix' => "",
'order' => "-1" //before the date column
)
);
//remove date column
$post_columns->remove_column('date');
*/
if ( !class_exists( 'CPT_columns' ) ) {
/**
* CPT_columns
* Simple class to add remove and manage admin post columns
* @author Ohad Raz <[email protected]>
* @version 0.1
* @copyright 2013 Ohad Raz
*/
class CPT_columns {
/**
* $cpt_columns
*
* holds columns
* @var array
*/
public $cpt_columns = array();
/**
* $cpt_remove_columns
*
* holds columns to be removed
* @var array
*/
public $cpt_remove_columns = array();
/**
* $cpt_sortable_columns
*
* holds sortable columns
* @var array
*/
public $cpt_sortable_columns = array();
/**
* $cpt_name
*
* Holds custom post type name
* @var string
*/
public $cpt_name = '';
/**
* $replace
*
* Should coulmns be replace (true) or added (false)
* @var boolean
*/
public $replace = false;
/**
* __construct
*
* @author Ohad Raz <[email protected]>
* @since 0.1
* @param string $cpt custom post type name
* @param boolean $replace (optional) replace or add
*/
function __construct( $cpt = '', $replace = false ) {
$this->cpt_name = $cpt;
$this->replace = $replace;
//add columns
add_filter( "manage_{$cpt}_posts_columns", array( $this, '_cpt_columns' ), 50 );
//remove columns
add_filter( "manage_{$cpt}_posts_columns", array( $this, '_cpt_columns_remove' ), 60 );
//display columns
add_action( "manage_{$cpt}_posts_custom_column", array( $this, '_cpt_custom_column' ), 50, 2 );
//sortable columns
add_filter( "manage_edit-{$cpt}_sortable_columns", array( $this, "_sortable_columns" ), 50 );
//sort order
add_filter( 'pre_get_posts', array( $this, '_column_orderby' ), 50 );
}
/**
* _cpt_columns
*
* @author Ohad Raz <[email protected]> & Mte90 <[email protected]>
* @since 0.2
* @param array $defaults
* @return array
*/
function _cpt_columns( $defaults ) {
global $typenow;
if ( $this->cpt_name == $typenow ) {
$tmp = array();
if ( $this->replace ) {
foreach ( $this->cpt_columns as $key => $args ) {
$tmp[ $key ] = $args[ 'label' ];
}
return $tmp;
} else {
foreach ( $this->cpt_columns as $key => $args ) {
$tmp[ $key ] = $args[ 'label' ];
if ( isset( $args[ 'order' ] ) ) {
$before = array_slice( $defaults, 0, $args[ 'order' ] );
$after = array_slice( $defaults, $args[ 'order' ] );
$return = array_merge( $before, ( array ) $tmp );
$defaults = array_merge( $return, $after );
} else {
$defaults = array_merge( $defaults, $tmp );
}
$tmp = array();
}
}
}
return $defaults;
}
/**
* _cpt_columns_remove
*
* used to remove columns
* @author Ohad Raz <[email protected]>
* @since 0.1
* @param array $columns
* @return array
*/
function _cpt_columns_remove( $columns ) {
foreach ( $this->cpt_remove_columns as $key ) {
if ( isset( $columns[ $key ] ) ) {
unset( $columns[ $key ] );
}
}
return $columns;
}
/**
* _sortable_columns
*
* sets sortable columns
* @author Ohad Raz <[email protected]>
* @since 0.1
* @param array $columns
* @return array
*/
function _sortable_columns( $columns ) {
global $typenow;
if ( $this->cpt_name == $typenow ) {
foreach ( $this->cpt_sortable_columns as $key => $args ) {
$columns[ $key ] = $key;
}
}
return $columns;
}
/**
* _cpt_custom_column
*
* calls do_column() when the column is set
* @author Ohad Raz <[email protected]>
* @since 0.1
* @param string $column_name column name
* @param int $post_id post ID
* @return void
*/
function _cpt_custom_column( $column_name, $post_id ) {
if ( isset( $this->cpt_columns[ $column_name ] ) ) {
$this->do_column( $post_id, $this->cpt_columns[ $column_name ], $column_name );
}
}
/**
* do_column
*
* used to display the column
* @author Ohad Raz <[email protected]>
* @since 0.1
* @param int $post_id post ID
* @param array $column column args
* @param string $column_name column name
* @return void
*/
function do_column( $post_id, $column, $column_name ) {
if ( in_array( $column[ 'type' ], array( 'text', 'thumb', 'post_meta', 'custom_tax' ) ) ) {
echo $column[ 'prefix' ];
}
switch ( $column[ 'type' ] ) {
case 'text':
echo apply_filters( 'cpt_columns_text_' . $column_name, $column[ 'text' ], $post_id, $column, $column_name );
break;
case 'thumb':
if ( has_post_thumbnail( $post_id ) ) {
the_post_thumbnail( $column[ 'size' ] );
} else {
echo 'N/A';
}
break;
case 'post_meta':
$tmp = get_post_meta( $post_id, $column[ 'meta_key' ], true );
echo (!empty( $tmp )) ? $tmp : $column[ 'def' ];
break;
case 'author':
$author_id = get_post_field( 'post_author', $post_id );
$post_type = get_post_type( $post_id );
echo '<a href="' . admin_url() . 'edit.php?post_type=' . $post_type . '&author=' . $author_id . '">' . get_the_author_meta( 'user_nicename', $author_id ) . '</a>';
break;
case 'custom_tax':
$post_type = get_post_type( $post_id );
$terms = get_the_terms( $post_id, $column[ 'taxonomy' ] );
if ( !empty( $terms ) ) {
foreach ( $terms as $term ) {
$href = "edit.php?post_type={$post_type}&{$column[ 'taxonomy' ]}={$term->slug}";
$name = esc_html( sanitize_term_field( 'name', $term->name, $term->term_id, $column[ 'taxonomy' ], 'edit' ) );
$post_terms[] = "<a href='{$href}'>{$name}</a>";
}
echo join( ', ', $post_terms );
} else {
echo '';
}
break;
case 'custom_value':
if ( isset( $column[ 'callback' ] ) ) {
if ( is_callable( $column[ 'callback' ] ) ) {
echo call_user_func( $column[ 'callback' ], $post_id );
} else {
echo $column[ 'callback' ] . ' is not a callable object!';
}
} else {
echo 'The \'callback\' parameter is not defined!';
}
break;
}
if ( in_array( $column[ 'type' ], array( 'text', 'thumb', 'post_meta', 'custom_tax' ) ) ) {
echo $column[ 'suffix' ];
}
}
/**
* _column_orderby
*
* used to roder by meta keys
* @author Ohad Raz <[email protected]>
* @since 0.1
* @param object $query
* @return void
*/
function _column_orderby( $query ) {
if ( !is_admin() ) {
return;
}
$orderby = $query->get( 'orderby' );
if ( !is_array( $orderby ) && (!empty( $orderby ) && isset( $this->cpt_sortable_columns[ $orderby ], $this->cpt_sortable_columns[ $orderby ][ 'type' ] )) && $this->cpt_sortable_columns[ $orderby ][ 'type' ] === 'custom_value' ) {
$query->set( 'orderby', 'meta_value' );
//$query->set( 'meta_key', $this->cpt_sortable_columns[ $orderby ][ 'meta_key' ] );
$query->set( 'meta_query', array(
'relation' => 'OR',
array(
'key' => $this->cpt_sortable_columns[ $orderby ][ 'meta_key' ],
'compare' => 'NOT EXISTS',
'value' => ''
),
array(
'key' => $this->cpt_sortable_columns[ $orderby ][ 'meta_key' ],
'compare' => 'EXISTS',
)
) );
} else {
$keys = array_keys( ( array ) $this->cpt_sortable_columns );
if ( in_array( $orderby, $keys ) ) {
//order by meta
if ( 'post_meta' == $this->cpt_sortable_columns[ $orderby ][ 'type' ] ) {
$query->set( 'meta_key', $orderby );
$query->set( 'orderby', $this->cpt_sortable_columns[ $orderby ][ 'orderby' ] );
}
}
}
}
/**
* add_column
*
* used to add column
* @author Ohad Raz <[email protected]>
* @since 0.1
* @param string $key column id
* @param array $args column arguments
* @return void
*/
function add_column( $key, $args ) {
$def = array(
'label' => 'column label',
'size' => array( '80', '80' ),
'taxonomy' => '',
'meta_key' => '',
'sortable' => false,
'text' => '',
'type' => 'native', //'native','post_meta','custom_tax',text
'orderby' => 'meta_value',
'prefix' => '',
'suffix' => '',
'def' => '',
);
$this->cpt_columns[ $key ] = array_merge( $def, $args );
if ( $this->cpt_columns[ $key ][ 'sortable' ] ) {
$this->cpt_sortable_columns[ $key ] = $this->cpt_columns[ $key ];
}
}
/**
* remove_column
*
* Used to remove columns
*
* @author Ohad Raz <[email protected]>
* @since 0.1
* @param string $key column key to be removed
* @return void
*/
function remove_column( $key ) {
$this->cpt_remove_columns[] = $key;
}
}
}