<?php
/*
Plugin Name: coComment widget
Plugin URI: http://blog.23corner.com/2006/04/03/1408/
Description: display coComment conversations
Author: Jian-Zhou Huang
Version: 1.3
Author URI: http://blog.23corner.com
*/

// This gets called at the plugins_loaded action
function widget_cocomment_init() {
    
    
// Check for the required API functions
    
if ( !function_exists('register_sidebar_widget') || !function_exists('register_widget_control') )
        return;

    
// This saves options and prints the widget's config form.
    
function widget_cocomment_control() {
        
$options $newoptions get_option('widget_cocomment');
        if ( 
$_POST['cocomment-submit'] ) {
            
$newoptions['title'] = strip_tags(stripslashes($_POST['cocomment-title']));
            
$newoptions['username'] = strip_tags(stripslashes($_POST['cocomment-username']));
        
/*
            $newoptions['box'] = isset($_POST['cocomment-box']);
            $newoptions['topuser'] = isset($_POST['cocomment-topuser']);
            $newoptions['toptags'] = isset($_POST['cocomment-toptags']);
         */
        
}
        if ( 
$options != $newoptions ) {
            
$options $newoptions;
            
update_option('widget_cocomment'$options);
        }
        
$title wp_specialchars($options['title']);
        
$username wp_specialchars($options['username']);
        
/*
        $box = $options['box'] ? 'checked="checked"' : '';
        $topuser = $options['topuser'] ? 'checked="checked"' : '';
        $toptags = $options['toptags'] ? 'checked="checked"' : '';
        */        
    
?>
                <div style="text-align:right">
                <label for="cocomment-title" style="line-height:35px;display:block;">Widget title: <input type="text" id="cocomment-title" name="cocomment-title" value="<?php echo $title?>" /></label>
                <label for="cocomment-username" style="line-height:35px;display:block;">Username: <input type="text" id="cocomment-username" name="cocomment-username" value="<?php echo $username?>" /></label>
                <input type="hidden" name="cocomment-submit" id="cocomment-submit" value="1" />
                </div>
    <?php
    
}

    
// style function
    
function widget_cocomment_style() {
    
?>
    <style type="text/css">
        DIV#cocomment-link { 
            border:1px solid #DDDDDD; 
            padding:2px; 
            margin:1px; 
            background-color:white;
        }
        
        DIV#cocomment-link .entry {
            padding:5px; 
            font-size:small; 
            background-color:#DBE8FF; 
            border-bottom: 1px dashed #CCCCCC; 
            clear:both; 
        }
        
        DIV#cocomment-link .title { 
            padding:5px; 
            margin-bottom:3px; 
            background-color:white; 
            color:black;
        }
        
        DIV#cocomment-link .article { 
            color:#0D497B; 
        }
        
        DIV#cocomment-link .author { 
            color:#0D497B;
        }
        
        DIV#cocomment-link .comment { 
            color:black;
        }
        
        DIV#cocomment-link A { 
            text-decoration:none;
        }
        
        DIV#cocomment-link A:hover { 
            text-decoration:underline;
        }
        
        DIV#cocomment-link #boxfooter {
            font-size:x-small;
        }
        
        DIV#cocomment-link #footerleft {
            float:left; 
            text-align:left;
        }
        
        DIV#cocomment-link #footerleft A {
            color:gray;
        }
        
        DIV#cocomment-link #footerright { 
            float:right; 
            text-align:right;
        }
        
        DIV#cocomment-link #footerright A {
            color:gray;
        }
        
        .cocommentfeed img { 
            border: none; 
            position: absolute; 
            right: 0; 
        }
    </style>
    <?php
    
}
    
    
// This prints the widget
    
