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 |