1 | <?php |
||
19 | class Base { |
||
20 | /** |
||
21 | * Contains debug info we'll send back in the response headers |
||
22 | * @var array |
||
23 | */ |
||
24 | protected $_debug_info = array(); |
||
25 | |||
26 | /** |
||
27 | * Indicates whether or not the API is in debug mode |
||
28 | * @var boolean |
||
29 | */ |
||
30 | protected $_debug_mode = false; |
||
31 | |||
32 | /** |
||
33 | * Indicates the version that was requested |
||
34 | * @var string |
||
35 | */ |
||
36 | protected $_requested_version; |
||
37 | |||
38 | public function __construct() { |
||
39 | $this->_debug_mode = defined( 'EE_REST_API_DEBUG_MODE' ) ? EE_REST_API_DEBUG_MODE : false; |
||
40 | } |
||
41 | |||
42 | |||
43 | /** |
||
44 | * Sets the version the user requested |
||
45 | * @param string $version eg '4.8' |
||
46 | */ |
||
47 | public function set_requested_version( $version ) { |
||
48 | $this->_requested_version = $version; |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * Sets some debug info that we'll send back in headers |
||
53 | * @param string $key |
||
54 | * @param string|array $info |
||
55 | */ |
||
56 | protected function _set_debug_info( $key, $info ){ |
||
59 | |||
60 | |||
61 | |||
62 | /** |
||
63 | * Sends a response, but also makes sure to attach headers that |
||
64 | * are handy for debugging. |
||
65 | * Specifically, we assume folks will want to know what exactly was the DB query that got run, |
||
66 | * what exactly was the Models query that got run, what capabilities came into play, what fields were omitted from |
||
67 | * the response, others? |
||
68 | * |
||
69 | * @param array|\WP_Error|\Exception $response |
||
70 | * @return \WP_REST_Response |
||
71 | */ |
||
72 | public function send_response( $response ) { |
||
73 | if( $response instanceof \Exception ) { |
||
74 | $response = new \WP_Error( $response->getCode(), $response->getMessage() ); |
||
75 | } |
||
76 | if( $response instanceof \WP_Error ) { |
||
|
|||
77 | //we want to send a "normal"-looking WP error response, but we also |
||
78 | //want to add headers. It doesn't seem WP API 1.2 supports this. |
||
79 | //I'd like to use WP_JSON_Server::error_to_response() but its protected |
||
80 | //so here's most of it copy-and-pasted :P |
||
81 | $error_data = $response->get_error_data(); |
||
82 | if ( is_array( $error_data ) && isset( $error_data['status'] ) ) { |
||
83 | $status = $error_data['status']; |
||
84 | } else { |
||
85 | $status = 500; |
||
86 | } |
||
87 | |||
88 | $errors = array(); |
||
89 | foreach ( (array) $response->errors as $code => $messages ) { |
||
90 | foreach ( (array) $messages as $message ) { |
||
91 | $errors[] = array( |
||
92 | 'code' => $code, |
||
93 | 'message' => $message, |
||
94 | 'data' => $response->get_error_data( $code ) |
||
95 | ); |
||
96 | } |
||
97 | } |
||
98 | $data = isset( $errors[0] ) ? $errors[0] : array(); |
||
99 | if ( count( $errors ) > 1 ) { |
||
100 | // Remove the primary error. |
||
101 | array_shift( $errors ); |
||
102 | $data['additional_errors'] = $errors; |
||
103 | } |
||
104 | $rest_response = new \WP_REST_Response( $data, $status ); |
||
105 | }else{ |
||
106 | $rest_response = new \WP_REST_Response( $response, 200 ); |
||
107 | } |
||
108 | $headers = array(); |
||
109 | if( $this->_debug_mode && is_array( $this->_debug_info ) ) { |
||
110 | foreach( $this->_debug_info as $debug_key => $debug_info ) { |
||
111 | if( is_array( $debug_info ) ) { |
||
112 | $debug_info = json_encode( $debug_info ); |
||
113 | } |
||
114 | $headers[ 'X-EE4-Debug-' . ucwords( $debug_key ) ] = $debug_info; |
||
115 | } |
||
116 | } |
||
117 | $rest_response->set_headers( $headers ); |
||
118 | return $rest_response; |
||
119 | } |
||
120 | |||
121 | |||
122 | |||
123 | /** |
||
124 | * Applies the regex to the route, then creates an array using the values of |
||
125 | * $match_keys as keys (but ignores the full pattern match). Returns the array of matches. |
||
126 | * For example, if you call |
||
127 | * parse_route( '/ee/v4.8/events', '~\/ee\/v([^/]*)\/(.*)~', array( 'version', 'model' ) ) |
||
128 | * it will return array( 'version' => '4.8', 'model' => 'events' ) |
||
129 | * |
||
130 | * @param string $route |
||
131 | * @param string $regex |
||
132 | * @param array $match_keys EXCLUDING matching the entire regex |
||
133 | * @return array where $match_keys are the keys (the first value of $match_keys |
||
134 | * becomes the first key of the return value, etc. Eg passing in $match_keys of |
||
135 | * array( 'model', 'id' ), will, if the regex is successful, will return |
||
136 | * array( 'model' => 'foo', 'id' => 'bar' ) |
||
137 | * @throws \EE_Error if it couldn't be parsed |
||
138 | */ |
||
139 | public function parse_route( $route, $regex, $match_keys ) { |
||
161 | } |
||
162 | |||
163 | // End of file Base.php |
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.