Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Fix method names coding standard #82

Merged
merged 4 commits into from

2 participants

@osiux

Hopefully this works to fix #36.

Tested it locally and everything seems to work good (with just a notice no related to this change). I tried to also update docs (maybe missed something?).

@rmccue rmccue added this to the 0.9 milestone
@rmccue rmccue merged commit f9a1b71 into WP-API:master
@rmccue
Owner

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
28 docs/guides/extending.md
@@ -130,9 +130,9 @@ preparation and request handling. This will automatically register all the post
methods for their endpoints.
Along these lines, keep your methods named as generically as possible; while
-`MyPlugin_API_MyType::getMyTypeItems()` might seem like a good name, it makes it
+`MyPlugin_API_MyType::get_my_type_items()` might seem like a good name, it makes it
harder for other plugins to use; standardising on
-`MyPlugin_API_MyType::getPosts()` with similar arguments to the parent is a
+`MyPlugin_API_MyType::get_posts()` with similar arguments to the parent is a
better idea and allows a nicer fall-through.
You should also aim to keep these related endpoints modular, and make liberal
@@ -159,7 +159,7 @@ errors.
For example, an endpoint that takes a required `context` parameter, an optional
`type` parameter and uses the `X-WP-Example` header would look like this:
- function getMyData( $context, $_headers, $type = 'my-default-value' ) {
+ function get_my_data( $context, $_headers, $type = 'my-default-value' ) {
if ( isset( $_headers['X-WP-EXAMPLE'] ) ) {
$my_header_value = $_headers['X-WP-EXAMPLE'];
}
@@ -233,20 +233,20 @@ built-in types, your registration code should look something like this:
global $myplugin_api_mytype;
$myplugin_api_mytype = new MyPlugin_API_MyType();
- add_filter( 'json_endpoints', array( $myplugin_api_mytype, 'registerRoutes' ) );
+ add_filter( 'json_endpoints', array( $myplugin_api_mytype, 'register_routes' ) );
}
add_action( 'wp_json_server_before_serve', 'myplugin_api_init' );
class MyPlugin_API_MyType {
- public function registerRoutes( $routes ) {
+ public function register_routes( $routes ) {
$routes['/myplugin/mytypeitems'] = array(
- array( array( $this, 'getPosts'), WP_JSON_Server::READABLE ),
- array( array( $this, 'newPost'), WP_JSON_Server::CREATABLE | WP_JSON_Server::ACCEPT_JSON ),
+ array( array( $this, 'get_posts'), WP_JSON_Server::READABLE ),
+ array( array( $this, 'new_post'), WP_JSON_Server::CREATABLE | WP_JSON_Server::ACCEPT_JSON ),
);
$routes['/myplugin/mytypeitems/(?P<id>\d+)'] = array(
- array( array( $this, 'getPost'), WP_JSON_Server::READABLE ),
- array( array( $this, 'editPost'), WP_JSON_Server::EDITABLE | WP_JSON_Server::ACCEPT_JSON ),
- array( array( $this, 'deletePost'), WP_JSON_Server::DELETABLE ),
+ array( array( $this, 'get_post'), WP_JSON_Server::READABLE ),
+ array( array( $this, 'edit_post'), WP_JSON_Server::EDITABLE | WP_JSON_Server::ACCEPT_JSON ),
+ array( array( $this, 'delete_post'), WP_JSON_Server::DELETABLE ),
);
// Add more custom routes here
@@ -274,10 +274,10 @@ hooking and more for you:
protected $base = '/myplugin/mytypeitems';
protected $type = 'myplugin-mytype';
- public function registerRoutes( $routes ) {
- $routes = parent::registerRoutes( $routes );
- // $routes = parent::registerRevisionRoutes( $routes );
- // $routes = parent::registerCommentRoutes( $routes );
+ public function register_routes( $routes ) {
+ $routes = parent::register_routes( $routes );
+ // $routes = parent::register_revision_routes( $routes );
+ // $routes = parent::register_comment_routes( $routes );
// Add more custom routes here
View
46 lib/class-wp-json-customposttype.php
@@ -38,7 +38,7 @@ public function __construct(WP_JSON_ResponseHandler $server) {
return;
}
- add_filter( 'json_endpoints', array( $this, 'registerRoutes' ) );
+ add_filter( 'json_endpoints', array( $this, 'register_routes' ) );
add_filter( 'json_post_type_data', array( $this, 'type_archive_link' ), 10, 2 );
parent::__construct($server);
@@ -50,16 +50,16 @@ public function __construct(WP_JSON_ResponseHandler $server) {
* @param array $routes Routes for the post type
* @return array Modified routes
*/
- public function registerRoutes( $routes ) {
+ public function register_routes( $routes ) {
$routes[ $this->base ] = array(
- array( array( $this, 'getPosts' ), WP_JSON_Server::READABLE ),
- array( array( $this, 'newPost' ), WP_JSON_Server::CREATABLE ),
+ array( array( $this, 'get_posts' ), WP_JSON_Server::READABLE ),
+ array( array( $this, 'new_post' ), WP_JSON_Server::CREATABLE ),
);
$routes[ $this->base . '/(?P<id>\d+)' ] = array(
- array( array( $this, 'getPost' ), WP_JSON_Server::READABLE ),
- array( array( $this, 'editPost' ), WP_JSON_Server::EDITABLE ),
- array( array( $this, 'deletePost' ), WP_JSON_Server::DELETABLE ),
+ array( array( $this, 'get_post' ), WP_JSON_Server::READABLE ),
+ array( array( $this, 'edit_post' ), WP_JSON_Server::EDITABLE ),
+ array( array( $this, 'delete_post' ), WP_JSON_Server::DELETABLE ),
);
return $routes;
}
@@ -70,7 +70,7 @@ public function registerRoutes( $routes ) {
* @param array $routes Routes for the post type
* @return array Modified routes
*/
- public function registerRevisionRoutes( $routes ) {
+ public function register_revision_routes( $routes ) {
$routes[ $this->base . '/(?P<id>\d+)/revisions' ] = array(
array( '__return_null', WP_JSON_Server::READABLE ),
);
@@ -83,13 +83,13 @@ public function registerRevisionRoutes( $routes ) {
* @param array $routes Routes for the post type
* @return array Modified routes
*/
- public function registerCommentRoutes( $routes ) {
+ public function register_comment_routes( $routes ) {
$routes[ $this->base . '/(?P<id>\d+)/comments'] = array(
- array( array( $this, 'getComments' ), WP_JSON_Server::READABLE ),
+ array( array( $this, 'get_comments' ), WP_JSON_Server::READABLE ),
array( '__return_null', WP_JSON_Server::CREATABLE | WP_JSON_Server::ACCEPT_JSON ),
);
$routes[ $this->base . '/(?P<id>\d+)/comments/(?P<comment>\d+)' ] = array(
- array( array( $this, 'getComment' ), WP_JSON_Server::READABLE ),
+ array( array( $this, 'get_comment' ), WP_JSON_Server::READABLE ),
array( '__return_null', WP_JSON_Server::EDITABLE | WP_JSON_Server::ACCEPT_JSON ),
array( '__return_null', WP_JSON_Server::DELETABLE ),
);
@@ -102,21 +102,21 @@ public function registerCommentRoutes( $routes ) {
* Overrides the $type to set to $this->type, then passes through to the
* post endpoints.
*
- * @see WP_JSON_Posts::getPosts()
+ * @see WP_JSON_Posts::get_posts()
*/
- public function getPosts( $filter = array(), $context = 'view', $type = null, $page = 1 ) {
+ public function get_posts( $filter = array(), $context = 'view', $type = null, $page = 1 ) {
if ( !empty( $type ) && $type !== $this->type )
return new WP_Error( 'json_post_invalid_type', __( 'Invalid post type' ), array( 'status' => 400 ) );
- return parent::getPosts( $filter, $context, $this->type, $page );
+ return parent::get_posts( $filter, $context, $this->type, $page );
}
/**
* Retrieve a post
*
- * @see WP_JSON_Posts::getPost()
+ * @see WP_JSON_Posts::get_post()
*/
- public function getPost( $id, $context = 'view' ) {
+ public function get_post( $id, $context = 'view' ) {
$id = (int) $id;
if ( empty( $id ) )
@@ -127,15 +127,15 @@ public function getPost( $id, $context = 'view' ) {
if ( $post['post_type'] !== $this->type )
return new WP_Error( 'json_post_invalid_type', __( 'Invalid post type' ), array( 'status' => 400 ) );
- return parent::getPost( $id, $context );
+ return parent::get_post( $id, $context );
}
/**
* Edit a post
*
- * @see WP_JSON_Posts::editPost()
+ * @see WP_JSON_Posts::edit_post()
*/
- function editPost( $id, $data, $_headers = array() ) {
+ function edit_post( $id, $data, $_headers = array() ) {
$id = (int) $id;
if ( empty( $id ) )
return new WP_Error( 'json_post_invalid_id', __( 'Invalid post ID.' ), array( 'status' => 404 ) );
@@ -148,15 +148,15 @@ function editPost( $id, $data, $_headers = array() ) {
if ( $post['post_type'] !== $this->type )
return new WP_Error( 'json_post_invalid_type', __( 'Invalid post type' ), array( 'status' => 400 ) );
- return parent::editPost( $id, $data, $_headers );
+ return parent::edit_post( $id, $data, $_headers );
}
/**
* Delete a post
*
- * @see WP_JSON_Posts::deletePost()
+ * @see WP_JSON_Posts::delete_post()
*/
- public function deletePost( $id, $force = false ) {
+ public function delete_post( $id, $force = false ) {
$id = (int) $id;
if ( empty( $id ) )
@@ -167,7 +167,7 @@ public function deletePost( $id, $force = false ) {
if ( $post['post_type'] !== $this->type )
return new WP_Error( 'json_post_invalid_type', __( 'Invalid post type' ), array( 'status' => 400 ) );
- return parent::deletePost( $id, $force );
+ return parent::delete_post( $id, $force );
}
/**
View
62 lib/class-wp-json-media.php
@@ -7,17 +7,17 @@ class WP_JSON_Media extends WP_JSON_Posts {
* @param array $routes Existing routes
* @return array Modified routes
*/
- public function registerRoutes( $routes ) {
+ public function register_routes( $routes ) {
$media_routes = array(
'/media' => array(
- array( array( $this, 'getPosts' ), WP_JSON_Server::READABLE ),
- array( array( $this, 'uploadAttachment' ), WP_JSON_Server::CREATABLE ),
+ array( array( $this, 'get_posts' ), WP_JSON_Server::READABLE ),
+ array( array( $this, 'upload_attachment' ), WP_JSON_Server::CREATABLE ),
),
'/media/(?P<id>\d+)' => array(
- array( array( $this, 'getPost' ), WP_JSON_Server::READABLE ),
- array( array( $this, 'editPost' ), WP_JSON_Server::EDITABLE ),
- array( array( $this, 'deletePost' ), WP_JSON_Server::DELETABLE ),
+ array( array( $this, 'get_post' ), WP_JSON_Server::READABLE ),
+ array( array( $this, 'edit_post' ), WP_JSON_Server::EDITABLE ),
+ array( array( $this, 'delete_post' ), WP_JSON_Server::DELETABLE ),
),
);
return array_merge( $routes, $media_routes );
@@ -29,9 +29,9 @@ public function registerRoutes( $routes ) {
* Overrides the $type to set to 'attachment', then passes through to the post
* endpoints.
*
- * @see WP_JSON_Posts::getPosts()
+ * @see WP_JSON_Posts::get_posts()
*/
- public function getPosts( $filter = array(), $context = 'view', $type = 'attachment', $page = 1 ) {
+ public function get_posts( $filter = array(), $context = 'view', $type = 'attachment', $page = 1 ) {
if ( $type !== 'attachment' )
return new WP_Error( 'json_post_invalid_type', __( 'Invalid post type' ), array( 'status' => 400 ) );
@@ -39,10 +39,10 @@ public function getPosts( $filter = array(), $context = 'view', $type = 'attachm
$filter['post_status'] = array( 'publish', 'inherit' );
// Always allow status queries for attachments
- add_filter( 'query_vars', array( $this, 'allowStatusQuery' ) );
+ add_filter( 'query_vars', array( $this, 'allow_status_query' ) );
}
- $posts = parent::getPosts( $filter, $context, 'attachment', $page );
+ $posts = parent::get_posts( $filter, $context, 'attachment', $page );
return $posts;
}
@@ -53,8 +53,8 @@ public function getPosts( $filter = array(), $context = 'view', $type = 'attachm
* @param array $vars Query variables
* @return array Filtered query variables
*/
- public function allowStatusQuery( $vars ) {
- remove_filter( 'query_vars', array( $this, 'allowStatusQuery' ) );
+ public function allow_status_query( $vars ) {
+ remove_filter( 'query_vars', array( $this, 'allow_status_query' ) );
$vars[] = 'post_status';
return $vars;
@@ -63,9 +63,9 @@ public function allowStatusQuery( $vars ) {
/**
* Retrieve a attachment
*
- * @see WP_JSON_Posts::getPost()
+ * @see WP_JSON_Posts::get_post()
*/
- public function getPost( $id, $context = 'view' ) {
+ public function get_post( $id, $context = 'view' ) {
$id = (int) $id;
if ( empty( $id ) )
@@ -76,7 +76,7 @@ public function getPost( $id, $context = 'view' ) {
if ( $post['post_type'] !== 'attachment' )
return new WP_Error( 'json_post_invalid_type', __( 'Invalid post type' ), array( 'status' => 400 ) );
- return parent::getPost( $id, $context );
+ return parent::get_post( $id, $context );
}
/**
@@ -130,9 +130,9 @@ protected function prepare_post( $post, $context = 'single' ) {
/**
* Edit a attachment
*
- * @see WP_JSON_Posts::editPost()
+ * @see WP_JSON_Posts::edit_post()
*/
- public function editPost( $id, $data, $_headers = array() ) {
+ public function edit_post( $id, $data, $_headers = array() ) {
$id = (int) $id;
if ( empty( $id ) )
return new WP_Error( 'json_post_invalid_id', __( 'Invalid post ID.' ), array( 'status' => 404 ) );
@@ -145,15 +145,15 @@ public function editPost( $id, $data, $_headers = array() ) {
if ( $post['post_type'] !== 'attachment' )
return new WP_Error( 'json_post_invalid_type', __( 'Invalid post type' ), array( 'status' => 400 ) );
- return parent::editPost( $id, $data, $_headers );
+ return parent::edit_post( $id, $data, $_headers );
}
/**
* Delete a attachment
*
- * @see WP_JSON_Posts::deletePost()
+ * @see WP_JSON_Posts::delete_post()
*/
- public function deletePost( $id, $force = false ) {
+ public function delete_post( $id, $force = false ) {
$id = (int) $id;
if ( empty( $id ) )
@@ -164,7 +164,7 @@ public function deletePost( $id, $force = false ) {
if ( $post['post_type'] !== 'attachment' )
return new WP_Error( 'json_post_invalid_type', __( 'Invalid post type' ), array( 'status' => 400 ) );
- return parent::deletePost( $id, $force );
+ return parent::delete_post( $id, $force );
}
/**
@@ -178,7 +178,7 @@ public function deletePost( $id, $force = false ) {
* @param array $_headers HTTP headers from the request
* @return array|WP_Error Attachment data or error
*/
- public function uploadAttachment( $_files, $_headers ) {
+ public function upload_attachment( $_files, $_headers ) {
$post_type = get_post_type_object( 'attachment' );
if ( ! $post_type )
return new WP_Error( 'json_invalid_post_type', __( 'Invalid post type' ), array( 'status' => 400 ) );
@@ -189,10 +189,10 @@ public function uploadAttachment( $_files, $_headers ) {
// Get the file via $_FILES or raw data
if ( empty( $_files ) ) {
- $file = $this->uploadFromData( $_files, $_headers );
+ $file = $this->upload_from_data( $_files, $_headers );
}
else {
- $file = $this->uploadFromFile( $_files, $_headers );
+ $file = $this->upload_from_file( $_files, $_headers );
}
if ( is_wp_error( $file ) )
@@ -247,7 +247,7 @@ public function uploadAttachment( $_files, $_headers ) {
* @param array $_headers HTTP headers from the request
* @return array|WP_Error Data from {@see wp_handle_sideload()}
*/
- protected function uploadFromData( $_files, $_headers ) {
+ protected function upload_from_data( $_files, $_headers ) {
$data = $this->server->get_raw_data();
if ( empty( $data ) ) {
@@ -328,7 +328,7 @@ protected function uploadFromData( $_files, $_headers ) {
* @param array $_headers HTTP headers from the request
* @return array|WP_Error Data from {@see wp_handle_upload()}
*/
- protected function uploadFromFile( $_files, $_headers ) {
+ protected function upload_from_file( $_files, $_headers ) {
if ( empty( $_files['file'] ) )
return new WP_Error( 'json_upload_no_data', __( 'No data supplied' ), array( 'status' => 400 ) );
@@ -361,7 +361,7 @@ protected function uploadFromFile( $_files, $_headers ) {
* @param array $data Supplied post data
* @return bool|WP_Error Success or error object
*/
- public function preinsertCheck( $status, $post, $data ) {
+ public function preinsert_check( $status, $post, $data ) {
if ( is_wp_error( $status ) ) {
return $status;
}
@@ -384,10 +384,10 @@ public function preinsertCheck( $status, $post, $data ) {
* @param array $data Supplied post data
* @param boolean $update Is this an update?
*/
- public function attachThumbnail( $post, $data, $update ) {
+ public function attach_thumbnail( $post, $data, $update ) {
if ( ! empty( $data['featured_image'] ) ) {
// Already verified in preinsertCheck()
- $thumbnail = $this->getPost( $data['featured_image'], 'child' );
+ $thumbnail = $this->get_post( $data['featured_image'], 'child' );
set_post_thumbnail( $post_ID, $thumbnail['ID'] );
}
}
@@ -400,7 +400,7 @@ public function attachThumbnail( $post, $data, $update ) {
* @param string $context Display context
* @return array Filtered post data
*/
- public function addThumbnailData( $data, $post, $context ) {
+ public function add_thumbnail_data( $data, $post, $context ) {
if( !post_type_supports( $post['post_type'], 'thumbnail' ) ) {
return $data;
}
@@ -409,7 +409,7 @@ public function addThumbnailData( $data, $post, $context ) {
$data['featured_image'] = null;
$thumbnail_id = get_post_thumbnail_id( $post['ID'] );
if ( $thumbnail_id ) {
- $data['featured_image'] = $this->getPost( $thumbnail_id, 'child' );
+ $data['featured_image'] = $this->get_post( $thumbnail_id, 'child' );
}
return $data;
View
26 lib/class-wp-json-pages.php
@@ -39,16 +39,16 @@ class WP_JSON_Pages extends WP_JSON_CustomPostType {
* @param array $routes Existing routes
* @return array Modified routes
*/
- public function registerRoutes( $routes ) {
- $routes = parent::registerRoutes( $routes );
- $routes = parent::registerRevisionRoutes( $routes );
- $routes = parent::registerCommentRoutes( $routes );
+ public function register_routes( $routes ) {
+ $routes = parent::register_routes( $routes );
+ $routes = parent::register_revision_routes( $routes );
+ $routes = parent::register_comment_routes( $routes );
// Add post-by-path routes
$routes[ $this->base . '/(?P<path>.+)'] = array(
- array( array( $this, 'getPostByPath' ), WP_JSON_Server::READABLE ),
- array( array( $this, 'editPostByPath' ), WP_JSON_Server::EDITABLE | WP_JSON_Server::ACCEPT_JSON ),
- array( array( $this, 'deletePostByPath' ), WP_JSON_Server::DELETABLE ),
+ array( array( $this, 'get_post_by_path' ), WP_JSON_Server::READABLE ),
+ array( array( $this, 'edit_post_by_path' ), WP_JSON_Server::EDITABLE | WP_JSON_Server::ACCEPT_JSON ),
+ array( array( $this, 'delete_post_by_path' ), WP_JSON_Server::DELETABLE ),
);
return $routes;
@@ -59,13 +59,13 @@ public function registerRoutes( $routes ) {
*
* @param string $path
*/
- public function getPostByPath( $path, $context = 'view' ) {
+ public function get_post_by_path( $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 );
+ return $this->get_post( $post['ID'], $context );
}
/**
@@ -73,13 +73,13 @@ public function getPostByPath( $path, $context = 'view' ) {
*
* @param string $path
*/
- public function editPostByPath( $path, $data, $_headers = array() ) {
+ public function edit_post_by_path( $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 );
+ return $this->edit_post( $post['ID'], $data, $_headers );
}
/**
@@ -87,13 +87,13 @@ public function editPostByPath( $path, $data, $_headers = array() ) {
*
* @param string $path
*/
- public function deletePostByPath( $path, $force = false ) {
+ public function delete_post_by_path( $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 );
+ return $this->delete_post( $post['ID'], $force );
}
/**
View
79 lib/class-wp-json-posts.php
@@ -23,36 +23,36 @@ public function __construct(WP_JSON_ResponseHandler $server) {
* @param array $routes Existing routes
* @return array Modified routes
*/
- public function registerRoutes( $routes ) {
+ public function register_routes( $routes ) {
$post_routes = array(
// Post endpoints
'/posts' => array(
- array( array( $this, 'getPosts' ), WP_JSON_Server::READABLE ),
- array( array( $this, 'newPost' ), WP_JSON_Server::CREATABLE | WP_JSON_Server::ACCEPT_JSON ),
+ array( array( $this, 'get_posts' ), WP_JSON_Server::READABLE ),
+ array( array( $this, 'new_post' ), WP_JSON_Server::CREATABLE | WP_JSON_Server::ACCEPT_JSON ),
),
'/posts/(?P<id>\d+)' => array(
- array( array( $this, 'getPost' ), WP_JSON_Server::READABLE ),
- array( array( $this, 'editPost' ), WP_JSON_Server::EDITABLE | WP_JSON_Server::ACCEPT_JSON ),
- array( array( $this, 'deletePost' ), WP_JSON_Server::DELETABLE ),
+ array( array( $this, 'get_post' ), WP_JSON_Server::READABLE ),
+ array( array( $this, 'edit_post' ), WP_JSON_Server::EDITABLE | WP_JSON_Server::ACCEPT_JSON ),
+ array( array( $this, 'delete_post' ), WP_JSON_Server::DELETABLE ),
),
'/posts/(?P<id>\d+)/revisions' => array( '__return_null', WP_JSON_Server::READABLE ),
// Comments
'/posts/(?P<id>\d+)/comments' => array(
- array( array( $this, 'getComments' ), WP_JSON_Server::READABLE ),
+ array( array( $this, 'get_comments' ), WP_JSON_Server::READABLE ),
array( '__return_null', WP_JSON_Server::CREATABLE | WP_JSON_Server::ACCEPT_JSON ),
),
'/posts/(?P<id>\d+)/comments/(?P<comment>\d+)' => array(
- array( array( $this, 'getComment' ), WP_JSON_Server::READABLE ),
+ array( array( $this, 'get_comment' ), WP_JSON_Server::READABLE ),
array( '__return_null', WP_JSON_Server::EDITABLE | WP_JSON_Server::ACCEPT_JSON ),
array( '__return_null', WP_JSON_Server::DELETABLE ),
),
// Meta-post endpoints
- '/posts/types' => array( array( $this, 'getPostTypes' ), WP_JSON_Server::READABLE ),
- '/posts/types/(?P<type>\w+)' => array( array( $this, 'getPostType' ), WP_JSON_Server::READABLE ),
- '/posts/statuses' => array( array( $this, 'getPostStatuses' ), WP_JSON_Server::READABLE ),
+ '/posts/types' => array( array( $this, 'get_post_types' ), WP_JSON_Server::READABLE ),
+ '/posts/types/(?P<type>\w+)' => array( array( $this, 'get_post_type' ), WP_JSON_Server::READABLE ),
+ '/posts/statuses' => array( array( $this, 'get_post_statuses' ), WP_JSON_Server::READABLE ),
);
return array_merge( $routes, $post_routes );
}
@@ -70,14 +70,14 @@ public function registerRoutes( $routes ) {
* in the response array.
*
* @uses wp_get_recent_posts()
- * @see WP_JSON_Posts::getPost() for more on $fields
+ * @see WP_JSON_Posts::get_post() for more on $fields
* @see get_posts() for more on $filter values
*
* @param array $filter optional
* @param array $fields optional
* @return array contains a collection of Post entities.
*/
- public function getPosts( $filter = array(), $context = 'view', $type = 'post', $page = 1 ) {
+ public function get_posts( $filter = array(), $context = 'view', $type = 'post', $page = 1 ) {
$query = array();
$post_type = get_post_type_object( $type );
@@ -140,7 +140,7 @@ public function getPosts( $filter = array(), $context = 'view', $type = 'post',
$post = get_object_vars( $post );
// Do we have permission to read this post?
- if ( ! $this->checkReadPermission( $post ) )
+ if ( ! $this->check_read_permission( $post ) )
continue;
$response->link_header( 'item', json_url( '/posts/' . $post['ID'] ), array( 'title' => $post['post_title'] ) );
@@ -158,7 +158,7 @@ public function getPosts( $filter = array(), $context = 'view', $type = 'post',
* @param array $post Post data
* @return boolean Can we read it?
*/
- protected function checkReadPermission( $post ) {
+ protected function check_read_permission( $post ) {
// Can we read the post?
$post_type = get_post_type_object( $post['post_type'] );
if ( 'publish' === $post['post_status'] || current_user_can( $post_type->cap->read_post, $post['ID'] ) ) {
@@ -169,7 +169,7 @@ protected function checkReadPermission( $post ) {
if ( 'inherit' === $post['post_status'] && $post['post_parent'] > 0 ) {
$parent = get_post( $post['post_parent'], ARRAY_A );
- if ( $this->checkReadPermission( $parent ) ) {
+ if ( $this->check_read_permission( $parent ) ) {
return true;
}
}
@@ -208,14 +208,14 @@ protected function checkReadPermission( $post ) {
* - terms_names - array, with taxonomy names as keys and arrays of term names as values
* - enclosure
* - any other fields supported by wp_insert_post()
- * @return array Post data (see {@see WP_JSON_Posts::getPost})
+ * @return array Post data (see {@see WP_JSON_Posts::get_post})
*/
- function newPost( $data ) {
+ function new_post( $data ) {
unset( $data['ID'] );
$result = $this->insert_post( $data );
if ( is_string( $result ) || is_int( $result ) ) {
- $response = $this->getPost( $result );
+ $response = $this->get_post( $result );
$response->set_status( 201 );
$response->header( 'Location', json_url( '/posts/' . $result ) );
return $response;
@@ -236,7 +236,7 @@ function newPost( $data ) {
* @param array $fields Post fields to return (optional)
* @return array Post entity
*/
- public function getPost( $id, $context = 'view' ) {
+ public function get_post( $id, $context = 'view' ) {
$id = (int) $id;
if ( empty( $id ) )
@@ -248,7 +248,7 @@ public function getPost( $id, $context = 'view' ) {
return new WP_Error( 'json_post_invalid_id', __( 'Invalid post ID.' ), array( 'status' => 404 ) );
$post_type = get_post_type_object( $post['post_type'] );
- if ( ! $this->checkReadPermission( $post ) )
+ if ( ! $this->check_read_permission( $post ) )
return new WP_Error( 'json_user_cannot_read', __( 'Sorry, you cannot read this post.' ), array( 'status' => 401 ) );
// Link headers (see RFC 5988)
@@ -279,11 +279,11 @@ public function getPost( $id, $context = 'view' ) {
* @internal 'data' is used here rather than 'content', as get_default_post_to_edit uses $_REQUEST['content']
*
* @param int $id Post ID to edit
- * @param array $data Data construct, see {@see WP_JSON_Posts::newPost}
+ * @param array $data Data construct, see {@see WP_JSON_Posts::new_post}
* @param array $_headers Header data
* @return true on success
*/
- function editPost( $id, $data, $_headers = array() ) {
+ function edit_post( $id, $data, $_headers = array() ) {
$id = (int) $id;
if ( empty( $id ) )
@@ -328,7 +328,7 @@ function editPost( $id, $data, $_headers = array() ) {
* @param int $id
* @return true on success
*/
- public function deletePost( $id, $force = false ) {
+ public function delete_post( $id, $force = false ) {
$id = (int) $id;
if ( empty( $id ) )
@@ -363,7 +363,7 @@ public function deletePost( $id, $force = false ) {
* @param int $id Post ID to retrieve comments for
* @return array List of Comment entities
*/
- public function getComments( $id ) {
+ public function get_comments( $id ) {
//$args = array('status' => $status, 'post_id' => $id, 'offset' => $offset, 'number' => $number )l
$comments = get_comments( array('post_id' => $id) );
@@ -380,7 +380,7 @@ public function getComments( $id ) {
* @param int $comment Comment ID
* @return array Comment entity
*/
- public function getComment( $comment ) {
+ public function get_comment( $comment ) {
$comment = get_comment( $comment );
$data = $this->prepare_comment( $comment );
return $data;
@@ -389,15 +389,15 @@ public function getComment( $comment ) {
/**
* Get all public post types
*
- * @uses self::getPostType()
+ * @uses self::get_post_type()
* @return array List of post type data
*/
- public function getPostTypes() {
+ public function get_post_types() {
$data = get_post_types( array(), 'objects' );
$types = array();
foreach ($data as $name => $type) {
- $type = $this->getPostType( $type, true );
+ $type = $this->get_post_type( $type, true );
if ( is_wp_error( $type ) )
continue;
@@ -414,7 +414,7 @@ public function getPostTypes() {
* @param boolean $_in_collection Is this in a collection? (internal use)
* @return array Post type data
*/
- public function getPostType( $type, $_in_collection = false ) {
+ public function get_post_type( $type, $_in_collection = false ) {
if ( ! is_object( $type ) )
$type = get_post_type_object($type);
@@ -454,7 +454,7 @@ public function getPostType( $type, $_in_collection = false ) {
*
* @return array List of post status data
*/
- public function getPostStatuses() {
+ public function get_post_statuses() {
$statuses = get_post_stati(array(), 'objects');
$data = array();
@@ -501,7 +501,7 @@ protected function prepare_post( $post, $context = 'view' ) {
);
$post_type = get_post_type_object( $post['post_type'] );
- if ( ! $this->checkReadPermission( $post ) )
+ if ( ! $this->check_read_permission( $post ) )
return new WP_Error( 'json_user_cannot_read', __( 'Sorry, you cannot read this post.' ), array( 'status' => 401 ) );
// prepare common post fields
@@ -1025,4 +1025,19 @@ protected function prepare_comment( $comment, $requested_fields = array( 'commen
return $data;
}
+
+ /**
+ * Magic method used to temporaly deprecate camelcase functions
+ *
+ * @param string $name Function name
+ * @param array $arguments Function arguments
+ * @return mixed
+ */
+ public function __call($name, $arguments) {
+ $underscored = strtolower(preg_replace('/(?!^)[[:upper:]][[:lower:]]/', '_$0', $name));
+ if ( method_exists( $this, $underscored ) ) {
+ _deprecated_function( __CLASS__ . '->' . $name, 'WPAPI-0.9', __CLASS__ . '->' . $underscored );
+ return call_user_func_array( array( $this, $underscored ), $arguments );
+ }
+ }
}
View
10 lib/class-wp-json-server.php
@@ -288,10 +288,10 @@ public function serve_request( $path = null ) {
*
* @return array `'/path/regex' => array( $callback, $bitmask )` or `'/path/regex' => array( array( $callback, $bitmask ), ...)`
*/
- public function getRoutes() {
+ public function get_routes() {
$endpoints = array(
// Meta endpoints
- '/' => array( array( $this, 'getIndex' ), self::READABLE ),
+ '/' => array( array( $this, 'get_index' ), self::READABLE ),
// Users
'/users' => array(
@@ -349,7 +349,7 @@ public function dispatch() {
default:
return new WP_Error( 'json_unsupported_method', __( 'Unsupported request method' ), array( 'status' => 400 ) );
}
- foreach ( $this->getRoutes() as $route => $handlers ) {
+ foreach ( $this->get_routes() as $route => $handlers ) {
foreach ( $handlers as $handler ) {
$callback = $handler[0];
$supported = isset( $handler[1] ) ? $handler[1] : self::METHOD_GET;
@@ -446,7 +446,7 @@ protected function sort_callback_params( $callback, $provided ) {
*
* @return array Index entity
*/
- public function getIndex() {
+ public function get_index() {
// General site data
$available = array(
'name' => get_option( 'blogname' ),
@@ -462,7 +462,7 @@ public function getIndex() {
);
// Find the available routes
- foreach ( $this->getRoutes() as $route => $callbacks ) {
+ foreach ( $this->get_routes() as $route => $callbacks ) {
$data = array();
$route = preg_replace( '#\(\?P(<\w+?>).*?\)#', '$1', $route );
View
41 lib/class-wp-json-taxonomies.php
@@ -7,20 +7,20 @@ class WP_JSON_Taxonomies {
* @param array $routes Existing routes
* @return array Modified routes
*/
- public function registerRoutes( $routes ) {
+ public function register_routes( $routes ) {
$tax_routes = array(
'/posts/types/(?P<type>\w+)/taxonomies' => array(
- array( array( $this, 'getTaxonomies' ), WP_JSON_Server::READABLE ),
+ array( array( $this, 'get_taxonomies' ), WP_JSON_Server::READABLE ),
),
'/posts/types/(?P<type>\w+)/taxonomies/(?P<taxonomy>\w+)' => array(
- array( array( $this, 'getTaxonomy' ), WP_JSON_Server::READABLE ),
+ array( array( $this, 'get_taxonomy' ), WP_JSON_Server::READABLE ),
),
'/posts/types/(?P<type>\w+)/taxonomies/(?P<taxonomy>\w+)/terms' => array(
- array( array( $this, 'getTerms' ), WP_JSON_Server::READABLE ),
+ array( array( $this, 'get_terms' ), WP_JSON_Server::READABLE ),
array( '__return_null', WP_JSON_Server::CREATABLE | WP_JSON_Server::ACCEPT_JSON ),
),
'/posts/types/(?P<type>\w+)/taxonomies/(?P<taxonomy>\w+)/terms/(?P<term>\w+)' => array(
- array( array( $this, 'getTerm' ), WP_JSON_Server::READABLE ),
+ array( array( $this, 'get_term' ), WP_JSON_Server::READABLE ),
array( '__return_null', WP_JSON_Server::EDITABLE | WP_JSON_Server::ACCEPT_JSON ),
array( '__return_null', WP_JSON_Server::DELETABLE ),
),
@@ -30,11 +30,11 @@ public function registerRoutes( $routes ) {
/**
* Get taxonomies
- *
+ *
* @param string $type Post type to get taxonomies for
* @return array Taxonomy data
*/
- public function getTaxonomies( $type ) {
+ public function get_taxonomies( $type ) {
$taxonomies = get_object_taxonomies( $type, 'objects' );
$data = array();
@@ -51,11 +51,11 @@ public function getTaxonomies( $type ) {
/**
* Get taxonomies
- *
+ *
* @param string $type Post type to get taxonomies for
* @return array Taxonomy data
*/
- public function getTaxonomy( $type, $taxonomy ) {
+ public function get_taxonomy( $type, $taxonomy ) {
$tax = get_taxonomy( $taxonomy );
if ( empty( $tax ) )
return new WP_Error( 'json_taxonomy_invalid_id', __( 'Invalid taxonomy ID.' ), array( 'status' => 404 ) );
@@ -108,7 +108,7 @@ protected function prepare_taxonomy( $taxonomy, $type, $_in_collection = false )
* @return array Filtered data
*/
public function add_taxonomy_data( $data, $type ) {
- $data['taxonomies'] = $this->getTaxonomies( $type->name );
+ $data['taxonomies'] = $this->get_taxonomies( $type->name );
return $data;
}
@@ -120,7 +120,7 @@ public function add_taxonomy_data( $data, $type ) {
* @param string $taxonomy Taxonomy slug
* @return array Term collection
*/
- public function getTerms( $type, $taxonomy ) {
+ public function get_terms( $type, $taxonomy ) {
if ( ! taxonomy_exists( $taxonomy ) )
return new WP_Error( 'json_taxonomy_invalid_id', __( 'Invalid taxonomy ID.' ), array( 'status' => 404 ) );
@@ -147,7 +147,7 @@ public function getTerms( $type, $taxonomy ) {
* @param string $context Context (view/view-parent)
* @return array Term entity
*/
- public function getTerm( $type, $taxonomy, $term, $context = 'view' ) {
+ public function get_term( $type, $taxonomy, $term, $context = 'view' ) {
if ( ! taxonomy_exists( $taxonomy ) )
return new WP_Error( 'json_taxonomy_invalid_id', __( 'Invalid taxonomy ID.' ), array( 'status' => 404 ) );
@@ -201,7 +201,7 @@ protected function prepare_term( $term, $type, $context = 'view' ) {
);
if ( ! empty( $data['parent'] ) && $context === 'view' ) {
- $data['parent'] = $this->getTerm( $type, $term->taxonomy, $data['parent'], 'view-parent' );
+ $data['parent'] = $this->get_term( $type, $term->taxonomy, $data['parent'], 'view-parent' );
}
elseif ( empty( $data['parent'] ) ) {
$data['parent'] = null;
@@ -209,4 +209,19 @@ protected function prepare_term( $term, $type, $context = 'view' ) {
return apply_filters( 'json_prepare_term', $data, $term );
}
+
+ /**
+ * Magic method used to temporaly deprecate camelcase functions
+ *
+ * @param string $name Function name
+ * @param array $arguments Function arguments
+ * @return mixed
+ */
+ public function __call($name, $arguments) {
+ $underscored = strtolower(preg_replace('/(?!^)[[:upper:]][[:lower:]]/', '_$0', $name));
+ if ( method_exists( $this, $underscored ) ) {
+ _deprecated_function( __CLASS__ . '->' . $name, 'WPAPI-0.9', __CLASS__ . '->' . $underscored );
+ return call_user_func_array( array( $this, $underscored ), $arguments );
+ }
+ }
}
View
14 plugin.php
@@ -47,24 +47,24 @@ function json_api_default_filters($server) {
// Posts
$wp_json_posts = new WP_JSON_Posts($server);
- add_filter( 'json_endpoints', array( $wp_json_posts, 'registerRoutes' ), 0 );
+ add_filter( 'json_endpoints', array( $wp_json_posts, 'register_routes' ), 0 );
// Pages
$wp_json_pages = new WP_JSON_Pages($server);
- add_filter( 'json_endpoints', array( $wp_json_pages, 'registerRoutes' ), 1 );
+ add_filter( 'json_endpoints', array( $wp_json_pages, 'register_routes' ), 1 );
add_filter( 'json_post_type_data', array( $wp_json_pages, 'type_archive_link' ), 10, 2 );
// Media
$wp_json_media = new WP_JSON_Media($server);
- add_filter( 'json_endpoints', array( $wp_json_media, 'registerRoutes' ), 1 );
- add_filter( 'json_prepare_post', array( $wp_json_media, 'addThumbnailData' ), 10, 3 );
- add_filter( 'json_pre_insert_post', array( $wp_json_media, 'preinsertCheck' ), 10, 3 );
- add_filter( 'json_insert_post', array( $wp_json_media, 'attachThumbnail' ), 10, 3 );
+ add_filter( 'json_endpoints', array( $wp_json_media, 'register_routes' ), 1 );
+ add_filter( 'json_prepare_post', array( $wp_json_media, 'add_thumbnail_data' ), 10, 3 );
+ add_filter( 'json_pre_insert_post', array( $wp_json_media, 'preinsert_check' ), 10, 3 );
+ add_filter( 'json_insert_post', array( $wp_json_media, 'attach_thumbnail' ), 10, 3 );
add_filter( 'json_post_type_data', array( $wp_json_media, 'type_archive_link' ), 10, 2 );
// Posts
$wp_json_taxonomies = new WP_JSON_Taxonomies($server);
- add_filter( 'json_endpoints', array( $wp_json_taxonomies, 'registerRoutes' ), 2 );
+ add_filter( 'json_endpoints', array( $wp_json_taxonomies, 'register_routes' ), 2 );
add_filter( 'json_post_type_data', array( $wp_json_taxonomies, 'add_taxonomy_data' ), 10, 2 );
add_filter( 'json_prepare_post', array( $wp_json_taxonomies, 'add_term_data' ), 10, 3 );
}
View
14 testhelper.php
@@ -16,11 +16,11 @@ class WP_JSON_Server_TestHelper {
protected $reports = array();
public function __construct() {
- add_action('init', array($this, 'startCoverage'));
- add_filter('json_endpoints', array($this, 'addEndpoints'));
+ add_action('init', array($this, 'start_coverage'));
+ add_filter('json_endpoints', array($this, 'add_endpoints'));
}
- public function startCoverage() {
+ public function start_coverage() {
if ( ! isset( $_REQUEST['_jsoncurrenttest'] ) ) {
return;
}
@@ -38,7 +38,7 @@ public function startCoverage() {
$this->coverage->start( $current_test );
}
- public function endCoverage() {
+ public function end_coverage() {
if ( ! $this->coverage ) {
return;
}
@@ -48,14 +48,14 @@ public function endCoverage() {
set_transient('json_testhelper_coverage', $this->reports, 30 * MINUTE_IN_SECONDS);
}
- public function addEndpoints($routes) {
+ public function add_endpoints($routes) {
$routes['/testhelper/report'] = array(
- array( array( $this, 'getReports' ), WP_JSON_Server::METHOD_POST ),
+ array( array( $this, 'get_reports' ), WP_JSON_Server::METHOD_POST ),
);
return $routes;
}
- public function getReports() {
+ public function get_reports() {
$this->reports = get_transient('json_testhelper_coverage');
if (empty($this->reports)) {
return new WP_Error('json_testhelper_no_report', __('No report data available', 'json_testhelper'), array('status' => 400));
Something went wrong with that request. Please try again.