<!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>[2267] 2013/rmccue/trunk: Add page support</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/2267">2267</a></dd>
<dt>Author</dt> <dd>rmccue</dd>
<dt>Date</dt> <dd>2013-09-10 18:32:06 +0000 (Tue, 10 Sep 2013)</dd>
</dl>
<h3>Log Message</h3>
<pre>Add page support
See <a href="http://gsoc.trac.wordpress.org/ticket/271">#271</a></pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#2013rmccuetrunklibclasswpjsonserverphp">2013/rmccue/trunk/lib/class-wp-json-server.php</a></li>
<li><a href="#2013rmccuetrunkpluginphp">2013/rmccue/trunk/plugin.php</a></li>
</ul>
<h3>Added Paths</h3>
<ul>
<li><a href="#2013rmccuetrunklibclasswpjsonpagesphp">2013/rmccue/trunk/lib/class-wp-json-pages.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="2013rmccuetrunklibclasswpjsonpagesphp"></a>
<div class="addfile"><h4>Added: 2013/rmccue/trunk/lib/class-wp-json-pages.php (0 => 2267)</h4>
<pre class="diff"><span>
<span class="info">--- 2013/rmccue/trunk/lib/class-wp-json-pages.php (rev 0)
+++ 2013/rmccue/trunk/lib/class-wp-json-pages.php 2013-09-10 18:32:06 UTC (rev 2267)
</span><span class="lines">@@ -0,0 +1,164 @@
</span><ins>+<?php
+/**
+ * Page post type handlers
+ *
+ * @package WordPress
+ * @subpackage JSON API
+ */
+
+/**
+ * Page post type handlers
+ *
+ * This class serves as a small addition on top of the basic post handlers to
+ * add small functionality on top of the existing API.
+ *
+ * In addition, this class serves as a sample implementation of building on top
+ * of the existing APIs for custom post types.
+ *
+ * @package WordPress
+ * @subpackage JSON API
+ */
+class WP_JSON_Pages extends WP_JSON_Posts {
+ /**
+ * Retrieve pages
+ *
+ * Overrides the $type to set to 'page', then passes through to the post
+ * endpoints.
+ *
+ * @see WP_JSON_Posts::getPosts()
+ */
+ public function getPosts( $filter = array(), $context = 'view', $type = 'page', $page = 1 ) {
+ if ( $type !== 'page' )
+ return new WP_Error( 'json_post_invalid_type', __( 'Invalid post type' ), array( 'status' => 400 ) );
+
+ return parent::getPosts( $filter, $context, 'page', $page );
+ }
+
+ /**
+ * Retrieve a page
+ *
+ * @see WP_JSON_Posts::getPost()
+ */
+ public function getPost( $id, $context = 'view' ) {
+ $id = (int) $id;
+
+ if ( empty( $id ) )
+ return new WP_Error( 'json_post_invalid_id', __( 'Invalid post ID.' ), array( 'status' => 404 ) );
+
+ $post = get_post( $id, ARRAY_A );
+
+ if ( $post['post_type'] !== 'page' )
+ return new WP_Error( 'json_post_invalid_type', __( 'Invalid post type' ), array( 'status' => 400 ) );
+
+ return parent::getPost( $id, $context );
+ }
+
+ /**
+ * Edit a page
+ *
+ * @see WP_JSON_Posts::editPost()
+ */
+ function editPost( $id, $data, $_headers = array() ) {
+ $id = (int) $id;
+ if ( empty( $id ) )
+ return new WP_Error( 'json_post_invalid_id', __( 'Invalid post ID.' ), array( 'status' => 404 ) );
+
+ $post = get_post( $id, ARRAY_A );
+
+ if ( empty( $post['ID'] ) )
+ return new WP_Error( 'json_post_invalid_id', __( 'Invalid post ID.' ), array( 'status' => 404 ) );
+
+ if ( $post['post_type'] !== 'page' )
+ return new WP_Error( 'json_post_invalid_type', __( 'Invalid post type' ), array( 'status' => 400 ) );
+
+ return parent::editPost( $id, $data, $_headers );
+ }
+
+ /**
+ * Delete a page
+ *
+ * @see WP_JSON_Posts::deletePost()
+ */
+ public function deletePost( $id, $force = false ) {
+ $id = (int) $id;
+
+ if ( empty( $id ) )
+ return new WP_Error( 'json_post_invalid_id', __( 'Invalid post ID.' ), array( 'status' => 404 ) );
+
+ $post = get_post( $id, ARRAY_A );
+
+ if ( $post['post_type'] !== 'page' )
+ return new WP_Error( 'json_post_invalid_type', __( 'Invalid post type' ), array( 'status' => 400 ) );
+
+ return parent::deletePost( $id, $force );
+ }
+
+ /**
+ * Retrieve a page by path name
+ *
+ * @param string $path
+ */
+ public function getPostByPath( $path, $context = 'view' ) {
+ $post = get_page_by_path( $path, ARRAY_A );
+
+ if ( empty( $post ) )
+ return new WP_Error( 'json_post_invalid_id', __( 'Invalid post ID.' ), array( 'status' => 404 ) );
+
+ return $this->getPost( $post['ID'], $context );
+ }
+
+ /**
+ * Edit a page by path name
+ *
+ * @param string $path
+ */
+ public function editPostByPath( $path, $data, $_headers = array() ) {
+ $post = get_page_by_path( $path, ARRAY_A );
+
+ if ( empty( $post ) )
+ return new WP_Error( 'json_post_invalid_id', __( 'Invalid post ID.' ), array( 'status' => 404 ) );
+
+ return $this->editPost( $post['ID'], $data, $_headers );
+ }
+
+ /**
+ * Delete a page by path name
+ *
+ * @param string $path
+ */
+ public function deletePostByPath( $path, $force = false ) {
+ $post = get_page_by_path( $path, ARRAY_A );
+
+ if ( empty( $post ) )
+ return new WP_Error( 'json_post_invalid_id', __( 'Invalid post ID.' ), array( 'status' => 404 ) );
+
+ return $this->deletePost( $post['ID'], $force );
+ }
+
+ /**
+ * Prepare post data
+ *
+ * @param array $post The unprepared post data
+ * @param array $fields The subset of post type fields to return
+ * @return array The prepared post data
+ */
+ protected function prepare_post( $post, $context = 'view' ) {
+ $_post = parent::prepare_post( $post, $context );
+
+ // Override entity meta keys with the correct links
+ $_post['meta'] = array(
+ 'links' => array(
+ 'self' => json_url( '/pages/' . get_page_uri( $post['ID'] ) ),
+ 'author' => json_url( '/users/' . $post['post_author'] ),
+ 'collection' => json_url( '/pages' ),
+ 'replies' => json_url( '/pages/' . $post['ID'] . '/comments' ),
+ 'version-history' => json_url( '/pages/' . $post['ID'] . '/revisions' ),
+ ),
+ );
+
+ if ( ! empty( $post['post_parent'] ) )
+ $_post['meta']['links']['up'] = json_url( '/posts/' . get_page_uri( (int) $post['post_parent'] ) );
+
+ return apply_filters( 'json_prepare_page', $_post, $post, $context );
+ }
+}
</ins><span class="cx">\ No newline at end of file
</span><span class="cx">Property changes on: 2013/rmccue/trunk/lib/class-wp-json-pages.php
</span><span class="cx">___________________________________________________________________
</span></span></pre></div>
<a id="svnexecutable"></a>
<div class="addfile"><h4>Added: svn:executable</h4></div>
<ins>+*
</ins><span class="cx">\ No newline at end of property
</span><a id="2013rmccuetrunklibclasswpjsonserverphp"></a>
<div class="modfile"><h4>Modified: 2013/rmccue/trunk/lib/class-wp-json-server.php (2266 => 2267)</h4>
<pre class="diff"><span>
<span class="info">--- 2013/rmccue/trunk/lib/class-wp-json-server.php 2013-09-10 18:32:00 UTC (rev 2266)
+++ 2013/rmccue/trunk/lib/class-wp-json-server.php 2013-09-10 18:32:06 UTC (rev 2267)
</span><span class="lines">@@ -213,6 +213,7 @@
</span><span class="cx"> */
</span><span class="cx"> public function getRoutes() {
</span><span class="cx"> $GLOBALS['wp_json_posts'] = $posts = new WP_JSON_Posts();
</span><ins>+ $GLOBALS['wp_json_pages'] = $pages = new WP_JSON_Pages();
</ins><span class="cx">
</span><span class="cx"> $endpoints = array(
</span><span class="cx"> // Meta endpoints
</span><span class="lines">@@ -247,6 +248,36 @@
</span><span class="cx"> '/posts/types/(?P<type>\w+)' => array( array( $posts, 'getPostType' ), self::READABLE ),
</span><span class="cx"> '/posts/statuses' => array( array( $posts, 'getPostStatuses' ), self::READABLE ),
</span><span class="cx">
</span><ins>+ // Page endpoints
+ '/pages' => array(
+ array( array( $pages, 'getPosts' ), self::READABLE ),
+ array( array( $pages, 'newPost' ), self::CREATABLE | self::ACCEPT_JSON ),
+ ),
+
+ '/pages/(?P<id>\d+)' => array(
+ array( array( $pages, 'getPost' ), self::READABLE ),
+ array( array( $pages, 'editPost' ), self::EDITABLE | self::ACCEPT_JSON ),
+ array( array( $pages, 'deletePost' ), self::DELETABLE ),
+ ),
+ '/pages/(?P<path>.+)' => array(
+ array( array( $pages, 'getPostByPath' ), self::READABLE ),
+ array( array( $pages, 'editPostByPath' ), self::EDITABLE | self::ACCEPT_JSON ),
+ array( array( $pages, 'deletePostByPath' ), self::DELETABLE ),
+ ),
+
+ '/pages/(?P<id>\d+)/revisions' => array( '__return_null', self::READABLE ),
+
+ // Page comments
+ '/pages/(?P<id>\d+)/comments' => array(
+ array( array( $pages, 'getComments' ), self::READABLE ),
+ array( '__return_null', self::CREATABLE | self::ACCEPT_JSON ),
+ ),
+ '/pages/(?P<id>\d+)/comments/(?P<comment>\d+)' => array(
+ array( array( $pages, 'getComment' ), self::READABLE ),
+ array( '__return_null', self::EDITABLE | self::ACCEPT_JSON ),
+ array( '__return_null', self::DELETABLE ),
+ ),
+
</ins><span class="cx"> // Taxonomies
</span><span class="cx"> '/taxonomies' => array( '__return_null', self::READABLE ),
</span><span class="cx"> '/taxonomies/(?P<taxonomy>\w+)' => array(
</span></span></pre></div>
<a id="2013rmccuetrunkpluginphp"></a>
<div class="modfile"><h4>Modified: 2013/rmccue/trunk/plugin.php (2266 => 2267)</h4>
<pre class="diff"><span>
<span class="info">--- 2013/rmccue/trunk/plugin.php 2013-09-10 18:32:00 UTC (rev 2266)
+++ 2013/rmccue/trunk/plugin.php 2013-09-10 18:32:06 UTC (rev 2267)
</span><span class="lines">@@ -31,6 +31,7 @@
</span><span class="cx"> include_once( ABSPATH . WPINC . '/class-wp-xmlrpc-server.php' );
</span><span class="cx"> include_once( dirname( __FILE__ ) . '/lib/class-wp-json-server.php' );
</span><span class="cx"> include_once( dirname( __FILE__ ) . '/lib/class-wp-json-posts.php' );
</span><ins>+ include_once( dirname( __FILE__ ) . '/lib/class-wp-json-pages.php' );
</ins><span class="cx">
</span><span class="cx"> /**
</span><span class="cx"> * Whether this is a XMLRPC Request
</span></span></pre>
</div>
</div>
</body>
</html>