Guest User

class-wcml-currency-switcher.php

a guest
Oct 22nd, 2024
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 10.78 KB | None | 0 0
  1. <?php
  2.  
  3. use WCML\MultiCurrency\Settings;
  4. use WCML\MultiCurrency\Geolocation;
  5. use WPML\Core\ISitePress;
  6. use WCML\StandAlone\NullSitePress;
  7.  
  8. /**
  9.  * Class WCML_Currency_Switcher
  10.  *
  11.  * Main class
  12.  */
  13. class WCML_Currency_Switcher {
  14.  
  15.     /** @var woocommerce_wpml $woocommerce_wpml */
  16.     private $woocommerce_wpml;
  17.     /** @var SitePress|NullSitePress $sitepress */
  18.     private $sitepress;
  19.  
  20.     /**
  21.      * @param woocommerce_wpml        $woocommerce_wpml
  22.      * @param SitePress|NullSitePress $sitepress
  23.      */
  24.     public function __construct( woocommerce_wpml $woocommerce_wpml, ISitePress $sitepress ) {
  25.         $this->woocommerce_wpml = $woocommerce_wpml;
  26.         $this->sitepress        = $sitepress;
  27.     }
  28.  
  29.     public function add_hooks() {
  30.         add_action( 'init', [ $this, 'on_init' ], 5 );
  31.     }
  32.  
  33.     public function on_init() {
  34.         add_action( 'wcml_currency_switcher', [ $this, 'wcml_currency_switcher' ] );
  35.         // @deprecated 3.9
  36.         add_action( 'currency_switcher', [ $this, 'currency_switcher' ] );
  37.         add_shortcode( 'currency_switcher', [ $this, 'currency_switcher_shortcode' ] );
  38.         // Built in currency switcher
  39.         add_action( 'woocommerce_product_meta_start', [ $this, 'show_currency_switcher' ] );
  40.         add_action( 'pre_update_option_sidebars_widgets', [ $this, 'update_option_sidebars_widgets' ], 10, 2 );
  41.     }
  42.  
  43.     public static function get_settings( $switcher_id ) {
  44.         global $woocommerce_wpml;
  45.  
  46.         $wcml_settings = $woocommerce_wpml->get_settings();
  47.  
  48.         return isset( $wcml_settings['currency_switchers'][ $switcher_id ] ) ? $wcml_settings['currency_switchers'][ $switcher_id ] : [];
  49.     }
  50.  
  51.     public function currency_switcher_shortcode( $atts ) {
  52.  
  53.         $atts = (array) $atts;
  54.  
  55.         ob_start();
  56.         $this->wcml_currency_switcher( $atts );
  57.         $html = ob_get_contents();
  58.         ob_end_clean();
  59.  
  60.         return $html;
  61.     }
  62.  
  63.     public function wcml_currency_switcher( $args = [] ) {
  64.  
  65.         if ( ! is_array( $args ) ) {
  66.             $args = [];
  67.         }
  68.  
  69.         if ( ! isset( $args['switcher_id'] ) ) {
  70.             $args['switcher_id'] = 'product';
  71.         }
  72.  
  73.         $wcml_settings              = $this->woocommerce_wpml->get_settings();
  74.         $currency_switcher_settings = [];
  75.  
  76.         if ( isset( $wcml_settings['currency_switchers'][ $args['switcher_id'] ] ) ) {
  77.             $currency_switcher_settings = $wcml_settings['currency_switchers'][ $args['switcher_id'] ];
  78.         }
  79.  
  80.         $args = $this->check_and_convert_switcher_style( $args );
  81.  
  82.         if (
  83.             ! isset( $args['preview'] ) &&
  84.             ! isset( $args['switcher_style'] )
  85.         ) {
  86.             $args['switcher_style'] = isset( $currency_switcher_settings['switcher_style'] ) ? $currency_switcher_settings['switcher_style'] : $this->woocommerce_wpml->cs_templates->get_first_active();
  87.         }
  88.  
  89.         if ( ! isset( $args['format'] ) ) {
  90.  
  91.             $args['format'] = '%name% (%symbol%) - %code%';
  92.             if ( isset( $currency_switcher_settings['template'] ) && '' !== $currency_switcher_settings['template'] ) {
  93.                 $args['format'] = apply_filters( 'wpml_translate_single_string', $currency_switcher_settings['template'], 'woocommerce-multilingual', $args['switcher_id'] . '_switcher_format' );
  94.             }
  95.         }
  96.  
  97.         if ( ! isset( $args['color_scheme'] ) ) {
  98.             $args['color_scheme'] = isset( $currency_switcher_settings['color_scheme'] ) ? $currency_switcher_settings['color_scheme'] : [];
  99.         }
  100.  
  101.         $preview                = '';
  102.         $show_currency_switcher = true;
  103.  
  104.         $display_custom_prices = isset( $wcml_settings['display_custom_prices'] ) && $wcml_settings['display_custom_prices'];
  105.         $is_cart_or_checkout   = is_page( wc_get_page_id( 'cart' ) ) || is_page( wc_get_page_id( 'checkout' ) );
  106.  
  107.         if ( $display_custom_prices ) {
  108.             if ( $is_cart_or_checkout ) {
  109.                 $show_currency_switcher = false;
  110.             } elseif ( is_product() ) {
  111.                 $current_product_id = get_the_ID();
  112.        
  113.                 // Get the product object
  114.                 $product = wc_get_product( $current_product_id );
  115.        
  116.                 if ( $product && $product->is_type( 'grouped' ) ) {
  117.                     // The product is a grouped product
  118.                     $child_ids = $product->get_children(); // Get the IDs of the products in the grouped product
  119.                    
  120.                     if ( ! empty( $child_ids ) && is_array( $child_ids ) ) {
  121.                         $show_custom_prices_for_all_children = true;
  122.        
  123.                         foreach ( $child_ids as $child_id ) {
  124.                             $original_child_id = $this->woocommerce_wpml->products->get_original_product_id( $child_id );
  125.                             $use_custom_prices = get_post_meta(
  126.                                 $original_child_id,
  127.                                 '_wcml_custom_prices_status',
  128.                                 true
  129.                             );
  130.        
  131.                             if ( ! $use_custom_prices ) {
  132.                                 $show_custom_prices_for_all_children = false;
  133.                                 break; // If one child product doesn’t have custom prices, stop
  134.                             }
  135.                         }
  136.        
  137.                         if ( ! $show_custom_prices_for_all_children ) {
  138.                             $show_currency_switcher = false;
  139.                         }
  140.                     }
  141.                 } else {
  142.                     // Logic for normal products
  143.                     $original_product_id = $this->woocommerce_wpml->products->get_original_product_id( $current_product_id );
  144.                     $use_custom_prices   = get_post_meta(
  145.                         $original_product_id,
  146.                         '_wcml_custom_prices_status',
  147.                         true
  148.                     );
  149.        
  150.                     if ( ! $use_custom_prices ) {
  151.                         $show_currency_switcher = false;
  152.                     }
  153.                 }
  154.             }
  155.         }
  156.  
  157.         if ( $show_currency_switcher ) {
  158.  
  159.             $currencies = Settings::getOrderedCurrencyCodes();
  160.  
  161.             if ( ! is_admin() ) {
  162.                 $currencies = $this->filter_allowed_currencies_on_frontend( $currencies );
  163.             }
  164.  
  165.             if ( count( $currencies ) > 1 ) {
  166.                 $template = $this->woocommerce_wpml->cs_templates->get_template( $args['switcher_style'] );
  167.  
  168.                 if ( $template ) {
  169.                     $this->woocommerce_wpml->cs_templates->maybe_late_enqueue_template( $args['switcher_style'], $template );
  170.                     $template->set_model( $this->get_model_data( $args, $currencies ) );
  171.                     $preview = $template->get_view();
  172.                 }
  173.             } elseif ( is_admin() ) {
  174.                 $preview = '<i>' . esc_html__( "You haven't added any secondary currencies.", 'woocommerce-multilingual' ) . '</i>';
  175.             }
  176.         }
  177.  
  178.         if ( ! isset( $args['echo'] ) || $args['echo'] ) {
  179.             echo $preview;
  180.         } else {
  181.             return $preview;
  182.         }
  183.     }
  184.  
  185.     /**
  186.      * @param array $currencies
  187.      *
  188.      * @return array
  189.      */
  190.     private function filter_allowed_currencies_on_frontend( $currencies ){
  191.         $ifDisallowedByLanguage = function( $currency ) {
  192.             return ! Settings::isValidCurrencyForLang( $currency, $this->sitepress->get_current_language() );
  193.         };
  194.  
  195.         $ifDisallowedByLocation = function( $currency ) {
  196.             return ! Settings::isValidCurrencyByCountry( $currency, Geolocation::getUserCountry() );
  197.         };
  198.  
  199.         return wpml_collect( $currencies )
  200.             ->reject( Settings::isModeByLanguage() ? $ifDisallowedByLanguage : $ifDisallowedByLocation )
  201.             ->toArray();
  202.     }
  203.  
  204.     public function get_model_data( $args, $currencies ) {
  205.  
  206.         $css_classes = $this->get_css_classes( [ $args['switcher_style'], $args['switcher_id'], 'wcml_currency_switcher' ] );
  207.  
  208.         $format = isset( $args['format'] ) ? $args['format'] : '%name% (%symbol%) - %code%';
  209.  
  210.         $model = [
  211.             'css_classes'       => $css_classes,
  212.             'format'            => $format,
  213.             'currencies'        => $currencies,
  214.             'selected_currency' => $this->woocommerce_wpml->multi_currency->get_client_currency(),
  215.         ];
  216.  
  217.         return $model;
  218.     }
  219.  
  220.     public function get_css_classes( $classes = [] ) {
  221.  
  222.         if ( $this->sitepress->is_rtl( $this->sitepress->get_current_language() ) ) {
  223.             $classes[] = 'wcml-cs-rtl';
  224.         }
  225.  
  226.         $classes = $this->add_user_agent_touch_device_classes( $classes );
  227.         $classes = apply_filters( 'wcml_cs_template_css_classes', $classes );
  228.  
  229.         return implode( ' ', $classes );
  230.     }
  231.  
  232.     public function add_user_agent_touch_device_classes( $classes ) {
  233.        
  234.         if ( wp_is_mobile() ) {
  235.             $classes[] = 'wcml-cs-touch-device';
  236.         }
  237.  
  238.         return $classes;
  239.     }
  240.  
  241.     public function show_currency_switcher() {
  242.         $settings = $this->woocommerce_wpml->get_settings();
  243.  
  244.         if (
  245.             isset( $settings['currency_switcher_product_visibility'] ) &&
  246.             $settings['currency_switcher_product_visibility'] === 1 &&
  247.             is_product()
  248.         ) {
  249.             echo( do_shortcode( '[currency_switcher]' ) );
  250.         }
  251.     }
  252.  
  253.     /**
  254.      * @deprecated 3.9
  255.      */
  256.     public function currency_switcher( $args = [] ) {
  257.  
  258.         $this->wcml_currency_switcher( $args );
  259.     }
  260.  
  261.     /**
  262.      * @return array
  263.      */
  264.     public function get_registered_sidebars() {
  265.         global $wp_registered_sidebars;
  266.  
  267.         return is_array( $wp_registered_sidebars ) ? $wp_registered_sidebars : [];
  268.     }
  269.  
  270.     public function get_available_sidebars() {
  271.         $sidebars      = $this->get_registered_sidebars();
  272.         $wcml_settings = $this->woocommerce_wpml->get_settings();
  273.  
  274.         foreach ( $sidebars as $key => $sidebar ) {
  275.             if ( isset( $wcml_settings['currency_switchers'][ $sidebar['id'] ] ) ) {
  276.                 unset( $sidebars[ $key ] );
  277.             }
  278.         }
  279.  
  280.         return $sidebars;
  281.     }
  282.  
  283.     public function update_option_sidebars_widgets( $sidebars, $old_sidebars ) {
  284.  
  285.         foreach ( $sidebars as $sidebar => $widgets ) {
  286.             if ( 'wp_inactive_widgets' === $sidebar ) {
  287.                 continue;
  288.             }
  289.             $found = false;
  290.             if ( is_array( $widgets ) ) {
  291.                 foreach ( $widgets as $key => $widget_id ) {
  292.                     if ( strpos( $widget_id, WCML_Currency_Switcher_Widget::SLUG ) === 0 ) {
  293.                         if ( $found ) { // Only one CS widget instance per sidebar
  294.                             unset( $sidebars[ $sidebar ][ $key ] );
  295.                             continue;
  296.                         }
  297.                         $found = true;
  298.                     }
  299.                 }
  300.             }
  301.  
  302.             $wcml_settings = $this->woocommerce_wpml->get_settings();
  303.             if ( $found && empty( $wcml_settings['currency_switchers'][ $sidebar ] ) ) {
  304.                 $wcml_settings['currency_switchers'][ $sidebar ] = $this->get_switcher_default_settings();
  305.             } elseif ( ! $found && isset( $wcml_settings['currency_switchers'][ $sidebar ] ) ) {
  306.                 unset( $wcml_settings['currency_switchers'][ $sidebar ] );
  307.             }
  308.  
  309.             $this->woocommerce_wpml->update_settings( $wcml_settings );
  310.         }
  311.  
  312.         return $sidebars;
  313.     }
  314.  
  315.     public function get_switcher_default_settings() {
  316.         return [
  317.             'switcher_style' => 'wcml-dropdown',
  318.             'widget_title'   => '',
  319.             'template'       => '%name% (%symbol%) - %code%',
  320.             'color_scheme'   => [
  321.                 'font_current_normal'       => '',
  322.                 'font_current_hover'        => '',
  323.                 'background_current_normal' => '',
  324.                 'background_current_hover'  => '',
  325.                 'font_other_normal'         => '',
  326.                 'font_other_hover'          => '',
  327.                 'background_other_normal'   => '',
  328.                 'background_other_hover'    => '',
  329.                 'border_normal'             => '',
  330.             ],
  331.         ];
  332.     }
  333.  
  334.     // backward compatibility to convert switcher style for users who uses old parameters wcml-1874
  335.     public function check_and_convert_switcher_style( $args ) {
  336.  
  337.         if ( isset( $args['switcher_style'] ) ) {
  338.             if (
  339.                 'list' === $args['switcher_style'] &&
  340.                 isset( $args['orientation'] )
  341.             ) {
  342.                 if ( 'horizontal' === $args['orientation'] ) {
  343.                     $args['switcher_style'] = 'wcml-horizontal-list';
  344.                 } else {
  345.                     $args['switcher_style'] = 'wcml-vertical-list';
  346.                 }
  347.                 unset( $args['orientation'] );
  348.             } elseif ( 'dropdown' === $args['switcher_style'] ) {
  349.                 $args['switcher_style'] = 'wcml-dropdown';
  350.             }
  351.         }
  352.  
  353.         return $args;
  354.     }
  355.  
  356. }
Advertisement
Add Comment
Please, Sign In to add comment