function widget_cocomment($args) {
        
extract($args);
        
$defaults = array('title' => 'My coComment''username' => 'cocomment');
        
$options = (array) get_option('widget_cocomment');
    
        foreach ( 
$defaults as $key => $value )
            if ( !isset(
$options[$key]) || empty($options[$key]) )
                
$options[$key] = $defaults[$key];

        
$title $options['title'];
        
$username rawurlencode($options['username']);
        
$box '1';

        
$json_url 'http://www.cocomment.com/mybox-json?nick=' $username;
        
$json_url.= '&box=' $box;

        if ( 
file_exists(dirname(__FILE__) . '/rss.png') )
            
$icon str_replace(ABSPATHget_settings('siteurl').'/'dirname(__FILE__)) . '/rss.png';
        else
            
$icon get_settings('siteurl').'/wp-includes/images/rss.png';

    
?>
        <?php echo $before_widget?>
            <?php echo $before_title $options['title'] . $after_title?>
            <a href="http://www.cocomment.com/mybox-rss2/<?php echo $username?>/1" class="cocommentfeed"><img src="<?php echo $icon?>" alt="RSS" title="RSS feed of this box"></a>

            <div id="cocomment-link" style="text-align:left; overflow:hidden;">
            <div id="boxcontent"></div>
            <div id="boxfooter">
                <div id="footerleft"><a href="http://www.cocomment.com/?box">Powered by coComment</a></div>
                <div id="footerright"><a href="http://www.cocomment.com/comments/<?php echo $username?>">more &raquo;</a></div>
            </div>
            </div>
            <script type="text/javascript" src="<?php echo $json_url?>"></script>
            <script type="text/javascript">

            // walk thru all articles:
            for (var i=0, article; article=cocommentResultSet[i]; i++) 
            {
                // walk thru all comments to an article:
                for (var j=0, comment; comment=article.comments[j]; j++) 
                {
                    var entrydiv = document.createElement('div');
                    entrydiv.setAttribute('class', 'entry');

                    if (j == 0)
                    {                
                        var btitlediv = document.createElement('div');
                        btitlediv.setAttribute('class', 'title');
                        btitlediv.setAttribute('style', 'overflow:hidden;');

                        //var btitle = document.createElement('div');
                        var blogLink = document.createElement('a');
                        blogLink.setAttribute('href', article.blogURL);
                        blogLink.appendChild( document.createTextNode(article.blogTitle ) );
                
                        var articleLink = document.createElement('a');
                        articleLink.setAttribute('href', article.articleURL);
                        articleLink.setAttribute('class', 'article');
                        articleLink.appendChild( document.createTextNode( article.articleTitle ));

                        btitlediv.appendChild(blogLink);
                        btitlediv.appendChild(document.createTextNode(': '));
                        btitlediv.appendChild(articleLink);
                        //btitlediv.appendChild(btitle);                        
                        entrydiv.appendChild(btitlediv);
                    }

                    // comments
                    
                    var commentdiv = document.createElement('div');
                    
                    if (comment.author == '<?php echo $username?>')
                    {
                        commentdiv.setAttribute('class', 'from-me');
                    }
                    else
                    {
                        commentdiv.setAttribute('class', 'from-other');
                    }
                    
                    var authorLink = document.createElement('a');
                    authorLink.setAttribute('href', comment.authorURL);
                    authorLink.setAttribute('class', 'author');
                    authorLink.appendChild( document.createTextNode(comment.author) );
      
                    //var authoraliasSpan = document.createElement('span');
                    //authoraliasSpan.setAttribute('class', 'author-alias');
                    //authoraliasSpan.appendChild(document.createTextNode('(' + comment.authorAlias + ')'));
                    
                    var commentLinkSpan = document.createElement('span');
                    commentLinkSpan.setAttribute('class', 'comment');
                    commentLinkSpan.innerHTML = comment.comment;
                    
                    var commentLinkCoco = document.createElement( 'a' );
                    commentLinkCoco.setAttribute( 'href', comment.commentURLCoco );
                    commentLinkCoco.innerHTML = ' [..]';
                                     
                    commentdiv.appendChild(authorLink);
                    //commentdiv.appendChild(authoraliasSpan);
                    commentdiv.appendChild(document.createTextNode(':'));
                    commentdiv.appendChild(commentLinkSpan);
                    commentdiv.appendChild(document.createTextNode(' '));
                    commentdiv.appendChild(commentLinkCoco);
      
                    entrydiv.appendChild(commentdiv);
                    document.getElementById('boxcontent').appendChild(entrydiv);
                }
            }
            </script>
        <?php echo $after_widget?>    
    <?php        

    
}

    
    
// Tell Dynamic Sidebar about our new widget and its control
    
register_sidebar_widget('coComment''widget_cocomment');
    
register_widget_control('coComment''widget_cocomment_control');
    
    if (
is_active_widget('widget_cocomment'))
        
add_action('wp_head''widget_cocomment_style');
    
}

// Delay plugin execution to ensure Dynamic Sidebar has a chance to load first
add_action('plugins_loaded''widget_cocomment_init');

?>