PluginProbe ʕ •ᴥ•ʔ
Admin Columns / 1.3
Admin Columns v1.3
7.0.19 2.3.5 2.4 2.4.1 2.4.10 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.4.8 2.4.9 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.6.1 2.5.6.2 2.5.6.3 2.5.6.4 3.0 3.0.1 3.0.2 3.0.3 3.0.5 3.0.7 3.1 3.1.1 3.1.10 3.1.2 3.1.3 3.1.5 3.2.3 3.2.7 3.3.1 3.4.1 3.4.6 3.4.8 4.0.1 4.0.3 4.1.6 4.2.2 4.2.5 4.3 4.3.2 4.4.1 4.4.4 4.4.5 4.5.5 4.6.1 4.7.18 4.7.19 4.7.20 4.7.7 7.0.13 7.0.14 7.0.16 trunk 1.0 1.1 1.1.3 1.2 1.2.1 1.3 1.3.1 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.5.1 1.4.6 1.4.6.1 1.4.6.2 1.4.6.3 1.4.6.4 1.4.7 1.4.8 1.4.9 2.0.0 2.0.1 2.0.2 2.0.3 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.2 2.2.1 2.2.1.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.5.1 2.2.6 2.2.6.1 2.2.6.2 2.2.6.3 2.2.6.4 2.2.7 2.2.8 2.2.8.1 2.2.9 2.3.1 2.3.2 2.3.3
codepress-admin-columns / classes / sortable.php
codepress-admin-columns / classes Last commit date
sortable.php 14 years ago
sortable.php
594 lines
1 <?php
2 /**
3 * Init Class
4 *
5 * @since 1.3
6 */
7 new Codepress_Sortable_Columns();
8
9 /**
10 * Coderess Sortable Columns Class
11 *
12 * @since 1.3
13 *
14 */
15 class Codepress_Sortable_Columns extends Codepress_Admin_Columns
16 {
17 private $post_types,
18 $is_unlocked,
19 $show_all_results;
20
21 /**
22 * Constructor
23 *
24 * @since 1.0
25 */
26 function __construct()
27 {
28 add_action( 'wp_loaded', array( &$this, 'init') );
29 }
30
31 /**
32 * Initialize
33 *
34 * @since 1.0
35 */
36 public function init()
37 {
38 // vars
39 $this->is_unlocked = $this->is_unlocked('sortable');
40 $this->post_types = $this->get_post_types();
41 $this->show_all_results = false;
42
43 add_action( 'admin_init', array( &$this, 'register_sortable_columns' ) );
44
45 // handle requests for sorting columns
46 add_filter( 'request', array( &$this, 'handle_requests_orderby_column'), 1 );
47 add_action( 'pre_user_query', array( &$this, 'handle_requests_orderby_users_column'), 1 );
48 }
49
50 /**
51 * Register sortable columns
52 *
53 * @since 1.0
54 */
55 function register_sortable_columns()
56 {
57 /** Posts */
58 foreach ( $this->post_types as $post_type )
59 add_filter( "manage_edit-{$post_type}_sortable_columns", array(&$this, 'callback_add_sortable_posts_column'));
60
61 if ( ! $this->is_unlocked )
62 return false;
63
64 /** Users */
65 add_filter( "manage_users_sortable_columns", array(&$this, 'callback_add_sortable_users_column'));
66
67 /** Media */
68 add_filter( "manage_upload_sortable_columns", array(&$this, 'callback_add_sortable_media_column'));
69 }
70
71 /**
72 * Callback add Posts sortable column
73 *
74 * @since 1.0
75 */
76 public function callback_add_sortable_posts_column($columns)
77 {
78 global $post_type;
79
80 return $this->add_managed_sortable_columns($post_type, $columns);
81 }
82
83 /**
84 * Callback add Users sortable column
85 *
86 * @since 1.1
87 */
88 public function callback_add_sortable_users_column($columns)
89 {
90 return $this->add_managed_sortable_columns('wp-users', $columns);
91 }
92
93 /**
94 * Callback add Media sortable column
95 *
96 * @since 1.3
97 */
98 public function callback_add_sortable_media_column($columns)
99 {
100 return $this->add_managed_sortable_columns('wp-media', $columns);
101 }
102
103 /**
104 * Add managed sortable columns by Type
105 *
106 * @since 1.1
107 */
108 private function add_managed_sortable_columns( $type = 'post', $columns )
109 {
110 $display_columns = $this->get_merged_columns($type);
111
112 if ( ! $display_columns )
113 return $columns;
114
115 foreach ( $display_columns as $id => $vars ) {
116 if ( isset($vars['options']['sortorder']) && $vars['options']['sortorder'] == 'on' ){
117
118 // register format
119 $columns[$id] = $this->sanitize_string($vars['label']);
120 }
121 }
122 return $columns;
123 }
124
125 /**
126 * Admin requests for orderby column
127 *
128 * @since 1.0
129 */
130 public function handle_requests_orderby_column( $vars )
131 {
132 if ( ! isset( $vars['orderby'] ) )
133 return $vars;
134
135 /** Users */
136 // You would expect to see get_orderby_users_vars(), but sorting for
137 // users is handled through a different filter. Not 'request', but 'pre_user_query'.
138 // See handle_requests_orderby_users_column().
139
140 /** Media */
141 elseif ( $this->request_uri_is_media() )
142 $vars = $this->get_orderby_media_vars($vars);
143
144 /** Posts */
145 elseif ( !empty($vars['post_type']) )
146 $vars = $this->get_orderby_posts_vars($vars);
147
148 return $vars;
149 }
150
151 /**
152 * Orderby Users column
153 *
154 * @since 1.3
155 */
156 public function handle_requests_orderby_users_column($user_query)
157 {
158 // query vars
159 $vars = $user_query->query_vars;
160
161 // Column
162 $column = $this->get_orderby_type( $vars['orderby'], 'wp-users' );
163
164 if ( empty($column) )
165 return $vars;
166
167 // var
168 $cusers = array();
169 switch( key($column) ) :
170
171 case 'column-user_id':
172 $user_query->query_vars['orderby'] = 'ID';
173 break;
174
175 case 'column-user_registered':
176 $user_query->query_vars['orderby'] = 'registered';
177 break;
178
179 case 'column-nickname':
180 $user_query->query_vars['orderby'] = 'nickname';
181 break;
182
183 case 'column-first_name':
184 foreach ( $this->get_users_data() as $u )
185 if ($u->first_name || $this->show_all_results )
186 $cusers[$u->ID] = $this->prepare_sort_string_value($u->first_name);
187 $this->set_users_query_vars( &$user_query, $cusers, SORT_REGULAR );
188 break;
189
190 case 'column-last_name':
191 foreach ( $this->get_users_data() as $u )
192 if ($u->last_name || $this->show_all_results )
193 $cusers[$u->ID] = $this->prepare_sort_string_value($u->last_name);
194 $this->set_users_query_vars( &$user_query, $cusers, SORT_REGULAR );
195 break;
196
197 case 'column-user_url':
198 foreach ( $this->get_users_data() as $u )
199 if ($u->user_url || $this->show_all_results )
200 $cusers[$u->ID] = $this->prepare_sort_string_value($u->user_url);
201 $this->set_users_query_vars( &$user_query, $cusers, SORT_REGULAR );
202 break;
203
204 case 'column-user_description':
205 foreach ( $this->get_users_data() as $u )
206 if ($u->user_description || $this->show_all_results )
207 $cusers[$u->ID] = $this->prepare_sort_string_value($u->user_description);
208 $this->set_users_query_vars( &$user_query, $cusers, SORT_REGULAR );
209 break;
210
211 endswitch;
212
213 return $user_query;
214 }
215
216 /**
217 * Set sorting vars in User Query Object
218 *
219 * @since 1.3
220 */
221 private function set_users_query_vars(&$user_query, $sortusers, $sort_flags = SORT_REGULAR )
222 {
223 global $wpdb;
224
225 // vars
226 $vars = $user_query->query_vars;
227
228 // sorting
229 if ( $vars['order'] == 'ASC' )
230 asort($sortusers, $sort_flags);
231 else
232 arsort($sortusers, $sort_flags);
233
234 // alter orderby SQL
235 if ( ! empty ( $sortusers ) ) {
236 $ids = implode(',', array_keys($sortusers));
237 $user_query->query_where .= " AND {$wpdb->prefix}users.ID IN ({$ids})";
238 $user_query->query_orderby = "ORDER BY FIELD ({$wpdb->prefix}users.ID,{$ids})";
239 }
240
241 // cleanup the vars we dont need
242 $vars['order'] = '';
243 $vars['orderby'] = '';
244
245 $user_query->query_vars = $vars;
246 }
247
248 /**
249 * Orderby Media column
250 *
251 * @since 1.3
252 */
253 private function get_orderby_media_vars($vars)
254 {
255 // Column
256 $column = $this->get_orderby_type( $vars['orderby'], 'wp-media' );
257
258 if ( empty($column) )
259 return $vars;
260
261 // var
262 $cposts = array();
263 switch( key($column) ) :
264
265 case 'column-mediaid' :
266 $vars['orderby'] = 'ID';
267 break;
268
269 case 'column-width' :
270 foreach ( (array) $this->get_any_posts_by_posttype('attachment') as $p ) {
271 $meta = wp_get_attachment_metadata($p->ID);
272 $width = !empty($meta['width']) ? $meta['width'] : 0;
273 if ( $width || $this->show_all_results )
274 $cposts[$p->ID] = $width;
275 }
276 $this->set_vars_post__in( &$vars, $cposts, SORT_NUMERIC );
277 break;
278
279 case 'column-height' :
280 foreach ( (array) $this->get_any_posts_by_posttype('attachment') as $p ) {
281 $meta = wp_get_attachment_metadata($p->ID);
282 $height = !empty($meta['height']) ? $meta['height'] : 0;
283 if ( $height || $this->show_all_results )
284 $cposts[$p->ID] = $height;
285 }
286 $this->set_vars_post__in( &$vars, $cposts, SORT_NUMERIC );
287 break;
288
289 case 'column-dimensions' :
290 foreach ( (array) $this->get_any_posts_by_posttype('attachment') as $p ) {
291 $meta = wp_get_attachment_metadata($p->ID);
292 $height = !empty($meta['height']) ? $meta['height'] : 0;
293 $width = !empty($meta['width']) ? $meta['width'] : 0;
294 $surface = $height*$width;
295
296 if ( $surface || $this->show_all_results )
297 $cposts[$p->ID] = $surface;
298 }
299 $this->set_vars_post__in( &$vars, $cposts, SORT_NUMERIC );
300 break;
301
302 case 'column-caption' :
303 foreach ( (array) $this->get_any_posts_by_posttype('attachment') as $p )
304 if ( $p->post_excerpt || $this->show_all_results )
305 $cposts[$p->ID] = $this->prepare_sort_string_value($p->post_excerpt);
306 $this->set_vars_post__in( &$vars, $cposts, SORT_STRING);
307 break;
308
309 case 'column-description' :
310 foreach ( (array) $this->get_any_posts_by_posttype('attachment') as $p )
311 if ( $p->post_content || $this->show_all_results )
312 $cposts[$p->ID] = $this->prepare_sort_string_value( $p->post_content );
313 $this->set_vars_post__in( &$vars, $cposts, SORT_STRING);
314 break;
315
316 case 'column-mime_type' :
317 foreach ( (array) $this->get_any_posts_by_posttype('attachment') as $p )
318 if ( $p->post_mime_type || $this->show_all_results )
319 $cposts[$p->ID] = $this->prepare_sort_string_value( $p->post_mime_type );
320 $this->set_vars_post__in( &$vars, $cposts, SORT_STRING);
321 break;
322
323 case 'column-file_name' :
324 foreach ( (array) $this->get_any_posts_by_posttype('attachment') as $p ) {
325 $meta = get_post_meta($p->ID, '_wp_attached_file', true);
326 $file = !empty($meta) ? basename($meta) : '';
327 if ( $file || $this->show_all_results )
328 $cposts[$p->ID] = $file;
329 }
330 $this->set_vars_post__in( &$vars, $cposts, SORT_STRING);
331 break;
332
333 case 'column-alternate_text' :
334 foreach ( (array) $this->get_any_posts_by_posttype('attachment') as $p ) {
335 $alt = get_post_meta($p->ID, '_wp_attachment_image_alt', true);
336 if ( $alt || $this->show_all_results ) {
337 $cposts[$p->ID] = $this->prepare_sort_string_value( $alt );
338 }
339 }
340 $this->set_vars_post__in( &$vars, $cposts, SORT_STRING);
341 break;
342
343 endswitch;
344
345 return $vars;
346 }
347
348 /**
349 * Orderby Posts column
350 *
351 * @since 1.3
352 */
353 private function get_orderby_posts_vars($vars)
354 {
355 $post_type = $vars['post_type'];
356
357 // Column
358 $column = $this->get_orderby_type( $vars['orderby'], $post_type );
359
360 if ( empty($column) )
361 return $vars;
362
363 // id
364 $id = key($column);
365
366 // type
367 $type = $id;
368
369 // custom fields
370 if ( $this->is_column_meta($type) )
371 $type = 'column-post-meta';
372
373 // attachments
374 if ( $type == 'column-attachment-count' )
375 $type = 'column-attachment';
376
377 // var
378 $cposts = array();
379 switch( $type ) :
380
381 case 'column-postid' :
382 $vars['orderby'] = 'ID';
383 break;
384
385 case 'column-order' :
386 $vars['orderby'] = 'menu_order';
387 break;
388
389 case 'column-post-meta' :
390 $field = $column[$id]['field'];
391
392 // orderby type
393 $field_type = 'meta_value';
394 if ( $column[$id]['field_type'] == 'numeric' || $column[$id]['field_type'] == 'library_id' )
395 $field_type = 'meta_value_num';
396
397 $vars = array_merge($vars, array(
398 'meta_key' => $field,
399 'orderby' => $field_type
400 ));
401 break;
402
403 case 'column-excerpt' :
404 foreach ( (array) $this->get_any_posts_by_posttype($post_type) as $p ) {
405
406 // add excerpt to the post ids
407 $cposts[$p->ID] = $this->prepare_sort_string_value($p->post_content);
408 }
409 // we will add the sorted post ids to vars['post__in'] and remove unused vars
410 $this->set_vars_post__in( &$vars, $cposts, SORT_STRING );
411 break;
412
413 case 'column-word-count' :
414 foreach ( (array) $this->get_any_posts_by_posttype($post_type) as $p )
415 $cposts[$p->ID] = str_word_count( $this->strip_trim( $p->post_content ) );
416 $this->set_vars_post__in( &$vars, $cposts, SORT_NUMERIC );
417 break;
418
419 case 'column-page-template' :
420 $templates = get_page_templates();
421 foreach ( (array) $this->get_any_posts_by_posttype($post_type) as $p ) {
422 $page_template = get_post_meta($p->ID, '_wp_page_template', true);
423 $cposts[$p->ID] = array_search($page_template, $templates);
424 }
425 $this->set_vars_post__in( &$vars, $cposts );
426 break;
427
428 case 'column-post_formats' :
429 foreach ( (array) $this->get_any_posts_by_posttype($post_type) as $p ) {
430 $cposts[$p->ID] = get_post_format($p->ID);
431 }
432 $this->set_vars_post__in( &$vars, $cposts );
433 break;
434
435 case 'column-attachment' :
436 foreach ( (array) $this->get_any_posts_by_posttype($post_type) as $p )
437 $cposts[$p->ID] = count( $this->get_attachment_ids($p->ID) );
438 $this->set_vars_post__in( &$vars, $cposts, SORT_NUMERIC );
439 break;
440
441
442 case 'column-page-slug' :
443 foreach ( (array) $this->get_any_posts_by_posttype($post_type) as $p )
444 $cposts[$p->ID] = $p->post_name;
445 $this->set_vars_post__in( &$vars, $cposts );
446 break;
447
448 endswitch;
449
450 return $vars;
451 }
452
453 /**
454 * Set post__in for use in WP_Query
455 *
456 * This will order the ID's asc or desc and set the appropriate filters.
457 *
458 * @since 1.2.1
459 */
460 private function set_vars_post__in( &$vars, $sortposts, $sort_flags = SORT_REGULAR )
461 {
462 // sort post ids by value
463 if ( $vars['order'] == 'asc' )
464 asort($sortposts, $sort_flags);
465 else
466 arsort($sortposts, $sort_flags);
467
468 // this will make sure WP_Query will use the order of the ids that we have just set in 'post__in'
469 add_filter('posts_orderby', array( &$this, 'filter_orderby_post__in'), 10, 2 );
470
471 // cleanup the vars we dont need
472 $vars['order'] = '';
473 $vars['orderby'] = '';
474
475 // add the sorted post ids to the query with the use of post__in
476 $vars['post__in'] = array_keys($sortposts);
477 }
478
479 /**
480 * Get orderby type
481 *
482 * @since 1.1
483 */
484 private function get_orderby_type($orderby, $type)
485 {
486 $db_columns = $this->get_stored_columns($type);
487
488 if ( $db_columns ) {
489 foreach ( $db_columns as $id => $vars ) {
490
491 // check which custom column was clicked
492 if ( isset( $vars['label'] ) && $orderby == $this->sanitize_string( $vars['label'] ) ) {
493 $column[$id] = $vars;
494 return $column;
495 }
496 }
497 }
498 return false;
499 }
500
501 /**
502 * Maintain order of ids that are set in the post__in var.
503 *
504 * This will force the returned posts to use the order of the ID's that
505 * have been set in post__in. Without this the ID's will be set in numeric order.
506 * See the WP_Query object for more info about the use of post__in.
507 *
508 * @since 1.2.1
509 */
510 public function filter_orderby_post__in($orderby, $wp)
511 {
512 global $wpdb;
513
514 // we need the query vars
515 $vars = $wp->query_vars;
516 if ( ! empty ( $vars['post__in'] ) ) {
517 // now we can get the ids
518 $ids = implode(',', $vars['post__in']);
519
520 // by adding FIELD to the SQL query we are forcing the order of the ID's
521 return "FIELD ({$wpdb->prefix}posts.ID,{$ids})";
522 }
523 }
524
525 /**
526 * Get any posts by post_type
527 *
528 * @since 1.2.1
529 */
530 private function get_any_posts_by_posttype( $post_type )
531 {
532 $allposts = get_posts(array(
533 'numberposts' => -1,
534 'post_status' => 'any',
535 'post_type' => $post_type
536 ));
537 return $allposts;
538 }
539 /**
540 * Request URI is Media
541 *
542 * @since 1.3
543 */
544 private function request_uri_is_media()
545 {
546 if (strpos( $_SERVER['REQUEST_URI'], '/upload.php' ) !== false )
547 return true;
548
549 return false;
550 }
551
552 /**
553 * Request URI is Users
554 *
555 * @since 1.3
556 */
557 private function request_uri_is_users()
558 {
559 if (strpos( $_SERVER['REQUEST_URI'], '/users.php' ) !== false )
560 return true;
561
562 return false;
563 }
564
565 /**
566 * Prepare the value for being by sorting
567 *
568 * @since 1.3
569 */
570 private function prepare_sort_string_value($string)
571 {
572 // remove tags and only get the first 20 chars and force lowercase.
573 $string = strtolower( substr( $this->strip_trim($string),0 ,20 ) );
574
575 return $string;
576 }
577
578 /**
579 * Get users data
580 *
581 * @since 1.3
582 */
583 function get_users_data()
584 {
585 $userdatas = array();
586 $wp_users = get_users( array(
587 'blog_id' => $GLOBALS['blog_id'],
588 ));
589 foreach ( $wp_users as $u ) {
590 $userdatas[$u->ID] = get_userdata($u->ID);
591 }
592 return $userdatas;
593 }
594 }