~ubuntu-branches/debian/sid/wordpress/sid

« back to all changes in this revision

Viewing changes to wp-includes/post.php

  • Committer: Package Import Robot
  • Author(s): Raphaël Hertzog
  • Date: 2013-09-04 23:18:58 UTC
  • mfrom: (1.2.28)
  • Revision ID: package-import@ubuntu.com-20130904231858-nljmn1buzswh63jk
Tags: 3.6+dfsg-1
* New upstream release.
* Improve wp-settings to verify that $_SERVER['HTTP_X_FORWARDED_PROTO']
  exists before accessing it (avoids a PHP notice).
  Thanks to Paul Dreik <slask@pauldreik.se> for the report and the patch.
* Document in README.Debian the need to login to /wp-admin/ to complete
  an upgrade.
* Drop useless debian/README.source
* Drop 008CVE2008-2392.patch since upstream now disables unfiltered
  uploads by default. See http://core.trac.wordpress.org/ticket/10692
* Drop 009CVE2008-6767.patch since the backto parameter is validated
  against a whitelist, and externally triggered upgrades are not a
  security problem as long as they work.
* Update debian/missing-sources with latest versions.
* Update upstream l10n.

Show diffs side-by-side

added added

removed removed

Lines of Context:
203
203
 *
204
204
 * @param int $attachment_id Attachment ID
205
205
 * @param string $file File path for the attachment
206
 
 * @return bool False on failure, true on success.
 
206
 * @return bool True on success, false on failure.
207
207
 */
208
208
function update_attached_file( $attachment_id, $file ) {
209
209
        if ( !get_post( $attachment_id ) )
360
360
                $more_text = '';
361
361
        }
362
362
 
363
 
        // Strip leading and trailing whitespace
 
363
        // ` leading and trailing whitespace
364
364
        $main = preg_replace('/^[\s]*(.*)[\s]*$/', '\\1', $main);
365
365
        $extended = preg_replace('/^[\s]*(.*)[\s]*$/', '\\1', $extended);
366
366
        $more_text = preg_replace('/^[\s]*(.*)[\s]*$/', '\\1', $more_text);
567
567
         */
568
568
        public $filter;
569
569
 
 
570
        /**
 
571
         * Private variable used by post formats to cache parsed content.
 
572
         *
 
573
         * @since 3.6.0
 
574
         *
 
575
         * @var array
 
576
         * @access private
 
577
         */
 
578
        public $format_content;
 
579
 
 
580
 
