viewing source for "expand-comments.php"

last commit

commit ec9726ece091495bad03d1d5096e468fbba6ca06 Author: andy <andy@a04a6e75-5819-0410-a511-8dafea38c924> Date: Thu Mar 22 13:42:47 2007 +0000
Added 1.3.1 info

source code

001: <?php
002: /*
003: Plugin Name: Expand Comments
004: Plugin URI: http://nymb.us/projects/show/212
005: Description: This plugin gives the blog an "Expand Comments" link at the bottom of each post. When clicked, the comments will be added below the post using AJAX. Some index.php editing is required
006: Author: Andrew Rader
007: Version: 1.3.1
008: Author URI: http://nymb.us
009: 
010: Copyright 2006 Andrew Rader
011: 
012: This file is part of Expand Comments
013: 
014:     Expand Comments is free software; you can redistribute it and/or modify
015:     it under the terms of the GNU General Public License as published by
016:     the Free Software Foundation; either version 2 of the License, or
017:     (at your option) any later version.
018: 
019:     Expand Comments is distributed in the hope that it will be useful,
020:     but WITHOUT ANY WARRANTY; without even the implied warranty of
021:     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
022:     GNU General Public License for more details.
023: 
024:     You should have received a copy of the GNU General Public License
025:     along with Expand Comments; if not, write to the Free Software
026:     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
027: 
028: */ 
029: 
030: add_action('wp_head', 'expand_comment_head');
031: add_action('admin_menu', array('expcom','setup'));
032: add_action('activate_expand-comments/expand-comments.php',array('expcom','init'));
033: add_action('init', array('expcom','ajax_data'));
034: add_action('init', array('expcom','js_vars'));
035: 
036: class expcom {
037:     function init() {
038:         if( function_exists('add_option') ) {
039:             add_option('expcom-intro','&raquo;');
040:             add_option('expcom-outro','');
041:             add_option('expcom-expand','Expand Comments ($count)');
042:             add_option('expcom-collapse','Collapse Comments');
043:             add_option('expcom-comlistclass', 'commentlist');
044:             add_option('expcom-comtextauthorclass', 'postauthor');
045:             add_option('expcom-comtextevenclass', '');
046:             add_option('expcom-comtextoddclass', 'alt' );
047:             add_option('expcom-authclass', 'commentauthor' );
048:             add_option('expcom-dateclass', 'commentmetadata' );
049:             add_option('expcom-comcontentclass', 'commentcontent' );
050:             add_option('expcom-collclass', 'collapsediv');
051:             add_option('expcom-showwithoutcomments', 'true');
052:             add_option('expcom-showform','false');
053:             add_option('expcom-jump', 'false');
054:             add_option('expcom-loading-text', 'Loading...');
055:             add_option('expcom-loading-class', 'commentsloading' );
056:             add_option('expcom-ajax-submit','true');
057:             add_option('expcom-moofx-submit','true');
058:         }
059:     }
060: 
061:     function setup() {
062:         if( function_exists('add_options_page') ) {
063:             add_options_page(__('Expand Comments'),__('Expand Comments'),1,basename(__FILE__),array('expcom','ui_page'));
064:         }
065:     }
066: 
067:     function ui_page() {
068:         include_once( 'expand-comments-ui.php' );
069:     }
070: 
071:     function set_intro($intro) {
072:         if( function_exists('update_option') ) {
073:             update_option('expcom-intro',$intro);
074:         }
075:     }
076: 
077:     function get_intro() {
078:         if( function_exists('get_option') ) {
079:             $ret = get_option('expcom-intro');
080:         }
081:         return $ret;
082:     }
083: 
084:     function set_outro($outro) {
085:         if( function_exists('update_option') ) {
086:             update_option('expcom-outro',$outro);
087:         }
088:     }
089: 
090:     function get_outro() {
091:         if( function_exists('get_option') ) {
092:             $ret = get_option('expcom-outro');
093:         }
094:         return $ret;
095:     }
096: 
097:     function set_expand($expand) {
098:         if( function_exists('update_option') ) {
099:             update_option('expcom-expand',$expand);
100:         }
101:     }
102: 
103:     function get_expand() {
104:         if( function_exists('get_option') ) {
105:             $ret = get_option('expcom-expand');
106:         }
107:         return $ret;
108:     }
109: 
110:     function set_collapse($collapse) {
111:         if( function_exists('update_option') ) {
112:             update_option('expcom-collapse',$collapse);
113:         }
114:     }
115: 
116:     function get_collapse() {
117:         if( function_exists('get_option') ) {
118:             $ret = get_option('expcom-collapse');
119:         }
120:         return $ret;
121:     }
122: 
123:     function set_loading_text($text) {
124:         if( function_exists('update_option') ) {
125:             update_option('expcom-loading-text', $text);
126:         }
127:     }
128: 
129:     function get_loading_text() {
130:         if( function_exists('get_option') ) {
131:             $ret = get_option('expcom-loading-text');
132:         }
133:         return $ret;
134:     }
135: 
136:     function set_commentlist_class($class) {
137:         if( function_exists('update_option') ) {
138:             update_option('expcom-comlistclass',$class);
139:         }
140:     }
141: 
142:     function get_commentlist_class() {
143:         if( function_exists('get_option') ) {
144:             $ret = get_option( 'expcom-comlistclass' );
145:         }
146:         return $ret;
147:     }
148: 
149:     function set_commenttext_author_class($class) {
150:         if( function_exists('update_option') ) {
151:             update_option( 'expcom-comtextauthorclass', $class );
152:         }
153:     }
154: 
155:     function get_commenttext_author_class() {
156:         if( function_exists('get_option') ) {
157:             $ret = get_option( 'expcom-comtextauthorclass' );
158:         }
159:         return $ret;
160:     }
161: 
162:     function set_commenttext_even_class($class) {
163:         if( function_exists('update_option') ) {
164:             update_option('expcom-comtextevenclass',$class);
165:         }
166:     }
167: 
168:     function get_commenttext_even_class() {
169:         if( function_exists('get_option') ) {
170:             $ret = get_option( 'expcom-comtextevenclass' );
171:         }
172:         return $ret;
173:     }
174: 
175:     function set_commenttext_odd_class($class) {
176:         if( function_exists('update_option') ) {
177:             update_option('expcom-comtextoddclass',$class);
178:         }
179:     }
180: 
181:     function get_commenttext_odd_class() {
182:         if( function_exists('get_option') ) {
183:             $ret = get_option( 'expcom-comtextoddclass' );
184:         }
185:         return $ret;
186:     }
187: 
188:     function set_commentcontent_class($class) {
189:         if( function_exists('update_option') ) {
190:             update_option('expcom-comcontentclass', $class);
191:         }
192:     }
193: 
194:     function get_commentcontent_class() {
195:         if( function_exists('get_option') ) {
196:             $ret = get_option( 'expcom-comcontentclass' );
197:         }
198:         return $ret;
199:     }
200: 
201:     function set_author_class($class) {
202:         if( function_exists('update_option') ) {
203:             update_option('expcom-authclass',$class);
204:         }
205:     }
206: 
207:     function get_author_class() {
208:         if( function_exists('get_option') ) {
209:             $ret = get_option( 'expcom-authclass' );
210:         }
211:         return $ret;
212:     }
213: 
214:     function set_date_class($class) {
215:         if( function_exists('update_option') ) {
216:             update_option('expcom-dateclass',$class);
217:         }
218:     }
219: 
220:     function get_date_class() {
221:         if( function_exists('get_option') ) {
222:             $ret = get_option( 'expcom-dateclass' );
223:         }
224:         return $ret;
225:     }
226: 
227:     function set_collapse_class($class) {
228:         if( function_exists('update_option') ) {
229:             update_option('expcom-collclass', $class);
230:         }
231:     }
232: 
233:     function get_collapse_class() {
234:         if( function_exists('get_option') ) {
235:             $ret = get_option('expcom-collclass');
236:         }
237:         return $ret;
238:     }
239: 
240:     function set_loading_class($class) {
241:         if( function_exists('update_option') ) {
242:             update_option('expcom-loading-class', $class);
243:         }
244:     }
245: 
246:     function get_loading_class() {
247:         if( function_exists('get_option') ) {
248:             $ret = get_option('expcom-loading-class');
249:         }
250:         return $ret;
251:     }
252: 
253:     function set_show_without_comments($yesno) {
254:         if( function_exists('update_option') ) {
255:             if( $yesno ) {
256:                 update_option('expcom-showwithoutcomments', 'true');
257:             }
258:             else {
259:                 update_option('expcom-showwithoutcomments', 'false');
260:             }
261:         }
262:     }
263: 
264:     function get_show_without_comments() {
265:         if( function_exists('get_option') ) {
266:             $ret = get_option('expcom-showwithoutcomments');
267:         }
268:         return( $ret == 'true' );
269:     }
270: 
271:     function set_show_form($yesno) {
272:         if( function_exists('update_option') ) {
273:             if( $yesno ) {
274:                 update_option('expcom-show-form', 'true');
275:             }
276:             else {
277:                 update_option('expcom-show-form', 'false');
278:             }
279:         }
280:     }
281: 
282:     function get_show_form() {
283:         if( function_exists('get_option') ) {
284:             $ret = get_option('expcom-show-form');
285:         }
286:         return ($ret == 'true');
287:     }
288: 
289:     function set_comments_jump($yesno) {
290:         if( function_exists('update_option') ) {
291:             if( $yesno ) {
292:                 update_option('expcom-jump', 'true');
293:             }
294:             else {
295:                 update_option('expcom-jump', 'false');
296:             }
297:         }
298:     }
299: 
300:     function get_comments_jump() {
301:         if( function_exists('get_option') ) {
302:             $ret = get_option('expcom-jump');
303:         }
304:         return( $ret == 'true' );
305:     }
306: 
307:     function set_ajax_submit($yesno) {
308:         if( function_exists('update_option') ) {
309:             if( $yesno ) {
310:                 update_option('expcom-ajax-submit', 'true');
311:             }
312:             else {
313:                 update_option('expcom-ajax-submit', 'false');
314:             }
315:         }
316:     }
317: 
318:     function get_ajax_submit() {
319:         if( function_exists('get_option') ) {
320:             $ret = get_option('expcom-ajax-submit');
321:         }
322:         return ($ret == 'true');
323:     }
324: 
325:     function set_moofx_submit($yesno) {
326:         if( function_exists('update_option') ) {
327:             if( $yesno ) {
328:                 update_option('expcom-moofx-submit', 'true');
329:             }
330:             else {
331:                 update_option('expcom-moofx-submit', 'false');
332:             }
333:         }
334:     }
335: 
336:     function get_moofx_submit() {
337:         if( function_exists('get_option') ) {
338:             $ret = get_option('expcom-moofx-submit');
339:         }
340:         return ($ret == 'true');
341:     }
342: 
343:     /* This gets called to create the link */
344:     function the_link() {
345:         global $id;
346:         if( expcom::get_show_without_comments() || get_comments_number($id) > 0 ) {
347:             $link = get_permalink();
348:             $intro = expcom::get_intro();
349:             $outro = expcom::get_outro();
350: 
351:             $link_text = str_replace( '$count', get_comments_number($id), expcom::get_expand() );
352:     
353:             echo "<a name=\"expComComments-$id\"></a>";
354: 
355:             echo "$intro <a class=\"expComLink\" id=\"expComLink-$id\" href=\"$link\" rel=\"get\" onclick=\"expandCollapseComments(this); return false;\">$link_text</a> $outro";
356:         }
357:     }
358:     
359:     /* This gets called to create the div */
360:     function the_div() {
361:         global $id;
362:     
363:         if( expcom::get_show_without_comments() || get_comments_number($id) > 0 ) {
364:             $class = expcom::get_commentlist_class();
365:             $loading = expcom::get_loading_text();
366:             $loadClass = expcom::get_loading_class();
367: 
368:             echo "<div id=\"expComLoading-$id\" class=\"$loadClass\" style=\"display:none;\">$loading</div>";
369:             echo "<div class=\"$class\" id=\"expComDiv-$id\" style=\"display:none;\">\n";
370: 
371:             $collClass = expcom::get_collapse_class();
372:             echo "<div id=\"expCollLinkDiv-$id\" class=\"$collClass\"><a href=\"javascript:;\" id=\"expCollLink-$id\" onclick=\"collapse(this); return false;\">";
373: 
374:             if( get_comments_number($id) > 0 ) {
375:                 echo expcom::get_collapse();
376:             }
377: 
378:             echo "</a></div>\n";
379:             
380:             if( expcom::get_show_form() ) {
381:                 echo "<div class=\"expcom-newform\" id=\"expcom-newform-$id\">";
382:                 include( 'inline-comment-form.php' );
383:                 echo "</div>\n";
384:             }
385:             
386:             echo "</div>\n";
387:         }
388:     }
389: 
390:     function submit_onclick() {
391:         global $id;
392: 
393:         if( !expcom::get_ajax_submit() ) return;
394: 
395:         echo "onclick=\"submitCommentForm('";
396:         echo get_option( 'siteurl' );
397:         echo "/wp-comments-post.php', this.form, $id ); return false;\"";
398:     }
399: 
400:     /* Echo out the head data for the plugin */
401:     function head() {
402:         $url = get_settings('siteurl');  
403:         echo "<link rel=\"stylesheet\" href=\"$url/wp-content/plugins/expand-comments/style.css\"></link>\n";
404:         echo "<script type=\"text/javascript\" src=\"$url/wp-content/plugins/expand-comments/expand-comments.js\"></script>\n";
405:         echo "<script type=\"text/javascript\" src=\"$url/wp-content/plugins/expand-comments/expand-comments-events.js\"></script>\n";
406:         if( expcom::get_moofx_submit() ) {
407:             echo "<script type=\"text/javascript\" src=\"$url/wp-content/plugins/expand-comments/prototype.lite.js\"></script>\n";
408:             echo "<script type=\"text/javascript\" src=\"$url/wp-content/plugins/expand-comments/moo.fx.js\"></script>\n";
409:         }
410:         echo "<script type=\"text/javascript\" src=\"$url/?expcomvars\"></script>\n";
411:     }
412: 
413:     function js_vars() {
414:         if( isset( $_GET['expcomvars'] ) ) {
415:             header('Content-type: application/x-javascript');
416:             echo "// These are variables used in the Expand Comments plugin for Wordpress\n";
417:             echo "// Copyright Andrew Rader (nymb.us)\n";
418:             $exp = expcom::get_expand();
419:             $col = expcom::get_collapse();
420:             echo "expcom_exp = \"$exp\";\n";
421:             echo "expcom_col = \"$col\";\n";
422:             echo "expcom_comtext_author_class = \"";
423:             echo expcom::get_commenttext_author_class();
424:             echo "\";\nexpcom_comtext_even_class = \"";
425:             echo expcom::get_commenttext_even_class();
426:             echo "\";\nexpcom_comtext_odd_class = \"";
427:             echo expcom::get_commenttext_odd_class();
428:             echo "\";\nexpcom_comcontentclass = \"";
429:             echo expcom::get_commentcontent_class();
430:             echo "\";\nexpcom_authclass = \"";
431:             echo expcom::get_author_class();
432:             echo "\";\nexpcom_dateclass = \"";
433:             echo expcom::get_date_class();
434: 
435:             echo "\";\nexpcom_moo_fx = ";
436:             if( expcom::get_moofx_submit() ) {
437:                 echo 'true;';
438:             }
439:             else {
440:                 echo 'false;';
441:             }
442: 
443:             echo "\nexpcom_jump = ";
444:             if( expcom::get_comments_jump() ) {
445:                 echo 'true;';
446:             }
447:             else {
448:                 echo 'false;';
449:             }
450: 
451:             echo "\nexpcom_says = \"";
452:             _e('says');
453:             echo "\";";
454:             exit;
455:         }
456:     }
457: 
458:     function ajax_data() {
459:         if( isset( $_GET['expcom'] ) && isset( $_GET['id'] ) ) {
460:             global $id, $wpdb, $comment, $post;
461: 
462:             $id = $_GET['id'];
463:             $post = get_post( $id );
464: 
465:             $comment_author = isset($_COOKIE['comment_author_'.COOKIEHASH]) ? trim(stripslashes($_COOKIE['comment_author_'.COOKIEHASH])) : '';
466: 
467:             $comment_author_email = isset($_COOKIE['comment_author_email_'.COOKIEHASH]) ? trim(stripslashes($_COOKIE['comment_author_email_'.COOKIEHASH])) : '';
468: 
469:             $comment_author_url = isset($_COOKIE['comment_author_url_'.COOKIEHASH]) ? trim(stripslashes($_COOKIE['comment_author_url_'.COOKIEHASH])) : '';
470: 
471:             if ( empty($comment_author) ) {
472:                 $comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_post_ID = '$id' AND comment_approved = '1' ORDER BY comment_date");
473:             } else {
474:                 $author_db = $wpdb->escape($comment_author);
475:                 $email_db  = $wpdb->escape($comment_author_email);
476:                 $comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_post_ID = '$id' AND ( comment_approved = '1' OR ( comment_author = '$author_db' AND comment_author_email = '$email_db' AND comment_approved = '0' ) ) ORDER BY comment_date");
477:             }
478: 
479:             /* Output the header stuff */
480:             header('Content-Type: text/xml');
481:             echo '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>';
482:             echo "\n";
483: 
484:             /* Output all of the data in XML format */
485:             echo "<expand-comments>\n";
486:             if( $comments ) {
487:                 foreach( $comments as $comment_row ) {
488:                     echo "<comment>\n";
489: 
490:                     /* Send all data for a comment, useful for custom fields */
491:                     foreach( $comment_row as $attr_name => $attr_val ) {
492:                         /* Don't send email and IP */
493:                         if( $attr_name != 'comment_author_email' && $attr_name != 'comment_author_IP' ) {
494: 
495:                             /* Format the content */
496:                             if( $attr_name == 'comment_content' ) {
497:                                 $comment = $comment_row;
498:                                 echo "\t<$attr_name><![CDATA[";
499:                                 comment_text();
500:                                 echo "]]></$attr_name>\n";
501:                             }
502:                             /* Format the date */
503:                             else if( $attr_name == 'comment_date' ) {
504:                                 $comment = $comment_row;
505:                                 $date = get_comment_date();
506:                                 $time = get_comment_time();
507:                                 echo "\t<comment_date><![CDATA[$date]]></comment_date>\n";
508:                                 echo "\t<comment_time>$time</comment_time>\n";
509:                             }
510:                             else if( $attr_name == 'comment_author' ) {
511:                                 $comment = $comment_row;
512:                                 $auth = get_comment_author();
513:                                 echo "\t<comment_author><![CDATA[$auth]]></comment_author>\n";
514:                             }
515:                             /* Format the author link */
516:                             else if( $attr_name == 'comment_author_url' ) {
517:                                 $comment = $comment_row;
518:                                 $link = get_comment_author_link();
519:                                 echo "\t<$attr_name><![CDATA[$link]]></$attr_name>\n";
520:                             }
521:                             else if( $attr_name == 'user_id' ) {
522:                                 echo "\t<$attr_name>$attr_val</$attr_name>\n";
523:                                 // This checks to see if this comment is by the post author
524:                                 if( $attr_val == $post->post_author ) {
525:                                     echo "\t<post_author>1</post_author>\n";
526:                                 }
527:                                 else {
528:                                     // otherwise this isn't the post author
529:                                     echo "\t<post_author>0</post_author>\n";
530:                                 }
531:                             }
532:                             else {
533:                                 echo "\t<$attr_name><![CDATA[$attr_val]]></$attr_name>\n";
534:                             }
535:                         }
536:                     }
537:     
538:                     /* Echo the comment's link */
539:                     $comment = $comment_row;
540:                     $comment_link = get_comment_link();
541:                     echo "<comment_link>$comment_link</comment_link>\n";
542:     
543:                     /* See if we're allowed to edit this comment */
544:                     if ( user_can_edit_post_comments($user_ID, $id) ) {
545:                         $edit = get_option('siteurl') . "/wp-admin/comment.php?action=editcomment&amp;c=$comment->comment_ID";
546:                         echo "<comment_edit>$edit</comment_edit>\n";
547:                     }
548:                     else {
549:                         /* Guess we can't edit */
550:                         echo "<comment_edit></comment_edit>\n";
551:                     }
552:     
553:                     echo "</comment>\n";
554:                 }
555:             }
556:             echo "</expand-comments>\n";
557:             exit;
558:         }
559:     }
560: }
561: 
562: function expand_comment_head() {
563:     expcom::head();
564: }
565: 
566: function expand_comment_link() {
567:     expcom::the_link();
568: }
569: 
570: function expand_comment_div() {
571:     expcom::the_div();
572: }
573: 
574: function expand_comment_submit_onclick() {
575:     expcom::submit_onclick();
576: }
577: 
578: ?>