WP_REST_Comments_Controller::update_item() – Updates a comment.

You appear to be a bot. Output may be restricted

Description

Updates a comment.

Usage

$WP_REST_Response|WP_Error = WP_REST_Comments_Controller::update_item( $request );

Parameters

$request
( WP_REST_Request ) required – Full details about the request.

Returns

WP_REST_Response|WP_Error Response object on success, or error object on failure.

Source

File name: wordpress/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php
Lines:

1 to 100 of 116
  public function update_item( $request ) {
    $comment = $this->WP_REST_Comments_Controller::get_comment( $request['id'] );
    if ( is_wp_error( $comment ) ) {
      return $comment;
    }

    $id = $comment->comment_ID;

    if ( isset( $request['type'] ) && get_comment_type( $id ) !== $request['type'] ) {
      return new WP_Error(
        'rest_comment_invalid_type',
        __( 'Sorry, you are not allowed to change the comment type.' ),
        array( 'status' => 404 )
      );
    }

    $prepared_args = $this->WP_REST_Comments_Controller::prepare_item_for_database( $request );

    if ( is_wp_error( $prepared_args ) ) {
      return $prepared_args;
    }

    if ( ! empty( $prepared_args['comment_post_ID'] ) ) {
      $post = get_post( $prepared_args['comment_post_ID'] );

      if ( empty( $post ) ) {
        return new WP_Error(
          'rest_comment_invalid_post_id',
          __( 'Invalid post ID.' ),
          array( 'status' => 403 )
        );
      }
    }

    if ( empty( $prepared_args ) && isset( $request['status'] ) ) {
      // Only the comment status is being changed.
      $change = $this->WP_REST_Comments_Controller::handle_status_param( $request['status'], $id );

      if ( ! $change ) {
        return new WP_Error(
          'rest_comment_failed_edit',
          __( 'Updating comment status failed.' ),
          array( 'status' => 500 )
        );
      }
    } elseif ( ! empty( $prepared_args ) ) {
      if ( is_wp_error( $prepared_args ) ) {
        return $prepared_args;
      }

      if ( isset( $prepared_args['comment_content'] ) && empty( $prepared_args['comment_content'] ) ) {
        return new WP_Error(
          'rest_comment_content_invalid',
          __( 'Invalid comment content.' ),
          array( 'status' => 400 )
        );
      }

      $prepared_args['comment_ID'] = $id;

      $check_comment_lengths = wp_check_comment_data_max_lengths( $prepared_args );

      if ( is_wp_error( $check_comment_lengths ) ) {
        $error_code = $check_comment_lengths->get_error_code();
        return new WP_Error(
          $error_code,
          __( 'Comment field exceeds maximum length allowed.' ),
          array( 'status' => 400 )
        );
      }

      $updated = wp_update_comment( wp_slash( (array) $prepared_args ), true );

      if ( is_wp_error( $updated ) ) {
        return new WP_Error(
          'rest_comment_failed_edit',
          __( 'Updating comment failed.' ),
          array( 'status' => 500 )
        );
      }

      if ( isset( $request['status'] ) ) {
        $this->WP_REST_Comments_Controller::handle_status_param( $request['status'], $id );
      }
    }

    $comment = get_comment( $id );

    
/** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php */
    do_action( 'rest_insert_comment', $comment, $request, false );

    $schema = $this->WP_REST_Comments_Controller::get_item_schema();

    if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) {
      $meta_update = $this->meta->update_value( $request['meta'], $id );

      if ( is_wp_error( $meta_update ) ) {
        return $meta_update;
      }
    }
 

 View on GitHub View on Trac