/var/www/html/wp-content/plugins/woocommerce/includes/admin/class-wc-admin-reports.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<?php
/**
 * Admin Reports
 *
 * Functions used for displaying sales and customer reports in admin.
 *
 * @author      WooThemes
 * @category    Admin
 * @package     WooCommerce\Admin\Reports
 * @version     2.0.0
 */

if ( ! defined'ABSPATH' ) ) {
    exit;
}

if ( 
class_exists'WC_Admin_Reports'false ) ) {
    return;
}

/**
 * WC_Admin_Reports Class.
 */
class WC_Admin_Reports {

    
/**
     * Register the proper hook handlers.
     */
    
public static function register_hook_handlers() {
        
add_filter'woocommerce_after_dashboard_status_widget_parameter', array( __CLASS__'get_report_instance' ) );
        
add_filter'woocommerce_dashboard_status_widget_reports', array( __CLASS__'replace_dashboard_status_widget_reports' ) );
    }

    
/**
     * Get an instance of WC_Admin_Report.
     *
     * @return WC_Admin_Report
     */
    
public static function get_report_instance() {
        include_once 
__DIR__ '/reports/class-wc-admin-report.php';
        return new 
WC_Admin_Report();
    }

    
/**
     * Filter handler for replacing the data of the status widget on the Dashboard page.
     *
     * @param array $status_widget_reports The data to display in the status widget.
     */
    
public static function replace_dashboard_status_widget_reports$status_widget_reports ) {
        
$report self::get_report_instance();

        include_once 
__DIR__ '/reports/class-wc-report-sales-by-date.php';

        
$sales_by_date                 = new WC_Report_Sales_By_Date();
        
$sales_by_date->start_date     strtotimegmdate'Y-m-01'current_time'timestamp' ) ) ); // phpcs:ignore WordPress.DateTime.CurrentTimeTimestamp.Requested
        
$sales_by_date->end_date       strtotimegmdate'Y-m-d'current_time'timestamp' ) ) ); // phpcs:ignore WordPress.DateTime.CurrentTimeTimestamp.Requested
        
$sales_by_date->chart_groupby  'day';
        
$sales_by_date->group_by_query 'YEAR(posts.post_date), MONTH(posts.post_date), DAY(posts.post_date)';

        
$status_widget_reports['net_sales_link']      = 'admin.php?page=wc-reports&tab=orders&range=month';
        
$status_widget_reports['top_seller_link']     = 'admin.php?page=wc-reports&tab=orders&report=sales_by_product&range=month&product_ids=';
        
$status_widget_reports['lowstock_link']       = 'admin.php?page=wc-reports&tab=stock&report=low_in_stock';
        
$status_widget_reports['outofstock_link']     = 'admin.php?page=wc-reports&tab=stock&report=out_of_stock';
        
$status_widget_reports['report_data']         = $sales_by_date->get_report_data();
        
$status_widget_reports['get_sales_sparkline'] = array( $report'get_sales_sparkline' );

        return 
$status_widget_reports;
    }

    
/**
     * Handles output of the reports page in admin.
     */
    
public static function output() {
        
$reports        self::get_reports();
        
$first_tab      array_keys$reports );
        
$current_tab    = ! empty( $_GET['tab'] ) && array_key_exists$_GET['tab'], $reports ) ? sanitize_title$_GET['tab'] ) : $first_tab[0];
        
$current_report = isset( $_GET['report'] ) ? sanitize_title$_GET['report'] ) : currentarray_keys$reports$current_tab ]['reports'] ) );

        include_once 
dirname__FILE__ ) . '/reports/class-wc-admin-report.php';
        include_once 
dirname__FILE__ ) . '/views/html-admin-page-reports.php';
    }

    
/**
     * Returns the definitions for the reports to show in admin.
     *
     * @return array
     */
    
public static function get_reports() {
        
$reports = array(
            
'orders'    => array(
                
'title'   => __'Orders''woocommerce' ),
                
'reports' => array(
                    
'sales_by_date'     => array(
                        
'title'       => __'Sales by date''woocommerce' ),
                        
'description' => '',
                        
'hide_title'  => true,
                        
'callback'    => array( __CLASS__'get_report' ),
                    ),
                    
'sales_by_product'  => array(
                        
'title'       => __'Sales by product''woocommerce' ),
                        
'description' => '',
                        
'hide_title'  => true,
                        
'callback'    => array( __CLASS__'get_report' ),
                    ),
                    
'sales_by_category' => array(
                        
'title'       => __'Sales by category''woocommerce' ),
                        
'description' => '',
                        
'hide_title'  => true,
                        
'callback'    => array( __CLASS__'get_report' ),
                    ),
                    
'coupon_usage'      => array(
                        
'title'       => __'Coupons by date''woocommerce' ),
                        
