Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

REST API Support #334

Closed
wants to merge 67 commits into from
Closed
Show file tree
Hide file tree
Changes from 63 commits
Commits
Show all changes
67 commits
Select commit Hold shift + click to select a range
17d8d6b
Initial Commit
hbarroso Feb 16, 2016
9d3a7ed
Added Search Route and Unit Test
hbarroso Feb 16, 2016
1b0c34f
Adds WordPress version check
hbarroso Feb 17, 2016
abe47ce
Added 'existing_authors' to search route and fixed some coding standards
hbarroso Feb 17, 2016
0cc0334
Added a better type check for the 'existing_authors' parameter for th…
hbarroso Feb 17, 2016
55caf4f
Added version check for the REST API test class.
hbarroso Feb 17, 2016
5ed568f
Fixed an authentication issue with the tests
hbarroso Feb 17, 2016
bf08818
Replaced all array declarations to be compatible with PHP <= 5.3
hbarroso Feb 17, 2016
efb528f
Removed array dereferencing in API tests.
hbarroso Feb 18, 2016
271bce4
Added new /post route and code refactoring
hbarroso Feb 18, 2016
2fb2aee
Fixed get_request_response() array parameter for PHP =< 5.3 tests
hbarroso Feb 18, 2016
58e513a
Added DELETE route for Posts and refactored validation methods.
hbarroso Feb 20, 2016
fbd7f59
Huge restructure of current API class design.
hbarroso Feb 20, 2016
f1a5ce9
Improved code documentation
hbarroso Feb 20, 2016
ccf4abb
Refactored a new add_coauthors() in the CoAuthors_API_Post class.
hbarroso Feb 21, 2016
aa2a019
Added GET action to /Post route.
hbarroso Feb 21, 2016
bc5efce
Fixed issue on CoAuthors_API_Controller create_routes() method.
hbarroso Feb 21, 2016
df8a93f
Added the correct domain to the translations methods.
hbarroso Feb 21, 2016
a4e64d1
Add missing key id, and removed unused debug function from tests.
hbarroso Feb 21, 2016
184498c
Switched throw exceptions to is_wp_error().
hbarroso Feb 21, 2016
34b23ff
Added Post method for the Guest/ endpoint.
hbarroso Feb 22, 2016
d570076
Added PUT method for guest/ route.
hbarroso Feb 22, 2016
41b13b5
Removed 'user_login' from being updated from the guest/ route.
hbarroso Feb 22, 2016
b6903ed
Added GET method for /Guest routes.
hbarroso Feb 23, 2016
84af809
Added Delete method for /guests route.
hbarroso Feb 28, 2016
2f9f388
Adds id sanitization/validation to the PUT method
hbarroso Feb 28, 2016
cd2a9c0
Changes Tabs/Spaces and other coding guidelines for WordPress.
hbarroso Feb 28, 2016
2d9c93e
Removed a big ugly If statement wrap around tests.
hbarroso Feb 28, 2016
f291d0b
Added a new support class test for Rest Server.
hbarroso Feb 28, 2016
28ef797
Removed unused statement inside Guest API Class.
hbarroso Feb 28, 2016
da5f9e4
Removed duplicated validation logic checks.
hbarroso Feb 28, 2016
8e3b5a8
Improved better http return codes funcionality.
hbarroso Feb 28, 2016
4b72026
Removed unused variable.
hbarroso Feb 28, 2016
f5973e3
Changed POST to PUT method for /Post routes
hbarroso Feb 29, 2016
cb616e4
Changed the Search/ from POST to GET
hbarroso Mar 2, 2016
326f1f0
Changed /search to /authors
hbarroso Mar 2, 2016
af7397b
Changed /post to /posts
hbarroso Mar 2, 2016
b4d93b0
Changed /post to /posts
hbarroso Mar 2, 2016
c88fff7
Added new methods to CoAuthors_API_Controller class
hbarroso Mar 2, 2016
1b16fae
Replaced API methods to use the new parent class item methods.
hbarroso Mar 2, 2016
9d75b23
Added prepare_data() to Authors and Guests routes.
hbarroso Mar 2, 2016
ed08ddd
GET /posts/<id> is now public.
hbarroso Mar 2, 2016
62a2186
Changed the way the get_args() work.
hbarroso Mar 5, 2016
f1d462d
Removed unecessary argument on Posts delete_item route.
hbarroso Mar 5, 2016
eacccc2
Updated core function add_coauthors() to ignore invalid authors.
hbarroso Mar 5, 2016
d35879f
Posts route are now better organized and deleted message responses.
hbarroso Mar 6, 2016
d691693
Guests route DELETE returns the deleted object instead of message.
hbarroso Mar 6, 2016
048f6e6
Removed unused 'exclude_authors'
hbarroso Mar 6, 2016
fafa22b
Guests new collection GET endpoint.
hbarroso Mar 6, 2016
a4689d5
Fixes a missing statement in the search_guests() method.
hbarroso Mar 7, 2016
a3b08d7
Code guidelines cleaning
hbarroso Mar 9, 2016
92d202f
Added API Documentation
hbarroso Mar 31, 2016
ab0c76b
Replaced sanitization field to better suited functions.
hbarroso Apr 1, 2016
42c1409
Removed unecessary post_type check.
hbarroso Apr 1, 2016
deeed3f
Removed logic to check for duplicated usernames in Guests PUT
hbarroso Apr 1, 2016
67a3ccc
Added esc_like() to search_guests() method on query db.
hbarroso Apr 1, 2016
248176c
Route arguments big refactor.
hbarroso Apr 2, 2016
d1123e4
Code guidelines format [ci skip]
hbarroso Apr 2, 2016
8d2dd44
Updated documentation to match the new return results [ci skip]
hbarroso Apr 10, 2016
b0fef6e
Added correct checkmark to Guests route
hbarroso Apr 10, 2016
65ae179
Changed `wp_set_current_user` to use 0 instead of -1
hbarroso Apr 10, 2016
311e344
Updated `q` parameter to use only one sanitization function.
hbarroso Apr 10, 2016
0c4599a
Renamed and remove uneeded logic for does_coauthor_exist and does_coa…
hbarroso Apr 10, 2016
cd1a741
Updates API.md documentation
hbarroso Apr 19, 2016
aefface
Renamed does_coauthor_email_exist() to is_email_unique()
hbarroso Apr 19, 2016
a0c360a
Removed uneeded sanitization function for Id param.
hbarroso Apr 19, 2016
b78ced2
Refactored current_user_can_set_authors() method.
hbarroso Apr 19, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
289 changes: 289 additions & 0 deletions API.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,289 @@
This is the documentation support for the REST API feature.

