Make WordPress Core

Ticket #7956: 7956.7.patch

File 7956.7.patch, 4.6 KB (added by Viper007Bond, 16 years ago)

Properly intval the $page, props azaozz

  • wp-includes/comment-template.php

     
    435435 * @uses $comment
    436436 *
    437437 * @param object|string|int $comment Comment to retrieve.
     438 * @param string|int $page The comment's page if known. Optional. Avoids extra database query.
    438439 * @return string The permalink to the current comment
    439440 */
    440 function get_comment_link($comment = null) {
     441function get_comment_link( $comment = null, $page = null ) {
    441442        global $wp_rewrite;
    442443
    443444        $comment = get_comment($comment);
    444445
    445446        if ( get_option('page_comments') ) {
    446                 $page = get_page_of_comment( $comment->comment_ID );
     447                $page = ( null !== $page ) ? (int) $page : get_page_of_comment( $comment->comment_ID );
    447448
    448449                if ( $wp_rewrite->using_permalinks() )
    449450                        return user_trailingslashit( trailingslashit( get_permalink( $comment->comment_post_ID ) ) . "comment-page-$page", 'comment' ) . '#comment-' . $comment->comment_ID;
     
    11501151                <br />
    11511152<?php endif; ?>
    11521153
    1153                 <div class="comment-meta commentmetadata"><a href="<?php echo htmlspecialchars( get_comment_link( $comment->comment_ID ) ) ?>"><?php printf(__('%1$s at %2$s'), get_comment_date('F jS, Y'),  get_comment_time()) ?></a><?php edit_comment_link('edit','&nbsp;&nbsp;','') ?></div>
     1154                <div class="comment-meta commentmetadata"><a href="<?php echo htmlspecialchars( get_comment_link( $comment->comment_ID, $page ) ) ?>"><?php printf(__('%1$s at %2$s'), get_comment_date('F jS, Y'),  get_comment_time()) ?></a><?php edit_comment_link('edit','&nbsp;&nbsp;','') ?></div>
    11541155
    11551156                <?php comment_text() ?>
    11561157
  • wp-includes/comment.php

     
    536536 * @return int|null Comment page number or null on error.
    537537 */
    538538function get_page_of_comment( $comment_ID, $per_page = null, $threaded = null ) {
     539        global $wpdb;
     540
    539541        if ( !$comment = get_comment( $comment_ID ) )
    540542                return;
    541543
     
    552554        if ( $threaded && 0 != $comment->comment_parent )
    553555                return get_page_of_comment( $comment->comment_parent, $per_page, $threaded );
    554556
    555         $comments = get_comments( array( 'post_id' => $comment->comment_post_ID, 'order' => 'ASC' ) );
     557        // Count comments older than this one
     558        $oldercoms = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(comment_ID) FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_parent = 0 AND comment_date_gmt < '%s'", $comment->comment_post_ID, $comment->comment_date_gmt ) );
    556559
    557         // Start going through the comments until we find what page number the above top level comment is on
    558         $page = 1;
    559         $comthispage = 0;
    560         foreach ( $comments as $com ) {
    561                 if ( $threaded && 0 != $com->comment_parent )
    562                         continue;
     560        // No older comments? Then it's page #1.
     561        if ( 0 == $oldercoms )
     562                return 1;
    563563
    564                 if ( $com->comment_ID == $comment->comment_ID )
    565                         return $page;
    566 
    567                 $comthispage++;
    568 
    569                 if ( $comthispage >= $per_page ) {
    570                         $page++;
    571                         $comthispage = 0;
    572                 }
    573         }
     564        // Divide comments older than this one by comments per page to get this comment's page number
     565        return ceil( ( $oldercoms + 1 ) / $per_page );
    574566}
    575567
    576568/**
  • wp-includes/widgets.php

     
    13871387                $number = 15;
    13881388
    13891389        if ( !$comments = wp_cache_get( 'recent_comments', 'widget' ) ) {
    1390                 $comments = $wpdb->get_results("SELECT comment_author, comment_author_url, comment_ID, comment_post_ID FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT $number");
     1390                $comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT $number");
    13911391                wp_cache_add( 'recent_comments', $comments, 'widget' );
    13921392        }
    13931393?>
     
    13961396                        <?php echo $before_title . $title . $after_title; ?>
    13971397                        <ul id="recentcomments"><?php
    13981398                        if ( $comments ) : foreach ( (array) $comments as $comment) :
    1399                         echo  '<li class="recentcomments">' . sprintf(__('%1$s on %2$s'), get_comment_author_link(), '<a href="'. get_permalink($comment->comment_post_ID) . '#comment-' . $comment->comment_ID . '">' . get_the_title($comment->comment_post_ID) . '</a>') . '</li>';
     1399                        echo  '<li class="recentcomments">' . sprintf(__('%1$s on %2$s'), get_comment_author_link(), '<a href="'. get_comment_link($comment->comment_ID) . '">' . get_the_title($comment->comment_post_ID) . '</a>') . '</li>';
    14001400                        endforeach; endif;?></ul>
    14011401                <?php echo $after_widget; ?>
    14021402<?php