StorefrontIntegration.php
3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
/**
* Class StorefrontIntegration
*
* @package WooCommerce\Payments\StorefrontIntegration
*/
namespace WCPay\MultiCurrency;
defined( 'ABSPATH' ) || exit;
/**
* Class that controls Multi-Currency Storefront Integration.
*/
class StorefrontIntegration {
/**
* Multi-Currency instance.
*
* @var MultiCurrency
*/
protected $multi_currency;
/**
* Constructor.
*
* @param MultiCurrency $multi_currency The MultiCurrency instance.
*/
public function __construct( MultiCurrency $multi_currency ) {
$this->multi_currency = $multi_currency;
$this->init_actions_and_filters();
}
/**
* Adds the CSS to the head of the page.
*
* @return void
*/
public function add_inline_css() {
$css = '
#woocommerce-payments-multi-currency-storefront-widget {
float: right;
}
#woocommerce-payments-multi-currency-storefront-widget form {
margin: 0;
}
';
wp_add_inline_style(
'storefront-style',
apply_filters( MultiCurrency::FILTER_PREFIX . 'storefront_widget_css', $css )
);
}
/**
* This modifies the breadcrumb defaults for us to be able to place the widget.
*
* @param array $defaults The defaults breadcrumb properties.
*
* @return array The modified defaults properties.
*/
public function modify_breadcrumb_defaults( array $defaults ): array {
// Set the instance and args arrays for the widget.
$instance = apply_filters( MultiCurrency::FILTER_PREFIX . 'storefront_widget_instance', [] );
$args = apply_filters(
MultiCurrency::FILTER_PREFIX . 'storefront_widget_args',
[
'before_widget' => '<div id="woocommerce-payments-multi-currency-storefront-widget" class="woocommerce-breadcrumb">',
'after_widget' => '</div>',
]
);
/**
* Some storefront child themes use different wrappers and styles. We need to place the widget before
* the <nav> to display it properly.
*/
$defaults['wrap_before'] = str_replace( '<nav', $this->multi_currency->get_switcher_widget_markup( $instance, $args ) . '<nav', $defaults['wrap_before'] );
return $defaults;
}
/**
* Adds the actions and filters.
*
* @return void
*/
private function init_actions_and_filters() {
// Do not enable the breadcrumb widget if there's only one currency active.
if ( 1 >= count( $this->multi_currency->get_enabled_currencies() ) ) {
return;
}
// Simulation overrides for Multi-Currency onboarding preview.
$simulation_variables = $this->multi_currency->get_multi_currency_onboarding_simulation_variables() ?? [];
$simulation_enabled = false;
$simulation_hide_switcher = false;
if ( 0 < count( $simulation_variables ) && isset( $simulation_variables['enable_storefront_switcher'] ) ) {
// We have a incoming override request! Simulate the flag.
$simulation_enabled = true;
$enable_storefront_switcher = (bool) $simulation_variables['enable_storefront_switcher'];
// If the Storefront switcher is not enabled on the onboarding page, hide it.
if ( ! $enable_storefront_switcher ) {
$simulation_hide_switcher = true;
}
}
// We want this enabled by default, so we default the option to 'yes'.
if ( ! $simulation_hide_switcher
&& (
$simulation_enabled
|| $this->multi_currency->is_using_storefront_switcher()
)
) {
add_filter( 'woocommerce_breadcrumb_defaults', [ $this, 'modify_breadcrumb_defaults' ], 9999 );
add_action( 'wp_enqueue_scripts', [ $this, 'add_inline_css' ], 50 );
}
}
}