WPConsentAPI.php
114 lines
| 1 | <?php |
| 2 | declare( strict_types=1 ); |
| 3 | |
| 4 | namespace Automattic\WooCommerce\Internal\Integrations; |
| 5 | |
| 6 | use Automattic\Jetpack\Constants; |
| 7 | use Automattic\WooCommerce\Internal\Traits\ScriptDebug; |
| 8 | use WP_CONSENT_API; |
| 9 | |
| 10 | /** |
| 11 | * Class WPConsentAPI |
| 12 | * |
| 13 | * @since 8.5.0 |
| 14 | */ |
| 15 | class WPConsentAPI { |
| 16 | |
| 17 | use ScriptDebug; |
| 18 | |
| 19 | |
| 20 | /** |
| 21 | * Identifier of the consent category used for order attribution. |
| 22 | * |
| 23 | * @var string |
| 24 | */ |
| 25 | public static $consent_category = 'marketing'; |
| 26 | |
| 27 | /** |
| 28 | * Register the consent API. |
| 29 | * |
| 30 | * @return void |
| 31 | */ |
| 32 | public function register() { |
| 33 | add_action( |
| 34 | 'init', |
| 35 | function() { |
| 36 | $this->on_init(); |
| 37 | }, |
| 38 | 20 // After OrderAttributionController. |
| 39 | ); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Register our hooks on init. |
| 44 | * |
| 45 | * @return void |
| 46 | */ |
| 47 | protected function on_init() { |
| 48 | // Include integration to WP Consent Level API if available. |
| 49 | if ( ! $this->is_wp_consent_api_active() ) { |
| 50 | return; |
| 51 | } |
| 52 | |
| 53 | $plugin = plugin_basename( WC_PLUGIN_FILE ); |
| 54 | add_filter( "wp_consent_api_registered_{$plugin}", '__return_true' ); |
| 55 | add_action( |
| 56 | 'wp_enqueue_scripts', |
| 57 | function() { |
| 58 | $this->enqueue_consent_api_scripts(); |
| 59 | } |
| 60 | ); |
| 61 | |
| 62 | /** |
| 63 | * Modify the "allowTracking" flag consent if the user has consented to marketing. |
| 64 | * |
| 65 | * Wp-consent-api will initialize the modules on "init" with priority 9, |
| 66 | * So this code needs to be run after that. |
| 67 | */ |
| 68 | add_filter( |
| 69 | 'wc_order_attribution_allow_tracking', |
| 70 | function() { |
| 71 | return function_exists( 'wp_has_consent' ) && wp_has_consent( self::$consent_category ); |
| 72 | } |
| 73 | ); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Check if WP Cookie Consent API is active |
| 78 | * |
| 79 | * @return bool |
| 80 | */ |
| 81 | protected function is_wp_consent_api_active() { |
| 82 | return class_exists( WP_CONSENT_API::class ); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Enqueue JS for integration with WP Consent Level API |
| 87 | * |
| 88 | * @return void |
| 89 | */ |
| 90 | private function enqueue_consent_api_scripts() { |
| 91 | wp_enqueue_script( |
| 92 | 'wp-consent-api-integration', |
| 93 | plugins_url( |
| 94 | "assets/js/frontend/wp-consent-api-integration{$this->get_script_suffix()}.js", |
| 95 | WC_PLUGIN_FILE |
| 96 | ), |
| 97 | array( 'wp-consent-api', 'wc-order-attribution' ), |
| 98 | Constants::get_constant( 'WC_VERSION' ), |
| 99 | true |
| 100 | ); |
| 101 | |
| 102 | // Add data for the script above. `wp_enqueue_script` API does not allow data attributes, |
| 103 | // so we need a separate script tag and pollute the global scope. |
| 104 | wp_add_inline_script( |
| 105 | 'wp-consent-api-integration', |
| 106 | sprintf( |
| 107 | 'window.wc_order_attribution.params.consentCategory = %s;', |
| 108 | wp_json_encode( self::$consent_category, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ) |
| 109 | ), |
| 110 | 'before' |
| 111 | ); |
| 112 | } |
| 113 | } |
| 114 |