declare( strict_types=1 );
namespace Automattic\WooCommerce\Internal\Integrations;
use Automattic\Jetpack\Constants;
use Automattic\WooCommerce\Internal\Traits\ScriptDebug;
* Identifier of the consent category used for order attribution.
public static $consent_category = 'marketing';
* Register the consent API.
public function register() {
20 // After OrderAttributionController.
* Register our hooks on init.
protected function on_init() {
// Include integration to WP Consent Level API if available.
if ( ! $this->is_wp_consent_api_active() ) {
$plugin = plugin_basename( WC_PLUGIN_FILE );
add_filter( "wp_consent_api_registered_{$plugin}", '__return_true' );
$this->enqueue_consent_api_scripts();
* Modify the "allowTracking" flag consent if the user has consented to marketing.
* Wp-consent-api will initialize the modules on "init" with priority 9,
* So this code needs to be run after that.
'wc_order_attribution_allow_tracking',
return function_exists( 'wp_has_consent' ) && wp_has_consent( self::$consent_category );
* Check if WP Cookie Consent API is active
protected function is_wp_consent_api_active() {
return class_exists( WP_CONSENT_API::class );
* Enqueue JS for integration with WP Consent Level API
private function enqueue_consent_api_scripts() {
'wp-consent-api-integration',
"assets/js/frontend/wp-consent-api-integration{$this->get_script_suffix()}.js",
array( 'wp-consent-api', 'wc-order-attribution' ),
Constants::get_constant( 'WC_VERSION' ),
// Add data for the script above. `wp_enqueue_script` API does not allow data attributes,
// so we need a separate script tag and pollute the global scope.
'wp-consent-api-integration',
'window.wc_order_attribution.params.consentCategory = %s;',
wp_json_encode( self::$consent_category )