本文整理汇总了PHP中WP_REST_Request::get_body方法的典型用法代码示例。如果您正苦于以下问题:PHP WP_REST_Request::get_body方法的具体用法?PHP WP_REST_Request::get_body怎么用?PHP WP_REST_Request::get_body使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类WP_REST_Request
的用法示例。
在下文中一共展示了WP_REST_Request::get_body方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: update_items
/**
*
* @param WP_REST_Request $request - See WC_Connect_API_Client::get_label_rates()
* @return array|WP_Error
*/
public function update_items($request)
{
$request_body = $request->get_body();
$payload = json_decode($request_body, true, WOOCOMMERCE_CONNECT_MAX_JSON_DECODE_DEPTH);
// Hardcode USPS rates for now
$payload['carrier'] = 'usps';
$response = $this->api_client->get_label_rates($payload);
if (is_wp_error($response)) {
$error = new WP_Error($response->get_error_code(), $response->get_error_message(), array('message' => $response->get_error_message()));
$this->logger->log($error, __CLASS__);
return $error;
}
return array('success' => true, 'rates' => property_exists($response, 'rates') ? $response->rates : new stdClass());
}
开发者ID:Automattic,项目名称:woocommerce-connect-client,代码行数:19,代码来源:class-wc-rest-connect-shipping-rates-controller.php
示例2: update_item
/**
* Update one item from the collection
*
* @param \WP_REST_Request $request Full data about the request.
*
* @return \WP_Error|\WP_REST_Request
*/
public function update_item($request)
{
$params = $request->get_params();
$color = $params[0];
if (!array_key_exists($color, self::$lights)) {
return new \WP_Error('cant-update', __('message', 'particle-api'), array('status' => 500));
}
$json = json_decode($request->get_body());
$status = isset($json->status) ? $json->status : null;
$string_status = $status ? 'true' : 'false';
$data = new \stdClass();
update_option(self::$lights[$color], $string_status);
$data->status = $status;
if (is_object($data)) {
return new \WP_REST_Response($data, 200);
}
return new \WP_Error('cant-update', __('message', 'particle-api'), array('status' => 500));
}
示例3: update_item
/**
* Update one item from the collection
*
* @param \WP_REST_Request $request Full data about the request.
*
* @return \WP_Error|\WP_REST_Request
*/
public function update_item($request)
{
//$item = $this->prepare_item_for_database( $request );
$params = $request->get_params();
$id = intval($params[0]);
if (!array_key_exists($id, self::$switches)) {
return new \WP_Error('cant-update', __('message', 'particle-api'), array('status' => 500));
}
$json = json_decode($request->get_body());
$status = isset($json->status) ? $json->status : null;
$data = new \stdClass();
update_option(self::$switches[$id], $status);
$data->status = $status;
if (is_object($data)) {
return new \WP_REST_Response($data, 200);
}
return new \WP_Error('cant-update', __('message', 'particle-api'), array('status' => 500));
}
示例4: 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;
}
示例5: create_item
/**
* Creates a single attachment.
*
* @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, WP_Error object on failure.
*/
public function create_item($request)
{
if (!empty($request['post']) && in_array(get_post_type($request['post']), array('revision', 'attachment'), true)) {
return new WP_Error('rest_invalid_param', __('Invalid parent type.'), array('status' => 400));
}
// Get the file via $_FILES or raw data.
$files = $request->get_file_params();
$headers = $request->get_headers();
if (!empty($files)) {
$file = $this->upload_from_file($files, $headers);
} else {
$file = $this->upload_from_data($request->get_body(), $headers);
}
if (is_wp_error($file)) {
return $file;
}
$name = basename($file['file']);
$name_parts = pathinfo($name);
$name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
$url = $file['url'];
$type = $file['type'];
$file = $file['file'];
// use image exif/iptc data for title and caption defaults if possible
$image_meta = @wp_read_image_metadata($file);
if (!empty($image_meta)) {
if (empty($request['title']) && trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
$request['title'] = $image_meta['title'];
}
if (empty($request['caption']) && trim($image_meta['caption'])) {
$request['caption'] = $image_meta['caption'];
}
}
$attachment = $this->prepare_item_for_database($request);
$attachment->file = $file;
$attachment->post_mime_type = $type;
$attachment->guid = $url;
if (empty($attachment->post_title)) {
$attachment->post_title = preg_replace('/\\.[^.]+$/', '', basename($file));
}
$id = wp_insert_post(wp_slash((array) $attachment), true);
if (is_wp_error($id)) {
if ('db_update_error' === $id->get_error_code()) {
$id->add_data(array('status' => 500));
} else {
$id->add_data(array('status' => 400));
}
return $id;
}
$attachment = get_post($id);
/**
* Fires after a single attachment is created or updated via the REST API.
*
* @since 4.7.0
*
* @param WP_Post $attachment Inserted or updated attachment
* object.
* @param WP_REST_Request $request The request sent to the API.
* @param bool $creating True when creating an attachment, false when updating.
*/
do_action('rest_insert_attachment', $attachment, $request, true);
// Include admin functions to get access to wp_generate_attachment_metadata().
require_once ABSPATH . 'wp-admin/includes/admin.php';
wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
if (isset($request['alt_text'])) {
update_post_meta($id, '_wp_attachment_image_alt', sanitize_text_field($request['alt_text']));
}
$fields_update = $this->update_additional_fields_for_object($attachment, $request);
if (is_wp_error($fields_update)) {
return $fields_update;
}
$request->set_param('context', 'edit');
$response = $this->prepare_item_for_response($attachment, $request);
$response = rest_ensure_response($response);
$response->set_status(201);
$response->header('Location', rest_url(sprintf('%s/%s/%d', $this->namespace, $this->rest_base, $id)));
return $response;
}
示例6: prepare_item_for_database
/**
* Prepare the item for create or update operation
*
* @param WP_REST_Request $request Request object
* @since 7.0
* @return WP_Error|object $prepared_item
*/
protected function prepare_item_for_database($request)
{
$body = $request->get_body();
if (!empty($body)) {
$body = json_decode($body, true);
$raw_title = !empty($body['title']) && !empty($body['title']['raw']) ? $body['title']['raw'] : '';
$body['title'] = $raw_title;
return $body;
}
return false;
}
示例7: create_item
/**
* Create a single attachment
*
* @param WP_REST_Request $request Full details about the request
* @return WP_Error|WP_REST_Response
*/
public function create_item($request)
{
// Permissions check - Note: "upload_files" cap is returned for an attachment by $post_type_obj->cap->create_posts
$post_type_obj = get_post_type_object($this->post_type);
if (!current_user_can($post_type_obj->cap->create_posts) || !current_user_can($post_type_obj->cap->edit_posts)) {
return new WP_Error('rest_cannot_create', __('Sorry, you are not allowed to post on this site.'), array('status' => 400));
}
// If a user is trying to attach to a post make sure they have permissions. Bail early if post_id is not being passed
if (!empty($request['post'])) {
$parent = get_post((int) $request['post']);
$post_parent_type = get_post_type_object($parent->post_type);
if (!current_user_can($post_parent_type->cap->edit_post, $request['post'])) {
return new WP_Error('rest_cannot_edit', __('Sorry, you are not allowed to edit this post.'), array('status' => 401));
}
}
// Get the file via $_FILES or raw data
$files = $request->get_file_params();
$headers = $request->get_headers();
if (!empty($files)) {
$file = $this->upload_from_file($files, $headers);
} else {
$file = $this->upload_from_data($request->get_body(), $headers);
}
if (is_wp_error($file)) {
return $file;
}
$name = basename($file['file']);
$name_parts = pathinfo($name);
$name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
$url = $file['url'];
$type = $file['type'];
$file = $file['file'];
$title = $name;
$caption = '';
// use image exif/iptc data for title and caption defaults if possible
// @codingStandardsIgnoreStart
$image_meta = @wp_read_image_metadata($file);
// @codingStandardsIgnoreEnd
if (!empty($image_meta)) {
if (empty($request['title']) && trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
$title = $image_meta['title'];
}
if (empty($request['caption']) && trim($image_meta['caption'])) {
$caption = $image_meta['caption'];
}
}
$attachment = $this->prepare_item_for_database($request);
$attachment->file = $file;
$attachment->post_mime_type = $type;
$attachment->guid = $url;
$id = wp_insert_post($attachment, true);
if (is_wp_error($id)) {
return $id;
}
/** Include admin functions to get access to wp_generate_attachment_metadata() */
require_once ABSPATH . 'wp-admin/includes/admin.php';
wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
if (isset($request['alt_text'])) {
update_post_meta($id, '_wp_attachment_image_alt', sanitize_text_field($request['alt_text']));
}
$this->update_additional_fields_for_object($attachment, $request);
$response = $this->get_item(array('id' => $id, 'context' => 'edit'));
$response = rest_ensure_response($response);
$response->set_status(201);
$response->header('Location', rest_url('/wp/v2/' . $this->get_post_type_base($attachment->post_type) . '/' . $id));
/**
* Fires after a single attachment is created or updated via the REST API.
*
* @param object $attachment Inserted attachment.
* @param WP_REST_Request $request The request sent to the API.
* @param bool $creating True when creating an attachment, false when updating.
*/
do_action('rest_insert_attachment', $attachment, $request, true);
return $response;
}
示例8: update_unit
public function update_unit(WP_REST_Request $request)
{
$id = (int) $request['id'];
$unit = $this->get_unit(array("id" => $id), false);
if (empty($id) || empty($unit->id)) {
return new WP_Error('error', __('unit not found 3 '), array('status' => 404));
}
// why do I have to do this??
$data = json_decode($request->get_body(), true);
if (!empty($data['unit'])) {
$data = $data["unit"];
}
// $data["parent"] = $data["parent_id"];
// unset($data["parent_id"]);
// $data["type"] = $data["type_id"];
// unset($data["type_id"]);
/*
if ( isset( $_headers['IF_UNMODIFIED_SINCE'] ) ) {
// As mandated by RFC2616, we have to check all of RFC1123, RFC1036
// and C's asctime() format (and ignore invalid headers)
$formats = array( DateTime::RFC1123, DateTime::RFC1036, 'D M j H:i:s Y' );
foreach ( $formats as $format ) {
$check = WP_JSON_DateTime::createFromFormat( $format, $_headers['IF_UNMODIFIED_SINCE'] );
if ( $check !== false ) {
break;
}
}
// If the post has been modified since the date provided, return an error.
if ( $check && mysql2date( 'U', $post['post_modified_gmt'] ) > $check->format('U') ) {
return new WP_Error( 'json_old_revision', __( 'There is a revision of this post that is more recent.' ), array( 'status' => 412 ) );
}
}
*/
global $wpdb;
$table_name = $wpdb->prefix . "ot_unit";
$res = $wpdb->update($table_name, $data, array('id' => $id));
if (false !== $res) {
return $this->get_unit(array("id" => $id));
} else {
return new WP_Error('error', __('update unit error ' . $res->last_error), array('status' => 404));
}
}
示例9: create_item
/**
* Create a single attachment
*
* @param WP_REST_Request $request Full details about the request
* @return WP_Error|WP_REST_Response
*/
public function create_item($request)
{
// Get the file via $_FILES or raw data
$files = $request->get_file_params();
$headers = $request->get_headers();
if (!empty($files)) {
$file = $this->upload_from_file($files, $headers);
} else {
$file = $this->upload_from_data($request->get_body(), $headers);
}
if (is_wp_error($file)) {
return $file;
}
$name = basename($file['file']);
$name_parts = pathinfo($name);
$name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
$url = $file['url'];
$type = $file['type'];
$file = $file['file'];
// use image exif/iptc data for title and caption defaults if possible
// @codingStandardsIgnoreStart
$image_meta = @wp_read_image_metadata($file);
// @codingStandardsIgnoreEnd
if (!empty($image_meta)) {
if (empty($request['title']) && trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
$request['title'] = $image_meta['title'];
}
if (empty($request['caption']) && trim($image_meta['caption'])) {
$request['caption'] = $image_meta['caption'];
}
}
$attachment = $this->prepare_item_for_database($request);
$attachment->file = $file;
$attachment->post_mime_type = $type;
$attachment->guid = $url;
$id = wp_insert_post($attachment, true);
if (is_wp_error($id)) {
if (in_array($id->get_error_code(), array('db_update_error'))) {
$id->add_data(array('status' => 500));
} else {
$id->add_data(array('status' => 400));
}
return $id;
}
$attachment = get_post($id);
/** Include admin functions to get access to wp_generate_attachment_metadata() */
require_once ABSPATH . 'wp-admin/includes/admin.php';
wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
if (isset($request['alt_text'])) {
update_post_meta($id, '_wp_attachment_image_alt', sanitize_text_field($request['alt_text']));
}
$this->update_additional_fields_for_object($attachment, $request);
$request->set_param('context', 'edit');
$response = $this->prepare_item_for_response($attachment, $request);
$response = rest_ensure_response($response);
$response->set_status(201);
$response->header('Location', rest_url('/wp/v2/' . $this->get_post_type_base($attachment->post_type) . '/' . $id));
/**
* Fires after a single attachment is created or updated via the REST API.
*
* @param object $attachment Inserted attachment.
* @param WP_REST_Request $request The request sent to the API.
* @param boolean $creating True when creating an attachment, false when updating.
*/
do_action('rest_insert_attachment', $attachment, $request, true);
return $response;
}