This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | /** |
||
4 | * Class PodsRESTHandlers |
||
5 | * |
||
6 | * Handlers for reading and writing Pods fields via REST API |
||
7 | * |
||
8 | * @package Pods |
||
9 | * @since 2.5.6 |
||
10 | */ |
||
11 | class PodsRESTHandlers { |
||
12 | |||
13 | /** |
||
14 | * Holds a Pods object to avoid extra DB queries |
||
15 | * |
||
16 | * @since 2.5.6 |
||
17 | * |
||
18 | * @var Pods |
||
19 | */ |
||
20 | private static $pod; |
||
21 | |||
22 | /** |
||
23 | * Get Pod object |
||
24 | * |
||
25 | * @since 2.5.6 |
||
26 | * |
||
27 | * @param $pod_name |
||
28 | * @param $id |
||
29 | * |
||
30 | * @return bool|Pods |
||
0 ignored issues
–
show
|
|||
31 | */ |
||
32 | protected static function get_pod( $pod_name, $id ) { |
||
33 | |||
34 | if ( ! self::$pod || self::$pod->pod !== $pod_name ) { |
||
35 | self::$pod = pods( $pod_name, $id, true ); |
||
36 | } |
||
37 | |||
38 | if ( self::$pod && (int) self::$pod->id !== (int) $id ) { |
||
39 | self::$pod->fetch( $id ); |
||
40 | } |
||
41 | |||
42 | return self::$pod; |
||
43 | |||
44 | } |
||
45 | |||
46 | /** |
||
47 | * Handler for getting custom field data. |
||
48 | * |
||
49 | * @since 2.5.6 |
||
50 | * |
||
51 | * @param array $object The object from the response |
||
52 | * @param string $field_name Name of field |
||
53 | * @param WP_REST_Request $request Current request |
||
54 | * @param string $object_type Type of object |
||
55 | * |
||
56 | * @return mixed |
||
57 | */ |
||
58 | public static function get_handler( $object, $field_name, $request, $object_type ) { |
||
59 | |||
60 | $pod_name = pods_v( 'type', $object ); |
||
61 | |||
62 | /** |
||
63 | * If $pod_name in the line above is empty then the route invoked |
||
64 | * may be for a taxonomy, so lets try and check for that |
||
65 | */ |
||
66 | if ( empty( $pod_name ) ) { |
||
67 | $pod_name = pods_v( 'taxonomy', $object ); |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * $pod_name is still empty, so check lets check $object_type |
||
72 | */ |
||
73 | |||
74 | if ( empty( $pod_name ) ) { |
||
75 | if ( 'attachment' === $object_type ) { |
||
76 | $pod_name = 'media'; |
||
77 | } else { |
||
78 | $pod_name = $object_type; |
||
79 | } |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * Filter the pod name |
||
84 | * |
||
85 | * @since 2.6.7 |
||
86 | * |
||
87 | * @param array $pod_name Pod name |
||
88 | * @param Pods $object Rest object |
||
89 | * @param string $field_name Name of the field |
||
90 | * @param WP_REST_Request $request Current request |
||
91 | * @param string $object_type Rest Object type |
||
92 | */ |
||
93 | $pod_name = apply_filters( 'pods_rest_api_pod_name', $pod_name, $object, $field_name, $request, $object_type ); |
||
94 | |||
95 | $id = pods_v( 'id', $object ); |
||
96 | |||
97 | if ( empty( $id ) ) { |
||
98 | $id = pods_v( 'ID', $object ); |
||
99 | } |
||
100 | |||
101 | $pod = self::get_pod( $pod_name, $id ); |
||
102 | |||
103 | $value = false; |
||
104 | |||
105 | if ( $pod && PodsRESTFields::field_allowed_to_extend( $field_name, $pod, 'read' ) ) { |
||
106 | $params = null; |
||
107 | |||
108 | $field_data = $pod->fields( $field_name ); |
||
109 | |||
110 | if ( 'pick' === pods_v( 'type', $field_data ) ) { |
||
111 | $output_type = pods_v( 'rest_pick_response', $field_data['options'], 'array' ); |
||
112 | |||
113 | /** |
||
114 | * What output type to use for a related field REST response. |
||
115 | * |
||
116 | * @since 2.7 |
||
117 | * |
||
118 | * @param string $output_type The pick response output type. |
||
119 | * @param string $field_name The name of the field |
||
120 | * @param array $field_data The field data |
||
121 | * @param object|Pods $pod The Pods object for Pod relationship is from. |
||
122 | * @param int $id Current item ID |
||
123 | * @param object|WP_REST_Request Current request object. |
||
124 | */ |
||
125 | $output_type = apply_filters( 'pods_rest_api_output_type_for_relationship_response', $output_type, $field_name, $field_data, $pod, $id, $request ); |
||
126 | |||
127 | if ( 'array' === $output_type ) { |
||
128 | $related_pod_items = $pod->field( $field_name, array( 'output' => 'pod' ) ); |
||
129 | |||
130 | if ( $related_pod_items ) { |
||
131 | $fields = false; |
||
132 | $items = array(); |
||
133 | $depth = pods_v( 'rest_pick_depth', $field_data['options'], 2 ); |
||
134 | |||
135 | if ( ! is_array( $related_pod_items ) ) { |
||
136 | $related_pod_items = array( $related_pod_items ); |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * @var $related_pod Pods |
||
141 | */ |
||
142 | foreach ( $related_pod_items as $related_pod ) { |
||
143 | if ( ! is_object( $related_pod ) || ! is_a( $related_pod, 'Pods' ) ) { |
||
144 | $items = $related_pod_items; |
||
145 | |||
146 | break; |
||
147 | } |
||
148 | |||
149 | if ( false === $fields ) { |
||
150 | $fields = $related_pod->fields(); |
||
151 | $fields = array_keys( $fields ); |
||
152 | |||
153 | if ( isset( $related_pod->pod_data['object_fields'] ) && ! empty( $related_pod->pod_data['object_fields'] ) ) { |
||
154 | $fields = array_merge( $fields, array_keys( $related_pod->pod_data['object_fields'] ) ); |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * What fields to show in a related field REST response. |
||
159 | * |
||
160 | * @since 0.0.1 |
||
161 | * |
||
162 | * @param array $fields The fields to show |
||
163 | * @param string $field_name The name of the field |
||
164 | * @param object|Pods $pod The Pods object for Pod relationship is from. |
||
165 | * @param object|Pods $pod The Pods object for Pod relationship is to. |
||
166 | * @param int $id Current item ID |
||
167 | * @param object|WP_REST_Request Current request object. |
||
168 | */ |
||
169 | $fields = apply_filters( 'pods_rest_api_fields_for_relationship_response', $fields, $field_name, $pod, $related_pod, $id, $request ); |
||
170 | }//end if |
||
171 | |||
172 | /** |
||
173 | * What depth to use for a related field REST response. |
||
174 | * |
||
175 | * @since 0.0.1 |
||
176 | * |
||
177 | * @param array $depth The depth. |
||
178 | * @param string $field_name The name of the field |
||
179 | * @param object|Pods $pod The Pods object for Pod relationship is from. |
||
180 | * @param object|Pods $pod The Pods object for Pod relationship is to. |
||
181 | * @param int $id Current item ID |
||
182 | * @param object|WP_REST_Request Current request object. |
||
183 | */ |
||
184 | $depth = apply_filters( 'pods_rest_api_depth_for_relationship_response', $depth, $field_name, $pod, $related_pod, $id, $request ); |
||
185 | |||
186 | $params = array( |
||
187 | 'fields' => $fields, |
||
188 | 'depth' => $depth, |
||
189 | 'context' => 'rest', |
||
190 | ); |
||
191 | |||
192 | $items[] = $related_pod->export( $params ); |
||
193 | }//end foreach |
||
194 | |||
195 | $value = $items; |
||
196 | }//end if |
||
197 | }//end if |
||
198 | |||
199 | $params = array( |
||
200 | 'output' => $output_type, |
||
201 | ); |
||
202 | }//end if |
||
203 | |||
204 | // If no value set yet, get normal field value |
||
205 | if ( ! $value && ! is_array( $value ) ) { |
||
206 | $value = $pod->field( $field_name, $params ); |
||
207 | } |
||
208 | }//end if |
||
209 | |||
210 | return $value; |
||
211 | |||
212 | } |
||
213 | |||
214 | /** |
||
215 | * Handle saving of Pod fields from REST API write requests. |
||
216 | * |
||
217 | * @param WP_Post|WP_Term|WP_User|WP_Comment $object Inserted or updated object. |
||
218 | * @param WP_REST_Request $request Request object. |
||
219 | * @param bool $creating True when creating an item, false when updating. |
||
220 | */ |
||
221 | public static function save_handler( $object, $request, $creating ) { |
||
222 | |||
223 | if ( is_a( $object, 'WP_Post' ) ) { |
||
224 | $pod_name = $object->post_type; |
||
225 | |||
226 | if ( 'attachment' === $pod_name ) { |
||
227 | $pod_name = 'media'; |
||
228 | } |
||
229 | |||
230 | $id = $object->ID; |
||
231 | } elseif ( is_a( $object, 'WP_Term' ) ) { |
||
232 | $pod_name = $object->taxonomy; |
||
233 | |||
234 | $id = $object->term_id; |
||
235 | } elseif ( is_a( $object, 'WP_User' ) ) { |
||
236 | $pod_name = 'user'; |
||
237 | |||
238 | $id = $object->ID; |
||
239 | } elseif ( is_a( $object, 'WP_Comment' ) ) { |
||
240 | $pod_name = 'comment'; |
||
241 | |||
242 | $id = $object->comment_ID; |
||
243 | } else { |
||
244 | // Not a supported object |
||
245 | return; |
||
246 | }//end if |
||
247 | |||
248 | $pod = self::get_pod( $pod_name, $id ); |
||
249 | |||
250 | global $wp_rest_additional_fields; |
||
0 ignored issues
–
show
Compatibility
Best Practice
introduced
by
Use of
global functionality is not recommended; it makes your code harder to test, and less reusable.
Instead of relying on 1. Pass all data via parametersfunction myFunction($a, $b) {
// Do something
}
2. Create a class that maintains your stateclass MyClass {
private $a;
private $b;
public function __construct($a, $b) {
$this->a = $a;
$this->b = $b;
}
public function myFunction() {
// Do something
}
}
![]() |
|||
251 | |||
252 | $rest_enable = (boolean) pods_v( 'rest_enable', $pod->pod_data['options'], false ); |
||
253 | |||
254 | if ( $pod && $rest_enable && ! empty( $wp_rest_additional_fields[ $pod_name ] ) ) { |
||
255 | $fields = $pod->fields(); |
||
256 | |||
257 | $save_fields = array(); |
||
258 | |||
259 | $params = array( |
||
260 | 'is_new_item' => $creating, |
||
261 | ); |
||
262 | |||
263 | foreach ( $fields as $field_name => $field ) { |
||
264 | if ( empty( $wp_rest_additional_fields[ $pod_name ][ $field_name ]['pods_update'] ) ) { |
||
265 | continue; |
||
266 | } elseif ( ! isset( $request[ $field_name ] ) ) { |
||
267 | continue; |
||
268 | } elseif ( ! PodsRESTFields::field_allowed_to_extend( $field_name, $pod, 'write' ) ) { |
||
269 | continue; |
||
270 | } |
||
271 | |||
272 | $save_fields[ $field_name ] = $request[ $field_name ]; |
||
273 | } |
||
274 | |||
275 | if ( ! empty( $save_fields ) || $creating ) { |
||
276 | $pod->save( $save_fields, null, null, $params ); |
||
277 | } |
||
278 | }//end if |
||
279 | |||
280 | } |
||
281 | |||
282 | } |
||
283 |
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.