/
post.php
2984 lines (2568 loc) · 80.7 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
*/
/**
* get_attached_file() - Get metadata for an attached file
*
* {@internal Missing Long Description}}
*
* @package WordPress
* @subpackage Post
* @since 2.0
*
* @param int $attachment_id Attachment ID
* @param bool $unfiltered Whether to apply filters or not
* @return array {@internal Missing Description}}
*/
function get_attached_file( $attachment_id, $unfiltered = false ) {
$file = get_post_meta( $attachment_id, '_wp_attached_file', true );
if ( $unfiltered )
return $file;
return apply_filters( 'get_attached_file', $file, $attachment_id );
}
/**
* update_attached_file() - Update attached file metadata
*
* {@internal Missing Long Description}}
*
* @package WordPress
* @subpackage Post
* @since 2.1
*
* @param int $attachment_id Attachment ID
* @param string $file {@internal Missing Description}}
* @return bool|mixed {@internal Missing Description}}
*/
function update_attached_file( $attachment_id, $file ) {
if ( !get_post( $attachment_id ) )
return false;
$old_file = get_attached_file( $attachment_id, true );
$file = apply_filters( 'update_attached_file', $file, $attachment_id );
if ( $old_file )
return update_post_meta( $attachment_id, '_wp_attached_file', $file, $old_file );
else
return add_post_meta( $attachment_id, '_wp_attached_file', $file );
}
/**
* get_children() - Get post children
*
* {@internal Missing Long Description}}
*
* @package WordPress
* @subpackage Post
* @since 2.0
*
* @param mixed $args {@internal Missing Description}}
* @param string $output {@internal Missing Description}}
* @return mixed {@internal Missing Description}}
*/
function &get_children($args = '', $output = OBJECT) {
if ( empty( $args ) ) {
if ( isset( $GLOBALS['post'] ) ) {
$args = 'post_parent=' . (int) $GLOBALS['post']->post_parent;
} else {
return false;
}
} elseif ( is_object( $args ) ) {
$args = 'post_parent=' . (int) $args->post_parent;
} elseif ( is_numeric( $args ) ) {
$args = 'post_parent=' . (int) $args;
}
$defaults = array(
'numberposts' => -1, 'post_type' => '',
'post_status' => '', 'post_parent' => 0
);
$r = wp_parse_args( $args, $defaults );
$children = get_posts( $r );
if ( !$children )
return false;
update_post_cache($children);
foreach ( $children as $key => $child )
$kids[$child->ID] =& $children[$key];
if ( $output == OBJECT ) {
return $kids;
} elseif ( $output == ARRAY_A ) {
foreach ( $kids as $kid )
$weeuns[$kid->ID] = get_object_vars($kids[$kid->ID]);
return $weeuns;
} elseif ( $output == ARRAY_N ) {
foreach ( $kids as $kid )
$babes[$kid->ID] = array_values(get_object_vars($kids[$kid->ID]));
return $babes;
} else {
return $kids;
}
}
/**
* get_extended() - get extended entry info (<!--more-->)
*
* {@internal Missing Long Description}}
*
* @package WordPress
* @subpackage Post
* @since 1.0.1
*
* @param string $post {@internal Missing Description}}
* @return array {@internal Missing Description}}
*/
function get_extended($post) {
//Match the new style more links
if ( preg_match('/<!--more(.*?)?-->/', $post, $matches) ) {
list($main, $extended) = explode($matches[0], $post, 2);
} else {
$main = $post;
$extended = '';
}
// Strip leading and trailing whitespace
$main = preg_replace('/^[\s]*(.*)[\s]*$/', '\\1', $main);
$extended = preg_replace('/^[\s]*(.*)[\s]*$/', '\\1', $extended);
return array('main' => $main, 'extended' => $extended);
}
/**
* get_post() - Retrieves post data given a post ID or post object.
*
* {@internal Missing Long Description}}
*
* @package WordPress
* @subpackage Post
* @since 1.5.1
* @uses $wpdb
*
* @param int|object &$post post ID or post object
* @param string $output {@internal Missing Description}}
* @param string $filter {@internal Missing Description}}
* @return mixed {@internal Missing Description}}
*/
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) ) {
wp_cache_add($post->ID, $post, 'posts');
$_post = &$post;
} else {
$post = (int) $post;
if ( ! $_post = wp_cache_get($post, 'posts') ) {
$_post = & $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->posts WHERE ID = %d LIMIT 1", $post));
wp_cache_add($_post->ID, $_post, 'posts');
}
}
// Populate the ancestors field.
// Not cached since we don't clear cache for ancestors when a post changes.
_get_post_ancestors($_post);
$_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;
}
}
/**
* get_post_ancestors() - Retrieve ancestors for a post
*
* @package WordPress
* @subpackage Post
* @since 2.5
*
* @param string $field {@internal Missing Description}}
* @param int|object &$post post ID or post object
* @return array of ancestor IDs
*/
function get_post_ancestors($post) {
$post = get_post();
if ( !empty($post->ancestors) )
return $post->ancestors;
return array();
}
/**
* get_post_field() - Retrieve a field based on a post ID.
*
* @package WordPress
* @subpackage Post
* @since 2.3
*
* @param string $field {@internal Missing Description}}
* @param id $post Post ID
* @param string $context Optional. How to filter the field
* @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);
}
/**
* get_post_mime_type() - Takes a post ID, returns its mime type.
*
* @package WordPress
* @subpackage Post
* @since 2.0
*
* @param int $ID 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;
}
/**
* get_post_status() - Takes a post ID and returns its status
*
* {@internal Missing Long Description}}
*
* @package WordPress
* @subpackage Post
* @since 2.0
*
* @param int $ID {@internal Missing Description}}
* @return string|bool post status or false
*/
function get_post_status($ID = '') {
$post = get_post($ID);
if ( is_object($post) ) {
if ( ('attachment' == $post->post_type) && $post->post_parent && ($post->ID != $post->post_parent) )
return get_post_status($post->post_parent);
else
return $post->post_status;
}
return false;
}
/**
* get_post_statuses( ) - Retuns the possible user post status values
*
* Posts have a limited set of valid status values, this provides the
* post_status values and descriptions.
*
* @package WordPress
* @subpackage Post
* @since 2.5
*
* @return array
*/
function get_post_statuses( ) {
$status = array(
'draft' => __('Draft'),
'pending' => __('Pending Review'),
'private' => __('Private'),
'publish' => __('Published')
);
return $status;
}
/**
* get_page_statuses( ) - Retuns the possible user page status values
*
* Pages have a limited set of valid status values, this provides the
* post_status values and descriptions.
*
* @package WordPress
* @subpackage Page
* @since 2.5
*
* @return array
*/
function get_page_statuses( ) {
$status = array(
'draft' => __('Draft'),
'private' => __('Private'),
'publish' => __('Published')
);
return $status;
}
/**
* get_post_type() - Returns post type
*
* {@internal Missing Long Description}}
*
* @package WordPress
* @subpackage Post
* @since 2.1
*
* @uses $wpdb
* @uses $posts {@internal Missing Description}}
*
* @param mixed $post post object or post ID
* @return mixed post type or false
*/
function get_post_type($post = false) {
global $posts;
if ( false === $post )
$post = $posts[0];
elseif ( (int) $post )
$post = get_post($post, OBJECT);
if ( is_object($post) )
return $post->post_type;
return false;
}
/**
* set_post_type() - Set post type
*
* {@internal Missing Long Description}}
*
* @package WordPress
* @subpackage Post
* @since 2.5
*
* @uses $wpdb
* @uses $posts {@internal Missing Description}}
*
* @param mixed $post_id post ID
* @param mixed post type
* @return bool {@internal Missing Description}}
*/
function set_post_type( $post_id = 0, $post_type = 'post' ) {
global $wpdb;
$post_type = sanitize_post_field('post_type', $post_type, $post_id, 'db');
$return = $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET post_type = %s WHERE ID = %d", $post_type, $post_id) );
if ( 'page' == $post_type )
clean_page_cache($post_id);
else
clean_post_cache($post_id);
return $return;
}
/**
* get_posts() - Returns a number of posts
*
* {@internal Missing Long Description}}
*
* @package WordPress
* @subpackage Post
* @since 1.2
* @uses $wpdb
*
* @param array $args {@internal Missing Description}}
* @return array {@internal Missing Description}}
*/
function get_posts($args) {
global $wpdb;
$defaults = array(
'numberposts' => 5, 'offset' => 0,
'category' => 0, 'orderby' => 'post_date',
'order' => 'DESC', 'include' => '',
'exclude' => '', 'meta_key' => '',
'meta_value' =>'', 'post_type' => 'post',
'post_status' => 'publish', 'post_parent' => 0
);
$r = wp_parse_args( $args, $defaults );
extract( $r, EXTR_SKIP );
$numberposts = (int) $numberposts;
$offset = (int) $offset;
$category = (int) $category;
$post_parent = (int) $post_parent;
$inclusions = '';
if ( !empty($include) ) {
$offset = 0; //ignore offset, category, exclude, meta_key, and meta_value, post_parent if using include
$category = 0;
$exclude = '';
$meta_key = '';
$meta_value = '';
$post_parent = 0;
$incposts = preg_split('/[\s,]+/',$include);
$numberposts = count($incposts); // only the number of posts included
if ( count($incposts) ) {
foreach ( $incposts as $incpost ) {
if (empty($inclusions))
$inclusions = $wpdb->prepare(' AND ( ID = %d ', $incpost);
else
$inclusions .= $wpdb->prepare(' OR ID = %d ', $incpost);
}
}
}
if (!empty($inclusions))
$inclusions .= ')';
$exclusions = '';
if ( !empty($exclude) ) {
$exposts = preg_split('/[\s,]+/',$exclude);
if ( count($exposts) ) {
foreach ( $exposts as $expost ) {
if (empty($exclusions))
$exclusions = $wpdb->prepare(' AND ( ID <> %d ', $expost);
else
$exclusions .= $wpdb->prepare(' AND ID <> %d ', $expost);
}
}
}
if (!empty($exclusions))
$exclusions .= ')';
// orderby
if ( preg_match( '/.+ +(ASC|DESC)/i', $orderby ) )
$order = ''; // orderby has its own order, so we'll use that
$query = "SELECT DISTINCT * FROM $wpdb->posts ";
$query .= empty( $category ) ? '' : ", $wpdb->term_relationships, $wpdb->term_taxonomy ";
$query .= empty( $meta_key ) ? '' : ", $wpdb->postmeta ";
$query .= " WHERE 1=1 ";
$query .= empty( $post_type ) ? '' : $wpdb->prepare("AND post_type = %s ", $post_type);
$query .= empty( $post_status ) ? '' : $wpdb->prepare("AND post_status = %s ", $post_status);
$query .= "$exclusions $inclusions " ;
$query .= empty( $category ) ? '' : $wpdb->prepare("AND ($wpdb->posts.ID = $wpdb->term_relationships.object_id AND $wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id AND $wpdb->term_taxonomy.term_id = %d AND $wpdb->term_taxonomy.taxonomy = 'category')", $category);
$query .= empty( $post_parent ) ? '' : $wpdb->prepare("AND $wpdb->posts.post_parent = %d ", $post_parent);
// expected_slashed ($meta_key, $meta_value) -- Also, this looks really funky, doesn't seem like it works
$query .= empty( $meta_key ) | empty($meta_value) ? '' : $wpdb->prepare(" AND ($wpdb->posts.ID = $wpdb->postmeta.post_id AND $wpdb->postmeta.meta_key = %s AND $wpdb->postmeta.meta_value = %s )", $meta_key, $meta_value);
$query .= empty( $post_mime_type ) ? '' : wp_post_mime_type_where($post_mime_type);
$query .= " GROUP BY $wpdb->posts.ID ORDER BY " . $orderby . ' ' . $order;
if ( 0 < $numberposts )
$query .= $wpdb->prepare(" LIMIT %d,%d", $offset, $numberposts);
$posts = $wpdb->get_results($query);
update_post_caches($posts);
return $posts;
}
//
// Post meta functions
//
/**
* add_post_meta() - adds metadata for post
*
* {@internal Missing Long Description}}
*
* @package WordPress
* @subpackage Post
* @since 1.5
* @uses $wpdb
*
* @param int $post_id post ID
* @param string $key {@internal Missing Description}}
* @param mixed $value {@internal Missing Description}}
* @param bool $unique whether to check for a value with the same key
* @return bool {@internal Missing Description}}
*/
function add_post_meta($post_id, $meta_key, $meta_value, $unique = false) {
global $wpdb;
// expected_slashed ($meta_key)
$meta_key = stripslashes($meta_key);
if ( $unique && $wpdb->get_var( $wpdb->prepare( "SELECT meta_key FROM $wpdb->postmeta WHERE meta_key = %s AND post_id = %d", $meta_key, $post_id ) ) )
return false;
$meta_value = maybe_serialize($meta_value);
$wpdb->insert( $wpdb->postmeta, compact( 'post_id', 'meta_key', 'meta_value' ) );
wp_cache_delete($post_id, 'post_meta');
return true;
}
/**
* delete_post_meta() - delete post metadata
*
* {@internal Missing Long Description}}
*
* @package WordPress
* @subpackage Post
* @since 1.5
* @uses $wpdb
*
* @param int $post_id post ID
* @param string $key {@internal Missing Description}}
* @param mixed $value {@internal Missing Description}}
* @return bool {@internal Missing Description}}
*/
function delete_post_meta($post_id, $key, $value = '') {
global $wpdb;
$post_id = absint( $post_id );
// expected_slashed ($key, $value)
$key = stripslashes( $key );
$value = stripslashes( $value );
if ( empty( $value ) )
$meta_id = $wpdb->get_var( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = %s", $post_id, $key ) );
else
$meta_id = $wpdb->get_var( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = %s AND meta_value = %s", $post_id, $key, $value ) );
if ( !$meta_id )
return false;
if ( empty( $value ) )
$wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = %s", $post_id, $key ) );
else
$wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = %s AND meta_value = %s", $post_id, $key, $value ) );
wp_cache_delete($post_id, 'post_meta');
return true;
}
/**
* get_post_meta() - Get a post meta field
*
* {@internal Missing Long Description}}
*
* @package WordPress
* @subpackage Post
* @since 1.5
* @uses $wpdb
*
* @param int $post_id post ID
* @param string $key The meta key to retrieve
* @param bool $single Whether to return a single value
* @return mixed {@internal Missing Description}}
*/
function get_post_meta($post_id, $key, $single = false) {
$post_id = (int) $post_id;
$meta_cache = wp_cache_get($post_id, 'post_meta');
if ( isset($meta_cache[$key]) ) {
if ( $single ) {
return maybe_unserialize( $meta_cache[$key][0] );
} else {
return maybe_unserialize( $meta_cache[$key] );
}
}
if ( !$meta_cache ) {
update_postmeta_cache($post_id);
$meta_cache = wp_cache_get($post_id, 'post_meta');
}
if ( $single ) {
if ( isset($meta_cache[$key][0]) )
return maybe_unserialize($meta_cache[$key][0]);
else
return '';
} else {
return maybe_unserialize($meta_cache[$key]);
}
}
/**
* update_post_meta() - Update a post meta field
*
* {@internal Missing Long Description}}
*
* @package WordPress
* @subpackage Post
* @since 1.5
* @uses $wpdb
*
* @param int $post_id post ID
* @param string $key {@internal Missing Description}}
* @param mixed $value {@internal Missing Description}}
* @param mixed $prev_value previous value (for differentiating between meta fields with the same key and post ID)
* @return bool {@internal Missing Description}}
*/
function update_post_meta($post_id, $meta_key, $meta_value, $prev_value = '') {
global $wpdb;
$meta_value = maybe_serialize($meta_value);
$prev_value = maybe_serialize($prev_value);
// expected_slashed ($meta_key)
$meta_key = stripslashes($meta_key);
if ( ! $wpdb->get_var( $wpdb->prepare( "SELECT meta_key FROM $wpdb->postmeta WHERE meta_key = %s AND post_id = %d", $meta_key, $post_id ) ) )
return false;
$data = compact( 'meta_value' );
$where = compact( 'meta_key', 'post_id' );
if ( !empty( $prev_value ) )
$where['meta_value'] = $prev_value;
$wpdb->update( $wpdb->postmeta, $data, $where );
wp_cache_delete($post_id, 'post_meta');
return true;
}
/**
* delete_post_meta_by_key() - Delete everything from post meta matching $post_meta_key
*
* @package WordPress
* @subpackage Post
* @since 2.3
* @uses $wpdb
*
* @param string $post_meta_key What to search for when deleting
* @return bool Whether the post meta key was deleted from the database
*/
function delete_post_meta_by_key($post_meta_key) {
global $wpdb;
if ( $wpdb->query($wpdb->prepare("DELETE FROM $wpdb->postmeta WHERE meta_key = %s", $post_meta_key)) ) {
/** @todo Get post_ids and delete cache */
// wp_cache_delete($post_id, 'post_meta');
return true;
}
return false;
}
/**
* get_post_custom() - Retrieve post custom fields
*
* {@internal Missing Long Description}}
*
* @package WordPress
* @subpackage Post
* @since 1.2
*
* @uses $id
* @uses $wpdb
*
* @param int $post_id post ID
* @return array {@internal Missing Description}}
*/
function get_post_custom($post_id = 0) {
global $id;
if ( !$post_id )
$post_id = (int) $id;
$post_id = (int) $post_id;
if ( ! wp_cache_get($post_id, 'post_meta') )
update_postmeta_cache($post_id);
return wp_cache_get($post_id, 'post_meta');
}
/**
* get_post_custom_keys() - Retrieve post custom field names
*
* @package WordPress
* @subpackage Post
* @since 1.2
*
* @param int $post_id post ID
* @return array|null Either array of the keys, or null if keys would not be retrieved
*/
function get_post_custom_keys( $post_id = 0 ) {
$custom = get_post_custom( $post_id );
if ( !is_array($custom) )
return;
if ( $keys = array_keys($custom) )
return $keys;
}
function get_post_custom_values( $key = '', $post_id = 0 ) {
$custom = get_post_custom($post_id);
return $custom[$key];
}
function sanitize_post($post, $context = 'display') {
if ( 'raw' == $context )
return $post;
if ( is_object($post) ) {
foreach ( array_keys(get_object_vars($post)) as $field )
$post->$field = sanitize_post_field($field, $post->$field, $post->ID, $context);
} else {
foreach ( array_keys($post) as $field )
$post[$field] = sanitize_post_field($field, $post[$field], $post['ID'], $context);
}
return $post;
}
/**
* sanitize_post_field() - Sanitize post field based on context
*
* {@internal Missing Long Description}}
*
* @package WordPress
* @subpackage Post
* @since 2.3
*
* @param string $field The Post Object field name
* @param string $value The Post Object value
* @param int $postid Post ID
* @param string $context How to sanitize post fields
* @return string Sanitized value
*/
function sanitize_post_field($field, $value, $post_id, $context) {
$int_fields = array('ID', 'post_parent', 'menu_order');
if ( in_array($field, $int_fields) )
$value = (int) $value;
if ( 'raw' == $context )
return $value;
$prefixed = false;
if ( false !== strpos($field, 'post_') ) {
$prefixed = true;
$field_no_prefix = str_replace('post_', '', $field);
}
if ( 'edit' == $context ) {
$format_to_edit = array('post_content', 'post_excerpt', 'post_title', 'post_password');
if ( $prefixed ) {
$value = apply_filters("edit_$field", $value, $post_id);
// Old school
$value = apply_filters("${field_no_prefix}_edit_pre", $value, $post_id);
} else {
$value = apply_filters("edit_post_$field", $value, $post_id);
}
if ( in_array($field, $format_to_edit) ) {
if ( 'post_content' == $field )
$value = format_to_edit($value, user_can_richedit());
else
$value = format_to_edit($value);
} else {
$value = attribute_escape($value);
}
} else if ( 'db' == $context ) {
if ( $prefixed ) {
$value = apply_filters("pre_$field", $value);
$value = apply_filters("${field_no_prefix}_save_pre", $value);
} else {
$value = apply_filters("pre_post_$field", $value);
$value = apply_filters("${field}_pre", $value);
}
} else {
// Use display filters by default.
if ( $prefixed )
$value = apply_filters($field, $value, $post_id, $context);
else
$value = apply_filters("post_$field", $value, $post_id, $context);
}
if ( 'attribute' == $context )
$value = attribute_escape($value);
else if ( 'js' == $context )
$value = js_escape($value);
return $value;
}
/**
* wp_count_posts() - Count number of posts with a given type
*
* {@internal Missing Long Description}}
*
* @package WordPress
* @subpackage Post
* @since 2.5
*
* @param string $type Post type
* @return array Number of posts for each status
*/
function wp_count_posts( $type = 'post', $perm = '' ) {
global $wpdb;
$user = wp_get_current_user();
$cache_key = $type;
if ( !empty($perm) )
$cache_key .= '_' . $perm;
$query = "SELECT post_status, COUNT( * ) AS num_posts FROM {$wpdb->posts} WHERE post_type = %s";
if ( 'readable' == $perm && is_user_logged_in() ) {
if ( !current_user_can("read_private_{$type}s") ) {
$cache_key .= '_' . $user->ID;
$query .= " AND (post_status != 'private' OR ( post_author = '$user->ID' AND post_status = 'private' ))";
}
}
$query .= ' GROUP BY post_status';
$count = wp_cache_get($cache_key, 'counts');
if ( false !== $count )
return $count;
$count = $wpdb->get_results( $wpdb->prepare( $query, $type ), ARRAY_A );
$stats = array( );
foreach( (array) $count as $row_num => $row ) {
$stats[$row['post_status']] = $row['num_posts'];
}
$stats = (object) $stats;
wp_cache_set($cache_key, $stats, 'counts');
return $stats;
}
/**
* wp_count_attachments() - Count number of attachments
*
* {@internal Missing Long Description}}
*
* @package WordPress
* @subpackage Post
* @since 2.5
*
* @param string|array $post_mime_type Array or comma-separated list of MIME patterns
* @return array Number of posts for each post_mime_type
*/
function wp_count_attachments( $mime_type = '' ) {
global $wpdb;
$and = wp_post_mime_type_where( $mime_type );
$count = $wpdb->get_results( "SELECT post_mime_type, COUNT( * ) AS num_posts FROM $wpdb->posts WHERE post_type = 'attachment' $and GROUP BY post_mime_type", ARRAY_A );
$stats = array( );
foreach( (array) $count as $row ) {
$stats[$row['post_mime_type']] = $row['num_posts'];
}
return (object) $stats;
}
/**
* wp_match_mime_type() - Check a MIME-Type against a list
*
* {@internal Missing Long Description}}
*
* @package WordPress
* @subpackage Post
* @since 2.5
*
* @param string|array $wildcard_mime_types e.g. audio/mpeg or image (same as image/*) or flash (same as *flash*)
* @param string|array $real_mime_types post_mime_type values
* @return array array(wildcard=>array(real types))
*/
function wp_match_mime_types($wildcard_mime_types, $real_mime_types) {
$matches = array();
if ( is_string($wildcard_mime_types) )
$wildcard_mime_types = array_map('trim', explode(',', $wildcard_mime_types));
if ( is_string($real_mime_types) )
$real_mime_types = array_map('trim', explode(',', $real_mime_types));
$wild = '[-._a-z0-9]*';
foreach ( (array) $wildcard_mime_types as $type ) {
$type = str_replace('*', $wild, $type);
$patternses[1][$type] = "^$type$";
if ( false === strpos($type, '/') ) {
$patternses[2][$type] = "^$type/";
$patternses[3][$type] = $type;
}
}
asort($patternses);
foreach ( $patternses as $patterns )
foreach ( $patterns as $type => $pattern )
foreach ( (array) $real_mime_types as $real )
if ( preg_match("#$pattern#", $real) && ( empty($matches[$type]) || false === array_search($real, $matches[$type]) ) )
$matches[$type][] = $real;
return $matches;
}
/**
* wp_get_post_mime_type_where() - Convert MIME types into SQL
*
* @package WordPress
* @subpackage Post
* @since 2.5
*
* @param string|array $mime_types MIME types
* @return string SQL AND clause
*/
function wp_post_mime_type_where($post_mime_types) {
$where = '';
$wildcards = array('', '%', '%/%');
if ( is_string($post_mime_types) )
$post_mime_types = array_map('trim', explode(',', $post_mime_types));
foreach ( (array) $post_mime_types as $mime_type ) {
$mime_type = preg_replace('/\s/', '', $mime_type);
$slashpos = strpos($mime_type, '/');
if ( false !== $slashpos ) {
$mime_group = preg_replace('/[^-*.a-zA-Z0-9]/', '', substr($mime_type, 0, $slashpos));
$mime_subgroup = preg_replace('/[^-*.a-zA-Z0-9]/', '', substr($mime_type, $slashpos + 1));
if ( empty($mime_subgroup) )
$mime_subgroup = '*';
else
$mime_subgroup = str_replace('/', '', $mime_subgroup);
$mime_pattern = "$mime_group/$mime_subgroup";
} else {
$mime_pattern = preg_replace('/[^-*.a-zA-Z0-9]/', '', $mime_type);
if ( false === strpos($mime_pattern, '*') )
$mime_pattern .= '/*';
}
$mime_pattern = preg_replace('/\*+/', '%', $mime_pattern);
if ( in_array( $mime_type, $wildcards ) )
return '';
if ( false !== strpos($mime_pattern, '%') )
$wheres[] = "post_mime_type LIKE '$mime_pattern'";
else
$wheres[] = "post_mime_type = '$mime_pattern'";
}
if ( !empty($wheres) )
$where = ' AND (' . join(' OR ', $wheres) . ') ';
return $where;
}
/**
* wp_delete_post() - Deletes a Post
*
* {@internal Missing Long Description}}
*
* @package WordPress
* @subpackage Post
* @since 1.0.1
*
* @param int $postid post ID
* @return mixed {@internal Missing Description}}
*/
function wp_delete_post($postid = 0) {
global $wpdb, $wp_rewrite;
if ( !$post = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->posts WHERE ID = %d", $postid)) )
return $post;
if ( 'attachment' == $post->post_type )
return wp_delete_attachment($postid);
do_action('delete_post', $postid);
/** @todo delete for pluggable post taxonomies too */
wp_delete_object_term_relationships($postid, array('category', 'post_tag'));
$parent_data = array( 'post_parent' => $post->post_parent );
$parent_where = array( 'post_parent' => $postid );