-
Notifications
You must be signed in to change notification settings - Fork 203
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
Closed
REST API Support #334
Changes from 24 commits
Commits
Show all changes
67 commits
Select commit
Hold shift + click to select a range
17d8d6b
Initial Commit
hbarroso 9d3a7ed
Added Search Route and Unit Test
hbarroso 1b0c34f
Adds WordPress version check
hbarroso abe47ce
Added 'existing_authors' to search route and fixed some coding standards
hbarroso 0cc0334
Added a better type check for the 'existing_authors' parameter for th…
hbarroso 55caf4f
Added version check for the REST API test class.
hbarroso 5ed568f
Fixed an authentication issue with the tests
hbarroso bf08818
Replaced all array declarations to be compatible with PHP <= 5.3
hbarroso efb528f
Removed array dereferencing in API tests.
hbarroso 271bce4
Added new /post route and code refactoring
hbarroso 2fb2aee
Fixed get_request_response() array parameter for PHP =< 5.3 tests
hbarroso 58e513a
Added DELETE route for Posts and refactored validation methods.
hbarroso fbd7f59
Huge restructure of current API class design.
hbarroso f1a5ce9
Improved code documentation
hbarroso ccf4abb
Refactored a new add_coauthors() in the CoAuthors_API_Post class.
hbarroso aa2a019
Added GET action to /Post route.
hbarroso bc5efce
Fixed issue on CoAuthors_API_Controller create_routes() method.
hbarroso df8a93f
Added the correct domain to the translations methods.
hbarroso a4e64d1
Add missing key id, and removed unused debug function from tests.
hbarroso 184498c
Switched throw exceptions to is_wp_error().
hbarroso 34b23ff
Added Post method for the Guest/ endpoint.
hbarroso d570076
Added PUT method for guest/ route.
hbarroso 41b13b5
Removed 'user_login' from being updated from the guest/ route.
hbarroso b6903ed
Added GET method for /Guest routes.
hbarroso 84af809
Added Delete method for /guests route.
hbarroso 2f9f388
Adds id sanitization/validation to the PUT method
hbarroso cd2a9c0
Changes Tabs/Spaces and other coding guidelines for WordPress.
hbarroso 2d9c93e
Removed a big ugly If statement wrap around tests.
hbarroso f291d0b
Added a new support class test for Rest Server.
hbarroso 28ef797
Removed unused statement inside Guest API Class.
hbarroso da5f9e4
Removed duplicated validation logic checks.
hbarroso 8e3b5a8
Improved better http return codes funcionality.
hbarroso 4b72026
Removed unused variable.
hbarroso f5973e3
Changed POST to PUT method for /Post routes
hbarroso cb616e4
Changed the Search/ from POST to GET
hbarroso 326f1f0
Changed /search to /authors
hbarroso af7397b
Changed /post to /posts
hbarroso b4d93b0
Changed /post to /posts
hbarroso c88fff7
Added new methods to CoAuthors_API_Controller class
hbarroso 1b16fae
Replaced API methods to use the new parent class item methods.
hbarroso 9d75b23
Added prepare_data() to Authors and Guests routes.
hbarroso ed08ddd
GET /posts/<id> is now public.
hbarroso 62a2186
Changed the way the get_args() work.
hbarroso f1d462d
Removed unecessary argument on Posts delete_item route.
hbarroso eacccc2
Updated core function add_coauthors() to ignore invalid authors.
hbarroso d35879f
Posts route are now better organized and deleted message responses.
hbarroso d691693
Guests route DELETE returns the deleted object instead of message.
hbarroso 048f6e6
Removed unused 'exclude_authors'
hbarroso fafa22b
Guests new collection GET endpoint.
hbarroso a4689d5
Fixes a missing statement in the search_guests() method.
hbarroso a3b08d7
Code guidelines cleaning
hbarroso 92d202f
Added API Documentation
hbarroso ab0c76b
Replaced sanitization field to better suited functions.
hbarroso 42c1409
Removed unecessary post_type check.
hbarroso deeed3f
Removed logic to check for duplicated usernames in Guests PUT
hbarroso 67a3ccc
Added esc_like() to search_guests() method on query db.
hbarroso 248176c
Route arguments big refactor.
hbarroso d1123e4
Code guidelines format [ci skip]
hbarroso 8d2dd44
Updated documentation to match the new return results [ci skip]
hbarroso b0fef6e
Added correct checkmark to Guests route
hbarroso 65ae179
Changed `wp_set_current_user` to use 0 instead of -1
hbarroso 311e344
Updated `q` parameter to use only one sanitization function.
hbarroso 0c4599a
Renamed and remove uneeded logic for does_coauthor_exist and does_coa…
hbarroso cd1a741
Updates API.md documentation
hbarroso aefface
Renamed does_coauthor_email_exist() to is_email_unique()
hbarroso a0c360a
Removed uneeded sanitization function for Id param.
hbarroso b78ced2
Refactored current_user_can_set_authors() method.
hbarroso File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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' ); | ||
} | ||
|
@@ -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() { | ||
|
@@ -126,6 +127,7 @@ function coauthors_plus() { | |
* and the custom post type to store our author data | ||
*/ | ||
public function action_init() { | ||
global $wp_version; | ||
|
||
// Allow Co-Authors Plus to be easily translated | ||
load_plugin_textdomain( 'co-authors-plus', null, dirname( plugin_basename( __FILE__ ) ) . '/languages/' ); | ||
|
@@ -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 ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In 271bce4, you mentioned that you added this |
||
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; | ||
|
@@ -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; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
/** | ||
* CoAuthors Plus REST API Boot file. | ||
* It loads all the required classes need by it. | ||
*/ | ||
global $wp_version; | ||
|
||
if ( version_compare( $wp_version, '4.4', '<' ) ) { | ||
return; | ||
} | ||
|
||
require_once( dirname( __FILE__ ) . '/class-coauthors-api.php' ); | ||
require_once( dirname( __FILE__ ) . '/class-coauthors-api-controller.php' ); | ||
require_once( dirname( __FILE__ ) . '/class-coauthors-api-search.php' ); | ||
require_once( dirname( __FILE__ ) . '/class-coauthors-api-post.php' ); | ||
require_once( dirname( __FILE__ ) . '/class-coauthors-api-guest.php' ); | ||
|
||
define('COAUTHORS_PLUS_API_NAMESPACE', 'coauthors/'); | ||
define('COAUTHORS_PLUS_API_VERSION', 'v1'); | ||
|
||
$coauthors_api = new CoAuthors_API(); | ||
$coauthors_api->register_controller( new CoAuthors_API_Search() ); | ||
$coauthors_api->register_controller( new CoAuthors_API_Post() ); | ||
$coauthors_api->register_controller( new CoAuthors_API_Guest() ); | ||
|
||
add_action( 'rest_api_init', array( $coauthors_api, 'boot' ) ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
<?php | ||
|
||
/** | ||
* The base class for all API Controllers. | ||
* | ||
* Class CoAuthors_API_Controller | ||
*/ | ||
class CoAuthors_API_Controller { | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $route = null; | ||
|
||
/** | ||
* Use this method to define the routes using WP register_rest_route() function. | ||
* | ||
* @throws Exception | ||
* @return array | ||
*/ | ||
public function create_routes() { | ||
throw new Exception( 'Child class needs to implement this method.' ); | ||
} | ||
|
||
/** | ||
* Handles route authorization if requested. | ||
* | ||
* @param WP_REST_Request $request | ||
* @return bool | ||
*/ | ||
public function authorization( WP_REST_Request $request ) { | ||
return true; | ||
} | ||
|
||
/** | ||
* DELETE HTTP method | ||
* | ||
* @param WP_REST_Request $request | ||
* @throws Exception | ||
* @return WP_REST_Response | ||
*/ | ||
public function get( WP_REST_Request $request ) { | ||
throw new Exception( 'Not implemented.' ); | ||
} | ||
|
||
/** | ||
* DELETE HTTP method | ||
* | ||
* @param WP_REST_Request $request | ||
* @throws Exception | ||
* @return WP_REST_Response | ||
*/ | ||
public function post( WP_REST_Request $request ) { | ||
throw new Exception( 'Not implemented.' ); | ||
} | ||
|
||
/** | ||
* DELETE HTTP method | ||
* | ||
* @param WP_REST_Request $request | ||
* @throws Exception | ||
* @return WP_REST_Response | ||
*/ | ||
public function put( WP_REST_Request $request ) { | ||
throw new Exception( 'Not implemented.' ); | ||
} | ||
|
||
/** | ||
* DELETE HTTP method | ||
* | ||
* @param WP_REST_Request $request | ||
* @throws Exception | ||
* @return WP_REST_Response | ||
*/ | ||
public function delete( WP_REST_Request $request ) { | ||
throw new Exception( 'Not implemented.' ); | ||
} | ||
|
||
/** | ||
* Returns a clean array | ||
* | ||
* @param $param | ||
* @param WP_REST_Request $request | ||
* @param $key | ||
* | ||
* @return array | ||
*/ | ||
public function sanitize_array($param, WP_REST_Request $request, $key) { | ||
return array_map( 'esc_attr', (array) $param ); | ||
} | ||
|
||
/** | ||
* Helper method that returns a filtered array for the search_authors() main class | ||
* method. | ||
* | ||
* @param array $arr | ||
* | ||
* @return array | ||
*/ | ||
protected function filter_authors_array( $arr ) { | ||
$coauthors = array(); | ||
|
||
if ( ! is_array( $arr ) ) | ||
return $coauthors; | ||
|
||
foreach ( $arr as $author ) { | ||
$coauthors[] = array( | ||
'id' => $author->ID, | ||
'user_login' => $author->user_login, | ||
'display_name' => $author->display_name, | ||
'user_email' => $author->user_email, | ||
'user_nicename' => $author->user_nicename | ||
); | ||
} | ||
|
||
return $coauthors; | ||
} | ||
|
||
/** | ||
* Returns an array with the register_rest_route args definition. | ||
* | ||
* @param string $method | ||
* @return array | ||
*/ | ||
protected function get_args( $method = null ) { | ||
return array(); | ||
} | ||
|
||
/** | ||
* Wraps an array into the WP_REST_Response. | ||
* Currently very limited, it should allow header and code to be setted. | ||
* | ||
* @param array $data | ||
* @throws Exception | ||
* @return WP_REST_Response | ||
*/ | ||
protected function send_response( array $data ) { | ||
$response = new WP_REST_Response( $data ); | ||
|
||
return $response; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
protected function get_route() { | ||
return $this->route; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
protected function get_namespace() { | ||
return COAUTHORS_PLUS_API_NAMESPACE . COAUTHORS_PLUS_API_VERSION; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's some whitespace issues here. Also, this variable is not used.