本文整理汇总了PHP中WP_REST_Response类的典型用法代码示例。如果您正苦于以下问题:PHP WP_REST_Response类的具体用法?PHP WP_REST_Response怎么用?PHP WP_REST_Response使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了WP_REST_Response类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: prepare_response_for_collection
/**
* Prepare a response for inserting into a collection.
*
* @param WP_REST_Response $response Response object.
* @return array Response data, ready for insertion into collection data.
*/
public function prepare_response_for_collection($response)
{
if (!$response instanceof WP_REST_Response) {
return $response;
}
$data = (array) $response->get_data();
$server = rest_get_server();
if (method_exists($server, 'get_compact_response_links')) {
$links = call_user_func(array($server, 'get_compact_response_links'), $response);
} else {
$links = call_user_func(array($server, 'get_response_links'), $response);
}
if (!empty($links)) {
$data['_links'] = $links;
}
return $data;
}
示例2: wpsms_list
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', '*'));
}
}
示例3: me_get_modules
/**
* 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;
}
示例4: get_item
/**
* get_item function.
*
* returns data about a BuddyPress site
*
* @access public
* @param mixed $request
* @return void
*/
public function get_item($request)
{
global $bp;
$core = array('version' => $bp->version, 'active_components' => $bp->active_components, 'directory_page_ids' => bp_core_get_directory_page_ids());
$core = apply_filters('core_api_data_filter', $core);
$response = new WP_REST_Response();
$response->set_data($core);
$response = rest_ensure_response($response);
return $response;
}
示例5: create_price
public function create_price($request)
{
$required_keys = array('ticket_price', 'price_band_id', 'ticket_type_id');
$meta = $request[$this->post_type . "_meta"] ? $request[$this->post_type . "_meta"] : array();
if (count(array_intersect_key(array_flip($required_keys), $meta)) >= count($required_keys)) {
return $this->create_item($request);
} else {
$error = new WP_REST_Response(array('message' => 'Insufficient parameters', 'data' => array('status' => 422)));
$error->set_status(422);
return $error;
}
}
示例6: filter_ee_metadata_into_index
/**
* Adds EE metadata to the index
* @param WP_REST_Response $rest_response_obj
* @return WP_REST_Response
*/
public static function filter_ee_metadata_into_index($rest_response_obj)
{
$response_data = $rest_response_obj->get_data();
$addons = array();
foreach (\EE_Registry::instance()->addons as $addon) {
$addon_json = array('name' => $addon->name(), 'version' => $addon->version());
$addons[$addon_json['name']] = $addon_json;
}
$response_data['ee'] = array('version' => \EEM_System_Status::instance()->get_ee_version(), 'addons' => $addons, 'maintenance_mode' => \EE_Maintenance_Mode::instance()->real_level(), 'served_core_versions' => array_keys(\EED_Core_Rest_Api::versions_served()));
$rest_response_obj->set_data($response_data);
return $rest_response_obj;
}
示例7: create_event_instance
public function create_event_instance($request)
{
$event_id = $request['event_id'];
$event_post = get_post($event_id);
if ($event_post) {
return $this->create_item($request);
} else {
$error = new WP_REST_Response(array('message' => 'Event not found'));
$error->set_status(404);
return $error;
}
}
示例8: filter_ee_metadata_into_index
/**
* Adds EE metadata to the index
* @param \WP_REST_Response $rest_response_obj
* @return \WP_REST_Response
*/
public static function filter_ee_metadata_into_index(\WP_REST_Response $rest_response_obj)
{
$response_data = $rest_response_obj->get_data();
$addons = array();
foreach (\EE_Registry::instance()->addons as $addon) {
$addon_json = array('name' => $addon->name(), 'version' => $addon->version());
$addons[$addon_json['name']] = $addon_json;
}
$response_data['ee'] = array('version' => \EEM_System_Status::instance()->get_ee_version(), 'documentation_url' => 'https://github.com/eventespresso/event-espresso-core/tree/master/docs/C--REST-API', 'addons' => $addons, 'maintenance_mode' => \EE_Maintenance_Mode::instance()->real_level(), 'served_core_versions' => array_keys(\EED_Core_Rest_Api::versions_served()));
$rest_response_obj->set_data($response_data);
return $rest_response_obj;
}
示例9: get_pins
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;
}
示例10: get_activity
/**
* get_activity function.
*
* @access public
* @param mixed $filter
* @return void
*/
public function get_activity($filter)
{
$args = $filter;
if (bp_has_activities($args)) {
while (bp_activities()) {
bp_the_activity();
$activity = array('avatar' => bp_core_fetch_avatar(array('html' => false, 'item_id' => bp_get_activity_id())), 'action' => bp_get_activity_action(), 'content' => bp_get_activity_content_body(), 'activity_id' => bp_get_activity_id(), 'activity_username' => bp_core_get_username(bp_get_activity_user_id()), 'user_id' => bp_get_activity_user_id(), 'comment_count' => bp_activity_get_comment_count(), 'can_comment' => bp_activity_can_comment(), 'can_favorite' => bp_activity_can_favorite(), 'is_favorite' => bp_get_activity_is_favorite(), 'can_delete' => bp_activity_user_can_delete());
$activity = apply_filters('bp_json_prepare_activity', $activity);
$activities[] = $activity;
}
$data = array('activity' => $activities, 'has_more_items' => bp_activity_has_more_items());
$data = apply_filters('bp_json_prepare_activities', $data);
} else {
return new WP_Error('bp_json_activity', __('No Activity Found.', 'buddypress'), array('status' => 200));
}
$response = new WP_REST_Response();
$response->set_data($data);
$response = rest_ensure_response($response);
return $response;
}
示例11: onGithubRequest
/**
* This method use for handler request from Github's webhook.
*
* Because of github not required to response payload on response, so we can make a do_action instead of apply_filters
*
* @param \WP_REST_Request $request
*
* @return \WP_REST_Response
*/
public function onGithubRequest(\WP_REST_Request $request)
{
// Create the response object
$request_payload = json_decode($request->get_body());
$response = new \WP_REST_Response();
if ($request_payload) {
ob_start();
$repo = new Repository($request_payload->repository->git_url);
if ($repo->exists()) {
$event = $request->get_header('X-Github-Event');
do_action('wppm_webhook_recived_' . $event, $repo, $request_payload);
} else {
$response->set_status(404);
echo "Repo is not exist.";
}
$out_put = ob_get_clean();
$response->set_data($out_put);
} else {
$response->set_status(500);
}
return $response;
}
示例12: delete_item
/**
* Deletes a comment.
*
* @since 4.7.0
* @access public
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|WP_REST_Response Response object on success, or error object on failure.
*/
public function delete_item($request)
{
$id = (int) $request['id'];
$force = isset($request['force']) ? (bool) $request['force'] : false;
$comment = get_comment($id);
if (empty($comment)) {
return new WP_Error('rest_comment_invalid_id', __('Invalid comment ID.'), array('status' => 404));
}
/**
* Filters whether a comment can be trashed.
*
* Return false to disable trash support for the post.
*
* @since 4.7.0
*
* @param bool $supports_trash Whether the post type support trashing.
* @param WP_Post $comment The comment object being considered for trashing support.
*/
$supports_trash = apply_filters('rest_comment_trashable', EMPTY_TRASH_DAYS > 0, $comment);
$request->set_param('context', 'edit');
if ($force) {
$previous = $this->prepare_item_for_response($comment, $request);
$result = wp_delete_comment($comment->comment_ID, true);
$response = new WP_REST_Response();
$response->set_data(array('deleted' => true, 'previous' => $previous->get_data()));
} else {
// If this type doesn't support trashing, error out.
if (!$supports_trash) {
return new WP_Error('rest_trash_not_supported', __('The comment does not support trashing. Set force=true to delete.'), array('status' => 501));
}
if ('trash' === $comment->comment_approved) {
return new WP_Error('rest_already_trashed', __('The comment has already been trashed.'), array('status' => 410));
}
$result = wp_trash_comment($comment->comment_ID);
$comment = get_comment($comment->comment_ID);
$response = $this->prepare_item_for_response($comment, $request);
}
if (!$result) {
return new WP_Error('rest_cannot_delete', __('The comment cannot be deleted.'), array('status' => 500));
}
/**
* Fires after a comment is deleted via the REST API.
*
* @since 4.7.0
*
* @param WP_Comment $comment The deleted comment data.
* @param WP_REST_Response $response The response returned from the API.
* @param WP_REST_Request $request The request sent to the API.
*/
do_action('rest_delete_comment', $comment, $response, $request);
return $response;
}
示例13: test_link_embedding_error
/**
* @depends test_link_embedding_params
*/
public function test_link_embedding_error()
{
// Register our testing route
$this->server->register_route('test', '/test/embeddable', array('methods' => 'GET', 'callback' => array($this, 'embedded_response_callback')));
$response = new WP_REST_Response();
$response->add_link('up', rest_url('/test/embeddable?error=1'), array('embeddable' => true));
$data = $this->server->response_to_data($response, true);
$this->assertArrayHasKey('_embedded', $data);
$this->assertArrayHasKey('up', $data['_embedded']);
// Check that errors are embedded correctly
$up = $data['_embedded']['up'];
$this->assertCount(1, $up);
$this->assertInstanceOf('WP_REST_Response', $up[0]);
$this->assertEquals(403, $up[0]->get_status());
$up_data = $up[0]->get_data();
$this->assertEquals('wp-api-test-error', $up_data[0]['code']);
$this->assertEquals('Test message', $up_data[0]['message']);
}
示例14: ingot_rest_response
/**
* 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;
}
}
示例15: send_response
/**
* Sends a response, but also makes sure to attach headers that
* are handy for debugging.
* Specifically, we assume folks will want to know what exactly was the DB query that got run,
* what exactly was the Models query that got run, what capabilities came into play, what fields were omitted from
* the response, others?
*
* @param array|\WP_Error|\Exception $response
* @return \WP_REST_Response
*/
public function send_response($response)
{
if ($response instanceof \Exception) {
$response = new \WP_Error($response->getCode(), $response->getMessage());
}
if ($response instanceof \WP_Error) {
$response = $this->_add_ee_errors_to_response($response);
$rest_response = $this->_create_rest_response_from_wp_error($response);
} else {
$rest_response = new \WP_REST_Response($response, 200);
}
$headers = array();
if ($this->_debug_mode && is_array($this->_debug_info)) {
foreach ($this->_debug_info as $debug_key => $debug_info) {
if (is_array($debug_info)) {
$debug_info = json_encode($debug_info);
}
$headers['X-EE4-Debug-' . ucwords($debug_key)] = $debug_info;
}
}
$headers = array_merge($headers, $this->_get_headers_from_ee_notices());
$rest_response->set_headers($headers);
return $rest_response;
}