~canonical-sysadmins/wordpress/3.9.x

« back to all changes in this revision

Viewing changes to wp-includes/post.php

  • Committer: Andrew Glen-Young
  • Date: 2011-03-08 14:47:51 UTC
  • Revision ID: andrew.glen-young@canonical.com-20110308144751-1n6spqgayztf9h77
[AGY] import 3.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
 
14
14
/**
15
15
 * Creates the initial post types when 'init' action is fired.
 
16
 *
 
17
 * @since 2.9.0
16
18
 */
17
19
function create_initial_post_types() {
18
20
        register_post_type( 'post', array(
20
22
                '_builtin' => true, /* internal use only. don't use this when registering your own post type. */
21
23
                '_edit_link' => 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */
22
24
                'capability_type' => 'post',
 
25
                'map_meta_cap' => true,
23
26
                'hierarchical' => false,
24
27
                'rewrite' => false,
25
28
                'query_var' => false,
26
 
                'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions' ),
 
29
                'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'post-formats' ),
27
30
        ) );
28
31
 
29
32
        register_post_type( 'page', array(
31
34
                '_builtin' => true, /* internal use only. don't use this when registering your own post type. */
32
35
                '_edit_link' => 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */
33
36
                'capability_type' => 'page',
 
37
                'map_meta_cap' => true,
34
38
                'hierarchical' => true,
35
39
                'rewrite' => false,
36
40
                'query_var' => false,
46
50
                '_builtin' => true, /* internal use only. don't use this when registering your own post type. */
47
51
                '_edit_link' => 'media.php?attachment_id=%d', /* internal use only. don't use this when registering your own post type. */
48
52
                'capability_type' => 'post',
 
53
                'map_meta_cap' => true,
49
54
                'hierarchical' => false,
50
55
                'rewrite' => false,
51
56
                'query_var' => false,
52
 
                'can_export' => false,
53
57
                'show_in_nav_menus' => false,
54
58
        ) );
55
59
 
62
66
                '_builtin' => true, /* internal use only. don't use this when registering your own post type. */
63
67
                '_edit_link' => 'revision.php?revision=%d', /* internal use only. don't use this when registering your own post type. */
64
68
                'capability_type' => 'post',
 
69
                'map_meta_cap' => true,
65
70
                'hierarchical' => false,
66
71
                'rewrite' => false,
67
72
                'query_var' => false,
 
73
                'can_export' => false,
68
74
        ) );
69
75
 
