Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
44 additions
and
130 deletions.
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 |
---|---|---|
@@ -1,103 +1,8 @@ | ||
# WonderWp API Component | ||
|
||
### Wordpress API Usage example : | ||
A WonderWp package allowing you to work with the WordPress API mechanism to create custom API endpoints | ||
|
||
Based on https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/ | ||
## Documentation | ||
|
||
Supposed we want this endpoint `https://domain.test/wp-json/questions/v1/questions-by-user?page=1` the annotation would be : | ||
- **namespace** : corresponding `questions` | ||
- **version** : corresponding to `v1`, we can maintain different api version by incrementing the version and keeping the same endpoint url (default to `v1`) | ||
- **url** : corresponding to `questions-by-user`, can include dynamic parameters according to wordpress documentation (example `/author/(?P<id>\d+)`) | ||
- **args** : corresponding to third parameter of `register_rest_route` function : [doc](https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/#arguments) | ||
|
||
```php | ||
|
||
class QuestionApiService extends AbstractApiService | ||
{ | ||
/** | ||
* @WPApiEndpoint( | ||
* namespace = "questions", | ||
* version = "v1", | ||
* url = "/questions-by-user", | ||
* args = { | ||
* "methods": "GET", | ||
* "args": { | ||
* "page": { | ||
* "default": 1, | ||
* "sanitize_callback": "convertToInt" | ||
* }, | ||
* "limit": { | ||
* "default": 1, | ||
* "sanitize_callback": "convertToInt" | ||
* } | ||
* }, | ||
* "permission_callback": "canFetchQuestions" | ||
* } | ||
* ) | ||
*/ | ||
public function questionsByUser(WP_REST_Request $request) | ||
{ | ||
$page = $request->get_param('page'); | ||
|
||
/* Processing get questions */ | ||
|
||
// To handle error we can return WP_Error instance | ||
return WP_Error('error_code', 'Error message', ['status' => 400]); | ||
|
||
// Or success response | ||
return new WP_REST_Response([ | ||
'questions' => [], | ||
]); | ||
} | ||
|
||
public function canFetchQuestions(WP_REST_Request $request) | ||
{ | ||
$user = wp_get_current_user(); | ||
|
||
/* processing verification */ | ||
|
||
return true; | ||
} | ||
|
||
public function convertToInt($param, $request, $key) | ||
{ | ||
/* Processing sanitize method */ | ||
return (int) $param; | ||
} | ||
} | ||
``` | ||
|
||
The namespace could be define globaly. | ||
For example to create this endpoint `https://domain.test/wp-json/questions/v1/delete-question/1` | ||
|
||
```php | ||
/** | ||
* @WPApiNamespace( | ||
* namespace="questions" | ||
* ) | ||
*/ | ||
class QuestionApiService extends AbstractApiService | ||
{ | ||
/** | ||
* @WPApiEndpoint( | ||
* url = "/delete-question/(?P<id>\d+)", | ||
* args = { | ||
* "methods": "DELETE", | ||
* "permission_callback": "canDeleteQuestion" | ||
* } | ||
* ) | ||
*/ | ||
public function deleteQuestion(WP_REST_Request $request) | ||
{ | ||
/* Processing deletion */ | ||
} | ||
|
||
public function canDeleteQuestion() | ||
{} | ||
} | ||
``` | ||
|
||
### Important notice | ||
|
||
Because we cannot reference `$this` from within an annotation, for any callback function specified inside a @WPApiEndpoint annotation, be it `sanitize_callback`, `validate_callback`, `permission_callback` and so on, the provided callback name (for example `myFunction`), is first looked for on the same class instance than the method carrying out the annotation. If not found there, it's then looked for on the global namespace (like a function name) | ||
In second time, we look for a global function. | ||
- Documentation about the API component can be found [here](http://wonderwp.net/Framewok_components/Ajax/index.html). | ||
- Documentation about API services can be found [here](http://wonderwp.net/Creating_a_plugin/Services/Api_service.html). |
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 |
---|---|---|
@@ -1,34 +1,34 @@ | ||
{ | ||
"name": "wonderwp/api", | ||
"type": "library", | ||
"description": "WonderWp Api Component", | ||
"keywords": ["api"], | ||
"homepage": "https://wonderwp.com", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Jeremy Desvaux", | ||
"homepage": "http://jdmweb.com" | ||
}, | ||
{ | ||
"name": "WonderWp Community", | ||
"homepage": "https://wonderwp.com/contributors" | ||
} | ||
], | ||
"require": { | ||
"php": "~5.6|~7", | ||
"wonderwp/service": "^1.0", | ||
"wonderwp/http-foundation": "^1.0", | ||
"doctrine/annotations": "^1.12" | ||
"name": "wonderwp/api", | ||
"description": "WonderWp Api Component", | ||
"type": "library", | ||
"keywords": ["api","WonderWp","WordPress"], | ||
"homepage": "https://github.com/wonderwp/API", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Jeremy Desvaux", | ||
"homepage": "https://jdmweb.com" | ||
}, | ||
"require-dev": { | ||
"johnpbloch/wordpress": "^4.7" | ||
}, | ||
"autoload": { | ||
"psr-4": {"WonderWp\\Component\\API\\": "src"}, | ||
"exclude-from-classmap": [ | ||
"/tests/" | ||
] | ||
}, | ||
"minimum-stability": "dev" | ||
{ | ||
"name": "WonderWp Community", | ||
"homepage": "http://wonderwp.net/Contribution/Contributors.html" | ||
} | ||
], | ||
"require": { | ||
"php": ">=7.1", | ||
"wonderwp/service": "^1.0", | ||
"wonderwp/http-foundation": "^1.0", | ||
"doctrine/annotations": "^1.12" | ||
}, | ||
"require-dev": { | ||
"johnpbloch/wordpress": "^4.7" | ||
}, | ||
"autoload": { | ||
"psr-4": {"WonderWp\\Component\\API\\": "src"}, | ||
"exclude-from-classmap": [ | ||
"/tests/" | ||
] | ||
}, | ||
"minimum-stability": "dev" | ||
} |
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