Route | Method | Description
-------- | --------------- | ----------
/**authors** | GET | Search for available authors.
/**posts**/`:post_id`/**authors** | GET | Get all authors from a post
/**posts**/`:post_id`/**authors** | PUT | Appends a new author to a post
/**posts**/`:post_id`/**authors**/`:author_id` | DELETE | Removes an author from a post
/**guests** | GET | Lists of filters all the currently available guests
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you clarify this line?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be "Lists OR filters current available guests". It's a typo. Going to update it the docs.

/**guests** | POST | Creates a new guest
/**guests**/`:guest_id` | PUT | Updates a new guest
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably updates an existing guest?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I'm going to correct this too.

/**guests**/`:guest_id` | DELETE | Deletes a guest

## Authors
This routes allows searching for authors.

### `GET`

**URL** : `http://<domain>/wp-json/coauthors/v1/authors`

**Requires Authentication**: :lock:

#### Parameters

Name | Type | Required | Description
------------ | ------------- | ------------- | -------------
`q` | String | :white_check_mark: | Query parameter

#### Response
```
{
"authors": [
{
"id": 3,
"display_name": "Dummy",
"user_email": "dummy@gmail.com",
"user_nicename": "dummy1"
},
{
"id": 4,
"display_name": "dummy2",
"user_email": "dummy2@gmail.com",
"user_nicename": "dummy2"
}
]
}
```


## Posts

Add, appends or remove a group of authors to an existing post.

### `GET`

**URL** : `http://<domain>/wp-json/coauthors/v1/posts/<post_id>/authors`

