class-privacy.php
273 lines
| 1 | <?php // phpcs:ignore WordPress.Files.FileName |
| 2 | |
| 3 | use AdvancedAds\Abstracts\Ad; |
| 4 | use AdvancedAds\Utilities\Conditional; |
| 5 | use AdvancedAds\Framework\Utilities\Str; |
| 6 | use AdvancedAds\Framework\Utilities\Params; |
| 7 | |
| 8 | /** |
| 9 | * Handles Advanced Ads privacy settings. |
| 10 | */ |
| 11 | class Advanced_Ads_Privacy { |
| 12 | /** |
| 13 | * Singleton instance of the plugin |
| 14 | * |
| 15 | * @var self |
| 16 | */ |
| 17 | protected static $instance; |
| 18 | |
| 19 | /** |
| 20 | * Module options |
| 21 | * |
| 22 | * @var null|array |
| 23 | */ |
| 24 | protected $options; |
| 25 | |
| 26 | /** |
| 27 | * Option key |
| 28 | * |
| 29 | * @const string |
| 30 | */ |
| 31 | const OPTION_KEY = 'advanced-ads-privacy'; |
| 32 | |
| 33 | /** |
| 34 | * Initialize the module |
| 35 | */ |
| 36 | private function __construct() { |
| 37 | add_filter( 'advanced-ads-can-display-ad', [ $this, 'can_display_by_consent' ], 10, 3 ); |
| 38 | |
| 39 | $this->options(); |
| 40 | |
| 41 | if ( ! empty( $this->options['enabled'] ) ) { |
| 42 | add_filter( 'advanced-ads-activate-advanced-js', '__return_true' ); |
| 43 | |
| 44 | if ( 'iab_tcf_20' === $this->options['consent-method'] ) { |
| 45 | add_filter( 'advanced-ads-ad-output', [ $this, 'final_ad_output' ], 10, 2 ); |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * If this ad is not image or dummy base64_encode the text. |
| 52 | * |
| 53 | * @param string $output The output string. |
| 54 | * @param Ad $ad Ad instance. |
| 55 | * |
| 56 | * @return string |
| 57 | */ |
| 58 | public function final_ad_output( $output, Ad $ad ) { |
| 59 | if ( |
| 60 | Conditional::is_amp() || |
| 61 | ! $this->ad_type_needs_consent( $ad->get_type() ) || |
| 62 | ( ! $ad->is_type( 'adsense' ) && $ad->get_prop( 'privacy.ignore-consent' ) ) |
| 63 | ) { |
| 64 | return $output; |
| 65 | } |
| 66 | |
| 67 | return $this->encode_ad( $output, $ad ); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Encode the ad output. |
| 72 | * |
| 73 | * @param string $output The output string. |
| 74 | * @param Ad $ad Ad intance. |
| 75 | * |
| 76 | * @return string |
| 77 | */ |
| 78 | public function encode_ad( $output, Ad $ad ) { |
| 79 | $data_attributes = [ |
| 80 | 'id' => $ad->get_id(), |
| 81 | 'bid' => get_current_blog_id(), |
| 82 | ]; |
| 83 | |
| 84 | if ( $ad->is_parent_placement() ) { |
| 85 | $data_attributes['placement'] = $ad->get_parent()->get_id(); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Filter the data attributes and allow removing/adding attributes. |
| 90 | * All attributes will be prefix with `data-` on output. |
| 91 | * |
| 92 | * @param array $data_attributes The default data attributes. |
| 93 | * @param Ad $ad Ad instance. |
| 94 | */ |
| 95 | $data_attributes = (array) apply_filters( 'advanced-ads-privacy-output-attributes', $data_attributes, $ad ); |
| 96 | |
| 97 | // Convert the data-attributes array into a string. |
| 98 | $attributes_string = ''; |
| 99 | array_walk( |
| 100 | $data_attributes, |
| 101 | function ( $value, $key ) use ( &$attributes_string ) { |
| 102 | $attributes_string .= sprintf( ' data-%s="%s"', sanitize_key( $key ), esc_attr( $value ) ); |
| 103 | } |
| 104 | ); |
| 105 | |
| 106 | return sprintf( |
| 107 | '<script type="text/plain" data-tcf="waiting-for-consent"%s>%s</script>', |
| 108 | $attributes_string, |
| 109 | // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode -- we need to obfuscate the html (and maybe decode it in JS). |
| 110 | base64_encode( $output ) |
| 111 | ); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Check if the current ad output is encoded. |
| 116 | * |
| 117 | * @param string $output The ad output. |
| 118 | * |
| 119 | * @return bool |
| 120 | */ |
| 121 | public function is_ad_output_encoded( $output ) { |
| 122 | return (bool) strpos( $output, 'data-tcf="waiting-for-consent"' ); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Return an instance of Advanced_Ads_Privacy |
| 127 | * |
| 128 | * @return self |
| 129 | */ |
| 130 | public static function get_instance() { |
| 131 | // If the single instance hasn't been set, set it now. |
| 132 | if ( null === self::$instance ) { |
| 133 | self::$instance = new self(); |
| 134 | } |
| 135 | |
| 136 | return self::$instance; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Return module options |
| 141 | * |
| 142 | * @return array |
| 143 | */ |
| 144 | public function options() { |
| 145 | if ( ! isset( $this->options ) ) { |
| 146 | $this->options = get_option( self::OPTION_KEY, [] ); |
| 147 | if ( isset( $this->options['enabled'] ) && empty( $this->options['consent-method'] ) ) { |
| 148 | $this->options['enabled'] = false; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | return $this->options; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Check if ad can be displayed based on user's consent. |
| 157 | * |
| 158 | * @param bool $can_display Whether to display this ad. |
| 159 | * @param Ad $ad Ad instance. |
| 160 | * @param array $check_options Additional options passed to can_display. |
| 161 | * |
| 162 | * @return bool |
| 163 | */ |
| 164 | public function can_display_by_consent( $can_display, Ad $ad, $check_options ) { |
| 165 | // Early bail!! |
| 166 | if ( ! $can_display ) { |
| 167 | return $can_display; |
| 168 | } |
| 169 | |
| 170 | // Passive cache busting enabled. |
| 171 | if ( $check_options['passive_cache_busting'] ) { |
| 172 | return true; |
| 173 | } |
| 174 | |
| 175 | // privacy module not active, bail early. |
| 176 | if ( empty( $this->options['enabled'] ) ) { |
| 177 | return true; |
| 178 | } |
| 179 | |
| 180 | // If consent is overriden for the ad. |
| 181 | if ( ! empty( $ad->get_prop( 'privacy.ignore-consent' ) ) ) { |
| 182 | return true; |
| 183 | } |
| 184 | |
| 185 | $consent_method = $this->options['consent-method'] ?? ''; |
| 186 | |
| 187 | // If the consent method is set to cookie and the ad type does not need consent. |
| 188 | if ( 'custom' === $consent_method && ! $this->ad_type_needs_consent( $ad->get_type() ) ) { |
| 189 | return true; |
| 190 | } |
| 191 | |
| 192 | // If method is iab_tcf_20, always set to true, JS needs to decide whether to display ad or not. |
| 193 | if ( 'iab_tcf_20' === $consent_method ) { |
| 194 | return true; |
| 195 | } |
| 196 | |
| 197 | // Either personalized or non-personalized ad will be shown. |
| 198 | if ( $ad->is_type( 'adsense' ) && ! empty( $this->options()['show-non-personalized-adsense'] ) ) { |
| 199 | return true; |
| 200 | } |
| 201 | |
| 202 | return $this->get_state() !== 'unknown'; |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Check whether this ad_type needs consent. |
| 207 | * |
| 208 | * @param string $type The ad type. |
| 209 | * |
| 210 | * @return bool |
| 211 | */ |
| 212 | public function ad_type_needs_consent( $type ) { |
| 213 | return ! in_array( $type, [ 'image', 'dummy', 'group' ], true ); |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Check if consent is not needed or was given by the user. |
| 218 | * |
| 219 | * @return string |
| 220 | * 'not_needed' - consent is not needed. |
| 221 | * 'accepted' - consent was given. |
| 222 | * 'unknown' - consent was not given yet. |
| 223 | */ |
| 224 | public function get_state() { |
| 225 | static $state; |
| 226 | if ( is_null( $state ) ) { |
| 227 | $state = $this->parse_state(); |
| 228 | } |
| 229 | |
| 230 | return $state; |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Used by get_state() to parse the state of privacy/consent. |
| 235 | * |
| 236 | * @return string |
| 237 | * 'not_needed' - consent is not needed. |
| 238 | * 'accepted' - consent was given. |
| 239 | * 'unknown' - consent was not given yet. |
| 240 | */ |
| 241 | private function parse_state() { |
| 242 | if ( empty( $this->options['enabled'] ) || Conditional::is_amp() ) { |
| 243 | return 'not_needed'; |
| 244 | } |
| 245 | |
| 246 | $consent_method = $this->options['consent-method'] ?? ''; |
| 247 | switch ( $consent_method ) { |
| 248 | case 'custom': |
| 249 | $name = $this->options['custom-cookie-name']; |
| 250 | if ( empty( $name ) ) { |
| 251 | return 'not_needed'; |
| 252 | } |
| 253 | |
| 254 | if ( ! Params::cookie( $name ) ) { |
| 255 | return 'unknown'; |
| 256 | } |
| 257 | |
| 258 | $value = $this->options['custom-cookie-value'] ?? ''; |
| 259 | if ( |
| 260 | ( '' === $value && '' === Params::cookie( $name ) ) || |
| 261 | Str::contains( $value, Params::cookie( $name ) ) |
| 262 | ) { |
| 263 | return 'accepted'; |
| 264 | } |
| 265 | |
| 266 | return 'unknown'; |
| 267 | case 'iab_tcf_20': |
| 268 | default: |
| 269 | return 'unknown'; |
| 270 | } |
| 271 | } |
| 272 | } |
| 273 |