70
76
        register_post_type( 'nav_menu_item', array(
74
80
                ),
75
81
                'public' => false,
76
82
                '_builtin' => true, /* internal use only. don't use this when registering your own post type. */
77
 
                'capability_type' => 'post',
78
83
                'hierarchical' => false,
79
84
                'rewrite' => false,
80
85
                'query_var' => false,
261
266
 * @param string $output Optional. Constant for return type, either OBJECT (default), ARRAY_A, ARRAY_N.
262
267
 * @return array|bool False on failure and the type will be determined by $output parameter.
263
268
 */
264
 
function &get_children($args = '', $output = OBJECT) {
 
269
function get_children($args = '', $output = OBJECT) {
265
270
        $kids = array();
266
271
        if ( empty( $args ) ) {
267
272
                if ( isset( $GLOBALS['post'] ) ) {
290
295
        update_post_cache($children);
291
296
 
292
297
        foreach ( $children as $key => $child )
293
 
                $kids[$child->ID] =& $children[$key];
 
298
                $kids[$child->ID] = $children[$key];
294
299
 
295
300
        if ( $output == OBJECT ) {
296
301
                return $kids;
471
476
}
472
477
 
473
478
/**
 
479
 * Retrieve the format slug for a post
 
480
 *
 
481
 * @since 3.1.0
 
482
 *
 
483
 * @param int|object $post A post
 
484
 *
 
485
 * @return mixed The format if successful. False if no format is set.  WP_Error if errors.
 
486
 */
 
487
function get_post_format( $post = null ) {
 
488
        $post = get_post($post);
 
489
 
 
490
        if ( ! post_type_supports( $post->post_type, 'post-formats' ) )
 
491
                return false;
 
492
 
 
493
        $_format = get_the_terms( $post->ID, 'post_format' );
 
494
 
 
495
        if ( empty( $_format ) )
 
496
                return false;
 
497
 
 
498
        $format = array_shift( $_format );
 
499
 
 
500
        return ( str_replace('post-format-', '', $format->slug ) );
 
501
}
 
502
 
 
503
/**
 
504
 * Check if a post has a particular format
 
505
 *
 
506
 * @since 3.1.0
 
507
 * @uses has_term()
 
508
 *
 
509
 * @param string $format The format to check for
 
510
 * @param object|id $post The post to check. If not supplied, defaults to the current post if used in the loop.
 
511
 * @return bool True if the post has the format, false otherwise.
 
512
 */
 
513
function has_post_format( $format, $post = null ) {
 
514
        return has_term('post-format-' . sanitize_key($format), 'post_format', $post);
 
515
}
 
516
 
 
517
/**
 
518
 * Assign a format to a post
 
519
 *
 
520
 * @since 3.1.0
 
521
 *
 
522
 * @param int|object $post The post for which to assign a format
 
523
 * @param string $format  A format to assign.  Use an empty string or array to remove all formats from the post.
 
524
 * @return mixed WP_Error on error. Array of affected term IDs on success.
 
525
 */
 
526
function set_post_format( $post, $format ) {
 
527
        $post = get_post($post);
 
528
 
 
529
        if ( empty($post) )
 
530
                return new WP_Error('invalid_post', __('Invalid post'));
 
531
 
 
532
        if ( !empty($format) ) {
 
533
                $format = sanitize_key($format);
 
534
                if ( 'standard' == $format || !in_array( $format, array_keys( get_post_format_slugs() ) ) )
 
535
                        $format = '';
 
536
                else
 
537
                        $format = 'post-format-' . $format;
 
538
        }
 
539
 
 
540
        return wp_set_post_terms($post->ID, $format, 'post_format');
 
541
}
 
542
 
 
543
/**
474
544
 * Retrieve the post status based on the Post ID.
475
545
 *
476
546
 * If the post ID is of an attachment, then the parent post status will be given
549
619
 * Optional $args contents:
550
620
 *
551
621
 * label - A descriptive name for the post status marked for translation. Defaults to $post_status.
552
 
 * public - Whether posts of this status should be shown in the admin UI. Defaults to true.
 
622
 * public - Whether posts of this status should be shown in the front end of the site. Defaults to true.
553
623
 * exclude_from_search - Whether to exclude posts with this post status from search results. Defaults to true.
 
624
 * show_in_admin_all_list - Whether to include posts in the edit listing for their post type
 
625
 * show_in_admin_status_list - Show in the list of statuses with post counts at the top of the edit
 
626
 *                             listings, e.g. All (12) | Published (9) | My Custom Status (2) ...
 
627
 *
 
628
 * Arguments prefixed with an _underscore shouldn't be used by plugins and themes.
554
629
 *
555
630
 * @package WordPress
556
631
 * @subpackage Post
571
646
        $args = wp_parse_args($args, $defaults);
572
647
        $args = (object) $args;
573
648
 
574
 
        $post_status = sanitize_user($post_status, true);
 
649
        $post_status = sanitize_key($post_status);
575
650
        $args->name = $post_status;
576
651
 
577
652
        if ( null === $args->public && null === $args->internal && null === $args->protected && null === $args->private )
625
700
 * @see register_post_status
626
701
 * @see get_post_statuses
627
702
 *
628
 
 * @param string $post_type The name of a registered post status
 
703
 * @param string $post_status The name of a registered post status
629
704
 * @return object A post status object
630
705
 */
631
706
function get_post_status_object( $post_status ) {
669
744
 * @since 3.0.0
670
745
 * @see get_post_type_object
671
746
 *
672
 
 * @param string $post Post type name
 
747
 * @param string $post_type Post type name
673
748
 * @return bool Whether post type is hierarchical.
674
749
 */
675
750
function is_post_type_hierarchical( $post_type ) {
686
761
 * @since 3.0.0
687
762
 * @uses get_post_type_object()
688
763
 *
689
 
 * @param string Post type name
 
764
 * @param string $post_type Post type name
690
765
 * @return bool Whether post type is registered.
691
766
 */
692
767
function post_type_exists( $post_type ) {
765
840
/**
766
841
 * Register a post type. Do not use before init.
767
842
 *
768
 
 * A simple function for creating or modifying a post type based on the
 
843
 * A function for creating or modifying a post type based on the
769
844
 * parameters given. The function will accept an array (second optional
770
845
 * parameter), along with a string for the post type name.
771
846
 *
772
 
 *
773
847
 * Optional $args contents:
774
848
 *
775
849
 * - label - Name of the post type shown in the menu. Usually plural. If not set, labels['name'] will be used.
776
850
 * - description - A short descriptive summary of what the post type is. Defaults to blank.
777
851
 * - public - Whether posts of this type should be shown in the admin UI. Defaults to false.
778
 
 * - 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.
779
 
 * - publicly_queryable - Whether post_type queries can be performed from the front page.  Defaults to whatever public is set as.
780
 
 * - 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.
 
852
 * - exclude_from_search - Whether to exclude posts with this post type from search results.
 
853
 *     Defaults to true if the type is not public, false if the type is public.
 
854
 * - publicly_queryable - Whether post_type queries can be performed from the front page.
 
855
 *     Defaults to whatever public is set as.
 
856
 * - show_ui - Whether to generate a default UI for managing this post type. Defaults to true
 
857
 *     if the type is public, false if the type is not public.
 
858
 * - show_in_menu - Where to show the post type in the admin menu. True for a top level menu,
 
859
 *     false for no menu, or can be a top level page like 'tools.php' or 'edit.php?post_type=page'.
 
860
 *     show_ui must be true.
781
861
 * - menu_position - The position in the menu order the post type should appear. Defaults to the bottom.
782
862
 * - menu_icon - The url to the icon to be used for this menu. Defaults to use the posts icon.
783
 
 * - capability_type - The post type to use for checking read, edit, and delete capabilities. Defaults to "post".
784
 
 * - capabilities - Array of capabilities for this post type. You can see accepted values in {@link get_post_type_capabilities()}. By default the capability_type is used to construct capabilities.
 
863
 * - capability_type - The string to use to build the read, edit, and delete capabilities. Defaults to 'post'.
 
864
 *   May be passed as an array to allow for alternative plurals when using this argument as a base to construct the
 
865
 *   capabilities, e.g. array('story', 'stories').
 
866
 * - capabilities - Array of capabilities for this post type. By default the capability_type is used
 
867
 *      as a base to construct capabilities. You can see accepted values in {@link get_post_type_capabilities()}.
 
868
 * - map_meta_cap - Whether to use the internal default meta capability handling. Defaults to false.
785
869
 * - hierarchical - Whether the post type is hierarchical. Defaults to false.
786
 
 * - supports - An alias for calling add_post_type_support() directly. See add_post_type_support() for Documentation. Defaults to none.
787
 
 * - 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.
788
 
 * - 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().
789
 
 * - labels - An array of labels for this post type. You can see accepted values in {@link get_post_type_labels()}. By default post labels are used for non-hierarchical types and page labels for hierarchical ones.
 
870
 * - supports - An alias for calling add_post_type_support() directly. See {@link add_post_type_support()}
 
871
 *     for documentation. Defaults to none.
 
872
 * - register_meta_box_cb - Provide a callback function that will be called when setting up the
 
873
 *     meta boxes for the edit form.  Do remove_meta_box() and add_meta_box() calls in the callback.
 
874
 * - taxonomies - An array of taxonomy identifiers that will be registered for the post type.
 
875
 *     Default is no taxonomies. Taxonomies can be registered later with register_taxonomy() or
 
876
 *     register_taxonomy_for_object_type().
 
877
 * - labels - An array of labels for this post type. By default post labels are used for non-hierarchical
 
878
 *     types and page labels for hierarchical ones. You can see accepted values in {@link get_post_type_labels()}.
790
879
 * - permalink_epmask - The default rewrite endpoint bitmasks.
791
 
 * - rewrite - false to prevent rewrite, or array('slug'=>$slug) to customize permastruct; default will use $post_type as slug.
 
880
 * - has_archive - True to enable post type archives. Will generate the proper rewrite rules if rewrite is enabled.
 
881
 * - rewrite - false to prevent rewrite. Defaults to true. Use array('slug'=>$slug) to customize permastruct;
 
882
 *     default will use $post_type as slug. Other options include 'with_front', 'feeds', and 'pages'.
792
883
 * - query_var - false to prevent queries, or string to value of the query var to use for this post type
793
884
 * - can_export - true allows this post type to be exported.
794
885
 * - show_in_nav_menus - true makes this post type available for selection in navigation menus.
795
 
 * - _builtin - true if this post type is a native or "built-in" post_type.  THIS IS FOR INTERNAL USE ONLY!
796
 
 * - _edit_link - URL segement to use for edit link of this post type.  Set to 'post.php?post=%d'.  THIS IS FOR INTERNAL USE ONLY!
 
886
 * - _builtin - true if this post type is a native or "built-in" post_type. THIS IS FOR INTERNAL USE ONLY!
 
887
 * - _edit_link - URL segement to use for edit link of this post type. THIS IS FOR INTERNAL USE ONLY!
797
888
 *
798
889
 * @since 2.9.0
799
890
 * @uses $wp_post_types Inserts new post type object into the list
800
891
 *
801
892
 * @param string $post_type Name of the post type.
802
893
 * @param array|string $args See above description.
803
 
 * @return object the registered post type object
 
894
 * @return object|WP_Error the registered post type object, or an error object
804
895
 */
805
896
function register_post_type($post_type, $args = array()) {
806
897
        global $wp_post_types, $wp_rewrite, $wp;
811
902
        // Args prefixed with an underscore are reserved for internal use.
812
903
        $defaults = array(
813
904
                'labels' => array(), 'description' => '', 'publicly_queryable' => null, 'exclude_from_search' => null,
814
 
                '_builtin' => false, '_edit_link' => 'post.php?post=%d', 'capability_type' => 'post', 'capabilities' => array(), 'hierarchical' => false,
815
 
                'public' => false, 'rewrite' => true, 'query_var' => true, 'supports' => array(), 'register_meta_box_cb' => null,
 
905
                'capability_type' => 'post', 'capabilities' => array(), 'map_meta_cap' => null,
 
906
                '_builtin' => false, '_edit_link' => 'post.php?post=%d', 'hierarchical' => false,
 
907
                'public' => false, 'rewrite' => true, 'has_archive' => false, 'query_var' => true,
 
908
                'supports' => array(), 'register_meta_box_cb' => null,
816
909
                'taxonomies' => array(), 'show_ui' => null, 'menu_position' => null, 'menu_icon' => null,
817
 
                'permalink_epmask' => EP_PERMALINK, 'can_export' => true, 'show_in_nav_menus' => null
 
910
                'permalink_epmask' => EP_PERMALINK, 'can_export' => true, 'show_in_nav_menus' => null, 'show_in_menu' => null,
818
911
        );
819
912
        $args = wp_parse_args($args, $defaults);
820
913
        $args = (object) $args;
821
914
 
822
 
        $post_type = sanitize_user($post_type, true);
 
915
        $post_type = sanitize_key($post_type);
823
916
        $args->name = $post_type;
824
917
 
 
918
        if ( strlen( $post_type ) > 20 )
 
919
                        return new WP_Error( 'post_type_too_long', __( 'Post types cannot exceed 20 characters in length' ) );
 
920
 
825
921
        // If not set, default to the setting for public.
826
922
        if ( null === $args->publicly_queryable )
827
923
                $args->publicly_queryable = $args->public;
830
926
        if ( null === $args->show_ui )
831
927
                $args->show_ui = $args->public;
832
928
 
 
929
        // If not set, default to the setting for show_ui.
 
930
        if ( null === $args->show_in_menu || ! $args->show_ui )
 
931
                $args->show_in_menu = $args->show_ui;
 
932
 
833
933
        // Whether to show this type in nav-menus.php.  Defaults to the setting for public.
834
934
        if ( null === $args->show_in_nav_menus )
835
935
                $args->show_in_nav_menus = $args->public;
838
938
        if ( null === $args->exclude_from_search )
839
939
                $args->exclude_from_search = !$args->public;
840
940
 
841
 
        if ( empty($args->capability_type) )
842
 
                $args->capability_type = 'post';
 
941
        // Back compat with quirky handling in version 3.0. #14122
 
942
        if ( empty( $args->capabilities ) && null === $args->map_meta_cap && in_array( $args->capability_type, array( 'post', 'page' ) ) )
 
943
                $args->map_meta_cap = true;
 
944
 
 
945
        if ( null === $args->map_meta_cap )
 
946
                $args->map_meta_cap = false;
843
947
 
844
948
        $args->cap = get_post_type_capabilities( $args );
845
949
        unset($args->capabilities);
846
950
 
 
951
        if ( is_array( $args->capability_type ) )
 
952
                $args->capability_type = $args->capability_type[0];
 
953
 
847
954
        if ( ! empty($args->supports) ) {
848
955
                add_post_type_support($post_type, $args->supports);
849
956
                unset($args->supports);
860
967
        }
861
968
 
862
969
        if ( false !== $args->rewrite && '' != get_option('permalink_structure') ) {
863
 
                if ( !is_array($args->rewrite) )
 
970
                if ( ! is_array( $args->rewrite ) )
864
971
                        $args->rewrite = array();
865
 
                if ( !isset($args->rewrite['slug']) )
 
972
                if ( empty( $args->rewrite['slug'] ) )
866
973
                        $args->rewrite['slug'] = $post_type;
867
 
                if ( !isset($args->rewrite['with_front']) )
 
974
                if ( ! isset( $args->rewrite['with_front'] ) )
868
975
                        $args->rewrite['with_front'] = true;
 
976
                if ( ! isset( $args->rewrite['pages'] ) )
 
977
                        $args->rewrite['pages'] = true;
 
978
                if ( ! isset( $args->rewrite['feeds'] ) || ! $args->has_archive )
 
979
                        $args->rewrite['feeds'] = (bool) $args->has_archive;
 
980
 
869
981
                if ( $args->hierarchical )
870
982
                        $wp_rewrite->add_rewrite_tag("%$post_type%", '(.+?)', $args->query_var ? "{$args->query_var}=" : "post_type=$post_type&name=");
871
983
                else
872
984
                        $wp_rewrite->add_rewrite_tag("%$post_type%", '([^/]+)', $args->query_var ? "{$args->query_var}=" : "post_type=$post_type&name=");
 
985
 
 
986
                if ( $args->has_archive ) {
 
987
                        $archive_slug = $args->has_archive === true ? $args->rewrite['slug'] : $args->has_archive;
 
988
                        $wp_rewrite->add_rule( "{$archive_slug}/?$", "index.php?post_type=$post_type", 'top' );
 
989
                        if ( $args->rewrite['feeds'] && $wp_rewrite->feeds ) {
 
990
                                $feeds = '(' . trim( implode( '|', $wp_rewrite->feeds ) ) . ')';
 
991
                                $wp_rewrite->add_rule( "{$archive_slug}/feed/$feeds/?$", "index.php?post_type=$post_type" . '&feed=$matches[1]', 'top' );
 
992
                                $wp_rewrite->add_rule( "{$archive_slug}/$feeds/?$", "index.php?post_type=$post_type" . '&feed=$matches[1]', 'top' );
 
993
                        }
 
994
                        if ( $args->rewrite['pages'] )
 
995
                                $wp_rewrite->add_rule( "{$archive_slug}/{$wp_rewrite->pagination_base}/([0-9]{1,})/?$", "index.php?post_type=$post_type" . '&paged=$matches[1]', 'top' );
 
996
                }
 
997
 
873
998
                $wp_rewrite->add_permastruct($post_type, "{$args->rewrite['slug']}/%$post_type%", $args->rewrite['with_front'], $args->permalink_epmask);
874
999
        }
875
1000
 
893
1018
/**
894
1019
 * Builds an object with all post type capabilities out of a post type object
895
1020
 *
896
 
 * Accepted keys of the capabilities array in the post type object:
897
 
 * - edit_post - The meta capability that controls editing a particular object of this post type. Defaults to "edit_ . $capability_type" (edit_post).
898
 
 * - edit_posts - The capability that controls editing objects of this post type as a class. Defaults to "edit_ . $capability_type . s" (edit_posts).
899
 
 * - edit_others_posts - The capability that controls editing objects of this post type that are owned by other users. Defaults to "edit_others_ . $capability_type . s" (edit_others_posts).
900
 
 * - publish_posts - The capability that controls publishing objects of this post type. Defaults to "publish_ . $capability_type . s" (publish_posts).
901
 
 * - read_post - The meta capability that controls reading a particular object of this post type. Defaults to "read_ . $capability_type" (read_post).
902
 
 * - read_private_posts - The capability that controls reading private posts. Defaults to "read_private . $capability_type . s" (read_private_posts).
903
 
 * - delete_post - The meta capability that controls deleting a particular object of this post type. Defaults to "delete_ . $capability_type" (delete_post).
904
 
 *
 
1021
 * Post type capabilities use the 'capability_type' argument as a base, if the
 
1022
 * capability is not set in the 'capabilities' argument array or if the
 
1023
 * 'capabilities' argument is not supplied.
 
1024
 *
 
1025
 * The capability_type argument can optionally be registered as an array, with
 
1026
 * the first value being singular and the second plural, e.g. array('story, 'stories')
 
1027
 * Otherwise, an 's' will be added to the value for the plural form. After
 
1028
 * registration, capability_type will always be a string of the singular value.
 
1029
 *
 
1030
 * By default, seven keys are accepted as part of the capabilities array:
 
1031
 *
 
1032
 * - edit_post, read_post, and delete_post are meta capabilities, which are then
 
1033
 *   generally mapped to corresponding primitive capabilities depending on the
 
1034
 *   context, which would be the post being edited/read/deleted and the user or
 
1035
 *   role being checked. Thus these capabilities would generally not be granted
 
1036
 *   directly to users or roles.
 
1037
 *
 
1038
 * - edit_posts - Controls whether objects of this post type can be edited.
 
1039
 * - edit_others_posts - Controls whether objects of this type owned by other users
 
1040
 *   can be edited. If the post type does not support an author, then this will
 
1041
 *   behave like edit_posts.
 
1042
 * - publish_posts - Controls publishing objects of this post type.
 
1043
 * - read_private_posts - Controls whether private objects can be read.
 
1044
 
 
1045
 * These four primitive capabilities are checked in core in various locations.
 
1046
 * There are also seven other primitive capabilities which are not referenced
 
1047
 * directly in core, except in map_meta_cap(), which takes the three aforementioned
 
1048
 * meta capabilities and translates them into one or more primitive capabilities
 
1049
 * that must then be checked against the user or role, depending on the context.
 
1050
 *
 
1051
 * - read - Controls whether objects of this post type can be read.
 
1052
 * - delete_posts - Controls whether objects of this post type can be deleted.
 
1053
 * - delete_private_posts - Controls whether private objects can be deleted.
 
1054
 * - delete_published_posts - Controls whether published objects can be deleted.
 
1055
 * - delete_others_posts - Controls whether objects owned by other users can be
 
1056
 *   can be deleted. If the post type does not support an author, then this will
 
1057
 *   behave like delete_posts.
 
1058
 * - edit_private_posts - Controls whether private objects can be edited.
 
1059
 * - edit_published_posts - Controls whether published objects can be deleted.
 
1060
 *
 
1061
 * These additional capabilities are only used in map_meta_cap(). Thus, they are
 
1062
 * only assigned by default if the post type is registered with the 'map_meta_cap'
 
1063
 * argument set to true (default is false).
 
1064
 *
 
1065
 * @see map_meta_cap()
905
1066
 * @since 3.0.0
906
 
 * @param object $args
 
1067
 *
 
1068
 * @param object $args Post type registration arguments
907
1069
 * @return object object with all the capabilities as member variables
908
1070
 */
909
1071
function get_post_type_capabilities( $args ) {
910
 
        $defaults = array(
911
 
                'edit_post'          => 'edit_'         . $args->capability_type,
912
 
                'edit_posts'         => 'edit_'         . $args->capability_type . 's',
913
 
                'edit_others_posts'  => 'edit_others_'  . $args->capability_type . 's',
914
 
                'publish_posts'      => 'publish_'      . $args->capability_type . 's',
915
 
                'read_post'          => 'read_'         . $args->capability_type,
916
 
                'read_private_posts' => 'read_private_' . $args->capability_type . 's',
917
 
                'delete_post'        => 'delete_'       . $args->capability_type,
 
1072
        if ( ! is_array( $args->capability_type ) )
 
1073
                $args->capability_type = array( $args->capability_type, $args->capability_type . 's' );
 
1074
 
 
1075
        // Singular base for meta capabilities, plural base for primitive capabilities.
 
1076
        list( $singular_base, $plural_base ) = $args->capability_type;
 
1077
 
 
1078
        $default_capabilities = array(
 
1079
                // Meta capabilities
 
1080
                'edit_post'          => 'edit_'         . $singular_base,
 
1081
                'read_post'          => 'read_'         . $singular_base,
 
1082
                'delete_post'        => 'delete_'       . $singular_base,
 
1083
                // Primitive capabilities used outside of map_meta_cap():
 
1084
                'edit_posts'         => 'edit_'         . $plural_base,
 
1085
                'edit_others_posts'  => 'edit_others_'  . $plural_base,
 
1086
                'publish_posts'      => 'publish_'      . $plural_base,
 
1087
                'read_private_posts' => 'read_private_' . $plural_base,
918
1088
        );
919
 
        $labels = array_merge( $defaults, $args->capabilities );
920
 
        return (object) $labels;
 
1089
 
 
1090
        // Primitive capabilities used within map_meta_cap():
 
1091
        if ( $args->map_meta_cap ) {
 
1092
                $default_capabilities_for_mapping = array(
 
1093
                        'read'                   => 'read',
 
1094
                        'delete_posts'           => 'delete_'           . $plural_base,
 
1095
                        'delete_private_posts'   => 'delete_private_'   . $plural_base,
 
1096
                        'delete_published_posts' => 'delete_published_' . $plural_base,
 
1097
                        'delete_others_posts'    => 'delete_others_'    . $plural_base,
 
1098
                        'edit_private_posts'     => 'edit_private_'     . $plural_base,
 
1099
                        'edit_published_posts'   => 'edit_published_'   . $plural_base,
 
1100
                );
 
1101
                $default_capabilities = array_merge( $default_capabilities, $default_capabilities_for_mapping );
 
1102
        }
 
1103
 
 
1104
        $capabilities = array_merge( $default_capabilities, $args->capabilities );
 
1105
 
 
1106
        // Remember meta capabilities for future reference.
 
1107
        if ( $args->map_meta_cap )
 
1108
                _post_type_meta_capabilities( $capabilities );
 
1109
 
 
1110
        return (object) $capabilities;
 
1111
}
 
1112
 
 
1113
/**
 
1114
 * Stores or returns a list of post type meta caps for map_meta_cap().
 
1115
 *
 
1116
 * @since 3.1.0
 
1117
 * @access private
 
1118
 */
 
1119
function _post_type_meta_capabilities( $capabilities = null ) {
 
1120
        static $meta_caps = array();
 
1121
        if ( null === $capabilities )
 
1122
                return $meta_caps;
 
1123
        foreach ( $capabilities as $core => $custom ) {
 
1124
                if ( in_array( $core, array( 'read_post', 'delete_post', 'edit_post' ) ) )
 
1125
                        $meta_caps[ $custom ] = $core;
 
1126
        }
921
1127
}
922
1128
 
923
1129
/**
936
1142
 * - not_found_in_trash - Default is No posts found in Trash/No pages found in Trash
937
1143
 * - parent_item_colon - This string isn't used on non-hierarchical types. In hierarchical ones the default is Parent Page:
938
1144
 *
939
 
 * Above, the first default value is for non-hierarchical post types (like posts) and the second one is for hierarchical post types (like pages.)
 
1145
 * Above, the first default value is for non-hierarchical post types (like posts) and the second one is for hierarchical post types (like pages).
940
1146
 *
941
1147
 * @since 3.0.0
942
1148
 * @param object $post_type_object
952
1158
                'new_item' => array( __('New Post'), __('New Page') ),
953
1159
                'view_item' => array( __('View Post'), __('View Page') ),
954
1160
                'search_items' => array( __('Search Posts'), __('Search Pages') ),
955
 
                'not_found' => array( __('No posts found'), __('No pages found') ),
956
 
                'not_found_in_trash' => array( __('No posts found in Trash'), __('No pages found in Trash') ),
957
 
                'parent_item_colon' => array( null, __('Parent Page:') )
 
1161
                'not_found' => array( __('No posts found.'), __('No pages found.') ),
 
1162
                'not_found_in_trash' => array( __('No posts found in Trash.'), __('No pages found in Trash.') ),
 
1163
                'parent_item_colon' => array( null, __('Parent Page:') ),
958
1164
        );
 
1165
        $nohier_vs_hier_defaults['menu_name'] = $nohier_vs_hier_defaults['name'];
959
1166
        return _get_custom_object_labels( $post_type_object, $nohier_vs_hier_defaults );
960
1167
}
961
1168
 
963
1170
 * Builds an object with custom-something object (post type, taxonomy) labels out of a custom-something object
964
1171
 *
965
1172
 * @access private
 
1173
 * @since 3.0.0
966
1174
 */
967
1175
function _get_custom_object_labels( $object, $nohier_vs_hier_defaults ) {
968
1176
 
972
1180
        if ( !isset( $object->labels['singular_name'] ) && isset( $object->labels['name'] ) )
973
1181
                $object->labels['singular_name'] = $object->labels['name'];
974
1182
 
975
 
        $defaults = array_map( create_function( '$x', $object->hierarchical? 'return $x[1];' : 'return $x[0];' ), $nohier_vs_hier_defaults );
 
1183
        if ( !isset( $object->labels['menu_name'] ) && isset( $object->labels['name'] ) )
 
1184
                $object->labels['menu_name'] = $object->labels['name'];
 
1185
 
 
1186
        foreach ( $nohier_vs_hier_defaults as $key => $value )
 
1187
                        $defaults[$key] = $object->hierarchical ? $value[1] : $value[0];
 
1188
 
976
1189
        $labels = array_merge( $defaults, $object->labels );
977
1190
        return (object)$labels;
978
1191
}
979
1192
 
980
1193
/**
 
1194
 * Adds submenus for post types.
 
1195
 *
 
1196
 * @access private
 
1197
 * @since 3.1.0
 
1198
 */
 
1199
function _add_post_type_submenus() {
 
1200
        foreach ( get_post_types( array( 'show_ui' => true ) ) as $ptype ) {
 
1201
                $ptype_obj = get_post_type_object( $ptype );
 
1202
                // Submenus only.
 
1203
                if ( ! $ptype_obj->show_in_menu || $ptype_obj->show_in_menu === true )
 
1204
                        continue;
 
1205
                add_submenu_page( $ptype_obj->show_in_menu, $ptype_obj->labels->name, $ptype_obj->labels->menu_name, $ptype_obj->cap->edit_posts, "edit.php?post_type=$ptype" );
 
1206
        }
 
1207
}
 
1208
add_action( 'admin_menu', '_add_post_type_submenus' );
 
1209
 
 
1210
/**
981
1211
 * Register support of certain features for a post type.
982
1212
 *
983
1213
 * All features are directly associated with a functional area of the edit screen, such as the
1111
1341
        $r = wp_parse_args( $args, $defaults );
1112
1342
        if ( empty( $r['post_status'] ) )
1113
1343
                $r['post_status'] = ( 'attachment' == $r['post_type'] ) ? 'inherit' : 'publish';
1114
 
        if ( ! empty($r['numberposts']) )
 
1344
        if ( ! empty($r['numberposts']) && empty($r['posts_per_page']) )
1115
1345
                $r['posts_per_page'] = $r['numberposts'];
1116
1346
        if ( ! empty($r['category']) )
1117
1347
                $r['cat'] = $r['category'];
1122
1352
        } elseif ( ! empty($r['exclude']) )
1123
1353
                $r['post__not_in'] = wp_parse_id_list( $r['exclude'] );
1124
1354
 
1125
 
        $r['caller_get_posts'] = true;
 
1355
        $r['ignore_sticky_posts'] = true;
 
1356
        $r['no_found_rows'] = true;
1126
1357
 
1127
1358
        $get_posts = new WP_Query;
1128
1359
        return $get_posts->query($r);
1143
1374
 * @link http://codex.wordpress.org/Function_Reference/add_post_meta
1144
1375
 *
1145
1376
 * @param int $post_id Post ID.
1146
 
 * @param string $key Metadata name.
1147
 
 * @param mixed $value Metadata value.
 
1377
 * @param string $meta_key Metadata name.
 
1378
 * @param mixed $meta_value Metadata value.
1148
1379
 * @param bool $unique Optional, default is false. Whether the same key should not be added.
1149
1380
 * @return bool False for failure. True for success.
1150
1381
 */
1205
1436
 *
1206
1437
 * If the meta field for the post does not exist, it will be added.
1207
1438
 *
1208
 
 * @since 1.5
 
1439
 * @since 1.5.0
1209
1440
 * @uses $wpdb
1210
1441
 * @link http://codex.wordpress.org/Function_Reference/update_post_meta
1211
1442
 *
1212
1443
 * @param int $post_id Post ID.
1213
 
 * @param string $key Metadata key.
1214
 
 * @param mixed $value Metadata value.
 
1444
 * @param string $meta_key Metadata key.
 
1445
 * @param mixed $meta_value Metadata value.
1215
1446
 * @param mixed $prev_value Optional. Previous value to check before removing.
1216
1447
 * @return bool False on failure, true if success.
1217
1448
 */
1266
1497
 * @param int $post_id post ID
1267
1498
 * @return array
1268
1499
 */
1269
 
function get_post_custom($post_id = 0) {
1270
 
        global $id;
1271
 
 
1272
 
        if ( !$post_id )
1273
 
                $post_id = (int) $id;
1274
 
 
1275
 
        $post_id = (int) $post_id;
1276
 
 
1277
 
        if ( ! wp_cache_get($post_id, 'post_meta') )
1278
 
                update_postmeta_cache($post_id);
1279
 
 
1280
 
        return wp_cache_get($post_id, 'post_meta');
 
1500
function get_post_custom( $post_id = 0 ) {
 
1501
        $post_id = absint( $post_id );
 
1502
 
 
1503
        if ( ! $post_id )
 
1504
                $post_id = get_the_ID();
 
1505
 
 
1506
        if ( ! wp_cache_get( $post_id, 'post_meta' ) )
 
1507
                update_postmeta_cache( $post_id );
 
1508
 
 
1509
        return wp_cache_get( $post_id, 'post_meta' );
1281
1510
}
1282
1511
 
1283
1512
/**
1334
1563
 * @param int $post_id Optional. Post ID.
1335
1564
 * @return bool Whether post is sticky.
1336
1565
 */
1337
 
function is_sticky($post_id = null) {
1338
 
        global $id;
1339
 
 
1340
 
        $post_id = absint($post_id);
1341
 
 
1342
 
        if ( !$post_id )
1343
 
                $post_id = absint($id);
1344
 
 
1345
 
        $stickies = get_option('sticky_posts');
1346
 
 
1347
 
        if ( !is_array($stickies) )
 
1566
function is_sticky( $post_id = 0 ) {
 
1567
        $post_id = absint( $post_id );
 
1568
 
 
1569
        if ( ! $post_id )
 
1570
                $post_id = get_the_ID();
 
1571
 
 
1572
        $stickies = get_option( 'sticky_posts' );
 
1573
 
 
1574
        if ( ! is_array( $stickies ) )
1348
1575
                return false;
1349
1576
 
1350
 
        if ( in_array($post_id, $stickies) )
 
1577
        if ( in_array( $post_id, $stickies ) )
1351
1578
                return true;
1352
1579
 
1353
1580
        return false;
1396
1623
 * when calling filters.
1397
1624
 *
1398
1625
 * @since 2.3.0
1399
 
 * @uses apply_filters() Calls 'edit_$field' and '${field_no_prefix}_edit_pre' passing $value and
 
1626
 * @uses apply_filters() Calls 'edit_$field' and '{$field_no_prefix}_edit_pre' passing $value and
1400
1627
 *  $post_id if $context == 'edit' and field name prefix == 'post_'.
1401
1628
 *
1402
1629
 * @uses apply_filters() Calls 'edit_post_$field' passing $value and $post_id if $context == 'db'.
1403
1630
 * @uses apply_filters() Calls 'pre_$field' passing $value if $context == 'db' and field name prefix == 'post_'.
1404
 
 * @uses apply_filters() Calls '${field}_pre' passing $value if $context == 'db' and field name prefix != 'post_'.
 
1631
 * @uses apply_filters() Calls '{$field}_pre' passing $value if $context == 'db' and field name prefix != 'post_'.
1405
1632
 *
1406
1633
 * @uses apply_filters() Calls '$field' passing $value, $post_id and $context if $context == anything
1407
1634
 *  other than 'raw', 'edit' and 'db' and field name prefix == 'post_'.
1440
1667
                $format_to_edit = array('post_content', 'post_excerpt', 'post_title', 'post_password');
1441
1668
 
1442
1669
                if ( $prefixed ) {
1443
 
                        $value = apply_filters("edit_$field", $value, $post_id);
 
1670
                        $value = apply_filters("edit_{$field}", $value, $post_id);
1444
1671
                        // Old school
1445
 
                        $value = apply_filters("${field_no_prefix}_edit_pre", $value, $post_id);
 
1672
                        $value = apply_filters("{$field_no_prefix}_edit_pre", $value, $post_id);
1446
1673
                } else {
1447
 
                        $value = apply_filters("edit_post_$field", $value, $post_id);
 
1674
                        $value = apply_filters("edit_post_{$field}", $value, $post_id);
1448
1675
                }
1449
1676
 
1450
1677
                if ( in_array($field, $format_to_edit) ) {
1457
1684
                }
1458
1685
        } else if ( 'db' == $context ) {
1459
1686
                if ( $prefixed ) {
1460
 
                        $value = apply_filters("pre_$field", $value);
1461
 
                        $value = apply_filters("${field_no_prefix}_save_pre", $value);
 
1687
                        $value = apply_filters("pre_{$field}", $value);
 
1688
                        $value = apply_filters("{$field_no_prefix}_save_pre", $value);
1462
1689
                } else {
1463
 
                        $value = apply_filters("pre_post_$field", $value);
1464
 
                        $value = apply_filters("${field}_pre", $value);
 
1690
                        $value = apply_filters("pre_post_{$field}", $value);
 
1691
                        $value = apply_filters("{$field}_pre", $value);
1465
1692
                }
1466
1693
        } else {
1467
1694
                // Use display filters by default.
1468
1695
                if ( $prefixed )
1469
1696
                        $value = apply_filters($field, $value, $post_id, $context);
1470
1697
                else
1471
 
                        $value = apply_filters("post_$field", $value, $post_id, $context);
 
1698
                        $value = apply_filters("post_{$field}", $value, $post_id, $context);
1472
1699
        }
1473
1700
 
1474
1701
        if ( 'attribute' == $context )
1653
1880
 *
1654
1881
 * @since 2.5.0
1655
1882
 *
1656
 
 * @param string|array $mime_types List of mime types or comma separated string of mime types.
 
1883
 * @param string|array $post_mime_types List of mime types or comma separated string of mime types.
1657
1884
 * @param string $table_alias Optional. Specify a table alias, if needed.
1658
1885
 * @return string The SQL AND clause for mime searching.
1659
1886
 */
1812
2039
 * @uses do_action() on 'trashed_post' after trashing
1813
2040
 * @uses wp_delete_post() if trash is disabled
1814
2041
 *
1815
 
 * @param int $postid Post ID.
 
2042
 * @param int $post_id Post ID.
1816
2043
 * @return mixed False on failure
1817
2044
 */
1818
2045
function wp_trash_post($post_id = 0) {
1847
2074
 * @uses do_action() on 'untrash_post' before undeletion
1848
2075
 * @uses do_action() on 'untrashed_post' after undeletion
1849
2076
 *
1850
 
 * @param int $postid Post ID.
 
2077
 * @param int $post_id Post ID.
1851
2078
 * @return mixed False on failure
1852
2079
 */
1853
2080
function wp_untrash_post($post_id = 0) {
2040
2267
 * Retrieve number of recent posts.
2041
2268
 *
2042
2269
 * @since 1.0.0
2043
 
 * @uses $wpdb
 
2270
 * @uses wp_parse_args()
 
2271
 * @uses get_posts()
2044
2272
 *
2045
 
 * @param int $num Optional, default is 10. Number of posts to get.
2046
 
 * @return array List of posts.
 
2273
 * @param string $deprecated Deprecated.
 
2274
 * @param array $args Optional. Overrides defaults.
 
2275
 * @param string $output Optional.
 
2276
 * @return unknown.
2047
2277
 */
2048
 
function wp_get_recent_posts($num = 10) {
2049
 
        global $wpdb;
2050
 
 
2051
 
        // Set the limit clause, if we got a limit
2052
 
        $num = (int) $num;
2053
 
        if ( $num ) {
2054
 
                $limit = "LIMIT $num";
2055
 
        }
2056
 
 
2057
 
        $sql = "SELECT * FROM $wpdb->posts WHERE post_type = 'post' AND post_status IN ( 'draft', 'publish', 'future', 'pending', 'private' ) ORDER BY post_date DESC $limit";
2058
 
        $result = $wpdb->get_results($sql, ARRAY_A);
2059
 
 
2060
 
        return $result ? $result : array();
 
2278
function wp_get_recent_posts( $args = array(), $output = ARRAY_A ) {
 
2279
 
 
2280
        if ( is_numeric( $args ) ) {
 
2281
                _deprecated_argument( __FUNCTION__, '3.1', __( 'Passing an integer number of posts is deprecated. Pass an array of arguments instead.' ) );
 
2282
                $args = array( 'numberposts' => absint( $args ) );
 
2283
        }
 
2284
 
 
2285
        // Set default arguments
 
2286
        $defaults = array(
 
2287
                'numberposts' => 10, 'offset' => 0,
 
2288
                'category' => 0, 'orderby' => 'post_date',
 
2289
                'order' => 'DESC', 'include' => '',
 
2290
                'exclude' => '', 'meta_key' => '',
 
2291
                'meta_value' =>'', 'post_type' => 'post', 'post_status' => 'draft, publish, future, pending, private',
 
2292
                'suppress_filters' => true
 
2293
        );
 
2294
 
 
2295
        $r = wp_parse_args( $args, $defaults );
 
2296
 
 
2297
        $results = get_posts( $r );
 
2298
 
 
2299
        // Backward compatibility. Prior to 3.1 expected posts to be returned in array
 
2300
        if ( ARRAY_A == $output ){
 
2301
                foreach( $results as $key => $result ) {
 
2302
                        $results[$key] = get_object_vars( $result );
 
2303
                }
 
2304
                return $results ? $results : array();
 
2305
        }
 
2306
 
 
2307
        return $results ? $results : false;
 
2308
 
2061
2309
}
2062
2310
 
2063
2311
/**
2077
2325
 
2078
2326
        $post = get_post($postid, $mode);
2079
2327
 
2080
 
        if ( 
 
2328
        if (
2081
2329
                ( OBJECT == $mode && empty( $post->ID ) ) ||
2082
2330
                ( OBJECT != $mode && empty( $post['ID'] ) )
2083
2331
        )
2128
2376
 *     'post_excerpt'  - Post excerpt.
2129
2377
 *
2130
2378
 * @since 1.0.0
2131
 
 * @link http://core.trac.wordpress.org/ticket/9084 Bug report on 'wp_insert_post_data' filter.
2132
2379
 * @uses $wpdb
2133
2380
 * @uses $wp_rewrite
2134
2381
 * @uses $user_ID
2135
 
 *
2136
2382
 * @uses do_action() Calls 'pre_post_update' on post ID if this is an update.
2137
2383
 * @uses do_action() Calls 'edit_post' action on post ID and post data if this is an update.
2138
 
 * @uses do_action() Calls 'save_post' and 'wp_insert_post' on post id and post data just before
2139
 
 *                   returning.
2140
 
 *
2141
 
 * @uses apply_filters() Calls 'wp_insert_post_data' passing $data, $postarr prior to database
2142
 
 *                       update or insert.
 
2384
 * @uses do_action() Calls 'save_post' and 'wp_insert_post' on post id and post data just before returning.
 
2385
 * @uses apply_filters() Calls 'wp_insert_post_data' passing $data, $postarr prior to database update or insert.
2143
2386
 * @uses wp_transition_post_status()
2144
2387
 *
2145
 
 * @param array $postarr Optional. Overrides defaults.
 
2388
 * @param array $postarr Elements that make up post to insert.
2146
2389
 * @param bool $wp_error Optional. Allow return of WP_Error on failure.
2147
2390
 * @return int|WP_Error The value 0 or WP_Error on failure. The post ID on success.
2148
2391
 */
2149
 
function wp_insert_post($postarr = array(), $wp_error = false) {
 
2392
function wp_insert_post($postarr, $wp_error = false) {
2150
2393
        global $wpdb, $wp_rewrite, $user_ID;
2151
2394
 
2152
2395
        $defaults = array('post_status' => 'draft', 'post_type' => 'post', 'post_author' => $user_ID,
2273
2516
        else
2274
2517
                $post_parent = 0;
2275
2518
 
2276
 
        if ( !empty($post_ID) ) {
2277
 
                if ( $post_parent == $post_ID ) {
2278
 
                        // Post can't be its own parent
2279
 
                        $post_parent = 0;
2280
 
                } elseif ( !empty($post_parent) ) {
2281
 
                        $parent_post = get_post($post_parent);
2282
 
                        // Check for circular dependency
2283
 
                        if ( isset( $parent_post->post_parent ) && $parent_post->post_parent == $post_ID )
2284
 
                                $post_parent = 0;
2285
 
                }
2286
 
        }
 
2519
        // Check the post_parent to see if it will cause a hierarchy loop
 
2520
        $post_parent = apply_filters( 'wp_insert_post_parent', $post_parent, $post_ID, compact( array_keys( $postarr ) ), $postarr );
2287
2521
 
2288
2522
        if ( isset($menu_order) )
2289
2523
                $menu_order = (int) $menu_order;
2518
2752
/**
2519
2753
 * Computes a unique slug for the post, when given the desired slug and some post details.
2520
2754
 *
 
2755
 * @since 2.8.0
 
2756
 *
2521
2757
 * @global wpdb $wpdb
2522
2758
 * @global WP_Rewrite $wp_rewrite
2523
2759
 * @param string $slug the desired slug (post_name)
2543
2779
                $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND ID != %d LIMIT 1";
2544
2780
                $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_ID ) );
2545
2781
 
2546
 
                if ( $post_name_check || in_array( $slug, $feeds ) ) {
 
2782
                if ( $post_name_check || in_array( $slug, $feeds ) || apply_filters( 'wp_unique_post_slug_is_bad_attachment_slug', false, $slug ) ) {
2547
2783
                        $suffix = 2;
2548
2784
                        do {
2549
2785
                                $alt_post_name = substr ($slug, 0, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix";
2558
2794
                $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type IN ( '" . implode( "', '", esc_sql( $hierarchical_post_types ) ) . "' ) AND ID != %d AND post_parent = %d LIMIT 1";
2559
2795
                $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_ID, $post_parent ) );
2560
2796
 
2561
 
                if ( $post_name_check || in_array( $slug, $feeds ) || preg_match( '@^(page)?\d+$@', $slug ) ) {
 
2797
                if ( $post_name_check || in_array( $slug, $feeds ) || preg_match( "@^($wp_rewrite->pagination_base)?\d+$@", $slug )  || apply_filters( 'wp_unique_post_slug_is_bad_hierarchical_slug', false, $slug, $post_type, $post_parent ) ) {
2562
2798
                        $suffix = 2;
2563
2799
                        do {
2564
2800
                                $alt_post_name = substr( $slug, 0, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix";
2572
2808
                $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type = %s AND ID != %d LIMIT 1";
2573
2809
                $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_type, $post_ID ) );
2574
2810
 
2575
 
                if ( $post_name_check || in_array( $slug, $feeds ) ) {
 
2811
                if ( $post_name_check || in_array( $slug, $feeds ) || apply_filters( 'wp_unique_post_slug_is_bad_flat_slug', false, $slug, $post_type ) ) {
2576
2812
                        $suffix = 2;
2577
2813
                        do {
2578
2814
                                $alt_post_name = substr( $slug, 0, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix";
2613
2849
 * @param int $post_id Post ID.
2614
2850
 * @param string $tags The tags to set for the post, separated by commas.
2615
2851
 * @param bool $append If true, don't delete existing tags, just add on. If false, replace the tags with the new tags.
2616
 
 * @return bool|null Will return false if $post_id is not an integer or is 0. Will return null otherwise
 
2852
 * @return mixed Array of affected term IDs. WP_Error or false on failure.
2617
2853
 */
2618
2854
function wp_set_post_tags( $post_id = 0, $tags = '', $append = false ) {
2619
2855
        return wp_set_post_terms( $post_id, $tags, 'post_tag', $append);
2628
2864
 * @param int $post_id Post ID.
2629
2865
 * @param string $tags The tags to set for the post, separated by commas.
2630
2866
 * @param bool $append If true, don't delete existing tags, just add on. If false, replace the tags with the new tags.
2631
 
 * @return bool|null Will return false if $post_id is not an integer or is 0. Will return null otherwise
 
2867
 * @return mixed Array of affected term IDs. WP_Error or false on failure.
2632
2868
 */
2633
2869
function wp_set_post_terms( $post_id = 0, $tags = '', $taxonomy = 'post_tag', $append = false ) {
2634
2870
        $post_id = (int) $post_id;
2648
2884
                $tags = array_unique( $tags );
2649
2885
        }
2650
2886
 
2651
 
        wp_set_object_terms($post_id, $tags, $taxonomy, $append);
 
2887
        return wp_set_object_terms($post_id, $tags, $taxonomy, $append);
2652
2888
}
2653
2889
 
2654
2890
/**
2704
2940
 *
2705
2941
 * @uses do_action() Calls 'transition_post_status' on $new_status, $old_status and
2706
2942
 *  $post if there is a status change.
2707
 
 * @uses do_action() Calls '${old_status}_to_$new_status' on $post if there is a status change.
2708
 
 * @uses do_action() Calls '${new_status}_$post->post_type' on post ID and $post.
 
2943
 * @uses do_action() Calls '{$old_status}_to_{$new_status}' on $post if there is a status change.
 
2944
 * @uses do_action() Calls '{$new_status}_{$post->post_type}' on post ID and $post.
2709
2945
 *
2710
2946
 * @param string $new_status Transition to this post status.
2711
2947
 * @param string $old_status Previous post status.
2713
2949
 */
2714
2950
function wp_transition_post_status($new_status, $old_status, $post) {
2715
2951
        do_action('transition_post_status', $new_status, $old_status, $post);
2716
 
        do_action("${old_status}_to_$new_status", $post);
2717
 
        do_action("${new_status}_$post->post_type", $post->ID, $post);
 
2952
        do_action("{$old_status}_to_{$new_status}", $post);
 
2953
        do_action("{$new_status}_{$post->post_type}", $post->ID, $post);
2718
2954
}
2719
2955
 
2720
2956
//
2767
3003
                        $pung[] = trim( $enclosure[ 0 ] );
2768
3004
                }
2769
3005
        }
2770
 
        $pung = apply_filters('get_enclosed', $pung);
 
3006
        $pung = apply_filters('get_enclosed', $pung, $post_id);
2771
3007
        return $pung;
2772
3008
}
2773
3009
 
2889
3125
 */
2890
3126
function get_page_by_path($page_path, $output = OBJECT, $post_type = 'page') {
2891
3127
        global $wpdb;
 
3128
        $null = null;
2892
3129
        $page_path = rawurlencode(urldecode($page_path));
2893
3130
        $page_path = str_replace('%2F', '/', $page_path);
2894
3131
        $page_path = str_replace('%20', ' ', $page_path);
2902
3139
        $pages = $wpdb->get_results( $wpdb->prepare( "SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_name = %s AND (post_type = %s OR post_type = 'attachment')", $leaf_path, $post_type ));
2903
3140
 
2904
3141
        if ( empty($pages) )
2905
 
                return null;
 
3142
                return $null;
2906
3143
 
2907
3144
        foreach ( $pages as $page ) {
2908
3145
                $path = '/' . $leaf_path;
2909
3146
                $curpage = $page;
2910
3147
                while ( $curpage->post_parent != 0 ) {
2911
 
                        $curpage = $wpdb->get_row( $wpdb->prepare( "SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE ID = %d and post_type = %s", $curpage->post_parent, $post_type ));
 
3148
                        $post_parent = $curpage->post_parent;
 
3149
                        $curpage = wp_cache_get( $post_parent, 'posts' );
 
3150
                        if ( false === $curpage )
 
3151
                                $curpage = $wpdb->get_row( $wpdb->prepare( "SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE ID = %d and post_type = %s", $post_parent, $post_type ) );
2912
3152
                        $path = '/' . $curpage->post_name . $path;
2913
3153
                }
2914
3154
 
2916
3156
                        return get_page($page->ID, $output, $post_type);
2917
3157
        }
2918
3158
 
2919
 
        return null;
 
3159
        return $null;
2920
3160
}
2921
3161
 
2922
3162
/**
2972
3212
 *
2973
3213
 * @since 2.0.0
2974
3214
 *
2975
 
 * @param array $posts Posts array.
2976
 
 * @param int $parent Parent page ID.
 
3215
 * @param array $pages Posts array.
 
3216
 * @param int $page_id Parent page ID.
2977
3217
 * @return array A list arranged by hierarchy. Children immediately follow their parents.
2978
3218
 */
2979
3219
function &get_page_hierarchy( &$pages, $page_id = 0 ) {
2980
 
 
2981
3220
        if ( empty( $pages ) ) {
2982
3221
                $result = array();
2983
3222
                return $result;
2985
3224
 
2986
3225
        $children = array();
2987
3226
        foreach ( (array) $pages as $p ) {
2988
 
 
2989
3227
                $parent_id = intval( $p->post_parent );
2990
3228
                $children[ $parent_id ][] = $p;
2991
 
         }
 
3229
        }
2992
3230
 
2993
 
         $result = array();
2994
 
         _page_traverse_name( $page_id, $children, $result );
 
3231
        $result = array();
 
3232
        _page_traverse_name( $page_id, $children, $result );
2995
3233
 
2996
3234
        return $result;
2997
3235
}
3000
3238
 * function to traverse and return all the nested children post names of a root page.
3001
3239
 * $children contains parent-chilren relations
3002
3240
 *
 
3241
 * @since 2.9.0
3003
3242
 */
3004
3243
function _page_traverse_name( $page_id, &$children, &$result ){
3005
 
 
3006
3244
        if ( isset( $children[ $page_id ] ) ){
3007
 
 
3008
3245
                foreach( (array)$children[ $page_id ] as $child ) {
3009
 
 
3010
3246
                        $result[ $child->ID ] = $child->post_name;
3011
3247
                        _page_traverse_name( $child->ID, $children, $result );
3012
3248
                }
3285
3521
 *
3286
3522
 * @param string|array $object Arguments to override defaults.
3287
3523
 * @param string $file Optional filename.
3288
 
 * @param int $post_parent Parent post ID.
 
3524
 * @param int $parent Parent post ID.
3289
3525
 * @return int Attachment ID.
3290
3526
 */
3291
3527
function wp_insert_attachment($object, $file = false, $parent = 0) {
3435
3671
 * @uses $wpdb
3436
3672
 * @uses do_action() Calls 'delete_attachment' hook on Attachment ID.
3437
3673
 *
3438
 
 * @param int $postid Attachment ID.
 
3674
 * @param int $post_id Attachment ID.
3439
3675
 * @param bool $force_delete Whether to bypass trash and force deletion. Defaults to false.
3440
3676
 * @return mixed False on failure. Post data on success.
3441
3677
 */
3511
3747
                foreach ( $backup_sizes as $size ) {
3512
3748
                        $del_file = path_join( dirname($meta['file']), $size['file'] );
3513
3749
                        $del_file = apply_filters('wp_delete_file', $del_file);
3514
 
            @ unlink( path_join($uploadpath['basedir'], $del_file) );
 
3750
                        @ unlink( path_join($uploadpath['basedir'], $del_file) );
3515
3751
                }
3516
3752
        }
3517
3753
 
3584
3820
                if ( ($uploads = wp_upload_dir()) && false === $uploads['error'] ) { //Get upload directory
3585
3821
                        if ( 0 === strpos($file, $uploads['basedir']) ) //Check that the upload base exists in the file location
3586
3822
                                $url = str_replace($uploads['basedir'], $uploads['baseurl'], $file); //replace file location with url location
3587
 
            elseif ( false !== strpos($file, 'wp-content/uploads') )
3588
 
                $url = $uploads['baseurl'] . substr( $file, strpos($file, 'wp-content/uploads') + 18 );
3589
 
            else
3590
 
                $url = $uploads['baseurl'] . "/$file"; //Its a newly uploaded file, therefor $file is relative to the basedir.
 
3823
                        elseif ( false !== strpos($file, 'wp-content/uploads') )
 
3824
                                $url = $uploads['baseurl'] . substr( $file, strpos($file, 'wp-content/uploads') + 18 );
 
3825
                        else
 
3826
                                $url = $uploads['baseurl'] . "/$file"; //Its a newly uploaded file, therefor $file is relative to the basedir.
3591
3827
                }
3592
3828
        }
3593
3829
 
3594
3830
        if ( empty($url) ) //If any of the above options failed, Fallback on the GUID as used pre-2.7, not recomended to rely upon this.
3595
3831
                $url = get_the_guid( $post->ID );
3596
3832
 
3597
 
        if ( 'attachment' != $post->post_type || empty($url) )
 
3833
        $url = apply_filters( 'wp_get_attachment_url', $url, $post->ID );
 
3834
 
 
3835
        if ( 'attachment' != $post->post_type || empty( $url ) )
3598
3836
                return false;
3599
3837
 
3600
 
        return apply_filters( 'wp_get_attachment_url', $url, $post->ID );
 
3838
        return $url;
3601
3839
}
3602
3840
 
3603
3841
/**
3761
3999
}
3762
4000
 
3763
4001
/**
3764
 
 * Checked for changed slugs for published posts and save old slug.
 
4002
 * Checked for changed slugs for published post objects and save the old slug.
3765
4003
 *
3766
 
 * The function is used along with form POST data. It checks for the wp-old-slug
3767
 
 * POST field. Will only be concerned with published posts and the slug actually
3768
 
 * changing.
 
4004
 * The function is used when a post object of any type is updated,
 
4005
 * by comparing the current and previous post objects.
3769
4006
 *
3770
4007
 * If the slug was changed and not already part of the old slugs then it will be
3771
4008
 * added to the post meta field ('_wp_old_slug') for storing old slugs for that
3772
4009
 * post.
3773
4010
 *
3774
 
 * The most logically usage of this function is redirecting changed posts, so
 
4011
 * The most logically usage of this function is redirecting changed post objects, so
3775
4012
 * that those that linked to an changed post will be redirected to the new post.
3776
4013
 *
3777
4014
 * @since 2.1.0
3778
4015
 *
3779
4016
 * @param int $post_id Post ID.
 
4017
 * @param object $post The Post Object
 
4018
 * @param object $post_before The Previous Post Object
3780
4019
 * @return int Same as $post_id
3781
4020
 */
3782
4021
function wp_check_for_changed_slugs($post_id, $post, $post_before) {
3784
4023
        if ( $post->post_name == $post_before->post_name )
3785
4024
                return;
3786
4025
 
3787
 
        // we're only concerned with published posts
3788
 
        if ( $post->post_status != 'publish' || $post->post_type != 'post' )
 
4026
        // we're only concerned with published, non-hierarchical objects
 
4027
        if ( $post->post_status != 'publish' || is_post_type_hierarchical( $post->post_type ) )
3789
4028
                return;
3790
4029
 
3791
4030
        $old_slugs = (array) get_post_meta($post_id, '_wp_old_slug');
3792
4031
 
3793
4032
        // if we haven't added this old slug before, add it now
3794
 
        if ( !in_array($post_before->post_name, $old_slugs) )
 
4033
        if ( !empty( $post_before->post_name ) && !in_array($post_before->post_name, $old_slugs) )
3795
4034
                add_post_meta($post_id, '_wp_old_slug', $post_before->post_name);
3796
4035
 
3797
4036
        // if the new slug was used previously, delete it from the list
3894
4133
 *
3895
4134
 * @since 0.71
3896
4135
 *
3897
 
 * @uses $wpdb
3898
 
 * @uses $blog_id
3899
4136
 * @uses apply_filters() Calls 'get_lastpostdate' filter
3900
4137
 *
3901
 
 * @global mixed $cache_lastpostdate Stores the last post date
3902
 
 * @global mixed $pagenow The current page being viewed
3903
 
 *
3904
4138
 * @param string $timezone The location to get the time. Can be 'gmt', 'blog', or 'server'.
3905
4139
 * @return string The date of the last post.
3906
4140
 */
3907
4141
function get_lastpostdate($timezone = 'server') {
3908
 
        global $cache_lastpostdate, $wpdb, $blog_id;
3909
 
        $add_seconds_server = date('Z');
3910
 
        if ( !isset($cache_lastpostdate[$blog_id][$timezone]) ) {
3911
 
                switch(strtolower($timezone)) {
3912
 
                        case 'gmt':
3913
 
                                $lastpostdate = $wpdb->get_var("SELECT post_date_gmt FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' ORDER BY post_date_gmt DESC LIMIT 1");
3914
 
                                break;
3915
 
                        case 'blog':
3916
 
                                $lastpostdate = $wpdb->get_var("SELECT post_date FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' ORDER BY post_date_gmt DESC LIMIT 1");
3917
 
                                break;
3918
 
                        case 'server':
3919
 
                                $lastpostdate = $wpdb->get_var("SELECT DATE_ADD(post_date_gmt, INTERVAL '$add_seconds_server' SECOND) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' ORDER BY post_date_gmt DESC LIMIT 1");
3920
 
                                break;
3921
 
                }
3922
 
                $cache_lastpostdate[$blog_id][$timezone] = $lastpostdate;
3923
 
        } else {
3924
 
                $lastpostdate = $cache_lastpostdate[$blog_id][$timezone];
3925
 
        }
3926
 
        return apply_filters( 'get_lastpostdate', $lastpostdate, $timezone );
 
4142
        return apply_filters( 'get_lastpostdate', _get_last_post_time( $timezone, 'date' ), $timezone );
3927
4143
}
3928
4144
 
3929
4145
/**
3934
4150
 * 'gmt' is when the last post was modified in GMT time.
3935
4151
 *
3936
4152
 * @since 1.2.0
3937
 
 * @uses $wpdb
3938
 
 * @uses $blog_id
3939
4153
 * @uses apply_filters() Calls 'get_lastpostmodified' filter
3940
4154
 *
3941
4155
 * @param string $timezone The location to get the time. Can be 'gmt', 'blog', or 'server'.
3942
4156
 * @return string The date the post was last modified.
3943
4157
 */
3944
4158
function get_lastpostmodified($timezone = 'server') {
3945
 
        global $wpdb;
3946
 
 
3947
 
        $add_seconds_server = date('Z');
3948
 
        $timezone = strtolower( $timezone );
3949
 
 
3950
 
        $lastpostmodified = wp_cache_get( "lastpostmodified:$timezone", 'timeinfo' );
3951
 
        if ( $lastpostmodified )
3952
 
                return apply_filters( 'get_lastpostmodified', $lastpostmodified, $timezone );
3953
 
 
3954
 
        switch ( strtolower($timezone) ) {
3955
 
                case 'gmt':
3956
 
                        $lastpostmodified = $wpdb->get_var("SELECT post_modified_gmt FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' ORDER BY post_modified_gmt DESC LIMIT 1");
3957
 
                        break;
3958
 
                case 'blog':
3959
 
                        $lastpostmodified = $wpdb->get_var("SELECT post_modified FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' ORDER BY post_modified_gmt DESC LIMIT 1");
3960
 
                        break;
3961
 
                case 'server':
3962
 
                        $lastpostmodified = $wpdb->get_var("SELECT DATE_ADD(post_modified_gmt, INTERVAL '$add_seconds_server' SECOND) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' ORDER BY post_modified_gmt DESC LIMIT 1");
3963
 
                        break;
3964
 
        }
 
4159
        $lastpostmodified = _get_last_post_time( $timezone, 'modified' );
3965
4160
 
3966
4161
        $lastpostdate = get_lastpostdate($timezone);
3967
4162
        if ( $lastpostdate > $lastpostmodified )
3968
4163
                $lastpostmodified = $lastpostdate;
3969
4164
 
3970
 
        if ( $lastpostmodified )
3971
 
                wp_cache_set( "lastpostmodified:$timezone", $lastpostmodified, 'timeinfo' );
3972
 
 
3973
4165
        return apply_filters( 'get_lastpostmodified', $lastpostmodified, $timezone );
3974
4166
}
3975
4167
 
3976
4168
/**
 
4169
 * Retrieve latest post date data based on timezone.
 
4170
 *
 
4171
 * @access private
 
4172
 * @since 3.1.0
 
4173
 *
 
4174
 * @param string $timezone The location to get the time. Can be 'gmt', 'blog', or 'server'.
 
4175
 * @param string $field Field to check. Can be 'date' or 'modified'.
 
4176
 * @return string The date.
 
4177
 */
 
4178
function _get_last_post_time( $timezone, $field ) {
 
4179
        global $wpdb;
 
4180
 
 
4181
        if ( !in_array( $field, array( 'date', 'modified' ) ) )
 
4182
                return false;
 
4183
 
 
4184
        $timezone = strtolower( $timezone );
 
4185
 
 
4186
        $key = "lastpost{$field}:$timezone";
 
4187
 
 
4188
        $date = wp_cache_get( $key, 'timeinfo' );
 
4189
 
 
4190
        if ( !$date ) {
 
4191
                $add_seconds_server = date('Z');
 
4192
 
 
4193
                $post_types = get_post_types( array( 'publicly_queryable' => true ) );
 
4194
                array_walk( $post_types, array( &$wpdb, 'escape_by_ref' ) );
 
4195
                $post_types = "'" . implode( "', '", $post_types ) . "'";
 
4196
 
 
4197
                switch ( $timezone ) {
 
4198
                        case 'gmt':
 
4199
                                $date = $wpdb->get_var("SELECT post_{$field}_gmt FROM $wpdb->posts WHERE post_status = 'publish' AND post_type IN ({$post_types}) ORDER BY post_{$field}_gmt DESC LIMIT 1");
 
4200
                                break;
 
4201
                        case 'blog':
 
4202
                                $date = $wpdb->get_var("SELECT post_{$field} FROM $wpdb->posts WHERE post_status = 'publish' AND post_type IN ({$post_types}) ORDER BY post_{$field}_gmt DESC LIMIT 1");
 
4203
                                break;
 
4204
                        case 'server':
 
4205
                                $date = $wpdb->get_var("SELECT DATE_ADD(post_{$field}_gmt, INTERVAL '$add_seconds_server' SECOND) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type IN ({$post_types}) ORDER BY post_{$field}_gmt DESC LIMIT 1");
 
4206
                                break;
 
4207
                }
 
4208
 
 
4209
                if ( $date )
 
4210
                        wp_cache_set( $key, $date, 'timeinfo' );
 
4211
        }
 
4212
 
 
4213
        return $date;
 
4214
}
 
4215
 
 
4216
/**
3977
4217
 * Updates posts in cache.
3978
4218
 *
3979
4219
 * @usedby update_page_cache() Aliased by this function.
4019
4259
 
4020
4260
        $id = (int) $id;
4021
4261
 
 
4262
        if ( 0 === $id )
 
4263
                return;
 
4264
 
4022
4265
        wp_cache_delete($id, 'posts');
4023
4266
        wp_cache_delete($id, 'post_meta');
4024
4267
 
4029
4272
        do_action('clean_post_cache', $id);
4030
4273
 
4031
4274
        if ( $children = $wpdb->get_col( $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_parent = %d", $id) ) ) {
4032
 
                foreach( $children as $cid )
 
4275
                foreach ( $children as $cid ) {
 
4276
                        // Loop detection
 
4277
                        if ( $cid == $id )
 
4278
                                continue;
4033
4279
                        clean_post_cache( $cid );
 
4280
                }
4034
4281
        }
4035
4282
 
4036
4283
        if ( is_multisite() )
4106
4353
        if ( empty($post_type) )
4107
4354
                $post_type = 'post';
4108
4355
 
4109
 
        if ( !is_array($post_type) && 'any' != $post_type && $update_term_cache )
4110
 
                update_object_term_cache($post_ids, $post_type);
 
4356
        if ( $update_term_cache ) {
 
4357
                if ( is_array($post_type) ) {
 
4358
                        $ptypes = $post_type;
 
4359
                } elseif ( 'any' == $post_type ) {
 
4360
                        // Just use the post_types in the supplied posts.
 
4361
                        foreach ( $posts as $post )
 
4362
                                $ptypes[] = $post->post_type;
 
4363
                        $ptypes = array_unique($ptypes);
 
4364
                } else {
 
4365
                        $ptypes = array($post_type);
 
4366
                }
 
4367
 
 
4368
                if ( ! empty($ptypes) )
 
4369
                        update_object_term_cache($post_ids, $ptypes);
 
4370
        }
4111
4371
 
4112
4372
        if ( $update_meta_cache )
4113
4373
                update_postmeta_cache($post_ids);
4197
4457
 
4198
4458
        // If published posts changed clear the lastpostmodified cache
4199
4459
        if ( 'publish' == $new_status || 'publish' == $old_status) {
4200
 
                wp_cache_delete( 'lastpostmodified:server', 'timeinfo' );
4201
 
                wp_cache_delete( 'lastpostmodified:gmt',    'timeinfo' );
4202
 
                wp_cache_delete( 'lastpostmodified:blog',   'timeinfo' );
 
4460
                foreach ( array( 'server', 'gmt', 'blog' ) as $timezone ) {
 
4461
                        wp_cache_delete( "lastpostmodified:$timezone", 'timeinfo' );
 
4462
                        wp_cache_delete( "lastpostdate:$timezone", 'timeinfo' );
 
4463
                }
4203
4464
        }
4204
4465
 
4205
4466
        // Always clears the hook in case the post status bounced from future to draft.
4302
4563
 * complete. The post parent will be an ancestor and the parent of the post
4303
4564
 * parent will be an ancestor. There will only be two ancestors at the most.
4304
4565
 *
4305
 
 * @since unknown
 
4566
 * @since 2.5.0
4306
4567
 * @access private
4307
4568
 * @uses $wpdb
4308
4569
 *
4322
4583
 
4323
4584
        $id = $_post->ancestors[] = $_post->post_parent;
4324
4585
        while ( $ancestor = $wpdb->get_var( $wpdb->prepare("SELECT `post_parent` FROM $wpdb->posts WHERE ID = %d LIMIT 1", $id) ) ) {
4325
 
                if ( $id == $ancestor )
 
4586
                // Loop detection: If the ancestor has been seen before, break.
 
4587
                if ( ( $ancestor == $_post->ID ) || in_array($ancestor,  $_post->ancestors) )
4326
4588
                        break;
4327
4589
                $id = $_post->ancestors[] = $ancestor;
4328
4590
        }
4650
4912
 * @uses wp_delete_post()
4651
4913
 *
4652
4914
 * @param int|object $revision_id Revision ID or revision object.
4653
 
 * @param array $fields Optional. What fields to restore from.  Defaults to all.
4654
 
 * @return mixed Null if error, false if no fields to restore, (int) post ID if success.
 
4915
 * @return mixed Null or WP_Error if error, deleted post if success.
4655
4916
 */
4656
4917
function wp_delete_post_revision( $revision_id ) {
4657
4918
        if ( !$revision = wp_get_post_revision( $revision_id ) )
4724
4985
                add_filter('the_preview', '_set_preview');
4725
4986
        }
4726
4987
}
 
4988
 
 
4989
/**
 
4990
 * Returns the post's parent's post_ID
 
4991
 *
 
4992
 * @since 3.1.0
 
4993
 *
 
4994
 * @param int $post_id
 
4995
 *
 
4996
 * @return int|bool false on error
 
4997
 */
 
4998
function wp_get_post_parent_id( $post_ID ) {
 
4999
        $post = get_post( $post_ID );
 
5000
        if ( !$post || is_wp_error( $post ) )
 
5001
                return false;
 
5002
        return (int) $post->post_parent;
 
5003
}
 
5004
 
 
5005
/**
 
5006
 * Checks the given subset of the post hierarchy for hierarchy loops.
 
5007
 * Prevents loops from forming and breaks those that it finds.
 
5008
 *
 
5009
 * Attached to the wp_insert_post_parent filter.
 
5010
 *
 
5011
 * @since 3.1.0
 
5012
 * @uses wp_find_hierarchy_loop()
 
5013
 *
 
5014
 * @param int $post_parent ID of the parent for the post we're checking.
 
5015
 * @parem int $post_ID ID of the post we're checking.
 
5016
 *
 
5017
 * @return int The new post_parent for the post.
 
5018
 */
 
5019
function wp_check_post_hierarchy_for_loops( $post_parent, $post_ID ) {
 
5020
        // Nothing fancy here - bail
 
5021
        if ( !$post_parent )
 
5022
                return 0;
 
5023
 
 
5024
        // New post can't cause a loop
 
5025
        if ( empty( $post_ID ) )
 
5026
                return $post_parent;
 
5027
 
 
5028
        // Can't be its own parent
 
5029
        if ( $post_parent == $post_ID )
 
5030
                return 0;
 
5031
 
 
5032
        // Now look for larger loops
 
5033
 
 
5034
        if ( !$loop = wp_find_hierarchy_loop( 'wp_get_post_parent_id', $post_ID, $post_parent ) )
 
5035
                return $post_parent; // No loop
 
5036
 
 
5037
        // Setting $post_parent to the given value causes a loop
 
5038
        if ( isset( $loop[$post_ID] ) )
 
5039
                return 0;
 
5040
 
 
5041
        // There's a loop, but it doesn't contain $post_ID.  Break the loop.
 
5042
        foreach ( array_keys( $loop ) as $loop_member )
 
5043
                wp_update_post( array( 'ID' => $loop_member, 'post_parent' => 0 ) );
 
5044
 
 
5045
        return $post_parent;
 
5046
}
 
5047
 
 
5048
/**
 
5049
 * Returns an array of post format slugs to their translated and pretty display versions
 
5050
 *
 
5051
 * @since 3.1.0
 
5052
 *
 
5053
 * @return array The array of translations
 
5054
 */
 
5055
function get_post_format_strings() {
 
5056
        $strings = array(
 
5057
                'standard' => _x( 'Standard', 'Post format' ), // Special case. any value that evals to false will be considered standard
 
5058
                'aside'    => _x( 'Aside',    'Post format' ),
 
5059
                'chat'     => _x( 'Chat',     'Post format' ),
 
5060
                'gallery'  => _x( 'Gallery',  'Post format' ),
 
5061
                'link'     => _x( 'Link',     'Post format' ),
 
5062
                'image'    => _x( 'Image',    'Post format' ),
 
5063
                'quote'    => _x( 'Quote',    'Post format' ),
 
5064
                'status'   => _x( 'Status',   'Post format' ),
 
5065
                'video'    => _x( 'Video',    'Post format' ),
 
5066
                'audio'    => _x( 'Audio',    'Post format' ),
 
5067
        );
 
5068
        return $strings;
 
5069
}
 
5070
 
 
5071
/**
 
5072
 * Retrieves an array of post format slugs.
 
5073
 *
 
5074
 * @since 3.1.0
 
5075
 *
 
5076
 * @return array The array of post format slugs.
 
5077
 */
 
5078
function get_post_format_slugs() {
 
5079
        // 3.2-early: use array_combine() and array_keys( get_post_format_strings() )
 
5080
        $slugs = array(
 
5081
                'standard' => 'standard', // Special case. any value that evals to false will be considered standard
 
5082
                'aside'    => 'aside',
 
5083
                'chat'     => 'chat',
 
5084
                'gallery'  => 'gallery',
 
5085
                'link'     => 'link',
 
5086
                'image'    => 'image',
 
5087
                'quote'    => 'quote',
 
5088
                'status'   => 'status',
 
5089
                'video'    => 'video',
 
5090
                'audio'    => 'audio',
 
5091
        );
 
5092
        return $slugs;
 
5093
}
 
5094
 
 
5095
/**
 
5096
 * Returns a pretty, translated version of a post format slug
 
5097
 *
 
5098
 * @since 3.1.0
 
5099
 *
 
5100
 * @param string $slug A post format slug
 
5101
 * @return string The translated post format name
 
5102
 */
 
5103
function get_post_format_string( $slug ) {
 
5104
        $strings = get_post_format_strings();
 
5105
        if ( !$slug )
 
5106
                return $strings['standard'];
 
5107
        else
 
5108
                return ( isset( $strings[$slug] ) ) ? $strings[$slug] : '';
 
5109
}
 
5110
 
 
5111
/**
 
5112
 * Sets a post thumbnail.
 
5113
 *
 
5114
 * @since 3.1.0
 
5115
 *
 
5116
 * @param int|object $post Post ID or object where thumbnail should be attached.
 
5117
 * @param int $thumbnail_id Thumbnail to attach.
 
5118
 * @return bool True on success, false on failure.
 
5119
 */
 
5120
function set_post_thumbnail( $post, $thumbnail_id ) {
 
5121
        $post = get_post( $post );
 
5122
        $thumbnail_id = absint( $thumbnail_id );
 
5123
        if ( $post && $thumbnail_id && get_post( $thumbnail_id ) ) {
 
5124
                $thumbnail_html = wp_get_attachment_image( $thumbnail_id, 'thumbnail' );
 
5125
                if ( ! empty( $thumbnail_html ) ) {
 
5126
                        update_post_meta( $post->ID, '_thumbnail_id', $thumbnail_id );
 
5127
                        return true;
 
5128
                }
 
5129
        }
 
5130
        return false;
 
5131
}
 
5132
 
 
5133
/**
 
5134
 * Returns a link to a post format index.
 
5135
 *
 
5136
 * @since 3.1.0
 
5137
 *
 
5138
 * @param $format string Post format
 
5139
 * @return string Link
 
5140
 */
 
5141
function get_post_format_link( $format ) {
 
5142
        $term = get_term_by('slug', 'post-format-' . $format, 'post_format' );
 
5143
        if ( ! $term || is_wp_error( $term ) )
 
5144
                return false;
 
5145
        return get_term_link( $term );
 
5146
}
 
5147
 
 
5148
/**
 
5149
 * Filters the request to allow for the format prefix.
 
5150
 *
 
5151
 * @access private
 
5152
 * @since 3.1.0
 
5153
 */
 
5154
function _post_format_request( $qvs ) {
 
5155
        if ( ! isset( $qvs['post_format'] ) )
 
5156
                return $qvs;
 
5157
        $slugs = get_post_format_slugs();
 
5158
        if ( isset( $slugs[ $qvs['post_format'] ] ) )
 
5159
                $qvs['post_format'] = 'post-format-' . $slugs[ $qvs['post_format'] ];
 
5160
        $tax = get_taxonomy( 'post_format' );
 
5161
        $qvs['post_type'] = $tax->object_type;
 
5162
        return $qvs;
 
5163
}
 
5164
add_filter( 'request', '_post_format_request' );
 
5165
 
 
5166
/**
 
5167
 * Filters the post format term link to remove the format prefix.
 
5168
 *
 
5169
 * @access private
 
5170
 * @since 3.1.0
 
5171
 */
 
5172
function _post_format_link( $link, $term, $taxonomy ) {
 
5173
        global $wp_rewrite;
 
5174
        if ( 'post_format' != $taxonomy )
 
5175
                return $link;
 
5176
        if ( $wp_rewrite->get_extra_permastruct( $taxonomy ) ) {
 
5177
                return str_replace( "/{$term->slug}", '/' . str_replace( 'post-format-', '', $term->slug ), $link );
 
5178
        } else {
 
5179
                $link = remove_query_arg( 'post_format', $link );
 
5180
                return add_query_arg( 'post_format', str_replace( 'post-format-', '', $term->slug ), $link );
 
5181
        }
 
5182
}
 
5183
add_filter( 'term_link', '_post_format_link', 10, 3 );
 
5184
 
 
5185
/**
 
5186
 * Remove the post format prefix from the name property of the term object created by get_term().
 
5187
 *
 
5188
 * @access private
 
5189
 * @since 3.1.0
 
5190
 */
 
5191
function _post_format_get_term( $term ) {
 
5192
        if ( isset( $term->slug ) ) {
 
5193
                $term->name = get_post_format_string( str_replace( 'post-format-', '', $term->slug ) );
 
5194
        }
 
5195
        return $term;
 
5196
}
 
5197
add_filter( 'get_post_format', '_post_format_get_term' );
 
5198
 
 
5199
/**
 
5200
 * Remove the post format prefix from the name property of the term objects created by get_terms().
 
5201
 *
 
5202
 * @access private
 
5203
 * @since 3.1.0
 
5204
 */
 
5205
function _post_format_get_terms( $terms, $taxonomies, $args ) {
 
5206
        if ( in_array( 'post_format', (array) $taxonomies ) ) {
 
5207
                if ( isset( $args['fields'] ) && 'names' == $args['fields'] ) {
 
5208
                        foreach( $terms as $order => $name ) {
 
5209
                                $terms[$order] = get_post_format_string( str_replace( 'post-format-', '', $name ) );
 
5210
                        }
 
5211
                } else {
 
5212
                        foreach ( (array) $terms as $order => $term ) {
 
5213
                                if ( isset( $term->taxonomy ) && 'post_format' == $term->taxonomy ) {
 
5214
                                        $terms[$order]->name = get_post_format_string( str_replace( 'post-format-', '', $term->slug ) );
 
5215
                                }
 
5216
                        }
 
5217
                }
 
5218
        }
 
5219
        return $terms;
 
5220
}
 
5221
add_filter( 'get_terms', '_post_format_get_terms', 10, 3 );
 
5222
 
 
5223
/**
 
5224
 * Remove the post format prefix from the name property of the term objects created by wp_get_object_terms().
 
5225
 *
 
5226
 * @access private
 
5227
 * @since 3.1.0
 
5228
 */
 
5229
function _post_format_wp_get_object_terms( $terms ) {
 
5230
        foreach ( (array) $terms as $order => $term ) {
 
5231
                if ( isset( $term->taxonomy ) && 'post_format' == $term->taxonomy ) {
 
5232
                        $terms[$order]->name = get_post_format_string( str_replace( 'post-format-', '', $term->slug ) );
 
5233
                }
 
5234
        }
 
5235
        return $terms;
 
5236
}
 
5237
add_filter( 'wp_get_object_terms', '_post_format_wp_get_object_terms' );
 
5238
 
 
5239
?>
 
 
b'\\ No newline at end of file'