<!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>[2265] 2013/rmccue/trunk/lib/class-wp-json-posts.php: Goodbye fields, hello context.</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/2265">2265</a></dd>
<dt>Author</dt> <dd>rmccue</dd>
<dt>Date</dt> <dd>2013-09-10 18:31:55 +0000 (Tue, 10 Sep 2013)</dd>
</dl>
<h3>Log Message</h3>
<pre>Goodbye fields, hello context.
See <a href="http://gsoc.trac.wordpress.org/ticket/328">#328</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#2013rmccuetrunklibclasswpjsonpostsphp">2013/rmccue/trunk/lib/class-wp-json-posts.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="2013rmccuetrunklibclasswpjsonpostsphp"></a>
<div class="modfile"><h4>Modified: 2013/rmccue/trunk/lib/class-wp-json-posts.php (2264 => 2265)</h4>
<pre class="diff"><span>
<span class="info">--- 2013/rmccue/trunk/lib/class-wp-json-posts.php 2013-09-10 18:31:50 UTC (rev 2264)
+++ 2013/rmccue/trunk/lib/class-wp-json-posts.php 2013-09-10 18:31:55 UTC (rev 2265)
</span><span class="lines">@@ -21,10 +21,8 @@
</span><span class="cx"> * @param array $fields optional
</span><span class="cx"> * @return array contains a collection of Post entities.
</span><span class="cx"> */
</span><del>- public function getPosts( $filter = array(), $fields = array(), $type = 'post', $page = 1 ) {
</del><ins>+ public function getPosts( $filter = array(), $context = 'view', $type = 'post', $page = 1 ) {
</ins><span class="cx"> global $wp_json_server;
</span><del>- if ( empty($fields) || in_array( 'default', $fields ) )
- $fields = array_merge( $fields, apply_filters( 'json_default_post_fields', array( 'post', 'meta', 'terms' ), 'getPosts' ) );
</del><span class="cx">
</span><span class="cx"> $query = array();
</span><span class="cx">
</span><span class="lines">@@ -88,7 +86,7 @@
</span><span class="cx"> continue;
</span><span class="cx">
</span><span class="cx"> $wp_json_server->link_header( 'item', json_url( '/posts/' . $post['ID'] ), array( 'title' => $post['post_title'] ) );
</span><del>- $struct[] = $this->prepare_post( $post, $fields );
</del><ins>+ $struct[] = $this->prepare_post( $post, $context );
</ins><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> return $struct;
</span><span class="lines">@@ -147,7 +145,7 @@
</span><span class="cx"> * @param array $fields Post fields to return (optional)
</span><span class="cx"> * @return array Post entity
</span><span class="cx"> */
</span><del>- public function getPost( $id, $fields = array() ) {
</del><ins>+ public function getPost( $id, $context = 'view' ) {
</ins><span class="cx"> global $wp_json_server;
</span><span class="cx"> $id = (int) $id;
</span><span class="cx">
</span><span class="lines">@@ -156,9 +154,6 @@
</span><span class="cx">
</span><span class="cx"> $post = get_post( $id, ARRAY_A );
</span><span class="cx">
</span><del>- if ( empty( $fields ) || in_array( 'default', $fields ) )
- $fields = array_merge( $fields, apply_filters( 'json_default_post_fields', array( 'post', 'post-extended', 'meta', 'terms', 'custom_fields' ), 'getPost' ) );
-
</del><span class="cx"> if ( empty( $post['ID'] ) )
</span><span class="cx"> return new WP_Error( 'json_post_invalid_id', __( 'Invalid post ID.' ), array( 'status' => 404 ) );
</span><span class="cx">
</span><span class="lines">@@ -170,7 +165,7 @@
</span><span class="cx">
</span><span class="cx"> header( 'Last-Modified: ' . mysql2date( 'D, d M Y H:i:s', $post['post_modified_gmt'] ) . 'GMT' );
</span><span class="cx">
</span><del>- $post = $this->prepare_post( $post, $fields );
</del><ins>+ $post = $this->prepare_post( $post, $context );
</ins><span class="cx"> if ( is_wp_error( $post ) )
</span><span class="cx"> return $post;
</span><span class="cx">
</span><span class="lines">@@ -403,7 +398,7 @@
</span><span class="cx"> * @param array $fields The subset of post type fields to return
</span><span class="cx"> * @return array The prepared post data
</span><span class="cx"> */
</span><del>- protected function prepare_post( $post, $fields, $context = 'single' ) {
</del><ins>+ protected function prepare_post( $post, $context = 'view' ) {
</ins><span class="cx"> global $wp_json_server;
</span><span class="cx">
</span><span class="cx"> // holds the data for this post. built up based on $fields
</span><span class="lines">@@ -480,64 +475,51 @@
</span><span class="cx">
</span><span class="cx"> $post_fields['author'] = $this->prepare_author( $post['post_author'] );
</span><span class="cx">
</span><del>- if ( ( 'single' === $context || 'single-parent' === $context ) && 0 !== $post['post_parent'] ) {
</del><ins>+ if ( 'view' === $context && 0 !== $post['post_parent'] ) {
</ins><span class="cx"> // Avoid nesting too deeply
</span><span class="cx"> // This gives post + post-extended + meta for the main post,
</span><span class="cx"> // post + meta for the parent and just meta for the grandparent
</span><del>- $parent_fields = array( 'meta' );
- if ( $context === 'single' )
- $parent_fields[] = 'post';
</del><span class="cx"> $parent = get_post( $post['post_parent'], ARRAY_A );
</span><del>- $post_fields['parent'] = $this->prepare_post( $parent, $parent_fields, 'single-parent' );
</del><ins>+ $post_fields['parent'] = $this->prepare_post( $parent, 'parent' );
</ins><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> // Merge requested $post_fields fields into $_post
</span><del>- if ( in_array( 'post', $fields ) ) {
- $_post = array_merge( $_post, $post_fields );
- } else {
- $requested_fields = array_intersect_key( $post_fields, array_flip( $fields ) );
- $_post = array_merge( $_post, $requested_fields );
- }
</del><ins>+ $_post = array_merge( $_post, $post_fields );
</ins><span class="cx">
</span><del>- if ( in_array( 'post-extended', $fields ) )
- $_post = array_merge( $_post, $post_fields_extended );
</del><ins>+ // Include extended fields. We might come back to this.
+ $_post = array_merge( $_post, $post_fields_extended );
</ins><span class="cx">
</span><del>- if ( in_array( 'post-raw', $fields ) && current_user_can( $post_type->cap->edit_post, $post['ID'] ) )
</del><ins>+ if ( 'edit' === $context && current_user_can( $post_type->cap->edit_post, $post['ID'] ) )
</ins><span class="cx"> $_post = array_merge( $_post, $post_fields_raw );
</span><del>- elseif ( in_array( 'post-raw', $fields ) )
</del><ins>+ elseif ( 'edit' === $context )
</ins><span class="cx"> return new WP_Error( 'json_cannot_edit', __( 'Sorry, you cannot edit this post' ), array( 'status' => 403 ) );
</span><span class="cx">
</span><span class="cx"> // Taxonomies
</span><del>- $all_taxonomy_fields = in_array( 'taxonomies', $fields );
-
- if ( $all_taxonomy_fields || in_array( 'terms', $fields ) ) {
- $post_type_taxonomies = get_object_taxonomies( $post['post_type'] );
- $terms = wp_get_object_terms( $post['ID'], $post_type_taxonomies );
- $_post['terms'] = array();
- foreach ( $terms as $term ) {
- $_post['terms'][ $term->taxonomy ] = $this->prepare_term( $term );
- }
</del><ins>+ $post_type_taxonomies = get_object_taxonomies( $post['post_type'] );
+ $terms = wp_get_object_terms( $post['ID'], $post_type_taxonomies );
+ $_post['terms'] = array();
+ foreach ( $terms as $term ) {
+ $_post['terms'][ $term->taxonomy ] = $this->prepare_term( $term );
</ins><span class="cx"> }
</span><span class="cx">
</span><del>- if ( in_array( 'custom_fields', $fields ) )
- $_post['post_meta'] = $this->prepare_meta( $post['ID'] );
</del><ins>+ // Post meta
+ $_post['post_meta'] = $this->prepare_meta( $post['ID'] );
</ins><span class="cx">
</span><del>- if ( in_array( 'meta', $fields ) ) {
- $_post['meta'] = array(
- 'links' => array(
- 'self' => json_url( '/posts/' . $post['ID'] ),
- 'author' => json_url( '/users/' . $post['post_author'] ),
- 'collection' => json_url( '/posts' ),
- 'replies' => json_url( '/posts/' . $post['ID'] . '/comments' ),
- 'version-history' => json_url( '/posts/' . $post['ID'] . '/revisions' ),
- ),
- );
</del><ins>+ // Entity meta
+ $_post['meta'] = array(
+ 'links' => array(
+ 'self' => json_url( '/posts/' . $post['ID'] ),
+ 'author' => json_url( '/users/' . $post['post_author'] ),
+ 'collection' => json_url( '/posts' ),
+ 'replies' => json_url( '/posts/' . $post['ID'] . '/comments' ),
+ 'version-history' => json_url( '/posts/' . $post['ID'] . '/revisions' ),
+ ),
+ );
</ins><span class="cx">
</span><del>- if ( ! empty( $post['post_parent'] ) )
- $_post['meta']['links']['up'] = json_url( '/posts/' . (int) $post['post_parent'] );
- }
</del><ins>+ if ( ! empty( $post['post_parent'] ) )
+ $_post['meta']['links']['up'] = json_url( '/posts/' . (int) $post['post_parent'] );
</ins><span class="cx">
</span><del>- return apply_filters( 'json_prepare_post', $_post, $post, $fields );
</del><ins>+ return apply_filters( 'json_prepare_post', $_post, $post, $context );
</ins><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> /**
</span></span></pre>
</div>
</div>
</body>
</html>