registered_integrations[ self::POINTS_AND_REWARDS_API ] ) || ! class_exists( 'WC_Points_Rewards_Manager' ) || ! method_exists( 'WC_Points_Rewards_Manager', 'get_users_points' ) ) { return null; } $points_and_rewards_script_data = $this->registered_integrations[ self::POINTS_AND_REWARDS_API ]->get_script_data(); list( $points, $monetary_value ) = explode( ':', get_option( 'wc_points_rewards_redeem_points_ratio', '' ) ); $points = floatval( $points ); $monetary_value = floatval( $monetary_value ); $points_and_rewards_script_data['points_ratio'] = [ 'points' => $points, 'monetary_value' => $monetary_value, ]; /** * Check if the user has points to show the verify email alert. * * @psalm-suppress UndefinedClass */ $available_points_for_user = \WC_Points_Rewards_Manager::get_users_points( $user->ID ); if ( $available_points_for_user > 0 && $available_points_for_user > $points_and_rewards_script_data['minimum_points_amount'] ) { // Only ask the user to verify email if they have available points. $points_and_rewards_script_data['should_verify_email'] = ! is_user_logged_in(); $points_and_rewards_script_data['points_available'] = $available_points_for_user; } return $points_and_rewards_script_data; } /** * Get Gift Cards settings for WooPay. * * @param \WP_User $user The user the data will be loaded. * * @return array|null The Gift Cards script data if installed. */ public function get_gift_cards_data( $user ) { if ( empty( $this->registered_integrations[ self::GIFT_CARDS_BLOCKS ] ) || ! function_exists( 'WC_GC' ) || ! property_exists( WC_GC(), 'account' ) || ! method_exists( WC_GC()->account, 'get_active_giftcards' ) ) { return null; } $gift_cards_script_data = $this->registered_integrations[ self::GIFT_CARDS_BLOCKS ]->get_script_data(); $gift_cards_script_data['should_verify_email'] = false; if ( ! is_user_logged_in() ) { // Verify if the user has Gift Card balance to ask them to verify email on WooPay. $gift_cards = WC_GC()->account->get_active_giftcards( $user->ID ); $balance = 0; foreach ( $gift_cards as $giftcard_data ) { $balance += (float) $giftcard_data->get_balance(); } if ( $balance > 0 ) { $gift_cards_script_data['should_verify_email'] = true; } } return $gift_cards_script_data; } /** * The custom data from plugins to be used on WooPay, * it's not an adapted extension because it doesn't * use the email verification integration. * * @return array The custom data. */ public function get_extension_data() { $extension_data = []; if ( defined( 'WOOCOMMERCE_MULTICURRENCY_VERSION' ) ) { $extension_data['woocommerce-multicurrency'] = [ 'currency' => get_woocommerce_currency(), ]; } if ( $this->is_affiliate_for_woocommerce_enabled() && function_exists( 'afwc_get_referrer_id' ) ) { /** * Suppress psalm warning. * * @psalm-suppress UndefinedFunction */ $extension_data['affiliate-for-woocommerce'] = [ 'affiliate-user' => afwc_get_referrer_id(), ]; } if ( $this->is_automate_woo_referrals_enabled() ) { $advocate_id = $this->get_automate_woo_advocate_id_from_cookie(); $extension_data['automatewoo-referrals'] = [ 'advocate_id' => $advocate_id, ]; } return $extension_data; } /** * Update order extension data after finishing * an order on WooPay, this usually is needed * for extensions which uses cookies when an * order is finished. * * @param int $order_id The successful WooPay order. */ public function update_order_extension_data( $order_id ) { if ( ! empty( $_GET['affiliate'] ) && // phpcs:ignore WordPress.Security.NonceVerification $this->is_affiliate_for_woocommerce_enabled() ) { $affiliate_id = (int) wc_clean( wp_unslash( $_GET['affiliate'] ) ); // phpcs:ignore WordPress.Security.NonceVerification if ( class_exists( '\AFWC_API' ) ) { // phpcs:ignore /** * @psalm-suppress UndefinedClass */ $affiliate_api = \AFWC_API::get_instance(); $affiliate_api->track_conversion( $order_id, $affiliate_id, '', [ 'is_affiliate_eligible' => true ] ); } } } /** * Get WC Blocks registered integrations. * * @param IntegrationInterface $integration An instance of IntegrationInterface. * * @return boolean True means registered successfully. */ public function register( IntegrationInterface $integration ) { $name = $integration->get_name(); if ( self::GIFT_CARDS_BLOCKS === $name || self::POINTS_AND_REWARDS_API === $name ) { $this->registered_integrations[ $name ] = $integration; } return true; } /** * Check if Affiliate for WooCommerce is enabled and * its functions used on WCPay are available. * * @return boolean */ public function is_affiliate_for_woocommerce_enabled() { return defined( 'AFWC_PLUGIN_FILE' ) && function_exists( 'afwc_get_referrer_id' ) && class_exists( 'AFWC_API' ) && method_exists( 'AFWC_API', 'get_instance' ) && method_exists( 'AFWC_API', 'track_conversion' ); } /** * Check if Automate Woo Referrals is enabled and * its functions used on WCPay are available. * * @psalm-suppress UndefinedClass * @psalm-suppress UndefinedFunction * * @return boolean */ private function is_automate_woo_referrals_enabled() { return function_exists( 'AW_Referrals' ) && method_exists( AW_Referrals(), 'options' ) && AW_Referrals()->options()->type === 'link' && class_exists( '\AutomateWoo\Referrals\Referral_Manager' ) && method_exists( \AutomateWoo\Referrals\Referral_Manager::class, 'get_advocate_key_from_cookie' ); } /** * Get AutomateWoo advocate id from cookie. * * @psalm-suppress UndefinedClass * * @return string|null */ private function get_automate_woo_advocate_id_from_cookie() { if ( class_exists( '\AutomateWoo\Referrals\Referral_Manager' ) ) { $advocate_from_key_cookie = \AutomateWoo\Referrals\Referral_Manager::get_advocate_key_from_cookie(); return $advocate_from_key_cookie ? $advocate_from_key_cookie->get_advocate_id() : null; } return null; } }