1 | <?php
|
---|
2 | /*
|
---|
3 | Plugin Name: BFA Popular in Cat
|
---|
4 | Plugin URI: http://wordpress.bytesforall.com/
|
---|
5 | Description: Configurable WordPress widget that displays the most popular posts in a given category based on the number of comments
|
---|
6 | Version: 1.0
|
---|
7 | Author: BFA Webdesign
|
---|
8 | Author URI: http://www.bytesforall.com/
|
---|
9 | */
|
---|
10 | /*
|
---|
11 | Based on Plugin "Most Commented" by Nick Momrik http://mtdewvirus.com/ Version 1.5
|
---|
12 | and the modification Last X days by DJ Chuang www.djchuang.com
|
---|
13 | */
|
---|
14 |
|
---|
15 |
|
---|
16 | // Check for the required plugin functions. This will prevent fatal
|
---|
17 | // errors occurring when you deactivate the dynamic-sidebar plugin.
|
---|
18 | if ( !function_exists('register_sidebar_widget') )
|
---|
19 | return;
|
---|
20 |
|
---|
21 | // This is the function that outputs our little widget
|
---|
22 | function widget_mdv_most_commented_per_cat($args) {
|
---|
23 | extract($args);
|
---|
24 |
|
---|
25 | if (is_category() ) {
|
---|
26 |
|
---|
27 | $cat_id = get_query_var('cat');
|
---|
28 |
|
---|
29 | // Fetch our parameters
|
---|
30 | $bfa_pic_options = get_option('widget_mdv_most_commented_per_cat');
|
---|
31 | $bfa_pic_title = $bfa_pic_options['bfa_pic_title'];
|
---|
32 | $bfa_pic_no_posts = $bfa_pic_options['bfa_pic_no_posts'];
|
---|
33 | $bfa_pic_duration = $bfa_pic_options['bfa_pic_duration'];
|
---|
34 | $bfa_pic_min_amount_comments = $bfa_pic_options['bfa_pic_min_amount_comments'];
|
---|
35 | $bfa_pic_prepend_cat_title = $bfa_pic_options['bfa_pic_prepend_cat_title'];
|
---|
36 | $bfa_pic_append_cat_title = $bfa_pic_options['bfa_pic_append_cat_title'];
|
---|
37 |
|
---|
38 | $current_cat_title = htmlentities(single_cat_title('', false),ENT_QUOTES);
|
---|
39 | if ($bfa_pic_prepend_cat_title == "on" ) { $bfa_pic_title = $current_cat_title . " " . $bfa_pic_title; }
|
---|
40 | if ($bfa_pic_append_cat_title == "on" ) { $bfa_pic_title = $bfa_pic_title . " " . $current_cat_title; }
|
---|
41 |
|
---|
42 | global $wpdb;
|
---|
43 |
|
---|
44 |
|
---|
45 | $bfa_pic_request = "SELECT DISTINCT ID, post_title, comment_count FROM $wpdb->posts as p";
|
---|
46 | $bfa_pic_request .= " INNER JOIN $wpdb->term_relationships AS tr ON";
|
---|
47 | $bfa_pic_request .= " (p.ID = tr.object_id AND";
|
---|
48 | $bfa_pic_request .= " tr.term_taxonomy_id = $cat_id )";
|
---|
49 | $bfa_pic_request .= " INNER JOIN $wpdb->term_taxonomy AS tt ON";
|
---|
50 | $bfa_pic_request .= " (tr.term_taxonomy_id = tt.term_taxonomy_id AND";
|
---|
51 | $bfa_pic_request .= " taxonomy = 'category')";
|
---|
52 | $bfa_pic_request .= " WHERE post_status = 'publish' AND comment_count >= $bfa_pic_min_amount_comments";
|
---|
53 | $bfa_pic_request .= " AND post_password =''";
|
---|
54 |
|
---|
55 | if ($bfa_pic_duration !="") $bfa_pic_request .= " AND DATE_SUB(CURDATE(),INTERVAL ".$bfa_pic_duration." DAY) < post_date ";
|
---|
56 |
|
---|
57 | $bfa_pic_request .= " ORDER BY comment_count DESC LIMIT $bfa_pic_no_posts";
|
---|
58 | $bfa_pic_posts = $wpdb->get_results($bfa_pic_request);
|
---|
59 |
|
---|
60 | if ($bfa_pic_posts) {
|
---|
61 | foreach ($bfa_pic_posts as $bfa_pic_post) {
|
---|
62 | $bfa_pic_post_title = stripslashes($bfa_pic_post->post_title);
|
---|
63 | $bfa_pic_comment_count = $bfa_pic_post->comment_count;
|
---|
64 | $bfa_pic_permalink = get_permalink($bfa_pic_post->ID);
|
---|
65 | $widget_mdv_most_commented_per_cat .= '<li><a href="' . $bfa_pic_permalink . '" title="' . $bfa_pic_post_title.'">' . $bfa_pic_post_title . ' (' . $bfa_pic_comment_count . ')</a></li>';
|
---|
66 | }
|
---|
67 | } else {
|
---|
68 | $widget_mdv_most_commented_per_cat = "None found";
|
---|
69 | }
|
---|
70 |
|
---|
71 |
|
---|
72 | if ($widget_mdv_most_commented_per_cat != "None found") {
|
---|
73 | echo $before_widget . $before_title . $bfa_pic_title . $after_title;
|
---|
74 | echo "<ul>" . $widget_mdv_most_commented_per_cat . "</ul>";
|
---|
75 | echo $after_widget;
|
---|
76 | } else { return $widget_mdv_most_commented_per_cat; }
|
---|
77 | }
|
---|
78 | }
|
---|
79 |
|
---|
80 |
|
---|
81 | // This is the function that outputs the form to let the users edit
|
---|
82 | // the widget's parameters.
|
---|
83 | function widget_mdv_most_commented_per_cat_control() {
|
---|
84 |
|
---|
85 | // Fetch the options, check them and if need be, update the options array
|
---|
86 | $bfa_pic_options = $bfa_pic_newoptions = get_option('widget_mdv_most_commented_per_cat');
|
---|
87 | if ( $_POST["bfa_pic_src-submit"] ) {
|
---|
88 | $bfa_pic_newoptions['bfa_pic_title'] = strip_tags(stripslashes($_POST["bfa_pic_src-title"]));
|
---|
89 | $bfa_pic_newoptions['bfa_pic_no_posts'] = (int) $_POST["bfa_pic_no_posts"];
|
---|
90 | $bfa_pic_newoptions['bfa_pic_duration'] = (int) $_POST["bfa_pic_duration"];
|
---|
91 | $bfa_pic_newoptions['bfa_pic_min_amount_comments'] = (int) $_POST["bfa_pic_min_amount_comments"];
|
---|
92 | $bfa_pic_newoptions['bfa_pic_append_cat_title'] = !isset($_POST["bfa_pic_append_cat_title"]) ? NULL : $_POST["bfa_pic_append_cat_title"];
|
---|
93 | $bfa_pic_newoptions['bfa_pic_prepend_cat_title'] = !isset($_POST["bfa_pic_prepend_cat_title"]) ? NULL : $_POST["bfa_pic_prepend_cat_title"];
|
---|
94 |
|
---|
95 | }
|
---|
96 | if ( $bfa_pic_options != $bfa_pic_newoptions ) {
|
---|
97 | $bfa_pic_options = $bfa_pic_newoptions;
|
---|
98 | update_option('widget_mdv_most_commented_per_cat', $bfa_pic_options);
|
---|
99 | }
|
---|
100 |
|
---|
101 | // Default options to the parameters
|
---|
102 | if ( !$bfa_pic_options['bfa_pic_no_posts'] ) $bfa_pic_options['bfa_pic_no_posts'] = 10;
|
---|
103 | if ( !$bfa_pic_options['bfa_pic_min_amount_comments'] OR $bfa_pic_options['bfa_pic_min_amount_comments'] == 0) $bfa_pic_options['bfa_pic_min_amount_comments'] = 1;
|
---|
104 |
|
---|
105 | $bfa_pic_no_posts = $bfa_pic_options['bfa_pic_no_posts'];
|
---|
106 | $bfa_pic_duration = $bfa_pic_options['bfa_pic_duration'];
|
---|
107 | $bfa_pic_min_amount_comments = $bfa_pic_options['bfa_pic_min_amount_comments'];
|
---|
108 | $bfa_pic_append_cat_title = $bfa_pic_options['bfa_pic_append_cat_title'];
|
---|
109 | $bfa_pic_prepend_cat_title = $bfa_pic_options['bfa_pic_prepend_cat_title'];
|
---|
110 |
|
---|
111 | // Deal with HTML in the parameters
|
---|
112 | $bfa_pic_title = htmlspecialchars($bfa_pic_options['bfa_pic_title'], ENT_QUOTES);
|
---|
113 |
|
---|
114 | ?>
|
---|
115 | Title: <input style="width: 450px;" id="bfa_pic_src-title" name="bfa_pic_src-title" type="text" value="<?php echo $bfa_pic_title; ?>" />
|
---|
116 | <hr noshade size="1" style="clear:left; color: #ccc">
|
---|
117 | <p style="text-align: left;"><input id="bfa_pic_prepend_cat_title" name="bfa_pic_prepend_cat_title" type="checkbox" <?php if($bfa_pic_prepend_cat_title == "on"){echo " CHECKED";}?> />Prepend
|
---|
118 | <p style="text-align: left;"><input id="bfa_pic_append_cat_title" name="bfa_pic_append_cat_title" type="checkbox" <?php if($bfa_pic_append_cat_title == "on"){echo " CHECKED";}?> /> Append Category Name to Title
|
---|
119 | <hr noshade size="1" style="clear:left; color: #ccc">
|
---|
120 | <p style="text-align: left;">Show <input style="width: 40px;" id="bfa_pic_no_posts" name="bfa_pic_no_posts" type="text" value="<?php echo $bfa_pic_no_posts; ?>" />
|
---|
121 | posts not older than <input style="width: 60px;" id="bfa_pic_duration" name="bfa_pic_duration" type="text" value="<?php echo $bfa_pic_duration; ?>" /> days and with at least
|
---|
122 | <input style="width: 40px;" id="bfa_pic_min_amount_comments" name="bfa_pic_min_amount_comments" type="text" value="<?php echo $bfa_pic_min_amount_comments; ?>" /> comments
|
---|
123 | </p>
|
---|
124 | <div style="clear:left"></div>
|
---|
125 | <input type="hidden" id="bfa_pic_src-submit" name="bfa_pic_src-submit" value="1" />
|
---|
126 | <?php
|
---|
127 | }
|
---|
128 |
|
---|
129 | // This registers our widget so it appears with the other available
|
---|
130 | // widgets and can be dragged and dropped into any active sidebars.
|
---|
131 | register_sidebar_widget('BFA Popular in Cat', 'widget_mdv_most_commented_per_cat');
|
---|
132 |
|
---|
133 | // This registers our optional widget control form. Because of this
|
---|
134 | // our widget will have a button that reveals a 520x480 pixel form.
|
---|
135 | register_widget_control('BFA Popular in Cat', 'widget_mdv_most_commented_per_cat_control', 520, 480);
|
---|
136 | ?> |
---|