'Most Popular Posts',
'number' => 5,
'comment' => ' checked',
'zero' => ' checked',
'onlycheck' => ' checked',
'only' => 1,
'exclude' => 1,
'excludecheck' => ' checked',
'time' => '',
'duration' => '',
'parentclass' => '',
'childclass' => '',
'listclass' => '',
'reverse' => '',
);
class Most_Popular_Posts extends WP_Widget {
function Most_Popular_Posts() {
$widget_ops = array('description' => __( "Displays links to the posts with the most comments." ) );
$this->WP_Widget('most_popular_posts', __('Most Popular Posts'), $widget_ops);
}
function form($instance) {
global $defaults;
//load translations
$plugin_dir = basename(dirname(__FILE__));
load_plugin_textdomain( 'most-popular-posts', null, $plugin_dir );
// check if options are saved, otherwise use defaults
if (mpm_isEmpty($instance))
$instance = $defaults;
$title = esc_attr($instance['title']);
$number = esc_attr($instance['number']);
$comment = esc_attr($instance['comment']);
$zero = esc_attr($instance['zero']);
$onlycheck = esc_attr($instance['onlycheck']);
$only = esc_attr($instance['only']);
$excludecheck = esc_attr($instance['excludecheck']);
$exclude = esc_attr($instance['exclude']);
$time = esc_attr($instance['time']);
$timeunit = esc_attr($instance['duration']);
$childclass = esc_attr($instance['childclass']);
$reverse = esc_attr($instance['reverse']);
//create widget configuration panel
?>
/>
/>
only category option is respected.', 'most-popular-posts'); ?>
/>
get_field_name('only') . '&hide_empty=0'); ?>
/>
get_field_name('exclude') . '&hide_empty=0'); ?>
/>
Formatting
0 ';
//sort out the duration system
if (($instance['duration'] != 'all') && ($instance['time'] != 'all')) {
//get current time of Wordpress
$time = current_time('mysql', 1);
$period = $instance['duration'];
//new date to compare
$new_date = date('Y-m-d H:i:s', strtotime($time . "-" . $instance['time'] . " " . $instance['duration']));
$compare = " AND (post_date_gmt >= '" . $new_date . "')";
}
// determine count order
if ($instance['reverse'] == 'checked')
$reverse = 'ASC';
else
$reverse = 'DESC';
//get the post data from the database
if (($instance['excludecheck'] == 'checked') && ($instance['onlycheck'] == 'checked')) {
$query = "SELECT ID, post_title, comment_count, post_date FROM " . $wpdb->posts . " WHERE post_type = 'post' && post_status = 'publish' " . $zero . $compare . " ORDER BY comment_count " . $reverse . " LIMIT " . $instance['number'];
$posts = $wpdb->get_results($query);
}
else if (($instance['excludecheck'] != 'checked') && ($instance['onlycheck'] == 'checked')) {
$query = "select ID, post_title, comment_count, post_date from " . $wpdb->posts . " where ID in (select object_ID from " . $wpdb->term_relationships . " where " . $wpdb->term_relationships . ".term_taxonomy_id in (select term_taxonomy_id from " . $wpdb->term_taxonomy . " where term_id != " . $instance['exclude'] . " AND taxonomy = 'category')) AND post_type = 'post'" . $compare . " order by comment_count " . $reverse . " limit " . $instance['number'];
$posts = $wpdb->get_results($query);
}
else {
$query = "select ID, post_title, comment_count, post_date from " . $wpdb->posts . " where ID in (select object_ID from " . $wpdb->term_relationships . " where " . $wpdb->term_relationships . ".term_taxonomy_id = (select term_taxonomy_id from " . $wpdb->term_taxonomy . " where term_id = " . $instance['only'] . " AND taxonomy = 'category')) AND post_type = 'post'" . $compare . " order by comment_count " . $reverse . " limit " . $instance['number'];
$posts = $wpdb->get_results($query);
}
// start widget output
echo $before_widget . "\n";
echo $before_title . $instance['title'] . $after_title . "\n";
if ($instance['nestedlist'] == 'checked') {
if ($instance['parentclass'] == '')
echo '';
else
echo '';
}
if ($instance['childclass'] == '')
echo '';
else
echo '' . "\n";
//display each page as a link
if (!empty($posts)) {
foreach ($posts as $links) {
if ($instance['comment'] == 'checked')
$comments = ' (' . $links->comment_count . ')';
if ($instance['listclass'] != '')
$li_class = ' class="' . $instance['listclass'] . '"';
echo "\t" . '' . "\n";
}
}
else {
echo "\n\t" . '- ';
_e('No posts to display', 'most-popular-posts');
echo '
' . "\n";
}
?>