函数文档

get_lastcommentmodified()

💡 云策文档标注

概述

get_lastcommentmodified() 函数用于获取最后一条已批准评论的修改日期,支持不同时区设置,并利用缓存优化性能。

关键要点

  • 参数 $timezone 指定时区,可选 'gmt'、'blog' 或 'server',默认值为 'server'。
  • 返回值为字符串(成功时)或 false(失败时),表示最后评论修改日期。
  • 函数内部使用 wp_cache_get() 和 wp_cache_set() 进行缓存管理,提升查询效率。
  • 根据时区参数,通过 wpdb::get_var() 执行不同的 SQL 查询来获取日期。

代码示例

function get_lastcommentmodified( $timezone = 'server' ) {
    global $wpdb;

    $timezone = strtolower( $timezone );
    $key      = "lastcommentmodified:$timezone";

    $comment_modified_date = wp_cache_get( $key, 'timeinfo' );
    if ( false !== $comment_modified_date ) {
        return $comment_modified_date;
    }

    switch ( $timezone ) {
        case 'gmt':
            $comment_modified_date = $wpdb->get_var( "SELECT comment_date_gmt FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT 1" );
            break;
        case 'blog':
            $comment_modified_date = $wpdb->get_var( "SELECT comment_date FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT 1" );
            break;
        case 'server':
            $add_seconds_server = gmdate( 'Z' );

            $comment_modified_date = $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 ) );
            break;
    }

    if ( $comment_modified_date ) {
        wp_cache_set( $key, $comment_modified_date, 'timeinfo' );

        return $comment_modified_date;
    }

    return false;
}

注意事项

  • 函数仅查询 comment_approved = '1' 的已批准评论,忽略未批准或垃圾评论。
  • 在版本 4.7.0 中,缓存机制从本地静态变量改为使用 Object Cache API。
  • 使用时需确保数据库表 $wpdb->comments 存在且可访问。

📄 原文内容

Retrieves the date the last comment was modified.

Parameters

$timezonestringrequired
Which timezone to use in reference to 'gmt', 'blog', or 'server' locations.

Return

string|false Last comment modified date on success, false on failure.

Source

function get_lastcommentmodified( $timezone = 'server' ) {
	global $wpdb;

	$timezone = strtolower( $timezone );
	$key      = "lastcommentmodified:$timezone";

	$comment_modified_date = wp_cache_get( $key, 'timeinfo' );
	if ( false !== $comment_modified_date ) {
		return $comment_modified_date;
	}

	switch ( $timezone ) {
		case 'gmt':
			$comment_modified_date = $wpdb->get_var( "SELECT comment_date_gmt FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT 1" );
			break;
		case 'blog':
			$comment_modified_date = $wpdb->get_var( "SELECT comment_date FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT 1" );
			break;
		case 'server':
			$add_seconds_server = gmdate( 'Z' );

			$comment_modified_date = $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 ) );
			break;
	}

	if ( $comment_modified_date ) {
		wp_cache_set( $key, $comment_modified_date, 'timeinfo' );

		return $comment_modified_date;
	}

	return false;
}

Changelog

Version Description
4.7.0 Replaced caching the modified date in a local static variable with the Object Cache API.
1.5.0 Introduced.