/var/www/html_us/wp-content/plugins/woocommerce/src/Admin/API/Reports/Import/Controller.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
<?php
/**
 * REST API Reports Import Controller
 *
 * Handles requests to /reports/import
 */

namespace Automattic\WooCommerce\Admin\API\Reports\Import;

defined'ABSPATH' ) || exit;

use 
Automattic\WooCommerce\Admin\ReportsSync;

/**
 * Reports Imports controller.
 *
 * @internal
 * @extends \Automattic\WooCommerce\Admin\API\Reports\Controller
 */
class Controller extends \Automattic\WooCommerce\Admin\API\Reports\Controller {

    
/**
     * Route base.
     *
     * @var string
     */
    
protected $rest_base 'reports/import';

    
/**
     * Register routes.
     */
    
public function register_routes() {
        
register_rest_route(
            
$this->namespace,
            
'/' $this->rest_base,
            array(
                array(
                    
'methods'             => \WP_REST_Server::EDITABLE,
                    
'callback'            => array( $this'import_items' ),
                    
'permission_callback' => array( $this'import_permissions_check' ),
                    
'args'                => $this->get_import_collection_params(),
                ),
                
'schema' => array( $this'get_import_public_schema' ),
            )
        );
        
register_rest_route(
            
$this->namespace,
            
'/' $this->rest_base '/cancel',
            array(
                array(
                    
'methods'             => \WP_REST_Server::EDITABLE,
                    
'callback'            => array( $this'cancel_import' ),
                    
'permission_callback' => array( $this'import_permissions_check' ),
                ),
                
'schema' => array( $this'get_import_public_schema' ),
            )
        );
        
register_rest_route(
            
$this->namespace,
            
'/' $this->rest_base '/delete',
            array(
                array(
                    
'methods'             => \WP_REST_Server::EDITABLE,
                    
'callback'            => array( $this'delete_imported_items' ),
                    
'permission_callback' => array( $this'import_permissions_check' ),
                ),
                
'schema' => array( $this'get_import_public_schema' ),
            )
        );
        
register_rest_route(
            
$this->namespace,
            
'/' $this->rest_base '/status',
            array(
                array(
                    
'methods'             => \WP_REST_Server::READABLE,
                    
'callback'            => array( $this'get_import_status' ),
                    
'permission_callback' => array( $this'import_permissions_check' ),
                ),
                
'schema' => array( $this'get_import_public_schema' ),
            )
        );
        
register_rest_route(
            
$this->namespace,
            
'/' $this->rest_base '/totals',
            array(
                array(
                    
'methods'             => \WP_REST_Server::READABLE,
                    
'callback'            => array( $this'get_import_totals' ),
                    
'permission_callback' => array( $this'import_permissions_check' ),
                    
'args'                => $this->get_import_collection_params(),
                ),
                
'schema' => array( $this'get_import_public_schema' ),
            )
        );
    }

    
/**
     * Makes sure the current user has access to WRITE the settings APIs.
     *
     * @param WP_REST_Request $request Full data about the request.
     * @return WP_Error|bool
     */
    
public function import_permissions_check$request ) {
        if ( ! 
wc_rest_check_manager_permissions'settings''edit' ) ) {
            return new 
\WP_Error'woocommerce_rest_cannot_edit'__'Sorry, you cannot edit this resource.''woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
        }
        return 
true;
    }

    
/**
     * Import data based on user request params.
     *
     * @param  WP_REST_Request $request Request data.
     * @return WP_Error|WP_REST_Response
     */
    
public function import_items$request ) {
        
$query_args $this->prepare_objects_query$request );
        
$import     ReportsSync::regenerate_report_data$query_args['days'], $query_args['skip_existing'] );

        if ( 
is_wp_error$import ) ) {
            
$result = array(
                
'status'  => 'error',
                
'message' => $import->get_error_message(),
            );
        } else {
            
$result = array(
                
'status'  => 'success',
                
'message' => $import,
            );
        }

        
$response $this->prepare_item_for_response$result$request );
        
$data     $this->prepare_response_for_collection$response );

        return 
rest_ensure_response$data );
    }

    
/**
     * Prepare request object as query args.
     *
     * @param WP_REST_Request $request Request data.
     * @return array
     */
    
protected function prepare_objects_query$request ) {
        
$args                  = array();
        
$args['skip_existing'] = $request['skip_existing'];
        
$args['days']          = $request['days'];

        return 
$args;
    }

    
