wc_is_webhook_valid_topic()WC 2.2.0

Check if the given topic is a valid webhook topic, a topic is valid if:

  • starts with action.woocommerce_ or action.wc_.
  • it has a valid resource & event.

Return

true|false.

Usage

wc_is_webhook_valid_topic( $topic );
$topic(string) (required)
Webhook topic.

Changelog

Since 2.2.0 Introduced.

wc_is_webhook_valid_topic() code WC 9.4.2

function wc_is_webhook_valid_topic( $topic ) {
	$invalid_topics = array(
		'action.woocommerce_login_credentials',
		'action.woocommerce_product_csv_importer_check_import_file_path',
		'action.woocommerce_webhook_should_deliver',
	);

	if ( in_array( $topic, $invalid_topics, true ) ) {
		return false;
	}

	// Custom topics are prefixed with woocommerce_ or wc_ are valid.
	if ( 0 === strpos( $topic, 'action.woocommerce_' ) || 0 === strpos( $topic, 'action.wc_' ) ) {
		return true;
	}

	$data = explode( '.', $topic );

	if ( ! isset( $data[0] ) || ! isset( $data[1] ) ) {
		return false;
	}

	$valid_resources = apply_filters( 'woocommerce_valid_webhook_resources', array( 'coupon', 'customer', 'order', 'product' ) );
	$valid_events    = apply_filters( 'woocommerce_valid_webhook_events', array( 'created', 'updated', 'deleted', 'restored' ) );

	if ( in_array( $data[0], $valid_resources, true ) && in_array( $data[1], $valid_events, true ) ) {
		return true;
	}

	return false;
}