+
+/**
+ * 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.
+ * @parem 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() {
+ // 3.2-early: use array_combine() and array_keys( get_post_format_strings() )
+ $slugs = array(
+ 'standard' => 'standard', // Special case. any value that evals to false will be considered standard
+ 'aside' => 'aside',
+ 'chat' => 'chat',
+ 'gallery' => 'gallery',
+ 'link' => 'link',
+ 'image' => 'image',
+ 'quote' => 'quote',
+ 'status' => 'status',
+ 'video' => 'video',
+ 'audio' => 'audio',
+ );
+ return $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 ) ) {
+ update_post_meta( $post->ID, '_thumbnail_id', $thumbnail_id );
+ return true;
+ }
+ }
+ return false;
+}
+
+/**
+ * Returns a link to a post format index.
+ *
+ * @since 3.1.0
+ *
+ * @param $format string 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 );
+}
+
+/**
+ * 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' );
+ $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' );
+
+?>
\ No newline at end of file