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 4.7.6 4.7.7 4.7.8 4.7.9 4.8.0 4.9.0 4.9.1 5.0.1 5.0.2 5.1.1 5.1.2 5.1.3 5.2.0 5.2.1 5.3.2 5.3.3.1 5.3.4 5.3.5 5.3.6 5.3.7
woocommerce-multilingual / classes / Multicurrency / Analytics / Hooks.php
woocommerce-multilingual / classes / Multicurrency / Analytics Last commit date
Export.php 1 week ago ExportHPOS.php 1 week ago ExportLegacy.php 1 week ago Factory.php 1 week ago Hooks.php 1 week ago
Hooks.php
184 lines
1 <?php
2
3 namespace WCML\Multicurrency\Analytics;
4
5 use WCML\COT\Helper as COTHelper;
6 use WCML\Orders\Helper as OrdersHelper;
7 use WCML\Utilities\Resources;
8 use WCML\Rest\Functions;
9 use WCML\StandAlone\IStandAloneAction;
10 use WPML\FP\Obj;
11 use WPML\FP\Fns;
12 use WCML\Utilities\WpAdminPages;
13
14 class Hooks implements \IWPML_Action, IStandAloneAction {
15
16 private $woocommerce_wpml;
17
18 private $wpdb;
19
20 private $requestedCurrencyForReport;
21
22 public function __construct( \woocommerce_wpml $woocommerce_wpml, \wpdb $wpdb ) {
23 $this->woocommerce_wpml = $woocommerce_wpml;
24 $this->wpdb = $wpdb;
25 }
26
27 public function add_hooks() {
28 if ( Functions::isAnalyticsPage() ) {
29 add_action( 'admin_enqueue_scripts', [ $this, 'enqueueAssets' ] );
30 }
31
32 wpml_collect(
33 [
34 'revenue',
35 'orders',
36 'orders_stats',
37 'products',
38 'products_stats',
39 'categories',
40 'coupons',
41 'coupons_stats',
42 'taxes',
43 'taxes_stats',
44 'variations',
45 'variations_stats',
46 ]
47 )->each(
48 function( $item ) {
49 add_filter( "woocommerce_analytics_{$item}_query_args", [ $this, 'addCurrencyArg' ] );
50 }
51 );
52
53 wpml_collect(
54 [
55 'products',
56 'orders',
57 'variations',
58 'categories',
59 'coupons',
60 'taxes',
61 ]
62 )->each(
63 function( $item ) {
64 add_filter( "woocommerce_analytics_clauses_where_{$item}_subquery", [ $this, 'addWhere' ] );
65 add_filter( "woocommerce_analytics_clauses_where_{$item}_stats_total", [ $this, 'addWhere' ] );
66 add_filter( "woocommerce_analytics_clauses_where_{$item}_stats_interval", [ $this, 'addWhere' ] );
67 }
68 );
69
70 add_action( 'woocommerce_admin_report_export', [ $this, 'saveRequestedCurrencyForReport' ], -PHP_INT_MAX, 4 );
71 }
72
73 public function enqueueAssets() {
74 $multiCurrency = $this->woocommerce_wpml->get_multi_currency();
75 $currencies = $multiCurrency->get_currency_codes();
76 $enqueue = Resources::enqueueApp( 'multicurrencyAnalytics' );
77
78 $enqueue(
79 [
80 'name' => 'wcmlAnalytics',
81 'data' => [
82 'strings' => [
83 'currencyLabel' => __( 'Currency', 'woocommerce-multilingual' ),
84 ],
85 'filterItems' => $this->getCurrencyFilterItems( $currencies ),
86 'currencyConfigs' => $this->getCurrencyConfigs( $currencies, $multiCurrency ),
87 ],
88 ]
89 );
90
91 $source = WCML_PLUGIN_URL . '/dist/js/multicurrencyAnalytics/app.js?ver=' . WCML_VERSION;
92 echo '<link rel="preload" href="' . esc_url( $source ) . '" as="script" />', "\n";
93 }
94
95 private function getCurrencyFilterItems( $currencies ) {
96 $currencyLabels = get_woocommerce_currencies();
97
98 return wpml_collect( $currencies )
99 ->map(
100 function( $currency ) use ( $currencyLabels ) {
101 return [
102 'value' => $currency,
103 'label' => $currencyLabels[ $currency ],
104 ];
105 }
106 )->toArray();
107 }
108
109 private function getCurrencyConfigs( $currencies, $multiCurrency ) {
110 return wpml_collect( $currencies )
111 ->mapWithKeys(
112 function( $currency ) use ( $multiCurrency ) {
113 $get = Obj::prop( Fns::__, $multiCurrency->get_currency_details_by_code( $currency ) );
114
115 return [
116 $currency => [
117 'code' => $currency,
118 'symbol' => html_entity_decode( get_woocommerce_currency_symbol( $currency ) ),
119 'symbolPosition' => $get( 'position' ),
120 'thousandSeparator' => $get( 'thousand_sep' ),
121 'decimalSeparator' => $get( 'decimal_sep' ),
122 'precision' => $get( 'num_decimals' ),
123 ],
124 ];
125 }
126 )->toArray();
127 }
128
129 public function addCurrencyArg( $args ) {
130 return Obj::assoc( 'currency', $this->getCurrency(), $args );
131 }
132
133 public function addWhere( $clauses ) {
134 if ( COTHelper::isUsageEnabled() ) {
135 $orderTable = COTHelper::getTableName();
136
137 $clauses[] = $this->wpdb->prepare(
138 "AND EXISTS (
139 SELECT 1 FROM {$orderTable}
140 WHERE id = {$this->wpdb->prefix}wc_order_stats.order_id
141 AND currency = %s
142 )",
143 $this->getCurrency()
144 );
145 } else {
146 $clauses[] = $this->wpdb->prepare(
147 "AND EXISTS (
148 SELECT 1 FROM {$this->wpdb->postmeta}
149 WHERE post_id = {$this->wpdb->prefix}wc_order_stats.order_id
150 AND meta_key = '" . OrdersHelper::KEY_LEGACY_CURRENCY . "'
151 AND meta_value = %s
152 )",
153 $this->getCurrency()
154 );
155 }
156
157 return $clauses;
158 }
159
160 public function saveRequestedCurrencyForReport( $page, $id, $type, $args ) {
161 if ( 'orders' === $type && isset( $args['currency'] ) ) {
162 $this->requestedCurrencyForReport = $args['currency'];
163 }
164 }
165
166 private function getCurrency() {
167 if ( ! empty( $this->requestedCurrencyForReport ) ) {
168 return $this->requestedCurrencyForReport;
169 }
170
171 $mustUseDashboardCookie = WpAdminPages::isDashboard()
172 || \WCML_Admin_Currency_Selector::isDashboardWidgetRequest();
173
174 $rawCurrency = $mustUseDashboardCookie
175 ? Obj::prop( '_wcml_dashboard_currency', $_COOKIE )
176 : Obj::prop( 'currency', $_GET );
177
178 return $rawCurrency
179 ? sanitize_text_field( wp_unslash( $rawCurrency ) )
180 : wcml_get_woocommerce_currency_option();
181 }
182
183 }
184