function wpsms_list(WP_REST_Request $request) { $list_sms = array(); $item = array(); global $account_sid; global $auth_token; try { $client = new Services_Twilio($account_sid, $auth_token); foreach ($client->account->messages as $message) { $from = $message->from; $to = $message->to; $body = $message->body; $date_sent = $message->date_sent; array_push($item, array("from" => $from, "to" => $to, "body" => $body, "date_sent" => $date_sent)); } $list_sms = array_merge($list_sms, array("list" => $item)); $response = new WP_REST_Response($list_sms); $response->header('Access-Control-Allow-Origin', apply_filters('wpsms_access_control_allow_origin', '*')); return $response; } catch (Exception $ex) { $user_msgs = array("data" => 'ERROR:' . $ex->getMessage()); $response = new WP_REST_Response($user_msgs); $response->header('Access-Control-Allow-Origin', apply_filters('wpsms_access_control_allow_origin', '*')); } }
/** * Gets a list of modules for our API endpoint * * Callback for the GET method of the "modules" endpoint * * @since 0.1.1 * * */ function me_get_modules() { $return = Me_Utils::get_all_modules(); $response = new WP_REST_Response($return); $response->header('Access-Control-Allow-Origin', apply_filters('giar_access_control_allow_origin', '*')); return $response; }
public function test_envelope() { $data = array('amount of arbitrary data' => 'alot'); $status = 987; $headers = array('Arbitrary-Header' => 'value', 'Multiple' => 'maybe, yes'); $response = new WP_REST_Response($data, $status); $response->header('Arbitrary-Header', 'value'); // Check header concatenation as well. $response->header('Multiple', 'maybe'); $response->header('Multiple', 'yes', false); $envelope_response = $this->server->envelope_response($response, false); // The envelope should still be a response, but with defaults. $this->assertInstanceOf('WP_REST_Response', $envelope_response); $this->assertEquals(200, $envelope_response->get_status()); $this->assertEmpty($envelope_response->get_headers()); $this->assertEmpty($envelope_response->get_links()); $enveloped = $envelope_response->get_data(); $this->assertEquals($data, $enveloped['body']); $this->assertEquals($status, $enveloped['status']); $this->assertEquals($headers, $enveloped['headers']); }
public function get_pins() { if (0 || false === ($return = get_transient('rmaps_all_posts'))) { $query = apply_filters('rmaps_get_posts_query', array('numberposts' => 20, 'post_type' => 'pin', 'post_status' => 'publish')); $pins = get_posts($query); $return = array(); foreach ($pins as $pin) { $return[] = array('ID' => $pin->ID, 'title' => esc_attr($pin->post_title), 'infowindow' => wp_kses_post($pin->post_content), 'see_more' => _x('See More', 'Link', $this->textdomain), 'permalink' => esc_url(get_permalink($pin->ID)), 'lat' => esc_attr(get_post_meta($pin->ID, 'latitude', true)), 'lon' => esc_attr(get_post_meta($pin->ID, 'longitude', true))); } /** Cache the query for 3 minutes */ set_transient('rmaps_all_posts', $return, apply_filters('rmaps_posts_ttl', 60 * 3)); } $response = new \WP_REST_Response($return); $response->header('Access-Control-Allow-Origin', apply_filters('rmaps_access_control_allow_origin', '*')); return $response; }
/** * Sends the "Allow" header to state all methods that can be sent to the current route. * * @since 4.4.0 * * @param WP_REST_Response $response Current response being served. * @param WP_REST_Server $server ResponseHandler instance (usually WP_REST_Server). * @param WP_REST_Request $request The request that was used to make current response. * @return WP_REST_Response Response to be served, with "Allow" header if route has allowed methods. */ function rest_send_allow_header($response, $server, $request) { $matched_route = $response->get_matched_route(); if (!$matched_route) { return $response; } $routes = $server->get_routes(); $allowed_methods = array(); // Get the allowed methods across the routes. foreach ($routes[$matched_route] as $_handler) { foreach ($_handler['methods'] as $handler_method => $value) { if (!empty($_handler['permission_callback'])) { $permission = call_user_func($_handler['permission_callback'], $request); $allowed_methods[$handler_method] = true === $permission; } else { $allowed_methods[$handler_method] = true; } } } // Strip out all the methods that are not allowed (false values). $allowed_methods = array_filter($allowed_methods); if ($allowed_methods) { $response->header('Allow', implode(', ', array_map('strtoupper', array_keys($allowed_methods)))); } return $response; }
/** * If the consumer_key and consumer_secret $_GET parameters are NOT provided * and the Basic auth headers are either not present or the consumer secret does not match the consumer * key provided, then return the correct Basic headers and an error message. * * @param WP_REST_Response $response Current response being served. * @return WP_REST_Response */ public function send_unauthorized_headers($response) { global $wc_rest_authentication_error; if (is_wp_error($wc_rest_authentication_error) && is_ssl()) { $auth_message = __('WooCommerce API - Use a consumer key in the username field and a consumer secret in the password field.', 'woocommerce'); $response->header('WWW-Authenticate', 'Basic realm="' . $auth_message . '"', true); } return $response; }
/** * Sends a response to the API request. * * @since 1.4 * @param int $status_code Status code. * @return void */ public function output($status_code = 200) { $response = new WP_REST_Response(array('result' => true)); $response->set_status($status_code); $response->header('Content-type', 'application/json'); $response->set_data($this->data); echo wp_json_encode($response); die; }
/** * 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; } }