owner token. * * @var array */ private static $rtwalwm_locks = array(); /** * Whether the current user may manage referrals and affiliates. * * @return bool */ public static function rtwalwm_can_manage() { return current_user_can( 'manage_options' ) || current_user_can( 'manage_woocommerce' ); } /** * Whether a user is an existing, enabled and approved affiliate. * * Approval follows the existing rule: an explicit rtwwwap_aff_approved = 0 is never approved, * and when affiliate verification is enabled the flag must be 1. * * @param int $rtwalwm_user_id User ID. * @return bool */ public static function rtwalwm_is_active_affiliate( $rtwalwm_user_id ) { $rtwalwm_user_id = absint( $rtwalwm_user_id ); if ( ! $rtwalwm_user_id || ! get_userdata( $rtwalwm_user_id ) ) { return false; } if ( '1' !== (string) get_user_meta( $rtwalwm_user_id, 'rtwwwap_affiliate', true ) ) { return false; } $rtwalwm_approved = (string) get_user_meta( $rtwalwm_user_id, 'rtwwwap_aff_approved', true ); if ( '0' === $rtwalwm_approved ) { return false; } $rtwalwm_extra = get_option( 'rtwwwap_extra_features_opt' ); if ( is_array( $rtwalwm_extra ) && ! empty( $rtwalwm_extra['aff_verify'] ) && '1' !== $rtwalwm_approved ) { return false; } return true; } /** * Whether attributing an order to an affiliate would be a self-referral: the affiliate is the order's customer * or billing contact (and, at checkout, the visitor placing the order). * * @param int $rtwalwm_aff_id Affiliate user ID. * @param WC_Order $rtwalwm_order Order. * @param bool $rtwalwm_include_current_user Also treat the current user as the buyer (checkout only). * @return bool */ public static function rtwalwm_is_self_referral( $rtwalwm_aff_id, $rtwalwm_order, $rtwalwm_include_current_user = true ) { $rtwalwm_aff_id = absint( $rtwalwm_aff_id ); if ( $rtwalwm_include_current_user && get_current_user_id() === $rtwalwm_aff_id ) { return true; } if ( $rtwalwm_order && (int) $rtwalwm_order->get_customer_id() === $rtwalwm_aff_id ) { return true; } $rtwalwm_user = get_userdata( $rtwalwm_aff_id ); $rtwalwm_billing = $rtwalwm_order ? strtolower( trim( (string) $rtwalwm_order->get_billing_email() ) ) : ''; if ( $rtwalwm_user && '' !== $rtwalwm_billing && strtolower( $rtwalwm_user->user_email ) === $rtwalwm_billing ) { return true; } return false; } /** * The affiliate of the order's affiliate coupon, if any. A coupon attributes only while it is applied to the order, * only when it is linked to an affiliate (meta rtwalwm_coupon_aff_id) who is an active affiliate and not a * self-referral. With several valid affiliate coupons, the most recently applied one wins (WooCommerce keeps the * coupon lines in the order they were applied). Ordinary discount coupons are ignored. * * @param WC_Order $rtwalwm_order Order. * @return int Affiliate user ID, or 0. */ public static function rtwalwm_coupon_affiliate( $rtwalwm_order ) { if ( ! $rtwalwm_order || ! function_exists( 'wc_get_coupon_id_by_code' ) || ! is_callable( array( $rtwalwm_order, 'get_coupon_codes' ) ) ) { return 0; } $rtwalwm_winner = 0; foreach ( (array) $rtwalwm_order->get_coupon_codes() as $rtwalwm_code ) { $rtwalwm_coupon_id = (int) wc_get_coupon_id_by_code( $rtwalwm_code ); $rtwalwm_aff_id = $rtwalwm_coupon_id ? absint( get_post_meta( $rtwalwm_coupon_id, 'rtwalwm_coupon_aff_id', true ) ) : 0; if ( $rtwalwm_aff_id && self::rtwalwm_is_active_affiliate( $rtwalwm_aff_id ) && ! self::rtwalwm_is_self_referral( $rtwalwm_aff_id, $rtwalwm_order ) ) { $rtwalwm_winner = $rtwalwm_aff_id; } } return $rtwalwm_winner; } /** * Parse the referral cookie: "" plus optional "#share", "#coupon" or product segments. * * @return array|null */ public static function rtwalwm_parse_referral_cookie() { if ( empty( $_COOKIE[ self::RTWALWM_COOKIE ] ) ) { return null; } $rtwalwm_raw = sanitize_text_field( wp_unslash( $_COOKIE[ self::RTWALWM_COOKIE ] ) ); $rtwalwm_parts = explode( '#', $rtwalwm_raw ); $rtwalwm_id = absint( $rtwalwm_parts[0] ); if ( ! $rtwalwm_id || (string) $rtwalwm_id !== $rtwalwm_parts[0] ) { return null; } return array( 'aff_id' => $rtwalwm_id, 'share' => in_array( 'share', $rtwalwm_parts, true ), 'coupon' => in_array( 'coupon', $rtwalwm_parts, true ), ); } /** * Cookie options. The path stays "/" so cookies set by earlier versions are replaced and cleared. * * @param int $rtwalwm_expires Expiry timestamp (0 = session). * @return array */ private static function rtwalwm_cookie_options( $rtwalwm_expires ) { return array( 'expires' => (int) $rtwalwm_expires, 'path' => '/', 'secure' => is_ssl(), 'httponly' => true, 'samesite' => 'Lax', ); } /** * Set the referral cookie. The lifetime is unchanged from earlier versions (session cookie by default). * * @param string $rtwalwm_value Cookie value. * @param int $rtwalwm_expires Expiry timestamp (0 = session). * @return bool */ public static function rtwalwm_set_referral_cookie( $rtwalwm_value, $rtwalwm_expires = 0 ) { if ( headers_sent() ) { return false; } $rtwalwm_value = (string) $rtwalwm_value; $rtwalwm_set = setcookie( self::RTWALWM_COOKIE, $rtwalwm_value, self::rtwalwm_cookie_options( $rtwalwm_expires ) ); if ( $rtwalwm_set ) { $_COOKIE[ self::RTWALWM_COOKIE ] = $rtwalwm_value; } return $rtwalwm_set; } /** * Delete the referral cookie. * * @return bool */ public static function rtwalwm_clear_referral_cookie() { unset( $_COOKIE[ self::RTWALWM_COOKIE ] ); if ( headers_sent() ) { return false; } return setcookie( self::RTWALWM_COOKIE, '', self::rtwalwm_cookie_options( time() - YEAR_IN_SECONDS ) ); } /** * Acquire a short-lived lock: an atomic INSERT IGNORE of an option row holding "