Make WordPress Core

Changeset 8546


Ignore:
Timestamp:
08/05/2008 05:48:21 AM (17 years ago)
Author:
ryan
Message:

Sticky Posts, firct cut. see #7457

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/edit-form-advanced.php

    r8530 r8546  
    118118
    119119<?php if ( current_user_can( 'publish_posts' ) ) : ?>
    120 <p><label for="post_status_private" class="selectit"><input id="post_status_private" name="post_status" type="checkbox" value="private" <?php checked($post->post_status, 'private'); ?> tabindex="4" /> <?php _e('Keep this post private') ?></label></p>
     120<p><label for="post_status_private" class="selectit"><input id="post_status_private" name="post_status" type="checkbox" value="private" <?php checked($post->post_status, 'private'); ?> tabindex="4" /> <?php _e('Keep this post private') ?></label><br />
     121<label for="sticky" class="selectit"><input id="sticky" name="sticky" type="checkbox" value="sticky" <?php checked(is_sticky($post->ID), true); ?> tabindex="4" /> <?php _e('Stick this post to the front page') ?></label></p>
    121122<?php endif; ?>
    122123<?php
  • trunk/wp-admin/includes/post.php

    r8397 r8546  
    162162    wp_set_post_lock( $post_ID, $GLOBALS['current_user']->ID );
    163163
     164    if ( !empty($_POST['sticky']) )
     165        stick_post($post_ID);
     166    else
     167        unstick_post($post_ID);
     168
    164169    return $post_ID;
    165170}
  • trunk/wp-includes/post.php

    r8513 r8546  
    483483        $r['post__not_in'] = preg_split('/[\s,]+/',$r['exclude']);
    484484
     485    $r['caller_get_posts'] = true;
     486
    485487    $get_posts = new WP_Query;
    486488    return $get_posts->query($r);
     
    748750
    749751    return $custom[$key];
     752}
     753
     754/**
     755 * is_sticky() - Check if post is sticky
     756 *
     757 * {@internal Missing Long Description}}
     758 *
     759 * @package WordPress
     760 * @subpackage Post
     761 * @since 2.7
     762 *
     763 * @param int $post_id A post ID
     764 * @return bool
     765 */
     766function is_sticky($post_id) {
     767    $stickies = get_option('sticky_posts');
     768
     769    if ( !is_array($stickies) )
     770        return false;
     771
     772    if ( in_array($post_id, $stickies) )
     773        return true;
     774
     775    return false;
    750776}
    751777
     
    846872
    847873    return $value;
     874}
     875
     876/**
     877 * Make a post sticky
     878 *
     879 * Makes a post stick to the top of the front page
     880 *
     881 * @package WordPress
     882 * @subpackage Post
     883 * @since 2.7
     884 *
     885 * @param int $post_id A post ID
     886 */
     887function stick_post($post_id) {
     888    $stickies = get_option('sticky_posts');
     889
     890    if ( !is_array($stickies) )
     891        $stickies = array($post_id);
     892
     893    if ( ! in_array($post_id, $stickies) )
     894        $stickies[] = $post_id;
     895
     896    update_option('sticky_posts', $stickies);
     897}
     898
     899/**
     900 * Unstick a post
     901 *
     902 * Unstick a post from the front page
     903 *
     904 * @package WordPress
     905 * @subpackage Post
     906 * @since 2.7
     907 *
     908 * @param int $post_id A post ID
     909 */
     910function unstick_post($post_id) {
     911    $stickies = get_option('sticky_posts');
     912
     913    if ( !is_array($stickies) )
     914        return;
     915
     916    if ( ! in_array($post_id, $stickies) )
     917        return;
     918
     919    $offset = array_search($post_id, $stickies);
     920    if ( false === $offset )
     921        return;
     922
     923    array_splice($stickies, $offset, 1);
     924   
     925    update_option('sticky_posts', $stickies);
    848926}
    849927
  • trunk/wp-includes/query.php

    r8504 r8546  
    832832        $post_status_join = false;
    833833
     834        if ( !isset($q['caller_get_posts']) )
     835            $q['caller_get_posts'] = false;
     836
    834837        if ( !isset($q['post_type']) ) {
    835838            if ( $this->is_search )
     
    14951498                    }
    14961499                }
     1500            }
     1501        }
     1502
     1503        // Put sticky posts at the top of the posts array
     1504        $sticky_posts = get_option('sticky_posts');
     1505        if ( $this->is_home && $page <= 1 && !empty($sticky_posts) && !$q['caller_get_posts'] ) {
     1506            $num_posts = count($this->posts);
     1507            $sticky_offset = 0;
     1508            // Loop over posts and relocate stickies to the front.
     1509            for ( $i = 0; $i < $num_posts; $i++ ) {
     1510                if ( in_array($this->posts[$i]->ID, $sticky_posts) ) {
     1511                    $sticky_post = $this->posts[$i];
     1512                    // Remove sticky from current position
     1513                    array_splice($this->posts, $i, 1);
     1514                    // Move to front, after other stickies
     1515                    array_splice($this->posts, $sticky_offset, 0, array($sticky_post));
     1516                    // Increment the sticky offset.  The next sticky will be placed at this offset.
     1517                    $sticky_offset++;
     1518                    // Remove post from sticky posts array
     1519                    $offset = array_search($sticky_post->ID, $sticky_posts);
     1520                    array_splice($sticky_posts, $offset, 1);
     1521                }
     1522            }
     1523
     1524            // Fetch sticky posts that weren't in the query results
     1525            $stickies__in = implode(',', array_map( 'absint', $sticky_posts ));
     1526            $stickies = $wpdb->get_results( "SELECT * FROM $wpdb->posts WHERE $wpdb->posts.ID IN ($stickies__in)" );
     1527            // TODO Make sure post is published or viewable by the current user
     1528            foreach ( $stickies as $sticky_post ) {
     1529                if ( 'publish' != $sticky_post->post_status )
     1530                    continue;
     1531                array_splice($this->posts, $sticky_offset, 0, array($sticky_post));
     1532                $sticky_offset++;
    14971533            }
    14981534        }
Note: See TracChangeset for help on using the changeset viewer.