/** * Get groups * * @since 0.4.0 * * @param \WP_REST_Request $request Full data about the request. * @return \WP_Error|\WP_REST_Response */ public function get_items($request) { $args = array('page' => $request->get_param('page'), 'limit' => $request->get_param('limit'), 'type' => $request->get_param('type')); $groups = group::get_items($args); if (!empty($groups)) { foreach ($groups as $i => $group) { if (is_array($group)) { $groups[$i] = $this->prepare_group($group, $request->get_param('context')); } } } return ingot_rest_response($groups, 200, (int) group::total()); }
/** * Create a REST response * * @param array|object|\WP_Error $data Response data * @param int $code Optional. Status cod. Default is 200 * @param int|null $total Optional. if is an integer, will be used to set X-Ingot-Total header * * @return \WP_REST_Response|\WP_Error */ function ingot_rest_response($data, $code = 200, $total = null) { if (!is_wp_error($data)) { if (404 == $code || empty($data)) { $response = new \WP_REST_Response(null, 404); } else { $response = new \WP_REST_Response($data, $code); } if (0 < absint($total)) { $response->header('X-Ingot-Total', (int) \ingot\testing\crud\group::total()); } return $response; } else { return $data; } }