/
post.php
5315 lines (4586 loc) · 169 KB
/
post.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
/**
* Post functions and post utility function.
*
* @package WordPress
* @subpackage Post
* @since 1.5.0
*/
//
// Post Type Registration
//
/**
* Creates the initial post types when 'init' action is fired.
*
* @since 2.9.0
*/
function create_initial_post_types() {
register_post_type( 'post', array(
'labels' => array(
'name_admin_bar' => _x( 'Post', 'add new on admin bar' ),
),
'public' => true,
'_builtin' => true, /* internal use only. don't use this when registering your own post type. */
'_edit_link' => 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */
'capability_type' => 'post',
'map_meta_cap' => true,
'hierarchical' => false,
'rewrite' => false,
'query_var' => false,
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'post-formats' ),
) );
register_post_type( 'page', array(
'labels' => array(
'name_admin_bar' => _x( 'Page', 'add new on admin bar' ),
),
'public' => true,
'publicly_queryable' => false,
'_builtin' => true, /* internal use only. don't use this when registering your own post type. */
'_edit_link' => 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */
'capability_type' => 'page',
'map_meta_cap' => true,
'hierarchical' => true,
'rewrite' => false,
'query_var' => false,
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'page-attributes', 'custom-fields', 'comments', 'revisions' ),
) );
register_post_type( 'attachment', array(
'labels' => array(
'name' => __( 'Media' ),
'edit_item' => __( 'Edit Media' ),
),
'public' => true,
'show_ui' => false,
'_builtin' => true, /* internal use only. don't use this when registering your own post type. */
'_edit_link' => 'media.php?attachment_id=%d', /* internal use only. don't use this when registering your own post type. */
'capability_type' => 'post',
'map_meta_cap' => true,
'hierarchical' => false,
'rewrite' => false,
'query_var' => false,
'show_in_nav_menus' => false,
'supports' => array( 'comments' ),
) );
register_post_type( 'revision', array(
'labels' => array(
'name' => __( 'Revisions' ),
'singular_name' => __( 'Revision' ),
),
'public' => false,
'_builtin' => true, /* internal use only. don't use this when registering your own post type. */
'_edit_link' => 'revision.php?revision=%d', /* internal use only. don't use this when registering your own post type. */
'capability_type' => 'post',
'map_meta_cap' => true,
'hierarchical' => false,
'rewrite' => false,
'query_var' => false,
'can_export' => false,
) );
register_post_type( 'nav_menu_item', array(
'labels' => array(
'name' => __( 'Navigation Menu Items' ),
'singular_name' => __( 'Navigation Menu Item' ),
),
'public' => false,
'_builtin' => true, /* internal use only. don't use this when registering your own post type. */
'hierarchical' => false,
'rewrite' => false,
'query_var' => false,
) );
register_post_status( 'publish', array(
'label' => _x( 'Published', 'post' ),
'public' => true,
'_builtin' => true, /* internal use only. */
'label_count' => _n_noop( 'Published <span class="count">(%s)</span>', 'Published <span class="count">(%s)</span>' ),
) );
register_post_status( 'future', array(
'label' => _x( 'Scheduled', 'post' ),
'protected' => true,
'_builtin' => true, /* internal use only. */
'label_count' => _n_noop('Scheduled <span class="count">(%s)</span>', 'Scheduled <span class="count">(%s)</span>' ),
) );
register_post_status( 'draft', array(
'label' => _x( 'Draft', 'post' ),
'protected' => true,
'_builtin' => true, /* internal use only. */
'label_count' => _n_noop( 'Draft <span class="count">(%s)</span>', 'Drafts <span class="count">(%s)</span>' ),
) );
register_post_status( 'pending', array(
'label' => _x( 'Pending', 'post' ),
'protected' => true,
'_builtin' => true, /* internal use only. */
'label_count' => _n_noop( 'Pending <span class="count">(%s)</span>', 'Pending <span class="count">(%s)</span>' ),
) );
register_post_status( 'private', array(
'label' => _x( 'Private', 'post' ),
'private' => true,
'_builtin' => true, /* internal use only. */
'label_count' => _n_noop( 'Private <span class="count">(%s)</span>', 'Private <span class="count">(%s)</span>' ),
) );
register_post_status( 'trash', array(
'label' => _x( 'Trash', 'post' ),
'internal' => true,
'_builtin' => true, /* internal use only. */
'label_count' => _n_noop( 'Trash <span class="count">(%s)</span>', 'Trash <span class="count">(%s)</span>' ),
'show_in_admin_status_list' => true,
) );
register_post_status( 'auto-draft', array(
'label' => 'auto-draft',
'internal' => true,
'_builtin' => true, /* internal use only. */
) );
register_post_status( 'inherit', array(
'label' => 'inherit',
'internal' => true,
'_builtin' => true, /* internal use only. */
'exclude_from_search' => false,
) );
}
add_action( 'init', 'create_initial_post_types', 0 ); // highest priority
/**
* Retrieve attached file path based on attachment ID.
*
* You can optionally send it through the 'get_attached_file' filter, but by
* default it will just return the file path unfiltered.
*
* The function works by getting the single post meta name, named
* '_wp_attached_file' and returning it. This is a convenience function to
* prevent looking up the meta name and provide a mechanism for sending the
* attached filename through a filter.
*
* @since 2.0.0
* @uses apply_filters() Calls 'get_attached_file' on file path and attachment ID.
*
* @param int $attachment_id Attachment ID.
* @param bool $unfiltered Whether to apply filters.
* @return string The file path to the attached file.
*/
function get_attached_file( $attachment_id, $unfiltered = false ) {
$file = get_post_meta( $attachment_id, '_wp_attached_file', true );
// If the file is relative, prepend upload dir
if ( 0 !== strpos($file, '/') && !preg_match('|^.:\\\|', $file) && ( ($uploads = wp_upload_dir()) && false === $uploads['error'] ) )
$file = $uploads['basedir'] . "/$file";
if ( $unfiltered )
return $file;
return apply_filters( 'get_attached_file', $file, $attachment_id );
}
/**
* Update attachment file path based on attachment ID.
*
* Used to update the file path of the attachment, which uses post meta name
* '_wp_attached_file' to store the path of the attachment.
*
* @since 2.1.0
* @uses apply_filters() Calls 'update_attached_file' on file path and attachment ID.
*
* @param int $attachment_id Attachment ID
* @param string $file File path for the attachment
* @return bool False on failure, true on success.
*/
function update_attached_file( $attachment_id, $file ) {
if ( !get_post( $attachment_id ) )
return false;
$file = apply_filters( 'update_attached_file', $file, $attachment_id );
$file = _wp_relative_upload_path($file);
return update_post_meta( $attachment_id, '_wp_attached_file', $file );
}
/**
* Return relative path to an uploaded file.
*
* The path is relative to the current upload dir.
*
* @since 2.9.0
* @uses apply_filters() Calls '_wp_relative_upload_path' on file path.
*
* @param string $path Full path to the file
* @return string relative path on success, unchanged path on failure.
*/
function _wp_relative_upload_path( $path ) {
$new_path = $path;
if ( ($uploads = wp_upload_dir()) && false === $uploads['error'] ) {
if ( 0 === strpos($new_path, $uploads['basedir']) ) {
$new_path = str_replace($uploads['basedir'], '', $new_path);
$new_path = ltrim($new_path, '/');
}
}
return apply_filters( '_wp_relative_upload_path', $new_path, $path );
}
/**
* Retrieve all children of the post parent ID.
*
* Normally, without any enhancements, the children would apply to pages. In the
* context of the inner workings of WordPress, pages, posts, and attachments
* share the same table, so therefore the functionality could apply to any one
* of them. It is then noted that while this function does not work on posts, it
* does not mean that it won't work on posts. It is recommended that you know
* what context you wish to retrieve the children of.
*
* Attachments may also be made the child of a post, so if that is an accurate
* statement (which needs to be verified), it would then be possible to get
* all of the attachments for a post. Attachments have since changed since
* version 2.5, so this is most likely unaccurate, but serves generally as an
* example of what is possible.
*
* The arguments listed as defaults are for this function and also of the
* {@link get_posts()} function. The arguments are combined with the
* get_children defaults and are then passed to the {@link get_posts()}
* function, which accepts additional arguments. You can replace the defaults in
* this function, listed below and the additional arguments listed in the
* {@link get_posts()} function.
*
* The 'post_parent' is the most important argument and important attention
* needs to be paid to the $args parameter. If you pass either an object or an
* integer (number), then just the 'post_parent' is grabbed and everything else
* is lost. If you don't specify any arguments, then it is assumed that you are
* in The Loop and the post parent will be grabbed for from the current post.
*
* The 'post_parent' argument is the ID to get the children. The 'numberposts'
* is the amount of posts to retrieve that has a default of '-1', which is
* used to get all of the posts. Giving a number higher than 0 will only
* retrieve that amount of posts.
*
* The 'post_type' and 'post_status' arguments can be used to choose what
* criteria of posts to retrieve. The 'post_type' can be anything, but WordPress
* post types are 'post', 'pages', and 'attachments'. The 'post_status'
* argument will accept any post status within the write administration panels.
*
* @see get_posts() Has additional arguments that can be replaced.
* @internal Claims made in the long description might be inaccurate.
*
* @since 2.0.0
*
* @param mixed $args Optional. User defined arguments for replacing the defaults.
* @param string $output Optional. Constant for return type, either OBJECT (default), ARRAY_A, ARRAY_N.
* @return array|bool False on failure and the type will be determined by $output parameter.
*/
function get_children($args = '', $output = OBJECT) {
$kids = array();
if ( empty( $args ) ) {
if ( isset( $GLOBALS['post'] ) ) {
$args = array('post_parent' => (int) $GLOBALS['post']->post_parent );
} else {
return $kids;
}
} elseif ( is_object( $args ) ) {
$args = array('post_parent' => (int) $args->post_parent );
} elseif ( is_numeric( $args ) ) {
$args = array('post_parent' => (int) $args);
}
$defaults = array(
'numberposts' => -1, 'post_type' => 'any',
'post_status' => 'any', 'post_parent' => 0,
);
$r = wp_parse_args( $args, $defaults );
$children = get_posts( $r );
if ( !$children )
return $kids;
update_post_cache($children);
foreach ( $children as $key => $child )
$kids[$child->ID] = $children[$key];
if ( $output == OBJECT ) {
return $kids;
} elseif ( $output == ARRAY_A ) {
foreach ( (array) $kids as $kid )
$weeuns[$kid->ID] = get_object_vars($kids[$kid->ID]);
return $weeuns;
} elseif ( $output == ARRAY_N ) {
foreach ( (array) $kids as $kid )
$babes[$kid->ID] = array_values(get_object_vars($kids[$kid->ID]));
return $babes;
} else {
return $kids;
}
}
/**
* Get extended entry info (<!--more-->).
*
* There should not be any space after the second dash and before the word
* 'more'. There can be text or space(s) after the word 'more', but won't be
* referenced.
*
* The returned array has 'main', 'extended', and 'more_text' keys. Main has the text before
* the <code><!--more--></code>. The 'extended' key has the content after the
* <code><!--more--></code> comment. The 'more_text' key has the custom "Read More" text.
*
* @since 1.0.0
*
* @param string $post Post content.
* @return array Post before ('main'), after ('extended'), and custom readmore ('more_text').
*/
function get_extended($post) {
//Match the new style more links
if ( preg_match('/<!--more(.*?)?-->/', $post, $matches) ) {
list($main, $extended) = explode($matches[0], $post, 2);
$more_text = $matches[1];
} else {
$main = $post;
$extended = '';
$more_text = '';
}
// Strip leading and trailing whitespace
$main = preg_replace('/^[\s]*(.*)[\s]*$/', '\\1', $main);
$extended = preg_replace('/^[\s]*(.*)[\s]*$/', '\\1', $extended);
$more_text = preg_replace('/^[\s]*(.*)[\s]*$/', '\\1', $more_text);
return array( 'main' => $main, 'extended' => $extended, 'more_text' => $more_text );
}
/**
* Retrieves post data given a post ID or post object.
*
* See {@link sanitize_post()} for optional $filter values. Also, the parameter
* $post, must be given as a variable, since it is passed by reference.
*
* @since 1.5.1
* @uses $wpdb
* @link http://codex.wordpress.org/Function_Reference/get_post
*
* @param int|object $post Post ID or post object.
* @param string $output Optional, default is Object. Either OBJECT, ARRAY_A, or ARRAY_N.
* @param string $filter Optional, default is raw.
* @return mixed Post data
*/
function &get_post(&$post, $output = OBJECT, $filter = 'raw') {
global $wpdb;
$null = null;
if ( empty($post) ) {
if ( isset($GLOBALS['post']) )
$_post = & $GLOBALS['post'];
else
return $null;
} elseif ( is_object($post) && empty($post->filter) ) {
_get_post_ancestors($post);
$_post = sanitize_post($post, 'raw');
wp_cache_add($post->ID, $_post, 'posts');
} elseif ( is_object($post) && 'raw' == $post->filter ) {
$_post = $post;
} else {
if ( is_object($post) )
$post_id = $post->ID;
else
$post_id = $post;
$post_id = (int) $post_id;
if ( ! $_post = wp_cache_get($post_id, 'posts') ) {
$_post = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->posts WHERE ID = %d LIMIT 1", $post_id));
if ( ! $_post )
return $null;
_get_post_ancestors($_post);
$_post = sanitize_post($_post, 'raw');
wp_cache_add($_post->ID, $_post, 'posts');
}
}
if ($filter != 'raw')
$_post = sanitize_post($_post, $filter);
if ( $output == OBJECT ) {
return $_post;
} elseif ( $output == ARRAY_A ) {
$__post = get_object_vars($_post);
return $__post;
} elseif ( $output == ARRAY_N ) {
$__post = array_values(get_object_vars($_post));
return $__post;
} else {
return $_post;
}
}
/**
* Retrieve ancestors of a post.
*
* @since 2.5.0
*
* @param int|object $post Post ID or post object
* @return array Ancestor IDs or empty array if none are found.
*/
function get_post_ancestors($post) {
$post = get_post($post);
if ( ! isset( $post->ancestors ) )
_get_post_ancestors( $post );
if ( ! empty( $post->ancestors ) )
return $post->ancestors;
return array();
}
/**
* Retrieve data from a post field based on Post ID.
*
* Examples of the post field will be, 'post_type', 'post_status', 'post_content',
* etc and based off of the post object property or key names.
*
* The context values are based off of the taxonomy filter functions and
* supported values are found within those functions.
*
* @since 2.3.0
* @uses sanitize_post_field() See for possible $context values.
*
* @param string $field Post field name
* @param id $post Post ID
* @param string $context Optional. How to filter the field. Default is display.
* @return WP_Error|string Value in post field or WP_Error on failure
*/
function get_post_field( $field, $post, $context = 'display' ) {
$post = (int) $post;
$post = get_post( $post );
if ( is_wp_error($post) )
return $post;
if ( !is_object($post) )
return '';
if ( !isset($post->$field) )
return '';
return sanitize_post_field($field, $post->$field, $post->ID, $context);
}
/**
* Retrieve the mime type of an attachment based on the ID.
*
* This function can be used with any post type, but it makes more sense with
* attachments.
*
* @since 2.0.0
*
* @param int $ID Optional. Post ID.
* @return bool|string False on failure or returns the mime type
*/
function get_post_mime_type($ID = '') {
$post = & get_post($ID);
if ( is_object($post) )
return $post->post_mime_type;
return false;
}
/**
* Retrieve the format slug for a post
*
* @since 3.1.0
*
* @param int|object $post A post
*
* @return mixed The format if successful. False if no format is set. WP_Error if errors.
*/
function get_post_format( $post = null ) {
$post = get_post($post);
if ( ! post_type_supports( $post->post_type, 'post-formats' ) )
return false;
$_format = get_the_terms( $post->ID, 'post_format' );
if ( empty( $_format ) )
return false;
$format = array_shift( $_format );
return ( str_replace('post-format-', '', $format->slug ) );
}
/**
* Check if a post has a particular format
*
* @since 3.1.0
* @uses has_term()
*
* @param string $format The format to check for
* @param object|id $post The post to check. If not supplied, defaults to the current post if used in the loop.
* @return bool True if the post has the format, false otherwise.
*/
function has_post_format( $format, $post = null ) {
return has_term('post-format-' . sanitize_key($format), 'post_format', $post);
}
/**
* Assign a format to a post
*
* @since 3.1.0
*
* @param int|object $post The post for which to assign a format
* @param string $format A format to assign. Use an empty string or array to remove all formats from the post.
* @return mixed WP_Error on error. Array of affected term IDs on success.
*/
function set_post_format( $post, $format ) {
$post = get_post($post);
if ( empty($post) )
return new WP_Error('invalid_post', __('Invalid post'));
if ( !empty($format) ) {
$format = sanitize_key($format);
if ( 'standard' == $format || !in_array( $format, array_keys( get_post_format_slugs() ) ) )
$format = '';
else
$format = 'post-format-' . $format;
}
return wp_set_post_terms($post->ID, $format, 'post_format');
}
/**
* Retrieve the post status based on the Post ID.
*
* If the post ID is of an attachment, then the parent post status will be given
* instead.
*
* @since 2.0.0
*
* @param int $ID Post ID
* @return string|bool Post status or false on failure.
*/
function get_post_status($ID = '') {
$post = get_post($ID);
if ( !is_object($post) )
return false;
if ( 'attachment' == $post->post_type ) {
if ( 'private' == $post->post_status )
return 'private';
// Unattached attachments are assumed to be published
if ( ( 'inherit' == $post->post_status ) && ( 0 == $post->post_parent) )
return 'publish';
// Inherit status from the parent
if ( $post->post_parent && ( $post->ID != $post->post_parent ) )
return get_post_status($post->post_parent);
}
return $post->post_status;
}
/**
* Retrieve all of the WordPress supported post statuses.
*
* Posts have a limited set of valid status values, this provides the
* post_status values and descriptions.
*
* @since 2.5.0
*
* @return array List of post statuses.
*/
function get_post_statuses( ) {
$status = array(
'draft' => __('Draft'),
'pending' => __('Pending Review'),
'private' => __('Private'),
'publish' => __('Published')
);
return $status;
}
/**
* Retrieve all of the WordPress support page statuses.
*
* Pages have a limited set of valid status values, this provides the
* post_status values and descriptions.
*
* @since 2.5.0
*
* @return array List of page statuses.
*/
function get_page_statuses( ) {
$status = array(
'draft' => __('Draft'),
'private' => __('Private'),
'publish' => __('Published')
);
return $status;
}
/**
* Register a post type. Do not use before init.
*
* A simple function for creating or modifying a post status based on the
* parameters given. The function will accept an array (second optional
* parameter), along with a string for the post status name.
*
*
* Optional $args contents:
*
* label - A descriptive name for the post status marked for translation. Defaults to $post_status.
* public - Whether posts of this status should be shown in the front end of the site. Defaults to true.
* exclude_from_search - Whether to exclude posts with this post status from search results. Defaults to false.
* show_in_admin_all_list - Whether to include posts in the edit listing for their post type
* show_in_admin_status_list - Show in the list of statuses with post counts at the top of the edit
* listings, e.g. All (12) | Published (9) | My Custom Status (2) ...
*
* Arguments prefixed with an _underscore shouldn't be used by plugins and themes.
*
* @package WordPress
* @subpackage Post
* @since 3.0.0
* @uses $wp_post_statuses Inserts new post status object into the list
*
* @param string $post_status Name of the post status.
* @param array|string $args See above description.
*/
function register_post_status($post_status, $args = array()) {
global $wp_post_statuses;
if (!is_array($wp_post_statuses))
$wp_post_statuses = array();
// Args prefixed with an underscore are reserved for internal use.
$defaults = array('label' => false, 'label_count' => false, 'exclude_from_search' => null, '_builtin' => false, '_edit_link' => 'post.php?post=%d', 'capability_type' => 'post', 'hierarchical' => false, 'public' => null, 'internal' => null, 'protected' => null, 'private' => null, 'show_in_admin_all' => null, 'publicly_queryable' => null, 'show_in_admin_status_list' => null, 'show_in_admin_all_list' => null, 'single_view_cap' => null);
$args = wp_parse_args($args, $defaults);
$args = (object) $args;
$post_status = sanitize_key($post_status);
$args->name = $post_status;
if ( null === $args->public && null === $args->internal && null === $args->protected && null === $args->private )
$args->internal = true;
if ( null === $args->public )
$args->public = false;
if ( null === $args->private )
$args->private = false;
if ( null === $args->protected )
$args->protected = false;
if ( null === $args->internal )
$args->internal = false;
if ( null === $args->publicly_queryable )
$args->publicly_queryable = $args->public;
if ( null === $args->exclude_from_search )
$args->exclude_from_search = $args->internal;
if ( null === $args->show_in_admin_all_list )
$args->show_in_admin_all_list = !$args->internal;
if ( null === $args->show_in_admin_status_list )
$args->show_in_admin_status_list = !$args->internal;
if ( null === $args->single_view_cap )
$args->single_view_cap = $args->public ? '' : 'edit';
if ( false === $args->label )
$args->label = $post_status;
if ( false === $args->label_count )
$args->label_count = array( $args->label, $args->label );
$wp_post_statuses[$post_status] = $args;
return $args;
}
/**
* Retrieve a post status object by name
*
* @package WordPress
* @subpackage Post
* @since 3.0.0
* @uses $wp_post_statuses
* @see register_post_status
* @see get_post_statuses
*
* @param string $post_status The name of a registered post status
* @return object A post status object
*/
function get_post_status_object( $post_status ) {
global $wp_post_statuses;
if ( empty($wp_post_statuses[$post_status]) )
return null;
return $wp_post_statuses[$post_status];
}
/**
* Get a list of all registered post status objects.
*
* @package WordPress
* @subpackage Post
* @since 3.0.0
* @uses $wp_post_statuses
* @see register_post_status
* @see get_post_status_object
*
* @param array|string $args An array of key => value arguments to match against the post status objects.
* @param string $output The type of output to return, either post status 'names' or 'objects'. 'names' is the default.
* @param string $operator The logical operation to perform. 'or' means only one element
* from the array needs to match; 'and' means all elements must match. The default is 'and'.
* @return array A list of post type names or objects
*/
function get_post_stati( $args = array(), $output = 'names', $operator = 'and' ) {
global $wp_post_statuses;
$field = ('names' == $output) ? 'name' : false;
return wp_filter_object_list($wp_post_statuses, $args, $operator, $field);
}
/**
* Whether the post type is hierarchical.
*
* A false return value might also mean that the post type does not exist.
*
* @since 3.0.0
* @see get_post_type_object
*
* @param string $post_type Post type name
* @return bool Whether post type is hierarchical.
*/
function is_post_type_hierarchical( $post_type ) {
if ( ! post_type_exists( $post_type ) )
return false;
$post_type = get_post_type_object( $post_type );
return $post_type->hierarchical;
}
/**
* Checks if a post type is registered.
*
* @since 3.0.0
* @uses get_post_type_object()
*
* @param string $post_type Post type name
* @return bool Whether post type is registered.
*/
function post_type_exists( $post_type ) {
return (bool) get_post_type_object( $post_type );
}
/**
* Retrieve the post type of the current post or of a given post.
*
* @since 2.1.0
*
* @uses $post The Loop current post global
*
* @param mixed $the_post Optional. Post object or post ID.
* @return bool|string post type or false on failure.
*/
function get_post_type( $the_post = false ) {
global $post;
if ( false === $the_post )
$the_post = $post;
elseif ( is_numeric($the_post) )
$the_post = get_post($the_post);
if ( is_object($the_post) )
return $the_post->post_type;
return false;
}
/**
* Retrieve a post type object by name
*
* @package WordPress
* @subpackage Post
* @since 3.0.0
* @uses $wp_post_types
* @see register_post_type
* @see get_post_types
*
* @param string $post_type The name of a registered post type
* @return object A post type object
*/
function get_post_type_object( $post_type ) {
global $wp_post_types;
if ( empty($wp_post_types[$post_type]) )
return null;
return $wp_post_types[$post_type];
}
/**
* Get a list of all registered post type objects.
*
* @package WordPress
* @subpackage Post
* @since 2.9.0
* @uses $wp_post_types
* @see register_post_type
*
* @param array|string $args An array of key => value arguments to match against the post type objects.
* @param string $output The type of output to return, either post type 'names' or 'objects'. 'names' is the default.
* @param string $operator The logical operation to perform. 'or' means only one element
* from the array needs to match; 'and' means all elements must match. The default is 'and'.
* @return array A list of post type names or objects
*/
function get_post_types( $args = array(), $output = 'names', $operator = 'and' ) {
global $wp_post_types;
$field = ('names' == $output) ? 'name' : false;
return wp_filter_object_list($wp_post_types, $args, $operator, $field);
}
/**
* Register a post type. Do not use before init.
*
* A function for creating or modifying a post type based on the
* parameters given. The function will accept an array (second optional
* parameter), along with a string for the post type name.
*
* Optional $args contents:
*
* - label - Name of the post type shown in the menu. Usually plural. If not set, labels['name'] will be used.
* - description - A short descriptive summary of what the post type is. Defaults to blank.
* - public - Whether posts of this type should be shown in the admin UI. Defaults to false.
* - exclude_from_search - Whether to exclude posts with this post type from search results.
* Defaults to true if the type is not public, false if the type is public.
* - publicly_queryable - Whether post_type queries can be performed from the front page.
* Defaults to whatever public is set as.
* - show_ui - Whether to generate a default UI for managing this post type. Defaults to true
* if the type is public, false if the type is not public.
* - show_in_menu - Where to show the post type in the admin menu. True for a top level menu,
* false for no menu, or can be a top level page like 'tools.php' or 'edit.php?post_type=page'.
* show_ui must be true.
* - menu_position - The position in the menu order the post type should appear. Defaults to the bottom.
* - menu_icon - The url to the icon to be used for this menu. Defaults to use the posts icon.
* - capability_type - The string to use to build the read, edit, and delete capabilities. Defaults to 'post'.
* May be passed as an array to allow for alternative plurals when using this argument as a base to construct the
* capabilities, e.g. array('story', 'stories').
* - capabilities - Array of capabilities for this post type. By default the capability_type is used
* as a base to construct capabilities. You can see accepted values in {@link get_post_type_capabilities()}.
* - map_meta_cap - Whether to use the internal default meta capability handling. Defaults to false.
* - hierarchical - Whether the post type is hierarchical. Defaults to false.
* - supports - An alias for calling add_post_type_support() directly. See {@link add_post_type_support()}
* for documentation. Defaults to none.
* - register_meta_box_cb - Provide a callback function that will be called when setting up the
* meta boxes for the edit form. Do remove_meta_box() and add_meta_box() calls in the callback.
* - taxonomies - An array of taxonomy identifiers that will be registered for the post type.
* Default is no taxonomies. Taxonomies can be registered later with register_taxonomy() or
* register_taxonomy_for_object_type().
* - labels - An array of labels for this post type. By default post labels are used for non-hierarchical
* types and page labels for hierarchical ones. You can see accepted values in {@link get_post_type_labels()}.
* - has_archive - True to enable post type archives. Will generate the proper rewrite rules if rewrite is enabled.
* - rewrite - false to prevent rewrite. Defaults to true. Use array('slug'=>$slug) to customize permastruct;
* default will use $post_type as slug. Other options include 'with_front', 'feeds', 'pages', and 'ep_mask'.
* - query_var - false to prevent queries, or string to value of the query var to use for this post type
* - can_export - true allows this post type to be exported.
* - show_in_nav_menus - true makes this post type available for selection in navigation menus.
* - _builtin - true if this post type is a native or "built-in" post_type. THIS IS FOR INTERNAL USE ONLY!
* - _edit_link - URL segement to use for edit link of this post type. THIS IS FOR INTERNAL USE ONLY!
*
* @since 2.9.0
* @uses $wp_post_types Inserts new post type object into the list
*
* @param string $post_type Name of the post type.
* @param array|string $args See above description.
* @return object|WP_Error the registered post type object, or an error object
*/
function register_post_type($post_type, $args = array()) {
global $wp_post_types, $wp_rewrite, $wp;
if ( !is_array($wp_post_types) )
$wp_post_types = array();
// Args prefixed with an underscore are reserved for internal use.
$defaults = array(
'labels' => array(), 'description' => '', 'publicly_queryable' => null, 'exclude_from_search' => null,
'capability_type' => 'post', 'capabilities' => array(), 'map_meta_cap' => null,
'_builtin' => false, '_edit_link' => 'post.php?post=%d', 'hierarchical' => false,
'public' => false, 'rewrite' => true, 'has_archive' => false, 'query_var' => true,
'supports' => array(), 'register_meta_box_cb' => null,
'taxonomies' => array(), 'show_ui' => null, 'menu_position' => null, 'menu_icon' => null,
'can_export' => true,
'show_in_nav_menus' => null, 'show_in_menu' => null, 'show_in_admin_bar' => null,
);
$args = wp_parse_args($args, $defaults);
$args = (object) $args;
$post_type = sanitize_key($post_type);
$args->name = $post_type;
if ( strlen( $post_type ) > 20 )
return new WP_Error( 'post_type_too_long', __( 'Post types cannot exceed 20 characters in length' ) );
// If not set, default to the setting for public.
if ( null === $args->publicly_queryable )
$args->publicly_queryable = $args->public;
// If not set, default to the setting for public.
if ( null === $args->show_ui )
$args->show_ui = $args->public;
// If not set, default to the setting for show_ui.
if ( null === $args->show_in_menu || ! $args->show_ui )
$args->show_in_menu = $args->show_ui;
// If not set, default to the whether the full UI is shown.
if ( null === $args->show_in_admin_bar )
$args->show_in_admin_bar = true === $args->show_in_menu;
// Whether to show this type in nav-menus.php. Defaults to the setting for public.
if ( null === $args->show_in_nav_menus )
$args->show_in_nav_menus = $args->public;
// If not set, default to true if not public, false if public.
if ( null === $args->exclude_from_search )
$args->exclude_from_search = !$args->public;
// Back compat with quirky handling in version 3.0. #14122
if ( empty( $args->capabilities ) && null === $args->map_meta_cap && in_array( $args->capability_type, array( 'post', 'page' ) ) )
$args->map_meta_cap = true;
if ( null === $args->map_meta_cap )
$args->map_meta_cap = false;
$args->cap = get_post_type_capabilities( $args );
unset($args->capabilities);
if ( is_array( $args->capability_type ) )
$args->capability_type = $args->capability_type[0];
if ( ! empty($args->supports) ) {
add_post_type_support($post_type, $args->supports);
unset($args->supports);
} else {
// Add default features
add_post_type_support($post_type, array('title', 'editor'));
}
if ( false !== $args->query_var && !empty($wp) ) {
if ( true === $args->query_var )
$args->query_var = $post_type;
$args->query_var = sanitize_title_with_dashes($args->query_var);
$wp->add_query_var($args->query_var);
}
if ( false !== $args->rewrite && ( is_admin() || '' != get_option('permalink_structure') ) ) {
if ( ! is_array( $args->rewrite ) )
$args->rewrite = array();
if ( empty( $args->rewrite['slug'] ) )
$args->rewrite['slug'] = $post_type;