WC_Webhook_Data_Store::create()publicWC 3.3.0

Create a new webhook in the database.

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->create( $webhook );
$webhook(WC_Webhook) (required) (passed by reference — &)
Webhook instance.

Changelog

Since 3.3.0 Introduced.

WC_Webhook_Data_Store::create() code WC 9.4.2

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

	$changes = $webhook->get_changes();
	if ( isset( $changes['date_created'] ) ) {
		$date_created     = $webhook->get_date_created()->date( 'Y-m-d H:i:s' );
		$date_created_gmt = gmdate( 'Y-m-d H:i:s', $webhook->get_date_created()->getTimestamp() );
	} else {
		$date_created     = current_time( 'mysql' );
		$date_created_gmt = current_time( 'mysql', 1 );
		$webhook->set_date_created( $date_created );
	}

	// Pending delivery by default if not set while creating a new webhook.
	if ( ! isset( $changes['pending_delivery'] ) ) {
		$webhook->set_pending_delivery( true );
	}

	$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_created'     => $date_created,
		'date_created_gmt' => $date_created_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->insert( $wpdb->prefix . 'wc_webhooks', $data ); // WPCS: DB call ok.

	$webhook_id = $wpdb->insert_id;
	$webhook->set_id( $webhook_id );
	$webhook->apply_changes();

	$this->delete_transients( $webhook->get_status( 'edit' ) );
	WC_Cache_Helper::invalidate_cache_group( 'webhooks' );
	do_action( 'woocommerce_new_webhook', $webhook_id, $webhook );
}