[44] | 1 | <?php
|
---|
| 2 | /*
|
---|
| 3 | Plugin Name: BFA Popular Posts
|
---|
| 4 | Plugin URI: http://wordpress.bytesforall.com/
|
---|
| 5 | Description: Configurable WordPress widget that displays the most popular posts 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($args) {
|
---|
| 23 | extract($args);
|
---|
| 24 |
|
---|
| 25 | // Fetch our parameters
|
---|
| 26 | $bfa_pp_options = get_option('widget_mdv_most_commented');
|
---|
| 27 | $bfa_pp_title = $bfa_pp_options['bfa_pp_title'];
|
---|
| 28 | $bfa_pp_no_posts = $bfa_pp_options['bfa_pp_no_posts'];
|
---|
| 29 | $bfa_pp_duration = $bfa_pp_options['bfa_pp_duration'];
|
---|
| 30 | $bfa_pp_min_amount_comments = $bfa_pp_options['bfa_pp_min_amount_comments'];
|
---|
| 31 | $bfa_pp_display_homepage = $bfa_pp_options['bfa_pp_display_homepage'];
|
---|
| 32 | $bfa_pp_display_category = $bfa_pp_options['bfa_pp_display_category'];
|
---|
| 33 | $bfa_pp_display_post = $bfa_pp_options['bfa_pp_display_post'];
|
---|
| 34 | $bfa_pp_display_page = $bfa_pp_options['bfa_pp_display_page'];
|
---|
| 35 | $bfa_pp_display_archive = $bfa_pp_options['bfa_pp_display_archive'];
|
---|
| 36 | $bfa_pp_display_tag = $bfa_pp_options['bfa_pp_display_tag'];
|
---|
| 37 | $bfa_pp_display_search = $bfa_pp_options['bfa_pp_display_search'];
|
---|
| 38 | $bfa_pp_display_author = $bfa_pp_options['bfa_pp_display_author'];
|
---|
| 39 | $bfa_pp_display_404 = $bfa_pp_options['bfa_pp_display_404'];
|
---|
| 40 |
|
---|
| 41 | global $wpdb;
|
---|
| 42 |
|
---|
| 43 |
|
---|
| 44 | $bfa_pp_request = "SELECT ID, post_title, comment_count FROM $wpdb->posts";
|
---|
| 45 | $bfa_pp_request .= " WHERE post_status = 'publish' AND comment_count >= $bfa_pp_min_amount_comments";
|
---|
| 46 | $bfa_pp_request .= " AND post_password =''";
|
---|
| 47 |
|
---|
| 48 | if ($bfa_pp_duration !="") $bfa_pp_request .= " AND DATE_SUB(CURDATE(),INTERVAL ".$bfa_pp_duration." DAY) < post_date ";
|
---|
| 49 |
|
---|
| 50 | $bfa_pp_request .= " ORDER BY comment_count DESC LIMIT $bfa_pp_no_posts";
|
---|
| 51 | $bfa_pp_posts = $wpdb->get_results($bfa_pp_request);
|
---|
| 52 |
|
---|
| 53 | if ($bfa_pp_posts) {
|
---|
| 54 | foreach ($bfa_pp_posts as $bfa_pp_post) {
|
---|
| 55 | $bfa_pp_post_title = stripslashes($bfa_pp_post->post_title);
|
---|
| 56 | $bfa_pp_comment_count = $bfa_pp_post->comment_count;
|
---|
| 57 | $bfa_pp_permalink = get_permalink($bfa_pp_post->ID);
|
---|
| 58 | $widget_mdv_most_commented .= '<li><a href="' . $bfa_pp_permalink . '" title="' . $bfa_pp_post_title.'">' . $bfa_pp_post_title . ' (' . $bfa_pp_comment_count . ')</a></li>';
|
---|
| 59 | }
|
---|
| 60 | } else {
|
---|
| 61 | $widget_mdv_most_commented = "None found";
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 |
|
---|
| 65 | if ($widget_mdv_most_commented != "None found") {
|
---|
| 66 | echo $before_widget . $before_title . $bfa_pp_title . $after_title;
|
---|
| 67 | echo "<ul>" . $widget_mdv_most_commented . "</ul>";
|
---|
| 68 | echo $after_widget;
|
---|
| 69 | } else { return $widget_mdv_most_commented; }
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 |
|
---|
| 73 |
|
---|
| 74 | // This is the function that outputs the form to let the users edit
|
---|
| 75 | // the widget's parameters.
|
---|
| 76 | function widget_mdv_most_commented_control() {
|
---|
| 77 |
|
---|
| 78 | // Fetch the options, check them and if need be, update the options array
|
---|
| 79 | $bfa_pp_options = $bfa_pp_newoptions = get_option('widget_mdv_most_commented');
|
---|
| 80 | if ( $_POST["bfa_pp_src-submit"] ) {
|
---|
| 81 | $bfa_pp_newoptions['bfa_pp_title'] = strip_tags(stripslashes($_POST["bfa_pp_src-title"]));
|
---|
| 82 | $bfa_pp_newoptions['bfa_pp_no_posts'] = (int) $_POST["bfa_pp_no_posts"];
|
---|
| 83 | $bfa_pp_newoptions['bfa_pp_duration'] = (int) $_POST["bfa_pp_duration"];
|
---|
| 84 | $bfa_pp_newoptions['bfa_pp_min_amount_comments'] = (int) $_POST["bfa_pp_min_amount_comments"];
|
---|
| 85 | $bfa_pp_newoptions['bfa_pp_display_homepage'] = !isset($_POST["bfa_pp_display_homepage"]) ? NULL : $_POST["bfa_pp_display_homepage"];
|
---|
| 86 | $bfa_pp_newoptions['bfa_pp_display_category'] = !isset($_POST["bfa_pp_display_category"]) ? NULL : $_POST["bfa_pp_display_category"];
|
---|
| 87 | $bfa_pp_newoptions['bfa_pp_display_post'] = !isset($_POST["bfa_pp_display_post"]) ? NULL : $_POST["bfa_pp_display_post"];
|
---|
| 88 | $bfa_pp_newoptions['bfa_pp_display_page'] = !isset($_POST["bfa_pp_display_page"]) ? NULL : $_POST["bfa_pp_display_page"];
|
---|
| 89 | $bfa_pp_newoptions['bfa_pp_display_archive'] = !isset($_POST["bfa_pp_display_archive"]) ? NULL : $_POST["bfa_pp_display_archive"];
|
---|
| 90 | $bfa_pp_newoptions['bfa_pp_display_tag'] = !isset($_POST["bfa_pp_display_tag"]) ? NULL : $_POST["bfa_pp_display_tag"];
|
---|
| 91 | $bfa_pp_newoptions['bfa_pp_display_search'] = !isset($_POST["bfa_pp_display_search"]) ? NULL : $_POST["bfa_pp_display_search"];
|
---|
| 92 | $bfa_pp_newoptions['bfa_pp_display_author'] = !isset($_POST["bfa_pp_display_author"]) ? NULL : $_POST["bfa_pp_display_author"];
|
---|
| 93 | $bfa_pp_newoptions['bfa_pp_display_404'] = !isset($_POST["bfa_pp_display_404"]) ? NULL : $_POST["bfa_pp_display_404"];
|
---|
| 94 |
|
---|
| 95 | }
|
---|
| 96 | if ( $bfa_pp_options != $bfa_pp_newoptions ) {
|
---|
| 97 | $bfa_pp_options = $bfa_pp_newoptions;
|
---|
| 98 | update_option('widget_mdv_most_commented', $bfa_pp_options);
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | // Default options to the parameters
|
---|
| 102 | if ( !$bfa_pp_options['bfa_pp_no_posts'] ) $options['bfa_pp_no_posts'] = 10;
|
---|
| 103 | if ( !$bfa_pp_options['bfa_pp_min_amount_comments'] OR $bfa_pp_options['bfa_pp_min_amount_comments'] == 0) $bfa_pp_options['bfa_pp_min_amount_comments'] = 1;
|
---|
| 104 |
|
---|
| 105 | $bfa_pp_no_posts = $bfa_pp_options['bfa_pp_no_posts'];
|
---|
| 106 | $bfa_pp_duration = $bfa_pp_options['bfa_pp_duration'];
|
---|
| 107 | $bfa_pp_min_amount_comments = $bfa_pp_options['bfa_pp_min_amount_comments'];
|
---|
| 108 | $bfa_pp_display_homepage = $bfa_pp_options['bfa_pp_display_homepage'];
|
---|
| 109 | $bfa_pp_display_category = $bfa_pp_options['bfa_pp_display_category'];
|
---|
| 110 | $bfa_pp_display_post = $bfa_pp_options['bfa_pp_display_post'];
|
---|
| 111 | $bfa_pp_display_page = $bfa_pp_options['bfa_pp_display_page'];
|
---|
| 112 | $bfa_pp_display_archive = $bfa_pp_options['bfa_pp_display_archive'];
|
---|
| 113 | $bfa_pp_display_tag = $bfa_pp_options['bfa_pp_display_tag'];
|
---|
| 114 | $bfa_pp_display_search = $bfa_pp_options['bfa_pp_display_search'];
|
---|
| 115 | $bfa_pp_display_author = $bfa_pp_options['bfa_pp_display_author'];
|
---|
| 116 | $bfa_pp_display_404 = $bfa_pp_options['bfa_pp_display_404'];
|
---|
| 117 |
|
---|
| 118 | // Deal with HTML in the parameters
|
---|
| 119 | $bfa_pp_title = htmlspecialchars($bfa_pp_options['bfa_pp_title'], ENT_QUOTES);
|
---|
| 120 |
|
---|
| 121 | ?>
|
---|
| 122 | Title: <input style="width: 450px;" id="bfa_pp_src-title" name="bfa_pp_src-title" type="text" value="<?php echo $bfa_pp_title; ?>" />
|
---|
| 123 | <hr noshade size="1" style="clear:left; color: #ccc">
|
---|
| 124 | <p style="text-align: left;">Show <input style="width: 40px;" id="bfa_pp_no_posts" name="bfa_pp_no_posts" type="text" value="<?php echo $bfa_pp_no_posts; ?>" />
|
---|
| 125 | posts not older than <input style="width: 60px;" id="bfa_pp_duration" name="bfa_pp_duration" type="text" value="<?php echo $bfa_pp_duration; ?>" /> days and with at least
|
---|
| 126 | <input style="width: 40px;" id="bfa_pp_min_amount_comments" name="bfa_pp_min_amount_comments" type="text" value="<?php echo $bfa_pp_min_amount_comments; ?>" /> comment(s)
|
---|
| 127 | </p>
|
---|
| 128 | <hr noshade size="1" style="clear:left; color: #ccc">
|
---|
| 129 | <p>And display the list on: </p>
|
---|
| 130 | <p style="float: left; width: 160px; text-align: left;"><input id="bfa_pp_display_homepage" name="bfa_pp_display_homepage" type="checkbox" <?php if($bfa_pp_display_homepage == "on"){echo " CHECKED";}?> /> Homepage</p>
|
---|
| 131 | <p style="float: left; width: 160px; text-align: left;"><input id="bfa_pp_display_category" name="bfa_pp_display_category" type="checkbox" <?php if($bfa_pp_display_category == "on"){echo " CHECKED";}?> /> Category Pages</p>
|
---|
| 132 | <p style="float: left; width: 160px; text-align: left;"><input id="bfa_pp_display_post" name="bfa_pp_display_post" type="checkbox" <?php if($bfa_pp_display_post == "on"){echo " CHECKED";}?> /> Single Post Pages</p>
|
---|
| 133 | <p style="float: left; width: 160px; text-align: left;"><input id="bfa_pp_display_page" name="bfa_pp_display_page" type="checkbox" <?php if($bfa_pp_display_page == "on"){echo " CHECKED";}?> /> "Page" Pages</p>
|
---|
| 134 | <p style="float: left; width: 160px; text-align: left;"><input id="bfa_pp_display_archive" name="bfa_pp_display_archive" type="checkbox" <?php if($bfa_pp_display_archive == "on"){echo " CHECKED";}?> /> Archive Pages</p>
|
---|
| 135 | <p style="float: left; width: 160px; text-align: left;"><input id="bfa_pp_display_tag" name="bfa_pp_display_tag" type="checkbox" <?php if($bfa_pp_display_tag == "on"){echo " CHECKED";}?> /> Tag Pages</p>
|
---|
| 136 | <p style="float: left; width: 160px; text-align: left;"><input id="bfa_pp_display_search" name="bfa_pp_display_search" type="checkbox" <?php if($bfa_pp_display_search == "on"){echo " CHECKED";}?> /> Search Result Pages</p>
|
---|
| 137 | <p style="float: left; width: 160px; text-align: left;"><input id="bfa_pp_display_author" name="bfa_pp_display_author" type="checkbox" <?php if($bfa_pp_display_author == "on"){echo " CHECKED";}?> /> Author Pages</p>
|
---|
| 138 | <p style="float: left; width: 160px; text-align: left;"><input id="bfa_pp_display_404" name="bfa_pp_display_404" type="checkbox" <?php if($bfa_pp_display_404 == "on"){echo " CHECKED";}?> /> 404 Not Found Pages</p>
|
---|
| 139 | <div style="clear:left"></div>
|
---|
| 140 | <input type="hidden" id="bfa_pp_src-submit" name="bfa_pp_src-submit" value="1" />
|
---|
| 141 | <?php
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | // This registers our widget so it appears with the other available
|
---|
| 145 | // widgets and can be dragged and dropped into any active sidebars.
|
---|
| 146 | register_sidebar_widget('BFA Popular Posts', 'widget_mdv_most_commented');
|
---|
| 147 |
|
---|
| 148 | // This registers our optional widget control form. Because of this
|
---|
| 149 | // our widget will have a button that reveals a 520x480 pixel form.
|
---|
| 150 | register_widget_control('BFA Popular Posts', 'widget_mdv_most_commented_control', 520, 480);
|
---|
| 151 | ?> |
---|