Skip to content

Commit

Permalink
Merge branch 'release/1.2.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
jdm-web committed Aug 20, 2021
2 parents 7ccab37 + 94c761f commit 7204cb0
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 130 deletions.
103 changes: 4 additions & 99 deletions README.md
@@ -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).
62 changes: 31 additions & 31 deletions composer.json
@@ -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"
}
9 changes: 9 additions & 0 deletions src/AbstractApiService.php
Expand Up @@ -122,6 +122,7 @@ protected function registerWPApiEndpoint(WPApiNamespace $apiNamespaceAnnotation,
$handler = [$this, $method];

$computedArgs = $this->recursiveBuildCallable($apiEndpointAnnotation->args);
$computedArgs = $this->addRequiredArguments($computedArgs);
$computedArgs['callback'] = $handler;

register_rest_route($namespaceAndVersion, $apiEndpointAnnotation->url, $computedArgs);
Expand Down Expand Up @@ -158,4 +159,12 @@ private function recursiveBuildCallable($args = []) {

return $args;
}

private function addRequiredArguments($args = []) {
if (!isset($args['permission_callback'])) {
$args['permission_callback'] = '__return_true';
}

return $args;
}
}

0 comments on commit 7204cb0

Please sign in to comment.