+
+/**
+ * Returns the post's parent's post_ID
+ *
+ * @since 3.1.0
+ *
+ * @param int $post_id
+ *
+ * @return int|bool false on error
+ */
+function wp_get_post_parent_id( $post_ID ) {
+ $post = get_post( $post_ID );
+ if ( !$post || is_wp_error( $post ) )
+ return false;
+ return (int) $post->post_parent;
+}
+
+/**
+ * Checks the given subset of the post hierarchy for hierarchy loops.
+ * Prevents loops from forming and breaks those that it finds.
+ *
+ * Attached to the wp_insert_post_parent filter.
+ *
+ * @since 3.1.0
+ * @uses wp_find_hierarchy_loop()
+ *
+ * @param int $post_parent ID of the parent for the post we're checking.
+ * @param int $post_ID ID of the post we're checking.
+ *
+ * @return int The new post_parent for the post.
+ */
+function wp_check_post_hierarchy_for_loops( $post_parent, $post_ID ) {
+ // Nothing fancy here - bail
+ if ( !$post_parent )
+ return 0;
+
+ // New post can't cause a loop
+ if ( empty( $post_ID ) )
+ return $post_parent;
+
+ // Can't be its own parent
+ if ( $post_parent == $post_ID )
+ return 0;
+
+ // Now look for larger loops
+
+ if ( !$loop = wp_find_hierarchy_loop( 'wp_get_post_parent_id', $post_ID, $post_parent ) )
+ return $post_parent; // No loop
+
+ // Setting $post_parent to the given value causes a loop
+ if ( isset( $loop[$post_ID] ) )
+ return 0;
+
+ // There's a loop, but it doesn't contain $post_ID. Break the loop.
+ foreach ( array_keys( $loop ) as $loop_member )
+ wp_update_post( array( 'ID' => $loop_member, 'post_parent' => 0 ) );
+
+ return $post_parent;
+}
+
+/**
+ * Returns an array of post format slugs to their translated and pretty display versions
+ *
+ * @since 3.1.0
+ *
+ * @return array The array of translations
+ */
+function get_post_format_strings() {
+ $strings = array(
+ 'standard' => _x( 'Standard', 'Post format' ), // Special case. any value that evals to false will be considered standard
+ 'aside' => _x( 'Aside', 'Post format' ),
+ 'chat' => _x( 'Chat', 'Post format' ),
+ 'gallery' => _x( 'Gallery', 'Post format' ),
+ 'link' => _x( 'Link', 'Post format' ),
+ 'image' => _x( 'Image', 'Post format' ),
+ 'quote' => _x( 'Quote', 'Post format' ),
+ 'status' => _x( 'Status', 'Post format' ),
+ 'video' => _x( 'Video', 'Post format' ),
+ 'audio' => _x( 'Audio', 'Post format' ),
+ );
+ return $strings;
+}
+
+/**
+ * Retrieves an array of post format slugs.
+ *
+ * @since 3.1.0
+ *
+ * @return array The array of post format slugs.
+ */
+function get_post_format_slugs() {
+ $slugs = array_keys( get_post_format_strings() );
+ return array_combine( $slugs, $slugs );
+}
+
+/**
+ * Returns a pretty, translated version of a post format slug
+ *
+ * @since 3.1.0
+ *
+ * @param string $slug A post format slug
+ * @return string The translated post format name
+ */
+function get_post_format_string( $slug ) {
+ $strings = get_post_format_strings();
+ if ( !$slug )
+ return $strings['standard'];
+ else
+ return ( isset( $strings[$slug] ) ) ? $strings[$slug] : '';
+}
+
+/**
+ * Sets a post thumbnail.
+ *
+ * @since 3.1.0
+ *
+ * @param int|object $post Post ID or object where thumbnail should be attached.
+ * @param int $thumbnail_id Thumbnail to attach.
+ * @return bool True on success, false on failure.
+ */
+function set_post_thumbnail( $post, $thumbnail_id ) {
+ $post = get_post( $post );
+ $thumbnail_id = absint( $thumbnail_id );
+ if ( $post && $thumbnail_id && get_post( $thumbnail_id ) ) {
+ $thumbnail_html = wp_get_attachment_image( $thumbnail_id, 'thumbnail' );
+ if ( ! empty( $thumbnail_html ) ) {
+ return update_post_meta( $post->ID, '_thumbnail_id', $thumbnail_id );
+ }
+ }
+ return false;
+}
+
+/**
+ * Removes a post thumbnail.
+ *
+ * @since 3.3.0
+ *
+ * @param int|object $post Post ID or object where thumbnail should be removed from.
+ * @return bool True on success, false on failure.
+ */
+function delete_post_thumbnail( $post ) {
+ $post = get_post( $post );
+ if ( $post )
+ return delete_post_meta( $post->ID, '_thumbnail_id' );
+ return false;
+}
+
+/**
+ * Returns a link to a post format index.
+ *
+ * @since 3.1.0
+ *
+ * @param string $format Post format
+ * @return string Link
+ */
+function get_post_format_link( $format ) {
+ $term = get_term_by('slug', 'post-format-' . $format, 'post_format' );
+ if ( ! $term || is_wp_error( $term ) )
+ return false;
+ return get_term_link( $term );
+}
+
+/**
+ * Deletes auto-drafts for new posts that are > 7 days old
+ *
+ * @since 3.4.0
+ */
+function wp_delete_auto_drafts() {
+ global $wpdb;
+
+ // Cleanup old auto-drafts more than 7 days old
+ $old_posts = $wpdb->get_col( "SELECT ID FROM $wpdb->posts WHERE post_status = 'auto-draft' AND DATE_SUB( NOW(), INTERVAL 7 DAY ) > post_date" );
+ foreach ( (array) $old_posts as $delete )
+ wp_delete_post( $delete, true ); // Force delete
+}
+
+/**
+ * Filters the request to allow for the format prefix.
+ *
+ * @access private
+ * @since 3.1.0
+ */
+function _post_format_request( $qvs ) {
+ if ( ! isset( $qvs['post_format'] ) )
+ return $qvs;
+ $slugs = get_post_format_slugs();
+ if ( isset( $slugs[ $qvs['post_format'] ] ) )
+ $qvs['post_format'] = 'post-format-' . $slugs[ $qvs['post_format'] ];
+ $tax = get_taxonomy( 'post_format' );
+ if ( ! is_admin() )
+ $qvs['post_type'] = $tax->object_type;
+ return $qvs;
+}
+add_filter( 'request', '_post_format_request' );
+
+/**
+ * Filters the post format term link to remove the format prefix.
+ *
+ * @access private
+ * @since 3.1.0
+ */
+function _post_format_link( $link, $term, $taxonomy ) {
+ global $wp_rewrite;
+ if ( 'post_format' != $taxonomy )
+ return $link;
+ if ( $wp_rewrite->get_extra_permastruct( $taxonomy ) ) {
+ return str_replace( "/{$term->slug}", '/' . str_replace( 'post-format-', '', $term->slug ), $link );
+ } else {
+ $link = remove_query_arg( 'post_format', $link );
+ return add_query_arg( 'post_format', str_replace( 'post-format-', '', $term->slug ), $link );
+ }
+}
+add_filter( 'term_link', '_post_format_link', 10, 3 );
+
+/**
+ * Remove the post format prefix from the name property of the term object created by get_term().
+ *
+ * @access private
+ * @since 3.1.0
+ */
+function _post_format_get_term( $term ) {
+ if ( isset( $term->slug ) ) {
+ $term->name = get_post_format_string( str_replace( 'post-format-', '', $term->slug ) );
+ }
+ return $term;
+}
+add_filter( 'get_post_format', '_post_format_get_term' );
+
+/**
+ * Remove the post format prefix from the name property of the term objects created by get_terms().
+ *
+ * @access private
+ * @since 3.1.0
+ */
+function _post_format_get_terms( $terms, $taxonomies, $args ) {
+ if ( in_array( 'post_format', (array) $taxonomies ) ) {
+ if ( isset( $args['fields'] ) && 'names' == $args['fields'] ) {
+ foreach( $terms as $order => $name ) {
+ $terms[$order] = get_post_format_string( str_replace( 'post-format-', '', $name ) );
+ }
+ } else {
+ foreach ( (array) $terms as $order => $term ) {
+ if ( isset( $term->taxonomy ) && 'post_format' == $term->taxonomy ) {
+ $terms[$order]->name = get_post_format_string( str_replace( 'post-format-', '', $term->slug ) );
+ }
+ }
+ }
+ }
+ return $terms;
+}
+add_filter( 'get_terms', '_post_format_get_terms', 10, 3 );
+
+/**
+ * Remove the post format prefix from the name property of the term objects created by wp_get_object_terms().
+ *
+ * @access private
+ * @since 3.1.0
+ */
+function _post_format_wp_get_object_terms( $terms ) {
+ foreach ( (array) $terms as $order => $term ) {
+ if ( isset( $term->taxonomy ) && 'post_format' == $term->taxonomy ) {
+ $terms[$order]->name = get_post_format_string( str_replace( 'post-format-', '', $term->slug ) );
+ }
+ }
+ return $terms;
+}
+add_filter( 'wp_get_object_terms', '_post_format_wp_get_object_terms' );
+
+/**
+ * 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.
+ *
+ * @access private
+ * @param string $new_status
+ * @param string $old_status
+ * @param object $post
+ * @since 3.3.0
+ */
+function _update_term_count_on_transition_post_status( $new_status, $old_status, $post ) {
+ // Update counts for the post's terms.
+ foreach ( (array) get_object_taxonomies( $post->post_type ) as $taxonomy ) {
+ $tt_ids = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'tt_ids' ) );
+ wp_update_term_count( $tt_ids, $taxonomy );
+ }
+}
+
+/**
+ * Adds any posts from the given ids to the cache that do not already exist in cache
+ *
+ * @since 3.4.0
+ *
+ * @access private
+ *
+ * @param array $post_ids ID list
+ * @param bool $update_term_cache Whether to update the term cache. Default is true.
+ * @param bool $update_meta_cache Whether to update the meta cache. Default is true.
+ */
+function _prime_post_caches( $ids, $update_term_cache = true, $update_meta_cache = true ) {
+ global $wpdb;
+
+ $non_cached_ids = _get_non_cached_ids( $ids, 'posts' );
+ if ( !empty( $non_cached_ids ) ) {
+ $fresh_posts = $wpdb->get_results( sprintf( "SELECT $wpdb->posts.* FROM $wpdb->posts WHERE ID IN (%s)", join( ",", $non_cached_ids ) ) );
+
+ update_post_caches( $fresh_posts, 'any', $update_term_cache, $update_meta_cache );
+ }
+}