570
581
        public static function get_instance( $post_id ) {
571
582
                global $wpdb;
572
583
 
713
724
 * @since 2.3.0
714
725
 * @uses sanitize_post_field() See for possible $context values.
715
726
 *
716
 
 * @param string $field Post field name
717
 
 * @param id $post Post ID
718
 
 * @param string $context Optional. How to filter the field. Default is display.
719
 
 * @return bool|string False on failure or returns the value in post field
 
727
 * @param string $field Post field name.
 
728
 * @param int|object $post Post ID or post object.
 
729
 * @param string $context Optional. How to filter the field. Default is 'display'.
 
730
 * @return string The value of the post field on success, empty string on failure.
720
731
 */
721
732
function get_post_field( $field, $post, $context = 'display' ) {
722
733
        $post = get_post( $post );
738
749
 *
739
750
 * @since 2.0.0
740
751
 *
741
 
 * @param int $ID Optional. Post ID.
742
 
 * @return bool|string False on failure or returns the mime type
 
752
 * @param int $ID Optional. Post ID. Default is the current post from the loop.
 
753
 * @return string|bool The mime type on success, false on failure.
743
754
 */
744
755
function get_post_mime_type($ID = '') {
745
756
        $post = get_post($ID);
751
762
}
752
763
 
753
764
/**
754
 
 * Retrieve the format slug for a post
755
 
 *
756
 
 * @since 3.1.0
757
 
 *
758
 
 * @param int|object $post A post
759
 
 *
760
 
 * @return mixed The format if successful. False if no format is set. WP_Error if errors.
761
 
 */
762
 
function get_post_format( $post = null ) {
763
 
        $post = get_post($post);
764
 
 
765
 
        if ( ! post_type_supports( $post->post_type, 'post-formats' ) )
766
 
                return false;
767
 
 
768
 
        $_format = get_the_terms( $post->ID, 'post_format' );
769
 
 
770
 
        if ( empty( $_format ) )
771
 
                return false;
772
 
 
773
 
        $format = array_shift( $_format );
774
 
 
775
 
        return ( str_replace('post-format-', '', $format->slug ) );
776
 
}
777
 
 
778
 
/**
779
 
 * Check if a post has a particular format
780
 
 *
781
 
 * @since 3.1.0
782
 
 * @uses has_term()
783
 
 *
784
 
 * @param string $format The format to check for
785
 
 * @param object|id $post The post to check. If not supplied, defaults to the current post if used in the loop.
786
 
 * @return bool True if the post has the format, false otherwise.
787
 
 */
788
 
function has_post_format( $format, $post = null ) {
789
 
        return has_term('post-format-' . sanitize_key($format), 'post_format', $post);
790
 
}
791
 
 
792
 
/**
793
 
 * Assign a format to a post
794
 
 *
795
 
 * @since 3.1.0
796
 
 *
797
 
 * @param int|object $post The post for which to assign a format
798
 
 * @param string $format  A format to assign. Use an empty string or array to remove all formats from the post.
799
 
 * @return mixed WP_Error on error. Array of affected term IDs on success.
800
 
 */
801
 
function set_post_format( $post, $format ) {
802
 
        $post = get_post($post);
803
 
 
804
 
        if ( empty($post) )
805
 
                return new WP_Error('invalid_post', __('Invalid post'));
806
 
 
807
 
        if ( !empty($format) ) {
808
 
                $format = sanitize_key($format);
809
 
                if ( 'standard' == $format || !in_array( $format, array_keys( get_post_format_slugs() ) ) )
810
 
                        $format = '';
811
 
                else
812
 
                        $format = 'post-format-' . $format;
813
 
        }
814
 
 
815
 
        return wp_set_post_terms($post->ID, $format, 'post_format');
816
 
}
817
 
 
818
 
/**
819
765
 * Retrieve the post status based on the Post ID.
820
766
 *
821
767
 * If the post ID is of an attachment, then the parent post status will be given
823
769
 *
824
770
 * @since 2.0.0
825
771
 *
826
 
 * @param int $ID Post ID
827
 
 * @return string|bool Post status or false on failure.
 
772
 * @param int $ID Optional. Post ID. Default is the current post from the loop.
 
773
 * @return string|bool Post status on success, false on failure.
828
774
 */
829
775
function get_post_status($ID = '') {
830
776
        $post = get_post($ID);
858
804
 *
859
805
 * @return array List of post statuses.
860
806
 */
861
 
function get_post_statuses( ) {
 
807
function get_post_statuses() {
862
808
        $status = array(
863
809
                'draft'                 => __('Draft'),
864
810
                'pending'               => __('Pending Review'),
879
825
 *
880
826
 * @return array List of page statuses.
881
827
 */
882
 
function get_page_statuses( ) {
 
828
function get_page_statuses() {
883
829
        $status = array(
884
830
                'draft'                 => __('Draft'),
885
831
                'private'               => __('Private'),
1063
1009
 *
1064
1010
 * @since 2.1.0
1065
1011
 *
1066
 
 * @uses $post The Loop current post global
1067
 
 *
1068
 
 * @param mixed $post Optional. Post object or post ID.
1069
 
 * @return bool|string post type or false on failure.
 
1012
 * @param int|object $post Optional. Post ID or post object. Default is the current post from the loop.
 
1013
 * @return string|bool Post type on success, false on failure.
1070
1014
 */
1071
1015
function get_post_type( $post = null ) {
1072
1016
        if ( $post = get_post( $post ) )
1139
1083
 *     * While the default settings of exclude_from_search, publicly_queryable, show_ui, and show_in_nav_menus are
1140
1084
 *       inherited from public, each does not rely on this relationship and controls a very specific intention.
1141
1085
 * - exclude_from_search - Whether to exclude posts with this post type from front end search results.
1142
 
 *     * If not set, the the opposite of public's current value is used.
 
1086
 *     * If not set, the opposite of public's current value is used.
1143
1087
 * - publicly_queryable - Whether queries can be performed on the front end for the post type as part of parse_request().
1144
1088
 *     * ?post_type={post_type_key}
1145
1089
 *     * ?{post_type_key}={single_post_slug}
1594
1538
function remove_post_type_support( $post_type, $feature ) {
1595
1539
        global $_wp_post_type_features;
1596
1540
 
1597
 
        if ( !isset($_wp_post_type_features[$post_type]) )
1598
 
                return;
1599
 
 
1600
 
        if ( isset($_wp_post_type_features[$post_type][$feature]) )
1601
 
                unset($_wp_post_type_features[$post_type][$feature]);
 
1541
        if ( isset( $_wp_post_type_features[$post_type][$feature] ) )
 
1542
                unset( $_wp_post_type_features[$post_type][$feature] );
1602
1543
}
1603
1544
 
1604
1545
/**
1630
1571
function post_type_supports( $post_type, $feature ) {
1631
1572
        global $_wp_post_type_features;
1632
1573
 
1633
 
        if ( !isset( $_wp_post_type_features[$post_type][$feature] ) )
1634
 
                return false;
1635
 
 
1636
 
        // If no args passed then no extra checks need be performed
1637
 
        if ( func_num_args() <= 2 )
1638
 
                return true;
1639
 
 
1640
 
        // @todo Allow pluggable arg checking
1641
 
        //$args = array_slice( func_get_args(), 2 );
1642
 
 
1643
 
        return true;
 
1574
        return ( isset( $_wp_post_type_features[$post_type][$feature] ) );
1644
1575
}
1645
1576
 
1646
1577
/**
1742
1673
 * @param string $meta_key Metadata name.
1743
1674
 * @param mixed $meta_value Metadata value.
1744
1675
 * @param bool $unique Optional, default is false. Whether the same key should not be added.
1745
 
 * @return bool False for failure. True for success.
 
1676
 * @return int|bool Meta ID on success, false on failure.
1746
1677
 */
1747
1678
function add_post_meta($post_id, $meta_key, $meta_value, $unique = false) {
1748
1679
        // make sure meta is added to the post, not a revision
1766
1697
 * @param int $post_id post ID
1767
1698
 * @param string $meta_key Metadata name.
1768
1699
 * @param mixed $meta_value Optional. Metadata value.
1769
 
 * @return bool False for failure. True for success.
 
1700
 * @return bool True on success, false on failure.
1770
1701
 */
1771
1702
function delete_post_meta($post_id, $meta_key, $meta_value = '') {
1772
1703
        // make sure meta is added to the post, not a revision
1809
1740
 * @param string $meta_key Metadata key.
1810
1741
 * @param mixed $meta_value Metadata value.
1811
1742
 * @param mixed $prev_value Optional. Previous value to check before removing.
1812
 
 * @return bool False on failure, true if success.
 
1743
 * @return bool True on success, false on failure.
1813
1744
 */
1814
1745
function update_post_meta($post_id, $meta_key, $meta_value, $prev_value = '') {
1815
1746
        // make sure meta is added to the post, not a revision
2170
2101
        $and = wp_post_mime_type_where( $mime_type );
2171
2102
        $count = $wpdb->get_results( "SELECT post_mime_type, COUNT( * ) AS num_posts FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status != 'trash' $and GROUP BY post_mime_type", ARRAY_A );
2172
2103
 
2173
 
        $stats = array( );
 
2104
        $stats = array();
2174
2105
        foreach( (array) $count as $row ) {
2175
2106
                $stats[$row['post_mime_type']] = $row['num_posts'];
2176
2107
        }
2458
2389
 * @uses do_action() on 'trash_post_comments' before trashing
2459
2390
 * @uses do_action() on 'trashed_post_comments' after trashing
2460
2391
 *
2461
 
 * @param int $post Post ID or object.
 
2392
 * @param int|object $post Post ID or object.
2462
2393
 * @return mixed False on failure
2463
2394
 */
2464
2395
function wp_trash_post_comments($post = null) {
2499
2430
 * @uses do_action() on 'untrash_post_comments' before trashing
2500
2431
 * @uses do_action() on 'untrashed_post_comments' after trashing
2501
2432
 *
2502
 
 * @param int $post Post ID or object.
 
2433
 * @param int|object $post Post ID or object.
2503
2434
 * @return mixed False on failure
2504
2435
 */
2505
2436
function wp_untrash_post_comments($post = null) {
2662
2593
 *
2663
2594
 * If the $postarr parameter has 'ID' set to a value, then post will be updated.
2664
2595
 *
2665
 
 * You can set the post date manually, but setting the values for 'post_date'
 
2596
 * You can set the post date manually, by setting the values for 'post_date'
2666
2597
 * and 'post_date_gmt' keys. You can close the comments or open the comments by
2667
2598
 * setting the value for 'comment_status' key.
2668
2599
 *
2713
2644
        extract($postarr, EXTR_SKIP);
2714
2645
 
2715
2646
        // Are we updating or creating?
 
2647
        $post_ID = 0;
2716
2648
        $update = false;
2717
 
        if ( !empty($ID) ) {
 
2649
        if ( ! empty( $ID ) ) {
2718
2650
                $update = true;
 
2651
 
 
2652
                // Get the post ID and GUID
 
2653
                $post_ID = $ID;
 
2654
                $post_before = get_post( $post_ID );
 
2655
                if ( is_null( $post_before ) ) {
 
2656
                        if ( $wp_error )
 
2657
                                return new WP_Error( 'invalid_post', __( 'Invalid post ID.' ) );
 
2658
                        return 0;
 
2659
                }
 
2660
 
 
2661
                $guid = get_post_field( 'guid', $post_ID );
2719
2662
                $previous_status = get_post_field('post_status', $ID);
2720
2663
        } else {
2721
2664
                $previous_status = 'new';
2723
2666
 
2724
2667
        $maybe_empty = ! $post_content && ! $post_title && ! $post_excerpt && post_type_supports( $post_type, 'editor' )
2725
2668
                && post_type_supports( $post_type, 'title' ) && post_type_supports( $post_type, 'excerpt' );
 
2669
 
2726
2670
        if ( apply_filters( 'wp_insert_post_empty_content', $maybe_empty, $postarr ) ) {
2727
2671
                if ( $wp_error )
2728
2672
                        return new WP_Error( 'empty_content', __( 'Content, title, and excerpt are empty.' ) );
2751
2695
        if ( empty($post_author) )
2752
2696
                $post_author = $user_ID;
2753
2697
 
2754
 
        $post_ID = 0;
2755
 
 
2756
 
        // Get the post ID and GUID
2757
 
        if ( $update ) {
2758
 
                $post_ID = (int) $ID;
2759
 
                $guid = get_post_field( 'guid', $post_ID );
2760
 
                $post_before = get_post($post_ID);
2761
 
        }
2762
 
 
2763
2698
        // Don't allow contributors to set the post slug for pending review posts
2764
2699
        if ( 'pending' == $post_status && !current_user_can( 'publish_posts' ) )
2765
2700
                $post_name = '';
2859
2794
        // expected_slashed (everything!)
2860
2795
        $data = compact( array( 'post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_content_filtered', 'post_title', 'post_excerpt', 'post_status', 'post_type', 'comment_status', 'ping_status', 'post_password', 'post_name', 'to_ping', 'pinged', 'post_modified', 'post_modified_gmt', 'post_parent', 'menu_order', 'guid' ) );
2861
2796
        $data = apply_filters('wp_insert_post_data', $data, $postarr);
2862
 
        $data = stripslashes_deep( $data );
 
2797
        $data = wp_unslash( $data );
2863
2798
        $where = array( 'ID' => $post_ID );
2864
2799
 
2865
2800
        if ( $update ) {
2866
 
                do_action( 'pre_post_update', $post_ID );
 
2801
                do_action( 'pre_post_update', $post_ID, $data );
2867
2802
                if ( false === $wpdb->update( $wpdb->posts, $data, $where ) ) {
2868
2803
                        if ( $wp_error )
2869
2804
                                return new WP_Error('db_update_error', __('Could not update post in the database'), $wpdb->last_error);
2872
2807
                }
2873
2808
        } else {
2874
2809
                if ( isset($post_mime_type) )
2875
 
                        $data['post_mime_type'] = stripslashes( $post_mime_type ); // This isn't in the update
 
2810
                        $data['post_mime_type'] = wp_unslash( $post_mime_type ); // This isn't in the update
2876
2811
                // If there is a suggested ID, use it if not already present
2877
2812
                if ( !empty($import_id) ) {
2878
2813
                        $import_id = (int) $import_id;
2966
2901
        if ( is_object($postarr) ) {
2967
2902
                // non-escaped post was passed
2968
2903
                $postarr = get_object_vars($postarr);
2969
 
                $postarr = add_magic_quotes($postarr);
 
2904
                $postarr = wp_slash($postarr);
2970
2905
        }
2971
2906
 
2972
2907
        // First, get all of the original fields
2973
2908
        $post = get_post($postarr['ID'], ARRAY_A);
2974
2909
 
 
2910
        if ( is_null( $post ) ) {
 
2911
                if ( $wp_error )
 
2912
                        return new WP_Error( 'invalid_post', __( 'Invalid post ID.' ) );
 
2913
                return 0;
 
2914
        }
 
2915
 
2975
2916
        // Escape data pulled from DB.
2976
 
        $post = add_magic_quotes($post);
 
2917
        $post = wp_slash($post);
2977
2918
 
2978
2919
        // Passed post category list overwrites existing category list if not empty.
2979
2920
        if ( isset($postarr['post_category']) && is_array($postarr['post_category'])
3010
2951
 * @uses $wpdb
3011
2952
 * @uses do_action() Calls 'edit_post', 'save_post', and 'wp_insert_post' on post_id and post data.
3012
2953
 *
3013
 
 * @param mixed $post Post ID or object.
 
2954
 * @param int|object $post Post ID or object.
3014
2955
 */
3015
2956
function wp_publish_post( $post ) {
3016
2957
        global $wpdb;
3081
3022
 * @return string unique slug for the post, based on $post_name (with a -1, -2, etc. suffix)
3082
3023
 */
3083
3024
function wp_unique_post_slug( $slug, $post_ID, $post_status, $post_type, $post_parent ) {
3084
 
        if ( in_array( $post_status, array( 'draft', 'pending', 'auto-draft' ) ) )
 
3025
        if ( in_array( $post_status, array( 'draft', 'pending', 'auto-draft' ) ) || ( 'inherit' == $post_status && 'revision' == $post_type ) )
3085
3026
                return $slug;
3086
3027
 
3087
3028
        global $wpdb, $wp_rewrite;
3101
3042
                if ( $post_name_check || in_array( $slug, $feeds ) || apply_filters( 'wp_unique_post_slug_is_bad_attachment_slug', false, $slug ) ) {
3102
3043
                        $suffix = 2;
3103
3044
                        do {
3104
 
                                $alt_post_name = substr ($slug, 0, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix";
3105
 
                                $post_name_check = $wpdb->get_var( $wpdb->prepare($check_sql, $alt_post_name, $post_ID ) );
 
3045
                                $alt_post_name = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix";
 
3046
                                $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_ID ) );
3106
3047
                                $suffix++;
3107
3048
                        } while ( $post_name_check );
3108
3049
                        $slug = $alt_post_name;
3118
3059
                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 ) ) {
3119
3060
                        $suffix = 2;
3120
3061
                        do {
3121
 
                                $alt_post_name = substr( $slug, 0, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix";
 
3062
                                $alt_post_name = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix";
3122
3063
                                $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_ID, $post_parent ) );
3123
3064
                                $suffix++;
3124
3065
                        } while ( $post_name_check );
3132
3073
                if ( $post_name_check || in_array( $slug, $feeds ) || apply_filters( 'wp_unique_post_slug_is_bad_flat_slug', false, $slug, $post_type ) ) {
3133
3074
                        $suffix = 2;
3134
3075
                        do {
3135
 
                                $alt_post_name = substr( $slug, 0, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix";
 
3076
                                $alt_post_name = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix";
3136
3077
                                $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_type, $post_ID ) );
3137
3078
                                $suffix++;
3138
3079
                        } while ( $post_name_check );
3144
3085
}
3145
3086
 
3146
3087
/**
 
3088
 * Truncates a post slug.
 
3089
 *
 
3090
 * @since 3.6.0
 
3091
 * @access private
 
3092
 * @uses utf8_uri_encode() Makes sure UTF-8 characters are properly cut and encoded.
 
3093
 *
 
3094
 * @param string $slug The slug to truncate.
 
3095
 * @param int $length Max length of the slug.
 
3096
 * @return string The truncated slug.
 
3097
 */
 
3098
function _truncate_post_slug( $slug, $length = 200 ) {
 
3099
        if ( strlen( $slug ) > $length ) {
 
3100
                $decoded_slug = urldecode( $slug );
 
3101
                if ( $decoded_slug === $slug )
 
3102
                        $slug = substr( $slug, 0, $length );
 
3103
                else
 
3104
                        $slug = utf8_uri_encode( $decoded_slug, $length );
 
3105
        }
 
3106
 
 
3107
        return rtrim( $slug, '-' );
 
3108
}
 
3109
 
 
3110
/**
3147
3111
 * Adds tags to a post.
3148
3112
 *
3149
3113
 * @uses wp_set_post_tags() Same first two parameters, but the last parameter is always set to true.
3296
3260
        $new = implode("\n", $pung);
3297
3261
        $new = apply_filters('add_ping', $new);
3298
3262
        // expected_slashed ($new)
3299
 
        $new = stripslashes($new);
 
3263
        $new = wp_unslash($new);
3300
3264
        return $wpdb->update( $wpdb->posts, array( 'pinged' => $new ), array( 'ID' => $post_id ) );
3301
3265
}
3302
3266
 
3383
3347
                $excerpt = strip_tags($post_excerpt ? $post_excerpt : $post_content);
3384
3348
 
3385
3349
                if (strlen($excerpt) > 255) {
3386
 
                        $excerpt = substr($excerpt,0,252) . '...';
 
3350
                        $excerpt = substr($excerpt,0,252) . '&hellip;';
3387
3351
                }
3388
3352
 
3389
3353
                $trackback_urls = explode(',', $tb_list);
3390
3354
                foreach( (array) $trackback_urls as $tb_url) {
3391
3355
                        $tb_url = trim($tb_url);
3392
 
                        trackback($tb_url, stripslashes($post_title), $excerpt, $post_id);
 
3356
                        trackback($tb_url, wp_unslash($post_title), $excerpt, $post_id);
3393
3357
                }
3394
3358
        }
3395
3359
}
3453
3417
        $page_path = str_replace('%2F', '/', $page_path);
3454
3418
        $page_path = str_replace('%20', ' ', $page_path);
3455
3419
        $parts = explode( '/', trim( $page_path, '/' ) );
3456
 
        $parts = array_map( 'esc_sql', $parts );
 
3420
        $parts = esc_sql( $parts );
3457
3421
        $parts = array_map( 'sanitize_title_for_query', $parts );
3458
3422
 
3459
3423
        $in_string = "'". implode( "','", $parts ) . "'";
3460
 
        $post_type_sql = $post_type;
3461
 
        $wpdb->escape_by_ref( $post_type_sql );
 
3424
        $post_type_sql = esc_sql( $post_type );
3462
3425
        $pages = $wpdb->get_results( "SELECT ID, post_name, post_parent, post_type FROM $wpdb->posts WHERE post_name IN ($in_string) AND (post_type = '$post_type_sql' OR post_type = 'attachment')", OBJECT_K );
3463
3426
 
3464
3427
        $revparts = array_reverse( $parts );
3646
3609
        if ( array_diff( $post_status, get_post_stati() ) )
3647
3610
                return $pages;
3648
3611
 
3649
 
        $cache = array();
 
3612
        // $args can be whatever, only use the args defined in defaults to compute the key
3650
3613
        $key = md5( serialize( compact(array_keys($defaults)) ) );
3651
 
        if ( $cache = wp_cache_get( 'get_pages', 'posts' ) ) {
3652
 
                if ( is_array($cache) && isset( $cache[ $key ] ) && is_array( $cache[ $key ] ) ) {
3653
 
                        // Convert to WP_Post instances
3654
 
                        $pages = array_map( 'get_post', $cache[ $key ] );
3655
 
                        $pages = apply_filters( 'get_pages', $pages, $r );
3656
 
                        return $pages;
3657
 
                }
 
3614
        $last_changed = wp_cache_get( 'last_changed', 'posts' );
 
3615
        if ( ! $last_changed ) {
 
3616
                $last_changed = microtime();
 
3617
                wp_cache_set( 'last_changed', $last_changed, 'posts' );
 
3618
        }
 
3619
 
 
3620
        $cache_key = "get_pages:$key:$last_changed";
 
3621
        if ( $cache = wp_cache_get( $cache_key, 'posts' ) ) {
 
3622
                // Convert to WP_Post instances
 
3623
                $pages = array_map( 'get_post', $cache );
 
3624
                $pages = apply_filters('get_pages', $pages, $r);
 
3625
                return $pages;
3658
3626
        }
3659
3627
 
3660
3628
        if ( !is_array($cache) )
3724
3692
 
3725
3693
        $join = '';
3726
3694
        $where = "$exclusions $inclusions ";
3727
 
        if ( ! empty( $meta_key ) || ! empty( $meta_value ) ) {
 
3695
        if ( '' !== $meta_key || '' !== $meta_value ) {
3728
3696
                $join = " LEFT JOIN $wpdb->postmeta ON ( $wpdb->posts.ID = $wpdb->postmeta.post_id )";
3729
3697
 
3730
3698
                // meta_key and meta_value might be slashed
3731
 
                $meta_key = stripslashes($meta_key);
3732
 
                $meta_value = stripslashes($meta_value);
3733
 
                if ( ! empty( $meta_key ) )
 
3699
                $meta_key = wp_unslash($meta_key);
 
3700
                $meta_value = wp_unslash($meta_value);
 
3701
                if ( '' !== $meta_key )
3734
3702
                        $where .= $wpdb->prepare(" AND $wpdb->postmeta.meta_key = %s", $meta_key);
3735
 
                if ( ! empty( $meta_value ) )
 
3703
                if ( '' !== $meta_value )
3736
3704
                        $where .= $wpdb->prepare(" AND $wpdb->postmeta.meta_value = %s", $meta_value);
3737
3705
 
3738
3706
        }
3824
3792
                }
3825
3793
        }
3826
3794
 
3827
 
        $cache[ $key ] = $pages;
3828
 
        wp_cache_set( 'get_pages', $cache, 'posts' );
 
3795
        $page_structure = array();
 
3796
        foreach ( $pages as $page )
 
3797
                $page_structure[] = $page->ID;
 
3798
 
 
3799
        wp_cache_set( $cache_key, $page_structure, 'posts' );
3829
3800
 
3830
3801
        // Convert to WP_Post instances
3831
3802
        $pages = array_map( 'get_post', $pages );
3905
3876
        global $wpdb, $user_ID;
3906
3877
 
3907
3878
        $defaults = array('post_status' => 'inherit', 'post_type' => 'post', 'post_author' => $user_ID,
3908
 
                'ping_status' => get_option('default_ping_status'), 'post_parent' => 0,
 
3879
                'ping_status' => get_option('default_ping_status'), 'post_parent' => 0, 'post_title' => '',
3909
3880
                'menu_order' => 0, 'to_ping' =>  '', 'pinged' => '', 'post_password' => '',
3910
3881
                'guid' => '', 'post_content_filtered' => '', 'post_excerpt' => '', 'import_id' => 0, 'context' => '');
3911
3882
 
3996
3967
 
3997
3968
        // expected_slashed (everything!)
3998
3969
        $data = compact( array( 'post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_content_filtered', 'post_title', 'post_excerpt', 'post_status', 'post_type', 'comment_status', 'ping_status', 'post_password', 'post_name', 'to_ping', 'pinged', 'post_modified', 'post_modified_gmt', 'post_parent', 'menu_order', 'post_mime_type', 'guid' ) );
3999
 
        $data = stripslashes_deep( $data );
 
3970
        $data = wp_unslash( $data );
4000
3971
 
4001
3972
        if ( $update ) {
4002
3973
                $wpdb->update( $wpdb->posts, $data, array( 'ID' => $post_ID ) );
4632
4603
 *
4633
4604
 * @uses do_action() Calls 'clean_post_cache' on $id before adding children (if any).
4634
4605
 *
4635
 
 * @param object|int $post The post object or ID to remove from the cache
 
4606
 * @param int|object $post Post ID or object to remove from the cache
4636
4607
 */
4637
4608
function clean_post_cache( $post ) {
4638
4609
        global $_wp_suspend_cache_invalidation, $wpdb;
4660
4631
                wp_cache_delete( 'all_page_ids', 'posts' );
4661
4632
                do_action( 'clean_page_cache', $post->ID );
4662
4633
        }
 
4634
 
 
4635
        wp_cache_set( 'last_changed', microtime(), 'posts' );
4663
4636
}
4664
4637
 
4665
4638
/**
4820
4793
 *   wp_transition_post_status() and the default filter for _future_post_hook().
4821
4794
 * @param object $post Object type containing the post information
4822
4795
 */
4823
 
function _future_post_hook( $deprecated = '', $post ) {
 
4796
function _future_post_hook( $deprecated, $post ) {
4824
4797
        wp_clear_scheduled_hook( 'publish_future_post', array( $post->ID ) );
4825
4798
        wp_schedule_single_event( strtotime( get_gmt_from_date( $post->post_date ) . ' GMT') , 'publish_future_post', array( $post->ID ) );
4826
4799
}
4830
4803
 *
4831
4804
 * @since 2.3.0
4832
4805
 * @access private
4833
 
 * @uses $wpdb
4834
 
 * @uses XMLRPC_REQUEST constant.
4835
 
 * @uses do_action() Calls 'xmlprc_publish_post' on post ID if XMLRPC_REQUEST is defined.
 
4806
 * @uses XMLRPC_REQUEST and WP_IMPORTING constants.
 
4807
 * @uses do_action() Calls 'xmlrpc_publish_post' on post ID if XMLRPC_REQUEST is defined.
4836
4808
 *
4837
4809
 * @param int $post_id The ID in the database table of the post being published
4838
4810
 */
4839
4811
function _publish_post_hook($post_id) {
4840
 
        global $wpdb;
4841
 
 
4842
4812
        if ( defined('XMLRPC_REQUEST') )
4843
4813
                do_action('xmlrpc_publish_post', $post_id);
4844
4814
 
4853
4823
}
4854
4824
 
4855
4825
/**
4856
 
 * Determines which fields of posts are to be saved in revisions.
4857
 
 *
4858
 
 * Does two things. If passed a post *array*, it will return a post array ready
4859
 
 * to be inserted into the posts table as a post revision. Otherwise, returns
4860
 
 * an array whose keys are the post fields to be saved for post revisions.
4861
 
 *
4862
 
 * @package WordPress
4863
 
 * @subpackage Post_Revisions
4864
 
 * @since 2.6.0
4865
 
 * @access private
4866
 
 * @uses apply_filters() Calls '_wp_post_revision_fields' on 'title', 'content' and 'excerpt' fields.
4867
 
 *
4868
 
 * @param array $post Optional a post array to be processed for insertion as a post revision.
4869
 
 * @param bool $autosave optional Is the revision an autosave?
4870
 
 * @return array Post array ready to be inserted as a post revision or array of fields that can be versioned.
4871
 
 */
4872
 
function _wp_post_revision_fields( $post = null, $autosave = false ) {
4873
 
        static $fields = false;
4874
 
 
4875
 
        if ( !$fields ) {
4876
 
                // Allow these to be versioned
4877
 
                $fields = array(
4878
 
                        'post_title' => __( 'Title' ),
4879
 
                        'post_content' => __( 'Content' ),
4880
 
                        'post_excerpt' => __( 'Excerpt' ),
4881
 
                );
4882
 
 
4883
 
                // Runs only once
4884
 
                $fields = apply_filters( '_wp_post_revision_fields', $fields );
4885
 
 
4886
 
                // WP uses these internally either in versioning or elsewhere - they cannot be versioned
4887
 
                foreach ( array( 'ID', 'post_name', 'post_parent', 'post_date', 'post_date_gmt', 'post_status', 'post_type', 'comment_count', 'post_author' ) as $protect )
4888
 
                        unset( $fields[$protect] );
4889
 
        }
4890
 
 
4891
 
        if ( !is_array($post) )
4892
 
                return $fields;
4893
 
 
4894
 
        $return = array();
4895
 
        foreach ( array_intersect( array_keys( $post ), array_keys( $fields ) ) as $field )
4896
 
                $return[$field] = $post[$field];
4897
 
 
4898
 
        $return['post_parent']   = $post['ID'];
4899
 
        $return['post_status']   = 'inherit';
4900
 
        $return['post_type']     = 'revision';
4901
 
        $return['post_name']     = $autosave ? "$post[ID]-autosave" : "$post[ID]-revision";
4902
 
        $return['post_date']     = isset($post['post_modified']) ? $post['post_modified'] : '';
4903
 
        $return['post_date_gmt'] = isset($post['post_modified_gmt']) ? $post['post_modified_gmt'] : '';
4904
 
 
4905
 
        return $return;
4906
 
}
4907
 
 
4908
 
/**
4909
 
 * Saves an already existing post as a post revision.
4910
 
 *
4911
 
 * Typically used immediately prior to post updates.
4912
 
 *
4913
 
 * @package WordPress
4914
 
 * @subpackage Post_Revisions
4915
 
 * @since 2.6.0
4916
 
 *
4917
 
 * @uses _wp_put_post_revision()
4918
 
 *
4919
 
 * @param int $post_id The ID of the post to save as a revision.
4920
 
 * @return mixed Null or 0 if error, new revision ID, if success.
4921
 
 */
4922
 
function wp_save_post_revision( $post_id ) {
4923
 
        // We do autosaves manually with wp_create_post_autosave()
4924
 
        if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
4925
 
                return;
4926
 
 
4927
 
        // WP_POST_REVISIONS = 0, false
4928
 
        if ( ! WP_POST_REVISIONS )
4929
 
                return;
4930
 
 
4931
 
        if ( !$post = get_post( $post_id, ARRAY_A ) )
4932
 
                return;
4933
 
 
4934
 
        if ( 'auto-draft' == $post['post_status'] )
4935
 
                return;
4936
 
 
4937
 
        if ( !post_type_supports($post['post_type'], 'revisions') )
4938
 
                return;
4939
 
 
4940
 
        $return = _wp_put_post_revision( $post );
4941
 
 
4942
 
        // WP_POST_REVISIONS = true (default), -1
4943
 
        if ( !is_numeric( WP_POST_REVISIONS ) || WP_POST_REVISIONS < 0 )
4944
 
                return $return;
4945
 
 
4946
 
        // all revisions and (possibly) one autosave
4947
 
        $revisions = wp_get_post_revisions( $post_id, array( 'order' => 'ASC' ) );
4948
 
 
4949
 
        // WP_POST_REVISIONS = (int) (# of autosaves to save)
4950
 
        $delete = count($revisions) - WP_POST_REVISIONS;
4951
 
 
4952
 
        if ( $delete < 1 )
4953
 
                return $return;
4954
 
 
4955
 
        $revisions = array_slice( $revisions, 0, $delete );
4956
 
 
4957
 
        for ( $i = 0; isset($revisions[$i]); $i++ ) {
4958
 
                if ( false !== strpos( $revisions[$i]->post_name, 'autosave' ) )
4959
 
                        continue;
4960
 
                wp_delete_post_revision( $revisions[$i]->ID );
4961
 
        }
4962
 
 
4963
 
        return $return;
4964
 
}
4965
 
 
4966
 
/**
4967
 
 * Retrieve the autosaved data of the specified post.
4968
 
 *
4969
 
 * Returns a post object containing the information that was autosaved for the
4970
 
 * specified post.
4971
 
 *
4972
 
 * @package WordPress
4973
 
 * @subpackage Post_Revisions
4974
 
 * @since 2.6.0
4975
 
 *
4976
 
 * @param int $post_id The post ID.
4977
 
 * @return object|bool The autosaved data or false on failure or when no autosave exists.
4978
 
 */
4979
 
function wp_get_post_autosave( $post_id ) {
4980
 
 
4981
 
        if ( !$post = get_post( $post_id ) )
4982
 
                return false;
4983
 
 
4984
 
        $q = array(
4985
 
                'name' => "{$post->ID}-autosave",
4986
 
                'post_parent' => $post->ID,
4987
 
                'post_type' => 'revision',
4988
 
                'post_status' => 'inherit'
4989
 
        );
4990
 
 
4991
 
        // Use WP_Query so that the result gets cached
4992
 
        $autosave_query = new WP_Query;
4993
 
 
4994
 
        add_action( 'parse_query', '_wp_get_post_autosave_hack' );
4995
 
        $autosave = $autosave_query->query( $q );
4996
 
        remove_action( 'parse_query', '_wp_get_post_autosave_hack' );
4997
 
 
4998
 
        if ( $autosave && is_array($autosave) && is_object($autosave[0]) )
4999
 
                return $autosave[0];
5000
 
 
5001
 
        return false;
5002
 
}
5003
 
 
5004
 
/**
5005
 
 * Internally used to hack WP_Query into submission.
5006
 
 *
5007
 
 * @package WordPress
5008
 
 * @subpackage Post_Revisions
5009
 
 * @since 2.6.0
5010
 
 *
5011
 
 * @param object $query WP_Query object
5012
 
 */
5013
 
function _wp_get_post_autosave_hack( $query ) {
5014
 
        $query->is_single = false;
5015
 
}
5016
 
 
5017
 
/**
5018
 
 * Determines if the specified post is a revision.
5019
 
 *
5020
 
 * @package WordPress
5021
 
 * @subpackage Post_Revisions
5022
 
 * @since 2.6.0
5023
 
 *
5024
 
 * @param int|object $post Post ID or post object.
5025
 
 * @return bool|int False if not a revision, ID of revision's parent otherwise.
5026
 
 */
5027
 
function wp_is_post_revision( $post ) {
5028
 
        if ( !$post = wp_get_post_revision( $post ) )
5029
 
                return false;
5030
 
        return (int) $post->post_parent;
5031
 
}
5032
 
 
5033
 
/**
5034
 
 * Determines if the specified post is an autosave.
5035
 
 *
5036
 
 * @package WordPress
5037
 
 * @subpackage Post_Revisions
5038
 
 * @since 2.6.0
5039
 
 *
5040
 
 * @param int|object $post Post ID or post object.
5041
 
 * @return bool|int False if not a revision, ID of autosave's parent otherwise
5042
 
 */
5043
 
function wp_is_post_autosave( $post ) {
5044
 
        if ( !$post = wp_get_post_revision( $post ) )
5045
 
                return false;
5046
 
        if ( "{$post->post_parent}-autosave" !== $post->post_name )
5047
 
                return false;
5048
 
        return (int) $post->post_parent;
5049
 
}
5050
 
 
5051
 
/**
5052
 
 * Inserts post data into the posts table as a post revision.
5053
 
 *
5054
 
 * @package WordPress
5055
 
 * @subpackage Post_Revisions
5056
 
 * @since 2.6.0
5057
 
 *
5058
 
 * @uses wp_insert_post()
5059
 
 *
5060
 
 * @param int|object|array $post Post ID, post object OR post array.
5061
 
 * @param bool $autosave Optional. Is the revision an autosave?
5062
 
 * @return mixed Null or 0 if error, new revision ID if success.
5063
 
 */
5064
 
function _wp_put_post_revision( $post = null, $autosave = false ) {
5065
 
        if ( is_object($post) )
5066
 
                $post = get_object_vars( $post );
5067
 
        elseif ( !is_array($post) )
5068
 
                $post = get_post($post, ARRAY_A);
5069
 
        if ( !$post || empty($post['ID']) )
5070
 
                return;
5071
 
 
5072
 
        if ( isset($post['post_type']) && 'revision' == $post['post_type'] )
5073
 
                return new WP_Error( 'post_type', __( 'Cannot create a revision of a revision' ) );
5074
 
 
5075
 
        $post = _wp_post_revision_fields( $post, $autosave );
5076
 
        $post = add_magic_quotes($post); //since data is from db
5077
 
 
5078
 
        $revision_id = wp_insert_post( $post );
5079
 
        if ( is_wp_error($revision_id) )
5080
 
                return $revision_id;
5081
 
 
5082
 
        if ( $revision_id )
5083
 
                do_action( '_wp_put_post_revision', $revision_id );
5084
 
        return $revision_id;
5085
 
}
5086
 
 
5087
 
/**
5088
 
 * Gets a post revision.
5089
 
 *
5090
 
 * @package WordPress
5091
 
 * @subpackage Post_Revisions
5092
 
 * @since 2.6.0
5093
 
 *
5094
 
 * @uses get_post()
5095
 
 *
5096
 
 * @param int|object $post Post ID or post object
5097
 
 * @param string $output Optional. OBJECT, ARRAY_A, or ARRAY_N.
5098
 
 * @param string $filter Optional sanitation filter. @see sanitize_post()
5099
 
 * @return mixed Null if error or post object if success
5100
 
 */
5101
 
function wp_get_post_revision(&$post, $output = OBJECT, $filter = 'raw') {
5102
 
        $null = null;
5103
 
        if ( !$revision = get_post( $post, OBJECT, $filter ) )
5104
 
                return $revision;
5105
 
        if ( 'revision' !== $revision->post_type )
5106
 
                return $null;
5107
 
 
5108
 
        if ( $output == OBJECT ) {
5109
 
                return $revision;
5110
 
        } elseif ( $output == ARRAY_A ) {
5111
 
                $_revision = get_object_vars($revision);
5112
 
                return $_revision;
5113
 
        } elseif ( $output == ARRAY_N ) {
5114
 
                $_revision = array_values(get_object_vars($revision));
5115
 
                return $_revision;
5116
 
        }
5117
 
 
5118
 
        return $revision;
5119
 
}
5120
 
 
5121
 
/**
5122
 
 * Restores a post to the specified revision.
5123
 
 *
5124
 
 * Can restore a past revision using all fields of the post revision, or only selected fields.
5125
 
 *
5126
 
 * @package WordPress
5127
 
 * @subpackage Post_Revisions
5128
 
 * @since 2.6.0
5129
 
 *
5130
 
 * @uses wp_get_post_revision()
5131
 
 * @uses wp_update_post()
5132
 
 * @uses do_action() Calls 'wp_restore_post_revision' on post ID and revision ID if wp_update_post()
5133
 
 *  is successful.
5134
 
 *
5135
 
 * @param int|object $revision_id Revision ID or revision object.
5136
 
 * @param array $fields Optional. What fields to restore from. Defaults to all.
5137
 
 * @return mixed Null if error, false if no fields to restore, (int) post ID if success.
5138
 
 */
5139
 
function wp_restore_post_revision( $revision_id, $fields = null ) {
5140
 
        if ( !$revision = wp_get_post_revision( $revision_id, ARRAY_A ) )
5141
 
                return $revision;
5142
 
 
5143
 
        if ( !is_array( $fields ) )
5144
 
                $fields = array_keys( _wp_post_revision_fields() );
5145
 
 
5146
 
        $update = array();
5147
 
        foreach( array_intersect( array_keys( $revision ), $fields ) as $field )
5148
 
                $update[$field] = $revision[$field];
5149
 
 
5150
 
        if ( !$update )
5151
 
                return false;
5152
 
 
5153
 
        $update['ID'] = $revision['post_parent'];
5154
 
 
5155
 
        $update = add_magic_quotes( $update ); //since data is from db
5156
 
 
5157
 
        $post_id = wp_update_post( $update );
5158
 
        if ( is_wp_error( $post_id ) )
5159
 
                return $post_id;
5160
 
 
5161
 
        if ( $post_id )
5162
 
                do_action( 'wp_restore_post_revision', $post_id, $revision['ID'] );
5163
 
 
5164
 
        return $post_id;
5165
 
}
5166
 
 
5167
 
/**
5168
 
 * Deletes a revision.
5169
 
 *
5170
 
 * Deletes the row from the posts table corresponding to the specified revision.
5171
 
 *
5172
 
 * @package WordPress
5173
 
 * @subpackage Post_Revisions
5174
 
 * @since 2.6.0
5175
 
 *
5176
 
 * @uses wp_get_post_revision()
5177
 
 * @uses wp_delete_post()
5178
 
 *
5179
 
 * @param int|object $revision_id Revision ID or revision object.
5180
 
 * @return mixed Null or WP_Error if error, deleted post if success.
5181
 
 */
5182
 
function wp_delete_post_revision( $revision_id ) {
5183
 
        if ( !$revision = wp_get_post_revision( $revision_id ) )
5184
 
                return $revision;
5185
 
 
5186
 
        $delete = wp_delete_post( $revision->ID );
5187
 
        if ( is_wp_error( $delete ) )
5188
 
                return $delete;
5189
 
 
5190
 
        if ( $delete )
5191
 
                do_action( 'wp_delete_post_revision', $revision->ID, $revision );
5192
 
 
5193
 
        return $delete;
5194
 
}
5195
 
 
5196
 
/**
5197
 
 * Returns all revisions of specified post.
5198
 
 *
5199
 
 * @package WordPress
5200
 
 * @subpackage Post_Revisions
5201
 
 * @since 2.6.0
5202
 
 *
5203
 
 * @uses get_children()
5204
 
 *
5205
 
 * @param int|object $post_id Post ID or post object
5206
 
 * @return array empty if no revisions
5207
 
 */
5208
 
function wp_get_post_revisions( $post_id = 0, $args = null ) {
5209
 
        if ( ! WP_POST_REVISIONS )
5210
 
                return array();
5211
 
        if ( ( !$post = get_post( $post_id ) ) || empty( $post->ID ) )
5212
 
                return array();
5213
 
 
5214
 
        $defaults = array( 'order' => 'DESC', 'orderby' => 'date' );
5215
 
        $args = wp_parse_args( $args, $defaults );
5216
 
        $args = array_merge( $args, array( 'post_parent' => $post->ID, 'post_type' => 'revision', 'post_status' => 'inherit' ) );
5217
 
 
5218
 
        if ( !$revisions = get_children( $args ) )
5219
 
                return array();
5220
 
        return $revisions;
5221
 
}
5222
 
 
5223
 
function _set_preview($post) {
5224
 
 
5225
 
        if ( ! is_object($post) )
5226
 
                return $post;
5227
 
 
5228
 
        $preview = wp_get_post_autosave($post->ID);
5229
 
 
5230
 
        if ( ! is_object($preview) )
5231
 
                return $post;
5232
 
 
5233
 
        $preview = sanitize_post($preview);
5234
 
 
5235
 
        $post->post_content = $preview->post_content;
5236
 
        $post->post_title = $preview->post_title;
5237
 
        $post->post_excerpt = $preview->post_excerpt;
5238
 
 
5239
 
        return $post;
5240
 
}
5241
 
 
5242
 
function _show_post_preview() {
5243
 
 
5244
 
        if ( isset($_GET['preview_id']) && isset($_GET['preview_nonce']) ) {
5245
 
                $id = (int) $_GET['preview_id'];
5246
 
 
5247
 
                if ( false == wp_verify_nonce( $_GET['preview_nonce'], 'post_preview_' . $id ) )
5248
 
                        wp_die( __('You do not have permission to preview drafts.') );
5249
 
 
5250
 
                add_filter('the_preview', '_set_preview');
5251
 
        }
5252
 
}
5253
 
 
5254
 
/**
5255
4826
 * Returns the post's parent's post_ID
5256
4827
 *
5257
4828
 * @since 3.1.0
5311
4882
}
5312
4883
 
5313
4884
/**
5314
 
 * Returns an array of post format slugs to their translated and pretty display versions
5315
 
 *
5316
 
 * @since 3.1.0
5317
 
 *
5318
 
 * @return array The array of translations
5319
 
 */
5320
 
function get_post_format_strings() {
5321
 
        $strings = array(
5322
 
                'standard' => _x( 'Standard', 'Post format' ), // Special case. any value that evals to false will be considered standard
5323
 
                'aside'    => _x( 'Aside',    'Post format' ),
5324
 
                'chat'     => _x( 'Chat',     'Post format' ),
5325
 
                'gallery'  => _x( 'Gallery',  'Post format' ),
5326
 
                'link'     => _x( 'Link',     'Post format' ),
5327
 
                'image'    => _x( 'Image',    'Post format' ),
5328
 
                'quote'    => _x( 'Quote',    'Post format' ),
5329
 
                'status'   => _x( 'Status',   'Post format' ),
5330
 
                'video'    => _x( 'Video',    'Post format' ),
5331
 
                'audio'    => _x( 'Audio',    'Post format' ),
5332
 
        );
5333
 
        return $strings;
5334
 
}
5335
 
 
5336
 
/**
5337
 
 * Retrieves an array of post format slugs.
5338
 
 *
5339
 
 * @since 3.1.0
5340
 
 *
5341
 
 * @return array The array of post format slugs.
5342
 
 */
5343
 
function get_post_format_slugs() {
5344
 
        $slugs = array_keys( get_post_format_strings() );
5345
 
        return array_combine( $slugs, $slugs );
5346
 
}
5347
 
 
5348
 
/**
5349
 
 * Returns a pretty, translated version of a post format slug
5350
 
 *
5351
 
 * @since 3.1.0
5352
 
 *
5353
 
 * @param string $slug A post format slug
5354
 
 * @return string The translated post format name
5355
 
 */
5356
 
function get_post_format_string( $slug ) {
5357
 
        $strings = get_post_format_strings();
5358
 
        if ( !$slug )
5359
 
                return $strings['standard'];
5360
 
        else
5361
 
                return ( isset( $strings[$slug] ) ) ? $strings[$slug] : '';
5362
 
}
5363
 
 
5364
 
/**
5365
4885
 * Sets a post thumbnail.
5366
4886
 *
5367
4887
 * @since 3.1.0
5398
4918
}
5399
4919
 
5400
4920
/**
5401
 
 * Returns a link to a post format index.
5402
 
 *
5403
 
 * @since 3.1.0
5404
 
 *
5405
 
 * @param string $format Post format
5406
 
 * @return string Link
5407
 
 */
5408
 
function get_post_format_link( $format ) {
5409
 
        $term = get_term_by('slug', 'post-format-' . $format, 'post_format' );
5410
 
        if ( ! $term || is_wp_error( $term ) )
5411
 
                return false;
5412
 
        return get_term_link( $term );
5413
 
}
5414
 
 
5415
 
/**
5416
4921
 * Deletes auto-drafts for new posts that are > 7 days old
5417
4922
 *
5418
4923
 * @since 3.4.0
5427
4932
}
5428
4933
 
5429
4934
/**
5430
 
 * Filters the request to allow for the format prefix.
5431
 
 *
5432
 
 * @access private
5433
 
 * @since 3.1.0
5434
 
 */
5435
 
function _post_format_request( $qvs ) {
5436
 
        if ( ! isset( $qvs['post_format'] ) )
5437
 
                return $qvs;
5438
 
        $slugs = get_post_format_slugs();
5439
 
        if ( isset( $slugs[ $qvs['post_format'] ] ) )
5440
 
                $qvs['post_format'] = 'post-format-' . $slugs[ $qvs['post_format'] ];
5441
 
        $tax = get_taxonomy( 'post_format' );
5442
 
        if ( ! is_admin() )
5443
 
                $qvs['post_type'] = $tax->object_type;
5444
 
        return $qvs;
5445
 
}
5446
 
add_filter( 'request', '_post_format_request' );
5447
 
 
5448
 
/**
5449
 
 * Filters the post format term link to remove the format prefix.
5450
 
 *
5451
 
 * @access private
5452
 
 * @since 3.1.0
5453
 
 */
5454
 
function _post_format_link( $link, $term, $taxonomy ) {
5455
 
        global $wp_rewrite;
5456
 
        if ( 'post_format' != $taxonomy )
5457
 
                return $link;
5458
 
        if ( $wp_rewrite->get_extra_permastruct( $taxonomy ) ) {
5459
 
                return str_replace( "/{$term->slug}", '/' . str_replace( 'post-format-', '', $term->slug ), $link );
5460
 
        } else {
5461
 
                $link = remove_query_arg( 'post_format', $link );
5462
 
                return add_query_arg( 'post_format', str_replace( 'post-format-', '', $term->slug ), $link );
5463
 
        }
5464
 
}
5465
 
add_filter( 'term_link', '_post_format_link', 10, 3 );
5466
 
 
5467
 
/**
5468
 
 * Remove the post format prefix from the name property of the term object created by get_term().
5469
 
 *
5470
 
 * @access private
5471
 
 * @since 3.1.0
5472
 
 */
5473
 
function _post_format_get_term( $term ) {
5474
 
        if ( isset( $term->slug ) ) {
5475
 
                $term->name = get_post_format_string( str_replace( 'post-format-', '', $term->slug ) );
5476
 
        }
5477
 
        return $term;
5478
 
}
5479
 
add_filter( 'get_post_format', '_post_format_get_term' );
5480
 
 
5481
 
/**
5482
 
 * Remove the post format prefix from the name property of the term objects created by get_terms().
5483
 
 *
5484
 
 * @access private
5485
 
 * @since 3.1.0
5486
 
 */
5487
 
function _post_format_get_terms( $terms, $taxonomies, $args ) {
5488
 
        if ( in_array( 'post_format', (array) $taxonomies ) ) {
5489
 
                if ( isset( $args['fields'] ) && 'names' == $args['fields'] ) {
5490
 
                        foreach( $terms as $order => $name ) {
5491
 
                                $terms[$order] = get_post_format_string( str_replace( 'post-format-', '', $name ) );
5492
 
                        }
5493
 
                } else {
5494
 
                        foreach ( (array) $terms as $order => $term ) {
5495
 
                                if ( isset( $term->taxonomy ) && 'post_format' == $term->taxonomy ) {
5496
 
                                        $terms[$order]->name = get_post_format_string( str_replace( 'post-format-', '', $term->slug ) );
5497
 
                                }
5498
 
                        }
5499
 
                }
5500
 
        }
5501
 
        return $terms;
5502
 
}
5503
 
add_filter( 'get_terms', '_post_format_get_terms', 10, 3 );
5504
 
 
5505
 
/**
5506
 
 * Remove the post format prefix from the name property of the term objects created by wp_get_object_terms().
5507
 
 *
5508
 
 * @access private
5509
 
 * @since 3.1.0
5510
 
 */
5511
 
function _post_format_wp_get_object_terms( $terms ) {
5512
 
        foreach ( (array) $terms as $order => $term ) {
5513
 
                if ( isset( $term->taxonomy ) && 'post_format' == $term->taxonomy ) {
5514
 
                        $terms[$order]->name = get_post_format_string( str_replace( 'post-format-', '', $term->slug ) );
5515
 
                }
5516
 
        }
5517
 
        return $terms;
5518
 
}
5519
 
add_filter( 'wp_get_object_terms', '_post_format_wp_get_object_terms' );
5520
 
 
5521
 
/**
5522
4935
 * Update the custom taxonomies' term counts when a post's status is changed. For example, default posts term counts (for custom taxonomies) don't include private / draft posts.
5523
4936
 *
5524
4937
 * @access private