Make WordPress Core


Ignore:
Timestamp:
09/03/2015 06:16:35 PM (9 years ago)
Author:
wonderboymusic
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/comment-functions.php

    r33822 r33891  
    165165 * @global object $comment
    166166 *
    167  * @param object|string|int $comment Comment to retrieve.
     167 * @param WP_Comment|string|int $comment Comment to retrieve.
    168168 * @param string $output Optional. OBJECT or ARRAY_A or ARRAY_N constants.
    169  * @return object|array|null Depends on $output value.
     169 * @return WP_Comment|array|null Depends on $output value.
    170170 */
    171171function get_comment(&$comment, $output = OBJECT) {
    172     global $wpdb;
    173 
    174     if ( empty($comment) ) {
    175         if ( isset($GLOBALS['comment']) )
    176             $_comment = & $GLOBALS['comment'];
    177         else
    178             $_comment = null;
    179     } elseif ( is_object($comment) ) {
    180         wp_cache_add($comment->comment_ID, $comment, 'comment');
     172    if ( empty( $comment ) && isset( $GLOBALS['comment'] ) ) {
     173        $comment = $GLOBALS['comment'];
     174    }
     175
     176    if ( $comment instanceof WP_Comment ) {
    181177        $_comment = $comment;
     178    } elseif ( is_object( $comment ) ) {
     179        $_comment = new WP_Comment( $comment );
    182180    } else {
    183         if ( isset($GLOBALS['comment']) && ($GLOBALS['comment']->comment_ID == $comment) ) {
    184             $_comment = & $GLOBALS['comment'];
    185         } elseif ( ! $_comment = wp_cache_get($comment, 'comment') ) {
    186             $_comment = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_ID = %d LIMIT 1", $comment));
    187             if ( ! $_comment )
    188                 return null;
    189             wp_cache_add($_comment->comment_ID, $_comment, 'comment');
    190         }
     181        $_comment = WP_Comment::get_instance( $comment );
     182    }
     183
     184    if ( ! $_comment ) {
     185        return null;
    191186    }
    192187
     
    203198        return $_comment;
    204199    } elseif ( $output == ARRAY_A ) {
    205         $__comment = get_object_vars($_comment);
    206         return $__comment;
     200        return $_comment->to_array();
    207201    } elseif ( $output == ARRAY_N ) {
    208         $__comment = array_values(get_object_vars($_comment));
    209         return $__comment;
    210     } else {
    211         return $_comment;
    212     }
     202        return array_values( $_comment->to_array() );
     203    }
     204    return $_comment;
    213205}
    214206
     
    480472 * to recall previous comments by this commentator that are still held in moderation.
    481473 *
    482  * @param object $comment Comment object.
    483  * @param object $user Comment author's object.
     474 * @param WP_Comment $comment Comment object.
     475 * @param object     $user    Comment author's object.
    484476 *
    485477 * @since 3.4.0
     
    761753 * @global WP_Query $wp_query
    762754 *
    763  * @param array $comments Optional array of comment objects. Defaults to $wp_query->comments
     755 * @param array $comments Optional array of WP_Comment objects. Defaults to $wp_query->comments
    764756 * @param int   $per_page Optional comments per page.
    765757 * @param bool  $threaded Optional control over flat or threaded comments.
     
    12891281         * @since 2.7.0
    12901282         *
    1291          * @param object $comment Comment object.
     1283         * @param WP_Comment $comment Comment object.
    12921284         */
    12931285        do_action( "comment_{$old_status}_to_{$new_status}", $comment );
     
    13041296     * @since 2.7.0
    13051297     *
    1306      * @param int $comment_ID The comment ID.
    1307      * @param obj $comment    Comment object.
     1298     * @param int        $comment_ID The comment ID.
     1299     * @param WP_Comment $comment    Comment object.
    13081300     */
    13091301    do_action( "comment_{$new_status}_{$comment->comment_type}", $comment->comment_ID, $comment );
     
    14241416     * @since 2.8.0
    14251417     *
    1426      * @param int $id      The comment ID.
    1427      * @param obj $comment Comment object.
     1418     * @param int        $id      The comment ID.
     1419     * @param WP_Comment $comment Comment object.
    14281420     */
    14291421    do_action( 'wp_insert_comment', $id, $comment );
Note: See TracChangeset for help on using the changeset viewer.