2018-09-17 14:32:03 +00:00
|
|
|
<?php
|
2018-09-18 19:41:45 +00:00
|
|
|
/**
|
|
|
|
* REST API bootstrap.
|
|
|
|
*
|
|
|
|
* @package WooCommerce Admin/Classes
|
|
|
|
*/
|
2018-09-17 14:32:03 +00:00
|
|
|
|
2018-09-18 19:41:45 +00:00
|
|
|
defined( 'ABSPATH' ) || exit;
|
2018-09-17 14:32:03 +00:00
|
|
|
|
2018-09-18 19:41:45 +00:00
|
|
|
/**
|
|
|
|
* WC_Admin_Api_Init class.
|
|
|
|
*/
|
2018-09-17 14:32:03 +00:00
|
|
|
class WC_Admin_Api_Init {
|
|
|
|
|
2019-01-11 18:31:10 +00:00
|
|
|
/**
|
|
|
|
* Action hook for reducing a range of batches down to single actions.
|
|
|
|
*/
|
|
|
|
const QUEUE_BATCH_ACTION = 'wc-admin_queue_batches';
|
|
|
|
|
2019-01-11 19:22:31 +00:00
|
|
|
/**
|
|
|
|
* Action hook for queuing an action after another is complete.
|
|
|
|
*/
|
|
|
|
const QUEUE_DEPEDENT_ACTION = 'wc-admin_queue_dependent_action';
|
|
|
|
|
2019-01-11 18:31:10 +00:00
|
|
|
/**
|
|
|
|
* Action hook for processing a batch of customers.
|
|
|
|
*/
|
|
|
|
const CUSTOMERS_BATCH_ACTION = 'wc-admin_process_customers_batch';
|
|
|
|
|
2019-01-11 18:32:37 +00:00
|
|
|
/**
|
|
|
|
* Action hook for processing a batch of orders.
|
|
|
|
*/
|
|
|
|
const ORDERS_BATCH_ACTION = 'wc-admin_process_orders_batch';
|
|
|
|
|
2019-01-11 19:22:31 +00:00
|
|
|
/**
|
|
|
|
* Action hook for initializing the orders lookup batch creation.
|
|
|
|
*/
|
|
|
|
const ORDERS_LOOKUP_BATCH_INIT = 'wc-admin_orders_lookup_batch_init';
|
|
|
|
|
2019-01-12 01:20:08 +00:00
|
|
|
/**
|
|
|
|
* Queue instance.
|
|
|
|
*
|
|
|
|
* @var WC_Queue_Interface
|
|
|
|
*/
|
|
|
|
protected static $queue = null;
|
|
|
|
|
2018-09-18 19:41:45 +00:00
|
|
|
/**
|
|
|
|
* Boostrap REST API.
|
|
|
|
*/
|
2018-09-17 14:32:03 +00:00
|
|
|
public function __construct() {
|
|
|
|
// Initialize classes.
|
2018-09-17 18:19:36 +00:00
|
|
|
add_action( 'plugins_loaded', array( $this, 'init_classes' ), 19 );
|
2018-09-17 14:32:03 +00:00
|
|
|
// Hook in data stores.
|
|
|
|
add_filter( 'woocommerce_data_stores', array( 'WC_Admin_Api_Init', 'add_data_stores' ) );
|
|
|
|
// Add wc-admin report tables to list of WooCommerce tables.
|
2018-09-20 23:56:35 +00:00
|
|
|
add_filter( 'woocommerce_install_get_tables', array( 'WC_Admin_Api_Init', 'add_tables' ) );
|
2018-09-17 14:32:03 +00:00
|
|
|
// REST API extensions init.
|
|
|
|
add_action( 'rest_api_init', array( $this, 'rest_api_init' ) );
|
2018-09-18 10:24:43 +00:00
|
|
|
add_filter( 'rest_endpoints', array( 'WC_Admin_Api_Init', 'filter_rest_endpoints' ), 10, 1 );
|
|
|
|
add_filter( 'woocommerce_debug_tools', array( 'WC_Admin_Api_Init', 'add_regenerate_tool' ) );
|
2018-09-17 14:32:03 +00:00
|
|
|
|
2018-09-21 10:14:08 +00:00
|
|
|
// Initialize Orders data store class's static vars.
|
|
|
|
add_action( 'woocommerce_after_register_post_type', array( 'WC_Admin_Api_Init', 'orders_data_store_init' ), 20 );
|
2019-01-08 17:18:25 +00:00
|
|
|
// Initialize Customers Report data store sync hooks.
|
2019-01-12 01:07:10 +00:00
|
|
|
// Note: we need to hook in before `wc_current_user_is_active`.
|
2019-01-08 17:18:25 +00:00
|
|
|
// See: https://github.com/woocommerce/woocommerce/blob/942615101ba00c939c107c3a4820c3d466864872/includes/wc-user-functions.php#L749.
|
2019-01-12 01:07:10 +00:00
|
|
|
add_action( 'wp_loaded', array( 'WC_Admin_Api_Init', 'customers_report_data_store_init' ) );
|
2019-01-11 15:21:04 +00:00
|
|
|
|
2019-01-11 18:31:10 +00:00
|
|
|
// Initialize scheduled action handlers.
|
|
|
|
add_action( self::QUEUE_BATCH_ACTION, array( __CLASS__, 'queue_batches' ), 10, 3 );
|
2019-01-11 19:22:31 +00:00
|
|
|
add_action( self::QUEUE_DEPEDENT_ACTION, array( __CLASS__, 'queue_dependent_action' ), 10, 2 );
|
2019-01-11 18:31:10 +00:00
|
|
|
add_action( self::CUSTOMERS_BATCH_ACTION, array( __CLASS__, 'customer_lookup_process_batch' ) );
|
2019-01-11 18:32:37 +00:00
|
|
|
add_action( self::ORDERS_BATCH_ACTION, array( __CLASS__, 'orders_lookup_process_batch' ) );
|
2019-01-11 19:22:31 +00:00
|
|
|
add_action( self::ORDERS_LOOKUP_BATCH_INIT, array( __CLASS__, 'orders_lookup_batch_init' ) );
|
2019-01-17 07:01:09 +00:00
|
|
|
|
|
|
|
// Add currency symbol to orders endpoint response.
|
|
|
|
add_filter( 'woocommerce_rest_prepare_shop_order_object', array( __CLASS__, 'add_currency_symbol_to_order_response' ) );
|
2018-09-17 14:32:03 +00:00
|
|
|
}
|
|
|
|
|
2019-01-12 01:20:08 +00:00
|
|
|
/**
|
|
|
|
* Get queue instance.
|
|
|
|
*
|
|
|
|
* @return WC_Queue_Interface
|
|
|
|
*/
|
|
|
|
public static function queue() {
|
|
|
|
if ( is_null( self::$queue ) ) {
|
|
|
|
self::$queue = WC()->queue();
|
|
|
|
}
|
|
|
|
|
|
|
|
return self::$queue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set queue instance.
|
|
|
|
*
|
|
|
|
* @param WC_Queue_Interface $queue Queue instance.
|
|
|
|
*/
|
|
|
|
public static function set_queue( $queue ) {
|
|
|
|
self::$queue = $queue;
|
|
|
|
}
|
|
|
|
|
2018-09-18 19:41:45 +00:00
|
|
|
/**
|
|
|
|
* Init classes.
|
|
|
|
*/
|
2018-09-17 14:32:03 +00:00
|
|
|
public function init_classes() {
|
|
|
|
// Interfaces.
|
2018-09-17 14:54:31 +00:00
|
|
|
require_once dirname( __FILE__ ) . '/interfaces/class-wc-admin-reports-data-store-interface.php';
|
2018-09-17 18:36:03 +00:00
|
|
|
require_once dirname( __FILE__ ) . '/class-wc-admin-reports-query.php';
|
|
|
|
|
2018-09-18 10:24:43 +00:00
|
|
|
// Common date time code.
|
|
|
|
require_once dirname( __FILE__ ) . '/class-wc-admin-reports-interval.php';
|
2018-09-17 14:32:03 +00:00
|
|
|
|
|
|
|
// Query classes for reports.
|
2018-09-17 14:54:31 +00:00
|
|
|
require_once dirname( __FILE__ ) . '/class-wc-admin-reports-revenue-query.php';
|
2019-01-15 01:53:02 +00:00
|
|
|
require_once dirname( __FILE__ ) . '/class-wc-admin-reports-orders-query.php';
|
2018-09-17 14:54:31 +00:00
|
|
|
require_once dirname( __FILE__ ) . '/class-wc-admin-reports-orders-stats-query.php';
|
|
|
|
require_once dirname( __FILE__ ) . '/class-wc-admin-reports-products-query.php';
|
2018-11-26 02:58:19 +00:00
|
|
|
require_once dirname( __FILE__ ) . '/class-wc-admin-reports-variations-query.php';
|
2018-09-17 14:54:31 +00:00
|
|
|
require_once dirname( __FILE__ ) . '/class-wc-admin-reports-products-stats-query.php';
|
|
|
|
require_once dirname( __FILE__ ) . '/class-wc-admin-reports-categories-query.php';
|
2018-12-11 00:58:05 +00:00
|
|
|
require_once dirname( __FILE__ ) . '/class-wc-admin-reports-taxes-query.php';
|
2018-12-11 00:55:05 +00:00
|
|
|
require_once dirname( __FILE__ ) . '/class-wc-admin-reports-taxes-stats-query.php';
|
2018-12-06 09:40:59 +00:00
|
|
|
require_once dirname( __FILE__ ) . '/class-wc-admin-reports-coupons-query.php';
|
|
|
|
require_once dirname( __FILE__ ) . '/class-wc-admin-reports-coupons-stats-query.php';
|
2018-12-21 22:57:46 +00:00
|
|
|
require_once dirname( __FILE__ ) . '/class-wc-admin-reports-downloads-query.php';
|
2019-01-03 18:00:48 +00:00
|
|
|
require_once dirname( __FILE__ ) . '/class-wc-admin-reports-downloads-stats-query.php';
|
2018-12-20 01:19:12 +00:00
|
|
|
require_once dirname( __FILE__ ) . '/class-wc-admin-reports-customers-query.php';
|
2019-01-18 21:35:43 +00:00
|
|
|
require_once dirname( __FILE__ ) . '/class-wc-admin-reports-customers-stats-query.php';
|
2018-09-17 14:32:03 +00:00
|
|
|
|
2018-09-20 23:56:35 +00:00
|
|
|
// Data stores.
|
2018-09-17 14:54:31 +00:00
|
|
|
require_once dirname( __FILE__ ) . '/data-stores/class-wc-admin-reports-data-store.php';
|
|
|
|
require_once dirname( __FILE__ ) . '/data-stores/class-wc-admin-reports-orders-data-store.php';
|
2019-01-15 01:53:02 +00:00
|
|
|
require_once dirname( __FILE__ ) . '/data-stores/class-wc-admin-reports-orders-stats-data-store.php';
|
2018-09-17 14:54:31 +00:00
|
|
|
require_once dirname( __FILE__ ) . '/data-stores/class-wc-admin-reports-products-data-store.php';
|
2018-11-26 02:58:19 +00:00
|
|
|
require_once dirname( __FILE__ ) . '/data-stores/class-wc-admin-reports-variations-data-store.php';
|
2018-09-17 14:54:31 +00:00
|
|
|
require_once dirname( __FILE__ ) . '/data-stores/class-wc-admin-reports-products-stats-data-store.php';
|
|
|
|
require_once dirname( __FILE__ ) . '/data-stores/class-wc-admin-reports-categories-data-store.php';
|
2018-12-11 00:58:05 +00:00
|
|
|
require_once dirname( __FILE__ ) . '/data-stores/class-wc-admin-reports-taxes-data-store.php';
|
2018-12-11 00:55:05 +00:00
|
|
|
require_once dirname( __FILE__ ) . '/data-stores/class-wc-admin-reports-taxes-stats-data-store.php';
|
2018-12-06 09:40:59 +00:00
|
|
|
require_once dirname( __FILE__ ) . '/data-stores/class-wc-admin-reports-coupons-data-store.php';
|
|
|
|
require_once dirname( __FILE__ ) . '/data-stores/class-wc-admin-reports-coupons-stats-data-store.php';
|
2018-12-21 22:57:46 +00:00
|
|
|
require_once dirname( __FILE__ ) . '/data-stores/class-wc-admin-reports-downloads-data-store.php';
|
2019-01-03 18:00:48 +00:00
|
|
|
require_once dirname( __FILE__ ) . '/data-stores/class-wc-admin-reports-downloads-stats-data-store.php';
|
2018-12-20 01:19:12 +00:00
|
|
|
require_once dirname( __FILE__ ) . '/data-stores/class-wc-admin-reports-customers-data-store.php';
|
2019-01-18 21:35:43 +00:00
|
|
|
require_once dirname( __FILE__ ) . '/data-stores/class-wc-admin-reports-customers-stats-data-store.php';
|
2018-09-24 17:16:10 +00:00
|
|
|
|
|
|
|
// Data triggers.
|
2018-09-20 23:56:35 +00:00
|
|
|
require_once dirname( __FILE__ ) . '/data-stores/class-wc-admin-notes-data-store.php';
|
|
|
|
|
|
|
|
// CRUD classes.
|
2018-09-21 23:44:04 +00:00
|
|
|
require_once dirname( __FILE__ ) . '/class-wc-admin-note.php';
|
2018-09-20 23:56:35 +00:00
|
|
|
require_once dirname( __FILE__ ) . '/class-wc-admin-notes.php';
|
2018-09-17 14:32:03 +00:00
|
|
|
}
|
|
|
|
|
2018-09-18 19:41:45 +00:00
|
|
|
/**
|
|
|
|
* Init REST API.
|
|
|
|
*/
|
2018-09-17 14:32:03 +00:00
|
|
|
public function rest_api_init() {
|
2018-10-10 23:46:08 +00:00
|
|
|
require_once dirname( __FILE__ ) . '/api/class-wc-admin-rest-admin-notes-controller.php';
|
2019-01-18 02:52:58 +00:00
|
|
|
require_once dirname( __FILE__ ) . '/api/class-wc-admin-rest-coupons-controller.php';
|
2018-12-17 16:20:11 +00:00
|
|
|
require_once dirname( __FILE__ ) . '/api/class-wc-admin-rest-customers-controller.php';
|
2018-12-22 00:40:41 +00:00
|
|
|
require_once dirname( __FILE__ ) . '/api/class-wc-admin-rest-data-controller.php';
|
2019-01-18 19:17:12 +00:00
|
|
|
require_once dirname( __FILE__ ) . '/api/class-wc-admin-rest-data-countries-controller.php';
|
2018-12-22 00:40:41 +00:00
|
|
|
require_once dirname( __FILE__ ) . '/api/class-wc-admin-rest-data-download-ips-controller.php';
|
2019-01-18 02:52:58 +00:00
|
|
|
require_once dirname( __FILE__ ) . '/api/class-wc-admin-rest-orders-controller.php';
|
2018-12-12 13:35:56 +00:00
|
|
|
require_once dirname( __FILE__ ) . '/api/class-wc-admin-rest-products-controller.php';
|
2019-01-21 17:12:00 +00:00
|
|
|
require_once dirname( __FILE__ ) . '/api/class-wc-admin-rest-product-categories-controller.php';
|
2018-12-12 13:35:56 +00:00
|
|
|
require_once dirname( __FILE__ ) . '/api/class-wc-admin-rest-product-reviews-controller.php';
|
2018-09-17 14:54:31 +00:00
|
|
|
require_once dirname( __FILE__ ) . '/api/class-wc-admin-rest-reports-controller.php';
|
|
|
|
require_once dirname( __FILE__ ) . '/api/class-wc-admin-rest-system-status-tools-controller.php';
|
|
|
|
require_once dirname( __FILE__ ) . '/api/class-wc-admin-rest-reports-categories-controller.php';
|
|
|
|
require_once dirname( __FILE__ ) . '/api/class-wc-admin-rest-reports-coupons-controller.php';
|
|
|
|
require_once dirname( __FILE__ ) . '/api/class-wc-admin-rest-reports-coupons-stats-controller.php';
|
|
|
|
require_once dirname( __FILE__ ) . '/api/class-wc-admin-rest-reports-customers-controller.php';
|
2019-01-18 21:35:43 +00:00
|
|
|
require_once dirname( __FILE__ ) . '/api/class-wc-admin-rest-reports-customers-stats-controller.php';
|
2018-09-17 14:54:31 +00:00
|
|
|
require_once dirname( __FILE__ ) . '/api/class-wc-admin-rest-reports-downloads-controller.php';
|
|
|
|
require_once dirname( __FILE__ ) . '/api/class-wc-admin-rest-reports-downloads-files-controller.php';
|
|
|
|
require_once dirname( __FILE__ ) . '/api/class-wc-admin-rest-reports-downloads-stats-controller.php';
|
2019-01-15 01:53:02 +00:00
|
|
|
require_once dirname( __FILE__ ) . '/api/class-wc-admin-rest-reports-orders-controller.php';
|
2018-09-17 14:54:31 +00:00
|
|
|
require_once dirname( __FILE__ ) . '/api/class-wc-admin-rest-reports-orders-stats-controller.php';
|
|
|
|
require_once dirname( __FILE__ ) . '/api/class-wc-admin-rest-reports-products-controller.php';
|
2018-11-26 02:58:19 +00:00
|
|
|
require_once dirname( __FILE__ ) . '/api/class-wc-admin-rest-reports-variations-controller.php';
|
2018-09-17 14:54:31 +00:00
|
|
|
require_once dirname( __FILE__ ) . '/api/class-wc-admin-rest-reports-products-stats-controller.php';
|
2019-01-09 21:08:39 +00:00
|
|
|
require_once dirname( __FILE__ ) . '/api/class-wc-admin-rest-reports-performance-indicators-controller.php';
|
2018-09-17 14:54:31 +00:00
|
|
|
require_once dirname( __FILE__ ) . '/api/class-wc-admin-rest-reports-revenue-stats-controller.php';
|
|
|
|
require_once dirname( __FILE__ ) . '/api/class-wc-admin-rest-reports-taxes-controller.php';
|
|
|
|
require_once dirname( __FILE__ ) . '/api/class-wc-admin-rest-reports-taxes-stats-controller.php';
|
2018-12-14 15:43:39 +00:00
|
|
|
require_once dirname( __FILE__ ) . '/api/class-wc-admin-rest-reports-stock-controller.php';
|
2019-01-30 09:43:30 +00:00
|
|
|
require_once dirname( __FILE__ ) . '/api/class-wc-admin-rest-taxes-controller.php';
|
2018-09-17 14:32:03 +00:00
|
|
|
|
2019-01-09 21:08:39 +00:00
|
|
|
$controllers = apply_filters(
|
|
|
|
'woocommerce_admin_rest_controllers',
|
|
|
|
array(
|
|
|
|
'WC_Admin_REST_Admin_Notes_Controller',
|
2019-01-18 02:52:58 +00:00
|
|
|
'WC_Admin_REST_Coupons_Controller',
|
2019-01-09 21:08:39 +00:00
|
|
|
'WC_Admin_REST_Customers_Controller',
|
|
|
|
'WC_Admin_REST_Data_Controller',
|
2019-01-18 19:17:12 +00:00
|
|
|
'WC_Admin_REST_Data_Countries_Controller',
|
2019-01-09 21:08:39 +00:00
|
|
|
'WC_Admin_REST_Data_Download_Ips_Controller',
|
2019-01-18 02:52:58 +00:00
|
|
|
'WC_Admin_REST_Orders_Controller',
|
2019-01-09 21:08:39 +00:00
|
|
|
'WC_Admin_REST_Products_Controller',
|
2019-01-21 17:12:00 +00:00
|
|
|
'WC_Admin_REST_Product_Categories_Controller',
|
2019-01-09 21:08:39 +00:00
|
|
|
'WC_Admin_REST_Product_Reviews_Controller',
|
|
|
|
'WC_Admin_REST_Reports_Controller',
|
|
|
|
'WC_Admin_REST_System_Status_Tools_Controller',
|
|
|
|
'WC_Admin_REST_Reports_Products_Controller',
|
|
|
|
'WC_Admin_REST_Reports_Variations_Controller',
|
|
|
|
'WC_Admin_REST_Reports_Products_Stats_Controller',
|
|
|
|
'WC_Admin_REST_Reports_Revenue_Stats_Controller',
|
2019-01-15 01:53:02 +00:00
|
|
|
'WC_Admin_REST_Reports_Orders_Controller',
|
2019-01-09 21:08:39 +00:00
|
|
|
'WC_Admin_REST_Reports_Orders_Stats_Controller',
|
|
|
|
'WC_Admin_REST_Reports_Categories_Controller',
|
|
|
|
'WC_Admin_REST_Reports_Taxes_Controller',
|
|
|
|
'WC_Admin_REST_Reports_Taxes_Stats_Controller',
|
|
|
|
'WC_Admin_REST_Reports_Coupons_Controller',
|
|
|
|
'WC_Admin_REST_Reports_Coupons_Stats_Controller',
|
|
|
|
'WC_Admin_REST_Reports_Stock_Controller',
|
|
|
|
'WC_Admin_REST_Reports_Downloads_Controller',
|
|
|
|
'WC_Admin_REST_Reports_Downloads_Stats_Controller',
|
|
|
|
'WC_Admin_REST_Reports_Customers_Controller',
|
2019-01-18 21:35:43 +00:00
|
|
|
'WC_Admin_REST_Reports_Customers_Stats_Controller',
|
2019-01-30 09:43:30 +00:00
|
|
|
'WC_Admin_REST_Taxes_Controller',
|
2019-01-09 21:08:39 +00:00
|
|
|
)
|
2018-09-17 14:32:03 +00:00
|
|
|
);
|
|
|
|
|
2019-01-09 21:08:39 +00:00
|
|
|
// The performance indicators controller must be registered last, after other /stats endpoints have been registered.
|
|
|
|
$controllers[] = 'WC_Admin_REST_Reports_Performance_Indicators_Controller';
|
|
|
|
|
2018-09-17 14:32:03 +00:00
|
|
|
foreach ( $controllers as $controller ) {
|
|
|
|
$this->$controller = new $controller();
|
|
|
|
$this->$controller->register_routes();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-18 19:41:45 +00:00
|
|
|
/**
|
|
|
|
* Filter REST API endpoints.
|
|
|
|
*
|
|
|
|
* @param array $endpoints List of endpoints.
|
|
|
|
* @return array
|
|
|
|
*/
|
2018-09-18 10:24:43 +00:00
|
|
|
public static function filter_rest_endpoints( $endpoints ) {
|
2019-01-18 02:52:58 +00:00
|
|
|
// Override GET /wc/v4/system_status/tools.
|
|
|
|
if ( isset( $endpoints['/wc/v4/system_status/tools'] )
|
|
|
|
&& isset( $endpoints['/wc/v4/system_status/tools'][1] )
|
|
|
|
&& isset( $endpoints['/wc/v4/system_status/tools'][0] )
|
|
|
|
&& $endpoints['/wc/v4/system_status/tools'][1]['callback'][0] instanceof WC_Admin_REST_System_Status_Tools_Controller
|
2018-09-18 10:24:43 +00:00
|
|
|
) {
|
2019-01-18 02:52:58 +00:00
|
|
|
$endpoints['/wc/v4/system_status/tools'][0] = $endpoints['/wc/v4/system_status/tools'][1];
|
2018-09-18 10:24:43 +00:00
|
|
|
}
|
2019-01-18 02:52:58 +00:00
|
|
|
// // Override GET & PUT for /wc/v4/system_status/tools.
|
|
|
|
if ( isset( $endpoints['/wc/v4/system_status/tools/(?P<id>[\w-]+)'] )
|
|
|
|
&& isset( $endpoints['/wc/v4/system_status/tools/(?P<id>[\w-]+)'][3] )
|
|
|
|
&& isset( $endpoints['/wc/v4/system_status/tools/(?P<id>[\w-]+)'][2] )
|
|
|
|
&& $endpoints['/wc/v4/system_status/tools/(?P<id>[\w-]+)'][2]['callback'][0] instanceof WC_Admin_REST_System_Status_Tools_Controller
|
|
|
|
&& $endpoints['/wc/v4/system_status/tools/(?P<id>[\w-]+)'][3]['callback'][0] instanceof WC_Admin_REST_System_Status_Tools_Controller
|
2018-09-18 10:24:43 +00:00
|
|
|
) {
|
2019-01-18 02:52:58 +00:00
|
|
|
$endpoints['/wc/v4/system_status/tools/(?P<id>[\w-]+)'][0] = $endpoints['/wc/v4/system_status/tools/(?P<id>[\w-]+)'][2];
|
|
|
|
$endpoints['/wc/v4/system_status/tools/(?P<id>[\w-]+)'][1] = $endpoints['/wc/v4/system_status/tools/(?P<id>[\w-]+)'][3];
|
2018-09-18 10:24:43 +00:00
|
|
|
}
|
|
|
|
|
2019-01-18 02:52:58 +00:00
|
|
|
// Override GET /wc/v4/reports.
|
|
|
|
if ( isset( $endpoints['/wc/v4/reports'] )
|
|
|
|
&& isset( $endpoints['/wc/v4/reports'][1] )
|
|
|
|
&& isset( $endpoints['/wc/v4/reports'][0] )
|
|
|
|
&& $endpoints['/wc/v4/reports'][1]['callback'][0] instanceof WC_Admin_REST_Reports_Controller
|
2018-09-18 10:24:43 +00:00
|
|
|
) {
|
2019-01-18 02:52:58 +00:00
|
|
|
$endpoints['/wc/v4/reports'][0] = $endpoints['/wc/v4/reports'][1];
|
2018-09-18 10:24:43 +00:00
|
|
|
}
|
|
|
|
|
2019-01-18 02:52:58 +00:00
|
|
|
// Override /wc/v4/coupons.
|
|
|
|
if ( isset( $endpoints['/wc/v4/coupons'] )
|
|
|
|
&& isset( $endpoints['/wc/v4/coupons'][3] )
|
|
|
|
&& isset( $endpoints['/wc/v4/coupons'][2] )
|
|
|
|
&& $endpoints['/wc/v4/coupons'][2]['callback'][0] instanceof WC_Admin_REST_Orders_Controller
|
|
|
|
&& $endpoints['/wc/v4/coupons'][3]['callback'][0] instanceof WC_Admin_REST_Orders_Controller
|
2018-12-17 16:20:11 +00:00
|
|
|
) {
|
2019-01-18 02:52:58 +00:00
|
|
|
$endpoints['/wc/v4/coupons'][0] = $endpoints['/wc/v4/coupons'][2];
|
|
|
|
$endpoints['/wc/v4/coupons'][1] = $endpoints['/wc/v4/coupons'][3];
|
2018-12-17 16:20:11 +00:00
|
|
|
}
|
|
|
|
|
2019-01-18 02:52:58 +00:00
|
|
|
// Override /wc/v4/customers.
|
|
|
|
if ( isset( $endpoints['/wc/v4/customers'] )
|
|
|
|
&& isset( $endpoints['/wc/v4/customers'][3] )
|
|
|
|
&& isset( $endpoints['/wc/v4/customers'][2] )
|
|
|
|
&& $endpoints['/wc/v4/customers'][2]['callback'][0] instanceof WC_Admin_REST_Customers_Controller
|
|
|
|
&& $endpoints['/wc/v4/customers'][3]['callback'][0] instanceof WC_Admin_REST_Customers_Controller
|
2018-12-22 00:40:41 +00:00
|
|
|
) {
|
2019-01-18 02:52:58 +00:00
|
|
|
$endpoints['/wc/v4/customers'][0] = $endpoints['/wc/v4/customers'][2];
|
|
|
|
$endpoints['/wc/v4/customers'][1] = $endpoints['/wc/v4/customers'][3];
|
2018-12-22 00:40:41 +00:00
|
|
|
}
|
|
|
|
|
2019-01-18 02:52:58 +00:00
|
|
|
// Override /wc/v4/orders/$id.
|
|
|
|
if ( isset( $endpoints['/wc/v4/orders/(?P<id>[\d]+)'] )
|
|
|
|
&& isset( $endpoints['/wc/v4/orders/(?P<id>[\d]+)'][5] )
|
|
|
|
&& isset( $endpoints['/wc/v4/orders/(?P<id>[\d]+)'][4] )
|
|
|
|
&& isset( $endpoints['/wc/v4/orders/(?P<id>[\d]+)'][3] )
|
|
|
|
&& $endpoints['/wc/v4/orders/(?P<id>[\d]+)'][3]['callback'][0] instanceof WC_Admin_REST_Orders_Controller
|
|
|
|
&& $endpoints['/wc/v4/orders/(?P<id>[\d]+)'][4]['callback'][0] instanceof WC_Admin_REST_Orders_Controller
|
|
|
|
&& $endpoints['/wc/v4/orders/(?P<id>[\d]+)'][5]['callback'][0] instanceof WC_Admin_REST_Orders_Controller
|
2018-12-22 00:40:41 +00:00
|
|
|
) {
|
2019-01-18 02:52:58 +00:00
|
|
|
$endpoints['/wc/v4/orders/(?P<id>[\d]+)'][0] = $endpoints['/wc/v4/orders/(?P<id>[\d]+)'][3];
|
|
|
|
$endpoints['/wc/v4/orders/(?P<id>[\d]+)'][1] = $endpoints['/wc/v4/orders/(?P<id>[\d]+)'][4];
|
|
|
|
$endpoints['/wc/v4/orders/(?P<id>[\d]+)'][2] = $endpoints['/wc/v4/orders/(?P<id>[\d]+)'][5];
|
2018-12-22 00:40:41 +00:00
|
|
|
}
|
|
|
|
|
2019-01-18 02:52:58 +00:00
|
|
|
// Override /wc/v4/orders.
|
|
|
|
if ( isset( $endpoints['/wc/v4/orders'] )
|
|
|
|
&& isset( $endpoints['/wc/v4/orders'][3] )
|
|
|
|
&& isset( $endpoints['/wc/v4/orders'][2] )
|
|
|
|
&& $endpoints['/wc/v4/orders'][2]['callback'][0] instanceof WC_Admin_REST_Orders_Controller
|
|
|
|
&& $endpoints['/wc/v4/orders'][3]['callback'][0] instanceof WC_Admin_REST_Orders_Controller
|
2018-12-22 00:40:41 +00:00
|
|
|
) {
|
2019-01-18 02:52:58 +00:00
|
|
|
$endpoints['/wc/v4/orders'][0] = $endpoints['/wc/v4/orders'][2];
|
|
|
|
$endpoints['/wc/v4/orders'][1] = $endpoints['/wc/v4/orders'][3];
|
2018-12-22 00:40:41 +00:00
|
|
|
}
|
|
|
|
|
2019-01-18 02:52:58 +00:00
|
|
|
// Override /wc/v4/data.
|
|
|
|
if ( isset( $endpoints['/wc/v4/data'] )
|
|
|
|
&& isset( $endpoints['/wc/v4/data'][1] )
|
|
|
|
&& $endpoints['/wc/v4/data'][1]['callback'][0] instanceof WC_Admin_REST_Data_Controller
|
2018-12-22 00:40:41 +00:00
|
|
|
) {
|
2019-01-18 02:52:58 +00:00
|
|
|
$endpoints['/wc/v4/data'][0] = $endpoints['/wc/v4/data'][1];
|
2018-12-22 00:40:41 +00:00
|
|
|
}
|
|
|
|
|
2019-01-18 02:52:58 +00:00
|
|
|
// Override /wc/v4/products.
|
|
|
|
if ( isset( $endpoints['/wc/v4/products'] )
|
|
|
|
&& isset( $endpoints['/wc/v4/products'][3] )
|
|
|
|
&& isset( $endpoints['/wc/v4/products'][2] )
|
|
|
|
&& $endpoints['/wc/v4/products'][2]['callback'][0] instanceof WC_Admin_REST_Products_Controller
|
|
|
|
&& $endpoints['/wc/v4/products'][3]['callback'][0] instanceof WC_Admin_REST_Products_Controller
|
2018-12-12 13:35:56 +00:00
|
|
|
) {
|
2019-01-18 02:52:58 +00:00
|
|
|
$endpoints['/wc/v4/products'][0] = $endpoints['/wc/v4/products'][2];
|
|
|
|
$endpoints['/wc/v4/products'][1] = $endpoints['/wc/v4/products'][3];
|
2018-12-12 13:35:56 +00:00
|
|
|
}
|
|
|
|
|
2019-01-18 02:52:58 +00:00
|
|
|
// Override /wc/v4/products/$id.
|
|
|
|
if ( isset( $endpoints['/wc/v4/products/(?P<id>[\d]+)'] )
|
|
|
|
&& isset( $endpoints['/wc/v4/products/(?P<id>[\d]+)'][5] )
|
|
|
|
&& isset( $endpoints['/wc/v4/products/(?P<id>[\d]+)'][4] )
|
|
|
|
&& isset( $endpoints['/wc/v4/products/(?P<id>[\d]+)'][3] )
|
|
|
|
&& $endpoints['/wc/v4/products/(?P<id>[\d]+)'][3]['callback'][0] instanceof WC_Admin_REST_Products_Controller
|
|
|
|
&& $endpoints['/wc/v4/products/(?P<id>[\d]+)'][4]['callback'][0] instanceof WC_Admin_REST_Products_Controller
|
|
|
|
&& $endpoints['/wc/v4/products/(?P<id>[\d]+)'][5]['callback'][0] instanceof WC_Admin_REST_Products_Controller
|
2018-12-12 13:35:56 +00:00
|
|
|
) {
|
2019-01-18 02:52:58 +00:00
|
|
|
$endpoints['/wc/v4/products/(?P<id>[\d]+)'][0] = $endpoints['/wc/v4/products/(?P<id>[\d]+)'][3];
|
|
|
|
$endpoints['/wc/v4/products/(?P<id>[\d]+)'][1] = $endpoints['/wc/v4/products/(?P<id>[\d]+)'][4];
|
|
|
|
$endpoints['/wc/v4/products/(?P<id>[\d]+)'][2] = $endpoints['/wc/v4/products/(?P<id>[\d]+)'][5];
|
|
|
|
}
|
|
|
|
|
2019-01-21 17:12:00 +00:00
|
|
|
// Override /wc/v4/products/categories.
|
|
|
|
if ( isset( $endpoints['/wc/v4/products/categories'] )
|
|
|
|
&& isset( $endpoints['/wc/v4/products/categories'][3] )
|
|
|
|
&& isset( $endpoints['/wc/v4/products/categories'][2] )
|
|
|
|
&& $endpoints['/wc/v4/products/categories'][2]['callback'][0] instanceof WC_Admin_REST_Product_categories_Controller
|
|
|
|
&& $endpoints['/wc/v4/products/categories'][3]['callback'][0] instanceof WC_Admin_REST_Product_categories_Controller
|
|
|
|
) {
|
|
|
|
$endpoints['/wc/v4/products/categories'][0] = $endpoints['/wc/v4/products/categories'][2];
|
|
|
|
$endpoints['/wc/v4/products/categories'][1] = $endpoints['/wc/v4/products/categories'][3];
|
|
|
|
}
|
|
|
|
|
2019-01-18 02:52:58 +00:00
|
|
|
// Override /wc/v4/products/reviews.
|
|
|
|
if ( isset( $endpoints['/wc/v4/products/reviews'] )
|
|
|
|
&& isset( $endpoints['/wc/v4/products/reviews'][3] )
|
|
|
|
&& isset( $endpoints['/wc/v4/products/reviews'][2] )
|
|
|
|
&& $endpoints['/wc/v4/products/reviews'][2]['callback'][0] instanceof WC_Admin_REST_Product_Reviews_Controller
|
|
|
|
&& $endpoints['/wc/v4/products/reviews'][3]['callback'][0] instanceof WC_Admin_REST_Product_Reviews_Controller
|
|
|
|
) {
|
|
|
|
$endpoints['/wc/v4/products/reviews'][0] = $endpoints['/wc/v4/products/reviews'][2];
|
|
|
|
$endpoints['/wc/v4/products/reviews'][1] = $endpoints['/wc/v4/products/reviews'][3];
|
2018-12-12 13:35:56 +00:00
|
|
|
}
|
|
|
|
|
2019-01-30 09:43:30 +00:00
|
|
|
// Override /wc/v4/taxes.
|
|
|
|
if ( isset( $endpoints['/wc/v4/taxes'] )
|
|
|
|
&& isset( $endpoints['/wc/v4/taxes'][3] )
|
|
|
|
&& isset( $endpoints['/wc/v4/taxes'][2] )
|
|
|
|
&& $endpoints['/wc/v4/taxes'][2]['callback'][0] instanceof WC_Admin_REST_Orders_Controller
|
|
|
|
&& $endpoints['/wc/v4/taxes'][3]['callback'][0] instanceof WC_Admin_REST_Orders_Controller
|
|
|
|
) {
|
|
|
|
$endpoints['/wc/v4/taxes'][0] = $endpoints['/wc/v4/taxes'][2];
|
|
|
|
$endpoints['/wc/v4/taxes'][1] = $endpoints['/wc/v4/taxes'][3];
|
|
|
|
}
|
|
|
|
|
2018-09-18 10:24:43 +00:00
|
|
|
return $endpoints;
|
|
|
|
}
|
|
|
|
|
2018-09-21 10:24:15 +00:00
|
|
|
/**
|
|
|
|
* Regenerate data for reports.
|
|
|
|
*/
|
2018-12-27 03:49:00 +00:00
|
|
|
public static function regenerate_report_data() {
|
2019-01-08 01:33:40 +00:00
|
|
|
// Add registered customers to the lookup table before updating order stats
|
|
|
|
// so that the orders can be associated with the `customer_id` column.
|
2019-01-11 16:32:23 +00:00
|
|
|
self::customer_lookup_batch_init();
|
2019-01-11 19:22:31 +00:00
|
|
|
// Queue orders lookup to occur after customers lookup generation is done.
|
|
|
|
self::queue_dependent_action( self::ORDERS_LOOKUP_BATCH_INIT, self::CUSTOMERS_BATCH_ACTION );
|
2018-09-21 10:24:15 +00:00
|
|
|
}
|
|
|
|
|
2018-09-18 19:41:45 +00:00
|
|
|
/**
|
|
|
|
* Adds regenerate tool.
|
|
|
|
*
|
|
|
|
* @param array $tools List of tools.
|
|
|
|
* @return array
|
|
|
|
*/
|
2018-09-18 10:24:43 +00:00
|
|
|
public static function add_regenerate_tool( $tools ) {
|
|
|
|
return array_merge(
|
|
|
|
$tools,
|
|
|
|
array(
|
|
|
|
'rebuild_stats' => array(
|
2018-09-24 15:56:43 +00:00
|
|
|
'name' => __( 'Rebuild reports data', 'wc-admin' ),
|
|
|
|
'button' => __( 'Rebuild reports', 'wc-admin' ),
|
|
|
|
'desc' => __( 'This tool will rebuild all of the information used by the reports.', 'wc-admin' ),
|
2018-12-27 03:49:00 +00:00
|
|
|
'callback' => array( 'WC_Admin_Api_Init', 'regenerate_report_data' ),
|
2018-09-18 10:24:43 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-09-18 19:41:45 +00:00
|
|
|
/**
|
|
|
|
* Init orders data store.
|
|
|
|
*/
|
2018-09-17 14:32:03 +00:00
|
|
|
public static function orders_data_store_init() {
|
2019-01-15 01:53:02 +00:00
|
|
|
WC_Admin_Reports_Orders_Stats_Data_Store::init();
|
2018-12-27 03:49:00 +00:00
|
|
|
WC_Admin_Reports_Products_Data_Store::init();
|
|
|
|
WC_Admin_Reports_Taxes_Data_Store::init();
|
|
|
|
WC_Admin_Reports_Coupons_Data_Store::init();
|
2018-09-17 14:32:03 +00:00
|
|
|
}
|
|
|
|
|
2018-09-18 19:41:45 +00:00
|
|
|
/**
|
2019-01-11 18:32:37 +00:00
|
|
|
* Init order/product lookup tables update (in batches).
|
2018-09-18 19:41:45 +00:00
|
|
|
*/
|
2019-01-11 18:32:37 +00:00
|
|
|
public static function orders_lookup_batch_init() {
|
|
|
|
$batch_size = self::get_batch_size( self::ORDERS_BATCH_ACTION );
|
|
|
|
$order_query = new WC_Order_Query(
|
|
|
|
array(
|
|
|
|
'return' => 'ids',
|
|
|
|
'limit' => 1,
|
|
|
|
'paginate' => true,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$result = $order_query->get_orders();
|
2019-01-14 22:03:30 +00:00
|
|
|
|
|
|
|
if ( 0 === $result->total ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-11 18:32:37 +00:00
|
|
|
$num_batches = ceil( $result->total / $batch_size );
|
2018-09-17 14:32:03 +00:00
|
|
|
|
2019-01-11 18:32:37 +00:00
|
|
|
self::queue_batches( 1, $num_batches, self::ORDERS_BATCH_ACTION );
|
|
|
|
}
|
2018-09-17 14:32:03 +00:00
|
|
|
|
2019-01-11 18:32:37 +00:00
|
|
|
/**
|
|
|
|
* Process a batch of orders to update (stats and products).
|
|
|
|
*
|
|
|
|
* @param int $batch_number Batch number to process (essentially a query page number).
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function orders_lookup_process_batch( $batch_number ) {
|
|
|
|
$batch_size = self::get_batch_size( self::ORDERS_BATCH_ACTION );
|
|
|
|
$order_query = new WC_Order_Query(
|
|
|
|
array(
|
|
|
|
'return' => 'ids',
|
|
|
|
'limit' => $batch_size,
|
|
|
|
'page' => $batch_number,
|
|
|
|
'orderby' => 'ID',
|
|
|
|
'order' => 'ASC',
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$order_ids = $order_query->get_orders();
|
|
|
|
|
|
|
|
foreach ( $order_ids as $order_id ) {
|
|
|
|
// TODO: schedule single order update if this fails?
|
|
|
|
WC_Admin_Reports_Orders_Stats_Data_Store::sync_order( $order_id );
|
2018-12-27 03:49:00 +00:00
|
|
|
WC_Admin_Reports_Products_Data_Store::sync_order_products( $order_id );
|
2019-01-15 22:12:51 +00:00
|
|
|
WC_Admin_Reports_Coupons_Data_Store::sync_order_coupons( $order_id );
|
|
|
|
WC_Admin_Reports_Taxes_Data_Store::sync_order_taxes( $order_id );
|
2018-09-17 14:32:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-08 17:18:25 +00:00
|
|
|
/**
|
|
|
|
* Init customers report data store.
|
|
|
|
*/
|
|
|
|
public static function customers_report_data_store_init() {
|
|
|
|
WC_Admin_Reports_Customers_Data_Store::init();
|
|
|
|
}
|
|
|
|
|
2019-01-11 15:21:04 +00:00
|
|
|
/**
|
|
|
|
* Returns the batch size for regenerating reports.
|
2019-01-11 18:31:10 +00:00
|
|
|
* Note: can differ per batch action.
|
2019-01-11 15:21:04 +00:00
|
|
|
*
|
2019-01-11 18:31:10 +00:00
|
|
|
* @param string $action Single batch action name.
|
2019-01-11 15:21:04 +00:00
|
|
|
* @return int Batch size.
|
|
|
|
*/
|
2019-01-11 18:31:10 +00:00
|
|
|
public static function get_batch_size( $action ) {
|
|
|
|
$batch_sizes = array(
|
|
|
|
self::QUEUE_BATCH_ACTION => 100,
|
|
|
|
self::CUSTOMERS_BATCH_ACTION => 25,
|
2019-01-11 18:32:37 +00:00
|
|
|
self::ORDERS_BATCH_ACTION => 10,
|
2019-01-11 18:31:10 +00:00
|
|
|
);
|
|
|
|
$batch_size = isset( $batch_sizes[ $action ] ) ? $batch_sizes[ $action ] : 25;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Filter the batch size for regenerating a report table.
|
|
|
|
*
|
|
|
|
* @param int $batch_size Batch size.
|
|
|
|
* @param string $action Batch action name.
|
|
|
|
*/
|
|
|
|
return apply_filters( 'wc_admin_report_regenerate_batch_size', $batch_size, $action );
|
2019-01-11 15:21:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Queue a large number of batch jobs, respecting the batch size limit.
|
|
|
|
* Reduces a range of batches down to "single batch" jobs.
|
|
|
|
*
|
|
|
|
* @param int $range_start Starting batch number.
|
|
|
|
* @param int $range_end Ending batch number.
|
|
|
|
* @param string $single_batch_action Action to schedule for a single batch.
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function queue_batches( $range_start, $range_end, $single_batch_action ) {
|
2019-01-15 22:18:32 +00:00
|
|
|
$batch_size = self::get_batch_size( self::QUEUE_BATCH_ACTION );
|
|
|
|
$range_size = 1 + ( $range_end - $range_start );
|
|
|
|
$action_timestamp = time() + 5;
|
2019-01-11 15:21:04 +00:00
|
|
|
|
|
|
|
if ( $range_size > $batch_size ) {
|
|
|
|
// If the current batch range is larger than a single batch,
|
2019-01-11 18:31:10 +00:00
|
|
|
// split the range into $queue_batch_size chunks.
|
2019-01-11 15:21:04 +00:00
|
|
|
$chunk_size = ceil( $range_size / $batch_size );
|
|
|
|
|
|
|
|
for ( $i = 0; $i < $batch_size; $i++ ) {
|
|
|
|
$batch_start = $range_start + ( $i * $chunk_size );
|
|
|
|
$batch_end = min( $range_end, $range_start + ( $chunk_size * ( $i + 1 ) ) - 1 );
|
|
|
|
|
2019-01-12 01:20:08 +00:00
|
|
|
self::queue()->schedule_single(
|
2019-01-15 22:18:32 +00:00
|
|
|
$action_timestamp,
|
2019-01-11 18:31:10 +00:00
|
|
|
self::QUEUE_BATCH_ACTION,
|
2019-01-11 15:21:04 +00:00
|
|
|
array( $batch_start, $batch_end, $single_batch_action )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Otherwise, queue the single batches.
|
|
|
|
for ( $i = $range_start; $i <= $range_end; $i++ ) {
|
2019-01-15 22:18:32 +00:00
|
|
|
self::queue()->schedule_single( $action_timestamp, $single_batch_action, array( $i ) );
|
2019-01-11 15:21:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-11 19:22:31 +00:00
|
|
|
/**
|
|
|
|
* Queue an action to run after another.
|
|
|
|
*
|
|
|
|
* @param string $action Action to run after prerequisite.
|
|
|
|
* @param string $prerequisite_action Prerequisite action.
|
|
|
|
*/
|
|
|
|
public static function queue_dependent_action( $action, $prerequisite_action ) {
|
2019-01-12 01:20:08 +00:00
|
|
|
$blocking_jobs = self::queue()->search(
|
2019-01-11 19:22:31 +00:00
|
|
|
array(
|
|
|
|
'status' => 'pending',
|
|
|
|
'orderby' => 'date',
|
|
|
|
'order' => 'DESC',
|
|
|
|
'per_page' => 1,
|
|
|
|
'claimed' => false,
|
|
|
|
'search' => $prerequisite_action, // search is used instead of hook to find queued batch creation.
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( $blocking_jobs ) {
|
|
|
|
$blocking_job = current( $blocking_jobs );
|
|
|
|
$after_blocking_job = $blocking_job->get_schedule()->next()->getTimestamp() + 5;
|
|
|
|
|
2019-01-12 01:20:08 +00:00
|
|
|
self::queue()->schedule_single(
|
2019-01-11 19:22:31 +00:00
|
|
|
$after_blocking_job,
|
|
|
|
self::QUEUE_DEPEDENT_ACTION,
|
|
|
|
array( $action, $prerequisite_action )
|
|
|
|
);
|
|
|
|
} else {
|
2019-01-12 01:20:08 +00:00
|
|
|
self::queue()->schedule_single( time() + 5, $action );
|
2019-01-11 19:22:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-19 02:11:19 +00:00
|
|
|
/**
|
2019-01-11 16:32:23 +00:00
|
|
|
* Init customer lookup table update (in batches).
|
2018-12-19 02:11:19 +00:00
|
|
|
*/
|
2019-01-11 16:32:23 +00:00
|
|
|
public static function customer_lookup_batch_init() {
|
2019-01-11 18:31:10 +00:00
|
|
|
$batch_size = self::get_batch_size( self::CUSTOMERS_BATCH_ACTION );
|
2019-01-11 16:32:23 +00:00
|
|
|
$customer_query = new WP_User_Query(
|
|
|
|
array(
|
|
|
|
'fields' => 'ID',
|
|
|
|
'number' => 1,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$total_customers = $customer_query->get_total();
|
2019-01-14 22:03:30 +00:00
|
|
|
|
|
|
|
if ( 0 === $total_customers ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-11 16:32:23 +00:00
|
|
|
$num_batches = ceil( $total_customers / $batch_size );
|
2018-12-19 02:11:19 +00:00
|
|
|
|
2019-01-11 18:31:10 +00:00
|
|
|
self::queue_batches( 1, $num_batches, self::CUSTOMERS_BATCH_ACTION );
|
2019-01-11 16:32:23 +00:00
|
|
|
}
|
2018-12-19 02:11:19 +00:00
|
|
|
|
2019-01-11 16:32:23 +00:00
|
|
|
/**
|
|
|
|
* Process a batch of customers to update.
|
|
|
|
*
|
|
|
|
* @param int $batch_number Batch number to process (essentially a query page number).
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function customer_lookup_process_batch( $batch_number ) {
|
2019-01-11 18:31:10 +00:00
|
|
|
$batch_size = self::get_batch_size( self::CUSTOMERS_BATCH_ACTION );
|
2019-01-11 16:32:23 +00:00
|
|
|
$customer_query = new WP_User_Query(
|
|
|
|
array(
|
|
|
|
'fields' => 'ID',
|
|
|
|
'orderby' => 'ID',
|
|
|
|
'order' => 'ASC',
|
|
|
|
'number' => $batch_size,
|
|
|
|
'paged' => $batch_number,
|
|
|
|
)
|
|
|
|
);
|
2018-12-19 02:11:19 +00:00
|
|
|
|
2019-01-11 16:32:23 +00:00
|
|
|
$customer_ids = $customer_query->get_results();
|
2018-12-19 02:11:19 +00:00
|
|
|
|
|
|
|
foreach ( $customer_ids as $customer_id ) {
|
2019-01-11 16:32:23 +00:00
|
|
|
// TODO: schedule single customer update if this fails?
|
|
|
|
WC_Admin_Reports_Customers_Data_Store::update_registered_customer( $customer_id );
|
2018-12-19 02:11:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-18 19:41:45 +00:00
|
|
|
/**
|
|
|
|
* Adds data stores.
|
|
|
|
*
|
|
|
|
* @param array $data_stores List of data stores.
|
|
|
|
* @return array
|
|
|
|
*/
|
2018-09-17 14:32:03 +00:00
|
|
|
public static function add_data_stores( $data_stores ) {
|
|
|
|
return array_merge(
|
|
|
|
$data_stores,
|
|
|
|
array(
|
2019-01-15 01:53:02 +00:00
|
|
|
'report-revenue-stats' => 'WC_Admin_Reports_Orders_Stats_Data_Store',
|
|
|
|
'report-orders' => 'WC_Admin_Reports_Orders_Data_Store',
|
|
|
|
'report-orders-stats' => 'WC_Admin_Reports_Orders_Stats_Data_Store',
|
|
|
|
'report-products' => 'WC_Admin_Reports_Products_Data_Store',
|
|
|
|
'report-variations' => 'WC_Admin_Reports_Variations_Data_Store',
|
|
|
|
'report-products-stats' => 'WC_Admin_Reports_Products_Stats_Data_Store',
|
|
|
|
'report-categories' => 'WC_Admin_Reports_Categories_Data_Store',
|
|
|
|
'report-taxes' => 'WC_Admin_Reports_Taxes_Data_Store',
|
|
|
|
'report-taxes-stats' => 'WC_Admin_Reports_Taxes_Stats_Data_Store',
|
|
|
|
'report-coupons' => 'WC_Admin_Reports_Coupons_Data_Store',
|
|
|
|
'report-coupons-stats' => 'WC_Admin_Reports_Coupons_Stats_Data_Store',
|
|
|
|
'report-downloads' => 'WC_Admin_Reports_Downloads_Data_Store',
|
2019-01-03 18:00:48 +00:00
|
|
|
'report-downloads-stats' => 'WC_Admin_Reports_Downloads_Stats_Data_Store',
|
2019-01-09 21:08:39 +00:00
|
|
|
'admin-note' => 'WC_Admin_Notes_Data_Store',
|
|
|
|
'report-customers' => 'WC_Admin_Reports_Customers_Data_Store',
|
2019-01-18 21:35:43 +00:00
|
|
|
'report-customers-stats' => 'WC_Admin_Reports_Customers_Stats_Data_Store',
|
2018-09-17 14:32:03 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-09-18 19:41:45 +00:00
|
|
|
/**
|
2018-09-20 23:56:35 +00:00
|
|
|
* Adds new tables.
|
2018-09-18 19:41:45 +00:00
|
|
|
*
|
|
|
|
* @param array $wc_tables List of WooCommerce tables.
|
|
|
|
* @return array
|
|
|
|
*/
|
2018-09-20 23:56:35 +00:00
|
|
|
public static function add_tables( $wc_tables ) {
|
2018-09-17 17:53:57 +00:00
|
|
|
global $wpdb;
|
|
|
|
|
2018-09-17 14:32:03 +00:00
|
|
|
return array_merge(
|
|
|
|
$wc_tables,
|
|
|
|
array(
|
|
|
|
// TODO: will this work on multisite?
|
2018-09-18 20:03:42 +00:00
|
|
|
"{$wpdb->prefix}wc_order_stats",
|
|
|
|
"{$wpdb->prefix}wc_order_product_lookup",
|
2018-09-18 11:24:11 +00:00
|
|
|
"{$wpdb->prefix}wc_order_tax_lookup",
|
2018-09-18 11:29:58 +00:00
|
|
|
"{$wpdb->prefix}wc_order_coupon_lookup",
|
2018-09-20 23:56:35 +00:00
|
|
|
"{$wpdb->prefix}woocommerce_admin_notes",
|
|
|
|
"{$wpdb->prefix}woocommerce_admin_note_actions",
|
2018-12-19 17:12:04 +00:00
|
|
|
"{$wpdb->prefix}wc_customer_lookup",
|
2018-09-17 14:32:03 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-09-18 19:41:45 +00:00
|
|
|
/**
|
|
|
|
* Get database schema.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2018-09-17 14:32:03 +00:00
|
|
|
private static function get_schema() {
|
|
|
|
global $wpdb;
|
|
|
|
|
|
|
|
if ( $wpdb->has_cap( 'collation' ) ) {
|
|
|
|
$collate = $wpdb->get_charset_collate();
|
|
|
|
}
|
|
|
|
|
|
|
|
$tables = "
|
2018-09-18 20:03:42 +00:00
|
|
|
CREATE TABLE {$wpdb->prefix}wc_order_stats (
|
2018-09-17 14:32:03 +00:00
|
|
|
order_id bigint(20) unsigned NOT NULL,
|
|
|
|
date_created timestamp DEFAULT '0000-00-00 00:00:00' NOT NULL,
|
|
|
|
num_items_sold int(11) UNSIGNED DEFAULT 0 NOT NULL,
|
|
|
|
gross_total double DEFAULT 0 NOT NULL,
|
|
|
|
coupon_total double DEFAULT 0 NOT NULL,
|
|
|
|
refund_total double DEFAULT 0 NOT NULL,
|
|
|
|
tax_total double DEFAULT 0 NOT NULL,
|
|
|
|
shipping_total double DEFAULT 0 NOT NULL,
|
|
|
|
net_total double DEFAULT 0 NOT NULL,
|
2018-10-31 19:09:38 +00:00
|
|
|
returning_customer boolean DEFAULT 0 NOT NULL,
|
2019-01-08 01:16:10 +00:00
|
|
|
status varchar(200) NOT NULL,
|
2019-01-08 01:21:36 +00:00
|
|
|
customer_id BIGINT UNSIGNED NOT NULL,
|
2018-09-17 14:32:03 +00:00
|
|
|
PRIMARY KEY (order_id),
|
|
|
|
KEY date_created (date_created)
|
|
|
|
) $collate;
|
2018-09-18 20:03:42 +00:00
|
|
|
CREATE TABLE {$wpdb->prefix}wc_order_product_lookup (
|
2018-09-17 14:32:03 +00:00
|
|
|
order_item_id BIGINT UNSIGNED NOT NULL,
|
|
|
|
order_id BIGINT UNSIGNED NOT NULL,
|
|
|
|
product_id BIGINT UNSIGNED NOT NULL,
|
2018-11-26 02:58:19 +00:00
|
|
|
variation_id BIGINT UNSIGNED NOT NULL,
|
2018-09-17 14:32:03 +00:00
|
|
|
customer_id BIGINT UNSIGNED NULL,
|
|
|
|
date_created timestamp DEFAULT '0000-00-00 00:00:00' NOT NULL,
|
|
|
|
product_qty INT UNSIGNED NOT NULL,
|
2018-12-19 00:56:27 +00:00
|
|
|
product_net_revenue double DEFAULT 0 NOT NULL,
|
2018-09-17 14:32:03 +00:00
|
|
|
PRIMARY KEY (order_item_id),
|
|
|
|
KEY order_id (order_id),
|
|
|
|
KEY product_id (product_id),
|
|
|
|
KEY customer_id (customer_id),
|
|
|
|
KEY date_created (date_created)
|
2018-09-18 11:24:11 +00:00
|
|
|
) $collate;
|
|
|
|
CREATE TABLE {$wpdb->prefix}wc_order_tax_lookup (
|
|
|
|
order_id BIGINT UNSIGNED NOT NULL,
|
|
|
|
tax_rate_id BIGINT UNSIGNED NOT NULL,
|
|
|
|
date_created timestamp DEFAULT '0000-00-00 00:00:00' NOT NULL,
|
|
|
|
shipping_tax double DEFAULT 0 NOT NULL,
|
|
|
|
order_tax double DEFAULT 0 NOT NULL,
|
|
|
|
total_tax double DEFAULT 0 NOT NULL,
|
|
|
|
KEY order_id (order_id),
|
|
|
|
KEY tax_rate_id (tax_rate_id),
|
|
|
|
KEY date_created (date_created)
|
2018-09-18 11:29:58 +00:00
|
|
|
) $collate;
|
|
|
|
CREATE TABLE {$wpdb->prefix}wc_order_coupon_lookup (
|
|
|
|
order_id BIGINT UNSIGNED NOT NULL,
|
|
|
|
coupon_id BIGINT UNSIGNED NOT NULL,
|
|
|
|
date_created timestamp DEFAULT '0000-00-00 00:00:00' NOT NULL,
|
2018-12-14 11:40:49 +00:00
|
|
|
discount_amount double DEFAULT 0 NOT NULL,
|
2018-11-23 21:29:47 +00:00
|
|
|
PRIMARY KEY (order_id, coupon_id),
|
2018-09-18 11:29:58 +00:00
|
|
|
KEY coupon_id (coupon_id),
|
|
|
|
KEY date_created (date_created)
|
2018-09-20 23:56:35 +00:00
|
|
|
) $collate;
|
|
|
|
CREATE TABLE {$wpdb->prefix}woocommerce_admin_notes (
|
|
|
|
note_id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
|
2018-09-21 23:44:04 +00:00
|
|
|
name varchar(255) NOT NULL,
|
|
|
|
type varchar(20) NOT NULL,
|
|
|
|
locale varchar(20) NOT NULL,
|
|
|
|
title longtext NOT NULL,
|
|
|
|
content longtext NOT NULL,
|
|
|
|
icon varchar(200) NOT NULL,
|
|
|
|
content_data longtext NULL default null,
|
2018-09-20 23:56:35 +00:00
|
|
|
status varchar(200) NOT NULL,
|
|
|
|
source varchar(200) NOT NULL,
|
|
|
|
date_created datetime NOT NULL default '0000-00-00 00:00:00',
|
|
|
|
date_reminder datetime NULL default null,
|
|
|
|
PRIMARY KEY (note_id)
|
|
|
|
) $collate;
|
|
|
|
CREATE TABLE {$wpdb->prefix}woocommerce_admin_note_actions (
|
|
|
|
action_id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
|
|
|
|
note_id BIGINT UNSIGNED NOT NULL,
|
2018-09-21 23:44:04 +00:00
|
|
|
name varchar(255) NOT NULL,
|
|
|
|
label varchar(255) NOT NULL,
|
|
|
|
query longtext NOT NULL,
|
2018-09-20 23:56:35 +00:00
|
|
|
PRIMARY KEY (action_id),
|
|
|
|
KEY note_id (note_id)
|
|
|
|
) $collate;
|
2018-12-19 00:18:15 +00:00
|
|
|
CREATE TABLE {$wpdb->prefix}wc_customer_lookup (
|
|
|
|
customer_id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
|
|
|
|
user_id BIGINT UNSIGNED DEFAULT NULL,
|
2018-12-19 17:11:39 +00:00
|
|
|
username varchar(60) DEFAULT '' NOT NULL,
|
2018-12-19 00:18:15 +00:00
|
|
|
first_name varchar(255) NOT NULL,
|
|
|
|
last_name varchar(255) NOT NULL,
|
|
|
|
email varchar(100) NOT NULL,
|
2019-01-25 17:37:50 +00:00
|
|
|
date_last_active timestamp NULL default null,
|
2019-01-03 18:51:58 +00:00
|
|
|
date_registered timestamp NULL default null,
|
2018-12-19 00:18:15 +00:00
|
|
|
country char(2) DEFAULT '' NOT NULL,
|
2018-12-20 01:14:32 +00:00
|
|
|
postcode varchar(20) DEFAULT '' NOT NULL,
|
2018-12-19 00:18:15 +00:00
|
|
|
city varchar(100) DEFAULT '' NOT NULL,
|
|
|
|
PRIMARY KEY (customer_id),
|
2018-12-20 18:09:39 +00:00
|
|
|
UNIQUE KEY user_id (user_id),
|
2018-12-19 00:18:15 +00:00
|
|
|
KEY email (email)
|
|
|
|
) $collate;
|
2018-09-20 23:56:35 +00:00
|
|
|
";
|
2018-09-17 18:46:55 +00:00
|
|
|
|
|
|
|
return $tables;
|
2018-09-17 14:32:03 +00:00
|
|
|
}
|
|
|
|
|
2018-09-18 19:41:45 +00:00
|
|
|
/**
|
|
|
|
* Create database tables.
|
|
|
|
*/
|
2018-09-20 14:20:04 +00:00
|
|
|
public static function create_db_tables() {
|
2018-09-17 14:54:31 +00:00
|
|
|
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
|
2018-09-17 14:32:03 +00:00
|
|
|
|
|
|
|
dbDelta( self::get_schema() );
|
|
|
|
}
|
|
|
|
|
2018-09-20 14:20:04 +00:00
|
|
|
/**
|
|
|
|
* Install plugin.
|
|
|
|
*/
|
|
|
|
public static function install() {
|
2018-09-21 10:14:08 +00:00
|
|
|
// Create tables.
|
2018-09-20 14:20:04 +00:00
|
|
|
self::create_db_tables();
|
2018-09-21 10:14:08 +00:00
|
|
|
|
|
|
|
// Initialize report tables.
|
2019-01-11 18:32:37 +00:00
|
|
|
add_action( 'woocommerce_after_register_post_type', array( __CLASS__, 'regenerate_report_data' ), 20 );
|
2018-09-20 14:20:04 +00:00
|
|
|
}
|
|
|
|
|
2019-01-17 07:01:09 +00:00
|
|
|
/**
|
|
|
|
* Add the currency symbol (in addition to currency code) to each Order
|
|
|
|
* object in REST API responses. For use in formatCurrency().
|
|
|
|
*
|
|
|
|
* @param {WP_REST_Response} $response REST response object.
|
|
|
|
* @returns {WP_REST_Response}
|
|
|
|
*/
|
|
|
|
public static function add_currency_symbol_to_order_response( $response ) {
|
|
|
|
$response_data = $response->get_data();
|
|
|
|
$currency_code = $response_data['currency'];
|
|
|
|
$currency_symbol = get_woocommerce_currency_symbol( $currency_code );
|
|
|
|
$response_data['currency_symbol'] = html_entity_decode( $currency_symbol );
|
|
|
|
$response->set_data( $response_data );
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
2018-09-17 14:32:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
new WC_Admin_Api_Init();
|