get_current_currency(); return $args; } /** * add_reports_currency_to_admin_bar. * * @version 2.13.0 * @since 1.0.0 */ public function add_reports_currency_to_admin_bar( $wp_admin_bar ) { $the_current_code = $this->get_current_currency(); $parent = 'reports_currency_select'; $args = array( 'parent' => false, 'id' => $parent, 'title' => __( 'Reports currency:', 'currency-switcher-woocommerce' ) . ' ' . $the_current_code, 'href' => false, 'meta' => array( 'title' => __( 'Show reports only in', 'currency-switcher-woocommerce' ) . ' ' . $the_current_code, ), ); $wp_admin_bar->add_node( $args ); $currency_symbols = array(); $currency_symbols[ $the_current_code ] = $the_current_code; foreach ( alg_get_enabled_currencies() as $currency ) { $currency_symbols[ $currency ] = $currency; } sort( $currency_symbols ); // $currency_symbols['merge'] = 'merge'; foreach ( $currency_symbols as $code ) { $args = array( 'parent' => $parent, 'id' => $parent . '_' . $code, 'title' => $code, 'href' => add_query_arg( 'currency', $code ), 'meta' => array( 'title' => __( 'Show reports only in', 'currency-switcher-woocommerce' ) . ' ' . $code, ), ); $wp_admin_bar->add_node( $args ); } } /** * change_currency_code_reports. * * @version 1.0.0 * @since 1.0.0 */ public function change_currency_code_reports( $currency ) { if ( isset( $_GET['page'] ) && 'wc-reports' === $_GET['page'] ) { if ( isset( $_GET['currency'] ) ) { return ( 'merge' === $_GET['currency'] ) ? '' : $_GET['currency']; } } return $currency; } /** * enqueue_script. * * @version 2.13.0 * @since 2.13.0 */ public function enqueue_script() { $the_current_code = $this->get_current_currency(); $currency_codes = array(); $currency_codes[ $the_current_code ] = $the_current_code; foreach ( alg_get_enabled_currencies() as $currency ) { $currency_codes[ $currency ] = $currency; } $currencies = array(); foreach ( $currency_codes as $currency_code ) { $currencies[] = array( 'label' => $currency_code, 'value' => $currency_code, ); } wp_enqueue_script( 'wpw-currency-switcher-analytics', plugin_dir_url( __FILE__ ) . 'js/analytics.min.js', array( 'jquery' ), alg_wc_currency_switcher_plugin()->version, true ); wp_localize_script( 'wpw-currency-switcher-analytics', 'wpw_currency_switcher', array( 'i18n' => array( 'Currency' => __( 'Currency', 'woocommerce' ), 'reports_currency' => __( 'Reports currency:', 'currency-switcher-woocommerce' ), 'show_reports_in' => __( 'Show reports only in', 'currency-switcher-woocommerce' ), ), 'currencies' => $currencies, ) ); } /** * Add currency to the JOIN clause. * * @param string[] $clauses The array of clauses. * @param string $context Unused. * * @return array * * @version 2.13.0 * @since 2.13.0 */ public function filter_clauses_join( $clauses, $context ) { global $wpdb; $clauses[] = "JOIN {$wpdb->postmeta} currency_postmeta ON {$wpdb->prefix}wc_order_stats.order_id = currency_postmeta.post_id"; return $clauses; } /** * Add currency to the WHERE clause. * * @param string[] $clauses The array of clauses. * @param string $context Unused. * * @return array * * @version 2.13.0 * @since 2.13.0 */ public function filter_clauses_where( $clauses, $context ) { $currency = $this->get_current_currency(); $clauses[] = "AND currency_postmeta.meta_key = '_order_currency' AND currency_postmeta.meta_value = '{$currency}'"; return $clauses; } /** * Add currency to the SELECT clause. * * @param string[] $clauses The array of clauses. * @param string $context Unused. * * @return array * * @version 2.13.0 * @since 2.13.0 */ public function filter_clauses_select( $clauses, $context ) { $clauses[] = ', currency_postmeta.meta_value AS currency'; return $clauses; } /** * filter_reports. * * @version 2.15.1 * @since 1.0.0 */ public function filter_reports( $query ) { global $wpdb; if ( isset( $_GET['currency'] ) && 'merge' === $_GET['currency'] ) { return $query; } $report_currency = isset( $_GET['currency'] ) ? sanitize_text_field( $_GET['currency'] ) : get_option( 'woocommerce_currency' ); if ( ! isset( $query['join'] ) ) { $query['join'] = ''; } $query['join'] .= " INNER JOIN {$wpdb->postmeta} AS wpw_order_currency ON posts.ID = wpw_order_currency.post_id"; if ( ! isset( $query['where'] ) ) { $query['where'] = ''; } $query['where'] .= " AND ( wpw_order_currency.meta_key = '_order_currency' AND wpw_order_currency.meta_value = '$report_currency' )"; return $query; } /** * Return the current currency from $_GET or return store default. * * @return string * * @version 2.13.0 * @since 2.13.0 */ protected function get_current_currency() { $default_currency = get_option( 'woocommerce_currency' ); if ( ! empty( $_GET['currency'] ) ) { $currency = sanitize_text_field( wp_unslash( $_GET['currency'] ) ); } else { $currency = $default_currency; } return $currency; } } endif; return new Alg_Currency_Switcher_Currency_Reports();