sortable.php
890 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 | |
| 15 | /** |
| 16 | * Constructor |
| 17 | * |
| 18 | * @since 1.0 |
| 19 | */ |
| 20 | function __construct() |
| 21 | { |
| 22 | add_action( 'wp_loaded', array( $this, 'init') ); |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * Initialize |
| 27 | * |
| 28 | * @since 1.0 |
| 29 | */ |
| 30 | public function init() |
| 31 | { |
| 32 | // vars |
| 33 | $this->unlocked = $this->is_unlocked('sortable'); |
| 34 | $this->post_types = $this->get_post_types(); |
| 35 | $this->show_all_results = false; |
| 36 | |
| 37 | add_action( 'admin_init', array( $this, 'register_sortable_columns' ) ); |
| 38 | |
| 39 | // handle requests for sorting columns |
| 40 | add_filter( 'request', array( $this, 'handle_requests_orderby_column'), 1 ); |
| 41 | add_action( 'pre_user_query', array( $this, 'handle_requests_orderby_users_column'), 1 ); |
| 42 | add_action( 'admin_init', array( $this, 'handle_requests_orderby_links_column'), 1 ); |
| 43 | add_action( 'admin_init', array( $this, 'handle_requests_orderby_comments_column'), 1 ); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Register sortable columns |
| 48 | * |
| 49 | * Hooks into apply_filters( "manage_{$screen->id}_sortable_columns" ) which is found in class-wp-list-table.php |
| 50 | * |
| 51 | * @since 1.0 |
| 52 | */ |
| 53 | function register_sortable_columns() |
| 54 | { |
| 55 | if ( ! $this->unlocked ) |
| 56 | return false; |
| 57 | |
| 58 | /** Posts */ |
| 59 | foreach ( $this->post_types as $post_type ) |
| 60 | add_filter( "manage_edit-{$post_type}_sortable_columns", array($this, 'callback_add_sortable_posts_column')); |
| 61 | |
| 62 | /** Users */ |
| 63 | add_filter( "manage_users_sortable_columns", array($this, 'callback_add_sortable_users_column')); |
| 64 | |
| 65 | /** Media */ |
| 66 | add_filter( "manage_upload_sortable_columns", array($this, 'callback_add_sortable_media_column')); |
| 67 | |
| 68 | /** Links */ |
| 69 | add_filter( "manage_link-manager_sortable_columns", array($this, 'callback_add_sortable_links_column')); |
| 70 | |
| 71 | /** Comments */ |
| 72 | add_filter( "manage_edit-comments_sortable_columns", array($this, 'callback_add_sortable_comments_column')); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Callback add Posts sortable column |
| 77 | * |
| 78 | * @since 1.0 |
| 79 | */ |
| 80 | public function callback_add_sortable_posts_column($columns) |
| 81 | { |
| 82 | global $post_type; |
| 83 | |
| 84 | return $this->add_managed_sortable_columns($post_type, $columns); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Callback add Users sortable column |
| 89 | * |
| 90 | * @since 1.1 |
| 91 | */ |
| 92 | public function callback_add_sortable_users_column($columns) |
| 93 | { |
| 94 | return $this->add_managed_sortable_columns('wp-users', $columns); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Callback add Media sortable column |
| 99 | * |
| 100 | * @since 1.3 |
| 101 | */ |
| 102 | public function callback_add_sortable_media_column($columns) |
| 103 | { |
| 104 | return $this->add_managed_sortable_columns('wp-media', $columns); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Callback add Links sortable column |
| 109 | * |
| 110 | * @since 1.3.1 |
| 111 | */ |
| 112 | public function callback_add_sortable_links_column($columns) |
| 113 | { |
| 114 | return $this->add_managed_sortable_columns('wp-links', $columns); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Callback add Comments sortable column |
| 119 | * |
| 120 | * @since 1.3.1 |
| 121 | */ |
| 122 | public function callback_add_sortable_comments_column($columns) |
| 123 | { |
| 124 | return $this->add_managed_sortable_columns('wp-comments', $columns); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Add managed sortable columns by Type |
| 129 | * |
| 130 | * @since 1.1 |
| 131 | */ |
| 132 | private function add_managed_sortable_columns( $type = 'post', $columns ) |
| 133 | { |
| 134 | $display_columns = $this->get_merged_columns($type); |
| 135 | |
| 136 | if ( ! $display_columns ) |
| 137 | return $columns; |
| 138 | |
| 139 | foreach ( $display_columns as $id => $vars ) { |
| 140 | if ( isset($vars['options']['sortorder']) && $vars['options']['sortorder'] == 'on' ){ |
| 141 | |
| 142 | // register format |
| 143 | $columns[$id] = $this->sanitize_string($vars['label']); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | return $columns; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Admin requests for orderby column |
| 152 | * |
| 153 | * Only works for WP_Query objects ( such as posts and media ) |
| 154 | * |
| 155 | * @since 1.0 |
| 156 | */ |
| 157 | public function handle_requests_orderby_column( $vars ) |
| 158 | { |
| 159 | if ( ! isset( $vars['orderby'] ) ) |
| 160 | return $vars; |
| 161 | |
| 162 | /** Users */ |
| 163 | // You would expect to see get_orderby_users_vars(), but sorting for |
| 164 | // users is handled through a different filter. Not 'request', but 'pre_user_query'. |
| 165 | // See handle_requests_orderby_users_column(). |
| 166 | |
| 167 | /** Media */ |
| 168 | elseif ( $this->request_uri_is('upload') ) |
| 169 | $vars = $this->get_orderby_media_vars($vars); |
| 170 | |
| 171 | /** Posts */ |
| 172 | elseif ( !empty($vars['post_type']) ) |
| 173 | $vars = $this->get_orderby_posts_vars($vars); |
| 174 | |
| 175 | return $vars; |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Orderby Users column |
| 180 | * |
| 181 | * @since 1.3 |
| 182 | */ |
| 183 | public function handle_requests_orderby_users_column($user_query) |
| 184 | { |
| 185 | // query vars |
| 186 | $vars = $user_query->query_vars; |
| 187 | |
| 188 | // Column |
| 189 | $column = $this->get_orderby_type( $vars['orderby'], 'wp-users' ); |
| 190 | |
| 191 | if ( empty($column) ) |
| 192 | return $vars; |
| 193 | |
| 194 | // id |
| 195 | $id = key($column); |
| 196 | |
| 197 | // type |
| 198 | $type = $id; |
| 199 | |
| 200 | // Check for user custom fields: column-meta-[customfieldname] |
| 201 | if ( $this->is_column_meta($type) ) |
| 202 | $type = 'column-user-meta'; |
| 203 | |
| 204 | // Check for post count: column-user_postcount-[posttype] |
| 205 | if ( $this->get_posttype_by_postcount_column($type) ) |
| 206 | $type = 'column-user_postcount'; |
| 207 | |
| 208 | // var |
| 209 | $cusers = array(); |
| 210 | switch( $type ) : |
| 211 | |
| 212 | case 'column-user_id': |
| 213 | $user_query->query_vars['orderby'] = 'ID'; |
| 214 | break; |
| 215 | |
| 216 | case 'column-user_registered': |
| 217 | $user_query->query_vars['orderby'] = 'registered'; |
| 218 | break; |
| 219 | |
| 220 | case 'column-nickname': |
| 221 | $user_query->query_vars['orderby'] = 'nickname'; |
| 222 | break; |
| 223 | |
| 224 | case 'column-first_name': |
| 225 | foreach ( $this->get_users_data() as $u ) |
| 226 | if ($u->first_name || $this->show_all_results ) |
| 227 | $cusers[$u->ID] = $this->prepare_sort_string_value($u->first_name); |
| 228 | $this->set_users_query_vars( &$user_query, $cusers, SORT_REGULAR ); |
| 229 | break; |
| 230 | |
| 231 | case 'column-last_name': |
| 232 | foreach ( $this->get_users_data() as $u ) |
| 233 | if ($u->last_name || $this->show_all_results ) |
| 234 | $cusers[$u->ID] = $this->prepare_sort_string_value($u->last_name); |
| 235 | $this->set_users_query_vars( &$user_query, $cusers, SORT_REGULAR ); |
| 236 | break; |
| 237 | |
| 238 | case 'column-user_url': |
| 239 | foreach ( $this->get_users_data() as $u ) |
| 240 | if ($u->user_url || $this->show_all_results ) |
| 241 | $cusers[$u->ID] = $this->prepare_sort_string_value($u->user_url); |
| 242 | $this->set_users_query_vars( &$user_query, $cusers, SORT_REGULAR ); |
| 243 | break; |
| 244 | |
| 245 | case 'column-user_description': |
| 246 | foreach ( $this->get_users_data() as $u ) |
| 247 | if ($u->user_description || $this->show_all_results ) |
| 248 | $cusers[$u->ID] = $this->prepare_sort_string_value($u->user_description); |
| 249 | $this->set_users_query_vars( &$user_query, $cusers, SORT_REGULAR ); |
| 250 | break; |
| 251 | |
| 252 | case 'column-user_postcount' : |
| 253 | $post_type = $this->get_posttype_by_postcount_column($id); |
| 254 | if ( $post_type ) { |
| 255 | foreach ( $this->get_users_data() as $u ) { |
| 256 | $count = $this->get_post_count( $post_type, $u->ID ); |
| 257 | $cusers[$u->ID] = $this->prepare_sort_string_value($count); |
| 258 | } |
| 259 | $this->set_users_query_vars( &$user_query, $cusers, SORT_REGULAR ); |
| 260 | } |
| 261 | break; |
| 262 | |
| 263 | case 'role' : |
| 264 | foreach ( $this->get_users_data() as $u ) { |
| 265 | $role = !empty($u->roles[0]) ? $u->roles[0] : ''; |
| 266 | if ($role || $this->show_all_results ) { |
| 267 | $cusers[$u->ID] = $this->prepare_sort_string_value($role); |
| 268 | } |
| 269 | } |
| 270 | $this->set_users_query_vars( &$user_query, $cusers, SORT_REGULAR ); |
| 271 | break; |
| 272 | |
| 273 | case 'column-user-meta' : |
| 274 | $field = $column[$id]['field']; |
| 275 | if ( $field ) { |
| 276 | |
| 277 | // order numeric or string |
| 278 | $order = SORT_REGULAR; |
| 279 | if ( $column[$id]['field_type'] == 'numeric' || $column[$id]['field_type'] == 'library_id' ) |
| 280 | $order = SORT_NUMERIC; |
| 281 | |
| 282 | // sort by metavalue |
| 283 | foreach ( $this->get_users_data() as $u ) { |
| 284 | $value = get_metadata('user', $u->ID, $field, true); |
| 285 | $cusers[$u->ID] = $this->prepare_sort_string_value($value); |
| 286 | } |
| 287 | $this->set_users_query_vars( &$user_query, $cusers, $order ); |
| 288 | } |
| 289 | break; |
| 290 | |
| 291 | endswitch; |
| 292 | |
| 293 | return $user_query; |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * Orderby Links column |
| 298 | * |
| 299 | * Makes use of filter 'get_bookmarks' from bookmark.php to change the result set of the links |
| 300 | * |
| 301 | * @since 1.3.1 |
| 302 | */ |
| 303 | public function handle_requests_orderby_links_column() |
| 304 | { |
| 305 | // fire only when we are in the admins link-manager |
| 306 | if ( $this->request_uri_is('link-manager') ) |
| 307 | add_filter( 'get_bookmarks', array( $this, 'callback_requests_orderby_links_column'), 10, 2); |
| 308 | } |
| 309 | |
| 310 | /** |
| 311 | * Orderby Links column |
| 312 | * |
| 313 | * @since 1.3.1 |
| 314 | */ |
| 315 | public function callback_requests_orderby_links_column($results, $vars) |
| 316 | { |
| 317 | global $wpdb; |
| 318 | |
| 319 | // Column |
| 320 | $column = $this->get_orderby_type( $vars['orderby'], 'wp-links' ); |
| 321 | |
| 322 | if ( empty($column) ) |
| 323 | return $results; |
| 324 | |
| 325 | // id |
| 326 | $type = $id = key($column); |
| 327 | |
| 328 | // var |
| 329 | $length = ''; |
| 330 | switch( $type ) : |
| 331 | |
| 332 | case 'column-link_id': |
| 333 | if ( version_compare( get_bloginfo('version'), '3.2', '>' ) ) |
| 334 | $vars['orderby'] = 'link_id'; |
| 335 | else |
| 336 | $vars['orderby'] = 'id'; |
| 337 | break; |
| 338 | |
| 339 | case 'column-owner': |
| 340 | $vars['orderby'] = 'link_owner'; |
| 341 | break; |
| 342 | |
| 343 | case 'column-length': |
| 344 | $vars['orderby'] = 'length'; |
| 345 | $length = ", CHAR_LENGTH(link_name) AS length"; |
| 346 | break; |
| 347 | |
| 348 | case 'column-target': |
| 349 | $vars['orderby'] = 'link_target'; |
| 350 | break; |
| 351 | |
| 352 | case 'column-description': |
| 353 | $vars['orderby'] = 'link_description'; |
| 354 | break; |
| 355 | |
| 356 | case 'column-notes': |
| 357 | $vars['orderby'] = 'link_notes'; |
| 358 | break; |
| 359 | |
| 360 | case 'column-rss': |
| 361 | $vars['orderby'] = 'link_rss'; |
| 362 | break; |
| 363 | |
| 364 | /** native WP columns */ |
| 365 | |
| 366 | // Relationship |
| 367 | case 'rel': |
| 368 | $vars['orderby'] = 'link_rel'; |
| 369 | break; |
| 370 | |
| 371 | default: |
| 372 | $vars['orderby'] = ''; |
| 373 | |
| 374 | endswitch; |
| 375 | |
| 376 | // get bookmarks by orderby vars |
| 377 | if ( $vars['orderby'] ) { |
| 378 | $vars['order'] = mysql_escape_string($vars['order']); |
| 379 | $sql = "SELECT * {$length} FROM {$wpdb->links} WHERE 1=1 ORDER BY {$vars['orderby']} {$vars['order']}"; |
| 380 | $results = $wpdb->get_results($sql); |
| 381 | |
| 382 | // check for errors |
| 383 | if( is_wp_error($results) ) |
| 384 | return false; |
| 385 | } |
| 386 | |
| 387 | return $results; |
| 388 | } |
| 389 | |
| 390 | /** |
| 391 | * Orderby Comments column |
| 392 | * |
| 393 | * @since 1.3.1 |
| 394 | */ |
| 395 | public function callback_requests_orderby_comments_column($pieces, $ref_comment) |
| 396 | { |
| 397 | // get query vars |
| 398 | $vars = $ref_comment->query_vars; |
| 399 | |
| 400 | // Column |
| 401 | $column = $this->get_orderby_type( $vars['orderby'], 'wp-comments' ); |
| 402 | |
| 403 | if ( empty($column) ) |
| 404 | return $pieces; |
| 405 | |
| 406 | // id |
| 407 | $type = $id = key($column); |
| 408 | |
| 409 | // var |
| 410 | switch( $type ) : |
| 411 | |
| 412 | case 'column-comment_id': |
| 413 | $pieces['orderby'] = 'comment_ID'; |
| 414 | break; |
| 415 | |
| 416 | case 'column-author_author': |
| 417 | $pieces['orderby'] = 'comment_author'; |
| 418 | break; |
| 419 | |
| 420 | case 'column-author_ip': |
| 421 | $pieces['orderby'] = 'comment_author_IP'; |
| 422 | break; |
| 423 | |
| 424 | case 'column-author_url': |
| 425 | $pieces['orderby'] = 'comment_author_url'; |
| 426 | break; |
| 427 | |
| 428 | case 'column-author_email': |
| 429 | $pieces['orderby'] = 'comment_author_email'; |
| 430 | break; |
| 431 | |
| 432 | case 'column-reply_to': |
| 433 | break; |
| 434 | |
| 435 | case 'column-approved': |
| 436 | $pieces['orderby'] = 'comment_approved'; |
| 437 | break; |
| 438 | |
| 439 | case 'column-date': |
| 440 | $pieces['orderby'] = 'comment_date'; |
| 441 | break; |
| 442 | |
| 443 | case 'column-agent': |
| 444 | $pieces['orderby'] = 'comment_agent'; |
| 445 | break; |
| 446 | |
| 447 | case 'column-excerpt': |
| 448 | $pieces['orderby'] = 'comment_content'; |
| 449 | break; |
| 450 | |
| 451 | case 'column-date_gmt': |
| 452 | // is default |
| 453 | break; |
| 454 | |
| 455 | /** native WP columns */ |
| 456 | |
| 457 | // Relationship |
| 458 | case 'comment': |
| 459 | $pieces['orderby'] = 'comment_content'; |
| 460 | break; |
| 461 | |
| 462 | default: |
| 463 | $vars['orderby'] = ''; |
| 464 | |
| 465 | endswitch; |
| 466 | |
| 467 | return $pieces; |
| 468 | } |
| 469 | |
| 470 | /** |
| 471 | * Orderby Comments column |
| 472 | * |
| 473 | * @since 1.3.1 |
| 474 | */ |
| 475 | public function handle_requests_orderby_comments_column() |
| 476 | { |
| 477 | // fire only when we are in the admins edit-comments |
| 478 | if ( $this->request_uri_is('edit-comments') ) |
| 479 | add_filter('comments_clauses', array( $this, 'callback_requests_orderby_comments_column'), 10, 2); |
| 480 | } |
| 481 | |
| 482 | /** |
| 483 | * Set sorting vars in User Query Object |
| 484 | * |
| 485 | * @since 1.3 |
| 486 | */ |
| 487 | private function set_users_query_vars(&$user_query, $sortusers, $sort_flags = SORT_REGULAR ) |
| 488 | { |
| 489 | global $wpdb; |
| 490 | |
| 491 | // vars |
| 492 | $vars = $user_query->query_vars; |
| 493 | |
| 494 | // sorting |
| 495 | if ( $vars['order'] == 'ASC' ) |
| 496 | asort($sortusers, $sort_flags); |
| 497 | else |
| 498 | arsort($sortusers, $sort_flags); |
| 499 | |
| 500 | // alter orderby SQL |
| 501 | if ( ! empty ( $sortusers ) ) { |
| 502 | $ids = implode(',', array_keys($sortusers)); |
| 503 | $user_query->query_where .= " AND {$wpdb->prefix}users.ID IN ({$ids})"; |
| 504 | $user_query->query_orderby = "ORDER BY FIELD ({$wpdb->prefix}users.ID,{$ids})"; |
| 505 | } |
| 506 | |
| 507 | // cleanup the vars we dont need |
| 508 | $vars['order'] = ''; |
| 509 | $vars['orderby'] = ''; |
| 510 | |
| 511 | $user_query->query_vars = $vars; |
| 512 | } |
| 513 | |
| 514 | /** |
| 515 | * Orderby Media column |
| 516 | * |
| 517 | * @since 1.3 |
| 518 | */ |
| 519 | private function get_orderby_media_vars($vars) |
| 520 | { |
| 521 | // Column |
| 522 | $column = $this->get_orderby_type( $vars['orderby'], 'wp-media' ); |
| 523 | |
| 524 | if ( empty($column) ) |
| 525 | return $vars; |
| 526 | |
| 527 | // var |
| 528 | $cposts = array(); |
| 529 | switch( key($column) ) : |
| 530 | |
| 531 | case 'column-mediaid' : |
| 532 | $vars['orderby'] = 'ID'; |
| 533 | break; |
| 534 | |
| 535 | case 'column-width' : |
| 536 | foreach ( (array) $this->get_any_posts_by_posttype('attachment') as $p ) { |
| 537 | $meta = wp_get_attachment_metadata($p->ID); |
| 538 | $width = !empty($meta['width']) ? $meta['width'] : 0; |
| 539 | if ( $width || $this->show_all_results ) |
| 540 | $cposts[$p->ID] = $width; |
| 541 | } |
| 542 | $this->set_vars_post__in( &$vars, $cposts, SORT_NUMERIC ); |
| 543 | break; |
| 544 | |
| 545 | case 'column-height' : |
| 546 | foreach ( (array) $this->get_any_posts_by_posttype('attachment') as $p ) { |
| 547 | $meta = wp_get_attachment_metadata($p->ID); |
| 548 | $height = !empty($meta['height']) ? $meta['height'] : 0; |
| 549 | if ( $height || $this->show_all_results ) |
| 550 | $cposts[$p->ID] = $height; |
| 551 | } |
| 552 | $this->set_vars_post__in( &$vars, $cposts, SORT_NUMERIC ); |
| 553 | break; |
| 554 | |
| 555 | case 'column-dimensions' : |
| 556 | foreach ( (array) $this->get_any_posts_by_posttype('attachment') as $p ) { |
| 557 | $meta = wp_get_attachment_metadata($p->ID); |
| 558 | $height = !empty($meta['height']) ? $meta['height'] : 0; |
| 559 | $width = !empty($meta['width']) ? $meta['width'] : 0; |
| 560 | $surface = $height*$width; |
| 561 | |
| 562 | if ( $surface || $this->show_all_results ) |
| 563 | $cposts[$p->ID] = $surface; |
| 564 | } |
| 565 | $this->set_vars_post__in( &$vars, $cposts, SORT_NUMERIC ); |
| 566 | break; |
| 567 | |
| 568 | case 'column-caption' : |
| 569 | foreach ( (array) $this->get_any_posts_by_posttype('attachment') as $p ) |
| 570 | if ( $p->post_excerpt || $this->show_all_results ) |
| 571 | $cposts[$p->ID] = $this->prepare_sort_string_value($p->post_excerpt); |
| 572 | $this->set_vars_post__in( &$vars, $cposts, SORT_STRING); |
| 573 | break; |
| 574 | |
| 575 | case 'column-description' : |
| 576 | foreach ( (array) $this->get_any_posts_by_posttype('attachment') as $p ) |
| 577 | if ( $p->post_content || $this->show_all_results ) |
| 578 | $cposts[$p->ID] = $this->prepare_sort_string_value( $p->post_content ); |
| 579 | $this->set_vars_post__in( &$vars, $cposts, SORT_STRING); |
| 580 | break; |
| 581 | |
| 582 | case 'column-mime_type' : |
| 583 | foreach ( (array) $this->get_any_posts_by_posttype('attachment') as $p ) |
| 584 | if ( $p->post_mime_type || $this->show_all_results ) |
| 585 | $cposts[$p->ID] = $this->prepare_sort_string_value( $p->post_mime_type ); |
| 586 | $this->set_vars_post__in( &$vars, $cposts, SORT_STRING); |
| 587 | break; |
| 588 | |
| 589 | case 'column-file_name' : |
| 590 | foreach ( (array) $this->get_any_posts_by_posttype('attachment') as $p ) { |
| 591 | $meta = get_post_meta($p->ID, '_wp_attached_file', true); |
| 592 | $file = !empty($meta) ? basename($meta) : ''; |
| 593 | if ( $file || $this->show_all_results ) |
| 594 | $cposts[$p->ID] = $file; |
| 595 | } |
| 596 | $this->set_vars_post__in( &$vars, $cposts, SORT_STRING); |
| 597 | break; |
| 598 | |
| 599 | case 'column-alternate_text' : |
| 600 | foreach ( (array) $this->get_any_posts_by_posttype('attachment') as $p ) { |
| 601 | $alt = get_post_meta($p->ID, '_wp_attachment_image_alt', true); |
| 602 | if ( $alt || $this->show_all_results ) { |
| 603 | $cposts[$p->ID] = $this->prepare_sort_string_value( $alt ); |
| 604 | } |
| 605 | } |
| 606 | $this->set_vars_post__in( &$vars, $cposts, SORT_STRING); |
| 607 | break; |
| 608 | |
| 609 | endswitch; |
| 610 | |
| 611 | return $vars; |
| 612 | } |
| 613 | |
| 614 | /** |
| 615 | * Orderby Posts column |
| 616 | * |
| 617 | * @since 1.3 |
| 618 | */ |
| 619 | private function get_orderby_posts_vars($vars) |
| 620 | { |
| 621 | $post_type = $vars['post_type']; |
| 622 | |
| 623 | // Column |
| 624 | $column = $this->get_orderby_type( $vars['orderby'], $post_type ); |
| 625 | |
| 626 | if ( empty($column) ) |
| 627 | return $vars; |
| 628 | |
| 629 | // id |
| 630 | $id = key($column); |
| 631 | |
| 632 | // type |
| 633 | $type = $id; |
| 634 | |
| 635 | // custom fields |
| 636 | if ( $this->is_column_meta($type) ) |
| 637 | $type = 'column-post-meta'; |
| 638 | |
| 639 | // attachments |
| 640 | if ( $type == 'column-attachment-count' ) |
| 641 | $type = 'column-attachment'; |
| 642 | |
| 643 | // var |
| 644 | $cposts = array(); |
| 645 | switch( $type ) : |
| 646 | |
| 647 | case 'column-postid' : |
| 648 | $vars['orderby'] = 'ID'; |
| 649 | break; |
| 650 | |
| 651 | case 'column-order' : |
| 652 | $vars['orderby'] = 'menu_order'; |
| 653 | break; |
| 654 | |
| 655 | case 'column-post-meta' : |
| 656 | $field = $column[$id]['field']; |
| 657 | |
| 658 | // orderby type |
| 659 | $field_type = 'meta_value'; |
| 660 | if ( $column[$id]['field_type'] == 'numeric' || $column[$id]['field_type'] == 'library_id' ) |
| 661 | $field_type = 'meta_value_num'; |
| 662 | |
| 663 | $vars = array_merge($vars, array( |
| 664 | 'meta_key' => $field, |
| 665 | 'orderby' => $field_type |
| 666 | )); |
| 667 | break; |
| 668 | |
| 669 | case 'column-excerpt' : |
| 670 | foreach ( (array) $this->get_any_posts_by_posttype($post_type) as $p ) { |
| 671 | |
| 672 | // add excerpt to the post ids |
| 673 | $cposts[$p->ID] = $this->prepare_sort_string_value($p->post_content); |
| 674 | } |
| 675 | // we will add the sorted post ids to vars['post__in'] and remove unused vars |
| 676 | $this->set_vars_post__in( &$vars, $cposts, SORT_STRING ); |
| 677 | break; |
| 678 | |
| 679 | case 'column-word-count' : |
| 680 | foreach ( (array) $this->get_any_posts_by_posttype($post_type) as $p ) |
| 681 | $cposts[$p->ID] = str_word_count( $this->strip_trim( $p->post_content ) ); |
| 682 | $this->set_vars_post__in( &$vars, $cposts, SORT_NUMERIC ); |
| 683 | break; |
| 684 | |
| 685 | case 'column-page-template' : |
| 686 | $templates = get_page_templates(); |
| 687 | foreach ( (array) $this->get_any_posts_by_posttype($post_type) as $p ) { |
| 688 | $page_template = get_post_meta($p->ID, '_wp_page_template', true); |
| 689 | $cposts[$p->ID] = array_search($page_template, $templates); |
| 690 | } |
| 691 | $this->set_vars_post__in( &$vars, $cposts ); |
| 692 | break; |
| 693 | |
| 694 | case 'column-post_formats' : |
| 695 | foreach ( (array) $this->get_any_posts_by_posttype($post_type) as $p ) { |
| 696 | $cposts[$p->ID] = get_post_format($p->ID); |
| 697 | } |
| 698 | $this->set_vars_post__in( &$vars, $cposts ); |
| 699 | break; |
| 700 | |
| 701 | case 'column-attachment' : |
| 702 | foreach ( (array) $this->get_any_posts_by_posttype($post_type) as $p ) |
| 703 | $cposts[$p->ID] = count( $this->get_attachment_ids($p->ID) ); |
| 704 | $this->set_vars_post__in( &$vars, $cposts, SORT_NUMERIC ); |
| 705 | break; |
| 706 | |
| 707 | |
| 708 | case 'column-page-slug' : |
| 709 | foreach ( (array) $this->get_any_posts_by_posttype($post_type) as $p ) |
| 710 | $cposts[$p->ID] = $p->post_name; |
| 711 | $this->set_vars_post__in( &$vars, $cposts ); |
| 712 | break; |
| 713 | |
| 714 | case 'column-sticky' : |
| 715 | $stickies = get_option('sticky_posts'); |
| 716 | foreach ( (array) $this->get_any_posts_by_posttype($post_type) as $p ) { |
| 717 | $cposts[$p->ID] = $p->ID; |
| 718 | if ( !empty($stickies) && in_array($p->ID, $stickies ) ) { |
| 719 | $cposts[$p->ID] = 0; |
| 720 | } |
| 721 | } |
| 722 | $this->set_vars_post__in( &$vars, $cposts ); |
| 723 | break; |
| 724 | |
| 725 | case 'column-featured_image' : |
| 726 | foreach ( (array) $this->get_any_posts_by_posttype($post_type) as $p ) { |
| 727 | $cposts[$p->ID] = $p->ID; |
| 728 | $thumb = get_the_post_thumbnail($p->ID); |
| 729 | if ( !empty($thumb) ) { |
| 730 | $cposts[$p->ID] = 0; |
| 731 | } |
| 732 | } |
| 733 | $this->set_vars_post__in( &$vars, $cposts ); |
| 734 | break; |
| 735 | |
| 736 | case 'column-roles' : |
| 737 | foreach ( (array) $this->get_any_posts_by_posttype($post_type) as $p ) { |
| 738 | $cposts[$p->ID] = 0; |
| 739 | $userdata = get_userdata($p->post_author); |
| 740 | if ( !empty($userdata->roles[0]) ) { |
| 741 | $cposts[$p->ID] = $userdata->roles[0]; |
| 742 | } |
| 743 | } |
| 744 | $this->set_vars_post__in( &$vars, $cposts, SORT_STRING ); |
| 745 | break; |
| 746 | |
| 747 | case 'column-status' : |
| 748 | foreach ( (array) $this->get_any_posts_by_posttype($post_type) as $p ) { |
| 749 | $cposts[$p->ID] = $p->post_status.strtotime($p->post_date); |
| 750 | } |
| 751 | $this->set_vars_post__in( &$vars, $cposts, SORT_STRING ); |
| 752 | break; |
| 753 | |
| 754 | endswitch; |
| 755 | |
| 756 | return $vars; |
| 757 | } |
| 758 | |
| 759 | /** |
| 760 | * Set post__in for use in WP_Query |
| 761 | * |
| 762 | * This will order the ID's asc or desc and set the appropriate filters. |
| 763 | * |
| 764 | * @since 1.2.1 |
| 765 | */ |
| 766 | private function set_vars_post__in( &$vars, $sortposts, $sort_flags = SORT_REGULAR ) |
| 767 | { |
| 768 | // sort post ids by value |
| 769 | if ( $vars['order'] == 'asc' ) |
| 770 | asort($sortposts, $sort_flags); |
| 771 | else |
| 772 | arsort($sortposts, $sort_flags); |
| 773 | |
| 774 | // this will make sure WP_Query will use the order of the ids that we have just set in 'post__in' |
| 775 | add_filter('posts_orderby', array( $this, 'filter_orderby_post__in'), 10, 2 ); |
| 776 | |
| 777 | // cleanup the vars we dont need |
| 778 | $vars['order'] = ''; |
| 779 | $vars['orderby'] = ''; |
| 780 | |
| 781 | // add the sorted post ids to the query with the use of post__in |
| 782 | $vars['post__in'] = array_keys($sortposts); |
| 783 | } |
| 784 | |
| 785 | /** |
| 786 | * Get orderby type |
| 787 | * |
| 788 | * @since 1.1 |
| 789 | */ |
| 790 | private function get_orderby_type($orderby, $type) |
| 791 | { |
| 792 | $db_columns = $this->get_stored_columns($type); |
| 793 | |
| 794 | if ( $db_columns ) { |
| 795 | foreach ( $db_columns as $id => $vars ) { |
| 796 | |
| 797 | // check which custom column was clicked |
| 798 | if ( isset( $vars['label'] ) && $orderby == $this->sanitize_string( $vars['label'] ) ) { |
| 799 | $column[$id] = $vars; |
| 800 | return $column; |
| 801 | } |
| 802 | } |
| 803 | } |
| 804 | return false; |
| 805 | } |
| 806 | |
| 807 | /** |
| 808 | * Maintain order of ids that are set in the post__in var. |
| 809 | * |
| 810 | * This will force the returned posts to use the order of the ID's that |
| 811 | * have been set in post__in. Without this the ID's will be set in numeric order. |
| 812 | * See the WP_Query object for more info about the use of post__in. |
| 813 | * |
| 814 | * @since 1.2.1 |
| 815 | */ |
| 816 | public function filter_orderby_post__in($orderby, $wp) |
| 817 | { |
| 818 | global $wpdb; |
| 819 | |
| 820 | // we need the query vars |
| 821 | $vars = $wp->query_vars; |
| 822 | if ( ! empty ( $vars['post__in'] ) ) { |
| 823 | // now we can get the ids |
| 824 | $ids = implode(',', $vars['post__in']); |
| 825 | |
| 826 | // by adding FIELD to the SQL query we are forcing the order of the ID's |
| 827 | return "FIELD ({$wpdb->prefix}posts.ID,{$ids})"; |
| 828 | } |
| 829 | } |
| 830 | |
| 831 | /** |
| 832 | * Get any posts by post_type |
| 833 | * |
| 834 | * @since 1.2.1 |
| 835 | */ |
| 836 | private function get_any_posts_by_posttype( $post_type ) |
| 837 | { |
| 838 | $allposts = get_posts(array( |
| 839 | 'numberposts' => -1, |
| 840 | 'post_status' => 'any', |
| 841 | 'post_type' => $post_type |
| 842 | )); |
| 843 | return $allposts; |
| 844 | } |
| 845 | |
| 846 | /** |
| 847 | * Request URI is |
| 848 | * |
| 849 | * @since 1.3.1 |
| 850 | */ |
| 851 | private function request_uri_is( $screen_id = '' ) |
| 852 | { |
| 853 | if (strpos( $_SERVER['REQUEST_URI'], "/{$screen_id}.php" ) !== false ) |
| 854 | return true; |
| 855 | |
| 856 | return false; |
| 857 | } |
| 858 | |
| 859 | /** |
| 860 | * Prepare the value for being by sorting |
| 861 | * |
| 862 | * @since 1.3 |
| 863 | */ |
| 864 | private function prepare_sort_string_value($string) |
| 865 | { |
| 866 | // remove tags and only get the first 20 chars and force lowercase. |
| 867 | $string = strtolower( substr( $this->strip_trim($string),0 ,20 ) ); |
| 868 | |
| 869 | return $string; |
| 870 | } |
| 871 | |
| 872 | /** |
| 873 | * Get users data |
| 874 | * |
| 875 | * @since 1.3 |
| 876 | */ |
| 877 | function get_users_data() |
| 878 | { |
| 879 | $userdatas = array(); |
| 880 | $wp_users = get_users( array( |
| 881 | 'blog_id' => $GLOBALS['blog_id'], |
| 882 | )); |
| 883 | foreach ( $wp_users as $u ) { |
| 884 | $userdatas[$u->ID] = get_userdata($u->ID); |
| 885 | } |
| 886 | return $userdatas; |
| 887 | } |
| 888 | } |
| 889 | |
| 890 | ?> |