[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

title

Body

[close]

/wp-includes/ -> class-wp-comment.php (source)

   1  <?php
   2  /**
   3   * Comment API: WP_Comment class
   4   *
   5   * @package WordPress
   6   * @subpackage Comments
   7   * @since 4.4.0
   8   */
   9  
  10  /**
  11   * Core class used to organize comments as instantiated objects with defined members.
  12   *
  13   * @since 4.4.0
  14   */
  15  final class WP_Comment {
  16  
  17      /**
  18       * Comment ID.
  19       *
  20       * @since 4.4.0
  21       * @var int
  22       */
  23      public $comment_ID;
  24  
  25      /**
  26       * ID of the post the comment is associated with.
  27       *
  28       * @since 4.4.0
  29       * @var int
  30       */
  31      public $comment_post_ID = 0;
  32  
  33      /**
  34       * Comment author name.
  35       *
  36       * @since 4.4.0
  37       * @var string
  38       */
  39      public $comment_author = '';
  40  
  41      /**
  42       * Comment author email address.
  43       *
  44       * @since 4.4.0
  45       * @var string
  46       */
  47      public $comment_author_email = '';
  48  
  49      /**
  50       * Comment author URL.
  51       *
  52       * @since 4.4.0
  53       * @var string
  54       */
  55      public $comment_author_url = '';
  56  
  57      /**
  58       * Comment author IP address (IPv4 format).
  59       *
  60       * @since 4.4.0
  61       * @var string
  62       */
  63      public $comment_author_IP = '';
  64  
  65      /**
  66       * Comment date in YYYY-MM-DD HH:MM:SS format.
  67       *
  68       * @since 4.4.0
  69       * @var string
  70       */
  71      public $comment_date = '0000-00-00 00:00:00';
  72  
  73      /**
  74       * Comment GMT date in YYYY-MM-DD HH::MM:SS format.
  75       *
  76       * @since 4.4.0
  77       * @var string
  78       */
  79      public $comment_date_gmt = '0000-00-00 00:00:00';
  80  
  81      /**
  82       * Comment content.
  83       *
  84       * @since 4.4.0
  85       * @var string
  86       */
  87      public $comment_content;
  88  
  89      /**
  90       * Comment karma count.
  91       *
  92       * @since 4.4.0
  93       * @var int
  94       */
  95      public $comment_karma = 0;
  96  
  97      /**
  98       * Comment approval status.
  99       *
 100       * @since 4.4.0
 101       * @var string
 102       */
 103      public $comment_approved = '1';
 104  
 105      /**
 106       * Comment author HTTP user agent.
 107       *
 108       * @since 4.4.0
 109       * @var string
 110       */
 111      public $comment_agent = '';
 112  
 113      /**
 114       * Comment type.
 115       *
 116       * @since 4.4.0
 117       * @var string
 118       */
 119      public $comment_type = '';
 120  
 121      /**
 122       * Parent comment ID.
 123       *
 124       * @since 4.4.0
 125       * @var int
 126       */
 127      public $comment_parent = 0;
 128  
 129      /**
 130       * Comment author ID.
 131       *
 132       * @since 4.4.0
 133       * @var int
 134       */
 135      public $user_id = 0;
 136  
 137      /**
 138       * Comment children.
 139       *
 140       * @since 4.4.0
 141       * @var array
 142       */
 143      protected $children;
 144  
 145      /**
 146       * Whether children have been populated for this comment object.
 147       *
 148       * @since 4.4.0
 149       * @var bool
 150       */
 151      protected $populated_children = false;
 152  
 153      /**
 154       * Post fields.
 155       *
 156       * @since 4.4.0
 157       * @var array
 158       */
 159      protected $post_fields = array( 'post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_excerpt', 'post_status', 'comment_status', 'ping_status', 'post_name', 'to_ping', 'pinged', 'post_modified', 'post_modified_gmt', 'post_content_filtered', 'post_parent', 'guid', 'menu_order', 'post_type', 'post_mime_type', 'comment_count' );
 160  
 161      /**
 162       * Retrieves a WP_Comment instance.
 163       *
 164       * @since 4.4.0
 165       *
 166       * @global wpdb $wpdb WordPress database abstraction object.
 167       *
 168       * @param int $id Comment ID.
 169       * @return WP_Comment|false Comment object, otherwise false.
 170       */
 171  	public static function get_instance( $id ) {
 172          global $wpdb;
 173  
 174          $comment_id = (int) $id;
 175          if ( ! $comment_id ) {
 176              return false;
 177          }
 178  
 179          $_comment = wp_cache_get( $comment_id, 'comment' );
 180  
 181          if ( ! $_comment ) {
 182              $_comment = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->comments WHERE comment_ID = %d LIMIT 1", $comment_id ) );
 183  
 184              if ( ! $_comment ) {
 185                  return false;
 186              }
 187  
 188              wp_cache_add( $_comment->comment_ID, $_comment, 'comment' );
 189          }
 190  
 191          return new WP_Comment( $_comment );
 192      }
 193  
 194      /**
 195       * Constructor.
 196       *
 197       * Populates properties with object vars.
 198       *
 199       * @since 4.4.0
 200       *
 201       * @param WP_Comment $comment Comment object.
 202       */
 203  	public function __construct( $comment ) {
 204          foreach ( get_object_vars( $comment ) as $key => $value ) {
 205              $this->$key = $value;
 206          }
 207      }
 208  
 209      /**
 210       * Convert object to array.
 211       *
 212       * @since 4.4.0
 213       *
 214       * @return array Object as array.
 215       */
 216  	public function to_array() {
 217          return get_object_vars( $this );
 218      }
 219  
 220      /**
 221       * Get the children of a comment.
 222       *
 223       * @since 4.4.0
 224       *
 225       * @param array $args {
 226       *     Array of arguments used to pass to get_comments() and determine format.
 227       *
 228       *     @type string $format        Return value format. 'tree' for a hierarchical tree, 'flat' for a flattened array.
 229       *                                 Default 'tree'.
 230       *     @type string $status        Comment status to limit results by. Accepts 'hold' (`comment_status=0`),
 231       *                                 'approve' (`comment_status=1`), 'all', or a custom comment status.
 232       *                                 Default 'all'.
 233       *     @type string $hierarchical  Whether to include comment descendants in the results.
 234       *                                 'threaded' returns a tree, with each comment's children
 235       *                                 stored in a `children` property on the `WP_Comment` object.
 236       *                                 'flat' returns a flat array of found comments plus their children.
 237       *                                 Pass `false` to leave out descendants.
 238       *                                 The parameter is ignored (forced to `false`) when `$fields` is 'ids' or 'counts'.
 239       *                                 Accepts 'threaded', 'flat', or false. Default: 'threaded'.
 240       *     @type string|array $orderby Comment status or array of statuses. To use 'meta_value'
 241       *                                 or 'meta_value_num', `$meta_key` must also be defined.
 242       *                                 To sort by a specific `$meta_query` clause, use that
 243       *                                 clause's array key. Accepts 'comment_agent',
 244       *                                 'comment_approved', 'comment_author',
 245       *                                 'comment_author_email', 'comment_author_IP',
 246       *                                 'comment_author_url', 'comment_content', 'comment_date',
 247       *                                 'comment_date_gmt', 'comment_ID', 'comment_karma',
 248       *                                 'comment_parent', 'comment_post_ID', 'comment_type',
 249       *                                 'user_id', 'comment__in', 'meta_value', 'meta_value_num',
 250       *                                 the value of $meta_key, and the array keys of
 251       *                                 `$meta_query`. Also accepts false, an empty array, or
 252       *                                 'none' to disable `ORDER BY` clause.
 253       * }
 254       * @return array Array of `WP_Comment` objects.
 255       */
 256  	public function get_children( $args = array() ) {
 257          $defaults = array(
 258              'format'       => 'tree',
 259              'status'       => 'all',
 260              'hierarchical' => 'threaded',
 261              'orderby'      => '',
 262          );
 263  
 264          $_args           = wp_parse_args( $args, $defaults );
 265          $_args['parent'] = $this->comment_ID;
 266  
 267          if ( is_null( $this->children ) ) {
 268              if ( $this->populated_children ) {
 269                  $this->children = array();
 270              } else {
 271                  $this->children = get_comments( $_args );
 272              }
 273          }
 274  
 275          if ( 'flat' === $_args['format'] ) {
 276              $children = array();
 277              foreach ( $this->children as $child ) {
 278                  $child_args           = $_args;
 279                  $child_args['format'] = 'flat';
 280                  // get_children() resets this value automatically.
 281                  unset( $child_args['parent'] );
 282  
 283                  $children = array_merge( $children, array( $child ), $child->get_children( $child_args ) );
 284              }
 285          } else {
 286              $children = $this->children;
 287          }
 288  
 289          return $children;
 290      }
 291  
 292      /**
 293       * Add a child to the comment.
 294       *
 295       * Used by `WP_Comment_Query` when bulk-filling descendants.
 296       *
 297       * @since 4.4.0
 298       *
 299       * @param WP_Comment $child Child comment.
 300       */
 301  	public function add_child( WP_Comment $child ) {
 302          $this->children[ $child->comment_ID ] = $child;
 303      }
 304  
 305      /**
 306       * Get a child comment by ID.
 307       *
 308       * @since 4.4.0
 309       *
 310       * @param int $child_id ID of the child.
 311       * @return WP_Comment|bool Returns the comment object if found, otherwise false.
 312       */
 313  	public function get_child( $child_id ) {
 314          if ( isset( $this->children[ $child_id ] ) ) {
 315              return $this->children[ $child_id ];
 316          }
 317  
 318          return false;
 319      }
 320  
 321      /**
 322       * Set the 'populated_children' flag.
 323       *
 324       * This flag is important for ensuring that calling `get_children()` on a childless comment will not trigger
 325       * unneeded database queries.
 326       *
 327       * @since 4.4.0
 328       *
 329       * @param bool $set Whether the comment's children have already been populated.
 330       */
 331  	public function populated_children( $set ) {
 332          $this->populated_children = (bool) $set;
 333      }
 334  
 335      /**
 336       * Check whether a non-public property is set.
 337       *
 338       * If `$name` matches a post field, the comment post will be loaded and the post's value checked.
 339       *
 340       * @since 4.4.0
 341       *
 342       * @param string $name Property name.
 343       * @return bool
 344       */
 345  	public function __isset( $name ) {
 346          if ( in_array( $name, $this->post_fields ) && 0 !== (int) $this->comment_post_ID ) {
 347              $post = get_post( $this->comment_post_ID );
 348              return property_exists( $post, $name );
 349          }
 350      }
 351  
 352      /**
 353       * Magic getter.
 354       *
 355       * If `$name` matches a post field, the comment post will be loaded and the post's value returned.
 356       *
 357       * @since 4.4.0
 358       *
 359       * @param string $name
 360       * @return mixed
 361       */
 362  	public function __get( $name ) {
 363          if ( in_array( $name, $this->post_fields ) ) {
 364              $post = get_post( $this->comment_post_ID );
 365              return $post->$name;
 366          }
 367      }
 368  }


Generated: Wed Oct 16 08:20:01 2019 Cross-referenced by PHPXref 0.7