Plugin Directory

Changeset 492853


Ignore:
Timestamp:
01/20/2012 11:11:27 PM (14 years ago)
Author:
Beer
Message:

add ttl to admin, add before/after features

File:
1 edited

Legend:

Unmodified
Added
Removed
  • ipb-comments-for-wordpress/trunk/class.ipbcomments.php

    r490272 r492853  
    66
    77class WP_IPBComments {
    8 
    9     public $ttl = 180;                    // cache values will expire every 3 minutes or 180 seconds
    108
    119    public $crosspost_edits = false;      // don't change this yet
     
    4947            'manage_options',                         // capability required to use this menu option
    5048            'ipb-comments',                           // slug to refer to this menu item
    51             array($this,'show_options_page')  // optional callback function
     49            array($this,'show_options_page')          // optional callback function
    5250            );
    5351
     
    6058    function register_comments() {
    6159
     60        // where to put the link + link text
    6261        switch ( $this->options['ipb_custom_link_filter'] ) {
    6362
    6463            case 'before_comments':
     64                add_filter( 'comments_array', array($this,'show_link_text') );
     65                break;
     66
    6567            case 'after_comments':
    66                 add_filter( 'comments_array', array($this,'show_link_text') );
     68                add_action( 'comment_form_before', array($this,'show_link_text') );
    6769                break;
    6870
     
    7173                add_filter( 'the_content', array($this,'show_link_text') );
    7274                break;
    73 
    74             default:
    75 
    7675        }
    7776
     
    177176                        'topic_id' => intval($topicData['tid']),    // forum topic id
    178177                        'replies' => array(),                       // last X replies in topic
    179                         'ttl' => $this->ttl,                        // time to live for cached replies
    180178                        'timestamp' => time()                       // current timestamp of this update
    181179                        ) );
     
    193191
    194192    /**
    195      * get the last 5 replies for IPB topic_id
     193     * get the last X replies for IPB topic_id
    196194     */
    197195    function get_replies( $topic_id ) {
     
    226224        $registry->init();
    227225
     226        // build the select statement, skip the original post, we just want the replies
    228227        $registry->DB()->build( array( 'select' => 'author_name,post_date,post',
    229228                            'from'  => 'posts',
    230229                            'where' => 'topic_id = '.intval($topic_id),
    231                             'order' => 'post_date DESC',
    232                             'limit' => array(0,$this->options['ipb_field_show_comments'])
     230                            'order' => 'post_date',
     231                            'limit' => array(1,$this->options['ipb_field_show_comments'])
    233232                            )
    234233                        );
     
    253252        }
    254253
    255         return $replies;
     254        // return the comments in reverse chronological order
     255        return array_reverse($replies);
    256256    }
    257257
     
    369369   
    370370            <li>
     371            <label for="ttl">Cache TTL:</label>
     372            <input type="text" size="3" name="ipb_comments_options[ipb_field_ttl]"
     373                value="<?php echo $this->options['ipb_field_ttl']; ?>" />
     374                <em>180 (seconds to cache the forum replies)</em>
     375            </li>
     376
     377            <li>
    371378            <label for="member_id">Member ID:</label>
    372379            <input type="text" size="5" name="ipb_comments_options[ipb_field_member_id]"
     
    473480        $topic_link_text = $this->options['ipb_field_link_text_wp'];
    474481
    475         $link = sprintf( '<p class="ipb_discussion"><a href="%s">%s</a></p>', $topic_link, $topic_link_text );
     482        $link_text = sprintf( '<p class="ipb_discussion"><a href="%s">%s</a></p>', $topic_link, $topic_link_text );
    476483
    477484        // add it to the content/comments
     
    479486
    480487            case 'before_comments':
     488            case 'after_comments':
     489                echo $this->get_forum_comments();
     490                echo $link_text;
     491                return $content;
     492
    481493            case 'before_content':
    482                 return $link.$content;
    483 
    484             case 'after_comments':
     494                return $this->get_forum_comments() . $link_text . $content;
     495
    485496            case 'after_content':
    486                 return $content.$link;
    487 
    488             default:
     497                return $content . $this->get_forum_comments() . $link_text;
     498
     499            default: 
    489500                return $content;
    490501        }
     
    500511     */
    501512   
    502     function show_forum_comments ( $comments ) {
    503    
     513    function get_forum_comments () {
     514
    504515        if ( ! is_single() ) return $comments;
    505516       
     
    522533        $meta = get_post_meta($post_ID,'forum_topic_meta');
    523534        extract($meta[0]);
    524    
     535
    525536        /**
    526537         * check if cache has expired using timestamp and ttl against current time()
     
    529540        $t = time();
    530541
    531         if ( empty($meta) OR ($t - $timestamp > $ttl) OR empty($replies) ) {
     542        $update_cache = (empty($meta) OR empty($replies) OR ($t - $timestamp > intval($this->options['ipb_field_ttl'])));
     543
     544        if ( $update_cache ) {
    532545   
    533546            $replies = $this->get_replies($topic_id);
     
    568581         * Show our forum comments just above our post comments
    569582         */
     583        ob_start();
    570584        ?>
    571585        <div id="ipb_comments">
    572586        <p class="ipb_discussion"><a href="<?php echo $topic_link; ?>"><?php echo $this->topic_text; ?></a></p>
    573         <h4>Most recent comments:</h4>
     587        <h4>Most recent forum comments:</h4>
    574588            <ul>
    575589                <?php echo $reply_content; ?>
     
    578592        <?php
    579593
    580         return $comments;
     594        return ob_get_clean();
    581595    }
    582596
Note: See TracChangeset for help on using the changeset viewer.