Example #1
0
    /**
     * @param array $args
     * @param array $instance
     */
    public function widget($args, $instance)
    {
        $cache = array();
        if (!$this->is_preview()) {
            $cache = hq_cache_get('widget_recent_posts', 'widget');
        }
        if (!is_array($cache)) {
            $cache = array();
        }
        if (!isset($args['widget_id'])) {
            $args['widget_id'] = $this->id;
        }
        if (isset($cache[$args['widget_id']])) {
            echo $cache[$args['widget_id']];
            return;
        }
        ob_start();
        $title = !empty($instance['title']) ? $instance['title'] : __('Recent Posts');
        /** This filter is documented in hq-includes/default-widgets.php */
        $title = apply_filters('widget_title', $title, $instance, $this->id_base);
        $number = !empty($instance['number']) ? absint($instance['number']) : 5;
        if (!$number) {
            $number = 5;
        }
        $show_date = isset($instance['show_date']) ? $instance['show_date'] : false;
        /**
         * Filter the arguments for the Recent Posts widget.
         *
         * @since 0.0.1
         *
         * @see HQ_Query::get_posts()
         *
         * @param array $args An array of arguments used to retrieve the recent posts.
         */
        $r = new HQ_Query(apply_filters('widget_posts_args', array('posts_per_page' => $number, 'no_found_rows' => true, 'post_status' => 'publish', 'ignore_sticky_posts' => true)));
        if ($r->have_posts()) {
            ?>
		<?php 
            echo $args['before_widget'];
            ?>
		<?php 
            if ($title) {
                echo $args['before_title'] . $title . $args['after_title'];
            }
            ?>
		<ul>
		<?php 
            while ($r->have_posts()) {
                $r->the_post();
                ?>
			<li>
				<a href="<?php 
                the_permalink();
                ?>
"><?php 
                get_the_title() ? the_title() : the_ID();
                ?>
</a>
			<?php 
                if ($show_date) {
                    ?>
				<span class="post-date"><?php 
                    echo get_the_date();
                    ?>
</span>
			<?php 
                }
                ?>
			</li>
		<?php 
            }
            ?>
		</ul>
		<?php 
            echo $args['after_widget'];
            // Reset the global $the_post as this query will have stomped on it
            hq_reset_postdata();
        }
        if (!$this->is_preview()) {
            $cache[$args['widget_id']] = ob_get_flush();
            hq_cache_set('widget_recent_posts', $cache, 'widget');
        } else {
            ob_end_flush();
        }
    }
Example #2
0
/**
 * Close comments on old posts on the fly, without any extra DB queries. Hooked to the_posts.
 *
 * @access private
 * @since 0.0.1
 *
 * @param HQ_Post  $posts Post data object.
 * @param HQ_Query $query Query object.
 * @return array
 */
function _close_comments_for_old_posts($posts, $query)
{
    if (empty($posts) || !$query->is_singular() || !get_option('close_comments_for_old_posts')) {
        return $posts;
    }
    /**
     * Filter the list of post types to automatically close comments for.
     *
     * @since 0.0.1
     *
     * @param array $post_types An array of registered post types. Default array with 'post'.
     */
    $post_types = apply_filters('close_comments_for_post_types', array('post'));
    if (!in_array($posts[0]->post_type, $post_types)) {
        return $posts;
    }
    $days_old = (int) get_option('close_comments_days_old');
    if (!$days_old) {
        return $posts;
    }
    if (time() - strtotime($posts[0]->post_date_gmt) > $days_old * DAY_IN_SECONDS) {
        $posts[0]->comment_status = 'closed';
        $posts[0]->ping_status = 'closed';
    }
    return $posts;
}
Example #3
0
/**
 * Retrieve list of latest posts or posts matching criteria.
 *
 * The defaults are as follows:
 *
 * @since 0.0.1
 *
 * @see HQ_Query::parse_query()
 *
 * @param array $args {
 *     Optional. Arguments to retrieve posts. {@see HQ_Query::parse_query()} for more
 *     available arguments.
 *
 *     @type int        $numberposts      Total number of posts to retrieve. Is an alias of $posts_per_page
 *                                        in HQ_Query. Accepts 1+ and -1 for all. Default 5.
 *     @type int        $offset           The number of posts to offset before retrieval. Default 0.
 *     @type int|string $category         Category ID or comma-separated list of IDs (this or any children).
 *                                        Is an alias of $cat in HQ_Query. Default 0.
 *     @type string     $orderby          Which field to order posts by. Accepts post fields. Default 'date'.
 *     @type array      $include          An array of post IDs to retrieve, sticky posts will be included.
 *                                        Is an alias of $post__in in HQ_Query. Default empty array.
 *     @type array      $exclude          An array of post IDs not to retrieve. Default empty array.
 *     @type string     $meta_key         Custom field key. Default empty.
 *     @type mixed      $meta_value       Custom field value. Default empty string.
 *     @type string     $post_type        Post type. Default 'post'.
 *     @type bool       $suppress_filters Whether to suppress filters. Default true.
 * }
 * @return array List of posts.
 */
function get_posts($args = null)
{
    $defaults = array('numberposts' => 5, 'offset' => 0, 'category' => 0, 'orderby' => 'date', 'order' => 'DESC', 'include' => array(), 'exclude' => array(), 'meta_key' => '', 'meta_value' => '', 'post_type' => 'post', 'suppress_filters' => true);
    $r = hq_parse_args($args, $defaults);
    if (empty($r['post_status'])) {
        $r['post_status'] = 'attachment' == $r['post_type'] ? 'inherit' : 'publish';
    }
    if (!empty($r['numberposts']) && empty($r['posts_per_page'])) {
        $r['posts_per_page'] = $r['numberposts'];
    }
    if (!empty($r['category'])) {
        $r['cat'] = $r['category'];
    }
    if (!empty($r['include'])) {
        $incposts = hq_parse_id_list($r['include']);
        $r['posts_per_page'] = count($incposts);
        // only the number of posts included
        $r['post__in'] = $incposts;
    } elseif (!empty($r['exclude'])) {
        $r['post__not_in'] = hq_parse_id_list($r['exclude']);
    }
    $r['ignore_sticky_posts'] = true;
    $r['no_found_rows'] = true;
    $get_posts = new HQ_Query();
    return $get_posts->query($r);
}