-
Notifications
You must be signed in to change notification settings - Fork 0
/
cc-cpt.php
1202 lines (1006 loc) · 39.9 KB
/
cc-cpt.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Custom Post Type Class
*
* @link https://github.com/corneliucirlan
*
* @author Corneliu Cirlan
* @link http://www.TwoCSoft.com
* @version 1.1
*/
class CC_CPT
{
/**
* Post type name.
*
* @since 1.0
*
* @var string $post_type_name Holds the name of the post type.
*/
public $post_type_name;
/**
* Holds the singular name of the post type. This is a human friendly
* name, capitalized with spaces assigned on __construct().
*
* @since 1.0
*
* @var string $singular Post type singular name.
*/
public $singular;
/**
* Holds the plural name of the post type. This is a human friendly
* name, capitalized with spaces assigned on __construct().
*
* @since 1.0
*
* @var string $plural Singular post type name.
*/
public $plural;
/**
* Post type slug. This is a robot friendly name, all lowercase and uses
* hyphens assigned on __construct().
*
* @since 1.0
*
* @var string $slug Holds the post type slug name.
*/
public $slug;
/**
* User submitted options assigned on __construct().
*
* @since 1.0
*
* @var array $options Holds the user submitted post type options.
*/
public $options;
/**
* Taxonomies
*
* @since 1.0
*
* @var array $taxonomies Holds an array of taxonomies associated with the post type.
*/
public $taxonomies;
/**
* Taxonomy settings, an array of the taxonomies associated with the post
* type and their options used when registering the taxonomies.
*
* @since 1.0
*
* @var array $taxonomy_settings Holds the taxonomy settings.
*/
public $taxonomy_settings;
/**
* Exisiting taxonomies to be registered after the posty has been registered
*
* @since 1.0
*
* @var array $existing_taxonomies holds exisiting taxonomies
*/
public $existing_taxonomies;
/**
* Taxonomy filters. Defines which filters are to appear on admin edit
* screen used in add_taxonmy_filters().
*
* @since 1.0
*
* @var array $filters Taxonomy filters.
*/
public $filters;
/**
* Defines which columns are to appear on the admin edit screen used
* in add_admin_columns().
*
* @since 1.0
*
* @var array $columns Columns visible in admin edit screen.
*/
public $columns;
/**
* User defined public functions to populate admin columns.
*
* @since 1.0
*
* @var array $custom_populate_columns User public functions to populate columns.
*/
public $custom_populate_columns;
/**
* Sortable columns.
*
* @since 1.0
*
* @var array $sortable Define which columns are sortable on the admin edit screen.
*/
public $sortable;
/**
* Textdomain used for translation. Use the set_textdomain() method to set a custom textdomain.
*
* @since 1.0
*
* @var string $textdomain Used for internationalising. Defaults to "cpt" without quotes.
*/
public $textdomain = 'cc-cpt';
/**
* Constructor
*
* Register a custom post type.
*
* @since 1.0
*
* @param mixed $post_type_names The name(s) of the post type, accepts (post type name, slug, plural, singular).
* @param array $options User submitted options.
*/
public function __construct($post_type_names, $options = array())
{
// Check if post type names is a string or an array.
if (is_array($post_type_names)):
// Add names to object.
$names = array(
'singular',
'plural',
'slug'
);
// Set the post type name.
$this->post_type_name = $post_type_names['post_type_name'];
// Cycle through possible names.
foreach ($names as $name):
// If the name has been set by user.
if (isset($post_type_names[$name])):
// Use the user setting
$this->$name = $post_type_names[$name];
// Else generate the name.
else:
// define the method to be used
$method = 'get_' . $name;
// Generate the name
$this->$name = $this->$method();
endif;
endforeach;
// Else the post type name is only supplied.
else:
// Apply to post type name.
$this->post_type_name = $post_type_names;
// Set the slug name.
$this->slug = $this->get_slug();
// Set the plural name label.
$this->plural = $this->get_plural();
// Set the singular name label.
$this->singular = $this->get_singular();
endif;
// Set the user submitted options to the object.
$this->options = $options;
// Register taxonomies.
$this->add_action('init', array(&$this, 'register_taxonomies'));
// Register the post type.
$this->add_action('init', array(&$this, 'register_post_type'));
// add dashboard glance item
$this->add_filter('dashboard_glance_items', array(&$this, 'dashboad_glance'));
// add recent activity
add_filter( 'dashboard_recent_posts_query_args', array(&$this, 'dashboard_recent_posts'), 10, 1 );
// Register exisiting taxonomies.
$this->add_action('init', array(&$this, 'register_exisiting_taxonomies'));
// Add taxonomy to admin edit columns.
$this->add_filter('manage_edit-' . $this->post_type_name.'_columns', array(&$this, 'add_admin_columns'));
// Populate the taxonomy columns with the posts terms.
$this->add_action('manage_' . $this->post_type_name.'_posts_custom_column', array(&$this, 'populate_admin_columns'), 10, 2);
// Add filter select option to admin edit.
$this->add_action('restrict_manage_posts', array(&$this, 'add_taxonomy_filters'));
// rewrite post update messages
$this->add_filter('post_updated_messages', array(&$this, 'updated_messages'));
$this->add_filter('bulk_post_updated_messages', array(&$this, 'bulk_updated_messages'), 10, 2);
}
/**
* Get
*
* Helper public function to get an object variable.
*
* @since 1.0
*
* @param string $var The variable you would like to retrieve.
* @return mixed Returns the value on success, boolean false whe it fails.
*/
public function get($var)
{
// If the variable exists.
if ($this->$var)
// On success return the value.
return $this->$var;
else
// on fail return false
return false;
}
/**
* Set
*
* Helper public function used to set an object variable. Can overwrite existsing
* variables or create new ones. Cannot overwrite reserved variables.
*
* @since 1.0
*
* @param mixed $var The variable you would like to create/overwrite.
* @param mixed $value The value you would like to set to the variable.
*/
public function set($var, $value)
{
// An array of reserved variables that cannot be overwritten.
$reserved = array(
'config',
'post_type_name',
'singular',
'plural',
'slug',
'options',
'taxonomies'
);
// If the variable is not a reserved variable
if (!in_array($var, $reserved))
// Write variable and value
$this->$var = $value;
}
/**
* Add Action
*
* Helper public function to add add_action WordPress filters.
*
* @since 1.0
*
* @param string $action Name of the action.
* @param string $function public function to hook that will run on action.
* @param integet $priority Order in which to execute the public function, relation to other public functions hooked to this action.
* @param integer $accepted_args The number of arguments the public function accepts.
*/
public function add_action($action, $function, $priority = 10, $accepted_args = 1)
{
// Pass variables into WordPress add_action public function
add_action($action, $function, $priority, $accepted_args);
}
/**
* Add Filter
*
* Create add_filter WordPress filter.
*
* @see http://codex.wordpress.org/public function_Reference/add_filter
*
* @since 1.0
*
* @param string $action Name of the action to hook to, e.g 'init'.
* @param string $function public function to hook that will run on @action.
* @param int $priority Order in which to execute the public function, relation to other public function hooked to this action.
* @param int $accepted_args The number of arguements the public function accepts.
*/
public function add_filter($action, $function, $priority = 10, $accepted_args = 1)
{
// Pass variables into Wordpress add_action public function
add_filter($action, $function, $priority, $accepted_args);
}
/**
* Get slug
*
* Creates an url friendly slug.
*
* @since 1.0
*
* @param string $name Name to slugify.
* @return string $name Returns the slug.
*/
public function get_slug($name = null)
{
// If no name set use the post type name.
if (!isset($name))
$name = $this->post_type_name;
// Name to lower case.
$name = strtolower($name);
// Replace spaces with hyphen.
$name = str_replace(" ", "-", $name);
// Replace underscore with hyphen.
$name = str_replace("_", "-", $name);
return $name;
}
/**
* Get plural
*
* Returns the friendly plural name.
*
* ucwords capitalize words
* strtolower makes string lowercase before capitalizing
* str_replace replace all instances of _ to space
*
* @since 1.0
*
* @param string $name The slug name you want to pluralize.
* @return string the friendly pluralized name.
*/
public function get_plural($name = null)
{
// If no name is passed the post_type_name is used.
if (!isset($name))
$name = $this->post_type_name;
// Return the plural name. Add 's' to the end.
return $this->get_human_friendly($name) . 's';
}
/**
* Get singular
*
* Returns the friendly singular name.
*
* ucwords capitalize words
* strtolower makes string lowercase before capitalizing
* str_replace replace all instances of _ to space
*
* @since 1.0
*
* @param string $name The slug name you want to unpluralize.
* @return string The friendly singular name.
*/
public function get_singular($name = null)
{
// If no name is passed the post_type_name is used.
if (!isset($name))
$name = $this->post_type_name;
// Return the string.
return $this->get_human_friendly($name);
}
/**
* Get human friendly
*
* Returns the human friendly name.
*
* ucwords capitalize words
* strtolower makes string lowercase before capitalizing
* str_replace replace all instances of hyphens and underscores to spaces
*
* @since 1.0
*
* @param string $name The name you want to make friendly.
* @return string The human friendly name.
*/
public function get_human_friendly($name = null)
{
// If no name is passed the post_type_name is used.
if (!isset($name))
$name = $this->post_type_name;
// Return human friendly name.
return ucwords(strtolower(str_replace("-", " ", str_replace("_", " ", $name))));
}
/**
* Register Post Type
*
* @see http://codex.wordpress.org/public function_Reference/register_post_type
*
* @since 1.0
*/
public function register_post_type()
{
// Friendly post type names.
$plural = $this->plural;
$singular = $this->singular;
$slug = $this->slug;
// Default labels.
$labels = array(
'name' => sprintf(__('%s', $this->textdomain), $plural),
'singular_name' => sprintf(__('%s', $this->textdomain), $singular),
'menu_name' => sprintf(__('%s', $this->textdomain), $plural),
'all_items' => sprintf(__('%s', $this->textdomain), $plural),
'add_new' => __('Add New', $this->textdomain),
'add_new_item' => sprintf(__('Add New %s', $this->textdomain), $singular),
'edit_item' => sprintf(__('Edit %s', $this->textdomain), $singular),
'new_item' => sprintf(__('New %s', $this->textdomain), $singular),
'view_item' => sprintf(__('View %s', $this->textdomain), $singular),
'search_items' => sprintf(__('Search %s', $this->textdomain), $plural),
'not_found' => sprintf(__('No %s found', $this->textdomain), $plural),
'not_found_in_trash' => sprintf(__('No %s found in Trash', $this->textdomain), $plural),
'parent_item_colon' => sprintf(__('Parent %s:', $this->textdomain), $singular)
);
// Default options.
$defaults = array(
'labels' => $labels,
'hierarchical' => false,
'description' => 'description',
'taxonomies' => array(),
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_admin_bar' => true,
'menu_position' => null,
'menu_icon' => null,
'show_in_nav_menus' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'has_archive' => true,
'query_var' => true,
'can_export' => true,
'capability_type' => 'post',
'supports' => array(
'title', 'editor', 'author', 'thumbnail',
'excerpt','custom-fields', 'trackbacks', 'comments',
'revisions', 'page-attributes', 'post-formats'
),
'rewrite' => array(
'slug' => $slug,
'with_front' => false,
),
);
// Merge user submitted options with defaults.
$options = array_replace_recursive($defaults, $this->options);
// Set the object options as full options passed.
$this->options = $options;
// Check that the post type doesn't already exist.
if (!post_type_exists($this->post_type_name))
// Register the post type.
register_post_type($this->post_type_name, $options);
}
/**
* Dashboard glance
*
* @see https://developer.wordpress.org/reference/hooks/dashboard_glance_items
*
* @since 1.0
*/
public function dashboad_glance()
{
// get number of published posts
$numberOfPosts = number_format_i18n(wp_count_posts($this->post_type_name)->publish);
// render CPT number
echo '<li class="post-count"><a href="edit.php?post_type='.$this->post_type_name.'">'.$numberOfPosts.' '._n($this->singular, $this->plural, intval($numberOfPosts), $this->textdomain).'</a></li>';
}
/**
* Recent Activity
*
* @since 1.1
*
* @param array $query_args The current arguments array for displaying recent activity
* @return array Modified arguments array to include this post type
*/
public function dashboard_recent_posts($query_args)
{
if (!is_array($query_args['post_type']))
$query_args['post_type'] = array($query_args['post_type'], $this->post_type_name);
else
$query_args['post_type'][] = $this->post_type_name;
return $query_args;
}
/**
* Register taxonomy
*
* @see https://codex.wordpress.org/Function_Reference/register_taxonomy
*
* @since 1.0
*
* @param string $taxonomy_name The slug for the taxonomy.
* @param array $options Taxonomy options.
*/
public function register_taxonomy($taxonomy_names, $options = array())
{
// Post type defaults to $this post type if unspecified.
$post_type = $this->post_type_name;
// An array of the names required excluding taxonomy_name.
$names = array(
'singular',
'plural',
'slug'
);
// if an array of names are passed
if (is_array($taxonomy_names)):
// Set the taxonomy name
$taxonomy_name = $taxonomy_names['taxonomy_name'];
// Cycle through possible names.
foreach ($names as $name):
// If the user has set the name.
if (isset($taxonomy_names[$name])):
// Use user submitted name.
$$name = $taxonomy_names[$name];
// Else generate the name.
else:
// Define the public function to be used.
$method = 'get_' . $name;
// Generate the name
$$name = $this->$method($taxonomy_name);
endif;
endforeach;
// Else if only the taxonomy_name has been supplied.
else:
// Create user friendly names.
$taxonomy_name = $taxonomy_names;
$singular = $this->get_singular($taxonomy_name);
$plural = $this->get_plural($taxonomy_name);
$slug = $this->get_slug($taxonomy_name);
endif;
// Default labels.
$labels = array(
'name' => sprintf(__('%s', $this->textdomain), $plural),
'singular_name' => sprintf(__('%s', $this->textdomain), $singular),
'menu_name' => sprintf(__('%s', $this->textdomain), $plural),
'all_items' => sprintf(__('All %s', $this->textdomain), $plural),
'edit_item' => sprintf(__('Edit %s', $this->textdomain), $singular),
'view_item' => sprintf(__('View %s', $this->textdomain), $singular),
'update_item' => sprintf(__('Update %s', $this->textdomain), $singular),
'add_new_item' => sprintf(__('Add New %s', $this->textdomain), $singular),
'new_item_name' => sprintf(__('New %s Name', $this->textdomain), $singular),
'parent_item' => sprintf(__('Parent %s', $this->textdomain), $plural),
'parent_item_colon' => sprintf(__('Parent %s:', $this->textdomain), $plural),
'search_items' => sprintf(__('Search %s', $this->textdomain), $plural),
'popular_items' => sprintf(__('Popular %s', $this->textdomain), $plural),
'separate_items_with_commas' => sprintf(__('Seperate %s with commas', $this->textdomain), $plural),
'add_or_remove_items' => sprintf(__('Add or remove %s', $this->textdomain), $plural),
'choose_from_most_used' => sprintf(__('Choose from most used %s', $this->textdomain), $plural),
'not_found' => sprintf(__('No %s found', $this->textdomain), $plural),
);
// Default options.
$defaults = array(
'labels' => $labels,
'hierarchical' => true,
'rewrite' => array('slug' => $slug)
);
// Merge default options with user submitted options.
$options = array_replace_recursive($defaults, $options);
// Add the taxonomy to the object array, this is used to add columns and filters to admin panel.
$this->taxonomies[] = $taxonomy_name;
// Create array used when registering taxonomies.
$this->taxonomy_settings[$taxonomy_name] = $options;
}
/**
* Register taxonomies
*
* Cycles through taxonomies added with the class and registers them.
*
* @since 1.0
*/
public function register_taxonomies()
{
if (is_array($this->taxonomy_settings)):
// Foreach taxonomy registered with the post type.
foreach ($this->taxonomy_settings as $taxonomy_name => $options):
// Register the taxonomy if it doesn't exist.
if (!taxonomy_exists($taxonomy_name))
// Register the taxonomy with Wordpress
register_taxonomy($taxonomy_name, $this->post_type_name, $options);
else
// If taxonomy exists, register it later with register_exisiting_taxonomies
$this->existing_taxonomies[] = $taxonomy_name;
endforeach;
endif;
}
/**
* Register Exisiting Taxonomies
*
* Cycles through exisiting taxonomies and registers them after the post type has been registered
*
* @since 1.0
*/
public function register_exisiting_taxonomies()
{
if (is_array($this->existing_taxonomies))
foreach ($this->existing_taxonomies as $taxonomy_name)
register_taxonomy_for_object_type($taxonomy_name, $this->post_type_name);
}
/**
* Add admin columns
*
* Adds columns to the admin edit screen. public function is used with add_action
*
* @since 1.0
*
* @param array $columns Columns to be added to the admin edit screen.
* @return array
*/
public function add_admin_columns($columns)
{
// If no user columns have been specified, add taxonomies
if (!isset($this->columns)):
$new_columns = array();
// determine which column to add custom taxonomies after
if (is_array($this->taxonomies) && in_array('post_tag', $this->taxonomies) || $this->post_type_name === 'post')
$after = 'tags';
elseif (is_array($this->taxonomies) && in_array('category', $this->taxonomies) || $this->post_type_name === 'post')
$after = 'categories';
elseif (post_type_supports($this->post_type_name, 'author'))
$after = 'author';
else
$after = 'title';
// foreach exisiting columns
foreach ($columns as $key => $title):
// add exisiting column to the new column array
$new_columns[$key] = $title;
// we want to add taxonomy columns after a specific column
if ($key === $after):
// If there are taxonomies registered to the post type.
if (is_array($this->taxonomies)):
// Create a column for each taxonomy.
foreach ($this->taxonomies as $tax):
// WordPress adds Categories and Tags automatically, ignore these
if ($tax !== 'category' && $tax !== 'post_tag'):
// Get the taxonomy object for labels.
$taxonomy_object = get_taxonomy($tax);
// Column key is the slug, value is friendly name.
$new_columns[$tax] = sprintf(__('%s', $this->textdomain), $taxonomy_object->labels->name);
endif;
endforeach;
endif;
endif;
endforeach;
// overide with new columns
$columns = $new_columns;
else:
// Use user submitted columns, these are defined using the object columns() method.
$columns = $this->columns;
endif;
return $columns;
}
/**
* Populate admin columns
*
* Populate custom columns on the admin edit screen.
*
* @since 1.0
*
* @param string $column The name of the column.
* @param integer $post_id The post ID.
*/
public function populate_admin_columns($column, $post_id)
{
// Get wordpress $post object.
global $post;
// determine the column
switch ($column):
// If column is a taxonomy associated with the post type.
case (taxonomy_exists($column)):
// Get the taxonomy for the post
$terms = get_the_terms($post_id, $column);
// If we have terms.
if (!empty($terms)):
$output = array();
// Loop through each term, linking to the 'edit posts' page for the specific term.
foreach ($terms as $term):
// Output is an array of terms associated with the post.
$output[] = sprintf(
// Define link.
'<a href="%s">%s</a>',
// Create filter url.
esc_url(add_query_arg(array('post_type' => $post->post_type, $column => $term->slug), 'edit.php')),
// Create friendly term name.
esc_html(sanitize_term_field('name', $term->name, $term->term_id, $column, 'display'))
);
endforeach;
// Join the terms, separating them with a comma.
echo join(', ', $output);
// If no terms found.
else:
// Get the taxonomy object for labels
$taxonomy_object = get_taxonomy($column);
// Echo no terms.
printf(__('No %s', $this->textdomain), $taxonomy_object->labels->name);
endif;
break;
// If column is for the post ID.
case 'post_id':
echo $post->ID;
break;
// if the column is prepended with 'meta_', this will automagically retrieve the meta values and display them.
case (preg_match('/^meta_/', $column) ? true : false):
// meta_book_author (meta key = book_author)
$x = substr($column, 5);
$meta = get_post_meta($post->ID, $x);
echo join(", ", $meta);
break;
// If the column is post thumbnail.
case 'icon':
// Create the edit link.
$link = esc_url(add_query_arg(array('post' => $post->ID, 'action' => 'edit'), 'post.php'));
// If it post has a featured image.
if (has_post_thumbnail()):
// Display post featured image with edit link.
echo '<a href="'.$link.'">';
the_post_thumbnail(array(60, 60));
echo '</a>';
else:
// Display default media image with link.
echo '<a href="'.$link.'"><img src="'. site_url('/wp-includes/images/crystal/default.png').'" alt="'.$post->post_title .'" /></a>';
endif;
break;
// Default case checks if the column has a user public function, this is most commonly used for custom fields.
default:
// If there are user custom columns to populate.
if (isset($this->custom_populate_columns) && is_array($this->custom_populate_columns))
// If this column has a user submitted public function to run.
if (isset($this->custom_populate_columns[$column]) && is_callable($this->custom_populate_columns[$column]))
// Run the public function.
call_user_func_array($this->custom_populate_columns[$column], array($column, $post));
break;
endswitch; // end switch ($column)
}
/**
* Filters
*
* User public function to define which taxonomy filters to display on the admin page.
*
* @since 1.0
*
* @param array $filters An array of taxonomy filters to display.
*/
public function filters($filters = array())
{
$this->filters = $filters;
}
/**
* Add taxtonomy filters
*
* Creates select fields for filtering posts by taxonomies on admin edit screen.
*
* @since 1.0
*/
public function add_taxonomy_filters()
{
global $typenow;
global $wp_query;
// Must set this to the post type you want the filter(s) displayed on.
if ($typenow == $this->post_type_name):
// if custom filters are defined use those
if (is_array($this->filters))
$filters = $this->filters;
// else default to use all taxonomies associated with the post
else
$filters = $this->taxonomies;
if (!empty($filters)):
// Foreach of the taxonomies we want to create filters for...
foreach ($filters as $tax_slug):
// ...object for taxonomy, doesn't contain the terms.
$tax = get_taxonomy($tax_slug);
// Get taxonomy terms and order by name.
$args = array(
'orderby' => 'name',
'hide_empty' => false
);
// Get taxonomy terms.
$terms = get_terms($tax_slug, $args);
// If we have terms.
if ($terms):
// Set up select box.
printf(' <select name="%s" class="postform">', $tax_slug);
// Default show all.
printf('<option value="0">%s</option>', sprintf(__('Show all %s', $this->textdomain), $tax->label));
// Foreach term create an option field ...
foreach ($terms as $term):
// ... if filtered by this term make it selected.
if (isset($_GET[$tax_slug]) && $_GET[$tax_slug] === $term->slug)
printf('<option value="%s" selected="selected">%s (%s)</option>', $term->slug, $term->name, $term->count);
// ... create option for taxonomy.
else
printf('<option value="%s">%s (%s)</option>', $term->slug, $term->name, $term->count);
endforeach; // foreach ($terms as $term)
// End the select field.
print('</select> ');
endif; // if ($terms)
endforeach; // foreach ($filters as $tax_slug)
endif; // if (!empty($filters))
endif; // if ($typenow == $this->post_type_name)
}
/**
* Columns
*
* Choose columns to be displayed on the admin edit screen.
*
* @since 1.0
*
* @param array $columns An array of columns to be displayed.
*/
public function columns($columns)
{
// If columns is set.
if (isset($columns))
// Assign user submitted columns to object.
$this->columns = $columns;
}
/**
* Populate columns
*
* Define what and how to populate a speicific admin column.
*
* @since 1.0
*
* @param string $column_name The name of the column to populate.
* @param mixed $callback An anonyous public function or callable array to call when populating the column.
*/
public function populate_column($column_name, $callback)
{
$this->custom_populate_columns[$column_name] = $callback;
}
/**
* Sortable
*
* Define what columns are sortable in the admin edit screen.
*
* @since 1.0
*
* @param array $columns An array of columns that are sortable.
*/
public function sortable($columns = array())
{
// Assign user defined sortable columns to object variable.
$this->sortable = $columns;
// Run filter to make columns sortable.
$this->add_filter('manage_edit-'.$this->post_type_name.'_sortable_columns', array(&$this, 'make_columns_sortable'));
// Run action that sorts columns on request.
$this->add_action('load-edit.php', array(&$this, 'load_edit'));
}
/**
* Make columns sortable
*
* Internal public function that adds user defined sortable columns to WordPress default columns.
*
* @since 1.0
*
* @param array $columns Columns to be sortable.