Parsed: 129882

  protected function prepare_item_for_database( $request ) {
    $template = $request['id'] ? get_block_template( $request['id'], $this->post_type ) : null;
    $changes  = new stdClass();
    if ( null === $template ) {
      $changes->post_type   = $this->post_type;
      $changes->post_status = 'publish';
      $changes->tax_input   = array(
        'wp_theme' => isset( $request['theme'] ) ? $request['theme'] : get_stylesheet(),
      );
    } elseif ( 'custom' !== $template->source ) {
      $changes->post_name   = $template->slug;
      $changes->post_type   = $this->post_type;
      $changes->post_status = 'publish';
      $changes->tax_input   = array(
        'wp_theme' => $template->theme,
      );
      $changes->meta_input  = array(
        'origin' => $template->source,
      );
    } else {
      $changes->post_name   = $template->slug;
      $changes->ID          = $template->wp_id;
      $changes->post_status = 'publish';
    }
    if ( isset( $request['content'] ) ) {
      if ( is_string( $request['content'] ) ) {
        $changes->post_content = $request['content'];
      } elseif ( isset( $request['content']['raw'] ) ) {
        $changes->post_content = $request['content']['raw'];
      }
    } elseif ( null !== $template && 'custom' !== $template->source ) {
      $changes->post_content = $template->content;
    }
    if ( isset( $request['title'] ) ) {
      if ( is_string( $request['title'] ) ) {
        $changes->post_title = $request['title'];
      } elseif ( ! empty( $request['title']['raw'] ) ) {
        $changes->post_title = $request['title']['raw'];
      }
    } elseif ( null !== $template && 'custom' !== $template->source ) {
      $changes->post_title = $template->title;
    }
    if ( isset( $request['description'] ) ) {
      $changes->post_excerpt = $request['description'];
    } elseif ( null !== $template && 'custom' !== $template->source ) {
      $changes->post_excerpt = $template->description;
    }

    if ( 'wp_template' === $this->post_type && isset( $request['is_wp_suggestion'] ) ) {
      $changes->meta_input     = wp_parse_args(
        array(
          'is_wp_suggestion' => $request['is_wp_suggestion'],
        ),
        $changes->meta_input = array()
      );
    }

    if ( 'wp_template_part' === $this->post_type ) {
      if ( isset( $request['area'] ) ) {
        $changes->tax_input['wp_template_part_area'] = _filter_block_template_part_area( $request['area'] );
      } elseif ( null !== $template && 'custom' !== $template->source && $template->area ) {
        $changes->tax_input['wp_template_part_area'] = _filter_block_template_part_area( $template->area );
      } elseif ( ! $template->area ) {
        $changes->tax_input['wp_template_part_area'] = WP_TEMPLATE_PART_AREA_UNCATEGORIZED;
      }
    }

    if ( ! empty( $request['author'] ) ) {
      $post_author = (int) $request['author'];

      if ( get_current_user_id() !== $post_author ) {
        $user_obj = get_userdata( $post_author );

        if ( ! $user_obj ) {
          return new WP_Error(
            'rest_invalid_author',
            __( 'Invalid author ID.' ),
            array( 'status' => 400 )
          );
        }
      }

      $changes->post_author = $post_author;
    }

    return $changes;
  }