'description' => '',
                        
'hide_title'  => true,
                        
'callback'    => array( __CLASS__'get_report' ),
                    ),
                    
'downloads'         => array(
                        
'title'       => __'Customer downloads''woocommerce' ),
                        
'description' => '',
                        
'hide_title'  => true,
                        
'callback'    => array( __CLASS__'get_report' ),
                    ),
                ),
            ),
            
'customers' => array(
                
'title'   => __'Customers''woocommerce' ),
                
'reports' => array(
                    
'customers'     => array(
                        
'title'       => __'Customers vs. guests''woocommerce' ),
                        
'description' => '',
                        
'hide_title'  => true,
                        
'callback'    => array( __CLASS__'get_report' ),
                    ),
                    
'customer_list' => array(
                        
'title'       => __'Customer list''woocommerce' ),
                        
'description' => '',
                        
'hide_title'  => true,
                        
'callback'    => array( __CLASS__'get_report' ),
                    ),
                ),
            ),
            
'stock'     => array(
                
'title'   => __'Stock''woocommerce' ),
                
'reports' => array(
                    
'low_in_stock' => array(
                        
'title'       => __'Low in stock''woocommerce' ),
                        
'description' => '',
                        
'hide_title'  => true,
                        
'callback'    => array( __CLASS__'get_report' ),
                    ),
                    
'out_of_stock' => array(
                        
'title'       => __'Out of stock''woocommerce' ),
                        
'description' => '',
                        
'hide_title'  => true,
                        
'callback'    => array( __CLASS__'get_report' ),
                    ),
                    
'most_stocked' => array(
                        
'title'       => __'Most stocked''woocommerce' ),
                        
'description' => '',
                        
'hide_title'  => true,
                        
'callback'    => array( __CLASS__'get_report' ),
                    ),
                ),
            ),
        );

        if ( 
wc_tax_enabled() ) {
            
$reports['taxes'] = array(
                
'title'   => __'Taxes''woocommerce' ),
                
'reports' => array(
                    
'taxes_by_code' => array(
                        
'title'       => __'Taxes by code''woocommerce' ),
                        
'description' => '',
                        
'hide_title'  => true,
                        
'callback'    => array( __CLASS__'get_report' ),
                    ),
                    
'taxes_by_date' => array(
                        
'title'       => __'Taxes by date''woocommerce' ),
                        
'description' => '',
                        
'hide_title'  => true,
                        
'callback'    => array( __CLASS__'get_report' ),
                    ),
                ),
            );
        }

        
/**
         * Filter the list and add reports to the legacy _WooCommerce > Reports_.
         *
         * Array items should be in the format of
         *
         * $reports['automatewoo'] = array(
         *     'title'   => 'AutomateWoo',
         *     'reports' => array(
         *         'runs_by_date' => array(
         *             'title'       => __( 'Workflow Runs', 'automatewoo' ),
         *             'description' => '',
         *             'hide_title'  => false,
         *             'callback'    => array( $this, 'get_runs_by_date' ),
         *         ),
         *         // ...
         *     ),
         * );
         *
         * This filter has a colliding name with the one in Automattic\WooCommerce\Admin\API\Reports\Controller.
         * To make sure your code runs in the context of the legacy _WooCommerce > Reports_ screen, and not the REST endpoint,
         * use the following:
         *
         * add_filter( 'woocommerce_admin_reports',
         *     function( $reports ) {
         *         if ( is_admin() ) {
         *             // ...
         *
         * @param array $reports The associative array of reports.
         */
        
$reports apply_filters'woocommerce_admin_reports'$reports );
        
$reports apply_filters'woocommerce_reports_charts'$reports ); // Backwards compatibility.

        
foreach ( $reports as $key => &$report_group ) {
            if ( isset( 
$report_group['charts'] ) ) {
                
$report_group['reports'] = $report_group['charts'];
            }

            
// Silently ignore reports given for the filter in Automattic\WooCommerce\Admin\API\Reports\Controller.
            
if ( ! isset( $report_group['reports'] ) ) {
                unset( 
$reports$key ] );
                continue;
            }

            foreach ( 
$report_group['reports'] as &$report ) {
                if ( isset( 
$report['function'] ) ) {
                    
$report['callback'] = $report['function'];
                }
            }
        }

        return 
$reports;
    }

    
/**
     * Get a report from our reports subfolder.
     *
     * @param string $name
     */
    
public static function get_report$name ) {
        
$name  sanitize_titlestr_replace'_''-'$name ) );
        
$class 'WC_Report_' str_replace'-''_'$name );

        include_once 
apply_filters'wc_admin_reports_path''reports/class-wc-report-' $name '.php'$name$class );

        if ( ! 
class_exists$class ) ) {
            return;
        }

        
$report = new $class();
        
$report->output_report();
    }
}