Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 115 lines (105 sloc) 3.569 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
<?php

/**
* @package anno
* This file is part of the Annotum theme for WordPress
* Built on the Carrington theme framework <http://carringtontheme.com>
*
* Copyright 2008-2011 Crowd Favorite, Ltd. All rights reserved. <http://crowdfavorite.com>
* Released under the GPL license
* http://www.opensource.org/licenses/gpl-license.php
*
* This file contains function wrappers for a few custom additions to the standard WordPress
* template tag milieu.
*/
if (__FILE__ == $_SERVER['SCRIPT_FILENAME']) { die(); }

class Anno_Widget_Recently extends WP_Widget {
public $key = 'anno_recently_html';
public $timeout = 3600; // 1 hour
public $enable_cache = true;
public $html_uid;
public $number = 5;

protected static $script_initiated = false;

public function __construct() {
$args = array(
'description' => __('Display the most recent posts and comments in a tabbed box.', 'anno'),
'classname' => 'widget-recent-posts'
);
parent::__construct('anno_recently', __('Recently&hellip;', 'anno'), $args);

$this->html_uid = uniqid('anno-recently');

// Clear widget cache on one of these important events
add_action( 'save_post', array($this, 'flush_widget_cache') );
add_action( 'deleted_post', array($this, 'flush_widget_cache') );
add_action( 'switch_theme', array($this, 'flush_widget_cache') );
add_action( 'comment_post', array($this, 'flush_widget_cache') );
add_action( 'transition_comment_status', array($this, 'flush_widget_cache') );
}

public function widget($args, $instance) {
extract($args);
$cache = get_transient($this->key);
if ($cache === false || $this->enable_cache === false) {
ob_start();
$this->cached($args, $instance);
$cache = ob_get_clean();
set_transient($this->key, $cache, $this->timeout);
}
echo $before_widget;
echo $cache;
echo $after_widget;
}

public function cached($args, $instance) { ?>
<div class="recently-container">
<div class="tabs">
<ul class="nav">
<li><a href="#p1-<?php echo $this->html_uid; ?>"><?php _e('Recent Articles', 'anno'); ?></a></li>
<li><a href="#p2-<?php echo $this->html_uid; ?>"><?php _e('Comments', 'anno'); ?></a></li>
</ul>
<div class="panel first-child" id="p1-<?php echo $this->html_uid; ?>">
<?php
$articles = new WP_Query(array(
'post_type' => 'article',
'posts_per_page' => $this->number
));
if ($articles->have_posts()) {
echo '<ol>';
while ($articles->have_posts()) {
$articles->the_post();
?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php
}
echo '</ol>';
wp_reset_postdata();
unset($articles);
}
?>
</div>
<div class="panel" id="p2-<?php echo $this->html_uid; ?>">
<?php
$comments = get_comments(array(
'number' => $this->number,
'status' => 'approve',
'post_status' => 'publish'
));
if (count($comments)) {
echo '<ul>';
foreach ((array) $comments as $comment) {
?>
<li class="recentcomments"><?php
/* translators: comments widget: 1: comment author, 2: post link */
printf(_x('%1$s on %2$s', '\'username\' on \'post title\'', 'anno'), get_comment_author_link($comment->comment_ID), '<a href="' . esc_url(get_comment_link($comment->comment_ID)) . '">' . get_the_title($comment->comment_post_ID) . '</a>');
?></li>
<?php
}
echo '</ul>';
}
?>
</div>
</div>
</div><!-- .recently-container -->
<?php
}

public function flush_widget_cache() {
delete_transient($this->key);
}
}
?>
Something went wrong with that request. Please try again.