Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
81.82% covered (warning)
81.82%
9 / 11
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
Store_Currency
81.82% covered (warning)
81.82%
9 / 11
0.00% covered (danger)
0.00%
0 / 2
7.29
0.00% covered (danger)
0.00%
0 / 1
 init
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 add_script_data
90.00% covered (success)
90.00%
9 / 10
0.00% covered (danger)
0.00%
0 / 1
6.04
1<?php
2/**
3 * The store currency the dashboard formats money in.
4 *
5 * @package automattic/jetpack-woocommerce-stats
6 */
7
8namespace Automattic\Jetpack\WooCommerceStats;
9
10/**
11 * Hands the WooCommerce store currency to the dashboard's money formatter, which otherwise prints USD.
12 *
13 * @since $$next-version$$
14 */
15class Store_Currency {
16
17    /**
18     * Hook the script data filter.
19     *
20     * Priority 20: the dashboard's sync status tracker replaces `premium_analytics` at 10.
21     *
22     * @return void
23     */
24    public static function init() {
25        add_filter( 'jetpack_admin_js_script_data', array( __CLASS__, 'add_script_data' ), 20 );
26    }
27
28    /**
29     * Add the store currency code and symbol as `premium_analytics.store_currency`.
30     *
31     * @param array $data The script data passed by the assets package.
32     * @return array The script data, unchanged without WooCommerce.
33     */
34    public static function add_script_data( $data ) {
35        if ( ! is_array( $data ) || ! function_exists( 'get_woocommerce_currency' ) || ! function_exists( 'get_woocommerce_currency_symbol' ) ) {
36            return $data;
37        }
38
39        if ( ! isset( $data['premium_analytics'] ) || ! is_array( $data['premium_analytics'] ) ) {
40            $data['premium_analytics'] = array();
41        }
42
43        $code = get_woocommerce_currency();
44
45        // Decoded the way WooCommerce's own `wc_currency_settings` does, since symbols are stored as entities.
46        $data['premium_analytics']['store_currency'] = array(
47            'code'   => $code,
48            'symbol' => html_entity_decode( get_woocommerce_currency_symbol( $code ), ENT_QUOTES, 'UTF-8' ),
49        );
50
51        return $data;
52    }
53}