mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-07-01 15:50:09 +00:00
Introduce WP_Comment class to model/strongly-type rows from the comments database table. Inclusion of this class is a pre-req for some more general comment cleanup and sanity.
* Takes inspiration from `WP_Post` and adds sanity to comment caching. * Clarifies when the current global value for `$comment` is returned. The current implementation in `get_comment()` introduces side effects and an occasion stale global value for `$comment` when comment caches are cleaned. * Strongly-types `@param` docs * This class is marked `final` for now Props wonderboymusic, nacin. See #32619. git-svn-id: https://develop.svn.wordpress.org/trunk@33891 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
118
src/wp-includes/class-wp-comment.php
Normal file
118
src/wp-includes/class-wp-comment.php
Normal file
@@ -0,0 +1,118 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* WordPress Comment class
|
||||
*
|
||||
* @since 4.4.0
|
||||
*/
|
||||
final class WP_Comment {
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
public $comment_ID;
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
public $comment_post_ID = 0;
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
public $comment_author;
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $comment_author_email = '';
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $comment_author_url = '';
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $comment_author_IP = '';
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $comment_date = '0000-00-00 00:00:00';
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $comment_date_gmt = '0000-00-00 00:00:00';
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $comment_content;
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
public $comment_karma = 0;
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $comment_approved = '1';
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $comment_agent = '';
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $comment_type = '';
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
public $comment_parent = 0;
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
public $user_id = 0;
|
||||
|
||||
/**
|
||||
* Retrieve WP_Comment instance.
|
||||
*
|
||||
* @static
|
||||
* @access public
|
||||
*
|
||||
* @global wpdb $wpdb
|
||||
*
|
||||
* @param int $id Comment ID.
|
||||
* @return WP_Comment|false Comment object, false otherwise.
|
||||
*/
|
||||
public static function get_instance( $id ) {
|
||||
global $wpdb;
|
||||
|
||||
$comment_id = (int) $id;
|
||||
if ( ! $comment_id ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$_comment = wp_cache_get( $comment_id, 'comment' );
|
||||
|
||||
if ( ! $_comment ) {
|
||||
$_comment = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->comments WHERE comment_ID = %d LIMIT 1", $comment_id ) );
|
||||
|
||||
if ( ! $_comment ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
wp_cache_add( $_comment->comment_ID, $_comment, 'comment' );
|
||||
}
|
||||
|
||||
return new WP_Comment( $_comment );
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param WP_Comment $comment Comment object.
|
||||
*/
|
||||
public function __construct( $comment ) {
|
||||
foreach ( get_object_vars( $comment ) as $key => $value ) {
|
||||
$this->$key = $value;
|
||||
}
|
||||
}
|
||||
|
||||
public function to_array() {
|
||||
return get_object_vars( $this );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user