1
|
|
|
<?php |
2
|
|
|
namespace NirjharLo\WP_Plugin_Framework\Src; |
3
|
|
|
|
4
|
|
|
use WP_REST_Controller as WP_REST_Controller; |
|
|
|
|
5
|
|
|
|
6
|
|
|
if ( ! defined( 'ABSPATH' ) ) exit; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Extending REST API framework of WordPress |
10
|
|
|
* |
11
|
|
|
* @author Nirjhar Lo |
12
|
|
|
* @package wp-plugin-framework |
13
|
|
|
*/ |
14
|
|
|
if ( ! class_exists( 'RestApi' ) ) { |
15
|
|
|
|
16
|
|
|
class RestApi extends WP_REST_Controller { |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* REST API routes |
21
|
|
|
* |
22
|
|
|
* @return Object |
23
|
|
|
*/ |
24
|
|
|
public function register_routes() { |
25
|
|
|
|
26
|
|
|
$version = '1'; |
27
|
|
|
$namespace = 'vendor/v' . $version; |
28
|
|
|
$base = 'route'; |
29
|
|
|
|
30
|
|
|
//Available options for methods are CREATABLE, READABLE, EDITABLE, DELETABLE |
31
|
|
|
register_rest_route( $namespace, '/' . $base, array( |
|
|
|
|
32
|
|
|
'methods' => WP_REST_Server::READABLE, |
|
|
|
|
33
|
|
|
'callback' => array( $this, 'callback' ), |
34
|
|
|
'permission_callback' => array( $this, 'permission' ), |
35
|
|
|
'args' => array('sample', 'list', 'of', 'args') |
36
|
|
|
)); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* The request handler |
42
|
|
|
* |
43
|
|
|
* @return Object |
44
|
|
|
*/ |
45
|
|
|
public function callback() { |
46
|
|
|
|
47
|
|
|
$params = $request->get_params(); |
|
|
|
|
48
|
|
|
$items = array(); |
49
|
|
|
$data = $this->prepare_item_for_response( $items, $request ); |
50
|
|
|
|
51
|
|
|
if ( $data ) { |
|
|
|
|
52
|
|
|
return new WP_REST_Response( $data, 200 ); |
|
|
|
|
53
|
|
|
} else { |
54
|
|
|
return new WP_Error( 'status_code', __( 'message', 'text-domain' ) ); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Prevent unauthorized access here |
61
|
|
|
* |
62
|
|
|
* @return Bool |
63
|
|
|
*/ |
64
|
|
|
public function permission() { |
65
|
|
|
|
66
|
|
|
return current_user_can( 'manage_options' ); |
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Processing of request takes place here |
72
|
|
|
* |
73
|
|
|
* @param Array |
74
|
|
|
* @param Object |
75
|
|
|
* |
76
|
|
|
* @return Array |
77
|
|
|
*/ |
78
|
|
|
public function prepare_item_for_response($items, $request) { |
79
|
|
|
|
80
|
|
|
//Process the data in any way you like |
81
|
|
|
$data = compact($items, $request); |
82
|
|
|
return $data; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
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