From c02dc5cb75dba8ce152ff6748df0e15f86407ce9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Hoste?= Date: Fri, 2 Apr 2021 09:24:51 +0200 Subject: [PATCH 1/4] [WPApi] add default value for required arguments --- src/AbstractApiService.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/AbstractApiService.php b/src/AbstractApiService.php index fad2fd0..6ae789b 100644 --- a/src/AbstractApiService.php +++ b/src/AbstractApiService.php @@ -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); @@ -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; + } } From 0d06af9e0974b3dae9e6307398682cd8fda14e92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Hoste?= Date: Thu, 19 Aug 2021 14:39:40 +0200 Subject: [PATCH 2/4] update --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 3e927a3..5f7366d 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,7 @@ } ], "require": { - "php": "~5.6|~7", + "php": ">=7.1", "wonderwp/service": "^1.0", "wonderwp/http-foundation": "^1.0", "doctrine/annotations": "^1.12" From 9d38d8339e25f61156f8804db81e1d61e2756c34 Mon Sep 17 00:00:00 2001 From: Jeremy Desvaux Date: Fri, 20 Aug 2021 11:52:16 +0200 Subject: [PATCH 3/4] Maj composer infos + bump minimal PHP version to 7.1 --- composer.json | 62 +++++++++++++++++++++++++-------------------------- 1 file changed, 31 insertions(+), 31 deletions(-) diff --git a/composer.json b/composer.json index 5f7366d..1f71011 100644 --- a/composer.json +++ b/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": ">=7.1", - "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" } From 94c761fb2804a79994b8a0c0532351d78c8a49b9 Mon Sep 17 00:00:00 2001 From: Jeremy Desvaux Date: Fri, 20 Aug 2021 11:52:28 +0200 Subject: [PATCH 4/4] Maj Readme --- README.md | 103 +++--------------------------------------------------- 1 file changed, 4 insertions(+), 99 deletions(-) diff --git a/README.md b/README.md index cca0ddc..357a9ed 100644 --- a/README.md +++ b/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\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\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).