WC_REST_Webhooks_V1_Controller::create_item()
Create 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->create_item( $request );
- $request(WP_REST_Request) (обязательный)
- Full details about the request.
Код WC_REST_Webhooks_V1_Controller::create_item() WC REST Webhooks V1 Controller::create item WC 9.2.3
public function create_item( $request ) { if ( ! empty( $request['id'] ) ) { /* translators: %s: post type */ return new WP_Error( "woocommerce_rest_{$this->post_type}_exists", sprintf( __( 'Cannot create existing %s.', 'woocommerce' ), $this->post_type ), array( 'status' => 400 ) ); } // Validate topic. if ( empty( $request['topic'] ) || ! wc_is_webhook_valid_topic( strtolower( $request['topic'] ) ) ) { return new WP_Error( "woocommerce_rest_{$this->post_type}_invalid_topic", __( 'Webhook topic is required and must be valid.', 'woocommerce' ), array( 'status' => 400 ) ); } // Validate delivery URL. if ( empty( $request['delivery_url'] ) || ! wc_is_valid_url( $request['delivery_url'] ) ) { 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 ) ); } $post = $this->prepare_item_for_database( $request ); if ( is_wp_error( $post ) ) { return $post; } $webhook = new WC_Webhook(); $webhook->set_name( $post->post_title ); $webhook->set_user_id( $post->post_author ); $webhook->set_status( 'publish' === $post->post_status ? 'active' : 'disabled' ); $webhook->set_topic( $request['topic'] ); $webhook->set_delivery_url( $request['delivery_url'] ); $webhook->set_secret( ! empty( $request['secret'] ) ? $request['secret'] : wp_generate_password( 50, true, true ) ); $webhook->set_api_version( $this->get_default_api_version() ); $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, true ); $request->set_param( 'context', 'edit' ); $response = $this->prepare_item_for_response( $webhook->get_id(), $request ); $response = rest_ensure_response( $response ); $response->set_status( 201 ); $response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $webhook->get_id() ) ) ); // Send ping. $webhook->deliver_ping(); return $response; }