Admin Columns - Version 1.4.9

Version Description

Download this release

Release Info

Developer codepress
Plugin Icon 128x128 Admin Columns
Version 1.4.9
Comparing to
See all releases

Code changes from version 1.4.8 to 1.4.9

classes/license.php CHANGED
@@ -4,16 +4,16 @@ class cpac_licence
4
{
5
/**
6
* The type of licence to check or activate
7
- *
8
* @var string $type
9
*/
10
public $type;
11
-
12
- public function __construct($type)
13
{
14
$this->type = $type;
15
}
16
-
17
/**
18
* Unlocks
19
*
@@ -22,25 +22,25 @@ class cpac_licence
22
public function is_unlocked()
23
{
24
return preg_match( '/^[a-f0-9]{40}#x2F;i', $this->get_license_key( $this->type ) );
25
- }
26
-
27
/**
28
* Check license key with API
29
*
30
* @since 1.3.3
31
*/
32
public function check_remote_key( $key )
33
- {
34
if ( empty( $key ) ) {
35
return false;
36
}
37
-
38
- // check key with remote API
39
- $response = wp_remote_post( 'http://www.codepress.nl/', array(
40
'body' => array(
41
'api' => 'addon',
42
'key' => $key,
43
- 'type' => $this->type
44
)
45
));
46
@@ -48,20 +48,20 @@ class cpac_licence
48
if ( is_wp_error($response) || ( isset($response['body']) && json_decode($response['body']) == 'valid' ) ) {
49
return true;
50
}
51
-
52
return false;
53
}
54
-
55
/**
56
* Set masked license key
57
*
58
* @since 1.3.1
59
*/
60
- public function get_masked_license_key()
61
{
62
- return '**************************'.substr( $this->get_license_key(), -4 );
63
- }
64
-
65
/**
66
* Get license key
67
*
@@ -71,17 +71,17 @@ class cpac_licence
71
{
72
return get_option("cpac_{$this->type}_ac");
73
}
74
-
75
/**
76
* Set license key
77
*
78
* @since 1.3
79
*/
80
public function set_license_key( $key )
81
- {
82
update_option( "cpac_{$this->type}_ac", trim( $key ) );
83
}
84
-
85
/**
86
* Remove license key
87
*
@@ -91,5 +91,5 @@ class cpac_licence
91
{
92
delete_option( "cpac_{$this->type}_ac" );
93
delete_transient("cpac_{$this->type}_trnsnt");
94
- }
95
}
4
{
5
/**
6
* The type of licence to check or activate
7
+ *
8
* @var string $type
9
*/
10
public $type;
11
+
12
+ public function __construct($type)
13
{
14
$this->type = $type;
15
}
16
+
17
/**
18
* Unlocks
19
*
22
public function is_unlocked()
23
{
24
return preg_match( '/^[a-f0-9]{40}#x2F;i', $this->get_license_key( $this->type ) );
25
+ }
26
+
27
/**
28
* Check license key with API
29
*
30
* @since 1.3.3
31
*/
32
public function check_remote_key( $key )
33
+ {
34
if ( empty( $key ) ) {
35
return false;
36
}
37
+
38
+ // check key with remote API
39
+ $response = wp_remote_post( 'http://www.codepress.nl/', array(
40
'body' => array(
41
'api' => 'addon',
42
'key' => $key,
43
+ 'type' => $this->type
44
)
45
));
46
48
if ( is_wp_error($response) || ( isset($response['body']) && json_decode($response['body']) == 'valid' ) ) {
49
return true;
50
}
51
+
52
return false;
53
}
54
+
55
/**
56
* Set masked license key
57
*
58
* @since 1.3.1
59
*/
60
+ public function get_masked_license_key()
61
{
62
+ return '**************************'.substr( $this->get_license_key(), -4 );
63
+ }
64
+
65
/**
66
* Get license key
67
*
71
{
72
return get_option("cpac_{$this->type}_ac");
73
}
74
+
75
/**
76
* Set license key
77
*
78
* @since 1.3
79
*/
80
public function set_license_key( $key )
81
+ {
82
update_option( "cpac_{$this->type}_ac", trim( $key ) );
83
}
84
+
85
/**
86
* Remove license key
87
*
91
{
92
delete_option( "cpac_{$this->type}_ac" );
93
delete_transient("cpac_{$this->type}_trnsnt");
94
+ }
95
}
classes/sortable.php CHANGED
@@ -7,12 +7,12 @@
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
*
@@ -22,7 +22,7 @@ class Codepress_Sortable_Columns extends Codepress_Admin_Columns
22
{
23
add_action( 'wp_loaded', array( $this, 'init') );
24
}
25
-
26
/**
27
* Initialize
28
*
@@ -31,26 +31,26 @@ class Codepress_Sortable_Columns extends Codepress_Admin_Columns
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
*
@@ -62,38 +62,38 @@ class Codepress_Sortable_Columns extends Codepress_Admin_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
@@ -102,64 +102,64 @@ class Codepress_Sortable_Columns extends Codepress_Admin_Columns
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
*
@@ -167,143 +167,143 @@ class Codepress_Sortable_Columns extends Codepress_Admin_Columns
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;
@@ -313,21 +313,21 @@ class Codepress_Sortable_Columns extends Codepress_Admin_Columns
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
*/
@@ -336,83 +336,83 @@ class Codepress_Sortable_Columns extends Codepress_Admin_Columns
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;
@@ -420,108 +420,108 @@ class Codepress_Sortable_Columns extends Codepress_Admin_Columns
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
@@ -531,53 +531,53 @@ class Codepress_Sortable_Columns extends Codepress_Admin_Columns
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 ) {
@@ -587,7 +587,7 @@ class Codepress_Sortable_Columns extends Codepress_Admin_Columns
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 ) {
@@ -597,7 +597,7 @@ class Codepress_Sortable_Columns extends Codepress_Admin_Columns
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 ) {
@@ -605,12 +605,12 @@ class Codepress_Sortable_Columns extends Codepress_Admin_Columns
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 ) {
@@ -619,7 +619,7 @@ class Codepress_Sortable_Columns extends Codepress_Admin_Columns
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 ) {
@@ -628,25 +628,25 @@ class Codepress_Sortable_Columns extends Codepress_Admin_Columns
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' :
@@ -658,20 +658,20 @@ class Codepress_Sortable_Columns extends Codepress_Admin_Columns
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 );
@@ -679,7 +679,7 @@ class Codepress_Sortable_Columns extends Codepress_Admin_Columns
679
680
return $vars;
681
}
682
-
683
/**
684
* Orderby Posts column
685
*
@@ -688,55 +688,55 @@ class Codepress_Sortable_Columns extends Codepress_Admin_Columns
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' )
@@ -747,52 +747,52 @@ class Codepress_Sortable_Columns extends Codepress_Admin_Columns
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');
@@ -803,7 +803,7 @@ class Codepress_Sortable_Columns extends Codepress_Admin_Columns
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 ) {
@@ -814,7 +814,7 @@ class Codepress_Sortable_Columns extends Codepress_Admin_Columns
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 ) {
@@ -825,34 +825,34 @@ class Codepress_Sortable_Columns extends Codepress_Admin_Columns
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'];
@@ -861,37 +861,37 @@ class Codepress_Sortable_Columns extends Codepress_Admin_Columns
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 );
@@ -899,12 +899,12 @@ class Codepress_Sortable_Columns extends Codepress_Admin_Columns
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
@@ -913,17 +913,17 @@ class Codepress_Sortable_Columns extends Codepress_Admin_Columns
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
*
@@ -935,13 +935,13 @@ class Codepress_Sortable_Columns extends Codepress_Admin_Columns
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
*
@@ -951,12 +951,12 @@ class Codepress_Sortable_Columns extends Codepress_Admin_Columns
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'];
@@ -968,7 +968,7 @@ class Codepress_Sortable_Columns extends Codepress_Admin_Columns
968
$this->set_sorting_preference( $type, $vars['orderby'], $vars['order'] );
969
}
970
}
971
-
972
/**
973
* Get posts sorted by taxonomy
974
*
@@ -976,23 +976,23 @@ class Codepress_Sortable_Columns extends Codepress_Admin_Columns
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
*
@@ -1015,59 +1015,59 @@ class Codepress_Sortable_Columns extends Codepress_Admin_Columns
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
*
@@ -1080,19 +1080,19 @@ class Codepress_Sortable_Columns extends Codepress_Admin_Columns
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
*
@@ -1100,12 +1100,12 @@ class Codepress_Sortable_Columns extends Codepress_Admin_Columns
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
*
@@ -1115,27 +1115,27 @@ class Codepress_Sortable_Columns extends Codepress_Admin_Columns
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
*
@@ -1145,11 +1145,11 @@ class Codepress_Sortable_Columns extends Codepress_Admin_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
*
@@ -1158,87 +1158,87 @@ class Codepress_Sortable_Columns extends Codepress_Admin_Columns
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
}
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
*
22
{
23
add_action( 'wp_loaded', array( $this, 'init') );
24
}
25
+
26
/**
27
* Initialize
28
*
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
*
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
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
*
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;
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
*/
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;
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
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 ) {
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 ) {
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 ) {
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 ) {
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 ) {
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' :
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 );
679
680
return $vars;
681
}
682
+
683
/**
684
* Orderby Posts column
685
*
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' )
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');
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 ) {
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 ) {
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'];
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 );
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
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
*
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
*
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'];
968
$this->set_sorting_preference( $type, $vars['orderby'], $vars['order'] );
969
}
970
}
971
+
972
/**
973
* Get posts sorted by taxonomy
974
*
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
*
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 apply_filters( 'cpac-get-orderby-type', $column, $orderby, $type );
1041
}
1042
}
1043
}
1044
+ return apply_filters( 'cpac-get-orderby-type', false, $orderby, $type );
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
*
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
*
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
*
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
*
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
*
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
}
classes/{utility.php → third_party.php} RENAMED
@@ -13,11 +13,12 @@ function pre_load_wordpress_seo_class_metabox()
13
{
14
global $pagenow;
15
16
- if (
17
- isset( $_REQUEST['page'] ) &&
18
- 'codepress-admin-columns' == $_REQUEST['page'] &&
19
- 'options-general.php' == $pagenow &&
20
- defined('WPSEO_PATH') && file_exists(WPSEO_PATH.'admin/class-metabox.php')
21
) {
22
require_once WPSEO_PATH.'admin/class-metabox.php';
23
}
@@ -27,14 +28,14 @@ add_action( 'plugins_loaded', 'pre_load_wordpress_seo_class_metabox', 0 );
27
/**
28
* Fix which remove the Advanced Custom Fields Type (acf) from the admin columns settings page
29
*
30
- * @since 1.5
31
*/
32
function remove_acf_from_cpac_post_types( $post_types )
33
{
34
- if ( class_exists('Acf') ) {
35
unset( $post_types['acf'] );
36
}
37
-
38
return $post_types;
39
}
40
add_filter( 'cpac-get-post-types', 'remove_acf_from_cpac_post_types' );
@@ -42,16 +43,29 @@ add_filter( 'cpac-get-post-types', 'remove_acf_from_cpac_post_types' );
42
/**
43
* Fix which removes bbPress Posttypes ( forum, reply and topic ) from the admin columns settings page
44
*
45
- * @since 1.5
46
*/
47
function remove_bbpress_from_cpac_post_types( $post_types )
48
{
49
- if ( class_exists('bbPress') ) {
50
unset( $post_types['topic'] );
51
unset( $post_types['reply'] );
52
unset( $post_types['forum'] );
53
}
54
-
55
return $post_types;
56
}
57
- add_filter( 'cpac-get-post-types', 'remove_bbpress_from_cpac_post_types' );
13
{
14
global $pagenow;
15
16
+ if (
17
+ isset($_REQUEST['page']) &&
18
+ 'codepress-admin-columns' == $_REQUEST['page'] &&
19
+ 'options-general.php' == $pagenow &&
20
+ defined('WPSEO_PATH') &&
21
+ file_exists(WPSEO_PATH.'admin/class-metabox.php')
22
) {
23
require_once WPSEO_PATH.'admin/class-metabox.php';
24
}
28
/**
29
* Fix which remove the Advanced Custom Fields Type (acf) from the admin columns settings page
30
*
31
+ * @since 1.5
32
*/
33
function remove_acf_from_cpac_post_types( $post_types )
34
{
35
+ if ( class_exists('Acf') ) {
36
unset( $post_types['acf'] );
37
}
38
+
39
return $post_types;
40
}
41
add_filter( 'cpac-get-post-types', 'remove_acf_from_cpac_post_types' );
43
/**
44
* Fix which removes bbPress Posttypes ( forum, reply and topic ) from the admin columns settings page
45
*
46
+ * @since 1.5
47
*/
48
function remove_bbpress_from_cpac_post_types( $post_types )
49
{
50
+ if ( class_exists('bbPress') ) {
51
unset( $post_types['topic'] );
52
unset( $post_types['reply'] );
53
unset( $post_types['forum'] );
54
}
55
+
56
return $post_types;
57
}
58
+ add_filter( 'cpac-get-post-types', 'remove_bbpress_from_cpac_post_types' );
59
+
60
+ /**
61
+ * Add support for All in SEO columns
62
+ *
63
+ * @since 1.5
64
+ */
65
+ function cpac_load_aioseop_addmycolumns()
66
+ {
67
+ if ( function_exists('aioseop_addmycolumns') ) {
68
+ aioseop_addmycolumns();
69
+ }
70
+ }
71
+ add_action( 'cpac-get-default-columns-posts', 'cpac_load_aioseop_addmycolumns' );
classes/values.php CHANGED
@@ -7,70 +7,70 @@
7
*
8
*/
9
class CPAC_Values
10
- {
11
protected $excerpt_length, $thumbnail_size;
12
-
13
/**
14
* Constructor
15
*
16
* @since 1.0
17
*/
18
function __construct()
19
- {
20
// number of words
21
- $this->excerpt_length = 20;
22
$this->thumbnail_size = apply_filters( 'cpac_thumbnail_size', array(80,80) );
23
}
24
-
25
/**
26
* Admin requests for orderby column
27
*
28
* @since 1.0
29
*/
30
public function get_stored_columns($type)
31
- {
32
return Codepress_Admin_Columns::get_stored_columns($type);
33
}
34
-
35
/**
36
* Checks if column-meta key exists
37
*
38
* @since 1.0
39
*/
40
- public static function is_column_meta( $id = '' )
41
{
42
return Codepress_Admin_Columns::is_column_meta( $id );
43
}
44
-
45
/**
46
* Returns excerpt
47
*
48
* @since 1.0
49
*/
50
- protected function get_post_excerpt($post_id)
51
{
52
global $post;
53
-
54
$save_post = $post;
55
$post = get_post($post_id);
56
$excerpt = get_the_excerpt();
57
$post = $save_post;
58
-
59
- $output = $this->get_shortened_string($excerpt, $this->excerpt_length );
60
-
61
return $output;
62
}
63
-
64
/**
65
* Returns shortened string
66
*
67
* @since 1.0
68
*/
69
- protected function get_shortened_string($string = '', $num_words = 55, $more = null)
70
{
71
if (!$string)
72
return false;
73
-
74
return wp_trim_words( $string, $num_words, $more );
75
}
76
@@ -83,8 +83,8 @@ class CPAC_Values
83
{
84
if ( $name )
85
return sprintf("<img alt='' src='%s' title='%s'/>", CPAC_URL."/assets/images/{$name}", $title);
86
- }
87
-
88
/**
89
* Shorten URL
90
*
@@ -94,19 +94,19 @@ class CPAC_Values
94
{
95
if ( !$url )
96
return false;
97
-
98
// shorten url
99
$short_url = url_shorten( $url );
100
-
101
- return "<a title='{$url}' href='{$url}'>{$short_url}</a>";
102
}
103
-
104
/**
105
* Get column value of post attachments
106
*
107
* @since 1.0
108
*/
109
- protected function get_column_value_attachments( $post_id )
110
{
111
$result = '';
112
$attachment_ids = $this->get_attachment_ids($post_id);
@@ -118,30 +118,30 @@ class CPAC_Values
118
}
119
return $result;
120
}
121
-
122
/**
123
* Get column value of post attachments
124
*
125
* @since 1.2.1
126
*/
127
- protected function get_attachment_ids( $post_id )
128
{
129
return Codepress_Admin_Columns::get_attachment_ids( $post_id );
130
}
131
-
132
/**
133
* Get a thumbnail
134
*
135
* @since 1.0
136
*/
137
- protected function get_thumbnail( $image = '' )
138
- {
139
if ( empty($image) )
140
return false;
141
-
142
// get thumbnail image size
143
$image_size = $this->thumbnail_size; // w, h
144
-
145
// incase the thumbnail dimension is set by name
146
if ( !is_array($image_size) ) {
147
global $_wp_additional_image_sizes;
@@ -150,71 +150,71 @@ class CPAC_Values
150
$image_size = array( $_size['width'], $_size['height'] );
151
}
152
}
153
-
154
// fallback for image size incase the passed size name does not exists
155
if ( !isset($image_size[0]) || !isset($image_size[1]) ) {
156
$image_size = array(80, 80);
157
}
158
-
159
// get correct image path
160
$image_path = str_replace( WP_CONTENT_URL, WP_CONTENT_DIR, $image);
161
-
162
- // resize image
163
if ( file_exists($image_path) && $this->is_image($image_path) ) {
164
$resized = image_resize( $image_path, $image_size[0], $image_size[1], true);
165
-
166
// resize worked
167
if ( ! is_wp_error( $resized ) ) {
168
$image = str_replace( WP_CONTENT_DIR, WP_CONTENT_URL, $resized);
169
-
170
return "<img src='{$image}' alt='' width='{$image_size[0]}' height='{$image_size[1]}' />";
171
}
172
-
173
// resizing failed so let's return full image with maxed dimensions
174
else {
175
- return "<img src='{$image}' alt='' style='max-width:{$image_size[0]}px;max-height:{$image_size[1]}px' />";
176
- }
177
}
178
-
179
return false;
180
}
181
-
182
/**
183
* Checks an URL for image extension
184
*
185
* @since 1.2
186
*/
187
- protected function is_image($url)
188
{
189
$validExt = array('.jpg', '.jpeg', '.gif', '.png', '.bmp');
190
$ext = strrchr($url, '.');
191
-
192
return in_array($ext, $validExt);
193
- }
194
-
195
/**
196
* Get a thumbnail
197
*
198
* @since 1.3.1
199
*/
200
- protected function get_media_thumbnails($meta)
201
{
202
$meta = $this->strip_trim( str_replace(' ','', $meta) );
203
-
204
// split media ids
205
$media_ids = array($meta);
206
- if ( strpos($meta, ',') !== false )
207
$media_ids = explode(',', $meta);
208
-
209
// check if media exists
210
$thumbs = '';
211
foreach ( $media_ids as $media_id )
212
if ( is_numeric($media_id) )
213
$thumbs .= wp_get_attachment_url($media_id) ? "<span class='cpac-column-value-image'>".wp_get_attachment_image( $media_id, array(80,80), true )."</span>" : '';
214
-
215
- return $thumbs;
216
- }
217
-
218
/**
219
* Convert file size to readable format
220
*
@@ -225,244 +225,244 @@ class CPAC_Values
225
$filesizename = array(" Bytes", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB");
226
return $size ? round($size/pow(1024, ($i = floor(log($size, 1024)))), 2) . $filesizename[$i] : '0 Bytes';
227
}
228
-
229
/**
230
* Get column value of Custom Field
231
*
232
* @since 1.0
233
- */
234
- protected function get_column_value_custom_field($object_id, $column_name, $meta_type = 'post')
235
{
236
/** Users */
237
- if ( $meta_type == 'user' ) {
238
$type = 'wp-users';
239
}
240
-
241
/** Media */
242
- if ( $meta_type == 'media' ) {
243
$type = 'wp-media';
244
$meta_type = 'post';
245
}
246
-
247
/** Posts */
248
else {
249
$type = get_post_type($object_id);
250
}
251
-
252
// get column
253
$columns = $this->get_stored_columns($type);
254
-
255
// inputs
256
$field = isset($columns[$column_name]['field']) ? $columns[$column_name]['field'] : '';
257
$fieldtype = isset($columns[$column_name]['field_type']) ? $columns[$column_name]['field_type'] : '';
258
$before = isset($columns[$column_name]['before']) ? $columns[$column_name]['before'] : '';
259
$after = isset($columns[$column_name]['after']) ? $columns[$column_name]['after'] : '';
260
-
261
// rename hidden custom fields to their original name
262
$field = substr($field,0,10) == "cpachidden" ? str_replace('cpachidden','',$field) : $field;
263
-
264
// Get meta field value
265
$meta = get_metadata($meta_type, $object_id, $field, true);
266
267
// multiple meta values
268
- if ( ( $fieldtype == 'array' && is_array($meta) ) || is_array($meta) ) {
269
$meta = get_metadata($meta_type, $object_id, $field, true);
270
$meta = $this->recursive_implode(', ', $meta);
271
}
272
273
// make sure there are no serialized arrays or null data
274
- if ( !is_string($meta) )
275
return false;
276
-
277
// handles each field type differently..
278
- switch ($fieldtype) :
279
-
280
// Image
281
case "image" :
282
$meta = $this->get_thumbnail($meta);
283
break;
284
-
285
// Media Library ID
286
case "library_id" :
287
- $meta = $this->get_media_thumbnails($meta);
288
break;
289
-
290
// Excerpt
291
case "excerpt" :
292
$meta = $this->get_shortened_string($meta, $this->excerpt_length);
293
break;
294
-
295
// Date
296
case "date" :
297
$meta = $this->get_date($meta);
298
break;
299
-
300
// Post Title
301
case "title_by_id" :
302
$titles = $this->get_custom_field_value_title($meta);
303
if ( $titles )
304
$meta = $titles;
305
break;
306
-
307
// User Name
308
case "user_by_id" :
309
$names = $this->get_custom_field_value_user($meta);
310
if ( $names )
311
- $meta = $names;
312
break;
313
-
314
// Checkmark
315
case "checkmark" :
316
$checkmark = $this->get_asset_image('checkmark.png');
317
-
318
if ( empty($meta) || 'false' === $meta || '0' === $meta ) {
319
$checkmark = '';
320
}
321
-
322
- $meta = $checkmark;
323
break;
324
-
325
// Color
326
case "color" :
327
if ( !empty($meta) ) {
328
$meta = "<div class='cpac-color'><span style='background-color:{$meta}'></span>{$meta}</div>";
329
}
330
break;
331
-
332
- endswitch;
333
334
// filter for customization
335
$meta = apply_filters('cpac_get_column_value_custom_field', $meta, $fieldtype, $field, $type, $object_id );
336
-
337
// add before and after string
338
if ( $meta ) {
339
$meta = "{$before}{$meta}{$after}";
340
}
341
-
342
return $meta;
343
}
344
-
345
/**
346
* Get custom field value 'Title by ID'
347
*
348
* @since 1.3
349
*/
350
- protected function get_custom_field_value_title($meta)
351
{
352
//remove white spaces and strip tags
353
$meta = $this->strip_trim( str_replace(' ','', $meta) );
354
-
355
// var
356
$ids = $titles = array();
357
-
358
// check for multiple id's
359
if ( strpos($meta, ',') !== false )
360
- $ids = explode(',',$meta);
361
elseif ( is_numeric($meta) )
362
- $ids[] = $meta;
363
-
364
// display title with link
365
if ( $ids && is_array($ids) ) {
366
- foreach ( $ids as $id ) {
367
$title = is_numeric($id) ? get_the_title($id) : '';
368
$link = get_edit_post_link($id);
369
if ( $title )
370
$titles[] = $link ? "<a href='{$link}'>{$title}</a>" : $title;
371
}
372
}
373
-
374
return implode('<span class="cpac-divider"></span>', $titles);
375
}
376
-
377
/**
378
* Get custom field value 'User by ID'
379
*
380
* @since 1.4.6.3
381
*/
382
- protected function get_custom_field_value_user($meta)
383
{
384
//remove white spaces and strip tags
385
$meta = $this->strip_trim( str_replace(' ','', $meta) );
386
-
387
// var
388
$ids = $names = array();
389
-
390
// check for multiple id's
391
if ( strpos($meta, ',') !== false )
392
$ids = explode(',',$meta);
393
elseif ( is_numeric($meta) )
394
- $ids[] = $meta;
395
-
396
// display username
397
if ( $ids && is_array($ids) ) {
398
foreach ( $ids as $id ) {
399
if ( !is_numeric($id) )
400
continue;
401
-
402
$userdata = get_userdata($id);
403
if ( is_object($userdata) && !empty( $userdata->display_name ) ) {
404
$names[] = $userdata->display_name;
405
- }
406
}
407
}
408
-
409
return implode('<span class="cpac-divider"></span>', $names);
410
}
411
-
412
/**
413
* Get column value of Custom Field
414
*
415
* @since 1.2
416
*/
417
- protected function get_user_column_value_custom_field($user_id, $id)
418
- {
419
$columns = $this->get_stored_columns('wp-users');
420
-
421
// inputs
422
$field = isset($columns[$id]['field']) ? $columns[$id]['field'] : '';
423
$fieldtype = isset($columns[$id]['field_type']) ? $columns[$id]['field_type'] : '';
424
$before = isset($columns[$id]['before']) ? $columns[$id]['before'] : '';
425
$after = isset($columns[$id]['after']) ? $columns[$id]['after'] : '';
426
-
427
// Get meta field value
428
$meta = get_user_meta($user_id, $field, true);
429
-
430
// multiple meta values
431
- if ( ( $fieldtype == 'array' && is_array($meta) ) || is_array($meta) ) {
432
$meta = get_user_meta($user_id, $field);
433
$meta = $this->recursive_implode(', ', $meta);
434
}
435
-
436
// make sure there are no serialized arrays or empty meta data
437
- if ( empty($meta) || !is_string($meta) )
438
return false;
439
-
440
// handles each field type differently..
441
- switch ($fieldtype) :
442
-
443
// Image
444
- case "image" :
445
$meta = $this->get_thumbnail($meta);
446
break;
447
-
448
// Media Library ID
449
case "library_id" :
450
$meta = $this->get_media_thumbnails($meta);
451
break;
452
-
453
// Excerpt
454
case "excerpt" :
455
$meta = $this->get_shortened_string($meta, $this->excerpt_length);
456
break;
457
-
458
- endswitch;
459
-
460
// filter for customization
461
$meta = apply_filters('cpac_get_user_column_value_custom_field', $meta, $fieldtype, $field );
462
-
463
// add before and after string
464
$meta = "{$before}{$meta}{$after}";
465
-
466
return $meta;
467
}
468
@@ -471,7 +471,7 @@ class CPAC_Values
471
*
472
* @since 1.0
473
*/
474
- protected function recursive_implode( $glue, $pieces )
475
{
476
foreach( $pieces as $r_pieces ) {
477
if( is_array( $r_pieces ) ) {
@@ -483,46 +483,55 @@ class CPAC_Values
483
}
484
if ( isset($retVal) && is_array($retVal) )
485
return implode( $glue, $retVal );
486
-
487
return false;
488
}
489
-
490
/**
491
* Strip tags and trim
492
*
493
* @since 1.3
494
*/
495
- protected function strip_trim($string)
496
{
497
return Codepress_Admin_Columns::strip_trim($string);
498
}
499
-
500
/**
501
* Get date
502
*
503
* @since 1.3.1
504
*/
505
- protected function get_date($date)
506
- {
507
- if ( ! $date )
508
return false;
509
-
510
- return date_i18n( get_option('date_format'), strtotime($date) );
511
}
512
-
513
/**
514
* Get time
515
*
516
* @since 1.3.1
517
*/
518
- protected function get_time($date)
519
{
520
if ( ! $date )
521
return false;
522
-
523
if ( ! is_numeric($date) )
524
$date = strtotime($date);
525
-
526
return date_i18n( get_option('time_format'), $date );
527
}
528
}
7
*
8
*/
9
class CPAC_Values
10
+ {
11
protected $excerpt_length, $thumbnail_size;
12
+
13
/**
14
* Constructor
15
*
16
* @since 1.0 </