**Requires Authentication**: :lock:

#### Parameters

Name | Type | Required | Description
------------ | ------------- | ------------- | -------------
`post_id` | Int | | The post unique id.

#### Response
```
{
"authors": [
{
"id": 3,
"display_name": "Dummy",
"user_nicename": "dummy1"
},
{
"id": 4,
"display_name": "dummy2",
"user_nicename": "dummy2"
}
]
}
```

### `PUT`

**URL** : `http://<domain>/wp-json/coauthors/v1/posts/<post_id>/authors`

**Requires Authentication**: :lock:

#### Parameters

Name | Type | Required | Description
------------ | ------------- | ------------- | -------------
`coauthors` | Array | :white_check_mark: | An array of authors usernames.

#### Response
```
[
{
"id": 3,
"display_name": "Dummy",
"user_nicename": "dummy1"
}
]
```

### `DELETE`

**URL** : `http://<domain>/wp-json/coauthors/v1/posts/<post_id>/authors/<coauthor_id>`

**Requires Authentication**: :lock:

#### Parameters

Name | Type | Required | Description
------------ | ------------- | ------------- | -------------
`post_id` | Int | :white_check_mark: | Post id
`author_id` | Int | :white_check_mark: | Author id to delete from post

#### Response
```
[
{
"id": 3,
"display_name": "Dummy",
"user_nicename": "dummy1"
}
]
```

## Guests

Creates, updates or removes a guest author.

### `GET`

**URL** : `http://<domain>/wp-json/coauthors/v1/guests`

**Requires Authentication**: :lock:

Name | Type | Required | Description
------------ | ------------- | ------------- | -------------
`q` | String | :white_check_mark: | If filled, narrows the search by the `user_nicename`

### `POST`

**URL** : `http://<domain>/wp-json/coauthors/v1/guests`

**Requires Authentication**: :lock:

#### Parameters

Name | Type | Required | Description
------------ | ------------- | ------------- | -------------
`display_name` | String | :white_check_mark: | Display name
`user_login` | String | :white_check_mark: | User login, must be unique to guests.
`user_email` | String | :white_check_mark: | User email
`first_name` | String | | First name
`last_name` | String | | Last name
`linked_account` | String | | Links the guest account to an existing account
`website` | String | | User website
`aim` | String | | AIM
`yahooim` | String | | Yahoo IM
`jabber` | String | | Jabber
`Description` | String | | A text Description

#### Response
```
[
{
"id": "172",
"display_name": "foo",
"first_name": "Foo",
"last_name": "Bar",
"user_email": "foobar@gmail.com",
"linked_account": "",
"website": "http://www.foobar.org/",
"aim": "testaim",
"yahooim": "testyaho2",
"jabber": "testjabber",
"description": "Some simple description.",
"user_nicename": "foobar",
}
]
```
### `GET`

**URL** : `http://<domain>/wp-json/coauthors/v1/guests/<guest_id>`

**Requires Authentication**: :lock:

#### Parameters

Name | Type | Required | Description
------------ | ------------- | ------------- | -------------
`ID` | Integer | :white_check_mark: | Guest Id

#### Response
```
[
{
"id": "172",
"display_name": "foo",
"first_name": "Foo",
"last_name": "Bar",
"user_email": "foobar@gmail.com",
"linked_account": "",
"website": "http://www.foobar.org/",
"aim": "testaim",
"yahooim": "testyaho2",
"jabber": "testjabber",
"description": "Some simple description.",
"user_nicename": "foobar",
}
]
```

### `PUT`

**URL** : `http://<domain>/wp-json/coauthors/v1/guests/<guest_id>`

**Requires Authentication**: :lock:

#### Parameters

Name | Type | Required | Description
------------ | ------------- | ------------- | -------------
`display_name` | String | | Display name
`user_email` | String | | User email
`first_name` | String | | First name
`last_name` | String | | Last name
`linked_account` | String | | Links the guest account to an existing account
`website` | String | | User website
`aim` | String | | AIM
`yahooim` | String | | Yahoo IM
`jabber` | String | | Jabber
`Description` | String | | A text Description

