Webhook::update()
Summary
Sends a request to the BC API to update a webhook. Creates it if it doesn’t exist.
Source
File: src/BigCommerce/Webhooks/Webhook.php
public function update() { /** * Create a password for authenticating the incoming request from BigCommerce. */ $password = $this->generate_password(); $existing_webhook_id = $this->is_webhook_exist(); /** * Check if webhook exists in BigCommerce */ if ( ! empty( $existing_webhook_id ) ) { $args = [ 'headers' => [ self::AUTH_HEADER => $password ], ]; $this->update_webhook( $existing_webhook_id, $args ); do_action( 'bigcommerce/webhooks/webhook_updated', intval( $existing_webhook_id ), static::NAME, $this->scope() ); return $existing_webhook_id; } $args = [ 'headers' => [ self::AUTH_HEADER => $password ], 'scope' => $this->scope(), 'destination' => $this->destination(), 'is_active' => true, ]; /** * Filter the arguments sent to the BigCommerce API to register a webhook */ $args = apply_filters( 'bigcommerce/webhooks/registration_args', $args, $this ); $result = (array) $this->create( $args ); if ( empty( $result[ 'id' ] ) ) { /** * Fires after webhook update failed. * * @param Webhook Webhook Webhook class. * @param array $result Result. */ do_action( 'bigcommerce/webhooks/update_failed', $this, $result ); } $webhooks = get_option( self::WEBHOOKS_OPTION, [] ); $previous_webhook_id = array_key_exists( static::NAME, $webhooks ) ? absint( $webhooks[ static::NAME ] ) : 0; // Save the returned webhook ID as an option to help with cleanup later. $webhooks[ static::NAME ] = $result[ 'id' ]; update_option( self::WEBHOOKS_OPTION, $webhooks ); if ( $previous_webhook_id ) { // Clean up obsolete web hook. $this->delete( $previous_webhook_id ); } /** * Fires when a webhook is added to the BigCommerce database. * * @param int Webhook ID. * @param string Webhook action name. * @param string Webhook scope. */ do_action( 'bigcommerce/webhooks/webhook_updated', intval( $result[ 'id' ] ), static::NAME, $this->scope() ); return $result[ 'id' ]; }