/**
     * Prepare the data object for response.
     *
     * @param object          $item Data object.
     * @param WP_REST_Request $request Request object.
     * @return WP_REST_Response $response Response data.
     */
    
public function prepare_item_for_response$item$request ) {
        
$data     $this->add_additional_fields_to_object$item$request );
        
$data     $this->filter_response_by_context$data'view' );
        
$response rest_ensure_response$data );

        
/**
         * Filter the list returned from the API.
         *
         * @param WP_REST_Response $response The response object.
         * @param array            $item     The original item.
         * @param WP_REST_Request  $request  Request used to generate the response.
         */
        
return apply_filters'woocommerce_rest_prepare_reports_import'$response$item$request );
    }

    
/**
     * Get the query params for collections.
     *
     * @return array
     */
    
public function get_import_collection_params() {
        
$params                  = array();
        
$params['days']          = array(
            
'description'       => __'Number of days to import.''woocommerce' ),
            
'type'              => 'integer',
            
'sanitize_callback' => 'absint',
            
'validate_callback' => 'rest_validate_request_arg',
            
'minimum'           => 0,
        );
        
$params['skip_existing'] = array(
            
'description'       => __'Skip importing existing order data.''woocommerce' ),
            
'type'              => 'boolean',
            
'default'           => false,
            
'sanitize_callback' => 'wc_string_to_bool',
            
'validate_callback' => 'rest_validate_request_arg',
        );
        return 
$params;
    }

    
/**
     * Get the Report's schema, conforming to JSON Schema.
     *
     * @return array
     */
    
public function get_import_public_schema() {
        
$schema = array(
            
'$schema'    => 'http://json-schema.org/draft-04/schema#',
            
'title'      => 'report_import',
            
'type'       => 'object',
            
'properties' => array(
                
'status'  => array(
                    
'description' => __'Regeneration status.''woocommerce' ),
                    
'type'        => 'string',
                    
'context'     => array( 'view''edit' ),
                    
'readonly'    => true,
                ),
                
'message' => array(
                    
'description' => __'Regenerate data message.''woocommerce' ),
                    
'type'        => 'string',
                    
'context'     => array( 'view''edit' ),
                    
'readonly'    => true,
                ),
            ),
        );

        return 
$this->add_additional_fields_schema$schema );
    }

    
/**
     * Cancel all queued import actions.
     *
     * @param  WP_REST_Request $request Request data.
     * @return WP_Error|WP_REST_Response
     */
    
public function cancel_import$request ) {
        
ReportsSync::clear_queued_actions();

        
$result = array(
            
'status'  => 'success',
            
'message' => __'All pending and in-progress import actions have been cancelled.''woocommerce' ),
        );

        
$response $this->prepare_item_for_response$result$request );
        
$data     $this->prepare_response_for_collection$response );

        return 
rest_ensure_response$data );
    }

    
/**
     * Delete all imported items.
     *
     * @param  WP_REST_Request $request Request data.
     * @return WP_Error|WP_REST_Response
     */
    
public function delete_imported_items$request ) {
        
$delete ReportsSync::delete_report_data();

        if ( 
is_wp_error$delete ) ) {
            
$result = array(
                
'status'  => 'error',
                
'message' => $delete->get_error_message(),
            );
        } else {
            
$result = array(
                
'status'  => 'success',
                
'message' => $delete,
            );
        }

        
$response $this->prepare_item_for_response$result$request );
        
$data     $this->prepare_response_for_collection$response );

        return 
rest_ensure_response$data );
    }

    
/**
     * Get the status of the current import.
     *
     * @param  WP_REST_Request $request Request data.
     * @return WP_Error|WP_REST_Response
     */
    
public function get_import_status$request ) {
        
$result   ReportsSync::get_import_stats();
        
$response $this->prepare_item_for_response$result$request );
        
$data     $this->prepare_response_for_collection$response );

        return 
rest_ensure_response$data );
    }

    
/**
     * Get the total orders and customers based on user supplied params.
     *
     * @param  WP_REST_Request $request Request data.
     * @return WP_Error|WP_REST_Response
     */
    
public function get_import_totals$request ) {
        
$query_args $this->prepare_objects_query$request );
        
$totals     ReportsSync::get_import_totals$query_args['days'], $query_args['skip_existing'] );

        
$response $this->prepare_item_for_response$totals$request );
        
$data     $this->prepare_response_for_collection$response );

        return 
rest_ensure_response$data );
    }
}