WC_Webhook_Data_Store::update()publicWC 3.3.0

Update a webhook.

Method of the class: WC_Webhook_Data_Store{}

Hooks from the method

Return

null. Nothing (null).

Usage

$WC_Webhook_Data_Store = new WC_Webhook_Data_Store();
$WC_Webhook_Data_Store->update( $webhook );
$webhook(WC_Webhook) (required) (passed by reference — &)
Webhook instance.

Changelog

Since 3.3.0 Introduced.

WC_Webhook_Data_Store::update() code WC 8.7.0

public function update( &$webhook ) {
	global $wpdb;

	$changes = $webhook->get_changes();
	$trigger = isset( $changes['delivery_url'] );

	if ( isset( $changes['date_modified'] ) ) {
		$date_modified     = $webhook->get_date_modified()->date( 'Y-m-d H:i:s' );
		$date_modified_gmt = gmdate( 'Y-m-d H:i:s', $webhook->get_date_modified()->getTimestamp() );
	} else {
		$date_modified     = current_time( 'mysql' );
		$date_modified_gmt = current_time( 'mysql', 1 );
		$webhook->set_date_modified( $date_modified );
	}

	$data = array(
		'status'            => $webhook->get_status( 'edit' ),
		'name'              => $webhook->get_name( 'edit' ),
		'user_id'           => $webhook->get_user_id( 'edit' ),
		'delivery_url'      => $webhook->get_delivery_url( 'edit' ),
		'secret'            => $webhook->get_secret( 'edit' ),
		'topic'             => $webhook->get_topic( 'edit' ),
		'date_modified'     => $date_modified,
		'date_modified_gmt' => $date_modified_gmt,
		'api_version'       => $this->get_api_version_number( $webhook->get_api_version( 'edit' ) ),
		'failure_count'     => $webhook->get_failure_count( 'edit' ),
		'pending_delivery'  => $webhook->get_pending_delivery( 'edit' ),
	);

	$wpdb->update(
		$wpdb->prefix . 'wc_webhooks',
		$data,
		array(
			'webhook_id' => $webhook->get_id(),
		)
	); // WPCS: DB call ok.

	$webhook->apply_changes();

	if ( isset( $changes['status'] ) ) {
		// We need to delete all transients, because we can't be sure of the old status.
		$this->delete_transients( 'all' );
	}
	wp_cache_delete( $webhook->get_id(), 'webhooks' );
	WC_Cache_Helper::invalidate_cache_group( 'webhooks' );

	if ( 'active' === $webhook->get_status() && ( $trigger || $webhook->get_pending_delivery() ) ) {
		$webhook->deliver_ping();
	}

	do_action( 'woocommerce_webhook_updated', $webhook->get_id() );
}