#### Response
```
[
{
"id": "172",
"display_name": "foo",
"first_name": "John",
"last_name": "Bar",
"user_email": "foobar@gmail.com",
"linked_account": "",
"website": "http://www.foobar.org/",
"aim": "testaim",
"yahooim": "testyaho2",
"jabber": "testjabber",
"description": "New description.",
"user_nicename": "foobar",
}
]
```

### `DELETE`

**URL** : `http://<domain>/wp-json/coauthors/v1/guests/<guest_id>`

**Requires Authentication**: :lock:

#### Parameters

Name | Type | Required | Description
------------ | ------------- | ------------- | -------------
`ID` | Integer | :white_check_mark: | Guest Id

#### Response
```
[
{
"id": "172",
"display_name": "foo",
"first_name": "John",
"last_name": "Bar",
"user_email": "foobar@gmail.com",
"linked_account": "",
"website": "http://www.foobar.org/",
"aim": "testaim",
"yahooim": "testyaho2",
"jabber": "testjabber",
"description": "New description.",
"user_nicename": "foobar",
}
]
```
26 changes: 16 additions & 10 deletions co-authors-plus.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@

require_once( dirname( __FILE__ ) . '/php/class-coauthors-template-filters.php' );

require_once( dirname( __FILE__ ) . '/php/api/boot.php' );

if ( defined( 'WP_CLI' ) && WP_CLI ) {
require_once( dirname( __FILE__ ) . '/php/class-wp-cli.php' );
}
Expand Down Expand Up @@ -114,7 +116,6 @@ function __construct() {

// Support infinite scroll for Guest Authors on author pages
add_filter( 'infinite_scroll_js_settings', array( $this, 'filter_infinite_scroll_js_settings' ), 10, 2 );

}

function coauthors_plus() {
Expand All @@ -126,7 +127,6 @@ function coauthors_plus() {
* and the custom post type to store our author data
*/
public function action_init() {

// Allow Co-Authors Plus to be easily translated
load_plugin_textdomain( 'co-authors-plus', null, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );

Expand Down Expand Up @@ -802,7 +802,6 @@ public function add_coauthors( $post_id, $coauthors, $append = false ) {
global $current_user, $wpdb;

$post_id = (int) $post_id;
$insert = false;

// Best way to persist order
if ( $append ) {
Expand All @@ -822,6 +821,9 @@ public function add_coauthors( $post_id, $coauthors, $append = false ) {
foreach ( $coauthors as &$author_name ) {

$author = $this->get_coauthor_by( 'user_nicename', $author_name );
if ( !$author ) {
continue;
}
$coauthor_objects[] = $author;
$term = $this->update_author_term( $author );
$author_name = $term->slug;
Expand Down Expand Up @@ -947,24 +949,27 @@ function filter_count_user_posts( $count, $user_id ) {
/**
* Checks to see if the current user can set authors or not
*/
function current_user_can_set_authors( $post = null ) {
function current_user_can_set_authors( $post = null, $is_api_request = false ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In 271bce4, you mentioned that you added this $is_api_request parameter, but you didn't say why. Why is it important/useful?

global $typenow;

if ( ! $post ) {
$post = get_post();
if ( ! $post ) {
if ( ! $post && ! $is_api_request) {
return false;
}
}

$post_type = $post->post_type;
if ( $post ) {
$post_type = $post->post_type;

// TODO: need to fix this; shouldn't just say no if don't have post_type
if ( ! $post_type ) {
return false;
// TODO: need to fix this; shouldn't just say no if don't have post_type
if ( ! $post_type ) {
return false;
}

$post_type_object = get_post_type_object( $post_type );
}

$post_type_object = get_post_type_object( $post_type );
$current_user = wp_get_current_user();
if ( ! $current_user ) {
return false;
Expand Down Expand Up @@ -1463,6 +1468,7 @@ public function filter_jetpack_open_graph_tags( $og_tags, $image_dimensions ) {
// Send back the updated Open Graph Tags
return apply_filters( 'coauthors_open_graph_tags', $og_tags );
}

}

global $coauthors_plus;
Expand Down
Loading