From 7564143444d617f0d6fb52672e2a50bbaa26c42e Mon Sep 17 00:00:00 2001 From: ryan Date: Tue, 5 Aug 2008 05:48:21 +0000 Subject: [PATCH] Sticky Posts, firct cut. see #7457 git-svn-id: http://svn.automattic.com/wordpress/trunk@8546 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/edit-form-advanced.php | 3 +- wp-admin/includes/post.php | 5 +++ wp-includes/post.php | 78 +++++++++++++++++++++++++++++++++ wp-includes/query.php | 36 +++++++++++++++ 4 files changed, 121 insertions(+), 1 deletion(-) diff --git a/wp-admin/edit-form-advanced.php b/wp-admin/edit-form-advanced.php index 94b21d909..76f01b406 100644 --- a/wp-admin/edit-form-advanced.php +++ b/wp-admin/edit-form-advanced.php @@ -117,7 +117,8 @@ if ( current_user_can('publish_posts') OR ( $post->post_status == 'publish' AND

-

+


+

ID ); + if ( !empty($_POST['sticky']) ) + stick_post($post_ID); + else + unstick_post($post_ID); + return $post_ID; } diff --git a/wp-includes/post.php b/wp-includes/post.php index 3c4bdebf1..7f7e7e7d3 100644 --- a/wp-includes/post.php +++ b/wp-includes/post.php @@ -482,6 +482,8 @@ function get_posts($args = null) { } elseif ( ! empty($r['exclude']) ) $r['post__not_in'] = preg_split('/[\s,]+/',$r['exclude']); + $r['caller_get_posts'] = true; + $get_posts = new WP_Query; return $get_posts->query($r); @@ -749,6 +751,30 @@ function get_post_custom_values( $key = '', $post_id = 0 ) { return $custom[$key]; } +/** + * is_sticky() - Check if post is sticky + * + * {@internal Missing Long Description}} + * + * @package WordPress + * @subpackage Post + * @since 2.7 + * + * @param int $post_id A post ID + * @return bool + */ +function is_sticky($post_id) { + $stickies = get_option('sticky_posts'); + + if ( !is_array($stickies) ) + return false; + + if ( in_array($post_id, $stickies) ) + return true; + + return false; +} + /** * sanitize_post() - Sanitize every post field * @@ -847,6 +873,58 @@ function sanitize_post_field($field, $value, $post_id, $context) { return $value; } +/** + * Make a post sticky + * + * Makes a post stick to the top of the front page + * + * @package WordPress + * @subpackage Post + * @since 2.7 + * + * @param int $post_id A post ID + */ +function stick_post($post_id) { + $stickies = get_option('sticky_posts'); + + if ( !is_array($stickies) ) + $stickies = array($post_id); + + if ( ! in_array($post_id, $stickies) ) + $stickies[] = $post_id; + + update_option('sticky_posts', $stickies); +} + +/** + * Unstick a post + * + * Unstick a post from the front page + * + * @package WordPress + * @subpackage Post + * @since 2.7 + * + * @param int $post_id A post ID + */ +function unstick_post($post_id) { + $stickies = get_option('sticky_posts'); + + if ( !is_array($stickies) ) + return; + + if ( ! in_array($post_id, $stickies) ) + return; + + $offset = array_search($post_id, $stickies); + if ( false === $offset ) + return; + + array_splice($stickies, $offset, 1); + + update_option('sticky_posts', $stickies); +} + /** * Count number of posts of a post type and is user has permissions to view. * diff --git a/wp-includes/query.php b/wp-includes/query.php index 4c096b2ec..f203c50a6 100644 --- a/wp-includes/query.php +++ b/wp-includes/query.php @@ -831,6 +831,9 @@ class WP_Query { $groupby = ''; $post_status_join = false; + if ( !isset($q['caller_get_posts']) ) + $q['caller_get_posts'] = false; + if ( !isset($q['post_type']) ) { if ( $this->is_search ) $q['post_type'] = 'any'; @@ -1497,6 +1500,39 @@ class WP_Query { } } + // Put sticky posts at the top of the posts array + $sticky_posts = get_option('sticky_posts'); + if ( $this->is_home && $page <= 1 && !empty($sticky_posts) && !$q['caller_get_posts'] ) { + $num_posts = count($this->posts); + $sticky_offset = 0; + // Loop over posts and relocate stickies to the front. + for ( $i = 0; $i < $num_posts; $i++ ) { + if ( in_array($this->posts[$i]->ID, $sticky_posts) ) { + $sticky_post = $this->posts[$i]; + // Remove sticky from current position + array_splice($this->posts, $i, 1); + // Move to front, after other stickies + array_splice($this->posts, $sticky_offset, 0, array($sticky_post)); + // Increment the sticky offset. The next sticky will be placed at this offset. + $sticky_offset++; + // Remove post from sticky posts array + $offset = array_search($sticky_post->ID, $sticky_posts); + array_splice($sticky_posts, $offset, 1); + } + } + + // Fetch sticky posts that weren't in the query results + $stickies__in = implode(',', array_map( 'absint', $sticky_posts )); + $stickies = $wpdb->get_results( "SELECT * FROM $wpdb->posts WHERE $wpdb->posts.ID IN ($stickies__in)" ); + // TODO Make sure post is published or viewable by the current user + foreach ( $stickies as $sticky_post ) { + if ( 'publish' != $sticky_post->post_status ) + continue; + array_splice($this->posts, $sticky_offset, 0, array($sticky_post)); + $sticky_offset++; + } + } + $this->posts = apply_filters('the_posts', $this->posts); update_post_caches($this->posts);