WC_REST_Webhooks_V1_Controller::update_item()publicWC 1.0

Update a single webhook.

Метод класса: WC_REST_Webhooks_V1_Controller{}

Хуки из метода

Возвращает

WP_Error|WP_REST_Response.

Использование

$WC_REST_Webhooks_V1_Controller = new WC_REST_Webhooks_V1_Controller();
$WC_REST_Webhooks_V1_Controller->update_item( $request );
$request(WP_REST_Request) (обязательный)
Full details about the request.

Код WC_REST_Webhooks_V1_Controller::update_item() WC 8.7.0

public function update_item( $request ) {
	$id      = (int) $request['id'];
	$webhook = wc_get_webhook( $id );

	if ( empty( $webhook ) || is_null( $webhook ) ) {
		return new WP_Error( "woocommerce_rest_{$this->post_type}_invalid_id", __( 'ID is invalid.', 'woocommerce' ), array( 'status' => 400 ) );
	}

	// Update topic.
	if ( ! empty( $request['topic'] ) ) {
		if ( wc_is_webhook_valid_topic( strtolower( $request['topic'] ) ) ) {
			$webhook->set_topic( $request['topic'] );
		} else {
			return new WP_Error( "woocommerce_rest_{$this->post_type}_invalid_topic", __( 'Webhook topic must be valid.', 'woocommerce' ), array( 'status' => 400 ) );
		}
	}

	// Update delivery URL.
	if ( ! empty( $request['delivery_url'] ) ) {
		if ( wc_is_valid_url( $request['delivery_url'] ) ) {
			$webhook->set_delivery_url( $request['delivery_url'] );
		} else {
			return new WP_Error( "woocommerce_rest_{$this->post_type}_invalid_delivery_url", __( 'Webhook delivery URL must be a valid URL starting with http:// or https://.', 'woocommerce' ), array( 'status' => 400 ) );
		}
	}

	// Update secret.
	if ( ! empty( $request['secret'] ) ) {
		$webhook->set_secret( $request['secret'] );
	}

	// Update status.
	if ( ! empty( $request['status'] ) ) {
		if ( wc_is_webhook_valid_status( strtolower( $request['status'] ) ) ) {
			$webhook->set_status( $request['status'] );
		} else {
			return new WP_Error( "woocommerce_rest_{$this->post_type}_invalid_status", __( 'Webhook status must be valid.', 'woocommerce' ), array( 'status' => 400 ) );
		}
	}

	$post = $this->prepare_item_for_database( $request );
	if ( is_wp_error( $post ) ) {
		return $post;
	}

	if ( isset( $post->post_title ) ) {
		$webhook->set_name( $post->post_title );
	}

	$webhook->save();

	$this->update_additional_fields_for_object( $webhook, $request );

	/**
	 * Fires after a single item is created or updated via the REST API.
	 *
	 * @param WC_Webhook      $webhook  Webhook data.
	 * @param WP_REST_Request $request  Request object.
	 * @param bool            $creating True when creating item, false when updating.
	 */
	do_action( 'woocommerce_rest_insert_webhook_object', $webhook, $request, false );

	$request->set_param( 'context', 'edit' );
	$response = $this->prepare_item_for_response( $webhook->get_id(), $request );

	return rest_ensure_response( $response );
}