|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * WordAds Consent Management Provider |
| 4 | + * |
| 5 | + * @package automattic/jetpack |
| 6 | + */ |
| 7 | + |
| 8 | +use Automattic\Jetpack\Assets; |
| 9 | + |
| 10 | +/** |
| 11 | + * Class WordAds_Consent_Management_Provider |
| 12 | + * |
| 13 | + * This is an integration with the GDPR Consent Management Provider |
| 14 | + * to comply with GDPR requirements for privacy and transparency related to advertising. |
| 15 | + */ |
| 16 | +class WordAds_Consent_Management_Provider { |
| 17 | + |
| 18 | + /** |
| 19 | + * IAB specified cookie name for storing the consent string. |
| 20 | + */ |
| 21 | + const COOKIE_NAME = 'euconsent-v2'; |
| 22 | + |
| 23 | + /** |
| 24 | + * Initializes loading of the frontend framework. |
| 25 | + */ |
| 26 | + public static function init() { |
| 27 | + // Prevent Cookies & Consent banner from displaying when the CMP is active. |
| 28 | + add_filter( 'jetpack_disable_eu_cookie_law_widget', '__return_true' ); |
| 29 | + add_filter( 'jetpack_disable_cookie_consent_block', '__return_true' ); |
| 30 | + |
| 31 | + // Enqueue scripts. |
| 32 | + add_action( 'wp_enqueue_scripts', array( __CLASS__, 'enqueue_frontend_scripts' ) ); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * AJAX handlers for fetching purposes and vendor data and setting the cookie serverside. |
| 37 | + * |
| 38 | + * Serverside cookie used so that the expiration can be longer than one week. |
| 39 | + * This function is called from: /mu-plugins/wordads-ajax.php to ensure they run on all |
| 40 | + * requests including admin requests. |
| 41 | + */ |
| 42 | + public static function init_ajax_actions() { |
| 43 | + add_action( 'wp_ajax_gdpr_set_consent', array( __CLASS__, 'handle_set_consent_request' ) ); |
| 44 | + add_action( 'wp_ajax_nopriv_gdpr_set_consent', array( __CLASS__, 'handle_set_consent_request' ) ); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Handler for setting consent cookie AJAX request. |
| 49 | + */ |
| 50 | + public static function handle_set_consent_request() { |
| 51 | + |
| 52 | + // phpcs:disable WordPress.Security.NonceVerification.Missing |
| 53 | + if ( ! isset( $_POST['consent'] ) ) { |
| 54 | + wp_send_json_error(); |
| 55 | + } |
| 56 | + |
| 57 | + // TODO: Is there better sanitizing we can do here? |
| 58 | + $consent = trim( wp_unslash( $_POST['consent'] ) ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 59 | + |
| 60 | + setcookie( self::COOKIE_NAME, $consent, time() + YEAR_IN_SECONDS, '/', self::get_cookie_domain(), is_ssl(), false ); // phpcs:ignore Jetpack.Functions.SetCookie -- Client side CMP needs to be able to read this value. |
| 61 | + |
| 62 | + wp_send_json_success( true ); |
| 63 | + |
| 64 | + // phpcs:enable WordPress.Security.NonceVerification.Missing |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Enqueues the main frontend Javascript. |
| 69 | + */ |
| 70 | + public static function enqueue_frontend_scripts() { |
| 71 | + wp_enqueue_script( |
| 72 | + 'cmp_script_loader', |
| 73 | + Assets::get_file_url_for_environment( |
| 74 | + '__inc/build/wordads/js/cmp-loader.min.js', |
| 75 | + 'modules/wordads/js/cmp-loader.js' |
| 76 | + ), |
| 77 | + array(), |
| 78 | + JETPACK__VERSION, |
| 79 | + false |
| 80 | + ); |
| 81 | + |
| 82 | + $request_url = self::get_config_url(); |
| 83 | + wp_enqueue_script( |
| 84 | + 'cmp_config_script', |
| 85 | + Assets::get_file_url_for_environment( |
| 86 | + $request_url, |
| 87 | + $request_url |
| 88 | + ), |
| 89 | + array(), |
| 90 | + JETPACK__VERSION, |
| 91 | + false |
| 92 | + ); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Gets the value to be used when an opt-in cookie is set. |
| 97 | + * |
| 98 | + * @return string The value to store in the opt-in cookie. |
| 99 | + */ |
| 100 | + private static function get_config_url() { |
| 101 | + $locale = strtolower( get_locale() ); // Defaults to en_US not en. |
| 102 | + $request_url = 'https://public-api.wordpress.com/wpcom/v2/sites/' . self::get_blog_id() . '/cmp/configuration/' . $locale . '/?_jsonp=a8c_cmp_callback'; |
| 103 | + return $request_url; |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Get the blog ID. |
| 108 | + * |
| 109 | + * @return Object current blog id. |
| 110 | + */ |
| 111 | + private static function get_blog_id() { |
| 112 | + return Jetpack_Options::get_option( 'id' ); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Gets the domain to be used for the opt-out cookie. |
| 117 | + * Use the site's custom domain, or if the site has a wordpress.com subdomain, use .wordpress.com to share the cookie. |
| 118 | + * |
| 119 | + * @return string The domain to set for the opt-out cookie. |
| 120 | + */ |
| 121 | + public static function get_cookie_domain() { |
| 122 | + $host = 'localhost'; |
| 123 | + |
| 124 | + if ( isset( $_SERVER['HTTP_HOST'] ) ) { |
| 125 | + $host = filter_var( wp_unslash( $_SERVER['HTTP_HOST'] ) ); |
| 126 | + } |
| 127 | + |
| 128 | + return '.wordpress.com' === substr( $host, -strlen( '.wordpress.com' ) ) ? '.wordpress.com' : '.' . $host; |
| 129 | + } |
| 130 | +} |
0 commit comments