root/afridex/plugins/wordpress-23-related-posts-plugin/wp_related_posts.php @ 21

Revision 21, 15.6 kB (checked in by admin, 18 years ago)
Line 
1<?php
2/*
3Plugin Name: WordPress Related Posts
4Version: 0.9
5Plugin URI: http://fairyfish.net/2007/09/12/wordpress-23-related-posts-plugin/
6Description: Generate a related posts list via tags of WordPress
7Author: Denis
8Author URI: http://fairyfish.net/
9
10Copyright (c) 2007
11Released under the GPL license
12http://www.gnu.org/licenses/gpl.txt
13
14    This file is part of WordPress.
15    WordPress is free software; you can redistribute it and/or modify
16    it under the terms of the GNU General Public License as published by
17    the Free Software Foundation; either version 2 of the License, or
18    (at your option) any later version.
19
20    This program is distributed in the hope that it will be useful,
21    but WITHOUT ANY WARRANTY; without even the implied warranty of
22    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23    GNU General Public License for more details.
24
25    You should have received a copy of the GNU General Public License
26    along with this program; if not, write to the Free Software
27    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
28
29        INSTALL:
30        Just install the plugin in your blog and activate
31*/
32
33add_action('init', 'init_textdomain');
34function init_textdomain(){
35  load_plugin_textdomain('wp_related_posts',PLUGINDIR . '/' . dirname(plugin_basename (__FILE__)) . '/lang');
36}
37
38function wp_get_related_posts() {
39        global $wpdb, $post,$table_prefix;
40        $wp_rp = get_option("wp_rp");
41       
42        $exclude = explode(",",$wp_rp["wp_rp_exclude"]);
43        $limit = $wp_rp["wp_rp_limit"];
44        $wp_rp_title = $wp_rp["wp_rp_title"];
45        $wp_no_rp = $wp_rp["wp_no_rp"];
46        $wp_no_rp_text = $wp_rp["wp_no_rp_text"];
47        $show_date = $wp_rp["wp_rp_date"];
48        $show_comments_count = $wp_rp["wp_rp_comments"];
49       
50        if ( $exclude != '' ) {
51                $q = "SELECT tt.term_id FROM ". $table_prefix ."term_taxonomy tt, " . $table_prefix . "term_relationships tr WHERE tt.taxonomy = 'category' AND tt.term_taxonomy_id = tr.term_taxonomy_id AND tr.object_id = $post->ID";
52
53                $cats = $wpdb->get_results($q);
54               
55                foreach(($cats) as $cat) {
56                        if (in_array($cat->term_id, $exclude) != false){
57                                return;
58                        }
59                }
60        }
61               
62        if(!$post->ID){return;}
63        $now = current_time('mysql', 1);
64        $tags = wp_get_post_tags($post->ID);
65
66        //print_r($tags);
67       
68        $taglist = "'" . $tags[0]->term_id. "'";
69       
70        $tagcount = count($tags);
71        if ($tagcount > 1) {
72                for ($i = 1; $i <= $tagcount; $i++) {
73                        $taglist = $taglist . ", '" . $tags[$i]->term_id . "'";
74                }
75        }
76               
77        if ($limit) {
78                $limitclause = "LIMIT $limit";
79        }       else {
80                $limitclause = "LIMIT 10";
81        }
82       
83        $q = "SELECT DISTINCT p.ID, p.post_title, p.post_date, p.comment_count, count(t_r.object_id) as cnt FROM $wpdb->term_taxonomy t_t, $wpdb->term_relationships t_r, $wpdb->posts p WHERE t_t.taxonomy ='post_tag' AND t_t.term_taxonomy_id = t_r.term_taxonomy_id AND t_r.object_id  = p.ID AND (t_t.term_id IN ($taglist)) AND p.ID != $post->ID AND p.post_status = 'publish' AND p.post_date_gmt < '$now' GROUP BY t_r.object_id ORDER BY cnt DESC, p.post_date_gmt DESC $limitclause;";
84
85        //echo $q;
86
87        $related_posts = $wpdb->get_results($q);
88        $output = "";
89       
90        if (!$related_posts){
91               
92                if(!$wp_no_rp || ($wp_no_rp == "popularity" && !function_exists('akpc_most_popular'))) $wp_no_rp = "text";
93               
94                if($wp_no_rp == "text"){
95                        if(!$wp_no_rp_text) $wp_no_rp_text= __("No Related Post",'wp_related_posts');
96                        $output  .= '<li>'.$wp_no_rp_text .'</li>';
97                }       else{
98                        if($wp_no_rp == "random"){
99                                if(!$wp_no_rp_text) $wp_no_rp_text= __("Random Posts",'wp_related_posts');
100                                $related_posts = wp_get_random_posts($limitclause);
101                        }       elseif($wp_no_rp == "commented"){
102                                if(!$wp_no_rp_text) $wp_no_rp_text= __("Most Commented Posts",'wp_related_posts');
103                                $related_posts = wp_get_most_commented_posts($limitclause);
104                        }       elseif($wp_no_rp == "popularity"){
105                                if(!$wp_no_rp_text) $wp_no_rp_text= __("Most Popular Posts",'wp_related_posts');
106                                $related_posts = wp_get_most_popular_posts($limitclause);
107                        }else{
108                                return __("Something wrong",'wp_related_posts');;
109                        }
110                        $wp_rp_title = $wp_no_rp_text;
111                }
112        }               
113               
114        foreach ($related_posts as $related_post ){
115                $output .= '<li>';
116               
117                if ($show_date){
118                        $dateformat = get_option('date_format');
119                        $output .=   mysql2date($dateformat, $related_post->post_date) . " -- ";
120                }
121               
122                $output .=  '<a href="'.get_permalink($related_post->ID).'" title="'.wptexturize($related_post->post_title).'">'.wptexturize($related_post->post_title).'';
123               
124                if ($show_comments_count){
125                        $output .=  " (" . $related_post->comment_count . ")";
126                }
127               
128                $output .=  '</a></li>';
129        }
130       
131        $output = '<ul class="related_post">' . $output . '</ul>';
132               
133        if($wp_rp_title != '') $output =  '<h3>'.$wp_rp_title .'</h3>'. $output;
134       
135        return $output;
136}
137
138function wp_related_posts(){
139
140    global $id;
141   
142    $preview = $_GET['preview'];
143   
144    $output_old = get_post_meta($id, "related_posts", $single = true);
145   
146    if($output_old){
147      $time = time();
148      if(($time - $output_old["time"])<600){
149        echo $output_old["related_posts"];
150        return;
151      }
152    }
153               
154        $output = wp_get_related_posts() ;
155       
156  $output_new = array("time"=>time(),"related_posts"=>$output);
157    if($output_old){
158      update_post_meta($id, 'related_posts', $output_new);
159    }else{
160      if(!add_post_meta($id, 'related_posts', $output_new, true)){
161        update_post_meta($id, 'related_posts', $output_new);
162      }
163    }
164
165        echo $output;
166}
167
168function wp23_related_posts() {
169        wp_related_posts();
170}
171
172function wp_related_posts_for_feed($content=""){
173        $wp_rp = get_option("wp_rp");
174        $wp_rp_rss = ($wp_rp["wp_rp_rss"] == 'yes') ? 1 : 0;
175        if ( (! is_feed()) || (! $wp_rp_rss)) return $content;
176       
177        $output = wp_get_related_posts() ;
178        $content = $content . $output;
179       
180        return $content;
181}
182
183add_filter('the_content', 'wp_related_posts_for_feed',1);
184
185function wp_related_posts_auto($content=""){
186        $wp_rp = get_option("wp_rp");
187        $wp_rp_auto = ($wp_rp["wp_rp_auto"] == 'yes') ? 1 : 0;
188        if ( (! is_single()) || (! $wp_rp_auto)) return $content;
189       
190        $output = wp_get_related_posts() ;
191        $content = $content . $output;
192       
193        return $content;
194}
195
196add_filter('the_content', 'wp_related_posts_auto',99);
197
198function wp_get_random_posts ($limitclause="") {
199    global $wpdb, $tableposts, $post;
200               
201    $q = "SELECT ID, post_title, post_date, comment_count FROM $tableposts WHERE post_status = 'publish' AND post_type = 'post' AND ID != $post->ID ORDER BY RAND() $limitclause";
202    return $wpdb->get_results($q);
203}
204
205function wp_random_posts ($number = 10){
206        $limitclause="LIMIT " . $number;
207        $random_posts = wp_get_random_posts ($limitclause);
208       
209        foreach ($random_posts as $random_post ){
210                $output .= '<li>';
211               
212                $output .=  '<a href="'.get_permalink($random_post->ID).'" title="'.wptexturize($random_post->post_title).'">'.wptexturize($random_post->post_title).'</a></li>';
213        }
214       
215        $output = '<ul class="randome_post">' . $output . '</ul>';
216       
217        echo $output;
218}
219
220function wp_get_most_commented_posts($limitclause="") {
221        global $wpdb; 
222    $q = "SELECT ID, post_title, post_date, COUNT($wpdb->comments.comment_post_ID) AS 'comment_count' FROM $wpdb->posts, $wpdb->comments WHERE comment_approved = '1' AND $wpdb->posts.ID=$wpdb->comments.comment_post_ID AND post_status = 'publish' GROUP BY $wpdb->comments.comment_post_ID ORDER BY comment_count DESC $limitclause"; 
223    return $wpdb->get_results($q);
224} 
225
226function wp_most_commented_posts ($number = 10){
227        $limitclause="LIMIT " . $number;
228        $most_commented_posts = wp_get_most_commented_posts ($limitclause);
229       
230        foreach ($most_commented_posts as $most_commented_post ){
231                $output .= '<li>';
232               
233                $output .=  '<a href="'.get_permalink($most_commented_post->ID).'" title="'.wptexturize($most_commented_post->post_title).'">'.wptexturize($most_commented_post->post_title).'</a></li>';
234        }
235       
236        $output = '<ul class="most_commented_post">' . $output . '</ul>';
237       
238        echo $output;
239}
240
241function wp_get_most_popular_posts ($limitclause="") {
242    global $wpdb, $table_prefix;
243               
244    $q = $sql = "SELECT p.ID, p.post_title, p.post_date, p.comment_count FROM ". $table_prefix ."ak_popularity as akpc,".$table_prefix ."posts as p WHERE p.ID = akpc.post_id ORDER BY akpc.total DESC $limitclause";;
245    return $wpdb->get_results($q);
246}
247
248function wp_most_popular_posts ($number = 10){
249        $limitclause="LIMIT " . $number;
250        $most_popular_posts = wp_get_most_popular_posts ($limitclause);
251       
252        foreach ($most_popular_posts as $most_popular_post ){
253                $output .= '<li>';
254               
255                $output .=  '<a href="'.get_permalink($most_popular_post->ID).'" title="'.wptexturize($most_popular_post->post_title).'">'.wptexturize($most_popular_post->post_title).'</a></li>';
256        }
257       
258        $output = '<ul class="most_popular_post">' . $output . '</ul>';
259       
260        echo $output;
261}
262
263add_action('admin_menu', 'wp_add_related_posts_options_page');
264
265function wp_add_related_posts_options_page() {
266        if (function_exists('add_options_page')) {
267                add_options_page( __('WordPress Related Posts','wp_related_posts'), __('WordPress Related Posts','wp_related_posts'), 8, basename(__FILE__), 'wp_related_posts_options_subpanel');
268        }
269}
270
271function wp_related_posts_options_subpanel() {
272        if($_POST["wp_rp_Submit"]){
273                $message = "WordPress Related Posts Setting Updated";
274       
275                $wp_rp_saved = get_option("wp_rp");
276       
277                $wp_rp = array (
278                        "wp_rp_title"   => $_POST['wp_rp_title_option'],
279                        "wp_no_rp"              => $_POST['wp_no_rp_option'],
280                        "wp_no_rp_text" => $_POST['wp_no_rp_text_option'],
281                        "wp_rp_limit"   => $_POST['wp_rp_limit_option'],
282                        'wp_rp_exclude' => $_POST['wp_rp_exclude_option'],
283                        'wp_rp_auto'    => $_POST['wp_rp_auto_option'],
284                        'wp_rp_rss'             => $_POST['wp_rp_rss_option'],
285                        'wp_rp_comments'=> $_POST['wp_rp_comments_option'],
286                        'wp_rp_date'    => $_POST['wp_rp_date_option']
287                );
288               
289                if ($wp_rp_saved != $wp_rp)
290                        if(!update_option("wp_rp",$wp_rp))
291                                $message = "Update Failed";
292               
293                echo '<div id="message" class="updated fade"><p>'.$message.'.</p></div>';
294        }
295       
296        $wp_rp = get_option("wp_rp");
297?>
298    <div class="wrap">
299        <h2 id="write-post"><?php _e("Related Posts Options&hellip;",'wp_related_posts');?></h2>
300        <p><?php _e("WordPress Related Posts Plugin will generate a related posts via WordPress tags, and add the related posts to feed.",'wp_related_posts');?></p>
301        <div style="float:right;">
302          <form action="https://www.paypal.com/cgi-bin/webscr" method="post">
303          <input type="hidden" name="cmd" value="_donations">
304          <input type="hidden" name="business" value="honghua.deng@gmail.com">
305          <input type="hidden" name="item_name" value="Donate to fairyfish.net">
306          <input type="hidden" name="no_shipping" value="0">
307          <input type="hidden" name="no_note" value="1">
308          <input type="hidden" name="currency_code" value="USD">
309          <input type="hidden" name="tax" value="0">
310          <input type="hidden" name="lc" value="US">
311          <input type="hidden" name="bn" value="PP-DonationsBF">
312          <input type="image" src="https://www.paypal.com/en_US/i/btn/btn_donate_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
313          <img alt="" border="0" src="https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1"><br />
314          </form>
315        </div>
316        <h3><?php _e("Related Posts Preference",'wp_related_posts');?></h3>
317        <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>?page=<?php echo basename(__FILE__); ?>">
318       
319        <table class="form-table">
320          <tr>
321            <th><?php _e("Related Posts Title:",'wp_related_posts'); ?></th>
322            <td>
323              <input type="text" name="wp_rp_title_option" value="<?php echo $wp_rp["wp_rp_title"]; ?>" />
324            </td>
325          </tr>
326          <tr>
327            <th><?php _e("When No Related Posts, Dispaly:",'wp_related_posts'); ?></th>
328            <td>
329              <?php $wp_no_rp = $wp_rp["wp_no_rp"]; ?>
330              <select name="wp_no_rp_option" >
331              <option value="text" <?php if($wp_no_rp == 'text') echo 'selected' ?> ><?php _e("Text: 'No Related Posts'",'wp_related_posts'); ?></option>
332              <option value="random" <?php if($wp_no_rp == 'random') echo 'selected' ?>><?php _e("Random Posts",'wp_related_posts'); ?></option>
333              <option value="commented" <?php if($wp_no_rp == 'commented') echo 'selected' ?>><?php _e("Most Commented Posts",'wp_related_posts'); ?></option>
334              <?php if (function_exists('akpc_most_popular')){ ?>
335              <option value="popularity" <?php if($wp_no_rp == 'popularity') echo 'selected' ?>><?php _e("Most Popular Posts",'wp_related_posts'); ?></option>
336              <?php } ?> 
337              </select>
338            </td>
339          </tr>
340          <tr>
341            <th><?php _e("No Related Post's Title or Text:",'wp_related_posts'); ?></th>
342            <td>
343              <input type="text" name="wp_no_rp_text_option" value="<?php echo $wp_rp["wp_no_rp_text"]; ?>" />
344            </td>
345          </tr>
346          <tr>
347            <th><?php _e("Limit:",'wp_related_posts');?></th>
348            <td>
349              <input type="text" name="wp_rp_limit_option" value="<?php echo $wp_rp["wp_rp_limit"]; ?>" />
350            </td>
351          </tr>
352          <tr>
353            <th><?php _e("Exclude(category IDs):",'wp_related_posts');?></th>
354            <td>
355              <input type="text" name="wp_rp_exclude_option" value="<?php echo $wp_rp["wp_rp_exclude"]; ?>" />
356            </td>
357          </tr>
358          <tr>
359            <th><?php _e("Other Setting:",'wp_related_posts');?></th>
360            <td>
361              <label>
362              <?php
363              if ( $wp_rp["wp_rp_auto"] == 'yes' ) {
364              echo '<input name="wp_rp_auto_option" type="checkbox" value="yes" checked>';
365              } else {
366              echo '<input name="wp_rp_auto_option" type="checkbox" value="yes">';
367              }
368              ?>
369              <?php _e("Auto Insert Related Posts",'wp_related_posts');?>
370              </label>
371              <br />
372              <label>
373              <?php
374              if ( $wp_rp["wp_rp_rss"] == 'yes' ) {
375                echo '<input name="wp_rp_rss_option" type="checkbox" value="yes" checked>';
376              } else {
377                echo '<input name="wp_rp_rss_option" type="checkbox" value="yes">';
378              }
379              ?>
380              <?php _e("Related Posts for RSS",'wp_related_posts');?>
381              </label>
382              <br />
383              <label>
384              <?php
385              if ( $wp_rp["wp_rp_comments"] == 'yes' ) {
386                echo '<input name="wp_rp_comments_option" type="checkbox" value="yes" checked>';
387              } else {
388                echo '<input name="wp_rp_comments_option" type="checkbox" value="yes">';
389              }
390              ?>
391              <?php _e("Display Comments Count",'wp_related_posts');?>
392              </label>
393              <br />
394              <label>
395              <?php
396              if ( $wp_rp["wp_rp_date"] == 'yes' ) {
397                echo '<input name="wp_rp_date_option" type="checkbox" value="yes" checked>';
398              } else {
399                echo '<input name="wp_rp_date_option" type="checkbox" value="yes">';
400              }
401              ?>
402              <?php _e("Display Post Date",'wp_related_posts');?>
403              </label>
404              <br />
405            </td>
406          </tr>
407        </table>
408        <p class="submit"><input type="submit" value="<?php _e("Update Preferences &raquo;",'wp_related_posts');?>" name="wp_rp_Submit" /></p>
409        </form>
410      </div>
411<?php }?>
Note: See TracBrowser for help on using the browser.