WC_API_Webhooks::edit_webhook()
Edit a webhook
Method of the class: WC_API_Webhooks{}
Hooks from the method
Return
Array|WP_Error
.
Usage
$WC_API_Webhooks = new WC_API_Webhooks(); $WC_API_Webhooks->edit_webhook( $id, $data );
- $id(int) (required)
- webhook ID
- $data(array) (required)
- parsed webhook data
Changelog
Since 2.2 | Introduced. |
WC_API_Webhooks::edit_webhook() WC API Webhooks::edit webhook code WC 8.7.0
public function edit_webhook( $id, $data ) { try { if ( ! isset( $data['webhook'] ) ) { throw new WC_API_Exception( 'woocommerce_api_missing_webhook_data', sprintf( __( 'No %1$s data specified to edit %1$s', 'woocommerce' ), 'webhook' ), 400 ); } $data = $data['webhook']; $id = $this->validate_request( $id, 'shop_webhook', 'edit' ); if ( is_wp_error( $id ) ) { return $id; } $data = apply_filters( 'woocommerce_api_edit_webhook_data', $data, $id, $this ); $webhook = wc_get_webhook( $id ); // update topic if ( ! empty( $data['topic'] ) ) { if ( wc_is_webhook_valid_topic( strtolower( $data['topic'] ) ) ) { $webhook->set_topic( $data['topic'] ); } else { throw new WC_API_Exception( 'woocommerce_api_invalid_webhook_topic', __( 'Webhook topic must be valid.', 'woocommerce' ), 400 ); } } // update delivery URL if ( ! empty( $data['delivery_url'] ) ) { if ( wc_is_valid_url( $data['delivery_url'] ) ) { $webhook->set_delivery_url( $data['delivery_url'] ); } else { throw new WC_API_Exception( 'woocommerce_api_invalid_webhook_delivery_url', __( 'Webhook delivery URL must be a valid URL starting with http:// or https://', 'woocommerce' ), 400 ); } } // update secret if ( ! empty( $data['secret'] ) ) { $webhook->set_secret( $data['secret'] ); } // update status if ( ! empty( $data['status'] ) ) { $webhook->set_status( $data['status'] ); } // update name if ( ! empty( $data['name'] ) ) { $webhook->set_name( $data['name'] ); } $webhook->save(); do_action( 'woocommerce_api_edit_webhook', $webhook->get_id(), $this ); return $this->get_webhook( $webhook->get_id() ); } catch ( WC_API_Exception $e ) { return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); } }