WP_REST_Posts_Controller::update_item PHP Méthode

update_item() public méthode

Updates a single post.
Since: 4.7.0
public update_item ( WP_REST_Request $request ) : WP_REST_Response | WP_Error
$request WP_REST_Request Full details about the request.
Résultat WP_REST_Response | WP_Error Response object on success, or WP_Error object on failure.
    public function update_item($request)
    {
        $id = (int) $request['id'];
        $post = get_post($id);
        if (empty($id) || empty($post->ID) || $this->post_type !== $post->post_type) {
            return new WP_Error('rest_post_invalid_id', __('Invalid post ID.'), array('status' => 404));
        }
        $post = $this->prepare_item_for_database($request);
        if (is_wp_error($post)) {
            return $post;
        }
        // convert the post object to an array, otherwise wp_update_post will expect non-escaped input.
        $post_id = wp_update_post(wp_slash((array) $post), true);
        if (is_wp_error($post_id)) {
            if ('db_update_error' === $post_id->get_error_code()) {
                $post_id->add_data(array('status' => 500));
            } else {
                $post_id->add_data(array('status' => 400));
            }
            return $post_id;
        }
        $post = get_post($post_id);
        /* This action is documented in lib/endpoints/class-wp-rest-controller.php */
        do_action("rest_insert_{$this->post_type}", $post, $request, false);
        $schema = $this->get_item_schema();
        if (!empty($schema['properties']['format']) && !empty($request['format'])) {
            set_post_format($post, $request['format']);
        }
        if (!empty($schema['properties']['featured_media']) && isset($request['featured_media'])) {
            $this->handle_featured_media($request['featured_media'], $post_id);
        }
        if (!empty($schema['properties']['sticky']) && isset($request['sticky'])) {
            if (!empty($request['sticky'])) {
                stick_post($post_id);
            } else {
                unstick_post($post_id);
            }
        }
        if (!empty($schema['properties']['template']) && isset($request['template'])) {
            $this->handle_template($request['template'], $post->ID);
        }
        $terms_update = $this->handle_terms($post->ID, $request);
        if (is_wp_error($terms_update)) {
            return $terms_update;
        }
        if (!empty($schema['properties']['meta']) && isset($request['meta'])) {
            $meta_update = $this->meta->update_value($request['meta'], $post->ID);
            if (is_wp_error($meta_update)) {
                return $meta_update;
            }
        }
        $post = get_post($post_id);
        $fields_update = $this->update_additional_fields_for_object($post, $request);
        if (is_wp_error($fields_update)) {
            return $fields_update;
        }
        $request->set_param('context', 'edit');
        $response = $this->prepare_item_for_response($post, $request);
        return rest_ensure_response($response);
    }

Usage Example

 /**
  * Update a single post
  *
  * @param WP_REST_Request $request Full details about the request
  * @return WP_Error|WP_REST_Response
  */
 public function update_item($request)
 {
     $response = parent::update_item($request);
     if (is_wp_error($response)) {
         return $response;
     }
     $response = rest_ensure_response($response);
     $data = $response->get_data();
     if (isset($request['alt_text'])) {
         update_post_meta($data['id'], '_wp_attachment_image_alt', $request['alt_text']);
     }
     $response = $this->get_item(array('id' => $data['id'], 'context' => 'edit'));
     return rest_ensure_response($response);
 }
All Usage Examples Of WP_REST_Posts_Controller::update_item