|
1
|
|
|
<?php |
|
2
|
|
|
if ( ! defined( 'ABSPATH' ) ) exit; |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* Extending REST API framework of WordPress |
|
6
|
|
|
* |
|
7
|
|
|
* @author Nirjhar Lo |
|
8
|
|
|
* @package wp-plugin-framework |
|
9
|
|
|
*/ |
|
10
|
|
|
if ( ! class_exists( 'PLUGIN_CUSTOM_ROUTE' ) ) { |
|
11
|
|
|
|
|
12
|
|
|
class PLUGIN_CUSTOM_ROUTE extends WP_REST_Controller { |
|
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* REST API routes |
|
17
|
|
|
* |
|
18
|
|
|
* @return Object |
|
19
|
|
|
*/ |
|
20
|
|
|
public function register_routes() { |
|
21
|
|
|
|
|
22
|
|
|
$version = '1'; |
|
23
|
|
|
$namespace = 'vendor/v' . $version; |
|
24
|
|
|
$base = 'route'; |
|
25
|
|
|
|
|
26
|
|
|
//Available options for methods are CREATABLE, READABLE, EDITABLE, DELETABLE |
|
27
|
|
|
register_rest_route( $namespace, '/' . $base, array( |
|
|
|
|
|
|
28
|
|
|
'methods' => WP_REST_Server::READABLE, |
|
|
|
|
|
|
29
|
|
|
'callback' => array( $this, 'callback' ), |
|
30
|
|
|
'permission_callback' => array( $this, 'permission' ), |
|
31
|
|
|
'args' => array('sample', 'list', 'of', 'args') |
|
32
|
|
|
)); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* The request handler |
|
38
|
|
|
* |
|
39
|
|
|
* @return Object |
|
40
|
|
|
*/ |
|
41
|
|
|
public function callback() { |
|
42
|
|
|
|
|
43
|
|
|
$params = $request->get_params(); |
|
|
|
|
|
|
44
|
|
|
$items = array(); |
|
45
|
|
|
$data = $this->prepare_item_for_response( $items, $request ); |
|
46
|
|
|
|
|
47
|
|
|
if ( $data ) { |
|
|
|
|
|
|
48
|
|
|
return new WP_REST_Response( $data, 200 ); |
|
|
|
|
|
|
49
|
|
|
} else { |
|
50
|
|
|
return new WP_Error( 'status_code', __( 'message', 'text-domain' ) ); |
|
|
|
|
|
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Prevent unauthorized access here |
|
57
|
|
|
* |
|
58
|
|
|
* @return Bool |
|
59
|
|
|
*/ |
|
60
|
|
|
public function permission() { |
|
61
|
|
|
|
|
62
|
|
|
return current_user_can( 'manage_options' ); |
|
|
|
|
|
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Processing of request takes place here |
|
68
|
|
|
* |
|
69
|
|
|
* @param Array |
|
70
|
|
|
* @param Object |
|
71
|
|
|
* |
|
72
|
|
|
* @return Array |
|
73
|
|
|
*/ |
|
74
|
|
|
public function prepare_item_for_response($items, $request) { |
|
75
|
|
|
|
|
76
|
|
|
//Process the data in any way you like |
|
77
|
|
|
$data = compact($items, $request); |
|
78
|
|
|
return $data; |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths