WordPress GSoC

Changeset 2126


Ignore:
Timestamp:
07/17/2013 12:18:56 PM (13 years ago)
Author:
rmccue
Message:

Add comment endpoints. See #320

Location:
2013/rmccue/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • 2013/rmccue/trunk/docs/wp-json.md

    r2003 r2126  
    315315The `value` field of the metadata field is a string with the metadata
    316316field value.
     317
     318
     319Comment
     320-------
     321The Comment entity is a JSON object with comment properties. The following
     322properties are defined for the Comment entity object:
     323
     324### `ID`
     325The `ID` field is an integer with the comment's ID.
     326
     327### `content`
     328The `content` field is a string with the comment's content.
     329
     330### `status`
     331The `status` field is a string with the comment's status. This field indicates
     332whether the comment is in the publishing process, or if it has been deleted or
     333marked as spam.
     334
     335        comment-status = "hold" / "approved" / "spam" / "trash" / token
     336
     337Providers MAY use other values to indicate other statuses. Consumers who
     338encounter an unknown or missing status SHOULD treat it as "hold".
     339
     340### `type`
     341The `type` field is a string with the comment's type. This is usually one of the
     342following, but providers may provide additional values.
     343
     344        comment-type = "comment" / "trackback" / "pingback" / token
     345
     346Providers MAY use other values to indicate other types. Consumers who encounter
     347an unknown or missing status SHOULD treat it as "comment".
     348
     349### `post`
     350The `post` field is an integer with the parent post for the comment, or a Post
     351entity describing the parent post. A literal zero indicates that the comment
     352does not have a parent post.
     353
     354        comment-post-parent = "0" / 1*DIGIT
     355
     356Consumers who encounter a missing post ID MUST treat it the same as a parent
     357post ID of 0.
     358
     359### `parent`
     360The `post` field is an integer with the parent comment, or a Comment entity
     361describing the parent comment. A literal zero indicates that the comment does
     362not have a parent comment.
     363
     364        comment-parent = "0" / 1*DIGIT
     365
     366Consumers who encounter a missing parent ID MUST treat it the same as a parent
     367comment ID of 0.
     368
     369### `author`
     370The `author` field is a User entity with the comment author's data, or a
     371User-like object for anonymous authors. The User-like object contains the
     372following properties:
     373
     374#### `ID`
     375The `ID` property on the User-like object is always set to `0` for anonymous
     376authors.
     377
     378#### `name`
     379The `name` property on the User-like object is a string with the author's name.
     380
     381#### `URL`
     382The `URL` property on the User-like object is a string with the author's URL.
     383
     384#### `avatar`
     385The `avatar` property on the User-like object is a string with the URL to the
     386author's avatar image.
     387
     388This property should be treated the same as the avatar property on the
     389User entity.
     390
     391
     392### `date`, `date_gmt`
     393The `date` and `date_gmt` fields are strings with the post's creation date and
     394time in the local time and UTC respectively. These fields follow the [RFC3339][]
     395Section 5.6 datetime representation.
     396
     397        date     = date-time
     398        date_gmt = date-time
     399
     400This field should be treated the same as the `date` and `date_gmt` properties on
     401a Post entity.
     402
     403[RFC3339]: http://tools.ietf.org/html/rfc3339
     404
     405### `date_tz`, `modified_tz`
     406The `date_tz` and `modified_tz` fields are strings with the timezone applying to
     407the `date` and `modified` fields respectively. The timezone is a [Olsen zoneinfo
     408database][] identifier. While the `date` field includes timezone offset
     409information, the `date_tz` field allows proper data operations across Daylight
     410Savings Time boundaries.
     411
     412This field should be treated the same as the `date_tz` property on a
     413Post entity.
    317414
    318415
  • 2013/rmccue/trunk/lib/class-wp-json-server.php

    r2125 r2126  
    226226                        // Comments
    227227                        '/posts/(?P<id>\d+)/comments'                  => array(
    228                                 array( '__return_null', self::READABLE ),
     228                                array( array( $this, 'getComments' ), self::READABLE ),
    229229                                array( '__return_null', self::CREATABLE | self::ACCEPT_JSON ),
    230230                        ),
    231231                        '/posts/(?P<id>\d+)/comments/(?P<comment>\d+)' => array(
    232                                 array( '__return_null', self::READABLE ),
     232                                array( array( $this, 'getComment' ), self::READABLE ),
    233233                                array( '__return_null', self::EDITABLE | self::ACCEPT_JSON ),
    234234                                array( '__return_null', self::DELETABLE ),
    … …  
    703703                        return array( 'message' => __( 'Deleted post' ) );
    704704                }
     705        }
     706
     707        /**
     708         * Retrieve comments
     709         *
     710         * @param int $id Post ID to retrieve comments for
     711         * @return array List of Comment entities
     712         */
     713        public function getComments( $id ) {
     714                //$args = array('status' => $status, 'post_id' => $id, 'offset' => $offset, 'number' => $number )l
     715                $comments = get_comments( array('post_id' => $id) );
     716
     717                $struct = array();
     718                foreach ( $comments as $comment ) {
     719                        $struct[] = $this->prepare_comment( $comment, array( 'comment', 'meta' ), 'collection' );
     720                }
     721                return $struct;
     722        }
     723
     724        /**
     725         * Retrieve a single comment
     726         *
     727         * @param int $comment Comment ID
     728         * @return array Comment entity
     729         */
     730        public function getComment( $comment ) {
     731                $comment = get_comment( $comment );
     732                $data = $this->prepare_comment( $comment );
     733                return $data;
    705734        }
    706735
    … …  
    14311460
    14321461        /**
     1462         * Prepares comment data for returning as a JSON response.
     1463         *
     1464         * @param stdClass $comment Comment object
     1465         * @param array $requested_fields Fields to retrieve from the comment
     1466         * @param string $context Where is the comment being loaded?
     1467         * @return array Comment data for JSON serialization
     1468         */
     1469        protected function prepare_comment( $comment, $requested_fields = array( 'comment', 'meta' ), $context = 'single' ) {
     1470                $fields = array(
     1471                        'ID' => (int) $comment->comment_ID,
     1472                        'post' => (int) $comment->comment_post_ID,
     1473                );
     1474
     1475                // Content
     1476                $fields['content'] = apply_filters( 'comment_text', $comment->comment_content, $comment );
     1477                // $fields['content_raw'] = $comment->comment_content;
     1478
     1479                // Status
     1480                switch ( $comment->comment_approved ) {
     1481                        case 'hold':
     1482                        case '0':
     1483                                $fields['status'] = 'hold';
     1484                                break;
     1485
     1486                        case 'approve':
     1487                        case '1':
     1488                                $fields['status'] = 'approved';
     1489                                break;
     1490
     1491                        case 'spam':
     1492                        case 'trash':
     1493                        default:
     1494                                $fields['status'] = $comment->comment_approved;
     1495                }
     1496
     1497                // Type
     1498                $fields['type'] = apply_filters( 'get_comment_type', $comment->comment_type );
     1499                if ( empty( $fields['type'] ) ) {
     1500                        $fields['type'] = 'comment';
     1501                }
     1502
     1503                // Post
     1504                if ( 'single' === $context ) {
     1505                        $parent_fields = array( 'meta' );
     1506                        if ( $context === 'single' )
     1507                                $parent_fields[] = 'post';
     1508                        $parent = get_post( $post['post_parent'], ARRAY_A );
     1509                        $fields['parent'] = $this->prepare_post( $parent, $parent_fields, 'single-parent' );
     1510                }
     1511
     1512                // Parent
     1513                if ( ( 'single' === $context || 'single-parent' === $context ) && (int) $comment->comment_parent ) {
     1514                        $parent_fields = array( 'meta' );
     1515                        if ( $context === 'single' )
     1516                                $parent_fields[] = 'comment';
     1517                        $parent = get_comment( $post['post_parent'] );
     1518                        $fields['parent'] = $this->prepare_comment( $parent, $parent_fields, 'single-parent' );
     1519                }
     1520
     1521                // Parent
     1522                $fields['parent'] = (int) $comment->comment_parent;
     1523
     1524                // Author
     1525                if ( (int) $comment->user_id !== 0 ) {
     1526                        $fields['author'] = $this->prepare_author( (int) $comment->user_id );
     1527                }
     1528                else {
     1529                        $fields['author'] = array(
     1530                                'ID' => 0,
     1531                                'name' => $comment->comment_author,
     1532                                'URL' => $comment->comment_author_url,
     1533                                'avatar' => $this->get_avatar( $comment->comment_author_email ),
     1534                        );
     1535                }
     1536
     1537                // Date
     1538                $timezone = $this->get_timezone();
     1539
     1540                $date = DateTime::createFromFormat( 'Y-m-d H:i:s', $comment->comment_date, $timezone );
     1541                $fields['date'] = $date->format( 'c' );
     1542                $fields['date_tz'] = $date->format( 'e' );
     1543                $fields['date_gmt'] = date( 'c', strtotime( $post->comment_date_gmt ) );
     1544
     1545                // Meta
     1546                $meta = array(
     1547                        'links' => array(
     1548                                'in-reply-to' => json_url( sprintf( '/posts/%d', (int) $comment->comment_post_ID ) )
     1549                        ),
     1550                );
     1551                if ( 0 !== (int) $comment->comment_parent ) {
     1552                        $meta['links']['in-reply-to'] .= ',' . json_url( sprintf( '/posts/%d/comments/%d', (int) $comment->comment_post_ID, (int) $comment->comment_parent ) );
     1553                }
     1554                if ( 'single' !== $context ) {
     1555                        $meta['links']['self'] = json_url( sprintf( '/posts/%d/comments/%d', (int) $comment->comment_post_ID, (int) $comment->comment_ID ) );
     1556                }
     1557
     1558                // Remove unneeded fields
     1559                $data = array();
     1560                if ( in_array( 'comment', $requested_fields ) )
     1561                        $data = array_merge( $data, $fields );
     1562
     1563                if ( in_array( 'meta', $requested_fields ) )
     1564                        $data = array_merge( $data, $meta );
     1565
     1566                return $data;
     1567        }
     1568
     1569        /**
    14331570         * Get the timezone object for the site
    14341571         *
Note: See TracChangeset for help on using the changeset viewer.