PluginProbe ʕ •ᴥ•ʔ
Admin Columns / 1.4.8
Admin Columns v1.4.8
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
values 13 years ago license.php 13 years ago sortable.php 13 years ago utility.php 13 years ago values.php 13 years ago
sortable.php
1250 lines
1 <?php
2
3 /**
4 * Coderess Sortable Columns Class
5 *
6 * @since 1.3
7 *
8 */
9 class Codepress_Sortable_Columns extends Codepress_Admin_Columns
10 {
11 private $post_types,
12 $unlocked,
13 $show_all_results,
14 $current_user_id;
15
16 /**
17 * Constructor
18 *
19 * @since 1.0
20 */
21 function __construct()
22 {
23 add_action( 'wp_loaded', array( $this, 'init') );
24 }
25
26 /**
27 * Initialize
28 *
29 * @since 1.0
30 */
31 public function init()
32 {
33 $licence = new cpac_licence('sortable');
34
35 // vars
36 $this->unlocked = $licence->is_unlocked();
37 $this->post_types = Codepress_Admin_Columns::get_post_types();
38 $this->show_all_results = false;
39 $this->current_user_id = get_current_user_id();
40
41 // init sorting
42 add_action( 'admin_init', array( $this, 'register_sortable_columns' ) );
43
44 // init filtering
45 add_action( 'admin_init', array( $this, 'register_filtering_columns' ) );
46
47 // handle requests for sorting columns
48 add_filter( 'request', array( $this, 'handle_requests_orderby_column'), 1 );
49 add_action( 'pre_user_query', array( $this, 'handle_requests_orderby_users_column'), 1 );
50 add_action( 'admin_init', array( $this, 'handle_requests_orderby_links_column'), 1 );
51 add_action( 'admin_init', array( $this, 'handle_requests_orderby_comments_column'), 1 );
52 }
53
54 /**
55 * Register sortable columns
56 *
57 * Hooks into apply_filters( "manage_{$screen->id}_sortable_columns" ) which is found in class-wp-list-table.php
58 *
59 * @since 1.0
60 */
61 function register_sortable_columns()
62 {
63 if ( ! $this->unlocked )
64 return false;
65
66 /** Posts */
67 foreach ( $this->post_types as $post_type ) {
68 add_filter( "manage_edit-{$post_type}_sortable_columns", array($this, 'callback_add_sortable_posts_column'));
69 }
70
71 /** Users */
72 add_filter( "manage_users_sortable_columns", array($this, 'callback_add_sortable_users_column'));
73
74 /** Media */
75 add_filter( "manage_upload_sortable_columns", array($this, 'callback_add_sortable_media_column'));
76
77 /** Links */
78 add_filter( "manage_link-manager_sortable_columns", array($this, 'callback_add_sortable_links_column'));
79
80 /** Comments */
81 add_filter( "manage_edit-comments_sortable_columns", array($this, 'callback_add_sortable_comments_column'));
82 }
83
84 /**
85 * Callback add Posts sortable column
86 *
87 * @since 1.0
88 */
89 public function callback_add_sortable_posts_column($columns)
90 {
91 global $post_type;
92
93 // in some cases post_type is an array ( when clicking a tag inside the overview screen icm CCTM ), then we use this as a fallback so we get a string
94 if ( is_array($post_type) )
95 $post_type = $_REQUEST['post_type'];
96
97 return $this->add_managed_sortable_columns($post_type, $columns);
98 }
99
100 /**
101 * Callback add Users sortable column
102 *
103 * @since 1.1
104 */
105 public function callback_add_sortable_users_column($columns)
106 {
107 return $this->add_managed_sortable_columns('wp-users', $columns);
108 }
109
110 /**
111 * Callback add Media sortable column
112 *
113 * @since 1.3
114 */
115 public function callback_add_sortable_media_column($columns)
116 {
117 return $this->add_managed_sortable_columns('wp-media', $columns);
118 }
119
120 /**
121 * Callback add Links sortable column
122 *
123 * @since 1.3.1
124 */
125 public function callback_add_sortable_links_column($columns)
126 {
127 return $this->add_managed_sortable_columns('wp-links', $columns);
128 }
129
130 /**
131 * Callback add Comments sortable column
132 *
133 * @since 1.3.1
134 */
135 public function callback_add_sortable_comments_column($columns)
136 {
137 return $this->add_managed_sortable_columns('wp-comments', $columns);
138 }
139
140 /**
141 * Add managed sortable columns by Type
142 *
143 * @since 1.1
144 */
145 private function add_managed_sortable_columns( $type = 'post', $columns )
146 {
147 $display_columns = $this->get_merged_columns($type);
148
149 if ( ! $display_columns )
150 return $columns;
151
152 foreach ( $display_columns as $id => $vars ) {
153 if ( isset($vars['options']['sortorder']) && $vars['options']['sortorder'] == 'on' ){
154
155 // register format
156 $columns[$id] = $this->sanitize_string($vars['label']);
157 }
158 }
159
160 return $columns;
161 }
162
163 /**
164 * Admin requests for orderby column
165 *
166 * Only works for WP_Query objects ( such as posts and media )
167 *
168 * @since 1.0
169 */
170 public function handle_requests_orderby_column( $vars )
171 {
172 /** Users */
173 // You would expect to see get_orderby_users_vars(), but sorting for
174 // users is handled through a different filter. Not 'request', but 'pre_user_query'.
175 // See handle_requests_orderby_users_column().
176
177 /** Media */
178 if ( $this->request_uri_is('upload') ) {
179 $vars = $this->get_orderby_media_vars($vars);
180 }
181
182 /** Posts */
183 elseif ( !empty($vars['post_type']) ) {
184 $vars = $this->get_orderby_posts_vars($vars);
185 }
186
187 return $vars;
188 }
189
190 /**
191 * Orderby Users column
192 *
193 * @since 1.3
194 */
195 public function handle_requests_orderby_users_column($user_query)
196 {
197 // query vars
198 $vars = $user_query->query_vars;
199
200 // Column
201 $column = $this->get_orderby_type( $vars['orderby'], 'wp-users' );
202
203 if ( empty($column) )
204 return $user_query;
205
206 // id
207 $type = $id = key($column);
208
209 // Check for user custom fields: column-meta-[customfieldname]
210 if ( Codepress_Admin_Columns::is_column_meta($type) )
211 $type = 'column-user-meta';
212
213 // Check for post count: column-user_postcount-[posttype]
214 if ( Codepress_Admin_Columns::get_posttype_by_postcount_column($type) )
215 $type = 'column-user_postcount';
216
217 // var
218 $cusers = array();
219 switch( $type ) :
220
221 case 'column-user_id':
222 $user_query->query_orderby = "ORDER BY ID {$user_query->query_vars['order']}";
223 $user_query->query_vars['orderby'] = 'ID';
224 break;
225
226 case 'column-user_registered':
227 $user_query->query_orderby = "ORDER BY user_registered {$user_query->query_vars['order']}";
228 $user_query->query_vars['orderby'] = 'registered';
229 break;
230
231 case 'column-nickname' :
232 $sort_flag = SORT_REGULAR;
233 foreach ( $this->get_users_data() as $u ) {
234 if ($u->nickname || $this->show_all_results ) {
235 $cusers[$u->ID] = $this->prepare_sort_string_value($u->nickname);
236 }
237 }
238 break;
239
240 case 'column-first_name' :
241 $sort_flag = SORT_REGULAR;
242 foreach ( $this->get_users_data() as $u ) {
243 if ($u->first_name || $this->show_all_results ) {
244 $cusers[$u->ID] = $this->prepare_sort_string_value($u->first_name);
245 }
246 }
247 break;
248
249 case 'column-last_name' :
250 $sort_flag = SORT_REGULAR;
251 foreach ( $this->get_users_data() as $u ) {
252 if ($u->last_name || $this->show_all_results ) {
253 $cusers[$u->ID] = $this->prepare_sort_string_value($u->last_name);
254 }
255 }
256 break;
257
258 case 'column-user_url' :
259 $sort_flag = SORT_REGULAR;
260 foreach ( $this->get_users_data() as $u ) {
261 if ($u->user_url || $this->show_all_results ) {
262 $cusers[$u->ID] = $this->prepare_sort_string_value($u->user_url);
263 }
264 }
265 break;
266
267 case 'column-user_description' :
268 $sort_flag = SORT_REGULAR;
269 foreach ( $this->get_users_data() as $u ) {
270 if ($u->user_description || $this->show_all_results ) {
271 $cusers[$u->ID] = $this->prepare_sort_string_value($u->user_description);
272 }
273 }
274 break;
275
276 case 'column-user_postcount' :
277 $post_type = Codepress_Admin_Columns::get_posttype_by_postcount_column($id);
278 if ( $post_type ) {
279 $sort_flag = SORT_REGULAR;
280 foreach ( $this->get_users_data() as $u ) {
281 $count = Codepress_Admin_Columns::get_post_count( $post_type, $u->ID );
282 $cusers[$u->ID] = $this->prepare_sort_string_value($count);
283 }
284 }
285 break;
286
287 case 'column-user-meta' :
288 $field = $column[$id]['field'];
289 if ( $field ) {
290
291 // order numeric or string
292 $sort_flag = SORT_REGULAR;
293 if ( $column[$id]['field_type'] == 'numeric' || $column[$id]['field_type'] == 'library_id' ) {
294 $sort_flag = SORT_NUMERIC;
295 }
296
297 // sort by metavalue
298 foreach ( $this->get_users_data() as $u ) {
299 $value = get_metadata('user', $u->ID, $field, true);
300 $cusers[$u->ID] = $this->prepare_sort_string_value($value);
301 }
302 }
303 break;
304
305 /** native WP columns */
306
307 // role column
308 case 'role' :
309 $sort_flag = SORT_REGULAR;
310 foreach ( $this->get_users_data() as $u ) {
311 $role = !empty($u->roles[0]) ? $u->roles[0] : '';
312 if ($role || $this->show_all_results ) {
313 $cusers[$u->ID] = $this->prepare_sort_string_value($role);
314 }
315 }
316 break;
317
318 endswitch;
319
320 if ( isset($sort_flag) ) {
321 $user_query = $this->get_users_query_vars( $user_query, $cusers, $sort_flag );
322 }
323
324 return $user_query;
325 }
326
327 /**
328 * Orderby Links column
329 *
330 * Makes use of filter 'get_bookmarks' from bookmark.php to change the result set of the links
331 *
332 * @since 1.3.1
333 */
334 public function handle_requests_orderby_links_column()
335 {
336 // fire only when we are in the admins link-manager
337 if ( $this->request_uri_is('link-manager') )
338 add_filter( 'get_bookmarks', array( $this, 'callback_requests_orderby_links_column'), 10, 2);
339 }
340
341 /**
342 * Orderby Links column
343 *
344 * @since 1.3.1
345 */
346 public function callback_requests_orderby_links_column($results, $vars)
347 {
348 global $wpdb;
349
350 // apply sorting preference
351 $this->apply_sorting_preference( $vars, 'wp-links' );
352
353 // Column
354 $column = $this->get_orderby_type( $vars['orderby'], 'wp-links' );
355
356 if ( empty($column) )
357 return $results;
358
359 // id
360 $type = $id = key($column);
361
362 // var
363 $length = '';
364 switch( $type ) :
365
366 case 'column-link_id':
367 if ( version_compare( get_bloginfo('version'), '3.2', '>' ) )
368 $vars['orderby'] = 'link_id';
369 else
370 $vars['orderby'] = 'id';
371 break;
372
373 case 'column-owner':
374 $vars['orderby'] = 'link_owner';
375 break;
376
377 case 'column-length':
378 $vars['orderby'] = 'length';
379 $length = ", CHAR_LENGTH(link_name) AS length";
380 break;
381
382 case 'column-target':
383 $vars['orderby'] = 'link_target';
384 break;
385
386 case 'column-description':
387 $vars['orderby'] = 'link_description';
388 break;
389
390 case 'column-notes':
391 $vars['orderby'] = 'link_notes';
392 break;
393
394 case 'column-rss':
395 $vars['orderby'] = 'link_rss';
396 break;
397
398 /** native WP columns */
399
400 // Relationship
401 case 'rel':
402 $vars['orderby'] = 'link_rel';
403 break;
404
405 default:
406 $vars['orderby'] = '';
407
408 endswitch;
409
410 // get bookmarks by orderby vars
411 if ( $vars['orderby'] ) {
412 $vars['order'] = mysql_escape_string($vars['order']);
413 $sql = "SELECT * {$length} FROM {$wpdb->links} WHERE 1=1 ORDER BY {$vars['orderby']} {$vars['order']}";
414 $results = $wpdb->get_results($sql);
415
416 // check for errors
417 if( is_wp_error($results) )
418 return false;
419 }
420
421 return $results;
422 }
423
424 /**
425 * Orderby Comments column
426 *
427 * @since 1.3.1
428 */
429 public function callback_requests_orderby_comments_column($pieces, $ref_comment)
430 {
431 // get query vars
432 $vars = $ref_comment->query_vars;
433
434 // Column
435 $column = $this->get_orderby_type( $vars['orderby'], 'wp-comments' );
436
437 if ( empty($column) )
438 return $pieces;
439
440 // id
441 $type = $id = key($column);
442
443 // var
444 switch( $type ) :
445
446 case 'column-comment_id':
447 $pieces['orderby'] = 'comment_ID';
448 break;
449
450 case 'column-author_author':
451 $pieces['orderby'] = 'comment_author';
452 break;
453
454 case 'column-author_ip':
455 $pieces['orderby'] = 'comment_author_IP';
456 break;
457
458 case 'column-author_url':
459 $pieces['orderby'] = 'comment_author_url';
460 break;
461
462 case 'column-author_email':
463 $pieces['orderby'] = 'comment_author_email';
464 break;
465
466 case 'column-reply_to':
467 break;
468
469 case 'column-approved':
470 $pieces['orderby'] = 'comment_approved';
471 break;
472
473 case 'column-date':
474 $pieces['orderby'] = 'comment_date';
475 break;
476
477 case 'column-agent':
478 $pieces['orderby'] = 'comment_agent';
479 break;
480
481 case 'column-excerpt':
482 $pieces['orderby'] = 'comment_content';
483 break;
484
485 case 'column-date_gmt':
486 break;
487
488 /** native WP columns */
489
490 // Relationship
491 case 'comment':
492 $pieces['orderby'] = 'comment_content';
493 break;
494
495 default:
496 $vars['orderby'] = '';
497
498 endswitch;
499
500 return $pieces;
501 }
502
503 /**
504 * Orderby Comments column
505 *
506 * @since 1.3.1
507 */
508 public function handle_requests_orderby_comments_column()
509 {
510 // fire only when we are in the admins edit-comments
511 if ( $this->request_uri_is('edit-comments') ) {
512 add_filter('comments_clauses', array( $this, 'callback_requests_orderby_comments_column'), 10, 2);
513 }
514 }
515
516 /**
517 * Get sorting vars in User Query Object
518 *
519 * @since 1.3
520 */
521 private function get_users_query_vars( $user_query, $sortusers, $sort_flags = SORT_REGULAR )
522 {
523 global $wpdb;
524
525 // vars
526 $vars = $user_query->query_vars;
527
528 // sorting
529 if ( $vars['order'] == 'ASC' )
530 asort($sortusers, $sort_flags);
531 else
532 arsort($sortusers, $sort_flags);
533
534 // alter orderby SQL
535 if ( ! empty ( $sortusers ) ) {
536 $ids = implode(',', array_keys($sortusers));
537 $user_query->query_where .= " AND {$wpdb->prefix}users.ID IN ({$ids})";
538 $user_query->query_orderby = "ORDER BY FIELD({$wpdb->prefix}users.ID,{$ids})";
539 }
540
541 // cleanup the vars we dont need
542 $vars['order'] = '';
543 $vars['orderby'] = '';
544
545 // set query vars
546 $user_query->query_vars = $vars;
547
548 return $user_query;
549 }
550
551 /**
552 * Orderby Media column
553 *
554 * @since 1.3
555 */
556 private function get_orderby_media_vars($vars)
557 {
558 // apply sorting preference
559 $this->apply_sorting_preference( $vars, 'wp-media' );
560
561 // when sorting still isn't set we will just return the requested vars
562 if ( empty( $vars['orderby'] ) )
563 return $vars;
564
565 // Column
566 $column = $this->get_orderby_type( $vars['orderby'], 'wp-media' );
567
568 if ( empty($column) )
569 return $vars;
570
571 $id = key($column);
572
573 // var
574 $cposts = array();
575 switch( $id ) :
576
577 case 'column-mediaid' :
578 $vars['orderby'] = 'ID';
579 break;
580
581 case 'column-width' :
582 $sort_flag = SORT_NUMERIC;
583 foreach ( $this->get_any_posts_by_posttype('attachment') as $p ) {
584 $meta = wp_get_attachment_metadata($p->ID);
585 $width = !empty($meta['width']) ? $meta['width'] : 0;
586 if ( $width || $this->show_all_results )
587 $cposts[$p->ID] = $width;
588 }
589 break;
590
591 case 'column-height' :
592 $sort_flag = SORT_NUMERIC;
593 foreach ( $this->get_any_posts_by_posttype('attachment') as $p ) {
594 $meta = wp_get_attachment_metadata($p->ID);
595 $height = !empty($meta['height']) ? $meta['height'] : 0;
596 if ( $height || $this->show_all_results )
597 $cposts[$p->ID] = $height;
598 }
599 break;
600
601 case 'column-dimensions' :
602 $sort_flag = SORT_NUMERIC;
603 foreach ( $this->get_any_posts_by_posttype('attachment') as $p ) {
604 $meta = wp_get_attachment_metadata($p->ID);
605 $height = !empty($meta['height']) ? $meta['height'] : 0;
606 $width = !empty($meta['width']) ? $meta['width'] : 0;
607 $surface = $height*$width;
608
609 if ( $surface || $this->show_all_results )
610 $cposts[$p->ID] = $surface;
611 }
612 break;
613
614 case 'column-caption' :
615 $sort_flag = SORT_STRING;
616 foreach ( $this->get_any_posts_by_posttype('attachment') as $p ) {
617 if ( $p->post_excerpt || $this->show_all_results ) {
618 $cposts[$p->ID] = $this->prepare_sort_string_value($p->post_excerpt);
619 }
620 }
621 break;
622
623 case 'column-description' :
624 $sort_flag = SORT_STRING;
625 foreach ( $this->get_any_posts_by_posttype('attachment') as $p ) {
626 if ( $p->post_content || $this->show_all_results ) {
627 $cposts[$p->ID] = $this->prepare_sort_string_value( $p->post_content );
628 }
629 }
630 break;
631
632 case 'column-mime_type' :
633 $sort_flag = SORT_STRING;
634 foreach ( $this->get_any_posts_by_posttype('attachment') as $p ) {
635 if ( $p->post_mime_type || $this->show_all_results ) {
636 $cposts[$p->ID] = $this->prepare_sort_string_value( $p->post_mime_type );
637 }
638 }
639 break;
640
641 case 'column-file_name' :
642 $sort_flag = SORT_STRING;
643 foreach ( $this->get_any_posts_by_posttype('attachment') as $p ) {
644 $meta = get_post_meta($p->ID, '_wp_attached_file', true);
645 $file = !empty($meta) ? basename($meta) : '';
646 if ( $file || $this->show_all_results ) {
647 $cposts[$p->ID] = $file;
648 }
649 }
650 break;
651
652 case 'column-alternate_text' :
653 $sort_flag = SORT_STRING;
654 foreach ( $this->get_any_posts_by_posttype('attachment') as $p ) {
655 $alt = get_post_meta($p->ID, '_wp_attachment_image_alt', true);
656 if ( $alt || $this->show_all_results ) {
657 $cposts[$p->ID] = $this->prepare_sort_string_value( $alt );
658 }
659 }
660 break;
661
662 case 'column-filesize' :
663 $sort_flag = SORT_NUMERIC;
664 foreach ( $this->get_any_posts_by_posttype('attachment') as $p ) {
665 $file = wp_get_attachment_url($p->ID);
666 if ( $file || $this->show_all_results ) {
667 $abs = str_replace( WP_CONTENT_URL, WP_CONTENT_DIR, $file);
668 $cposts[$p->ID] = $this->prepare_sort_string_value( filesize($abs) );
669 }
670 }
671 break;
672
673 endswitch;
674
675 // we will add the sorted post ids to vars['post__in'] and remove unused vars
676 if ( isset($sort_flag) ) {
677 $vars = $this->get_vars_post__in( $vars, $cposts, $sort_flag );
678 }
679
680 return $vars;
681 }
682
683 /**
684 * Orderby Posts column
685 *
686 * @since 1.3
687 */
688 private function get_orderby_posts_vars($vars)
689 {
690 $post_type = $vars['post_type'];
691
692 // apply sorting preference
693 $this->apply_sorting_preference( $vars, $post_type );
694
695 // no sorting
696 if ( empty( $vars['orderby'] ) ) {
697 return $vars;
698 }
699
700 // Column
701 $column = $this->get_orderby_type( $vars['orderby'], $post_type );
702
703 if ( empty($column) )
704 return $vars;
705
706 // id
707 $type = $id = key($column);
708
709 // Check for taxonomies, such as column-taxonomy-[taxname]
710 if ( strpos($type, 'column-taxonomy-') !== false )
711 $type = 'column-taxonomy';
712
713 // Check for Custom Field
714 if ( Codepress_Admin_Columns::is_column_meta($type) )
715 $type = 'column-post-meta';
716
717 // var
718 $cposts = array();
719 switch( $type ) :
720
721 case 'column-postid' :
722 $vars['orderby'] = 'ID';
723 break;
724
725 case 'column-order' :
726 $vars['orderby'] = 'menu_order';
727 break;
728
729 case 'column-modified' :
730 $vars['orderby'] = 'modified';
731 break;
732
733 case 'column-comment-count' :
734 $vars['orderby'] = 'comment_count';
735 break;
736
737 case 'column-post-meta' :
738 $field = $column[$id]['field'];
739
740 // orderby type
741 $field_type = 'meta_value';
742 if ( $column[$id]['field_type'] == 'numeric' || $column[$id]['field_type'] == 'library_id' )
743 $field_type = 'meta_value_num';
744
745 $vars = array_merge($vars, array(
746 'meta_key' => $field,
747 'orderby' => $field_type
748 ));
749 break;
750
751 case 'column-excerpt' :
752 $sort_flag = SORT_STRING;
753 foreach ( $this->get_any_posts_by_posttype($post_type) as $p ) {
754 $cposts[$p->ID] = $this->prepare_sort_string_value($p->post_content);
755 }
756 break;
757
758 case 'column-word-count' :
759 $sort_flag = SORT_NUMERIC;
760 foreach ( $this->get_any_posts_by_posttype($post_type) as $p ) {
761 $cposts[$p->ID] = str_word_count( Codepress_Admin_Columns::strip_trim( $p->post_content ) );
762 }
763 break;
764
765 case 'column-page-template' :
766 $sort_flag = SORT_STRING;
767 $templates = get_page_templates();
768 foreach ( $this->get_any_posts_by_posttype($post_type) as $p ) {
769 $page_template = get_post_meta($p->ID, '_wp_page_template', true);
770 $cposts[$p->ID] = array_search($page_template, $templates);
771 }
772 break;
773
774 case 'column-post_formats' :
775 $sort_flag = SORT_REGULAR;
776 foreach ( $this->get_any_posts_by_posttype($post_type) as $p ) {
777 $cposts[$p->ID] = get_post_format($p->ID);
778 }
779 break;
780
781 case 'column-attachment' :
782 case 'column-attachment-count' :
783 $sort_flag = SORT_NUMERIC;
784 foreach ( $this->get_any_posts_by_posttype($post_type) as $p ) {
785 $cposts[$p->ID] = count( Codepress_Admin_Columns::get_attachment_ids($p->ID) );
786 }
787 break;
788
789 case 'column-page-slug' :
790 $sort_flag = SORT_REGULAR;
791 foreach ( $this->get_any_posts_by_posttype($post_type) as $p ) {
792 $cposts[$p->ID] = $p->post_name;
793 }
794 break;
795
796 case 'column-sticky' :
797 $sort_flag = SORT_REGULAR;
798 $stickies = get_option('sticky_posts');
799 foreach ( $this->get_any_posts_by_posttype($post_type) as $p ) {
800 $cposts[$p->ID] = $p->ID;
801 if ( !empty($stickies) && in_array($p->ID, $stickies ) ) {
802 $cposts[$p->ID] = 0;
803 }
804 }
805 break;
806
807 case 'column-featured_image' :
808 $sort_flag = SORT_REGULAR;
809 foreach ( $this->get_any_posts_by_posttype($post_type) as $p ) {
810 $cposts[$p->ID] = $p->ID;
811 $thumb = get_the_post_thumbnail($p->ID);
812 if ( !empty($thumb) ) {
813 $cposts[$p->ID] = 0;
814 }
815 }
816 break;
817
818 case 'column-roles' :
819 $sort_flag = SORT_STRING;
820 foreach ( $this->get_any_posts_by_posttype($post_type) as $p ) {
821 $cposts[$p->ID] = 0;
822 $userdata = get_userdata($p->post_author);
823 if ( !empty($userdata->roles[0]) ) {
824 $cposts[$p->ID] = $userdata->roles[0];
825 }
826 }
827 break;
828
829 case 'column-status' :
830 $sort_flag = SORT_STRING;
831 foreach ( $this->get_any_posts_by_posttype($post_type) as $p ) {
832 $cposts[$p->ID] = $p->post_status.strtotime($p->post_date);
833 }
834 break;
835
836 case 'column-comment-status' :
837 $sort_flag = SORT_STRING;
838 foreach ( $this->get_any_posts_by_posttype($post_type) as $p ) {
839 $cposts[$p->ID] = $p->comment_status;
840 }
841 break;
842
843 case 'column-ping-status' :
844 $sort_flag = SORT_STRING;
845 foreach ( $this->get_any_posts_by_posttype($post_type) as $p ) {
846 $cposts[$p->ID] = $p->ping_status;
847 }
848 break;
849
850 case 'column-taxonomy' :
851 $sort_flag = SORT_STRING; // needed to sort
852 $taxonomy = str_replace('column-taxonomy-', '', $id);
853 $cposts = $this->get_posts_sorted_by_taxonomy($post_type, $taxonomy);
854 break;
855
856 case 'column-author-name' :
857 $sort_flag = SORT_STRING;
858 $display_as = $column[$id]['display_as'];
859 if( 'userid' == $display_as ) {
860 $sort_flag = SORT_NUMERIC;
861 }
862 foreach ( $this->get_any_posts_by_posttype($post_type) as $p ) {
863 if ( !empty($p->post_author) ) {
864 $name = Codepress_Admin_Columns::get_author_field_by_nametype($display_as, $p->post_author);
865 $cposts[$p->ID] = $name;
866 }
867 }
868 break;
869
870 case 'column-before-moretag' :
871 $sort_flag = SORT_STRING;
872 foreach ( $this->get_any_posts_by_posttype($post_type) as $p ) {
873 $extended = get_extended($p->post_content);
874 $content = !empty($extended['extended']) ? $extended['main'] : '';
875 $cposts[$p->ID] = $this->prepare_sort_string_value($content);
876 }
877 break;
878
879 /** native WP columns */
880
881 // categories
882 case 'categories' :
883 $sort_flag = SORT_STRING; // needed to sort
884 $cposts = $this->get_posts_sorted_by_taxonomy($post_type, 'category');
885 break;
886
887 // tags
888 case 'tags' :
889 $sort_flag = SORT_STRING; // needed to sort
890 $cposts = $this->get_posts_sorted_by_taxonomy($post_type, 'post_tag');
891 break;
892
893 endswitch;
894
895 // we will add the sorted post ids to vars['post__in'] and remove unused vars
896 if ( isset($sort_flag) ) {
897 $vars = $this->get_vars_post__in( $vars, $cposts, $sort_flag );
898 }
899
900 return $vars;
901 }
902
903 /**
904 * Set sorting preference
905 *
906 * after sorting we will save this sorting preference to the column item
907 * we set the default_order to either asc, desc or empty.
908 * only ONE column item PER type can have a default_order
909 *
910 * @since 1.4.6.5
911 */
912 function set_sorting_preference( $type, $orderby = '', $order = 'asc' )
913 {
914 if ( !$orderby )
915 return false;
916
917 $options = get_user_meta( $this->current_user_id, 'cpac_sorting_preference', true );
918
919 $options[$type] = array(
920 'orderby' => $orderby,
921 'order' => $order
922 );
923
924 update_user_meta( $this->current_user_id, 'cpac_sorting_preference', $options );
925 }
926
927 /**
928 * Get sorting preference
929 *
930 * The default sorting of the column is saved to it's property default_order.
931 * Returns the orderby and order value of that column.
932 *
933 * @since 1.4.6.5
934 */
935 function get_sorting_preference( $type )
936 {
937 $options = get_user_meta( $this->current_user_id, 'cpac_sorting_preference', true );
938
939 if ( empty($options[$type]) )
940 return false;
941
942 return $options[$type];
943 }
944
945 /**
946 * Apply sorting preference
947 *
948 * @since 1.4.6.5
949 */
950 function apply_sorting_preference( &$vars, $type )
951 {
952 // user has not sorted
953 if ( empty( $vars['orderby'] ) ) {
954
955 // did the user sorted this column some other time?
956 if ( $preference = $this->get_sorting_preference($type) ) {
957 $vars['orderby'] = $preference['orderby'];
958 $vars['order'] = $preference['order'];
959
960 // used by active state in column header
961 $_GET['orderby'] = $preference['orderby'];
962 $_GET['order'] = $preference['order'];
963 }
964 }
965
966 // save the order preference
967 if ( !empty( $vars['orderby'] ) ) {
968 $this->set_sorting_preference( $type, $vars['orderby'], $vars['order'] );
969 }
970 }
971
972 /**
973 * Get posts sorted by taxonomy
974 *
975 * This will post ID's by the first term in the taxonomy
976 *
977 * @since 1.4.5
978 */
979 function get_posts_sorted_by_taxonomy($post_type, $taxonomy = 'category')
980 {
981 $cposts = array();
982 foreach ( $this->get_any_posts_by_posttype($post_type) as $p ) {
983 $cposts[$p->ID] = '';
984 $terms = get_the_terms($p->ID, $taxonomy);
985 if ( !is_wp_error($terms) && !empty($terms) ) {
986 // only use the first term to sort
987 $term = array_shift(array_values($terms));
988 if ( isset($term->term_id) ) {
989 $cposts[$p->ID] = sanitize_term_field('name', $term->name, $term->term_id, $term->taxonomy, 'display');
990 }
991 }
992 }
993 return $cposts;
994 }
995
996 /**
997 * Set post__in for use in WP_Query
998 *
999 * This will order the ID's asc or desc and set the appropriate filters.
1000 *
1001 * @since 1.2.1
1002 */
1003 private function get_vars_post__in( &$vars, $sortposts, $sort_flags = SORT_REGULAR )
1004 {
1005 // sort post ids by value
1006 if ( $vars['order'] == 'asc' )
1007 asort($sortposts, $sort_flags);
1008 else
1009 arsort($sortposts, $sort_flags);
1010
1011 // this will make sure WP_Query will use the order of the ids that we have just set in 'post__in'
1012 // set priority higher then default to prevent conflicts with 3rd party plugins
1013 add_filter('posts_orderby', array( $this, 'filter_orderby_post__in'), 10, 2 );
1014
1015 // cleanup the vars we dont need
1016 $vars['order'] = '';
1017 $vars['orderby'] = '';
1018
1019 // add the sorted post ids to the query with the use of post__in
1020 $vars['post__in'] = array_keys($sortposts);
1021
1022 return $vars;
1023 }
1024
1025 /**
1026 * Get orderby type
1027 *
1028 * @since 1.1
1029 */
1030 private function get_orderby_type($orderby, $type)
1031 {
1032 $db_columns = Codepress_Admin_Columns::get_stored_columns($type);
1033
1034 if ( $db_columns ) {
1035 foreach ( $db_columns as $id => $vars ) {
1036
1037 // check which custom column was clicked
1038 if ( isset( $vars['label'] ) && $orderby == $this->sanitize_string( $vars['label'] ) ) {
1039 $column[$id] = $vars;
1040 return $column;
1041 }
1042 }
1043 }
1044 return false;
1045 }
1046
1047 /**
1048 * Maintain order of ids that are set in the post__in var.
1049 *
1050 * This will force the returned posts to use the order of the ID's that
1051 * have been set in post__in. Without this the ID's will be set in numeric order.
1052 * See the WP_Query object for more info about the use of post__in.
1053 *
1054 * @since 1.2.1
1055 */
1056 public function filter_orderby_post__in($orderby, $wp)
1057 {
1058 global $wpdb;
1059
1060 // we need the query vars
1061 $vars = $wp->query_vars;
1062 if ( ! empty ( $vars['post__in'] ) ) {
1063 // now we can get the ids
1064 $ids = implode(',', $vars['post__in']);
1065
1066 // by adding FIELD to the SQL query we are forcing the order of the ID's
1067 return "FIELD({$wpdb->prefix}posts.ID,{$ids})";
1068 }
1069 }
1070
1071 /**
1072 * Get any posts by post_type
1073 *
1074 * @since 1.2.1
1075 */
1076 private function get_any_posts_by_posttype( $post_type )
1077 {
1078 $any_posts = (array) get_posts(array(
1079 'numberposts' => -1,
1080 'post_status' => 'any',
1081 'post_type' => $post_type
1082 ));
1083
1084 // trash posts are not included in the posts_status 'any' by default
1085 $trash_posts = (array) get_posts(array(
1086 'numberposts' => -1,
1087 'post_status' => 'trash',
1088 'post_type' => $post_type
1089 ));
1090
1091 $all_posts = array_merge($any_posts, $trash_posts);
1092
1093 return (array) $all_posts;
1094 }
1095
1096 /**
1097 * Request URI is
1098 *
1099 * @since 1.3.1
1100 */
1101 private function request_uri_is( $screen_id = '' )
1102 {
1103 if (strpos( $_SERVER['REQUEST_URI'], "/{$screen_id}.php" ) !== false )
1104 return true;
1105
1106 return false;
1107 }
1108
1109 /**
1110 * Prepare the value for being by sorting
1111 *
1112 * @since 1.3
1113 */
1114 private function prepare_sort_string_value($string)
1115 {
1116 // remove tags and only get the first 20 chars and force lowercase.
1117 $string = strtolower( substr( Codepress_Admin_Columns::strip_trim($string),0 ,20 ) );
1118
1119 return $string;
1120 }
1121
1122 /**
1123 * Get users data
1124 *
1125 * @since 1.3
1126 */
1127 function get_users_data()
1128 {
1129 $userdatas = array();
1130 $wp_users = get_users( array(
1131 'blog_id' => $GLOBALS['blog_id'],
1132 ));
1133 foreach ( $wp_users as $u ) {
1134 $userdatas[$u->ID] = get_userdata($u->ID);
1135 }
1136 return $userdatas;
1137 }
1138
1139 /**
1140 * Register filtering columns
1141 *
1142 * @since 1.4.2
1143 */
1144 function register_filtering_columns()
1145 {
1146 if ( ! $this->unlocked || apply_filters( 'cpac-remove-filtering-columns', true ) )
1147 return false;
1148
1149 // hook into wordpress
1150 add_action('restrict_manage_posts', array($this, 'callback_restrict_posts'));
1151 }
1152
1153 /**
1154 * Add taxonomy filters to posts
1155 *
1156 * @since 1.4.2
1157 */
1158 function callback_restrict_posts()
1159 {
1160 global $post_type_object;
1161
1162 if ( !isset($post_type_object->name) )
1163 return false;
1164
1165 // make a filter foreach taxonomy
1166 $taxonomies = get_object_taxonomies($post_type_object->name, 'names');
1167
1168 // get stored columns
1169 $db_columns = Codepress_Admin_Columns::get_stored_columns($post_type_object->name);
1170
1171 if ( $taxonomies ) {
1172 foreach ( $taxonomies as $tax ) {
1173
1174 // ignore core taxonomies
1175 if ( in_array($tax, array('post_tag','category','post_format') ) ) {
1176 continue;
1177 }
1178
1179 // only display taxonomy that is active as a column
1180 if ( isset($db_columns['column-taxonomy-'.$tax]) && $db_columns['column-taxonomy-'.$tax]['state'] == 'on' ) {
1181
1182 $terms = get_terms($tax);
1183 $terms = $this->indent($terms, 0, 'parent', 'term_id');
1184 $terms = $this->apply_dropdown_markup($terms);
1185
1186 $select = "<option value=''>".__('Show all ', CPAC_TEXTDOMAIN)."{$tax}</option>";
1187 if (!empty($terms)) {
1188 foreach( $terms as $term_slug => $term) {
1189
1190 $selected = isset($_GET[$tax]) && $term_slug == $_GET[$tax] ? " selected='selected'" : '';
1191 $select .= "<option value='{$term_slug}'{$selected}>{$term}</option>";
1192 }
1193 echo "<select class='postform' name='{$tax}'>{$select}</select>";
1194 }
1195 }
1196 }
1197 }
1198 }
1199
1200 /**
1201 * Applies dropdown markup for taxonomy dropdown
1202 *
1203 * @since 1.4.2
1204 */
1205 private function apply_dropdown_markup($array, $level = 0, $output = array())
1206 {
1207 foreach($array as $v) {
1208
1209 $prefix = '';
1210 for($i=0; $i<$level; $i++) {
1211 $prefix .= '&nbsp;&nbsp;';
1212 }
1213
1214 $output[$v->slug] = $prefix . $v->name;
1215
1216 if ( !empty($v->children) ) {
1217 $output = $this->apply_dropdown_markup($v->children, ($level + 1), $output);
1218 }
1219 }
1220
1221 return $output;
1222 }
1223
1224 /**
1225 * Indents any object as long as it has a unique id and that of its parent.
1226 *
1227 * @since 1.4.2
1228 */
1229 private function indent($array, $parentId = 0, $parentKey = 'post_parent', $selfKey = 'ID', $childrenKey = 'children')
1230 {
1231 $indent = array();
1232
1233 // clean counter
1234 $i = 0;
1235
1236 foreach($array as $v) {
1237
1238 if ($v->$parentKey == $parentId) {
1239 $indent[$i] = $v;
1240 $indent[$i]->$childrenKey = $this->indent($array, $v->$selfKey, $parentKey, $selfKey);
1241
1242 $i++;
1243 }
1244 }
1245
1246 return $indent;
1247 }
1248 }
1249
1250 ?>