WordPress.org

Make WordPress Core

Ticket #13066: get_lastcommentmodified.diff

File get_lastcommentmodified.diff, 3.5 KB (added by jgci, 7 years ago)
  • wp-includes/comment.php

     
    402402 * @return string Last comment modified date.
    403403 */
    404404function get_lastcommentmodified($timezone = 'server') {
    405         global $cache_lastcommentmodified, $wpdb;
    406 
     405        global $cache_lastcommentmodified, $wpdb, $wp, $wp_query;
     406       
    407407        if ( isset($cache_lastcommentmodified[$timezone]) )
    408408                return $cache_lastcommentmodified[$timezone];
    409409
     410        //If wp_query has not been completely loaded (e.g when it's called in send_headers()),
     411        //this has to parse the query, but without fetching anything from the database.
     412        if( empty($wp_query->query_vars) ) {
     413                $wp->build_query_string();
     414                $wp_query->parse_query($wp->query_vars);
     415        }
     416       
     417        $q = $wp_query->query_vars;
     418
    410419        $add_seconds_server = date('Z');
    411420
    412         switch ( strtolower($timezone)) {
     421        switch ( strtolower($timezone) ) {
    413422                case 'gmt':
    414                         $lastcommentmodified = $wpdb->get_var("SELECT comment_date_gmt FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT 1");
     423                        $select = 'SELECT comment_date_gmt';
    415424                        break;
    416425                case 'blog':
    417                         $lastcommentmodified = $wpdb->get_var("SELECT comment_date FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT 1");
     426                        $select = 'SELECT comment_date';
    418427                        break;
    419428                case 'server':
    420                         $lastcommentmodified = $wpdb->get_var($wpdb->prepare("SELECT DATE_ADD(comment_date_gmt, INTERVAL %s SECOND) FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT 1", $add_seconds_server));
     429                default:
     430                        $select = $wpdb->prepare('SELECT DATE_ADD(comment_date_gmt, INTERVAL %s SECOND)', $add_seconds_server);
    421431                        break;
    422432        }
    423433
     434        $join = '';
     435        $needposts = false;
     436        $where = "WHERE comment_approved = '1'";
     437
     438        $p = '';
     439        if ( '' !== $q['p'] )
     440                $p = $q['p'];
     441
     442        elseif ( '' !== $q['page_id'] )
     443                $p = $q['page_id'];
     444
     445        elseif ( '' !== $q['attachment_id'] )
     446                $p = $q['attachment_id'];
     447
     448
     449        $name = '';
     450        if ( '' !== $q['name'] )
     451                $name = $q['name'];
     452
     453        elseif ( '' !== $q['pagename'] )
     454                $name = $q['pagename'];
     455
     456        elseif ( '' !== $q['attachment'] )
     457                $name = $q['attachment'];
     458
     459
     460        if ( '' != $p)
     461                $where .= " AND comment_post_ID = '$p'";
     462       
     463        if ( '' != $name) {
     464                $needposts = true;
     465                $where .= " AND $wpdb->posts.post_name = '$name'";
     466        }
     467
     468        if ( !empty($q['author']) ) {
     469                $needposts = true;
     470                $where .= " AND $wpdb->posts.post_author='{$q['author']}'";
     471        }
     472
     473        if ( '' !== $q['author_name'] ) {
     474                $needposts = true;
     475                $author = get_user_by('slug', $q['author_name']);
     476                if ( $author !== false )
     477                        $where .= " AND $wpdb->posts.post_author='$author->ID'";
     478        }
     479
     480        if ( '' !== $q['cat'] || '' !== $q['category_name'] ) {
     481                if ( !empty($q['cat']) ) {
     482                        $cat = $q['cat'];
     483                } else {
     484                        if ( $term = get_term_by('name', $q['category_name'], 'category') !== false )
     485                                $cat = $term->term_taxonomy_id;
     486                }
     487               
     488                if ( isset($cat) ) {
     489                        $join =  "INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id)";
     490                        $where .= " AND $wpdb->term_relationships.term_taxonomy_id IN ($cat)";
     491
     492                        $needposts = true;
     493                }
     494        }
     495
     496        if($needposts == true)
     497                $join = "JOIN $wpdb->posts ON ( $wpdb->comments.comment_post_ID = $wpdb->posts.ID )" . $join;
     498
     499        $lastcommentmodified = $wpdb->get_var("$select FROM $wpdb->comments $join $where ORDER BY comment_date_gmt DESC LIMIT 1");
    424500        $cache_lastcommentmodified[$timezone] = $lastcommentmodified;
    425501
    426502        return $lastcommentmodified;