<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[2126] 2013/rmccue/trunk: Add comment endpoints.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://gsoc.trac.wordpress.org/changeset/2126">2126</a></dd>
<dt>Author</dt> <dd>rmccue</dd>
<dt>Date</dt> <dd>2013-07-17 12:18:56 +0000 (Wed, 17 Jul 2013)</dd>
</dl>
<h3>Log Message</h3>
<pre>Add comment endpoints. See <a href="http://gsoc.trac.wordpress.org/ticket/320">#320</a></pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#2013rmccuetrunkdocswpjsonmd">2013/rmccue/trunk/docs/wp-json.md</a></li>
<li><a href="#2013rmccuetrunklibclasswpjsonserverphp">2013/rmccue/trunk/lib/class-wp-json-server.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="2013rmccuetrunkdocswpjsonmd"></a>
<div class="modfile"><h4>Modified: 2013/rmccue/trunk/docs/wp-json.md (2125 => 2126)</h4>
<pre class="diff"><span>
<span class="info">--- 2013/rmccue/trunk/docs/wp-json.md 2013-07-17 12:18:49 UTC (rev 2125)
+++ 2013/rmccue/trunk/docs/wp-json.md 2013-07-17 12:18:56 UTC (rev 2126)
</span><span class="lines">@@ -316,6 +316,103 @@
</span><span class="cx"> field value.
</span><span class="cx">
</span><span class="cx">
</span><ins>+Comment
+-------
+The Comment entity is a JSON object with comment properties. The following
+properties are defined for the Comment entity object:
+
+### `ID`
+The `ID` field is an integer with the comment's ID.
+
+### `content`
+The `content` field is a string with the comment's content.
+
+### `status`
+The `status` field is a string with the comment's status. This field indicates
+whether the comment is in the publishing process, or if it has been deleted or
+marked as spam.
+
+ comment-status = "hold" / "approved" / "spam" / "trash" / token
+
+Providers MAY use other values to indicate other statuses. Consumers who
+encounter an unknown or missing status SHOULD treat it as "hold".
+
+### `type`
+The `type` field is a string with the comment's type. This is usually one of the
+following, but providers may provide additional values.
+
+ comment-type = "comment" / "trackback" / "pingback" / token
+
+Providers MAY use other values to indicate other types. Consumers who encounter
+an unknown or missing status SHOULD treat it as "comment".
+
+### `post`
+The `post` field is an integer with the parent post for the comment, or a Post
+entity describing the parent post. A literal zero indicates that the comment
+does not have a parent post.
+
+ comment-post-parent = "0" / 1*DIGIT
+
+Consumers who encounter a missing post ID MUST treat it the same as a parent
+post ID of 0.
+
+### `parent`
+The `post` field is an integer with the parent comment, or a Comment entity
+describing the parent comment. A literal zero indicates that the comment does
+not have a parent comment.
+
+ comment-parent = "0" / 1*DIGIT
+
+Consumers who encounter a missing parent ID MUST treat it the same as a parent
+comment ID of 0.
+
+### `author`
+The `author` field is a User entity with the comment author's data, or a
+User-like object for anonymous authors. The User-like object contains the
+following properties:
+
+#### `ID`
+The `ID` property on the User-like object is always set to `0` for anonymous
+authors.
+
+#### `name`
+The `name` property on the User-like object is a string with the author's name.
+
+#### `URL`
+The `URL` property on the User-like object is a string with the author's URL.
+
+#### `avatar`
+The `avatar` property on the User-like object is a string with the URL to the
+author's avatar image.
+
+This property should be treated the same as the avatar property on the
+User entity.
+
+
+### `date`, `date_gmt`
+The `date` and `date_gmt` fields are strings with the post's creation date and
+time in the local time and UTC respectively. These fields follow the [RFC3339][]
+Section 5.6 datetime representation.
+
+ date = date-time
+ date_gmt = date-time
+
+This field should be treated the same as the `date` and `date_gmt` properties on
+a Post entity.
+
+[RFC3339]: http://tools.ietf.org/html/rfc3339
+
+### `date_tz`, `modified_tz`
+The `date_tz` and `modified_tz` fields are strings with the timezone applying to
+the `date` and `modified` fields respectively. The timezone is a [Olsen zoneinfo
+database][] identifier. While the `date` field includes timezone offset
+information, the `date_tz` field allows proper data operations across Daylight
+Savings Time boundaries.
+
+This field should be treated the same as the `date_tz` property on a
+Post entity.
+
+
</ins><span class="cx"> Documents
</span><span class="cx"> =========
</span><span class="cx">
</span></span></pre></div>
<a id="2013rmccuetrunklibclasswpjsonserverphp"></a>
<div class="modfile"><h4>Modified: 2013/rmccue/trunk/lib/class-wp-json-server.php (2125 => 2126)</h4>
<pre class="diff"><span>
<span class="info">--- 2013/rmccue/trunk/lib/class-wp-json-server.php 2013-07-17 12:18:49 UTC (rev 2125)
+++ 2013/rmccue/trunk/lib/class-wp-json-server.php 2013-07-17 12:18:56 UTC (rev 2126)
</span><span class="lines">@@ -225,11 +225,11 @@
</span><span class="cx">
</span><span class="cx"> // Comments
</span><span class="cx"> '/posts/(?P<id>\d+)/comments' => array(
</span><del>- array( '__return_null', self::READABLE ),
</del><ins>+ array( array( $this, 'getComments' ), self::READABLE ),
</ins><span class="cx"> array( '__return_null', self::CREATABLE | self::ACCEPT_JSON ),
</span><span class="cx"> ),
</span><span class="cx"> '/posts/(?P<id>\d+)/comments/(?P<comment>\d+)' => array(
</span><del>- array( '__return_null', self::READABLE ),
</del><ins>+ array( array( $this, 'getComment' ), self::READABLE ),
</ins><span class="cx"> array( '__return_null', self::EDITABLE | self::ACCEPT_JSON ),
</span><span class="cx"> array( '__return_null', self::DELETABLE ),
</span><span class="cx"> ),
</span><span class="lines">@@ -705,6 +705,35 @@
</span><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> /**
</span><ins>+ * Retrieve comments
+ *
+ * @param int $id Post ID to retrieve comments for
+ * @return array List of Comment entities
+ */
+ public function getComments( $id ) {
+ //$args = array('status' => $status, 'post_id' => $id, 'offset' => $offset, 'number' => $number )l
+ $comments = get_comments( array('post_id' => $id) );
+
+ $struct = array();
+ foreach ( $comments as $comment ) {
+ $struct[] = $this->prepare_comment( $comment, array( 'comment', 'meta' ), 'collection' );
+ }
+ return $struct;
+ }
+
+ /**
+ * Retrieve a single comment
+ *
+ * @param int $comment Comment ID
+ * @return array Comment entity
+ */
+ public function getComment( $comment ) {
+ $comment = get_comment( $comment );
+ $data = $this->prepare_comment( $comment );
+ return $data;
+ }
+
+ /**
</ins><span class="cx"> * Get all public post types
</span><span class="cx"> *
</span><span class="cx"> * @uses self::getPostType()
</span><span class="lines">@@ -1430,6 +1459,114 @@
</span><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> /**
</span><ins>+ * Prepares comment data for returning as a JSON response.
+ *
+ * @param stdClass $comment Comment object
+ * @param array $requested_fields Fields to retrieve from the comment
+ * @param string $context Where is the comment being loaded?
+ * @return array Comment data for JSON serialization
+ */
+ protected function prepare_comment( $comment, $requested_fields = array( 'comment', 'meta' ), $context = 'single' ) {
+ $fields = array(
+ 'ID' => (int) $comment->comment_ID,
+ 'post' => (int) $comment->comment_post_ID,
+ );
+
+ // Content
+ $fields['content'] = apply_filters( 'comment_text', $comment->comment_content, $comment );
+ // $fields['content_raw'] = $comment->comment_content;
+
+ // Status
+ switch ( $comment->comment_approved ) {
+ case 'hold':
+ case '0':
+ $fields['status'] = 'hold';
+ break;
+
+ case 'approve':
+ case '1':
+ $fields['status'] = 'approved';
+ break;
+
+ case 'spam':
+ case 'trash':
+ default:
+ $fields['status'] = $comment->comment_approved;
+ }
+
+ // Type
+ $fields['type'] = apply_filters( 'get_comment_type', $comment->comment_type );
+ if ( empty( $fields['type'] ) ) {
+ $fields['type'] = 'comment';
+ }
+
+ // Post
+ if ( 'single' === $context ) {
+ $parent_fields = array( 'meta' );
+ if ( $context === 'single' )
+ $parent_fields[] = 'post';
+ $parent = get_post( $post['post_parent'], ARRAY_A );
+ $fields['parent'] = $this->prepare_post( $parent, $parent_fields, 'single-parent' );
+ }
+
+ // Parent
+ if ( ( 'single' === $context || 'single-parent' === $context ) && (int) $comment->comment_parent ) {
+ $parent_fields = array( 'meta' );
+ if ( $context === 'single' )
+ $parent_fields[] = 'comment';
+ $parent = get_comment( $post['post_parent'] );
+ $fields['parent'] = $this->prepare_comment( $parent, $parent_fields, 'single-parent' );
+ }
+
+ // Parent
+ $fields['parent'] = (int) $comment->comment_parent;
+
+ // Author
+ if ( (int) $comment->user_id !== 0 ) {
+ $fields['author'] = $this->prepare_author( (int) $comment->user_id );
+ }
+ else {
+ $fields['author'] = array(
+ 'ID' => 0,
+ 'name' => $comment->comment_author,
+ 'URL' => $comment->comment_author_url,
+ 'avatar' => $this->get_avatar( $comment->comment_author_email ),
+ );
+ }
+
+ // Date
+ $timezone = $this->get_timezone();
+
+ $date = DateTime::createFromFormat( 'Y-m-d H:i:s', $comment->comment_date, $timezone );
+ $fields['date'] = $date->format( 'c' );
+ $fields['date_tz'] = $date->format( 'e' );
+ $fields['date_gmt'] = date( 'c', strtotime( $post->comment_date_gmt ) );
+
+ // Meta
+ $meta = array(
+ 'links' => array(
+ 'in-reply-to' => json_url( sprintf( '/posts/%d', (int) $comment->comment_post_ID ) )
+ ),
+ );
+ if ( 0 !== (int) $comment->comment_parent ) {
+ $meta['links']['in-reply-to'] .= ',' . json_url( sprintf( '/posts/%d/comments/%d', (int) $comment->comment_post_ID, (int) $comment->comment_parent ) );
+ }
+ if ( 'single' !== $context ) {
+ $meta['links']['self'] = json_url( sprintf( '/posts/%d/comments/%d', (int) $comment->comment_post_ID, (int) $comment->comment_ID ) );
+ }
+
+ // Remove unneeded fields
+ $data = array();
+ if ( in_array( 'comment', $requested_fields ) )
+ $data = array_merge( $data, $fields );
+
+ if ( in_array( 'meta', $requested_fields ) )
+ $data = array_merge( $data, $meta );
+
+ return $data;
+ }
+
+ /**
</ins><span class="cx"> * Get the timezone object for the site
</span><span class="cx"> *
</span><span class="cx"> * @return DateTimeZone
</span></span></pre>
</div>
</div>
</body>
</html>