admin_url( 'admin-ajax.php' ), 'param' => self::PARAM, 'beacon' => $beacon, 'ask' => $ask, ); if ( $ask ) { $days = Affiliatant_Settings::cookie_days(); $config['days'] = $days; $config['text'] = array( 'label' => __( 'Partner referral', 'affiliatant-for-woocommerce' ), 'lead' => __( 'You arrived through a link from one of our partners.', 'affiliatant-for-woocommerce' ), 'body' => sprintf( /* translators: %d: number of days the referral is remembered */ _n( 'May we remember that for up to %d day, so the partner is credited if you sign up or buy later? We would store a reference to the link on this device and nothing else.', 'May we remember that for up to %d days, so the partner is credited if you sign up or buy later? We would store a reference to the link on this device and nothing else.', $days, 'affiliatant-for-woocommerce' ), $days ), 'yes' => __( 'Yes, remember it', 'affiliatant-for-woocommerce' ), 'no' => __( 'No, thanks', 'affiliatant-for-woocommerce' ), ); } wp_add_inline_script( 'affiliatant-click', 'window.affiliatantClick = ' . wp_json_encode( $config ) . ';', 'before' ); } public static function ajax_click() { if ( ! Affiliatant_Settings::active() ) { wp_send_json_success( array( 'stored' => false, 'ask' => false, ) ); } $posted = self::posted_click(); if ( null === $posted ) { wp_send_json_success( array( 'stored' => false, 'ask' => false, ) ); } self::remember( $posted['click_id'], $posted['subs'], $posted['landing'] ); wp_send_json_success( array( 'stored' => true, 'ask' => self::undecided(), ) ); } public static function ajax_consent() { if ( ! Affiliatant_Settings::active() || ! self::undecided() ) { wp_send_json_success( array( 'stored' => false ) ); } $posted = self::posted_click(); if ( null === $posted ) { wp_send_json_success( array( 'stored' => false ) ); } self::token( true ); self::remember( $posted['click_id'], $posted['subs'], $posted['landing'] ); wp_send_json_success( array( 'stored' => true ) ); } private static function posted_click() { $raw = isset( $_POST[ self::PARAM ] ) ? wp_unslash( $_POST[ self::PARAM ] ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- clean_click_id is the sanitiser. $click_id = self::clean_click_id( $raw ); if ( '' === $click_id ) { return null; } $subs = array(); for ( $i = 1; $i <= 5; $i++ ) { $name = 'sub' . $i; if ( isset( $_POST[ $name ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing $subs[ $name ] = self::clean_sub( wp_unslash( $_POST[ $name ] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized } } $landing = isset( $_POST['landing'] ) ? esc_url_raw( wp_unslash( $_POST['landing'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing if ( '' !== $landing && 0 !== strpos( $landing, home_url() ) ) { $landing = ''; } return array( 'click_id' => $click_id, 'subs' => $subs, 'landing' => $landing, ); } public static function remember( $click_id, array $subs, $landing ) { $context = array( 'click_id' => (string) $click_id, 'landing_url' => substr( (string) $landing, 0, 512 ), 'captured' => time(), ); for ( $i = 1; $i <= 5; $i++ ) { $context[ 'sub' . $i ] = isset( $subs[ 'sub' . $i ] ) ? (string) $subs[ 'sub' . $i ] : ''; } $days = (int) Affiliatant_Settings::get( 'cookie_days' ); if ( $days < 1 || $days > 365 ) { $days = 30; } $token = self::token( self::consented() ); if ( '' !== $token ) { set_transient( self::TRANSIENT . $token, $context, $days * DAY_IN_SECONDS ); } $session = self::session(); if ( null !== $session ) { $session->set( self::SESSION_KEY, $context ); // Starts the WooCommerce session for a visitor who has none, so the referral lasts the visit. if ( method_exists( $session, 'has_session' ) && method_exists( $session, 'set_customer_session_cookie' ) && ! $session->has_session() ) { $session->set_customer_session_cookie( true ); } } self::$cache = $context; return $context; } public static function consent_plugin_active() { return function_exists( 'wp_has_consent' ) && function_exists( 'wp_get_consent_type' ) && '' !== (string) wp_get_consent_type(); } public static function consented() { if ( self::consent_plugin_active() ) { return (bool) wp_has_consent( 'marketing' ); } switch ( Affiliatant_Settings::consent_mode() ) { case 'always': return true; case 'ask': return '' !== self::token( false ); default: return false; } } public static function undecided() { return 'ask' === Affiliatant_Settings::consent_mode() && ! self::consent_plugin_active() && ! isset( $_COOKIE[ self::REFUSED_COOKIE ] ) && '' === self::token( false ); } public static function sync_with_consent() { if ( is_admin() || headers_sent() || ! Affiliatant_Settings::active() ) { return; } $held = self::token( false ); if ( ! self::consented() ) { if ( '' !== $held ) { delete_transient( self::TRANSIENT . $held ); setcookie( self::TOKEN_COOKIE, '', time() - HOUR_IN_SECONDS, COOKIEPATH ? COOKIEPATH : '/', COOKIE_DOMAIN ); unset( $_COOKIE[ self::TOKEN_COOKIE ] ); } return; } if ( '' === $held ) { $session = self::session(); $stored = null !== $session ? $session->get( self::SESSION_KEY ) : null; if ( is_array( $stored ) && ! empty( $stored['click_id'] ) ) { $subs = array(); for ( $i = 1; $i <= 5; $i++ ) { $subs[ 'sub' . $i ] = isset( $stored[ 'sub' . $i ] ) ? (string) $stored[ 'sub' . $i ] : ''; } self::remember( (string) $stored['click_id'], $subs, isset( $stored['landing_url'] ) ? (string) $stored['landing_url'] : '' ); } } } public static function current() { if ( null !== self::$cache ) { return self::$cache; } $session = self::session(); if ( null !== $session ) { $stored = $session->get( self::SESSION_KEY ); if ( is_array( $stored ) && ! empty( $stored['click_id'] ) ) { self::$cache = $stored; return $stored; } } $token = self::token( false ); if ( '' !== $token ) { $stored = get_transient( self::TRANSIENT . $token ); if ( is_array( $stored ) && ! empty( $stored['click_id'] ) ) { if ( null !== $session ) { $session->set( self::SESSION_KEY, $stored ); } self::$cache = $stored; return $stored; } } self::$cache = array(); return self::$cache; } private static function session() { if ( ! function_exists( 'WC' ) ) { return null; } $wc = WC(); if ( ! is_object( $wc ) ) { return null; } if ( ! isset( $wc->session ) || ! is_object( $wc->session ) ) { $is_our_ajax = defined( 'DOING_AJAX' ) && DOING_AJAX && isset( $_REQUEST['action'] ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended && in_array( $_REQUEST['action'], array( 'affiliatant_click', 'affiliatant_consent' ), true ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized if ( $is_our_ajax && method_exists( $wc, 'initialize_session' ) ) { $wc->initialize_session(); } } return ( isset( $wc->session ) && is_object( $wc->session ) ) ? $wc->session : null; } private static function token( $create ) { $existing = isset( $_COOKIE[ self::TOKEN_COOKIE ] ) ? (string) wp_unslash( $_COOKIE[ self::TOKEN_COOKIE ] ) : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- validated by the pattern below. if ( preg_match( '/^[A-Za-z0-9]{32}$/', $existing ) ) { return $existing; } if ( ! $create ) { return ''; } $token = wp_generate_password( 32, false, false ); $days = (int) Affiliatant_Settings::get( 'cookie_days' ); if ( $days < 1 || $days > 365 ) { $days = 30; } if ( ! headers_sent() ) { setcookie( self::TOKEN_COOKIE, $token, array( 'expires' => time() + ( $days * DAY_IN_SECONDS ), 'path' => COOKIEPATH ? COOKIEPATH : '/', 'domain' => COOKIE_DOMAIN, 'secure' => is_ssl(), 'httponly' => true, 'samesite' => 'Lax', ) ); } $_COOKIE[ self::TOKEN_COOKIE ] = $token; return $token; } public static function clean_click_id( $value ) { if ( ! is_string( $value ) ) { return ''; } $value = trim( $value ); if ( ! preg_match( '/^[0-9A-Za-z]{10,32}$/', $value ) ) { return ''; } return $value; } public static function clean_sub( $value ) { if ( ! is_string( $value ) ) { return ''; } return substr( preg_replace( '/[^\x20-\x7E]/', '', $value ), 0, 190 ); } private static function current_url() { if ( empty( $_SERVER['REQUEST_URI'] ) ) { return ''; } $path = esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- esc_url_raw is the sanitiser. return substr( home_url( $path ), 0, 512 ); } }