Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
76.92% covered (warning)
76.92%
10 / 13
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
Default_Reports
76.92% covered (warning)
76.92%
10 / 13
0.00% covered (danger)
0.00%
0 / 1
4.20
0.00% covered (danger)
0.00%
0 / 1
 register
76.92% covered (warning)
76.92%
10 / 13
0.00% covered (danger)
0.00%
0 / 1
4.20
1<?php
2/**
3 * The package's own reports, handlers and providers.
4 *
5 * @package automattic/jetpack-woocommerce-stats
6 */
7
8namespace Automattic\Jetpack\WooCommerceStats\Report_Execution;
9
10use Automattic\Jetpack\WooCommerceStats\Analytics_Dashboard;
11use Automattic\Jetpack\WooCommerceStats\Api_Proxy_Controller;
12
13/**
14 * Registers the reports WordPress.com serves, and on a WooCommerce site the local handler of
15 * `orders.by_date`, on `Report_Registry::REGISTER_ACTION`.
16 *
17 * @since 0.3.3
18 */
19class Default_Reports {
20
21    /**
22     * Summary fields of `orders.by_date`, from WordPress.com's orders data store. The averages are null without orders.
23     */
24    private const ORDERS_BY_DATE_FIELDS = array(
25        'orders_no'                => 'number',
26        'orders_value_net'         => 'currency',
27        'orders_value_gross'       => 'currency',
28        'total_sales'              => 'currency',
29        'average_order_value'      => 'currency',
30        'avg_items'                => 'average',
31        'coupons'                  => 'currency',
32        'refunds'                  => 'currency',
33        'total_items'              => 'number',
34        'total_items_refunded'     => 'number',
35        'total_orders_with_refund' => 'number',
36        'paid_orders_count'        => 'number',
37        'paid_net_sales'           => 'currency',
38        'unpaid_orders_count'      => 'number',
39        'unpaid_net_sales'         => 'currency',
40        'product_net_revenue'      => 'currency',
41        'cogs_amount'              => 'currency',
42        'profit_margin'            => 'currency',
43    );
44
45    /**
46     * Summary fields of `sessions.by_device`, from WordPress.com's sessions data store.
47     */
48    private const SESSIONS_BY_DEVICE_FIELDS = array(
49        'active_sessions' => 'number',
50        'total_orders'    => 'number',
51    );
52
53    /**
54     * What an orders report counts, and by which calendar.
55     */
56    private const ORDER_SEMANTICS = array(
57        'population' => 'analytics_statuses',
58        'week_start' => 'site',
59        'timezone'   => 'site',
60    );
61
62    /**
63     * Reports WordPress.com serves, by id => route pattern.
64     */
65    private const ROUTES = array(
66        'bookings.by_date'                     => 'bookings/by-date',
67        'coupons'                              => 'coupons',
68        'coupons.by_date'                      => 'coupons/by-date',
69        'customers.by_date'                    => 'customers/by-date',
70        'customers.new_returning'              => 'customers/new-returning',
71        'order_attribution.compare'            => 'order-attribution/[\w-]+/compare',
72        'order_attribution.items'              => 'order-attribution/[\w-]+/items',
73        'order_attribution.summary'            => 'order-attribution/[\w-]+/summary',
74        'order_attribution_by_product.summary' => 'order-attribution-by-product/[\w-]+/summary',
75        'orders.by_date'                       => 'orders/by-date',
76        'orders_by_product_type.by_date'       => 'orders-by-product-type/by-date',
77        'products'                             => 'products',
78        'sessions.by_conversion_rate'          => 'sessions/by-conversion-rate',
79        'sessions.by_date'                     => 'sessions/by-date',
80        'sessions.by_device'                   => 'sessions/by-device',
81        'sessions.by_location'                 => 'sessions/by-location',
82        'taxes.breakdown'                      => 'taxes/breakdown',
83        'taxes.by_date'                        => 'taxes/by-date',
84    );
85
86    /**
87     * Fill the registry.
88     *
89     * @param mixed $registry The registry being filled.
90     * @return void
91     */
92    public static function register( $registry ) {
93        if ( ! $registry instanceof Report_Registry ) {
94            return;
95        }
96
97        $registry->register_provider( 'wpcom', new Wpcom_Report_Provider() );
98        $registry->register_handler( Report_Registry::ANY_REPORT, 'wpcom', new Wpcom_Forward_Handler( new Api_Proxy_Controller() ) );
99        if ( Analytics_Dashboard::is_woocommerce_available() ) {
100            $registry->register_provider( 'local', new Local_Report_Provider() );
101            $registry->register_handler( 'orders.by_date', 'local', new Core_Revenue_Stats_Handler() );
102        }
103
104        $contracts = array(
105            'orders.by_date'     => new Report_Definition( 'orders.by_date', 1, array( self::ROUTES['orders.by_date'] ), self::ORDERS_BY_DATE_FIELDS, array( 'orders' ), self::ORDER_SEMANTICS ),
106            'sessions.by_device' => new Report_Definition( 'sessions.by_device', 1, array( self::ROUTES['sessions.by_device'] ), self::SESSIONS_BY_DEVICE_FIELDS, array( 'sessions' ), array( 'timezone' => 'site' ) ),
107        );
108
109        foreach ( self::ROUTES as $id => $route ) {
110            $registry->register_report( $contracts[ $id ] ?? new Report_Definition( $id, 0, array( $route ) ) );
111        }
112    }
113}