Merge pull request woocommerce/woocommerce-admin#2717 from woocommerce/fix/2711-autoload-classes

Use Composer autoloading for class files
This commit is contained in:
Jeff Stieler 2019-08-13 11:19:39 -07:00 committed by GitHub
commit edbbc33701
148 changed files with 2005 additions and 1652 deletions

View File

@ -23,12 +23,14 @@ if ( 'core' !== $phase ) {
$write = "<?php\n"; $write = "<?php\n";
$write .= "// WARNING: Do not directly edit this file.\n"; $write .= "// WARNING: Do not directly edit this file.\n";
$write .= "// This file is auto-generated as part of the build process and things may break.\n"; $write .= "// This file is auto-generated as part of the build process and things may break.\n";
$write .= "function wc_admin_get_feature_config() {\n"; $write .= "if ( ! function_exists( 'wc_admin_get_feature_config' ) ) {\n";
$write .= "\treturn array(\n"; $write .= "\tfunction wc_admin_get_feature_config() {\n";
$write .= "\t\treturn array(\n";
foreach ( $config->features as $feature => $bool ) { foreach ( $config->features as $feature => $bool ) {
$write .= "\t\t'{$feature}' => " . ( $bool ? 'true' : 'false' ) . ",\n"; $write .= "\t\t\t'{$feature}' => " . ( $bool ? 'true' : 'false' ) . ",\n";
} }
$write .= "\t);\n"; $write .= "\t\t);\n";
$write .= "\t}\n";
$write .= "}\n"; $write .= "}\n";
$config_file = fopen( 'includes/feature-config.php', 'w' ); $config_file = fopen( 'includes/feature-config.php', 'w' );

View File

@ -2,11 +2,12 @@
# usage: travis.sh before|after # usage: travis.sh before|after
if [ $1 == 'before' ]; then if [ $1 == 'before' ]; then
cd "$WP_CORE_DIR/wp-content/plugins/woocommerce-admin/"
if [[ ${RUN_PHPCS} == 1 ]]; then if [[ ${RUN_PHPCS} == 1 ]]; then
cd "$WP_CORE_DIR/wp-content/plugins/woocommerce-admin/"
# This can (currently) only run for PHP 7.1+
composer install composer install
else
npm run build:feature-config
composer install --no-dev
fi fi
fi fi

View File

@ -30,5 +30,18 @@
"phpcs": "Analyze code against the WordPress coding standards with PHP_CodeSniffer", "phpcs": "Analyze code against the WordPress coding standards with PHP_CodeSniffer",
"phpcbf": "Fix coding standards warnings/errors automatically with PHP Code Beautifier" "phpcbf": "Fix coding standards warnings/errors automatically with PHP Code Beautifier"
} }
},
"autoload": {
"classmap": [
"includes/"
],
"files": [
"includes/core-functions.php",
"includes/feature-config.php",
"includes/page-controller-functions.php"
],
"psr-4": {
"Automattic\\WooCommerce\\Admin\\": "src/"
}
} }
} }

View File

@ -1,7 +1,7 @@
{ {
"_readme": [ "_readme": [
"This file locks the dependencies of your project to a known state", "This file locks the dependencies of your project to a known state",
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "c4e5044bed13f99e535b3d692d12005c", "content-hash": "c4e5044bed13f99e535b3d692d12005c",
@ -1163,7 +1163,6 @@
"mock", "mock",
"xunit" "xunit"
], ],
"abandoned": true,
"time": "2018-08-09T05:50:03+00:00" "time": "2018-08-09T05:50:03+00:00"
}, },
{ {
@ -1970,12 +1969,12 @@
"version": "1.2.1", "version": "1.2.1",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/WordPress/WordPress-Coding-Standards.git", "url": "https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards.git",
"reference": "f328bcafd97377e8e5e5d7b244d5ddbf301a3a5c" "reference": "f328bcafd97377e8e5e5d7b244d5ddbf301a3a5c"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/WordPress/WordPress-Coding-Standards/zipball/f328bcafd97377e8e5e5d7b244d5ddbf301a3a5c", "url": "https://api.github.com/repos/WordPress-Coding-Standards/WordPress-Coding-Standards/zipball/f328bcafd97377e8e5e5d7b244d5ddbf301a3a5c",
"reference": "f328bcafd97377e8e5e5d7b244d5ddbf301a3a5c", "reference": "f328bcafd97377e8e5e5d7b244d5ddbf301a3a5c",
"shasum": "" "shasum": ""
}, },

View File

@ -10,7 +10,7 @@
*/ */
function add_report_register_script() { function add_report_register_script() {
if ( ! class_exists( 'WC_Admin_Loader' ) || ! WC_Admin_Loader::is_admin_page() ) { if ( ! class_exists( 'Automattic\WooCommerce\Admin\Loader' ) || ! \Automattic\WooCommerce\Admin\Loader::is_admin_page() ) {
return; return;
} }

View File

@ -10,7 +10,7 @@
*/ */
function dashboard_section_register_script() { function dashboard_section_register_script() {
if ( ! class_exists( 'WC_Admin_Loader' ) || ! WC_Admin_Loader::is_admin_page() ) { if ( ! class_exists( 'Automattic\WooCommerce\Admin\Loader' ) || ! \Automattic\WooCommerce\Admin\Loader::is_admin_page() ) {
return; return;
} }

View File

@ -10,7 +10,7 @@
*/ */
function table_column_register_script() { function table_column_register_script() {
if ( ! class_exists( 'WC_Admin_Loader' ) || ! WC_Admin_Loader::is_admin_page() ) { if ( ! class_exists( 'Automattic\WooCommerce\Admin\Loader' ) || ! \Automattic\WooCommerce\Admin\Loader::is_admin_page() ) {
return; return;
} }

View File

@ -38,10 +38,10 @@ We also expose CSS classes on the `body` tag, so that you can target specific fe
Feature flags are also available via PHP. To ensure these are consistent with the built client assets, `includes/feature-flags.php` is generated by the plugin build process or `npm start`. Do not edit `includes/feature-flags.php` directly. Feature flags are also available via PHP. To ensure these are consistent with the built client assets, `includes/feature-flags.php` is generated by the plugin build process or `npm start`. Do not edit `includes/feature-flags.php` directly.
To check if a feature is enabled, you can use the `WC_Admin_Loader::is_feature_enabled()`: To check if a feature is enabled, you can use the `Automattic\WooCommerce\Admin\Loader::is_feature_enabled()`:
``` ```
if ( WC_Admin_Loader::is_feature_enabled( 'activity-panels' ) ) { if ( \Automattic\WooCommerce\Admin\Loader::is_feature_enabled( 'activity-panels' ) ) {
add_action( 'admin_header', 'wc_admin_activity_panel' ); add_action( 'admin_header', 'wc_admin_activity_panel' );
} }
``` ```

View File

@ -13,7 +13,7 @@ Connecting pages uses five parameters to `wc_admin_connect_page()`:
* `id` - Identifies the page with the controller. Required. * `id` - Identifies the page with the controller. Required.
* `parent` - Denotes the page as a child of `parent`. Used for breadcrumbs. Optional. * `parent` - Denotes the page as a child of `parent`. Used for breadcrumbs. Optional.
* `screen_id` - Corresponds to [`WC_Admin_Page_Controller::get_current_screen_id()`](../includes/page-controller/class-wc-admin-page-controller.php#L219) to determine the current page. Required. * `screen_id` - Corresponds to [`PageController::get_current_screen_id()`](../includes/page-controller/class-wc-admin-page-controller.php#L219) to determine the current page. Required.
* `title` - Page title. Used to build breadcrumbs. String or array of breadcrumb pieces. Required. * `title` - Page title. Used to build breadcrumbs. String or array of breadcrumb pieces. Required.
* `path` - Page path (relative). Used for linking breadcrumb pieces when this page is a `parent`. Optional. * `path` - Page path (relative). Used for linking breadcrumb pieces when this page is a `parent`. Optional.
@ -132,6 +132,6 @@ add_filter( 'woocommerce_admin_report_menu_items', 'add_report_menu_item' );
### Further Reading ### Further Reading
* Check out the [`WC_Admin_Page_Controller`](../includes/page-controller/class-wc-admin-page-controller.php) class. * Check out the [`PageController`](../includes/page-controller/class-wc-admin-page-controller.php) class.
* See how we're [connecting existing WooCommerce pages](../includes/page-controller/connect-existing-pages.php). * See how we're [connecting existing WooCommerce pages](../includes/page-controller/connect-existing-pages.php).
* See how we're [registering Analytics Reports](../includes/features/analytics/class-wc-admin-analytics.php#L75). * See how we're [registering Analytics Reports](../includes/features/analytics/class-wc-admin-analytics.php#L75).

View File

@ -1,429 +0,0 @@
<?php
/**
* REST API bootstrap.
*
* @package WooCommerce Admin/Classes
*/
defined( 'ABSPATH' ) || exit;
/**
* WC_Admin_Api_Init class.
*/
class WC_Admin_Api_Init {
/**
* Boostrap REST API.
*/
public function __construct() {
// Initialize classes.
add_action( 'plugins_loaded', array( $this, 'init_classes' ), 19 );
// Hook in data stores.
add_filter( 'woocommerce_data_stores', array( 'WC_Admin_Api_Init', 'add_data_stores' ) );
// REST API extensions init.
add_action( 'rest_api_init', array( $this, 'rest_api_init' ) );
add_filter( 'rest_endpoints', array( 'WC_Admin_Api_Init', 'filter_rest_endpoints' ), 10, 1 );
// Add currency symbol to orders endpoint response.
add_filter( 'woocommerce_rest_prepare_shop_order_object', array( __CLASS__, 'add_currency_symbol_to_order_response' ) );
}
/**
* Init classes.
*/
public function init_classes() {
// Interfaces.
require_once WC_ADMIN_ABSPATH . 'includes/interfaces/class-wc-admin-reports-data-store-interface.php';
require_once WC_ADMIN_ABSPATH . 'includes/class-wc-admin-reports-query.php';
// Common date time code.
require_once WC_ADMIN_ABSPATH . 'includes/class-wc-admin-reports-interval.php';
// Exceptions.
require_once WC_ADMIN_ABSPATH . 'includes/class-wc-admin-reports-parameter-exception.php';
// WC Class extensions.
require_once WC_ADMIN_ABSPATH . 'includes/class-wc-admin-order-trait.php';
require_once WC_ADMIN_ABSPATH . 'includes/class-wc-admin-order.php';
require_once WC_ADMIN_ABSPATH . 'includes/class-wc-admin-order-refund.php';
// Segmentation.
require_once WC_ADMIN_ABSPATH . 'includes/class-wc-admin-reports-segmenting.php';
require_once WC_ADMIN_ABSPATH . 'includes/class-wc-admin-reports-orders-stats-segmenting.php';
require_once WC_ADMIN_ABSPATH . 'includes/class-wc-admin-reports-products-stats-segmenting.php';
require_once WC_ADMIN_ABSPATH . 'includes/class-wc-admin-reports-coupons-stats-segmenting.php';
require_once WC_ADMIN_ABSPATH . 'includes/class-wc-admin-reports-taxes-stats-segmenting.php';
// Query classes for reports.
require_once WC_ADMIN_ABSPATH . 'includes/class-wc-admin-reports-revenue-query.php';
require_once WC_ADMIN_ABSPATH . 'includes/class-wc-admin-reports-orders-query.php';
require_once WC_ADMIN_ABSPATH . 'includes/class-wc-admin-reports-orders-stats-query.php';
require_once WC_ADMIN_ABSPATH . 'includes/class-wc-admin-reports-products-query.php';
require_once WC_ADMIN_ABSPATH . 'includes/class-wc-admin-reports-variations-query.php';
require_once WC_ADMIN_ABSPATH . 'includes/class-wc-admin-reports-products-stats-query.php';
require_once WC_ADMIN_ABSPATH . 'includes/class-wc-admin-reports-categories-query.php';
require_once WC_ADMIN_ABSPATH . 'includes/class-wc-admin-reports-taxes-query.php';
require_once WC_ADMIN_ABSPATH . 'includes/class-wc-admin-reports-taxes-stats-query.php';
require_once WC_ADMIN_ABSPATH . 'includes/class-wc-admin-reports-coupons-query.php';
require_once WC_ADMIN_ABSPATH . 'includes/class-wc-admin-reports-coupons-stats-query.php';
require_once WC_ADMIN_ABSPATH . 'includes/class-wc-admin-reports-downloads-query.php';
require_once WC_ADMIN_ABSPATH . 'includes/class-wc-admin-reports-downloads-stats-query.php';
require_once WC_ADMIN_ABSPATH . 'includes/class-wc-admin-reports-customers-query.php';
require_once WC_ADMIN_ABSPATH . 'includes/class-wc-admin-reports-customers-stats-query.php';
require_once WC_ADMIN_ABSPATH . 'includes/class-wc-admin-reports-stock-stats-query.php';
// Data stores.
require_once WC_ADMIN_ABSPATH . 'includes/data-stores/class-wc-admin-reports-data-store.php';
require_once WC_ADMIN_ABSPATH . 'includes/data-stores/class-wc-admin-reports-orders-data-store.php';
require_once WC_ADMIN_ABSPATH . 'includes/data-stores/class-wc-admin-reports-orders-stats-data-store.php';
require_once WC_ADMIN_ABSPATH . 'includes/data-stores/class-wc-admin-reports-products-data-store.php';
require_once WC_ADMIN_ABSPATH . 'includes/data-stores/class-wc-admin-reports-variations-data-store.php';
require_once WC_ADMIN_ABSPATH . 'includes/data-stores/class-wc-admin-reports-products-stats-data-store.php';
require_once WC_ADMIN_ABSPATH . 'includes/data-stores/class-wc-admin-reports-categories-data-store.php';
require_once WC_ADMIN_ABSPATH . 'includes/data-stores/class-wc-admin-reports-taxes-data-store.php';
require_once WC_ADMIN_ABSPATH . 'includes/data-stores/class-wc-admin-reports-taxes-stats-data-store.php';
require_once WC_ADMIN_ABSPATH . 'includes/data-stores/class-wc-admin-reports-coupons-data-store.php';
require_once WC_ADMIN_ABSPATH . 'includes/data-stores/class-wc-admin-reports-coupons-stats-data-store.php';
require_once WC_ADMIN_ABSPATH . 'includes/data-stores/class-wc-admin-reports-downloads-data-store.php';
require_once WC_ADMIN_ABSPATH . 'includes/data-stores/class-wc-admin-reports-downloads-stats-data-store.php';
require_once WC_ADMIN_ABSPATH . 'includes/data-stores/class-wc-admin-reports-customers-data-store.php';
require_once WC_ADMIN_ABSPATH . 'includes/data-stores/class-wc-admin-reports-customers-stats-data-store.php';
require_once WC_ADMIN_ABSPATH . 'includes/data-stores/class-wc-admin-reports-stock-stats-data-store.php';
}
/**
* Init REST API.
*/
public function rest_api_init() {
require_once WC_ADMIN_ABSPATH . 'includes/api/class-wc-admin-rest-admin-notes-controller.php';
require_once WC_ADMIN_ABSPATH . 'includes/api/class-wc-admin-rest-admin-note-action-controller.php';
require_once WC_ADMIN_ABSPATH . 'includes/api/class-wc-admin-rest-coupons-controller.php';
require_once WC_ADMIN_ABSPATH . 'includes/api/class-wc-admin-rest-data-controller.php';
require_once WC_ADMIN_ABSPATH . 'includes/api/class-wc-admin-rest-data-countries-controller.php';
require_once WC_ADMIN_ABSPATH . 'includes/api/class-wc-admin-rest-data-download-ips-controller.php';
require_once WC_ADMIN_ABSPATH . 'includes/api/class-wc-admin-rest-leaderboards-controller.php';
require_once WC_ADMIN_ABSPATH . 'includes/api/class-wc-admin-rest-orders-controller.php';
require_once WC_ADMIN_ABSPATH . 'includes/api/class-wc-admin-rest-products-controller.php';
require_once WC_ADMIN_ABSPATH . 'includes/api/class-wc-admin-rest-product-categories-controller.php';
require_once WC_ADMIN_ABSPATH . 'includes/api/class-wc-admin-rest-product-variations-controller.php';
require_once WC_ADMIN_ABSPATH . 'includes/api/class-wc-admin-rest-product-reviews-controller.php';
require_once WC_ADMIN_ABSPATH . 'includes/api/class-wc-admin-rest-product-variations-controller.php';
require_once WC_ADMIN_ABSPATH . 'includes/api/class-wc-admin-rest-reports-controller.php';
require_once WC_ADMIN_ABSPATH . 'includes/api/class-wc-admin-rest-setting-options-controller.php';
require_once WC_ADMIN_ABSPATH . 'includes/api/class-wc-admin-rest-reports-categories-controller.php';
require_once WC_ADMIN_ABSPATH . 'includes/api/class-wc-admin-rest-reports-coupons-controller.php';
require_once WC_ADMIN_ABSPATH . 'includes/api/class-wc-admin-rest-reports-coupons-stats-controller.php';
require_once WC_ADMIN_ABSPATH . 'includes/api/class-wc-admin-rest-reports-customers-controller.php';
require_once WC_ADMIN_ABSPATH . 'includes/api/class-wc-admin-rest-reports-customers-stats-controller.php';
require_once WC_ADMIN_ABSPATH . 'includes/api/class-wc-admin-rest-reports-downloads-controller.php';
require_once WC_ADMIN_ABSPATH . 'includes/api/class-wc-admin-rest-reports-downloads-files-controller.php';
require_once WC_ADMIN_ABSPATH . 'includes/api/class-wc-admin-rest-reports-downloads-stats-controller.php';
require_once WC_ADMIN_ABSPATH . 'includes/api/class-wc-admin-rest-reports-import-controller.php';
require_once WC_ADMIN_ABSPATH . 'includes/api/class-wc-admin-rest-reports-orders-controller.php';
require_once WC_ADMIN_ABSPATH . 'includes/api/class-wc-admin-rest-reports-orders-stats-controller.php';
require_once WC_ADMIN_ABSPATH . 'includes/api/class-wc-admin-rest-reports-products-controller.php';
require_once WC_ADMIN_ABSPATH . 'includes/api/class-wc-admin-rest-reports-variations-controller.php';
require_once WC_ADMIN_ABSPATH . 'includes/api/class-wc-admin-rest-reports-products-stats-controller.php';
require_once WC_ADMIN_ABSPATH . 'includes/api/class-wc-admin-rest-reports-performance-indicators-controller.php';
require_once WC_ADMIN_ABSPATH . 'includes/api/class-wc-admin-rest-reports-revenue-stats-controller.php';
require_once WC_ADMIN_ABSPATH . 'includes/api/class-wc-admin-rest-reports-taxes-controller.php';
require_once WC_ADMIN_ABSPATH . 'includes/api/class-wc-admin-rest-reports-taxes-stats-controller.php';
require_once WC_ADMIN_ABSPATH . 'includes/api/class-wc-admin-rest-reports-stock-controller.php';
require_once WC_ADMIN_ABSPATH . 'includes/api/class-wc-admin-rest-reports-stock-stats-controller.php';
require_once WC_ADMIN_ABSPATH . 'includes/api/class-wc-admin-rest-taxes-controller.php';
require_once WC_ADMIN_ABSPATH . 'includes/api/class-wc-admin-rest-themes-controller.php';
require_once WC_ADMIN_ABSPATH . 'includes/api/class-wc-admin-rest-customers-controller.php';
require_once WC_ADMIN_ABSPATH . 'includes/api/class-wc-admin-rest-reports-export-controller.php';
$controllers = array(
'WC_Admin_REST_Admin_Notes_Controller',
'WC_Admin_REST_Admin_Note_Action_Controller',
'WC_Admin_REST_Coupons_Controller',
'WC_Admin_REST_Customers_Controller',
'WC_Admin_REST_Data_Controller',
'WC_Admin_REST_Data_Countries_Controller',
'WC_Admin_REST_Data_Download_Ips_Controller',
'WC_Admin_REST_Leaderboards_Controller',
'WC_Admin_REST_Orders_Controller',
'WC_Admin_REST_Products_Controller',
'WC_Admin_REST_Product_Categories_Controller',
'WC_Admin_REST_Product_Variations_Controller',
'WC_Admin_REST_Product_Reviews_Controller',
'WC_Admin_REST_Product_Variations_Controller',
'WC_Admin_REST_Reports_Controller',
'WC_Admin_REST_Setting_Options_Controller',
'WC_Admin_REST_Reports_Import_Controller',
'WC_Admin_REST_Reports_Export_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',
'WC_Admin_REST_Reports_Orders_Controller',
'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_Stock_Stats_Controller',
'WC_Admin_REST_Reports_Downloads_Controller',
'WC_Admin_REST_Reports_Downloads_Stats_Controller',
'WC_Admin_REST_Reports_Customers_Controller',
'WC_Admin_REST_Reports_Customers_Stats_Controller',
'WC_Admin_REST_Taxes_Controller',
'WC_Admin_REST_Themes_Controller',
);
if ( WC_Admin_Loader::is_feature_enabled( 'onboarding' ) ) {
require_once WC_ADMIN_ABSPATH . 'includes/api/class-wc-admin-rest-onboarding-levels-controller.php';
require_once WC_ADMIN_ABSPATH . 'includes/api/class-wc-admin-rest-onboarding-profile-controller.php';
require_once WC_ADMIN_ABSPATH . 'includes/api/class-wc-admin-rest-onboarding-plugins-controller.php';
$controllers = array_merge(
$controllers,
array(
'WC_Admin_REST_Onboarding_Levels_Controller',
'WC_Admin_REST_Onboarding_Profile_Controller',
'WC_Admin_REST_Onboarding_Plugins_Controller',
)
);
}
// The performance indicators controller must be registered last, after other /stats endpoints have been registered.
$controllers[] = 'WC_Admin_REST_Reports_Performance_Indicators_Controller';
$controllers = apply_filters( 'woocommerce_admin_rest_controllers', $controllers );
foreach ( $controllers as $controller ) {
$this->$controller = new $controller();
$this->$controller->register_routes();
}
}
/**
* Filter REST API endpoints.
*
* @param array $endpoints List of endpoints.
* @return array
*/
public static function filter_rest_endpoints( $endpoints ) {
// 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
) {
$endpoints['/wc/v4/system_status/tools'][0] = $endpoints['/wc/v4/system_status/tools'][1];
}
// // 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
) {
$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];
}
// 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
) {
$endpoints['/wc/v4/reports'][0] = $endpoints['/wc/v4/reports'][1];
}
// 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
) {
$endpoints['/wc/v4/coupons'][0] = $endpoints['/wc/v4/coupons'][2];
$endpoints['/wc/v4/coupons'][1] = $endpoints['/wc/v4/coupons'][3];
}
// 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
) {
$endpoints['/wc/v4/customers'][0] = $endpoints['/wc/v4/customers'][2];
$endpoints['/wc/v4/customers'][1] = $endpoints['/wc/v4/customers'][3];
}
// 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
) {
$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];
}
// 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
) {
$endpoints['/wc/v4/orders'][0] = $endpoints['/wc/v4/orders'][2];
$endpoints['/wc/v4/orders'][1] = $endpoints['/wc/v4/orders'][3];
}
// 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
) {
$endpoints['/wc/v4/data'][0] = $endpoints['/wc/v4/data'][1];
}
// 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
) {
$endpoints['/wc/v4/products'][0] = $endpoints['/wc/v4/products'][2];
$endpoints['/wc/v4/products'][1] = $endpoints['/wc/v4/products'][3];
}
// 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
) {
$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];
}
// 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];
}
// 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];
}
// Override /wc/v4/products/$product_id/variations.
if ( isset( $endpoints['products/(?P<product_id>[\d]+)/variations'] )
&& isset( $endpoints['products/(?P<product_id>[\d]+)/variations'][3] )
&& isset( $endpoints['products/(?P<product_id>[\d]+)/variations'][2] )
&& $endpoints['products/(?P<product_id>[\d]+)/variations'][2]['callback'][0] instanceof WC_Admin_REST_Product_Variations_Controller
&& $endpoints['products/(?P<product_id>[\d]+)/variations'][3]['callback'][0] instanceof WC_Admin_REST_Product_Variations_Controller
) {
$endpoints['products/(?P<product_id>[\d]+)/variations'][0] = $endpoints['products/(?P<product_id>[\d]+)/variations'][2];
$endpoints['products/(?P<product_id>[\d]+)/variations'][1] = $endpoints['products/(?P<product_id>[\d]+)/variations'][3];
}
// 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];
}
// Override /wc/v4/settings/$group_id.
if ( isset( $endpoints['/wc/v4/settings/(?P<group_id>[\w-]+)'] )
&& isset( $endpoints['/wc/v4/settings/(?P<group_id>[\w-]+)'][5] )
&& isset( $endpoints['/wc/v4/settings/(?P<group_id>[\w-]+)'][4] )
&& isset( $endpoints['/wc/v4/settings/(?P<group_id>[\w-]+)'][3] )
&& $endpoints['/wc/v4/settings/(?P<group_id>[\w-]+)'][3]['callback'][0] instanceof WC_Admin_REST_Setting_Options_Controller
&& $endpoints['/wc/v4/settings/(?P<group_id>[\w-]+)'][4]['callback'][0] instanceof WC_Admin_REST_Setting_Options_Controller
&& $endpoints['/wc/v4/settings/(?P<group_id>[\w-]+)'][5]['callback'][0] instanceof WC_Admin_REST_Setting_Options_Controller
) {
$endpoints['/wc/v4/settings/(?P<group_id>[\w-]+)'][0] = $endpoints['/wc/v4/settings/(?P<group_id>[\w-]+)'][3];
$endpoints['/wc/v4/settings/(?P<group_id>[\w-]+)'][1] = $endpoints['/wc/v4/settings/(?P<group_id>[\w-]+)'][4];
$endpoints['/wc/v4/settings/(?P<group_id>[\w-]+)'][2] = $endpoints['/wc/v4/settings/(?P<group_id>[\w-]+)'][5];
}
return $endpoints;
}
/**
* Adds data stores.
*
* @param array $data_stores List of data stores.
* @return array
*/
public static function add_data_stores( $data_stores ) {
return array_merge(
$data_stores,
array(
'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',
'report-downloads-stats' => 'WC_Admin_Reports_Downloads_Stats_Data_Store',
'admin-note' => 'WC_Admin_Notes_Data_Store',
'report-customers' => 'WC_Admin_Reports_Customers_Data_Store',
'report-customers-stats' => 'WC_Admin_Reports_Customers_Stats_Data_Store',
'report-stock-stats' => 'WC_Admin_Reports_Stock_Stats_Data_Store',
)
);
}
/**
* 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;
}
}
new WC_Admin_Api_Init();

View File

@ -9,6 +9,8 @@
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
use \Automattic\WooCommerce\Admin\Loader;
/** /**
* Format a number using the decimal and thousands separator settings in WooCommerce. * Format a number using the decimal and thousands separator settings in WooCommerce.
* *
@ -16,7 +18,7 @@ defined( 'ABSPATH' ) || exit;
* @return string * @return string
*/ */
function wc_admin_number_format( $number ) { function wc_admin_number_format( $number ) {
$currency_settings = WC_Admin_Loader::get_currency_settings(); $currency_settings = Loader::get_currency_settings();
return number_format( return number_format(
$number, $number,
0, 0,

View File

@ -1,61 +1,63 @@
<?php <?php
/** /**
* Convenience functions for WC_Admin_Page_Controller. * Convenience functions for PageController.
* *
* @package Woocommerce Admin * @package Woocommerce Admin
*/ */
use Automattic\WooCommerce\Admin\PageController;
/** /**
* Connect an existing page to WooCommerce Admin. * Connect an existing page to WooCommerce Admin.
* Passthrough to WC_Admin_Page_Controller::connect_page(). * Passthrough to PageController::connect_page().
* *
* @param array $options Options for WC_Admin_Page_Controller::connect_page(). * @param array $options Options for PageController::connect_page().
*/ */
function wc_admin_connect_page( $options ) { function wc_admin_connect_page( $options ) {
$controller = WC_Admin_Page_Controller::get_instance(); $controller = PageController::get_instance();
$controller->connect_page( $options ); $controller->connect_page( $options );
} }
/** /**
* Register JS-powered WooCommerce Admin Page. * Register JS-powered WooCommerce Admin Page.
* Passthrough to WC_Admin_Page_Controller::register_page(). * Passthrough to PageController::register_page().
* *
* @param array $options Options for WC_Admin_Page_Controller::register_page(). * @param array $options Options for PageController::register_page().
*/ */
function wc_admin_register_page( $options ) { function wc_admin_register_page( $options ) {
$controller = WC_Admin_Page_Controller::get_instance(); $controller = PageController::get_instance();
$controller->register_page( $options ); $controller->register_page( $options );
} }
/** /**
* Is this page connected to WooCommerce Admin? * Is this page connected to WooCommerce Admin?
* Passthrough to WC_Admin_Page_Controller::is_connected_page(). * Passthrough to PageController::is_connected_page().
* *
* @return boolean True if the page is connected to WooCommerce Admin. * @return boolean True if the page is connected to WooCommerce Admin.
*/ */
function wc_admin_is_connected_page() { function wc_admin_is_connected_page() {
$controller = WC_Admin_Page_Controller::get_instance(); $controller = PageController::get_instance();
return $controller->is_connected_page(); return $controller->is_connected_page();
} }
/** /**
* Is this a WooCommerce Admin Page? * Is this a WooCommerce Admin Page?
* Passthrough to WC_Admin_Page_Controller::is_registered_page(). * Passthrough to PageController::is_registered_page().
* *
* @return boolean True if the page is a WooCommerce Admin page. * @return boolean True if the page is a WooCommerce Admin page.
*/ */
function wc_admin_is_registered_page() { function wc_admin_is_registered_page() {
$controller = WC_Admin_Page_Controller::get_instance(); $controller = PageController::get_instance();
return $controller->is_registered_page(); return $controller->is_registered_page();
} }
/** /**
* Get breadcrumbs for WooCommerce Admin Page navigation. * Get breadcrumbs for WooCommerce Admin Page navigation.
* Passthrough to WC_Admin_Page_Controller::get_breadcrumbs(). * Passthrough to PageController::get_breadcrumbs().
* *
* @return array Navigation pieces (breadcrumbs). * @return array Navigation pieces (breadcrumbs).
*/ */
function wc_admin_get_breadcrumbs() { function wc_admin_get_breadcrumbs() {
$controller = WC_Admin_Page_Controller::get_instance(); $controller = PageController::get_instance();
return $controller->get_breadcrumbs(); return $controller->get_breadcrumbs();
} }

View File

@ -7,6 +7,8 @@
* @package WooCommerce Admin/API * @package WooCommerce Admin/API
*/ */
namespace Automattic\WooCommerce\Admin\API;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
/** /**
@ -15,7 +17,7 @@ defined( 'ABSPATH' ) || exit;
* @package WooCommerce Admin/API * @package WooCommerce Admin/API
* @extends WC_REST_Coupons_Controller * @extends WC_REST_Coupons_Controller
*/ */
class WC_Admin_REST_Coupons_Controller extends WC_REST_Coupons_Controller { class Coupons extends \WC_REST_Coupons_Controller {
/** /**
* Endpoint namespace. * Endpoint namespace.

View File

@ -7,15 +7,17 @@
* @package WooCommerce Admin/API * @package WooCommerce Admin/API
*/ */
namespace Automattic\WooCommerce\Admin\API;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
/** /**
* Customers controller. * Customers controller.
* *
* @package WooCommerce Admin/API * @package WooCommerce Admin/API
* @extends WC_Admin_REST_Reports_Customers_Controller * @extends \Automattic\WooCommerce\Admin\API\Reports\Customers\Controller
*/ */
class WC_Admin_REST_Customers_Controller extends WC_Admin_REST_Reports_Customers_Controller { class Customers extends \Automattic\WooCommerce\Admin\API\Reports\Customers\Controller {
/** /**
* Route base. * Route base.

View File

@ -7,6 +7,8 @@
* @package WooCommerce Admin/API * @package WooCommerce Admin/API
*/ */
namespace Automattic\WooCommerce\Admin\API;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
/** /**
@ -15,7 +17,7 @@ defined( 'ABSPATH' ) || exit;
* @package WooCommerce Admin/API * @package WooCommerce Admin/API
* @extends WC_REST_Data_Controller * @extends WC_REST_Data_Controller
*/ */
class WC_Admin_REST_Data_Controller extends WC_REST_Data_Controller { class Data extends \WC_REST_Data_Controller {
/** /**
* Endpoint namespace. * Endpoint namespace.

View File

@ -7,6 +7,8 @@
* @package WooCommerce Admin/API * @package WooCommerce Admin/API
*/ */
namespace Automattic\WooCommerce\Admin\API;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
/** /**
@ -15,7 +17,7 @@ defined( 'ABSPATH' ) || exit;
* @package WooCommerce Admin/API * @package WooCommerce Admin/API
* @extends WC_REST_Data_Countries_Controller * @extends WC_REST_Data_Countries_Controller
*/ */
class WC_Admin_REST_Data_Countries_Controller extends WC_REST_Data_Countries_Controller { class DataCountries extends \WC_REST_Data_Countries_Controller {
/** /**
* Endpoint namespace. * Endpoint namespace.

View File

@ -7,6 +7,8 @@
* @package WooCommerce Admin/API * @package WooCommerce Admin/API
*/ */
namespace Automattic\WooCommerce\Admin\API;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
/** /**
@ -15,7 +17,7 @@ defined( 'ABSPATH' ) || exit;
* @package WooCommerce Admin/API * @package WooCommerce Admin/API
* @extends WC_REST_Data_Controller * @extends WC_REST_Data_Controller
*/ */
class WC_Admin_REST_Data_Download_Ips_Controller extends WC_REST_Data_Controller { class DataDownloadIPs extends \WC_REST_Data_Controller {
/** /**
* Endpoint namespace. * Endpoint namespace.
* *
@ -41,7 +43,7 @@ class WC_Admin_REST_Data_Download_Ips_Controller extends WC_REST_Data_Controller
'/' . $this->rest_base, '/' . $this->rest_base,
array( array(
array( array(
'methods' => WP_REST_Server::READABLE, 'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_items' ), 'callback' => array( $this, 'get_items' ),
'permission_callback' => array( $this, 'get_items_permissions_check' ), 'permission_callback' => array( $this, 'get_items_permissions_check' ),
), ),
@ -70,7 +72,7 @@ class WC_Admin_REST_Data_Download_Ips_Controller extends WC_REST_Data_Controller
) )
); );
} else { } else {
return new WP_Error( 'woocommerce_rest_data_download_ips_invalid_request', __( 'Invalid request. Please pass the match parameter.', 'woocommerce-admin' ), array( 'status' => 400 ) ); return new \WP_Error( 'woocommerce_rest_data_download_ips_invalid_request', __( 'Invalid request. Please pass the match parameter.', 'woocommerce-admin' ), array( 'status' => 400 ) );
} }
$data = array(); $data = array();

View File

@ -0,0 +1,322 @@
<?php
/**
* REST API bootstrap.
*
* @package WooCommerce Admin/Classes
*/
namespace Automattic\WooCommerce\Admin\API;
defined( 'ABSPATH' ) || exit;
use \Automattic\WooCommerce\Admin\Loader;
/**
* Init class.
*/
class Init {
/**
* Boostrap REST API.
*/
public function __construct() {
// Hook in data stores.
add_filter( 'woocommerce_data_stores', array( __CLASS__, 'add_data_stores' ) );
// REST API extensions init.
add_action( 'rest_api_init', array( $this, 'rest_api_init' ) );
add_filter( 'rest_endpoints', array( __CLASS__, 'filter_rest_endpoints' ), 10, 1 );
// Add currency symbol to orders endpoint response.
add_filter( 'woocommerce_rest_prepare_shop_order_object', array( __CLASS__, 'add_currency_symbol_to_order_response' ) );
}
/**
* Init REST API.
*/
public function rest_api_init() {
$controllers = array(
'Automattic\WooCommerce\Admin\API\Notes',
'Automattic\WooCommerce\Admin\API\NoteActions',
'Automattic\WooCommerce\Admin\API\Coupons',
'Automattic\WooCommerce\Admin\API\Customers',
'Automattic\WooCommerce\Admin\API\Data',
'Automattic\WooCommerce\Admin\API\DataCountries',
'Automattic\WooCommerce\Admin\API\DataDownloadIPs',
'Automattic\WooCommerce\Admin\API\Leaderboards',
'Automattic\WooCommerce\Admin\API\Orders',
'Automattic\WooCommerce\Admin\API\Products',
'Automattic\WooCommerce\Admin\API\ProductCategories',
'Automattic\WooCommerce\Admin\API\ProductVariations',
'Automattic\WooCommerce\Admin\API\ProductReviews',
'Automattic\WooCommerce\Admin\API\ProductVariations',
'Automattic\WooCommerce\Admin\API\Reports\Controller',
'Automattic\WooCommerce\Admin\API\SettingOptions',
'Automattic\WooCommerce\Admin\API\Reports\Import\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Export\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Products\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Variations\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Products\Stats\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Revenue\Stats\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Orders\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Orders\Stats\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Categories\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Taxes\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Taxes\Stats\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Coupons\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Coupons\Stats\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Stock\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Stock\Stats\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Downloads\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Downloads\Stats\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Customers\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Customers\Stats\Controller',
'Automattic\WooCommerce\Admin\API\Taxes',
'Automattic\WooCommerce\Admin\API\Themes',
);
if ( Loader::is_feature_enabled( 'onboarding' ) ) {
$controllers = array_merge(
$controllers,
array(
'Automattic\WooCommerce\Admin\API\OnboardingLevels',
'Automattic\WooCommerce\Admin\API\OnboardingProfile',
'Automattic\WooCommerce\Admin\API\OnboardingPlugins',
)
);
}
// The performance indicators controller must be registered last, after other /stats endpoints have been registered.
$controllers[] = 'Automattic\WooCommerce\Admin\API\Reports\PerformanceIndicators\Controller';
$controllers = apply_filters( 'woocommerce_admin_rest_controllers', $controllers );
foreach ( $controllers as $controller ) {
$this->$controller = new $controller();
$this->$controller->register_routes();
}
}
/**
* Filter REST API endpoints.
*
* @param array $endpoints List of endpoints.
* @return array
*/
public static function filter_rest_endpoints( $endpoints ) {
// 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
) {
$endpoints['/wc/v4/system_status/tools'][0] = $endpoints['/wc/v4/system_status/tools'][1];
}
// // 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
) {
$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];
}
// 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
) {
$endpoints['/wc/v4/reports'][0] = $endpoints['/wc/v4/reports'][1];
}
// 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 \Automattic\WooCommerce\Admin\API\Orders
&& $endpoints['/wc/v4/coupons'][3]['callback'][0] instanceof \Automattic\WooCommerce\Admin\API\Orders
) {
$endpoints['/wc/v4/coupons'][0] = $endpoints['/wc/v4/coupons'][2];
$endpoints['/wc/v4/coupons'][1] = $endpoints['/wc/v4/coupons'][3];
}
// 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 \Automattic\WooCommerce\Admin\API\Customers
&& $endpoints['/wc/v4/customers'][3]['callback'][0] instanceof \Automattic\WooCommerce\Admin\API\Customers
) {
$endpoints['/wc/v4/customers'][0] = $endpoints['/wc/v4/customers'][2];
$endpoints['/wc/v4/customers'][1] = $endpoints['/wc/v4/customers'][3];
}
// 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 \Automattic\WooCommerce\Admin\API\Orders
&& $endpoints['/wc/v4/orders/(?P<id>[\d]+)'][4]['callback'][0] instanceof \Automattic\WooCommerce\Admin\API\Orders
&& $endpoints['/wc/v4/orders/(?P<id>[\d]+)'][5]['callback'][0] instanceof \Automattic\WooCommerce\Admin\API\Orders
) {
$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];
}
// 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 \Automattic\WooCommerce\Admin\API\Orders
&& $endpoints['/wc/v4/orders'][3]['callback'][0] instanceof \Automattic\WooCommerce\Admin\API\Orders
) {
$endpoints['/wc/v4/orders'][0] = $endpoints['/wc/v4/orders'][2];
$endpoints['/wc/v4/orders'][1] = $endpoints['/wc/v4/orders'][3];
}
// Override /wc/v4/data.
if ( isset( $endpoints['/wc/v4/data'] )
&& isset( $endpoints['/wc/v4/data'][1] )
&& $endpoints['/wc/v4/data'][1]['callback'][0] instanceof \Automattic\WooCommerce\Admin\API\Data
) {
$endpoints['/wc/v4/data'][0] = $endpoints['/wc/v4/data'][1];
}
// 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 \Automattic\WooCommerce\Admin\API\Products
&& $endpoints['/wc/v4/products'][3]['callback'][0] instanceof \Automattic\WooCommerce\Admin\API\Products
) {
$endpoints['/wc/v4/products'][0] = $endpoints['/wc/v4/products'][2];
$endpoints['/wc/v4/products'][1] = $endpoints['/wc/v4/products'][3];
}
// 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 \Automattic\WooCommerce\Admin\API\Products
&& $endpoints['/wc/v4/products/(?P<id>[\d]+)'][4]['callback'][0] instanceof \Automattic\WooCommerce\Admin\API\Products
&& $endpoints['/wc/v4/products/(?P<id>[\d]+)'][5]['callback'][0] instanceof \Automattic\WooCommerce\Admin\API\Products
) {
$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];
}
// 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 \Automattic\WooCommerce\Admin\API\ProductCategories
&& $endpoints['/wc/v4/products/categories'][3]['callback'][0] instanceof \Automattic\WooCommerce\Admin\API\ProductCategories
) {
$endpoints['/wc/v4/products/categories'][0] = $endpoints['/wc/v4/products/categories'][2];
$endpoints['/wc/v4/products/categories'][1] = $endpoints['/wc/v4/products/categories'][3];
}
// 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 \Automattic\WooCommerce\Admin\API\ProductReviews
&& $endpoints['/wc/v4/products/reviews'][3]['callback'][0] instanceof \Automattic\WooCommerce\Admin\API\ProductReviews
) {
$endpoints['/wc/v4/products/reviews'][0] = $endpoints['/wc/v4/products/reviews'][2];
$endpoints['/wc/v4/products/reviews'][1] = $endpoints['/wc/v4/products/reviews'][3];
}
// Override /wc/v4/products/$product_id/variations.
if ( isset( $endpoints['products/(?P<product_id>[\d]+)/variations'] )
&& isset( $endpoints['products/(?P<product_id>[\d]+)/variations'][3] )
&& isset( $endpoints['products/(?P<product_id>[\d]+)/variations'][2] )
&& $endpoints['products/(?P<product_id>[\d]+)/variations'][2]['callback'][0] instanceof \Automattic\WooCommerce\Admin\API\ProductVariations
&& $endpoints['products/(?P<product_id>[\d]+)/variations'][3]['callback'][0] instanceof \Automattic\WooCommerce\Admin\API\ProductVariations
) {
$endpoints['products/(?P<product_id>[\d]+)/variations'][0] = $endpoints['products/(?P<product_id>[\d]+)/variations'][2];
$endpoints['products/(?P<product_id>[\d]+)/variations'][1] = $endpoints['products/(?P<product_id>[\d]+)/variations'][3];
}
// 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 \Automattic\WooCommerce\Admin\API\Orders
&& $endpoints['/wc/v4/taxes'][3]['callback'][0] instanceof \Automattic\WooCommerce\Admin\API\Orders
) {
$endpoints['/wc/v4/taxes'][0] = $endpoints['/wc/v4/taxes'][2];
$endpoints['/wc/v4/taxes'][1] = $endpoints['/wc/v4/taxes'][3];
}
// Override /wc/v4/settings/$group_id.
if ( isset( $endpoints['/wc/v4/settings/(?P<group_id>[\w-]+)'] )
&& isset( $endpoints['/wc/v4/settings/(?P<group_id>[\w-]+)'][5] )
&& isset( $endpoints['/wc/v4/settings/(?P<group_id>[\w-]+)'][4] )
&& isset( $endpoints['/wc/v4/settings/(?P<group_id>[\w-]+)'][3] )
&& $endpoints['/wc/v4/settings/(?P<group_id>[\w-]+)'][3]['callback'][0] instanceof \Automattic\WooCommerce\Admin\API\SettingOptions
&& $endpoints['/wc/v4/settings/(?P<group_id>[\w-]+)'][4]['callback'][0] instanceof \Automattic\WooCommerce\Admin\API\SettingOptions
&& $endpoints['/wc/v4/settings/(?P<group_id>[\w-]+)'][5]['callback'][0] instanceof \Automattic\WooCommerce\Admin\API\SettingOptions
) {
$endpoints['/wc/v4/settings/(?P<group_id>[\w-]+)'][0] = $endpoints['/wc/v4/settings/(?P<group_id>[\w-]+)'][3];
$endpoints['/wc/v4/settings/(?P<group_id>[\w-]+)'][1] = $endpoints['/wc/v4/settings/(?P<group_id>[\w-]+)'][4];
$endpoints['/wc/v4/settings/(?P<group_id>[\w-]+)'][2] = $endpoints['/wc/v4/settings/(?P<group_id>[\w-]+)'][5];
}
return $endpoints;
}
/**
* Adds data stores.
*
* @param array $data_stores List of data stores.
* @return array
*/
public static function add_data_stores( $data_stores ) {
return array_merge(
$data_stores,
array(
'report-revenue-stats' => 'Automattic\WooCommerce\Admin\API\Reports\Orders\Stats\DataStore',
'report-orders' => 'Automattic\WooCommerce\Admin\API\Reports\Orders\DataStore',
'report-orders-stats' => 'Automattic\WooCommerce\Admin\API\Reports\Orders\Stats\DataStore',
'report-products' => 'Automattic\WooCommerce\Admin\API\Reports\Products\DataStore',
'report-variations' => 'Automattic\WooCommerce\Admin\API\Reports\Variations\DataStore',
'report-products-stats' => 'Automattic\WooCommerce\Admin\API\Reports\Products\Stats\DataStore',
'report-categories' => 'Automattic\WooCommerce\Admin\API\Reports\Categories\DataStore',
'report-taxes' => 'Automattic\WooCommerce\Admin\API\Reports\Taxes\DataStore',
'report-taxes-stats' => 'Automattic\WooCommerce\Admin\API\Reports\Taxes\Stats\DataStore',
'report-coupons' => 'Automattic\WooCommerce\Admin\API\Reports\Coupons\DataStore',
'report-coupons-stats' => 'Automattic\WooCommerce\Admin\API\Reports\Coupons\Stats\DataStore',
'report-downloads' => 'Automattic\WooCommerce\Admin\API\Reports\Downloads\DataStore',
'report-downloads-stats' => 'Automattic\WooCommerce\Admin\API\Reports\Downloads\Stats\DataStore',
'admin-note' => 'Automattic\WooCommerce\Admin\Notes\DataStore',
'report-customers' => 'Automattic\WooCommerce\Admin\API\Reports\Customers\DataStore',
'report-customers-stats' => 'Automattic\WooCommerce\Admin\API\Reports\Customers\Stats\DataStore',
'report-stock-stats' => 'Automattic\WooCommerce\Admin\API\Reports\Stock\Stats\DataStore',
)
);
}
/**
* 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;
}
}

View File

@ -7,15 +7,22 @@
* @package WooCommerce Admin/API * @package WooCommerce Admin/API
*/ */
namespace Automattic\WooCommerce\Admin\API;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
use \Automattic\WooCommerce\Admin\API\Reports\Categories\DataStore as CategoriesDataStore;
use \Automattic\WooCommerce\Admin\API\Reports\Coupons\DataStore as CouponsDataStore;
use \Automattic\WooCommerce\Admin\API\Reports\Customers\DataStore as CustomersDataStore;
use \Automattic\WooCommerce\Admin\API\Reports\Products\DataStore as ProductsDataStore;
/** /**
* Leaderboards controller. * Leaderboards controller.
* *
* @package WooCommerce Admin/API * @package WooCommerce Admin/API
* @extends WC_REST_Data_Controller * @extends WC_REST_Data_Controller
*/ */
class WC_Admin_REST_Leaderboards_Controller extends WC_REST_Data_Controller { class Leaderboards extends \WC_REST_Data_Controller {
/** /**
* Endpoint namespace. * Endpoint namespace.
* *
@ -39,7 +46,7 @@ class WC_Admin_REST_Leaderboards_Controller extends WC_REST_Data_Controller {
'/' . $this->rest_base, '/' . $this->rest_base,
array( array(
array( array(
'methods' => WP_REST_Server::READABLE, 'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_items' ), 'callback' => array( $this, 'get_items' ),
'permission_callback' => array( $this, 'get_items_permissions_check' ), 'permission_callback' => array( $this, 'get_items_permissions_check' ),
'args' => $this->get_collection_params(), 'args' => $this->get_collection_params(),
@ -53,7 +60,7 @@ class WC_Admin_REST_Leaderboards_Controller extends WC_REST_Data_Controller {
'/' . $this->rest_base . '/allowed', '/' . $this->rest_base . '/allowed',
array( array(
array( array(
'methods' => WP_REST_Server::READABLE, 'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_allowed_items' ), 'callback' => array( $this, 'get_allowed_items' ),
'permission_callback' => array( $this, 'get_items_permissions_check' ), 'permission_callback' => array( $this, 'get_items_permissions_check' ),
), ),
@ -71,7 +78,7 @@ class WC_Admin_REST_Leaderboards_Controller extends WC_REST_Data_Controller {
* @param string $persisted_query URL query string. * @param string $persisted_query URL query string.
*/ */
public function get_coupons_leaderboard( $per_page, $after, $before, $persisted_query ) { public function get_coupons_leaderboard( $per_page, $after, $before, $persisted_query ) {
$coupons_data_store = new WC_Admin_Reports_Coupons_Data_Store(); $coupons_data_store = new CouponsDataStore();
$coupons_data = $per_page > 0 ? $coupons_data_store->get_data( $coupons_data = $per_page > 0 ? $coupons_data_store->get_data(
array( array(
'orderby' => 'orders_count', 'orderby' => 'orders_count',
@ -137,7 +144,7 @@ class WC_Admin_REST_Leaderboards_Controller extends WC_REST_Data_Controller {
* @param string $persisted_query URL query string. * @param string $persisted_query URL query string.
*/ */
public function get_categories_leaderboard( $per_page, $after, $before, $persisted_query ) { public function get_categories_leaderboard( $per_page, $after, $before, $persisted_query ) {
$categories_data_store = new WC_Admin_Reports_Categories_Data_Store(); $categories_data_store = new CategoriesDataStore();
$categories_data = $per_page > 0 ? $categories_data_store->get_data( $categories_data = $per_page > 0 ? $categories_data_store->get_data(
array( array(
'orderby' => 'items_sold', 'orderby' => 'items_sold',
@ -203,7 +210,7 @@ class WC_Admin_REST_Leaderboards_Controller extends WC_REST_Data_Controller {
* @param string $persisted_query URL query string. * @param string $persisted_query URL query string.
*/ */
public function get_customers_leaderboard( $per_page, $after, $before, $persisted_query ) { public function get_customers_leaderboard( $per_page, $after, $before, $persisted_query ) {
$customers_data_store = new WC_Admin_Reports_Customers_Data_Store(); $customers_data_store = new CustomersDataStore();
$customers_data = $per_page > 0 ? $customers_data_store->get_data( $customers_data = $per_page > 0 ? $customers_data_store->get_data(
array( array(
'orderby' => 'total_spend', 'orderby' => 'total_spend',
@ -267,7 +274,7 @@ class WC_Admin_REST_Leaderboards_Controller extends WC_REST_Data_Controller {
* @param string $persisted_query URL query string. * @param string $persisted_query URL query string.
*/ */
public function get_products_leaderboard( $per_page, $after, $before, $persisted_query ) { public function get_products_leaderboard( $per_page, $after, $before, $persisted_query ) {
$products_data_store = new WC_Admin_Reports_Products_Data_Store(); $products_data_store = new ProductsDataStore();
$products_data = $per_page > 0 ? $products_data_store->get_data( $products_data = $per_page > 0 ? $products_data_store->get_data(
array( array(
'orderby' => 'items_sold', 'orderby' => 'items_sold',

View File

@ -7,15 +7,19 @@
* @package WooCommerce Admin/API * @package WooCommerce Admin/API
*/ */
namespace Automattic\WooCommerce\Admin\API;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
use \Automattic\WooCommerce\Admin\Notes\WC_Admin_Notes;
/** /**
* REST API Admin Note Action controller class. * REST API Admin Note Action controller class.
* *
* @package WooCommerce/API * @package WooCommerce/API
* @extends WC_REST_CRUD_Controller * @extends WC_REST_CRUD_Controller
*/ */
class WC_Admin_REST_Admin_Note_Action_Controller extends WC_Admin_REST_Admin_Notes_Controller { class NoteActions extends Notes {
/** /**
* Register the routes for admin notes. * Register the routes for admin notes.
@ -36,7 +40,7 @@ class WC_Admin_REST_Admin_Note_Action_Controller extends WC_Admin_REST_Admin_Not
), ),
), ),
array( array(
'methods' => WP_REST_Server::EDITABLE, 'methods' => \WP_REST_Server::EDITABLE,
'callback' => array( $this, 'trigger_note_action' ), 'callback' => array( $this, 'trigger_note_action' ),
// @todo - double check these permissions for taking note actions. // @todo - double check these permissions for taking note actions.
'permission_callback' => array( $this, 'get_item_permissions_check' ), 'permission_callback' => array( $this, 'get_item_permissions_check' ),
@ -56,7 +60,7 @@ class WC_Admin_REST_Admin_Note_Action_Controller extends WC_Admin_REST_Admin_Not
$note = WC_Admin_Notes::get_note( $request->get_param( 'note_id' ) ); $note = WC_Admin_Notes::get_note( $request->get_param( 'note_id' ) );
if ( ! $note ) { if ( ! $note ) {
return new WP_Error( return new \WP_Error(
'woocommerce_admin_notes_invalid_id', 'woocommerce_admin_notes_invalid_id',
__( 'Sorry, there is no resource with that ID.', 'woocommerce-admin' ), __( 'Sorry, there is no resource with that ID.', 'woocommerce-admin' ),
array( 'status' => 404 ) array( 'status' => 404 )
@ -75,7 +79,7 @@ class WC_Admin_REST_Admin_Note_Action_Controller extends WC_Admin_REST_Admin_Not
} }
if ( ! $triggered_action ) { if ( ! $triggered_action ) {
return new WP_Error( return new \WP_Error(
'woocommerce_admin_note_action_invalid_id', 'woocommerce_admin_note_action_invalid_id',
__( 'Sorry, there is no resource with that ID.', 'woocommerce-admin' ), __( 'Sorry, there is no resource with that ID.', 'woocommerce-admin' ),
array( 'status' => 404 ) array( 'status' => 404 )

View File

@ -7,15 +7,20 @@
* @package WooCommerce Admin/API * @package WooCommerce Admin/API
*/ */
namespace Automattic\WooCommerce\Admin\API;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\Notes\WC_Admin_Note;
use Automattic\WooCommerce\Admin\Notes\WC_Admin_Notes;
/** /**
* REST API Admin Notes controller class. * REST API Admin Notes controller class.
* *
* @package WooCommerce/API * @package WooCommerce/API
* @extends WC_REST_CRUD_Controller * @extends WC_REST_CRUD_Controller
*/ */
class WC_Admin_REST_Admin_Notes_Controller extends WC_REST_CRUD_Controller { class Notes extends \WC_REST_CRUD_Controller {
/** /**
* Endpoint namespace. * Endpoint namespace.
@ -40,7 +45,7 @@ class WC_Admin_REST_Admin_Notes_Controller extends WC_REST_CRUD_Controller {
'/' . $this->rest_base, '/' . $this->rest_base,
array( array(
array( array(
'methods' => WP_REST_Server::READABLE, 'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_items' ), 'callback' => array( $this, 'get_items' ),
'permission_callback' => array( $this, 'get_items_permissions_check' ), 'permission_callback' => array( $this, 'get_items_permissions_check' ),
'args' => $this->get_collection_params(), 'args' => $this->get_collection_params(),
@ -60,12 +65,12 @@ class WC_Admin_REST_Admin_Notes_Controller extends WC_REST_CRUD_Controller {
), ),
), ),
array( array(
'methods' => WP_REST_Server::READABLE, 'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_item' ), 'callback' => array( $this, 'get_item' ),
'permission_callback' => array( $this, 'get_item_permissions_check' ), 'permission_callback' => array( $this, 'get_item_permissions_check' ),
), ),
array( array(
'methods' => WP_REST_Server::EDITABLE, 'methods' => \WP_REST_Server::EDITABLE,
'callback' => array( $this, 'update_item' ), 'callback' => array( $this, 'update_item' ),
'permission_callback' => array( $this, 'update_items_permissions_check' ), 'permission_callback' => array( $this, 'update_items_permissions_check' ),
), ),
@ -84,7 +89,7 @@ class WC_Admin_REST_Admin_Notes_Controller extends WC_REST_CRUD_Controller {
$note = WC_Admin_Notes::get_note( $request->get_param( 'id' ) ); $note = WC_Admin_Notes::get_note( $request->get_param( 'id' ) );
if ( ! $note ) { if ( ! $note ) {
return new WP_Error( return new \WP_Error(
'woocommerce_admin_notes_invalid_id', 'woocommerce_admin_notes_invalid_id',
__( 'Sorry, there is no resource with that ID.', 'woocommerce-admin' ), __( 'Sorry, there is no resource with that ID.', 'woocommerce-admin' ),
array( 'status' => 404 ) array( 'status' => 404 )
@ -167,7 +172,7 @@ class WC_Admin_REST_Admin_Notes_Controller extends WC_REST_CRUD_Controller {
*/ */
public function get_item_permissions_check( $request ) { public function get_item_permissions_check( $request ) {
if ( ! wc_rest_check_manager_permissions( 'system_status', 'read' ) ) { if ( ! wc_rest_check_manager_permissions( 'system_status', 'read' ) ) {
return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce-admin' ), array( 'status' => rest_authorization_required_code() ) ); return new \WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce-admin' ), array( 'status' => rest_authorization_required_code() ) );
} }
return true; return true;
@ -181,7 +186,7 @@ class WC_Admin_REST_Admin_Notes_Controller extends WC_REST_CRUD_Controller {
*/ */
public function get_items_permissions_check( $request ) { public function get_items_permissions_check( $request ) {
if ( ! wc_rest_check_manager_permissions( 'system_status', 'read' ) ) { if ( ! wc_rest_check_manager_permissions( 'system_status', 'read' ) ) {
return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce-admin' ), array( 'status' => rest_authorization_required_code() ) ); return new \WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce-admin' ), array( 'status' => rest_authorization_required_code() ) );
} }
return true; return true;
@ -197,7 +202,7 @@ class WC_Admin_REST_Admin_Notes_Controller extends WC_REST_CRUD_Controller {
$note = WC_Admin_Notes::get_note( $request->get_param( 'id' ) ); $note = WC_Admin_Notes::get_note( $request->get_param( 'id' ) );
if ( ! $note ) { if ( ! $note ) {
return new WP_Error( return new \WP_Error(
'woocommerce_admin_notes_invalid_id', 'woocommerce_admin_notes_invalid_id',
__( 'Sorry, there is no resource with that ID.', 'woocommerce-admin' ), __( 'Sorry, there is no resource with that ID.', 'woocommerce-admin' ),
array( 'status' => 404 ) array( 'status' => 404 )
@ -230,7 +235,7 @@ class WC_Admin_REST_Admin_Notes_Controller extends WC_REST_CRUD_Controller {
*/ */
public function update_items_permissions_check( $request ) { public function update_items_permissions_check( $request ) {
if ( ! wc_rest_check_manager_permissions( 'settings', 'edit' ) ) { if ( ! wc_rest_check_manager_permissions( 'settings', 'edit' ) ) {
return new WP_Error( 'woocommerce_rest_cannot_edit', __( 'Sorry, you cannot edit this resource.', 'woocommerce-admin' ), array( 'status' => rest_authorization_required_code() ) ); return new \WP_Error( 'woocommerce_rest_cannot_edit', __( 'Sorry, you cannot edit this resource.', 'woocommerce-admin' ), array( 'status' => rest_authorization_required_code() ) );
} }
return true; return true;
} }

View File

@ -7,6 +7,8 @@
* @package WooCommerce Admin/API * @package WooCommerce Admin/API
*/ */
namespace Automattic\WooCommerce\Admin\API;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
/** /**
@ -15,7 +17,7 @@ defined( 'ABSPATH' ) || exit;
* @package WooCommerce Admin/API * @package WooCommerce Admin/API
* @extends WC_REST_Data_Controller * @extends WC_REST_Data_Controller
*/ */
class WC_Admin_REST_Onboarding_Levels_Controller extends WC_REST_Data_Controller { class OnboardingLevels extends \WC_REST_Data_Controller {
/** /**
* Endpoint namespace. * Endpoint namespace.
* *
@ -39,7 +41,7 @@ class WC_Admin_REST_Onboarding_Levels_Controller extends WC_REST_Data_Controller
'/' . $this->rest_base, '/' . $this->rest_base,
array( array(
array( array(
'methods' => WP_REST_Server::READABLE, 'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_items' ), 'callback' => array( $this, 'get_items' ),
'permission_callback' => array( $this, 'get_items_permissions_check' ), 'permission_callback' => array( $this, 'get_items_permissions_check' ),
), ),

View File

@ -7,6 +7,8 @@
* @package WooCommerce Admin/API * @package WooCommerce Admin/API
*/ */
namespace Automattic\WooCommerce\Admin\API;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
/** /**
@ -15,7 +17,7 @@ defined( 'ABSPATH' ) || exit;
* @package WooCommerce Admin/API * @package WooCommerce Admin/API
* @extends WC_REST_Data_Controller * @extends WC_REST_Data_Controller
*/ */
class WC_Admin_REST_Onboarding_Plugins_Controller extends WC_REST_Data_Controller { class OnboardingPlugins extends \WC_REST_Data_Controller {
/** /**
* Endpoint namespace. * Endpoint namespace.
* *
@ -39,7 +41,7 @@ class WC_Admin_REST_Onboarding_Plugins_Controller extends WC_REST_Data_Controlle
'/' . $this->rest_base . '/install', '/' . $this->rest_base . '/install',
array( array(
array( array(
'methods' => WP_REST_Server::EDITABLE, 'methods' => \WP_REST_Server::EDITABLE,
'callback' => array( $this, 'install_plugin' ), 'callback' => array( $this, 'install_plugin' ),
'permission_callback' => array( $this, 'update_item_permissions_check' ), 'permission_callback' => array( $this, 'update_item_permissions_check' ),
), ),
@ -52,7 +54,7 @@ class WC_Admin_REST_Onboarding_Plugins_Controller extends WC_REST_Data_Controlle
'/' . $this->rest_base . '/activate', '/' . $this->rest_base . '/activate',
array( array(
array( array(
'methods' => WP_REST_Server::EDITABLE, 'methods' => \WP_REST_Server::EDITABLE,
'callback' => array( $this, 'activate_plugin' ), 'callback' => array( $this, 'activate_plugin' ),
'permission_callback' => array( $this, 'update_item_permissions_check' ), 'permission_callback' => array( $this, 'update_item_permissions_check' ),
), ),
@ -65,7 +67,7 @@ class WC_Admin_REST_Onboarding_Plugins_Controller extends WC_REST_Data_Controlle
'/' . $this->rest_base . '/connect-jetpack', '/' . $this->rest_base . '/connect-jetpack',
array( array(
array( array(
'methods' => WP_REST_Server::READABLE, 'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'connect_jetpack' ), 'callback' => array( $this, 'connect_jetpack' ),
'permission_callback' => array( $this, 'update_item_permissions_check' ), 'permission_callback' => array( $this, 'update_item_permissions_check' ),
), ),
@ -108,7 +110,7 @@ class WC_Admin_REST_Onboarding_Plugins_Controller extends WC_REST_Data_Controlle
*/ */
public function update_item_permissions_check( $request ) { public function update_item_permissions_check( $request ) {
if ( ! current_user_can( 'install_plugins' ) ) { if ( ! current_user_can( 'install_plugins' ) ) {
return new WP_Error( 'woocommerce_rest_cannot_update', __( 'Sorry, you cannot manage plugins.', 'woocommerce-admin' ), array( 'status' => rest_authorization_required_code() ) ); return new \WP_Error( 'woocommerce_rest_cannot_update', __( 'Sorry, you cannot manage plugins.', 'woocommerce-admin' ), array( 'status' => rest_authorization_required_code() ) );
} }
return true; return true;
} }
@ -136,7 +138,7 @@ class WC_Admin_REST_Onboarding_Plugins_Controller extends WC_REST_Data_Controlle
$allowed_plugins = $this->get_allowed_plugins(); $allowed_plugins = $this->get_allowed_plugins();
$plugin = sanitize_title_with_dashes( $request['plugin'] ); $plugin = sanitize_title_with_dashes( $request['plugin'] );
if ( ! in_array( $plugin, array_keys( $allowed_plugins ), true ) ) { if ( ! in_array( $plugin, array_keys( $allowed_plugins ), true ) ) {
return new WP_Error( 'woocommerce_rest_invalid_plugin', __( 'Invalid plugin.', 'woocommerce-admin' ), 404 ); return new \WP_Error( 'woocommerce_rest_invalid_plugin', __( 'Invalid plugin.', 'woocommerce-admin' ), 404 );
} }
require_once ABSPATH . 'wp-admin/includes/plugin.php'; require_once ABSPATH . 'wp-admin/includes/plugin.php';
@ -170,14 +172,14 @@ class WC_Admin_REST_Onboarding_Plugins_Controller extends WC_REST_Data_Controlle
); );
if ( is_wp_error( $api ) ) { if ( is_wp_error( $api ) ) {
return new WP_Error( 'woocommerce_rest_plugin_install', __( 'The requested plugin could not be installed.', 'woocommerce-admin' ), 500 ); return new \WP_Error( 'woocommerce_rest_plugin_install', __( 'The requested plugin could not be installed.', 'woocommerce-admin' ), 500 );
} }
$upgrader = new Plugin_Upgrader( new Automatic_Upgrader_Skin() ); $upgrader = new \Plugin_Upgrader( new \Automatic_Upgrader_Skin() );
$result = $upgrader->install( $api->download_link ); $result = $upgrader->install( $api->download_link );
if ( is_wp_error( $result ) || is_null( $result ) ) { if ( is_wp_error( $result ) || is_null( $result ) ) {
return new WP_Error( 'woocommerce_rest_plugin_install', __( 'The requested plugin could not be installed.', 'woocommerce-admin' ), 500 ); return new \WP_Error( 'woocommerce_rest_plugin_install', __( 'The requested plugin could not be installed.', 'woocommerce-admin' ), 500 );
} }
return array( return array(
@ -197,7 +199,7 @@ class WC_Admin_REST_Onboarding_Plugins_Controller extends WC_REST_Data_Controlle
$allowed_plugins = $this->get_allowed_plugins(); $allowed_plugins = $this->get_allowed_plugins();
$plugin = sanitize_title_with_dashes( $request['plugin'] ); $plugin = sanitize_title_with_dashes( $request['plugin'] );
if ( ! in_array( $plugin, array_keys( $allowed_plugins ), true ) ) { if ( ! in_array( $plugin, array_keys( $allowed_plugins ), true ) ) {
return new WP_Error( 'woocommerce_rest_invalid_plugin', __( 'Invalid plugin.', 'woocommerce-admin' ), 404 ); return new \WP_Error( 'woocommerce_rest_invalid_plugin', __( 'Invalid plugin.', 'woocommerce-admin' ), 404 );
} }
require_once ABSPATH . 'wp-admin/includes/plugin.php'; require_once ABSPATH . 'wp-admin/includes/plugin.php';
@ -207,12 +209,12 @@ class WC_Admin_REST_Onboarding_Plugins_Controller extends WC_REST_Data_Controlle
$installed_plugins = get_plugins(); $installed_plugins = get_plugins();
if ( ! in_array( $path, array_keys( $installed_plugins ), true ) ) { if ( ! in_array( $path, array_keys( $installed_plugins ), true ) ) {
return new WP_Error( 'woocommerce_rest_invalid_plugin', __( 'Invalid plugin.', 'woocommerce-admin' ), 404 ); return new \WP_Error( 'woocommerce_rest_invalid_plugin', __( 'Invalid plugin.', 'woocommerce-admin' ), 404 );
} }
$result = activate_plugin( $path ); $result = activate_plugin( $path );
if ( ! is_null( $result ) ) { if ( ! is_null( $result ) ) {
return new WP_Error( 'woocommerce_rest_invalid_plugin', __( 'The requested plugin could not be activated.', 'woocommerce-admin' ), 500 ); return new \WP_Error( 'woocommerce_rest_invalid_plugin', __( 'The requested plugin could not be activated.', 'woocommerce-admin' ), 500 );
} }
return( array( return( array(
@ -229,7 +231,7 @@ class WC_Admin_REST_Onboarding_Plugins_Controller extends WC_REST_Data_Controlle
*/ */
public function connect_jetpack() { public function connect_jetpack() {
if ( ! class_exists( 'Jetpack' ) ) { if ( ! class_exists( 'Jetpack' ) ) {
return new WP_Error( 'woocommerce_rest_jetpack_not_active', __( 'Jetpack is not installed or active.', 'woocommerce-admin' ), 404 ); return new \WP_Error( 'woocommerce_rest_jetpack_not_active', __( 'Jetpack is not installed or active.', 'woocommerce-admin' ), 404 );
} }
$next_step_slug = apply_filters( 'woocommerce_onboarding_after_jetpack_step', 'store-details' ); $next_step_slug = apply_filters( 'woocommerce_onboarding_after_jetpack_step', 'store-details' );
@ -243,7 +245,7 @@ class WC_Admin_REST_Onboarding_Plugins_Controller extends WC_REST_Data_Controlle
) )
); );
$connect_url = Jetpack::init()->build_connect_url( true, $redirect_url, 'woocommerce-setup-wizard' ); $connect_url = \Jetpack::init()->build_connect_url( true, $redirect_url, 'woocommerce-setup-wizard' );
// Redirect to local calypso instead of production. // Redirect to local calypso instead of production.
if ( defined( 'WOOCOMMERCE_CALYPSO_LOCAL' ) && WOOCOMMERCE_CALYPSO_LOCAL ) { if ( defined( 'WOOCOMMERCE_CALYPSO_LOCAL' ) && WOOCOMMERCE_CALYPSO_LOCAL ) {
@ -275,7 +277,7 @@ class WC_Admin_REST_Onboarding_Plugins_Controller extends WC_REST_Data_Controlle
$redirect_uri = wc_admin_url( '&task=connect&wccom-connected=1' ); $redirect_uri = wc_admin_url( '&task=connect&wccom-connected=1' );
$request = WC_Helper_API::post( $request = \WC_Helper_API::post(
'oauth/request_token', 'oauth/request_token',
array( array(
'body' => array( 'body' => array(
@ -304,7 +306,7 @@ class WC_Admin_REST_Onboarding_Plugins_Controller extends WC_REST_Data_Controlle
'secret' => rawurlencode( $secret ), 'secret' => rawurlencode( $secret ),
'wccom-from' => 'onboarding', 'wccom-from' => 'onboarding',
), ),
WC_Helper_API::url( 'oauth/authorize' ) \WC_Helper_API::url( 'oauth/authorize' )
); );
// Redirect to local calypso instead of production. // Redirect to local calypso instead of production.
@ -339,7 +341,7 @@ class WC_Admin_REST_Onboarding_Plugins_Controller extends WC_REST_Data_Controlle
} }
// Obtain an access token. // Obtain an access token.
$request = WC_Helper_API::post( $request = \WC_Helper_API::post(
'oauth/access_token', 'oauth/access_token',
array( array(
'body' => array( 'body' => array(
@ -359,7 +361,7 @@ class WC_Admin_REST_Onboarding_Plugins_Controller extends WC_REST_Data_Controlle
return new WP_Error( 'woocommerce_rest_helper_connect', __( 'There was an error connecting to WooCommerce.com. Please try again.', 'woocommerce-admin' ), 500 ); return new WP_Error( 'woocommerce_rest_helper_connect', __( 'There was an error connecting to WooCommerce.com. Please try again.', 'woocommerce-admin' ), 500 );
} }
WC_Helper_Options::update( \WC_Helper_Options::update(
'auth', 'auth',
array( array(
'access_token' => $access_token['access_token'], 'access_token' => $access_token['access_token'],
@ -370,13 +372,13 @@ class WC_Admin_REST_Onboarding_Plugins_Controller extends WC_REST_Data_Controlle
) )
); );
if ( ! WC_Helper::_flush_authentication_cache() ) { if ( ! \WC_Helper::_flush_authentication_cache() ) {
WC_Helper_Options::update( 'auth', array() ); \WC_Helper_Options::update( 'auth', array() );
return new WP_Error( 'woocommerce_rest_helper_connect', __( 'There was an error connecting to WooCommerce.com. Please try again.', 'woocommerce-admin' ), 500 ); return new WP_Error( 'woocommerce_rest_helper_connect', __( 'There was an error connecting to WooCommerce.com. Please try again.', 'woocommerce-admin' ), 500 );
} }
delete_transient( '_woocommerce_helper_subscriptions' ); delete_transient( '_woocommerce_helper_subscriptions' );
WC_Helper_Updater::flush_updates_cache(); \WC_Helper_Updater::flush_updates_cache();
do_action( 'woocommerce_helper_connected' ); do_action( 'woocommerce_helper_connected' );

View File

@ -7,15 +7,19 @@
* @package WooCommerce Admin/API * @package WooCommerce Admin/API
*/ */
namespace Automattic\WooCommerce\Admin\API;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\Features\Onboarding;
/** /**
* Onboarding Profile controller. * Onboarding Profile controller.
* *
* @package WooCommerce Admin/API * @package WooCommerce Admin/API
* @extends WC_REST_Data_Controller * @extends WC_REST_Data_Controller
*/ */
class WC_Admin_REST_Onboarding_Profile_Controller extends WC_REST_Data_Controller { class OnboardingProfile extends \WC_REST_Data_Controller {
/** /**
* Endpoint namespace. * Endpoint namespace.
* *
@ -39,7 +43,7 @@ class WC_Admin_REST_Onboarding_Profile_Controller extends WC_REST_Data_Controlle
'/' . $this->rest_base, '/' . $this->rest_base,
array( array(
array( array(
'methods' => WP_REST_Server::READABLE, 'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_items' ), 'callback' => array( $this, 'get_items' ),
'permission_callback' => array( $this, 'get_items_permissions_check' ), 'permission_callback' => array( $this, 'get_items_permissions_check' ),
), ),
@ -51,7 +55,7 @@ class WC_Admin_REST_Onboarding_Profile_Controller extends WC_REST_Data_Controlle
'/' . $this->rest_base, '/' . $this->rest_base,
array( array(
array( array(
'methods' => WP_REST_Server::EDITABLE, 'methods' => \WP_REST_Server::EDITABLE,
'callback' => array( $this, 'update_items' ), 'callback' => array( $this, 'update_items' ),
'permission_callback' => array( $this, 'update_items_permissions_check' ), 'permission_callback' => array( $this, 'update_items_permissions_check' ),
'args' => $this->get_collection_params(), 'args' => $this->get_collection_params(),
@ -69,7 +73,7 @@ class WC_Admin_REST_Onboarding_Profile_Controller extends WC_REST_Data_Controlle
*/ */
public function get_items_permissions_check( $request ) { public function get_items_permissions_check( $request ) {
if ( ! wc_rest_check_manager_permissions( 'settings', 'read' ) ) { if ( ! wc_rest_check_manager_permissions( 'settings', 'read' ) ) {
return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce-admin' ), array( 'status' => rest_authorization_required_code() ) ); return new \WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce-admin' ), array( 'status' => rest_authorization_required_code() ) );
} }
return true; return true;
@ -83,7 +87,7 @@ class WC_Admin_REST_Onboarding_Profile_Controller extends WC_REST_Data_Controlle
*/ */
public function update_items_permissions_check( $request ) { public function update_items_permissions_check( $request ) {
if ( ! wc_rest_check_manager_permissions( 'settings', 'edit' ) ) { if ( ! wc_rest_check_manager_permissions( 'settings', 'edit' ) ) {
return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot edit this resource.', 'woocommerce-admin' ), array( 'status' => rest_authorization_required_code() ) ); return new \WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot edit this resource.', 'woocommerce-admin' ), array( 'status' => rest_authorization_required_code() ) );
} }
return true; return true;
@ -106,7 +110,7 @@ class WC_Admin_REST_Onboarding_Profile_Controller extends WC_REST_Data_Controlle
$items[ $key ] = isset( $onboarding_data[ $key ] ) ? $onboarding_data[ $key ] : null; $items[ $key ] = isset( $onboarding_data[ $key ] ) ? $onboarding_data[ $key ] : null;
} }
$wccom_auth = WC_Helper_Options::get( 'auth' ); $wccom_auth = \WC_Helper_Options::get( 'auth' );
$items['wccom_connected'] = empty( $wccom_auth['access_token'] ) ? false : true; $items['wccom_connected'] = empty( $wccom_auth['access_token'] ) ? false : true;
$item = $this->prepare_item_for_response( $items, $request ); $item = $this->prepare_item_for_response( $items, $request );
@ -232,7 +236,7 @@ class WC_Admin_REST_Onboarding_Profile_Controller extends WC_REST_Data_Controlle
'sanitize_callback' => 'wp_parse_slug_list', 'sanitize_callback' => 'wp_parse_slug_list',
'validate_callback' => 'rest_validate_request_arg', 'validate_callback' => 'rest_validate_request_arg',
'items' => array( 'items' => array(
'enum' => array_keys( WC_Admin_Onboarding::get_allowed_industries() ), 'enum' => array_keys( Onboarding::get_allowed_industries() ),
'type' => 'string', 'type' => 'string',
), ),
), ),
@ -244,7 +248,7 @@ class WC_Admin_REST_Onboarding_Profile_Controller extends WC_REST_Data_Controlle
'sanitize_callback' => 'wp_parse_slug_list', 'sanitize_callback' => 'wp_parse_slug_list',
'validate_callback' => 'rest_validate_request_arg', 'validate_callback' => 'rest_validate_request_arg',
'items' => array( 'items' => array(
'enum' => array_keys( WC_Admin_Onboarding::get_allowed_product_types() ), 'enum' => array_keys( Onboarding::get_allowed_product_types() ),
'type' => 'string', 'type' => 'string',
), ),
), ),

View File

@ -7,6 +7,8 @@
* @package WooCommerce Admin/API * @package WooCommerce Admin/API
*/ */
namespace Automattic\WooCommerce\Admin\API;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
/** /**
@ -15,7 +17,7 @@ defined( 'ABSPATH' ) || exit;
* @package WooCommerce Admin/API * @package WooCommerce Admin/API
* @extends WC_REST_Orders_Controller * @extends WC_REST_Orders_Controller
*/ */
class WC_Admin_REST_Orders_Controller extends WC_REST_Orders_Controller { class Orders extends \WC_REST_Orders_Controller {
/** /**
* Endpoint namespace. * Endpoint namespace.
* *

View File

@ -7,6 +7,8 @@
* @package WooCommerce Admin/API * @package WooCommerce Admin/API
*/ */
namespace Automattic\WooCommerce\Admin\API;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
/** /**
@ -15,7 +17,7 @@ defined( 'ABSPATH' ) || exit;
* @package WooCommerce Admin/API * @package WooCommerce Admin/API
* @extends WC_REST_Product_Categories_Controller * @extends WC_REST_Product_Categories_Controller
*/ */
class WC_Admin_REST_Product_Categories_Controller extends WC_REST_Product_Categories_Controller { class ProductCategories extends \WC_REST_Product_Categories_Controller {
/** /**
* Endpoint namespace. * Endpoint namespace.
* *

View File

@ -7,6 +7,8 @@
* @package WooCommerce Admin/API * @package WooCommerce Admin/API
*/ */
namespace Automattic\WooCommerce\Admin\API;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
/** /**
@ -15,7 +17,7 @@ defined( 'ABSPATH' ) || exit;
* @package WooCommerce Admin/API * @package WooCommerce Admin/API
* @extends WC_REST_Product_Reviews_Controller * @extends WC_REST_Product_Reviews_Controller
*/ */
class WC_Admin_REST_Product_Reviews_Controller extends WC_REST_Product_Reviews_Controller { class ProductReviews extends \WC_REST_Product_Reviews_Controller {
/** /**
* Endpoint namespace. * Endpoint namespace.
* *

View File

@ -7,6 +7,8 @@
* @package WooCommerce Admin/API * @package WooCommerce Admin/API
*/ */
namespace Automattic\WooCommerce\Admin\API;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
/** /**
@ -15,7 +17,7 @@ defined( 'ABSPATH' ) || exit;
* @package WooCommerce Admin/API * @package WooCommerce Admin/API
* @extends WC_REST_Product_Variations_Controller * @extends WC_REST_Product_Variations_Controller
*/ */
class WC_Admin_REST_Product_Variations_Controller extends WC_REST_Product_Variations_Controller { class ProductVariations extends \WC_REST_Product_Variations_Controller {
/** /**
* Endpoint namespace. * Endpoint namespace.
* *
@ -62,13 +64,13 @@ class WC_Admin_REST_Product_Variations_Controller extends WC_REST_Product_Variat
* @return WP_Error|WP_REST_Response * @return WP_Error|WP_REST_Response
*/ */
public function get_items( $request ) { public function get_items( $request ) {
add_filter( 'posts_where', array( 'WC_Admin_REST_Products_Controller', 'add_wp_query_filter' ), 10, 2 ); add_filter( 'posts_where', array( 'Automattic\WooCommerce\Admin\API\Products', 'add_wp_query_filter' ), 10, 2 );
add_filter( 'posts_join', array( 'WC_Admin_REST_Products_Controller', 'add_wp_query_join' ), 10, 2 ); add_filter( 'posts_join', array( 'Automattic\WooCommerce\Admin\API\Products', 'add_wp_query_join' ), 10, 2 );
add_filter( 'posts_groupby', array( 'WC_Admin_REST_Products_Controller', 'add_wp_query_group_by' ), 10, 2 ); add_filter( 'posts_groupby', array( 'Automattic\WooCommerce\Admin\API\Products', 'add_wp_query_group_by' ), 10, 2 );
$response = parent::get_items( $request ); $response = parent::get_items( $request );
remove_filter( 'posts_where', array( 'WC_Admin_REST_Products_Controller', 'add_wp_query_filter' ), 10 ); remove_filter( 'posts_where', array( 'Automattic\WooCommerce\Admin\API\Products', 'add_wp_query_filter' ), 10 );
remove_filter( 'posts_join', array( 'WC_Admin_REST_Products_Controller', 'add_wp_query_join' ), 10 ); remove_filter( 'posts_join', array( 'Automattic\WooCommerce\Admin\API\Products', 'add_wp_query_join' ), 10 );
remove_filter( 'posts_groupby', array( 'WC_Admin_REST_Products_Controller', 'add_wp_query_group_by' ), 10 ); remove_filter( 'posts_groupby', array( 'Automattic\WooCommerce\Admin\API\Products', 'add_wp_query_group_by' ), 10 );
return $response; return $response;
} }

View File

@ -7,6 +7,8 @@
* @package WooCommerce Admin/API * @package WooCommerce Admin/API
*/ */
namespace Automattic\WooCommerce\Admin\API;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
/** /**
@ -15,7 +17,7 @@ defined( 'ABSPATH' ) || exit;
* @package WooCommerce Admin/API * @package WooCommerce Admin/API
* @extends WC_REST_Products_Controller * @extends WC_REST_Products_Controller
*/ */
class WC_Admin_REST_Products_Controller extends WC_REST_Products_Controller { class Products extends \WC_REST_Products_Controller {
/** /**
* Endpoint namespace. * Endpoint namespace.

View File

@ -7,15 +7,17 @@
* @package WooCommerce Admin/API * @package WooCommerce Admin/API
*/ */
namespace Automattic\WooCommerce\Admin\API\Reports\Categories;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
/** /**
* REST API Reports categories controller class. * REST API Reports categories controller class.
* *
* @package WooCommerce/API * @package WooCommerce/API
* @extends WC_Admin_REST_Reports_Controller * @extends \Automattic\WooCommerce\Admin\API\Reports\Controller
*/ */
class WC_Admin_REST_Reports_Categories_Controller extends WC_Admin_REST_Reports_Controller { class Controller extends \Automattic\WooCommerce\Admin\API\Reports\Controller {
/** /**
* Endpoint namespace. * Endpoint namespace.
@ -62,7 +64,7 @@ class WC_Admin_REST_Reports_Categories_Controller extends WC_Admin_REST_Reports_
*/ */
public function get_items( $request ) { public function get_items( $request ) {
$query_args = $this->prepare_reports_query( $request ); $query_args = $this->prepare_reports_query( $request );
$categories_query = new WC_Admin_Reports_Categories_Query( $query_args ); $categories_query = new Query( $query_args );
$report_data = $categories_query->get_data(); $report_data = $categories_query->get_data();
if ( is_wp_error( $report_data ) ) { if ( is_wp_error( $report_data ) ) {
@ -70,7 +72,7 @@ class WC_Admin_REST_Reports_Categories_Controller extends WC_Admin_REST_Reports_
} }
if ( ! isset( $report_data->data ) || ! isset( $report_data->page_no ) || ! isset( $report_data->pages ) ) { if ( ! isset( $report_data->data ) || ! isset( $report_data->page_no ) || ! isset( $report_data->pages ) ) {
return new WP_Error( 'woocommerce_rest_reports_categories_invalid_response', __( 'Invalid response from data store.', 'woocommerce-admin' ), array( 'status' => 500 ) ); return new \WP_Error( 'woocommerce_rest_reports_categories_invalid_response', __( 'Invalid response from data store.', 'woocommerce-admin' ), array( 'status' => 500 ) );
} }
$out_data = array(); $out_data = array();
@ -137,7 +139,7 @@ class WC_Admin_REST_Reports_Categories_Controller extends WC_Admin_REST_Reports_
/** /**
* Prepare links for the request. * Prepare links for the request.
* *
* @param WC_Admin_Reports_Query $object Object data. * @param \Automattic\WooCommerce\Admin\API\Reports\Query $object Object data.
* @return array * @return array
*/ */
protected function prepare_links( $object ) { protected function prepare_links( $object ) {

View File

@ -1,17 +1,22 @@
<?php <?php
/** /**
* WC_Admin_Reports_Categories_Data_Store class file. * API\Reports\Categories\DataStore class file.
* *
* @package WooCommerce Admin/Classes * @package WooCommerce Admin/Classes
*/ */
namespace Automattic\WooCommerce\Admin\API\Reports\Categories;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
use \Automattic\WooCommerce\Admin\API\Reports\DataStore as ReportsDataStore;
use \Automattic\WooCommerce\Admin\API\Reports\DataStoreInterface;
use \Automattic\WooCommerce\Admin\API\Reports\TimeInterval;
/** /**
* WC_Admin_Reports_Categories_Data_Store. * API\Reports\Categories\DataStore.
*/ */
class WC_Admin_Reports_Categories_Data_Store extends WC_Admin_Reports_Data_Store implements WC_Admin_Reports_Data_Store_Interface { class DataStore extends ReportsDataStore implements DataStoreInterface {
/** /**
* Table used to get the data. * Table used to get the data.
@ -207,7 +212,7 @@ class WC_Admin_Reports_Categories_Data_Store extends WC_Admin_Reports_Data_Store
*/ */
protected function include_extended_info( &$categories_data, $query_args ) { protected function include_extended_info( &$categories_data, $query_args ) {
foreach ( $categories_data as $key => $category_data ) { foreach ( $categories_data as $key => $category_data ) {
$extended_info = new ArrayObject(); $extended_info = new \ArrayObject();
if ( $query_args['extended_info'] ) { if ( $query_args['extended_info'] ) {
$extended_info['name'] = get_the_category_by_ID( $category_data['category_id'] ); $extended_info['name'] = get_the_category_by_ID( $category_data['category_id'] );
} }
@ -232,8 +237,8 @@ class WC_Admin_Reports_Categories_Data_Store extends WC_Admin_Reports_Data_Store
'page' => 1, 'page' => 1,
'order' => 'DESC', 'order' => 'DESC',
'orderby' => 'date', 'orderby' => 'date',
'before' => WC_Admin_Reports_Interval::default_before(), 'before' => TimeInterval::default_before(),
'after' => WC_Admin_Reports_Interval::default_after(), 'after' => TimeInterval::default_after(),
'fields' => '*', 'fields' => '*',
'categories' => array(), 'categories' => array(),
'extended_info' => false, 'extended_info' => false,
@ -295,7 +300,7 @@ class WC_Admin_Reports_Categories_Data_Store extends WC_Admin_Reports_Data_Store
); // WPCS: cache ok, DB call ok, unprepared SQL ok. ); // WPCS: cache ok, DB call ok, unprepared SQL ok.
if ( null === $categories_data ) { if ( null === $categories_data ) {
return new WP_Error( 'woocommerce_reports_categories_result_failed', __( 'Sorry, fetching revenue data failed.', 'woocommerce-admin' ), array( 'status' => 500 ) ); return new \WP_Error( 'woocommerce_reports_categories_result_failed', __( 'Sorry, fetching revenue data failed.', 'woocommerce-admin' ), array( 'status' => 500 ) );
} }
$record_count = count( $categories_data ); $record_count = count( $categories_data );

View File

@ -10,18 +10,22 @@
* 'order' => 'desc', * 'order' => 'desc',
* 'orderby' => 'items_sold', * 'orderby' => 'items_sold',
* ); * );
* $report = new WC_Admin_Reports_Categories_Query( $args ); * $report = new \Automattic\WooCommerce\Admin\API\Reports\Query( $args );
* $mydata = $report->get_data(); * $mydata = $report->get_data();
* *
* @package WooCommerce Admin/Classes * @package WooCommerce Admin/Classes
*/ */
namespace Automattic\WooCommerce\Admin\API\Reports\Categories;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
use \Automattic\WooCommerce\Admin\API\Reports\Query as ReportsQuery;
/** /**
* WC_Admin_Reports_Categories_Query * API\Reports\Query
*/ */
class WC_Admin_Reports_Categories_Query extends WC_Admin_Reports_Query { class Query extends ReportsQuery {
const REPORT_NAME = 'report-categories'; const REPORT_NAME = 'report-categories';
@ -41,7 +45,7 @@ class WC_Admin_Reports_Categories_Query extends WC_Admin_Reports_Query {
*/ */
public function get_data() { public function get_data() {
$args = apply_filters( 'woocommerce_reports_categories_query_args', $this->get_query_vars() ); $args = apply_filters( 'woocommerce_reports_categories_query_args', $this->get_query_vars() );
$results = WC_Data_Store::load( self::REPORT_NAME )->get_data( $args ); $results = \WC_Data_Store::load( self::REPORT_NAME )->get_data( $args );
return apply_filters( 'woocommerce_reports_categories_select_query', $results, $args ); return apply_filters( 'woocommerce_reports_categories_select_query', $results, $args );
} }

View File

@ -7,6 +7,8 @@
* @package WooCommerce Admin/API * @package WooCommerce Admin/API
*/ */
namespace Automattic\WooCommerce\Admin\API\Reports;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
/** /**
@ -15,7 +17,7 @@ defined( 'ABSPATH' ) || exit;
* @package WooCommerce Admin/API * @package WooCommerce Admin/API
* @extends WC_REST_Reports_Controller * @extends WC_REST_Reports_Controller
*/ */
class WC_Admin_REST_Reports_Controller extends WC_REST_Reports_Controller { class Controller extends \WC_REST_Reports_Controller {
/** /**
* Endpoint namespace. * Endpoint namespace.
@ -40,7 +42,7 @@ class WC_Admin_REST_Reports_Controller extends WC_REST_Reports_Controller {
'/' . $this->rest_base, '/' . $this->rest_base,
array( array(
array( array(
'methods' => WP_REST_Server::READABLE, 'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_items' ), 'callback' => array( $this, 'get_items' ),
'permission_callback' => array( $this, 'get_items_permissions_check' ), 'permission_callback' => array( $this, 'get_items_permissions_check' ),
'args' => $this->get_collection_params(), 'args' => $this->get_collection_params(),
@ -58,7 +60,7 @@ class WC_Admin_REST_Reports_Controller extends WC_REST_Reports_Controller {
*/ */
public function get_items_permissions_check( $request ) { public function get_items_permissions_check( $request ) {
if ( ! wc_rest_check_manager_permissions( 'reports', 'read' ) ) { if ( ! wc_rest_check_manager_permissions( 'reports', 'read' ) ) {
return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce-admin' ), array( 'status' => rest_authorization_required_code() ) ); return new \WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce-admin' ), array( 'status' => rest_authorization_required_code() ) );
} }
return true; return true;

View File

@ -7,6 +7,8 @@
* @package WooCommerce Admin/API * @package WooCommerce Admin/API
*/ */
namespace Automattic\WooCommerce\Admin\API\Reports\Coupons;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
/** /**
@ -15,7 +17,7 @@ defined( 'ABSPATH' ) || exit;
* @package WooCommerce/API * @package WooCommerce/API
* @extends WC_REST_Reports_Controller * @extends WC_REST_Reports_Controller
*/ */
class WC_Admin_REST_Reports_Coupons_Controller extends WC_REST_Reports_Controller { class Controller extends \WC_REST_Reports_Controller {
/** /**
* Endpoint namespace. * Endpoint namespace.
@ -58,7 +60,7 @@ class WC_Admin_REST_Reports_Coupons_Controller extends WC_REST_Reports_Controlle
*/ */
public function get_items( $request ) { public function get_items( $request ) {
$query_args = $this->prepare_reports_query( $request ); $query_args = $this->prepare_reports_query( $request );
$coupons_query = new WC_Admin_Reports_Coupons_Query( $query_args ); $coupons_query = new Query( $query_args );
$report_data = $coupons_query->get_data(); $report_data = $coupons_query->get_data();
$data = array(); $data = array();

View File

@ -1,16 +1,22 @@
<?php <?php
/** /**
* WC_Admin_Reports_Coupons_Data_Store class file. * API\Reports\Coupons\DataStore class file.
* *
* @package WooCommerce Admin/Classes * @package WooCommerce Admin/Classes
*/ */
namespace Automattic\WooCommerce\Admin\API\Reports\Coupons;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
use \Automattic\WooCommerce\Admin\API\Reports\DataStore as ReportsDataStore;
use \Automattic\WooCommerce\Admin\API\Reports\DataStoreInterface;
use \Automattic\WooCommerce\Admin\API\Reports\TimeInterval;
/** /**
* WC_Admin_Reports_Coupons_Data_Store. * API\Reports\Coupons\DataStore.
*/ */
class WC_Admin_Reports_Coupons_Data_Store extends WC_Admin_Reports_Data_Store implements WC_Admin_Reports_Data_Store_Interface { class DataStore extends ReportsDataStore implements DataStoreInterface {
/** /**
* Table used to get the data. * Table used to get the data.
@ -170,22 +176,22 @@ class WC_Admin_Reports_Coupons_Data_Store extends WC_Admin_Reports_Data_Store im
*/ */
protected function include_extended_info( &$coupon_data, $query_args ) { protected function include_extended_info( &$coupon_data, $query_args ) {
foreach ( $coupon_data as $idx => $coupon_datum ) { foreach ( $coupon_data as $idx => $coupon_datum ) {
$extended_info = new ArrayObject(); $extended_info = new \ArrayObject();
if ( $query_args['extended_info'] ) { if ( $query_args['extended_info'] ) {
$coupon_id = $coupon_datum['coupon_id']; $coupon_id = $coupon_datum['coupon_id'];
$coupon = new WC_Coupon( $coupon_id ); $coupon = new \WC_Coupon( $coupon_id );
$gmt_timzone = new DateTimeZone( 'UTC' ); $gmt_timzone = new \DateTimeZone( 'UTC' );
$date_expires = $coupon->get_date_expires(); $date_expires = $coupon->get_date_expires();
if ( null === $date_expires ) { if ( null === $date_expires ) {
$date_expires = ''; $date_expires = '';
$date_expires_gmt = ''; $date_expires_gmt = '';
} else { } else {
$date_expires = $date_expires->format( WC_Admin_Reports_Interval::$iso_datetime_format ); $date_expires = $date_expires->format( TimeInterval::$iso_datetime_format );
$date_expires_gmt = new DateTime( $date_expires ); $date_expires_gmt = new \DateTime( $date_expires );
$date_expires_gmt->setTimezone( $gmt_timzone ); $date_expires_gmt->setTimezone( $gmt_timzone );
$date_expires_gmt = $date_expires_gmt->format( WC_Admin_Reports_Interval::$iso_datetime_format ); $date_expires_gmt = $date_expires_gmt->format( TimeInterval::$iso_datetime_format );
} }
$date_created = $coupon->get_date_created(); $date_created = $coupon->get_date_created();
@ -193,10 +199,10 @@ class WC_Admin_Reports_Coupons_Data_Store extends WC_Admin_Reports_Data_Store im
$date_created = ''; $date_created = '';
$date_created_gmt = ''; $date_created_gmt = '';
} else { } else {
$date_created = $date_created->format( WC_Admin_Reports_Interval::$iso_datetime_format ); $date_created = $date_created->format( TimeInterval::$iso_datetime_format );
$date_created_gmt = new DateTime( $date_created ); $date_created_gmt = new \DateTime( $date_created );
$date_created_gmt->setTimezone( $gmt_timzone ); $date_created_gmt->setTimezone( $gmt_timzone );
$date_created_gmt = $date_created_gmt->format( WC_Admin_Reports_Interval::$iso_datetime_format ); $date_created_gmt = $date_created_gmt->format( TimeInterval::$iso_datetime_format );
} }
$extended_info = array( $extended_info = array(
@ -229,8 +235,8 @@ class WC_Admin_Reports_Coupons_Data_Store extends WC_Admin_Reports_Data_Store im
'page' => 1, 'page' => 1,
'order' => 'DESC', 'order' => 'DESC',
'orderby' => 'coupon_id', 'orderby' => 'coupon_id',
'before' => WC_Admin_Reports_Interval::default_before(), 'before' => TimeInterval::default_before(),
'after' => WC_Admin_Reports_Interval::default_after(), 'after' => TimeInterval::default_after(),
'fields' => '*', 'fields' => '*',
'coupons' => array(), 'coupons' => array(),
'extended_info' => false, 'extended_info' => false,
@ -385,7 +391,7 @@ class WC_Admin_Reports_Coupons_Data_Store extends WC_Admin_Reports_Data_Store im
'order_id' => $order_id, 'order_id' => $order_id,
'coupon_id' => $coupon_id, 'coupon_id' => $coupon_id,
'discount_amount' => $coupon_item->get_discount(), 'discount_amount' => $coupon_item->get_discount(),
'date_created' => $order->get_date_created( 'edit' )->date( WC_Admin_Reports_Interval::$sql_datetime_format ), 'date_created' => $order->get_date_created( 'edit' )->date( TimeInterval::$sql_datetime_format ),
), ),
array( array(
'%d', '%d',

View File

@ -9,18 +9,22 @@
* 'page' => 2, * 'page' => 2,
* 'coupons' => array(5, 120), * 'coupons' => array(5, 120),
* ); * );
* $report = new WC_Admin_Reports_Coupons_Query( $args ); * $report = new \Automattic\WooCommerce\Admin\API\Reports\Coupons\Query( $args );
* $mydata = $report->get_data(); * $mydata = $report->get_data();
* *
* @package WooCommerce Admin/Classes * @package WooCommerce Admin/Classes
*/ */
namespace Automattic\WooCommerce\Admin\API\Reports\Coupons;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
use \Automattic\WooCommerce\Admin\API\Reports\Query as ReportsQuery;
/** /**
* WC_Admin_Reports_Products_Query * API\Reports\Coupons\Query
*/ */
class WC_Admin_Reports_Coupons_Query extends WC_Admin_Reports_Query { class Query extends ReportsQuery {
/** /**
* Valid fields for Products report. * Valid fields for Products report.
@ -39,7 +43,7 @@ class WC_Admin_Reports_Coupons_Query extends WC_Admin_Reports_Query {
public function get_data() { public function get_data() {
$args = apply_filters( 'woocommerce_reports_coupons_query_args', $this->get_query_vars() ); $args = apply_filters( 'woocommerce_reports_coupons_query_args', $this->get_query_vars() );
$data_store = WC_Data_Store::load( 'report-coupons' ); $data_store = \WC_Data_Store::load( 'report-coupons' );
$results = $data_store->get_data( $args ); $results = $data_store->get_data( $args );
return apply_filters( 'woocommerce_reports_coupons_select_query', $results, $args ); return apply_filters( 'woocommerce_reports_coupons_select_query', $results, $args );
} }

View File

@ -7,15 +7,19 @@
* @package WooCommerce Admin/API * @package WooCommerce Admin/API
*/ */
namespace Automattic\WooCommerce\Admin\API\Reports\Coupons\Stats;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
use \Automattic\WooCommerce\Admin\API\Reports\ParameterException;
/** /**
* REST API Reports coupons stats controller class. * REST API Reports coupons stats controller class.
* *
* @package WooCommerce/API * @package WooCommerce/API
* @extends WC_REST_Reports_Controller * @extends WC_REST_Reports_Controller
*/ */
class WC_Admin_REST_Reports_Coupons_Stats_Controller extends WC_REST_Reports_Controller { class Controller extends \WC_REST_Reports_Controller {
/** /**
* Endpoint namespace. * Endpoint namespace.
@ -61,11 +65,11 @@ class WC_Admin_REST_Reports_Coupons_Stats_Controller extends WC_REST_Reports_Con
*/ */
public function get_items( $request ) { public function get_items( $request ) {
$query_args = $this->prepare_reports_query( $request ); $query_args = $this->prepare_reports_query( $request );
$coupons_query = new WC_Admin_Reports_Coupons_Stats_Query( $query_args ); $coupons_query = new Query( $query_args );
try { try {
$report_data = $coupons_query->get_data(); $report_data = $coupons_query->get_data();
} catch ( WC_Admin_Reports_Parameter_Exception $e ) { } catch ( ParameterException $e ) {
return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); return new \WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
} }
$out_data = array( $out_data = array(

View File

@ -1,18 +1,21 @@
<?php <?php
/** /**
* WC_Admin_Reports_Coupons_Stats_Data_Store class file. * API\Reports\Coupons\Stats\DataStore class file.
* *
* @package WooCommerce Admin/Classes * @package WooCommerce Admin/Classes
*/ */
defined( 'ABSPATH' ) || exit; namespace Automattic\WooCommerce\Admin\API\Reports\Coupons\Stats;
defined( 'ABSPATH' ) || exit;
use \Automattic\WooCommerce\Admin\API\Reports\Coupons\DataStore as CouponsDataStore;
use \Automattic\WooCommerce\Admin\API\Reports\DataStoreInterface;
use \Automattic\WooCommerce\Admin\API\Reports\TimeInterval;
/** /**
* WC_Reports_Coupons_Stats_Data_Store. * API\Reports\Coupons\Stats\DataStore.
*/ */
class WC_Admin_Reports_Coupons_Stats_Data_Store extends WC_Admin_Reports_Coupons_Data_Store implements WC_Admin_Reports_Data_Store_Interface { class DataStore extends CouponsDataStore implements DataStoreInterface {
/** /**
* Mapping columns to data type to return correct response types. * Mapping columns to data type to return correct response types.
* *
@ -102,8 +105,8 @@ class WC_Admin_Reports_Coupons_Stats_Data_Store extends WC_Admin_Reports_Coupons
'page' => 1, 'page' => 1,
'order' => 'DESC', 'order' => 'DESC',
'orderby' => 'date', 'orderby' => 'date',
'before' => WC_Admin_Reports_Interval::default_before(), 'before' => TimeInterval::default_before(),
'after' => WC_Admin_Reports_Interval::default_after(), 'after' => TimeInterval::default_after(),
'fields' => '*', 'fields' => '*',
'interval' => 'week', 'interval' => 'week',
'coupons' => array(), 'coupons' => array(),
@ -142,7 +145,7 @@ class WC_Admin_Reports_Coupons_Stats_Data_Store extends WC_Admin_Reports_Coupons
); // WPCS: cache ok, DB call ok, unprepared SQL ok. ); // WPCS: cache ok, DB call ok, unprepared SQL ok.
$db_interval_count = count( $db_intervals ); $db_interval_count = count( $db_intervals );
$expected_interval_count = WC_Admin_Reports_Interval::intervals_between( $query_args['after'], $query_args['before'], $query_args['interval'] ); $expected_interval_count = TimeInterval::intervals_between( $query_args['after'], $query_args['before'], $query_args['interval'] );
$total_pages = (int) ceil( $expected_interval_count / $intervals_query['per_page'] ); $total_pages = (int) ceil( $expected_interval_count / $intervals_query['per_page'] );
if ( $query_args['page'] < 1 || $query_args['page'] > $total_pages ) { if ( $query_args['page'] < 1 || $query_args['page'] > $total_pages ) {
return $data; return $data;
@ -164,7 +167,7 @@ class WC_Admin_Reports_Coupons_Stats_Data_Store extends WC_Admin_Reports_Coupons
if ( null === $totals ) { if ( null === $totals ) {
return $data; return $data;
} }
$segmenter = new WC_Admin_Reports_Coupons_Stats_Segmenting( $query_args, $this->report_columns ); $segmenter = new Segmenter( $query_args, $this->report_columns );
$totals[0]['segments'] = $segmenter->get_totals_segments( $totals_query, $table_name ); $totals[0]['segments'] = $segmenter->get_totals_segments( $totals_query, $table_name );
$totals = (object) $this->cast_numbers( $totals[0] ); $totals = (object) $this->cast_numbers( $totals[0] );
@ -207,7 +210,7 @@ class WC_Admin_Reports_Coupons_Stats_Data_Store extends WC_Admin_Reports_Coupons
'page_no' => (int) $query_args['page'], 'page_no' => (int) $query_args['page'],
); );
if ( WC_Admin_Reports_Interval::intervals_missing( $expected_interval_count, $db_interval_count, $intervals_query['per_page'], $query_args['page'], $query_args['order'], $query_args['orderby'], count( $intervals ) ) ) { if ( TimeInterval::intervals_missing( $expected_interval_count, $db_interval_count, $intervals_query['per_page'], $query_args['page'], $query_args['order'], $query_args['orderby'], count( $intervals ) ) ) {
$this->fill_in_missing_intervals( $db_intervals, $query_args['adj_after'], $query_args['adj_before'], $query_args['interval'], $data ); $this->fill_in_missing_intervals( $db_intervals, $query_args['adj_after'], $query_args['adj_before'], $query_args['interval'], $data );
$this->sort_intervals( $data, $query_args['orderby'], $query_args['order'] ); $this->sort_intervals( $data, $query_args['orderby'], $query_args['order'] );
$this->remove_extra_records( $data, $query_args['page'], $intervals_query['per_page'], $db_interval_count, $expected_interval_count, $query_args['orderby'], $query_args['order'] ); $this->remove_extra_records( $data, $query_args['page'], $intervals_query['per_page'], $db_interval_count, $expected_interval_count, $query_args['orderby'], $query_args['order'] );

View File

@ -9,18 +9,22 @@
* 'page' => 2, * 'page' => 2,
* 'coupons' => array(5, 120), * 'coupons' => array(5, 120),
* ); * );
* $report = new WC_Admin_Reports_Coupons_Stats_Query( $args ); * $report = new \Automattic\WooCommerce\Admin\API\Reports\Coupons\Stats\Query( $args );
* $mydata = $report->get_data(); * $mydata = $report->get_data();
* *
* @package WooCommerce Admin/Classes * @package WooCommerce Admin/Classes
*/ */
namespace Automattic\WooCommerce\Admin\API\Reports\Coupons\Stats;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
use \Automattic\WooCommerce\Admin\API\Reports\Query as ReportsQuery;
/** /**
* WC_Admin_Reports_Products_Query * API\Reports\Coupons\Stats\Query
*/ */
class WC_Admin_Reports_Coupons_Stats_Query extends WC_Admin_Reports_Query { class Query extends ReportsQuery {
/** /**
* Valid fields for Products report. * Valid fields for Products report.
@ -39,7 +43,7 @@ class WC_Admin_Reports_Coupons_Stats_Query extends WC_Admin_Reports_Query {
public function get_data() { public function get_data() {
$args = apply_filters( 'woocommerce_reports_coupons_query_args', $this->get_query_vars() ); $args = apply_filters( 'woocommerce_reports_coupons_query_args', $this->get_query_vars() );
$data_store = WC_Data_Store::load( 'report-coupons-stats' ); $data_store = \WC_Data_Store::load( 'report-coupons-stats' );
$results = $data_store->get_data( $args ); $results = $data_store->get_data( $args );
return apply_filters( 'woocommerce_reports_coupons_select_query', $results, $args ); return apply_filters( 'woocommerce_reports_coupons_select_query', $results, $args );
} }

View File

@ -5,12 +5,17 @@
* @package WooCommerce Admin/Classes * @package WooCommerce Admin/Classes
*/ */
namespace Automattic\WooCommerce\Admin\API\Reports\Coupons\Stats;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
use \Automattic\WooCommerce\Admin\API\Reports\Segmenter as ReportsSegmenter;
use \Automattic\WooCommerce\Admin\API\Reports\ParameterException;
/** /**
* Date & time interval and numeric range handling class for Reporting API. * Date & time interval and numeric range handling class for Reporting API.
*/ */
class WC_Admin_Reports_Coupons_Stats_Segmenting extends WC_Admin_Reports_Segmenting { class Segmenter extends ReportsSegmenter {
/** /**
* Returns SELECT clause statements to be used for product-related product-level segmenting query (e.g. coupon discount amount for product X when segmenting by product id or category). * Returns SELECT clause statements to be used for product-related product-level segmenting query (e.g. coupon discount amount for product X when segmenting by product id or category).
@ -244,7 +249,7 @@ class WC_Admin_Reports_Coupons_Stats_Segmenting extends WC_Admin_Reports_Segment
* @param string $table_name Name of main SQL table for the data store (used as basis for JOINS). * @param string $table_name Name of main SQL table for the data store (used as basis for JOINS).
* *
* @return array * @return array
* @throws WC_Admin_Reports_Parameter_Exception In case of segmenting by variations, when no parent product is specified. * @throws \Automattic\WooCommerce\Admin\API\Reports\ParameterException In case of segmenting by variations, when no parent product is specified.
*/ */
protected function get_segments( $type, $query_params, $table_name ) { protected function get_segments( $type, $query_params, $table_name ) {
global $wpdb; global $wpdb;
@ -271,7 +276,7 @@ class WC_Admin_Reports_Coupons_Stats_Segmenting extends WC_Admin_Reports_Segment
$segments = $this->get_product_related_segments( $type, $segmenting_selections, $segmenting_from, $segmenting_where, $segmenting_groupby, $segmenting_dimension_name, $table_name, $query_params, $unique_orders_table ); $segments = $this->get_product_related_segments( $type, $segmenting_selections, $segmenting_from, $segmenting_where, $segmenting_groupby, $segmenting_dimension_name, $table_name, $query_params, $unique_orders_table );
} elseif ( 'variation' === $this->query_args['segmentby'] ) { } elseif ( 'variation' === $this->query_args['segmentby'] ) {
if ( ! isset( $this->query_args['product_includes'] ) || count( $this->query_args['product_includes'] ) !== 1 ) { if ( ! isset( $this->query_args['product_includes'] ) || count( $this->query_args['product_includes'] ) !== 1 ) {
throw new WC_Admin_Reports_Parameter_Exception( 'wc_admin_reports_invalid_segmenting_variation', __( 'product_includes parameter need to specify exactly one product when segmenting by variation.', 'woocommerce-admin' ) ); throw new ParameterException( 'wc_admin_reports_invalid_segmenting_variation', __( 'product_includes parameter need to specify exactly one product when segmenting by variation.', 'woocommerce-admin' ) );
} }
$segmenting_selections = array( $segmenting_selections = array(

View File

@ -7,15 +7,19 @@
* @package WooCommerce Admin/API * @package WooCommerce Admin/API
*/ */
namespace Automattic\WooCommerce\Admin\API\Reports\Customers;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
use \Automattic\WooCommerce\Admin\API\Reports\TimeInterval;
/** /**
* REST API Reports customers controller class. * REST API Reports customers controller class.
* *
* @package WooCommerce/API * @package WooCommerce/API
* @extends WC_REST_Reports_Controller * @extends WC_REST_Reports_Controller
*/ */
class WC_Admin_REST_Reports_Customers_Controller extends WC_REST_Reports_Controller { class Controller extends \WC_REST_Reports_Controller {
/** /**
* Endpoint namespace. * Endpoint namespace.
@ -71,9 +75,9 @@ class WC_Admin_REST_Reports_Customers_Controller extends WC_REST_Reports_Control
$args['customers'] = $request['customers']; $args['customers'] = $request['customers'];
$between_params_numeric = array( 'orders_count', 'total_spend', 'avg_order_value' ); $between_params_numeric = array( 'orders_count', 'total_spend', 'avg_order_value' );
$normalized_params_numeric = WC_Admin_Reports_Interval::normalize_between_params( $request, $between_params_numeric, false ); $normalized_params_numeric = TimeInterval::normalize_between_params( $request, $between_params_numeric, false );
$between_params_date = array( 'last_active', 'registered' ); $between_params_date = array( 'last_active', 'registered' );
$normalized_params_date = WC_Admin_Reports_Interval::normalize_between_params( $request, $between_params_date, true ); $normalized_params_date = TimeInterval::normalize_between_params( $request, $between_params_date, true );
$args = array_merge( $args, $normalized_params_numeric, $normalized_params_date ); $args = array_merge( $args, $normalized_params_numeric, $normalized_params_date );
return $args; return $args;
@ -87,7 +91,7 @@ class WC_Admin_REST_Reports_Customers_Controller extends WC_REST_Reports_Control
*/ */
public function get_items( $request ) { public function get_items( $request ) {
$query_args = $this->prepare_reports_query( $request ); $query_args = $this->prepare_reports_query( $request );
$customers_query = new WC_Admin_Reports_Customers_Query( $query_args ); $customers_query = new Query( $query_args );
$report_data = $customers_query->get_data(); $report_data = $customers_query->get_data();
$data = array(); $data = array();
@ -431,7 +435,7 @@ class WC_Admin_REST_Reports_Customers_Controller extends WC_REST_Reports_Control
$params['last_active_between'] = array( $params['last_active_between'] = array(
'description' => __( 'Limit response to objects last active between two given ISO8601 compliant datetime.', 'woocommerce-admin' ), 'description' => __( 'Limit response to objects last active between two given ISO8601 compliant datetime.', 'woocommerce-admin' ),
'type' => 'array', 'type' => 'array',
'validate_callback' => array( 'WC_Admin_Reports_Interval', 'rest_validate_between_date_arg' ), 'validate_callback' => array( '\Automattic\WooCommerce\Admin\API\Reports\TimeInterval', 'rest_validate_between_date_arg' ),
); );
$params['registered_before'] = array( $params['registered_before'] = array(
'description' => __( 'Limit response to objects registered before (or at) a given ISO8601 compliant datetime.', 'woocommerce-admin' ), 'description' => __( 'Limit response to objects registered before (or at) a given ISO8601 compliant datetime.', 'woocommerce-admin' ),
@ -448,7 +452,7 @@ class WC_Admin_REST_Reports_Customers_Controller extends WC_REST_Reports_Control
$params['registered_between'] = array( $params['registered_between'] = array(
'description' => __( 'Limit response to objects last active between two given ISO8601 compliant datetime.', 'woocommerce-admin' ), 'description' => __( 'Limit response to objects last active between two given ISO8601 compliant datetime.', 'woocommerce-admin' ),
'type' => 'array', 'type' => 'array',
'validate_callback' => array( 'WC_Admin_Reports_Interval', 'rest_validate_between_date_arg' ), 'validate_callback' => array( '\Automattic\WooCommerce\Admin\API\Reports\TimeInterval', 'rest_validate_between_date_arg' ),
); );
$params['orders_count_min'] = array( $params['orders_count_min'] = array(
'description' => __( 'Limit response to objects with an order count greater than or equal to given integer.', 'woocommerce-admin' ), 'description' => __( 'Limit response to objects with an order count greater than or equal to given integer.', 'woocommerce-admin' ),
@ -465,7 +469,7 @@ class WC_Admin_REST_Reports_Customers_Controller extends WC_REST_Reports_Control
$params['orders_count_between'] = array( $params['orders_count_between'] = array(
'description' => __( 'Limit response to objects with an order count between two given integers.', 'woocommerce-admin' ), 'description' => __( 'Limit response to objects with an order count between two given integers.', 'woocommerce-admin' ),
'type' => 'array', 'type' => 'array',
'validate_callback' => array( 'WC_Admin_Reports_Interval', 'rest_validate_between_numeric_arg' ), 'validate_callback' => array( '\Automattic\WooCommerce\Admin\API\Reports\TimeInterval', 'rest_validate_between_numeric_arg' ),
); );
$params['total_spend_min'] = array( $params['total_spend_min'] = array(
'description' => __( 'Limit response to objects with a total order spend greater than or equal to given number.', 'woocommerce-admin' ), 'description' => __( 'Limit response to objects with a total order spend greater than or equal to given number.', 'woocommerce-admin' ),
@ -480,7 +484,7 @@ class WC_Admin_REST_Reports_Customers_Controller extends WC_REST_Reports_Control
$params['total_spend_between'] = array( $params['total_spend_between'] = array(
'description' => __( 'Limit response to objects with a total order spend between two given numbers.', 'woocommerce-admin' ), 'description' => __( 'Limit response to objects with a total order spend between two given numbers.', 'woocommerce-admin' ),
'type' => 'array', 'type' => 'array',
'validate_callback' => array( 'WC_Admin_Reports_Interval', 'rest_validate_between_numeric_arg' ), 'validate_callback' => array( '\Automattic\WooCommerce\Admin\API\Reports\TimeInterval', 'rest_validate_between_numeric_arg' ),
); );
$params['avg_order_value_min'] = array( $params['avg_order_value_min'] = array(
'description' => __( 'Limit response to objects with an average order spend greater than or equal to given number.', 'woocommerce-admin' ), 'description' => __( 'Limit response to objects with an average order spend greater than or equal to given number.', 'woocommerce-admin' ),
@ -495,7 +499,7 @@ class WC_Admin_REST_Reports_Customers_Controller extends WC_REST_Reports_Control
$params['avg_order_value_between'] = array( $params['avg_order_value_between'] = array(
'description' => __( 'Limit response to objects with an average order spend between two given numbers.', 'woocommerce-admin' ), 'description' => __( 'Limit response to objects with an average order spend between two given numbers.', 'woocommerce-admin' ),
'type' => 'array', 'type' => 'array',
'validate_callback' => array( 'WC_Admin_Reports_Interval', 'rest_validate_between_numeric_arg' ), 'validate_callback' => array( '\Automattic\WooCommerce\Admin\API\Reports\TimeInterval', 'rest_validate_between_numeric_arg' ),
); );
$params['last_order_before'] = array( $params['last_order_before'] = array(
'description' => __( 'Limit response to objects with last order before (or at) a given ISO8601 compliant datetime.', 'woocommerce-admin' ), 'description' => __( 'Limit response to objects with last order before (or at) a given ISO8601 compliant datetime.', 'woocommerce-admin' ),

View File

@ -1,16 +1,22 @@
<?php <?php
/** /**
* WC_Admin_Reports_Customers_Data_Store class file. * Admin\API\Reports\Customers\DataStore class file.
* *
* @package WooCommerce Admin/Classes * @package WooCommerce Admin/Classes
*/ */
namespace Automattic\WooCommerce\Admin\API\Reports\Customers;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
use \Automattic\WooCommerce\Admin\API\Reports\DataStore as ReportsDataStore;
use \Automattic\WooCommerce\Admin\API\Reports\DataStoreInterface;
use \Automattic\WooCommerce\Admin\API\Reports\TimeInterval;
/** /**
* WC_Admin_Reports_Customers_Data_Store. * Admin\API\Reports\Customers\DataStore.
*/ */
class WC_Admin_Reports_Customers_Data_Store extends WC_Admin_Reports_Data_Store implements WC_Admin_Reports_Data_Store_Interface { class DataStore extends ReportsDataStore implements DataStoreInterface {
/** /**
* Table used to get the data. * Table used to get the data.
@ -169,14 +175,14 @@ class WC_Admin_Reports_Customers_Data_Store extends WC_Admin_Reports_Data_Store
$column_name = $param_info['column']; $column_name = $param_info['column'];
if ( ! empty( $query_args[ $before_arg ] ) ) { if ( ! empty( $query_args[ $before_arg ] ) ) {
$datetime = new DateTime( $query_args[ $before_arg ] ); $datetime = new \DateTime( $query_args[ $before_arg ] );
$datetime_str = $datetime->format( WC_Admin_Reports_Interval::$sql_datetime_format ); $datetime_str = $datetime->format( TimeInterval::$sql_datetime_format );
$subclauses[] = "{$column_name} <= '$datetime_str'"; $subclauses[] = "{$column_name} <= '$datetime_str'";
} }
if ( ! empty( $query_args[ $after_arg ] ) ) { if ( ! empty( $query_args[ $after_arg ] ) ) {
$datetime = new DateTime( $query_args[ $after_arg ] ); $datetime = new \DateTime( $query_args[ $after_arg ] );
$datetime_str = $datetime->format( WC_Admin_Reports_Interval::$sql_datetime_format ); $datetime_str = $datetime->format( TimeInterval::$sql_datetime_format );
$subclauses[] = "{$column_name} >= '$datetime_str'"; $subclauses[] = "{$column_name} >= '$datetime_str'";
} }
@ -347,8 +353,8 @@ class WC_Admin_Reports_Customers_Data_Store extends WC_Admin_Reports_Data_Store
'page' => 1, 'page' => 1,
'order' => 'DESC', 'order' => 'DESC',
'orderby' => 'date_registered', 'orderby' => 'date_registered',
'order_before' => WC_Admin_Reports_Interval::default_before(), 'order_before' => TimeInterval::default_before(),
'order_after' => WC_Admin_Reports_Interval::default_after(), 'order_after' => TimeInterval::default_after(),
'fields' => '*', 'fields' => '*',
); );
$query_args = wp_parse_args( $query_args, $defaults ); $query_args = wp_parse_args( $query_args, $defaults );
@ -516,10 +522,10 @@ class WC_Admin_Reports_Customers_Data_Store extends WC_Admin_Reports_Data_Store
// Add registered customer data. // Add registered customer data.
if ( 0 !== $order->get_user_id() ) { if ( 0 !== $order->get_user_id() ) {
$user_id = $order->get_user_id(); $user_id = $order->get_user_id();
$customer = new WC_Customer( $user_id ); $customer = new \WC_Customer( $user_id );
$data['user_id'] = $user_id; $data['user_id'] = $user_id;
$data['username'] = $customer->get_username( 'edit' ); $data['username'] = $customer->get_username( 'edit' );
$data['date_registered'] = $customer->get_date_created( 'edit' ) ? $customer->get_date_created( 'edit' )->date( WC_Admin_Reports_Interval::$sql_datetime_format ) : null; $data['date_registered'] = $customer->get_date_created( 'edit' ) ? $customer->get_date_created( 'edit' )->date( TimeInterval::$sql_datetime_format ) : null;
$format[] = '%d'; $format[] = '%d';
$format[] = '%s'; $format[] = '%s';
$format[] = '%s'; $format[] = '%s';
@ -611,7 +617,7 @@ class WC_Admin_Reports_Customers_Data_Store extends WC_Admin_Reports_Data_Store
public static function update_registered_customer( $user_id ) { public static function update_registered_customer( $user_id ) {
global $wpdb; global $wpdb;
$customer = new WC_Customer( $user_id ); $customer = new \WC_Customer( $user_id );
if ( ! self::is_valid_customer( $user_id ) ) { if ( ! self::is_valid_customer( $user_id ) ) {
return false; return false;
@ -638,7 +644,7 @@ class WC_Admin_Reports_Customers_Data_Store extends WC_Admin_Reports_Data_Store
'state' => $customer->get_billing_state( 'edit' ), 'state' => $customer->get_billing_state( 'edit' ),
'postcode' => $customer->get_billing_postcode( 'edit' ), 'postcode' => $customer->get_billing_postcode( 'edit' ),
'country' => $customer->get_billing_country( 'edit' ), 'country' => $customer->get_billing_country( 'edit' ),
'date_registered' => $customer->get_date_created( 'edit' )->date( WC_Admin_Reports_Interval::$sql_datetime_format ), 'date_registered' => $customer->get_date_created( 'edit' )->date( TimeInterval::$sql_datetime_format ),
'date_last_active' => $last_active ? date( 'Y-m-d H:i:s', $last_active ) : null, 'date_last_active' => $last_active ? date( 'Y-m-d H:i:s', $last_active ) : null,
); );
$format = array( $format = array(
@ -680,7 +686,7 @@ class WC_Admin_Reports_Customers_Data_Store extends WC_Admin_Reports_Data_Store
* @return bool * @return bool
*/ */
protected static function is_valid_customer( $user_id ) { protected static function is_valid_customer( $user_id ) {
$customer = new WC_Customer( $user_id ); $customer = new \WC_Customer( $user_id );
if ( absint( $customer->get_id() ) !== absint( $user_id ) ) { if ( absint( $customer->get_id() ) !== absint( $user_id ) ) {
return false; return false;

View File

@ -10,18 +10,22 @@
* 'avg_order_value_min' => 100, * 'avg_order_value_min' => 100,
* 'country' => 'GB', * 'country' => 'GB',
* ); * );
* $report = new WC_Admin_Reports_Customers_Query( $args ); * $report = new \Automattic\WooCommerce\Admin\API\Reports\Customers\Query( $args );
* $mydata = $report->get_data(); * $mydata = $report->get_data();
* *
* @package WooCommerce Admin/Classes * @package WooCommerce Admin/Classes
*/ */
namespace Automattic\WooCommerce\Admin\API\Reports\Customers;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
use \Automattic\WooCommerce\Admin\API\Reports\Query as ReportsQuery;
/** /**
* WC_Admin_Reports_Customers_Query * API\Reports\Customers\Query
*/ */
class WC_Admin_Reports_Customers_Query extends WC_Admin_Reports_Query { class Query extends ReportsQuery {
/** /**
* Valid fields for Customers report. * Valid fields for Customers report.
@ -46,7 +50,7 @@ class WC_Admin_Reports_Customers_Query extends WC_Admin_Reports_Query {
public function get_data() { public function get_data() {
$args = apply_filters( 'woocommerce_reports_customers_query_args', $this->get_query_vars() ); $args = apply_filters( 'woocommerce_reports_customers_query_args', $this->get_query_vars() );
$data_store = WC_Data_Store::load( 'report-customers' ); $data_store = \WC_Data_Store::load( 'report-customers' );
$results = $data_store->get_data( $args ); $results = $data_store->get_data( $args );
return apply_filters( 'woocommerce_reports_customers_select_query', $results, $args ); return apply_filters( 'woocommerce_reports_customers_select_query', $results, $args );
} }

View File

@ -7,15 +7,19 @@
* @package WooCommerce Admin/API * @package WooCommerce Admin/API
*/ */
namespace Automattic\WooCommerce\Admin\API\Reports\Customers\Stats;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
use \Automattic\WooCommerce\Admin\API\Reports\TimeInterval;
/** /**
* REST API Reports customers stats controller class. * REST API Reports customers stats controller class.
* *
* @package WooCommerce/API * @package WooCommerce/API
* @extends WC_REST_Reports_Controller * @extends WC_REST_Reports_Controller
*/ */
class WC_Admin_REST_Reports_Customers_Stats_Controller extends WC_REST_Reports_Controller { class Controller extends \WC_REST_Reports_Controller {
/** /**
* Endpoint namespace. * Endpoint namespace.
* *
@ -63,9 +67,9 @@ class WC_Admin_REST_Reports_Customers_Stats_Controller extends WC_REST_Reports_C
$args['customers'] = $request['customers']; $args['customers'] = $request['customers'];
$between_params_numeric = array( 'orders_count', 'total_spend', 'avg_order_value' ); $between_params_numeric = array( 'orders_count', 'total_spend', 'avg_order_value' );
$normalized_params_numeric = WC_Admin_Reports_Interval::normalize_between_params( $request, $between_params_numeric, false ); $normalized_params_numeric = TimeInterval::normalize_between_params( $request, $between_params_numeric, false );
$between_params_date = array( 'last_active', 'registered' ); $between_params_date = array( 'last_active', 'registered' );
$normalized_params_date = WC_Admin_Reports_Interval::normalize_between_params( $request, $between_params_date, true ); $normalized_params_date = TimeInterval::normalize_between_params( $request, $between_params_date, true );
$args = array_merge( $args, $normalized_params_numeric, $normalized_params_date ); $args = array_merge( $args, $normalized_params_numeric, $normalized_params_date );
return $args; return $args;
@ -79,7 +83,7 @@ class WC_Admin_REST_Reports_Customers_Stats_Controller extends WC_REST_Reports_C
*/ */
public function get_items( $request ) { public function get_items( $request ) {
$query_args = $this->prepare_reports_query( $request ); $query_args = $this->prepare_reports_query( $request );
$customers_query = new WC_Admin_Reports_Customers_Stats_Query( $query_args ); $customers_query = new Query( $query_args );
$report_data = $customers_query->get_data(); $report_data = $customers_query->get_data();
$out_data = array( $out_data = array(
'totals' => $report_data, 'totals' => $report_data,
@ -271,7 +275,7 @@ class WC_Admin_REST_Reports_Customers_Stats_Controller extends WC_REST_Reports_C
$params['last_active_between'] = array( $params['last_active_between'] = array(
'description' => __( 'Limit response to objects last active between two given ISO8601 compliant datetime.', 'woocommerce-admin' ), 'description' => __( 'Limit response to objects last active between two given ISO8601 compliant datetime.', 'woocommerce-admin' ),
'type' => 'array', 'type' => 'array',
'validate_callback' => array( 'WC_Admin_Reports_Interval', 'rest_validate_between_date_arg' ), 'validate_callback' => array( '\Automattic\WooCommerce\Admin\API\Reports\TimeInterval', 'rest_validate_between_date_arg' ),
); );
$params['registered_before'] = array( $params['registered_before'] = array(
'description' => __( 'Limit response to objects registered before (or at) a given ISO8601 compliant datetime.', 'woocommerce-admin' ), 'description' => __( 'Limit response to objects registered before (or at) a given ISO8601 compliant datetime.', 'woocommerce-admin' ),
@ -288,7 +292,7 @@ class WC_Admin_REST_Reports_Customers_Stats_Controller extends WC_REST_Reports_C
$params['registered_between'] = array( $params['registered_between'] = array(
'description' => __( 'Limit response to objects last active between two given ISO8601 compliant datetime.', 'woocommerce-admin' ), 'description' => __( 'Limit response to objects last active between two given ISO8601 compliant datetime.', 'woocommerce-admin' ),
'type' => 'array', 'type' => 'array',
'validate_callback' => array( 'WC_Admin_Reports_Interval', 'rest_validate_between_date_arg' ), 'validate_callback' => array( '\Automattic\WooCommerce\Admin\API\Reports\TimeInterval', 'rest_validate_between_date_arg' ),
); );
$params['orders_count_min'] = array( $params['orders_count_min'] = array(
'description' => __( 'Limit response to objects with an order count greater than or equal to given integer.', 'woocommerce-admin' ), 'description' => __( 'Limit response to objects with an order count greater than or equal to given integer.', 'woocommerce-admin' ),
@ -305,7 +309,7 @@ class WC_Admin_REST_Reports_Customers_Stats_Controller extends WC_REST_Reports_C
$params['orders_count_between'] = array( $params['orders_count_between'] = array(
'description' => __( 'Limit response to objects with an order count between two given integers.', 'woocommerce-admin' ), 'description' => __( 'Limit response to objects with an order count between two given integers.', 'woocommerce-admin' ),
'type' => 'array', 'type' => 'array',
'validate_callback' => array( 'WC_Admin_Reports_Interval', 'rest_validate_between_numeric_arg' ), 'validate_callback' => array( '\Automattic\WooCommerce\Admin\API\Reports\TimeInterval', 'rest_validate_between_numeric_arg' ),
); );
$params['total_spend_min'] = array( $params['total_spend_min'] = array(
'description' => __( 'Limit response to objects with a total order spend greater than or equal to given number.', 'woocommerce-admin' ), 'description' => __( 'Limit response to objects with a total order spend greater than or equal to given number.', 'woocommerce-admin' ),
@ -320,7 +324,7 @@ class WC_Admin_REST_Reports_Customers_Stats_Controller extends WC_REST_Reports_C
$params['total_spend_between'] = array( $params['total_spend_between'] = array(
'description' => __( 'Limit response to objects with a total order spend between two given numbers.', 'woocommerce-admin' ), 'description' => __( 'Limit response to objects with a total order spend between two given numbers.', 'woocommerce-admin' ),
'type' => 'array', 'type' => 'array',
'validate_callback' => array( 'WC_Admin_Reports_Interval', 'rest_validate_between_numeric_arg' ), 'validate_callback' => array( '\Automattic\WooCommerce\Admin\API\Reports\TimeInterval', 'rest_validate_between_numeric_arg' ),
); );
$params['avg_order_value_min'] = array( $params['avg_order_value_min'] = array(
'description' => __( 'Limit response to objects with an average order spend greater than or equal to given number.', 'woocommerce-admin' ), 'description' => __( 'Limit response to objects with an average order spend greater than or equal to given number.', 'woocommerce-admin' ),
@ -335,7 +339,7 @@ class WC_Admin_REST_Reports_Customers_Stats_Controller extends WC_REST_Reports_C
$params['avg_order_value_between'] = array( $params['avg_order_value_between'] = array(
'description' => __( 'Limit response to objects with an average order spend between two given numbers.', 'woocommerce-admin' ), 'description' => __( 'Limit response to objects with an average order spend between two given numbers.', 'woocommerce-admin' ),
'type' => 'array', 'type' => 'array',
'validate_callback' => array( 'WC_Admin_Reports_Interval', 'rest_validate_between_numeric_arg' ), 'validate_callback' => array( '\Automattic\WooCommerce\Admin\API\Reports\TimeInterval', 'rest_validate_between_numeric_arg' ),
); );
$params['last_order_before'] = array( $params['last_order_before'] = array(
'description' => __( 'Limit response to objects with last order before (or at) a given ISO8601 compliant datetime.', 'woocommerce-admin' ), 'description' => __( 'Limit response to objects with last order before (or at) a given ISO8601 compliant datetime.', 'woocommerce-admin' ),

View File

@ -1,16 +1,21 @@
<?php <?php
/** /**
* WC_Admin_Reports_Customers_Stats_Data_Store class file. * API\Reports\Customers\Stats\DataStore class file.
* *
* @package WooCommerce Admin/Classes * @package WooCommerce Admin/Classes
*/ */
namespace Automattic\WooCommerce\Admin\API\Reports\Customers\Stats;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
use \Automattic\WooCommerce\Admin\API\Reports\Customers\DataStore as CustomersDataStore;
use \Automattic\WooCommerce\Admin\API\Reports\DataStoreInterface;
/** /**
* WC_Admin_Reports_Customers_Stats_Data_Store. * API\Reports\Customers\Stats\DataStore.
*/ */
class WC_Admin_Reports_Customers_Stats_Data_Store extends WC_Admin_Reports_Customers_Data_Store implements WC_Admin_Reports_Data_Store_Interface { class DataStore extends CustomersDataStore implements DataStoreInterface {
/** /**
* Mapping columns to data type to return correct response types. * Mapping columns to data type to return correct response types.
* *

View File

@ -10,18 +10,22 @@
* 'avg_order_value_min' => 100, * 'avg_order_value_min' => 100,
* 'country' => 'GB', * 'country' => 'GB',
* ); * );
* $report = new WC_Admin_Reports_Customers_Stats_Query( $args ); * $report = new \Automattic\WooCommerce\Admin\API\Reports\Customers\Stats\Query( $args );
* $mydata = $report->get_data(); * $mydata = $report->get_data();
* *
* @package WooCommerce Admin/Classes * @package WooCommerce Admin/Classes
*/ */
namespace Automattic\WooCommerce\Admin\API\Reports\Customers\Stats;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
use \Automattic\WooCommerce\Admin\API\Reports\Query as ReportsQuery;
/** /**
* WC_Admin_Reports_Customers_Stats_Query * API\Reports\Customers\Stats\Query
*/ */
class WC_Admin_Reports_Customers_Stats_Query extends WC_Admin_Reports_Query { class Query extends ReportsQuery {
/** /**
* Valid fields for Customers report. * Valid fields for Customers report.
@ -46,7 +50,7 @@ class WC_Admin_Reports_Customers_Stats_Query extends WC_Admin_Reports_Query {
public function get_data() { public function get_data() {
$args = apply_filters( 'woocommerce_reports_customers_stats_query_args', $this->get_query_vars() ); $args = apply_filters( 'woocommerce_reports_customers_stats_query_args', $this->get_query_vars() );
$data_store = WC_Data_Store::load( 'report-customers-stats' ); $data_store = \WC_Data_Store::load( 'report-customers-stats' );
$results = $data_store->get_data( $args ); $results = $data_store->get_data( $args );
return apply_filters( 'woocommerce_reports_customers_stats_select_query', $results, $args ); return apply_filters( 'woocommerce_reports_customers_stats_select_query', $results, $args );
} }

View File

@ -1,18 +1,22 @@
<?php <?php
/** /**
* WC_Admin_Reports_Data_Store class file. * Admin\API\Reports\DataStore class file.
* *
* @package WooCommerce Admin/Classes * @package WooCommerce Admin/Classes
*/ */
namespace Automattic\WooCommerce\Admin\API\Reports;
if ( ! defined( 'ABSPATH' ) ) { if ( ! defined( 'ABSPATH' ) ) {
exit; exit;
} }
use \Automattic\WooCommerce\Admin\API\Reports\TimeInterval;
/** /**
* WC_Admin_Reports_Data_Store: Common parent for custom report data stores. * Admin\API\Reports\DataStore: Common parent for custom report data stores.
*/ */
class WC_Admin_Reports_Data_Store { class DataStore {
/** /**
* Cache group for the reports. * Cache group for the reports.
@ -130,7 +134,7 @@ class WC_Admin_Reports_Data_Store {
*/ */
protected function fill_in_missing_intervals( $db_intervals, $start_datetime, $end_datetime, $time_interval, &$data ) { protected function fill_in_missing_intervals( $db_intervals, $start_datetime, $end_datetime, $time_interval, &$data ) {
// @todo This is ugly and messy. // @todo This is ugly and messy.
$local_tz = new DateTimeZone( wc_timezone_string() ); $local_tz = new \DateTimeZone( wc_timezone_string() );
// At this point, we don't know when we can stop iterating, as the ordering can be based on any value. // At this point, we don't know when we can stop iterating, as the ordering can be based on any value.
$time_ids = array_flip( wp_list_pluck( $data->intervals, 'time_interval' ) ); $time_ids = array_flip( wp_list_pluck( $data->intervals, 'time_interval' ) );
$db_intervals = array_flip( $db_intervals ); $db_intervals = array_flip( $db_intervals );
@ -142,14 +146,14 @@ class WC_Admin_Reports_Data_Store {
// @todo Should 'products' be in intervals? // @todo Should 'products' be in intervals?
unset( $totals_arr['products'] ); unset( $totals_arr['products'] );
while ( $start_datetime <= $end_datetime ) { while ( $start_datetime <= $end_datetime ) {
$next_start = WC_Admin_Reports_Interval::iterate( $start_datetime, $time_interval ); $next_start = TimeInterval::iterate( $start_datetime, $time_interval );
$time_id = WC_Admin_Reports_Interval::time_interval_id( $time_interval, $start_datetime ); $time_id = TimeInterval::time_interval_id( $time_interval, $start_datetime );
// Either create fill-zero interval or use data from db. // Either create fill-zero interval or use data from db.
if ( $next_start > $end_datetime ) { if ( $next_start > $end_datetime ) {
$interval_end = $end_datetime->format( 'Y-m-d H:i:s' ); $interval_end = $end_datetime->format( 'Y-m-d H:i:s' );
} else { } else {
$prev_end_timestamp = (int) $next_start->format( 'U' ) - 1; $prev_end_timestamp = (int) $next_start->format( 'U' ) - 1;
$prev_end = new DateTime(); $prev_end = new \DateTime();
$prev_end->setTimestamp( $prev_end_timestamp ); $prev_end->setTimestamp( $prev_end_timestamp );
$prev_end->setTimezone( $local_tz ); $prev_end->setTimezone( $local_tz );
$interval_end = $prev_end->format( 'Y-m-d H:i:s' ); $interval_end = $prev_end->format( 'Y-m-d H:i:s' );
@ -181,11 +185,11 @@ class WC_Admin_Reports_Data_Store {
* @param array $defaults Array of default values. * @param array $defaults Array of default values.
*/ */
protected function normalize_timezones( &$query_args, $defaults ) { protected function normalize_timezones( &$query_args, $defaults ) {
$local_tz = new DateTimeZone( wc_timezone_string() ); $local_tz = new \DateTimeZone( wc_timezone_string() );
foreach ( array( 'before', 'after' ) as $query_arg_key ) { foreach ( array( 'before', 'after' ) as $query_arg_key ) {
if ( isset( $query_args[ $query_arg_key ] ) && is_string( $query_args[ $query_arg_key ] ) ) { if ( isset( $query_args[ $query_arg_key ] ) && is_string( $query_args[ $query_arg_key ] ) ) {
// Assume that unspecified timezone is a local timezone. // Assume that unspecified timezone is a local timezone.
$datetime = new DateTime( $query_args[ $query_arg_key ], $local_tz ); $datetime = new \DateTime( $query_args[ $query_arg_key ], $local_tz );
// In case timezone was forced by using +HH:MM, convert to local timezone. // In case timezone was forced by using +HH:MM, convert to local timezone.
$datetime->setTimezone( $local_tz ); $datetime->setTimezone( $local_tz );
$query_args[ $query_arg_key ] = $datetime; $query_args[ $query_arg_key ] = $datetime;
@ -310,7 +314,7 @@ class WC_Admin_Reports_Data_Store {
if ( $db_interval_count === $expected_interval_count ) { if ( $db_interval_count === $expected_interval_count ) {
return; return;
} }
$local_tz = new DateTimeZone( wc_timezone_string() ); $local_tz = new \DateTimeZone( wc_timezone_string() );
if ( 'date' === strtolower( $query_args['orderby'] ) ) { if ( 'date' === strtolower( $query_args['orderby'] ) ) {
// page X in request translates to slightly different dates in the db, in case some // page X in request translates to slightly different dates in the db, in case some
// records are missing from the db. // records are missing from the db.
@ -327,7 +331,7 @@ class WC_Admin_Reports_Data_Store {
$start_iteration = 0; $start_iteration = 0;
break; break;
} }
$new_start_date = WC_Admin_Reports_Interval::iterate( $new_start_date, $query_args['interval'] ); $new_start_date = TimeInterval::iterate( $new_start_date, $query_args['interval'] );
$start_iteration ++; $start_iteration ++;
} }
@ -336,7 +340,7 @@ class WC_Admin_Reports_Data_Store {
if ( $new_end_date > $latest_end_date ) { if ( $new_end_date > $latest_end_date ) {
break; break;
} }
$new_end_date = WC_Admin_Reports_Interval::iterate( $new_end_date, $query_args['interval'] ); $new_end_date = TimeInterval::iterate( $new_end_date, $query_args['interval'] );
$end_iteration ++; $end_iteration ++;
} }
if ( $new_end_date > $latest_end_date ) { if ( $new_end_date > $latest_end_date ) {
@ -358,7 +362,7 @@ class WC_Admin_Reports_Data_Store {
$end_iteration = 0; $end_iteration = 0;
break; break;
} }
$new_end_date = WC_Admin_Reports_Interval::iterate( $new_end_date, $query_args['interval'], true ); $new_end_date = TimeInterval::iterate( $new_end_date, $query_args['interval'], true );
$end_iteration ++; $end_iteration ++;
} }
@ -367,7 +371,7 @@ class WC_Admin_Reports_Data_Store {
if ( $new_start_date < $earliest_start_date ) { if ( $new_start_date < $earliest_start_date ) {
break; break;
} }
$new_start_date = WC_Admin_Reports_Interval::iterate( $new_start_date, $query_args['interval'], true ); $new_start_date = TimeInterval::iterate( $new_start_date, $query_args['interval'], true );
$start_iteration ++; $start_iteration ++;
} }
if ( $new_start_date < $earliest_start_date ) { if ( $new_start_date < $earliest_start_date ) {
@ -382,8 +386,8 @@ class WC_Admin_Reports_Data_Store {
} }
$query_args['adj_after'] = $new_start_date; $query_args['adj_after'] = $new_start_date;
$query_args['adj_before'] = $new_end_date; $query_args['adj_before'] = $new_end_date;
$adj_after = $new_start_date->format( WC_Admin_Reports_Interval::$sql_datetime_format ); $adj_after = $new_start_date->format( TimeInterval::$sql_datetime_format );
$adj_before = $new_end_date->format( WC_Admin_Reports_Interval::$sql_datetime_format ); $adj_before = $new_end_date->format( TimeInterval::$sql_datetime_format );
$intervals_query['where_time_clause'] = ''; $intervals_query['where_time_clause'] = '';
$intervals_query['where_time_clause'] .= " AND {$table_name}.date_created <= '$adj_before'"; $intervals_query['where_time_clause'] .= " AND {$table_name}.date_created <= '$adj_before'";
$intervals_query['where_time_clause'] .= " AND {$table_name}.date_created >= '$adj_after'"; $intervals_query['where_time_clause'] .= " AND {$table_name}.date_created >= '$adj_after'";
@ -458,7 +462,7 @@ class WC_Admin_Reports_Data_Store {
* @return array * @return array
*/ */
protected static function get_excluded_report_order_statuses() { protected static function get_excluded_report_order_statuses() {
$excluded_statuses = WC_Admin_Settings::get_option( 'woocommerce_excluded_report_order_statuses', array( 'pending', 'failed', 'cancelled' ) ); $excluded_statuses = \WC_Admin_Settings::get_option( 'woocommerce_excluded_report_order_statuses', array( 'pending', 'failed', 'cancelled' ) );
$excluded_statuses = array_merge( array( 'trash' ), $excluded_statuses ); $excluded_statuses = array_merge( array( 'trash' ), $excluded_statuses );
return apply_filters( 'woocommerce_reports_excluded_order_statuses', $excluded_statuses ); return apply_filters( 'woocommerce_reports_excluded_order_statuses', $excluded_statuses );
} }
@ -499,11 +503,11 @@ class WC_Admin_Reports_Data_Store {
* @param array $intervals Array of intervals extracted from SQL db. * @param array $intervals Array of intervals extracted from SQL db.
*/ */
protected function update_interval_boundary_dates( $start_datetime, $end_datetime, $time_interval, &$intervals ) { protected function update_interval_boundary_dates( $start_datetime, $end_datetime, $time_interval, &$intervals ) {
$local_tz = new DateTimeZone( wc_timezone_string() ); $local_tz = new \DateTimeZone( wc_timezone_string() );
foreach ( $intervals as $key => $interval ) { foreach ( $intervals as $key => $interval ) {
$datetime = new DateTime( $interval['datetime_anchor'], $local_tz ); $datetime = new \DateTime( $interval['datetime_anchor'], $local_tz );
$prev_start = WC_Admin_Reports_Interval::iterate( $datetime, $time_interval, true ); $prev_start = TimeInterval::iterate( $datetime, $time_interval, true );
// @todo Not sure if the +1/-1 here are correct, especially as they are applied before the ?: below. // @todo Not sure if the +1/-1 here are correct, especially as they are applied before the ?: below.
$prev_start_timestamp = (int) $prev_start->format( 'U' ) + 1; $prev_start_timestamp = (int) $prev_start->format( 'U' ) + 1;
$prev_start->setTimestamp( $prev_start_timestamp ); $prev_start->setTimestamp( $prev_start_timestamp );
@ -514,7 +518,7 @@ class WC_Admin_Reports_Data_Store {
$intervals[ $key ]['date_start'] = $prev_start->format( 'Y-m-d H:i:s' ); $intervals[ $key ]['date_start'] = $prev_start->format( 'Y-m-d H:i:s' );
} }
$next_end = WC_Admin_Reports_Interval::iterate( $datetime, $time_interval ); $next_end = TimeInterval::iterate( $datetime, $time_interval );
$next_end_timestamp = (int) $next_end->format( 'U' ) - 1; $next_end_timestamp = (int) $next_end->format( 'U' ) - 1;
$next_end->setTimestamp( $next_end_timestamp ); $next_end->setTimestamp( $next_end_timestamp );
if ( $end_datetime ) { if ( $end_datetime ) {
@ -537,15 +541,15 @@ class WC_Admin_Reports_Data_Store {
*/ */
protected function create_interval_subtotals( &$intervals ) { protected function create_interval_subtotals( &$intervals ) {
foreach ( $intervals as $key => $interval ) { foreach ( $intervals as $key => $interval ) {
$start_gmt = WC_Admin_Reports_Interval::convert_local_datetime_to_gmt( $interval['date_start'] ); $start_gmt = TimeInterval::convert_local_datetime_to_gmt( $interval['date_start'] );
$end_gmt = WC_Admin_Reports_Interval::convert_local_datetime_to_gmt( $interval['date_end'] ); $end_gmt = TimeInterval::convert_local_datetime_to_gmt( $interval['date_end'] );
// Move intervals result to subtotals object. // Move intervals result to subtotals object.
$intervals[ $key ] = array( $intervals[ $key ] = array(
'interval' => $interval['time_interval'], 'interval' => $interval['time_interval'],
'date_start' => $interval['date_start'], 'date_start' => $interval['date_start'],
'date_start_gmt' => $start_gmt->format( WC_Admin_Reports_Interval::$sql_datetime_format ), 'date_start_gmt' => $start_gmt->format( TimeInterval::$sql_datetime_format ),
'date_end' => $interval['date_end'], 'date_end' => $interval['date_end'],
'date_end_gmt' => $end_gmt->format( WC_Admin_Reports_Interval::$sql_datetime_format ), 'date_end_gmt' => $end_gmt->format( TimeInterval::$sql_datetime_format ),
); );
unset( $interval['interval'] ); unset( $interval['interval'] );
@ -573,9 +577,9 @@ class WC_Admin_Reports_Data_Store {
if ( isset( $query_args['before'] ) && '' !== $query_args['before'] ) { if ( isset( $query_args['before'] ) && '' !== $query_args['before'] ) {
if ( is_a( $query_args['before'], 'WC_DateTime' ) ) { if ( is_a( $query_args['before'], 'WC_DateTime' ) ) {
$datetime_str = $query_args['before']->date( WC_Admin_Reports_Interval::$sql_datetime_format ); $datetime_str = $query_args['before']->date( TimeInterval::$sql_datetime_format );
} else { } else {
$datetime_str = $query_args['before']->format( WC_Admin_Reports_Interval::$sql_datetime_format ); $datetime_str = $query_args['before']->format( TimeInterval::$sql_datetime_format );
} }
$sql_query['where_time_clause'] .= " AND {$table_name}.date_created <= '$datetime_str'"; $sql_query['where_time_clause'] .= " AND {$table_name}.date_created <= '$datetime_str'";
@ -583,9 +587,9 @@ class WC_Admin_Reports_Data_Store {
if ( isset( $query_args['after'] ) && '' !== $query_args['after'] ) { if ( isset( $query_args['after'] ) && '' !== $query_args['after'] ) {
if ( is_a( $query_args['after'], 'WC_DateTime' ) ) { if ( is_a( $query_args['after'], 'WC_DateTime' ) ) {
$datetime_str = $query_args['after']->date( WC_Admin_Reports_Interval::$sql_datetime_format ); $datetime_str = $query_args['after']->date( TimeInterval::$sql_datetime_format );
} else { } else {
$datetime_str = $query_args['after']->format( WC_Admin_Reports_Interval::$sql_datetime_format ); $datetime_str = $query_args['after']->format( TimeInterval::$sql_datetime_format );
} }
$sql_query['where_time_clause'] .= " AND {$table_name}.date_created >= '$datetime_str'"; $sql_query['where_time_clause'] .= " AND {$table_name}.date_created >= '$datetime_str'";
} }
@ -708,7 +712,7 @@ class WC_Admin_Reports_Data_Store {
if ( isset( $query_args['interval'] ) && '' !== $query_args['interval'] ) { if ( isset( $query_args['interval'] ) && '' !== $query_args['interval'] ) {
$interval = $query_args['interval']; $interval = $query_args['interval'];
$intervals_query['select_clause'] = WC_Admin_Reports_Interval::db_datetime_format( $interval, $table_name ); $intervals_query['select_clause'] = TimeInterval::db_datetime_format( $interval, $table_name );
} }
$intervals_query = array_merge( $intervals_query, $this->get_limit_sql_params( $query_args ) ); $intervals_query = array_merge( $intervals_query, $this->get_limit_sql_params( $query_args ) );

View File

@ -5,6 +5,8 @@
* @package WooCommerce Admin/Interface * @package WooCommerce Admin/Interface
*/ */
namespace Automattic\WooCommerce\Admin\API\Reports;
if ( ! defined( 'ABSPATH' ) ) { if ( ! defined( 'ABSPATH' ) ) {
exit; exit;
} }
@ -14,7 +16,7 @@ if ( ! defined( 'ABSPATH' ) ) {
* *
* @since 3.5.0 * @since 3.5.0
*/ */
interface WC_Admin_Reports_Data_Store_Interface { interface DataStoreInterface {
/** /**
* Get the data based on args. * Get the data based on args.

View File

@ -7,16 +7,17 @@
* @package WooCommerce Admin/API * @package WooCommerce Admin/API
*/ */
namespace Automattic\WooCommerce\Admin\API\Reports\Downloads;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
/** /**
* REST API Reports downloads controller class. * REST API Reports downloads controller class.
* *
* @package WooCommerce/API * @package WooCommerce/API
* @extends WC_REST_Reports_Controller * @extends Automattic\WooCommerce\Admin\API\Reports\Controller
*/ */
class WC_Admin_REST_Reports_Downloads_Controller extends WC_Admin_REST_Reports_Controller { class Controller extends \Automattic\WooCommerce\Admin\API\Reports\Controller {
/** /**
* Endpoint namespace. * Endpoint namespace.
* *
@ -46,7 +47,7 @@ class WC_Admin_REST_Reports_Downloads_Controller extends WC_Admin_REST_Reports_C
} }
} }
$reports = new WC_Admin_Reports_Downloads_Query( $args ); $reports = new Query( $args );
$downloads_data = $reports->get_data(); $downloads_data = $reports->get_data();
$data = array(); $data = array();
@ -109,7 +110,7 @@ class WC_Admin_REST_Reports_Downloads_Controller extends WC_Admin_REST_Reports_C
$filename = basename( $file_path ); $filename = basename( $file_path );
$response->data['file_name'] = apply_filters( 'woocommerce_file_download_filename', $filename, $product_id ); $response->data['file_name'] = apply_filters( 'woocommerce_file_download_filename', $filename, $product_id );
$response->data['file_path'] = $file_path; $response->data['file_path'] = $file_path;
$customer = new WC_Customer( $data['user_id'] ); $customer = new \WC_Customer( $data['user_id'] );
$response->data['username'] = $customer->get_username(); $response->data['username'] = $customer->get_username();
$response->data['order_number'] = $this->get_order_number( $data['order_id'] ); $response->data['order_number'] = $this->get_order_number( $data['order_id'] );

View File

@ -1,16 +1,22 @@
<?php <?php
/** /**
* WC_Admin_Reports_Downloads_Data_Store class file. * API\Reports\Downloads\DataStore class file.
* *
* @package WooCommerce Admin/Classes * @package WooCommerce Admin/Classes
*/ */
namespace Automattic\WooCommerce\Admin\API\Reports\Downloads;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
use \Automattic\WooCommerce\Admin\API\Reports\DataStore as ReportsDataStore;
use \Automattic\WooCommerce\Admin\API\Reports\DataStoreInterface;
use \Automattic\WooCommerce\Admin\API\Reports\TimeInterval;
/** /**
* WC_Admin_Reports_Downloads_Data_Store. * API\Reports\Downloads\DataStore.
*/ */
class WC_Admin_Reports_Downloads_Data_Store extends WC_Admin_Reports_Data_Store implements WC_Admin_Reports_Data_Store_Interface { class DataStore extends ReportsDataStore implements DataStoreInterface {
/** /**
* Table used to get the data. * Table used to get the data.
@ -255,13 +261,13 @@ class WC_Admin_Reports_Downloads_Data_Store extends WC_Admin_Reports_Data_Store
); );
if ( $query_args['before'] ) { if ( $query_args['before'] ) {
$datetime_str = $query_args['before']->format( WC_Admin_Reports_Interval::$sql_datetime_format ); $datetime_str = $query_args['before']->format( TimeInterval::$sql_datetime_format );
$sql_query['where_time_clause'] .= " AND {$table_name}.timestamp <= '$datetime_str'"; $sql_query['where_time_clause'] .= " AND {$table_name}.timestamp <= '$datetime_str'";
} }
if ( $query_args['after'] ) { if ( $query_args['after'] ) {
$datetime_str = $query_args['after']->format( WC_Admin_Reports_Interval::$sql_datetime_format ); $datetime_str = $query_args['after']->format( TimeInterval::$sql_datetime_format );
$sql_query['where_time_clause'] .= " AND {$table_name}.timestamp >= '$datetime_str'"; $sql_query['where_time_clause'] .= " AND {$table_name}.timestamp >= '$datetime_str'";
} }
@ -312,8 +318,8 @@ class WC_Admin_Reports_Downloads_Data_Store extends WC_Admin_Reports_Data_Store
'page' => 1, 'page' => 1,
'order' => 'DESC', 'order' => 'DESC',
'orderby' => 'timestamp', 'orderby' => 'timestamp',
'before' => WC_Admin_Reports_Interval::default_before(), 'before' => TimeInterval::default_before(),
'after' => WC_Admin_Reports_Interval::default_after(), 'after' => TimeInterval::default_after(),
'fields' => '*', 'fields' => '*',
); );
$query_args = wp_parse_args( $query_args, $defaults ); $query_args = wp_parse_args( $query_args, $defaults );

View File

@ -7,6 +7,8 @@
* @package WooCommerce Admin/API * @package WooCommerce Admin/API
*/ */
namespace Automattic\WooCommerce\Admin\API\Reports\Downloads\Files;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
/** /**
@ -15,7 +17,7 @@ defined( 'ABSPATH' ) || exit;
* @package WooCommerce/API * @package WooCommerce/API
* @extends WC_REST_Reports_Controller * @extends WC_REST_Reports_Controller
*/ */
class WC_Admin_REST_Reports_Downloads_Files_Controller extends WC_REST_Reports_Controller { class Controller extends \WC_REST_Reports_Controller {
/** /**
* Endpoint namespace. * Endpoint namespace.

View File

@ -9,18 +9,22 @@
* 'page' => 2, * 'page' => 2,
* 'products' => array(1,2,3) * 'products' => array(1,2,3)
* ); * );
* $report = new WC_Admin_Reports_Downloads_Query( $args ); * $report = new \Automattic\WooCommerce\Admin\API\Reports\Downloads\Query( $args );
* $mydata = $report->get_data(); * $mydata = $report->get_data();
* *
* @package WooCommerce Admin/Classes * @package WooCommerce Admin/Classes
*/ */
namespace Automattic\WooCommerce\Admin\API\Reports\Downloads;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
use \Automattic\WooCommerce\Admin\API\Reports\Query as ReportsQuery;
/** /**
* WC_Admin_Reports_Downloads_Query * API\Reports\Downloads\Query
*/ */
class WC_Admin_Reports_Downloads_Query extends WC_Admin_Reports_Query { class Query extends ReportsQuery {
/** /**
* Valid fields for downloads report. * Valid fields for downloads report.
@ -39,7 +43,7 @@ class WC_Admin_Reports_Downloads_Query extends WC_Admin_Reports_Query {
public function get_data() { public function get_data() {
$args = apply_filters( 'woocommerce_reports_downloads_query_args', $this->get_query_vars() ); $args = apply_filters( 'woocommerce_reports_downloads_query_args', $this->get_query_vars() );
$data_store = WC_Data_Store::load( 'report-downloads' ); $data_store = \WC_Data_Store::load( 'report-downloads' );
$results = $data_store->get_data( $args ); $results = $data_store->get_data( $args );
return apply_filters( 'woocommerce_reports_downloads_select_query', $results, $args ); return apply_filters( 'woocommerce_reports_downloads_select_query', $results, $args );
} }

View File

@ -7,6 +7,8 @@
* @package WooCommerce Admin/API * @package WooCommerce Admin/API
*/ */
namespace Automattic\WooCommerce\Admin\API\Reports\Downloads\Stats;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
/** /**
@ -15,7 +17,7 @@ defined( 'ABSPATH' ) || exit;
* @package WooCommerce/API * @package WooCommerce/API
* @extends WC_REST_Reports_Controller * @extends WC_REST_Reports_Controller
*/ */
class WC_Admin_REST_Reports_Downloads_Stats_Controller extends WC_REST_Reports_Controller { class Controller extends \WC_REST_Reports_Controller {
/** /**
* Endpoint namespace. * Endpoint namespace.
@ -67,7 +69,7 @@ class WC_Admin_REST_Reports_Downloads_Stats_Controller extends WC_REST_Reports_C
*/ */
public function get_items( $request ) { public function get_items( $request ) {
$query_args = $this->prepare_reports_query( $request ); $query_args = $this->prepare_reports_query( $request );
$downloads_query = new WC_Admin_Reports_Downloads_Stats_Query( $query_args ); $downloads_query = new Query( $query_args );
$report_data = $downloads_query->get_data(); $report_data = $downloads_query->get_data();
$out_data = array( $out_data = array(

View File

@ -1,16 +1,22 @@
<?php <?php
/** /**
* WC_Admin_Reports_Downloads_Data_Store class file. * API\Reports\Downloads\Stats\DataStore class file.
* *
* @package WooCommerce Admin/Classes * @package WooCommerce Admin/Classes
*/ */
namespace Automattic\WooCommerce\Admin\API\Reports\Downloads\Stats;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
use \Automattic\WooCommerce\Admin\API\Reports\Downloads\DataStore as DownloadsDataStore;
use \Automattic\WooCommerce\Admin\API\Reports\DataStoreInterface;
use \Automattic\WooCommerce\Admin\API\Reports\TimeInterval;
/** /**
* WC_Admin_Reports_Downloads_Data_Store. * API\Reports\Downloads\Stats\DataStore.
*/ */
class WC_Admin_Reports_Downloads_Stats_Data_Store extends WC_Admin_Reports_Downloads_Data_Store implements WC_Admin_Reports_Data_Store_Interface { class DataStore extends DownloadsDataStore implements DataStoreInterface {
/** /**
* Mapping columns to data type to return correct response types. * Mapping columns to data type to return correct response types.
@ -57,8 +63,8 @@ class WC_Admin_Reports_Downloads_Stats_Data_Store extends WC_Admin_Reports_Downl
'orderby' => 'date', 'orderby' => 'date',
'fields' => '*', 'fields' => '*',
'interval' => 'week', 'interval' => 'week',
'before' => WC_Admin_Reports_Interval::default_before(), 'before' => TimeInterval::default_before(),
'after' => WC_Admin_Reports_Interval::default_after(), 'after' => TimeInterval::default_after(),
); );
$query_args = wp_parse_args( $query_args, $defaults ); $query_args = wp_parse_args( $query_args, $defaults );
$this->normalize_timezones( $query_args, $defaults ); $this->normalize_timezones( $query_args, $defaults );
@ -95,7 +101,7 @@ class WC_Admin_Reports_Downloads_Stats_Data_Store extends WC_Admin_Reports_Downl
$db_records_count = count( $db_intervals ); $db_records_count = count( $db_intervals );
$expected_interval_count = WC_Admin_Reports_Interval::intervals_between( $query_args['after'], $query_args['before'], $query_args['interval'] ); $expected_interval_count = TimeInterval::intervals_between( $query_args['after'], $query_args['before'], $query_args['interval'] );
$total_pages = (int) ceil( $expected_interval_count / $intervals_query['per_page'] ); $total_pages = (int) ceil( $expected_interval_count / $intervals_query['per_page'] );
if ( $query_args['page'] < 1 || $query_args['page'] > $total_pages ) { if ( $query_args['page'] < 1 || $query_args['page'] > $total_pages ) {
return array(); return array();
@ -118,7 +124,7 @@ class WC_Admin_Reports_Downloads_Stats_Data_Store extends WC_Admin_Reports_Downl
); // WPCS: cache ok, DB call ok, unprepared SQL ok. ); // WPCS: cache ok, DB call ok, unprepared SQL ok.
if ( null === $totals ) { if ( null === $totals ) {
return new WP_Error( 'woocommerce_reports_downloads_stats_result_failed', __( 'Sorry, fetching downloads data failed.', 'woocommerce-admin' ) ); return new \WP_Error( 'woocommerce_reports_downloads_stats_result_failed', __( 'Sorry, fetching downloads data failed.', 'woocommerce-admin' ) );
} }
if ( '' !== $selections ) { if ( '' !== $selections ) {
@ -146,7 +152,7 @@ class WC_Admin_Reports_Downloads_Stats_Data_Store extends WC_Admin_Reports_Downl
); // WPCS: cache ok, DB call ok, unprepared SQL ok. ); // WPCS: cache ok, DB call ok, unprepared SQL ok.
if ( null === $intervals ) { if ( null === $intervals ) {
return new WP_Error( 'woocommerce_reports_downloads_stats_result_failed', __( 'Sorry, fetching downloads data failed.', 'woocommerce-admin' ) ); return new \WP_Error( 'woocommerce_reports_downloads_stats_result_failed', __( 'Sorry, fetching downloads data failed.', 'woocommerce-admin' ) );
} }
$totals = (object) $this->cast_numbers( $totals[0] ); $totals = (object) $this->cast_numbers( $totals[0] );

View File

@ -5,12 +5,16 @@
* @package WooCommerce Admin/Classes * @package WooCommerce Admin/Classes
*/ */
namespace Automattic\WooCommerce\Admin\API\Reports\Downloads\Stats;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
use \Automattic\WooCommerce\Admin\API\Reports\Query as ReportsQuery;
/** /**
* WC_Admin_Reports_Downloads_Stats_Query * API\Reports\Downloads\Stats\Query
*/ */
class WC_Admin_Reports_Downloads_Stats_Query extends WC_Admin_Reports_Query { class Query extends ReportsQuery {
/** /**
* Valid fields for Orders report. * Valid fields for Orders report.
@ -29,7 +33,7 @@ class WC_Admin_Reports_Downloads_Stats_Query extends WC_Admin_Reports_Query {
public function get_data() { public function get_data() {
$args = apply_filters( 'woocommerce_reports_downloads_stats_query_args', $this->get_query_vars() ); $args = apply_filters( 'woocommerce_reports_downloads_stats_query_args', $this->get_query_vars() );
$data_store = WC_Data_Store::load( 'report-downloads-stats' ); $data_store = \WC_Data_Store::load( 'report-downloads-stats' );
$results = $data_store->get_data( $args ); $results = $data_store->get_data( $args );
return apply_filters( 'woocommerce_reports_downloads_stats_select_query', $results, $args ); return apply_filters( 'woocommerce_reports_downloads_stats_select_query', $results, $args );
} }

View File

@ -9,15 +9,19 @@
* @package WooCommerce Admin/API * @package WooCommerce Admin/API
*/ */
namespace Automattic\WooCommerce\Admin\API\Reports\Export;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
use \Automattic\WooCommerce\Admin\ReportExporter;
/** /**
* Reports Export controller. * Reports Export controller.
* *
* @package WooCommerce Admin/API * @package WooCommerce Admin/API
* @extends WC_REST_Data_Controller * @extends \Automattic\WooCommerce\Admin\API\Reports\Controller
*/ */
class WC_Admin_REST_Reports_Export_Controller extends WC_Admin_REST_Reports_Controller { class Controller extends \Automattic\WooCommerce\Admin\API\Reports\Controller {
/** /**
* Endpoint namespace. * Endpoint namespace.
* *
@ -41,7 +45,7 @@ class WC_Admin_REST_Reports_Export_Controller extends WC_Admin_REST_Reports_Cont
'/' . $this->rest_base, '/' . $this->rest_base,
array( array(
array( array(
'methods' => WP_REST_Server::EDITABLE, 'methods' => \WP_REST_Server::EDITABLE,
'callback' => array( $this, 'export_items' ), 'callback' => array( $this, 'export_items' ),
'permission_callback' => array( $this, 'get_items_permissions_check' ), 'permission_callback' => array( $this, 'get_items_permissions_check' ),
'args' => $this->get_export_collection_params(), 'args' => $this->get_export_collection_params(),
@ -55,7 +59,7 @@ class WC_Admin_REST_Reports_Export_Controller extends WC_Admin_REST_Reports_Cont
'/' . $this->rest_base . '/(?P<export_id>[a-z0-9]+)/status', '/' . $this->rest_base . '/(?P<export_id>[a-z0-9]+)/status',
array( array(
array( array(
'methods' => WP_REST_Server::READABLE, 'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'export_status' ), 'callback' => array( $this, 'export_status' ),
'permission_callback' => array( $this, 'get_items_permissions_check' ), 'permission_callback' => array( $this, 'get_items_permissions_check' ),
), ),
@ -148,7 +152,7 @@ class WC_Admin_REST_Reports_Export_Controller extends WC_Admin_REST_Reports_Cont
$report_args = empty( $request['report_args'] ) ? array() : $request['report_args']; $report_args = empty( $request['report_args'] ) ? array() : $request['report_args'];
$export_id = str_replace( '.', '', microtime( true ) ); $export_id = str_replace( '.', '', microtime( true ) );
$total_rows = WC_Admin_Report_Exporter::queue_report_export( $export_id, $report_type, $report_args ); $total_rows = ReportExporter::queue_report_export( $export_id, $report_type, $report_args );
if ( 0 === $total_rows ) { if ( 0 === $total_rows ) {
$response = rest_ensure_response( $response = rest_ensure_response(
@ -175,7 +179,7 @@ class WC_Admin_REST_Reports_Export_Controller extends WC_Admin_REST_Reports_Cont
) )
); );
WC_Admin_Report_Exporter::update_export_percentage_complete( $report_type, $export_id, 0 ); ReportExporter::update_export_percentage_complete( $report_type, $export_id, 0 );
} }
$data = $this->prepare_response_for_collection( $response ); $data = $this->prepare_response_for_collection( $response );
@ -192,10 +196,10 @@ class WC_Admin_REST_Reports_Export_Controller extends WC_Admin_REST_Reports_Cont
public function export_status( $request ) { public function export_status( $request ) {
$report_type = $request['type']; $report_type = $request['type'];
$export_id = $request['export_id']; $export_id = $request['export_id'];
$percentage = WC_Admin_Report_Exporter::get_export_percentage_complete( $report_type, $export_id ); $percentage = ReportExporter::get_export_percentage_complete( $report_type, $export_id );
if ( false === $percentage ) { if ( false === $percentage ) {
return new WP_Error( return new \WP_Error(
'woocommerce_admin_reports_export_invalid_id', 'woocommerce_admin_reports_export_invalid_id',
__( 'Sorry, there is no export with that ID.', 'woocommerce-admin' ), __( 'Sorry, there is no export with that ID.', 'woocommerce-admin' ),
array( 'status' => 404 ) array( 'status' => 404 )
@ -209,7 +213,7 @@ class WC_Admin_REST_Reports_Export_Controller extends WC_Admin_REST_Reports_Cont
// @todo - add thing in the links below instead? // @todo - add thing in the links below instead?
if ( 100 === $percentage ) { if ( 100 === $percentage ) {
$query_args = array( $query_args = array(
'action' => WC_Admin_Report_Exporter::DOWNLOAD_EXPORT_ACTION, 'action' => ReportExporter::DOWNLOAD_EXPORT_ACTION,
'filename' => "wc-{$report_type}-report-export-{$export_id}", 'filename' => "wc-{$report_type}-report-export-{$export_id}",
); );

View File

@ -7,15 +7,19 @@
* @package WooCommerce Admin/API * @package WooCommerce Admin/API
*/ */
namespace Automattic\WooCommerce\Admin\API\Reports\Import;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\ReportsSync;
/** /**
* Reports Imports controller. * Reports Imports controller.
* *
* @package WooCommerce Admin/API * @package WooCommerce Admin/API
* @extends WC_REST_Data_Controller * @extends \Automattic\WooCommerce\Admin\API\Reports\Controller
*/ */
class WC_Admin_REST_Reports_Import_Controller extends WC_Admin_REST_Reports_Controller { class Controller extends \Automattic\WooCommerce\Admin\API\Reports\Controller {
/** /**
* Endpoint namespace. * Endpoint namespace.
* *
@ -39,7 +43,7 @@ class WC_Admin_REST_Reports_Import_Controller extends WC_Admin_REST_Reports_Cont
'/' . $this->rest_base, '/' . $this->rest_base,
array( array(
array( array(
'methods' => WP_REST_Server::EDITABLE, 'methods' => \WP_REST_Server::EDITABLE,
'callback' => array( $this, 'import_items' ), 'callback' => array( $this, 'import_items' ),
'permission_callback' => array( $this, 'import_permissions_check' ), 'permission_callback' => array( $this, 'import_permissions_check' ),
'args' => $this->get_import_collection_params(), 'args' => $this->get_import_collection_params(),
@ -52,7 +56,7 @@ class WC_Admin_REST_Reports_Import_Controller extends WC_Admin_REST_Reports_Cont
'/' . $this->rest_base . '/cancel', '/' . $this->rest_base . '/cancel',
array( array(
array( array(
'methods' => WP_REST_Server::EDITABLE, 'methods' => \WP_REST_Server::EDITABLE,
'callback' => array( $this, 'cancel_import' ), 'callback' => array( $this, 'cancel_import' ),
'permission_callback' => array( $this, 'import_permissions_check' ), 'permission_callback' => array( $this, 'import_permissions_check' ),
), ),
@ -64,7 +68,7 @@ class WC_Admin_REST_Reports_Import_Controller extends WC_Admin_REST_Reports_Cont
'/' . $this->rest_base . '/delete', '/' . $this->rest_base . '/delete',
array( array(
array( array(
'methods' => WP_REST_Server::EDITABLE, 'methods' => \WP_REST_Server::EDITABLE,
'callback' => array( $this, 'delete_imported_items' ), 'callback' => array( $this, 'delete_imported_items' ),
'permission_callback' => array( $this, 'import_permissions_check' ), 'permission_callback' => array( $this, 'import_permissions_check' ),
), ),
@ -76,7 +80,7 @@ class WC_Admin_REST_Reports_Import_Controller extends WC_Admin_REST_Reports_Cont
'/' . $this->rest_base . '/status', '/' . $this->rest_base . '/status',
array( array(
array( array(
'methods' => WP_REST_Server::READABLE, 'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_import_status' ), 'callback' => array( $this, 'get_import_status' ),
'permission_callback' => array( $this, 'import_permissions_check' ), 'permission_callback' => array( $this, 'import_permissions_check' ),
), ),
@ -88,7 +92,7 @@ class WC_Admin_REST_Reports_Import_Controller extends WC_Admin_REST_Reports_Cont
'/' . $this->rest_base . '/totals', '/' . $this->rest_base . '/totals',
array( array(
array( array(
'methods' => WP_REST_Server::READABLE, 'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_import_totals' ), 'callback' => array( $this, 'get_import_totals' ),
'permission_callback' => array( $this, 'import_permissions_check' ), 'permission_callback' => array( $this, 'import_permissions_check' ),
'args' => $this->get_import_collection_params(), 'args' => $this->get_import_collection_params(),
@ -106,7 +110,7 @@ class WC_Admin_REST_Reports_Import_Controller extends WC_Admin_REST_Reports_Cont
*/ */
public function import_permissions_check( $request ) { public function import_permissions_check( $request ) {
if ( ! wc_rest_check_manager_permissions( 'settings', 'edit' ) ) { if ( ! wc_rest_check_manager_permissions( 'settings', 'edit' ) ) {
return new WP_Error( 'woocommerce_rest_cannot_edit', __( 'Sorry, you cannot edit this resource.', 'woocommerce-admin' ), array( 'status' => rest_authorization_required_code() ) ); return new \WP_Error( 'woocommerce_rest_cannot_edit', __( 'Sorry, you cannot edit this resource.', 'woocommerce-admin' ), array( 'status' => rest_authorization_required_code() ) );
} }
return true; return true;
} }
@ -119,7 +123,7 @@ class WC_Admin_REST_Reports_Import_Controller extends WC_Admin_REST_Reports_Cont
*/ */
public function import_items( $request ) { public function import_items( $request ) {
$query_args = $this->prepare_objects_query( $request ); $query_args = $this->prepare_objects_query( $request );
$import = WC_Admin_Reports_Sync::regenerate_report_data( $query_args['days'], $query_args['skip_existing'] ); $import = ReportsSync::regenerate_report_data( $query_args['days'], $query_args['skip_existing'] );
if ( is_wp_error( $import ) ) { if ( is_wp_error( $import ) ) {
$result = array( $result = array(
@ -235,7 +239,7 @@ class WC_Admin_REST_Reports_Import_Controller extends WC_Admin_REST_Reports_Cont
* @return WP_Error|WP_REST_Response * @return WP_Error|WP_REST_Response
*/ */
public function cancel_import( $request ) { public function cancel_import( $request ) {
WC_Admin_Reports_Sync::clear_queued_actions(); ReportsSync::clear_queued_actions();
$result = array( $result = array(
'status' => 'success', 'status' => 'success',
@ -255,7 +259,7 @@ class WC_Admin_REST_Reports_Import_Controller extends WC_Admin_REST_Reports_Cont
* @return WP_Error|WP_REST_Response * @return WP_Error|WP_REST_Response
*/ */
public function delete_imported_items( $request ) { public function delete_imported_items( $request ) {
$delete = WC_Admin_Reports_Sync::delete_report_data(); $delete = ReportsSync::delete_report_data();
if ( is_wp_error( $delete ) ) { if ( is_wp_error( $delete ) ) {
$result = array( $result = array(
@ -283,7 +287,7 @@ class WC_Admin_REST_Reports_Import_Controller extends WC_Admin_REST_Reports_Cont
*/ */
public function get_import_status( $request ) { public function get_import_status( $request ) {
$result = array( $result = array(
'is_importing' => WC_Admin_Reports_Sync::is_importing(), 'is_importing' => ReportsSync::is_importing(),
'customers_total' => (int) get_option( 'wc_admin_import_customers_total', 0 ), 'customers_total' => (int) get_option( 'wc_admin_import_customers_total', 0 ),
'customers_count' => (int) get_option( 'wc_admin_import_customers_count', 0 ), 'customers_count' => (int) get_option( 'wc_admin_import_customers_count', 0 ),
'orders_total' => (int) get_option( 'wc_admin_import_orders_total', 0 ), 'orders_total' => (int) get_option( 'wc_admin_import_orders_total', 0 ),
@ -305,7 +309,7 @@ class WC_Admin_REST_Reports_Import_Controller extends WC_Admin_REST_Reports_Cont
*/ */
public function get_import_totals( $request ) { public function get_import_totals( $request ) {
$query_args = $this->prepare_objects_query( $request ); $query_args = $this->prepare_objects_query( $request );
$totals = WC_Admin_Reports_Sync::get_import_totals( $query_args['days'], $query_args['skip_existing'] ); $totals = ReportsSync::get_import_totals( $query_args['days'], $query_args['skip_existing'] );
$response = $this->prepare_item_for_response( $totals, $request ); $response = $this->prepare_item_for_response( $totals, $request );
$data = $this->prepare_response_for_collection( $response ); $data = $this->prepare_response_for_collection( $response );

View File

@ -7,15 +7,17 @@
* @package WooCommerce Admin/API * @package WooCommerce Admin/API
*/ */
namespace Automattic\WooCommerce\Admin\API\Reports\Orders;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
/** /**
* REST API Reports orders controller class. * REST API Reports orders controller class.
* *
* @package WooCommerce/API * @package WooCommerce/API
* @extends WC_Admin_REST_Reports_Controller * @extends \Automattic\WooCommerce\Admin\API\Reports\Controller
*/ */
class WC_Admin_REST_Reports_Orders_Controller extends WC_Admin_REST_Reports_Controller { class Controller extends \Automattic\WooCommerce\Admin\API\Reports\Controller {
/** /**
* Endpoint namespace. * Endpoint namespace.
@ -69,7 +71,7 @@ class WC_Admin_REST_Reports_Orders_Controller extends WC_Admin_REST_Reports_Cont
*/ */
public function get_items( $request ) { public function get_items( $request ) {
$query_args = $this->prepare_reports_query( $request ); $query_args = $this->prepare_reports_query( $request );
$orders_query = new WC_Admin_Reports_Orders_Query( $query_args ); $orders_query = new Query( $query_args );
$report_data = $orders_query->get_data(); $report_data = $orders_query->get_data();
$data = array(); $data = array();

View File

@ -1,16 +1,21 @@
<?php <?php
/** /**
* WC_Admin_Reports_Orders_Data_Store class file. * API\Reports\Orders\DataStore class file.
* *
* @package WooCommerce Admin/Classes * @package WooCommerce Admin/Classes
*/ */
namespace Automattic\WooCommerce\Admin\API\Reports\Orders;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
use \Automattic\WooCommerce\Admin\API\Reports\DataStore as ReportsDataStore;
use \Automattic\WooCommerce\Admin\API\Reports\DataStoreInterface;
/** /**
* WC_Admin_Reports_Orders_Data_Store. * API\Reports\Orders\DataStore.
*/ */
class WC_Admin_Reports_Orders_Data_Store extends WC_Admin_Reports_Data_Store implements WC_Admin_Reports_Data_Store_Interface { class DataStore extends ReportsDataStore implements DataStoreInterface {
/** /**
* Table used to get the data. * Table used to get the data.

View File

@ -13,18 +13,22 @@
* 'status_is_not' => array('failed'), * 'status_is_not' => array('failed'),
* 'new_customers' => false, * 'new_customers' => false,
* ); * );
* $report = new WC_Admin_Reports_Orders_Query( $args ); * $report = new \Automattic\WooCommerce\Admin\API\Reports\Orders\Query( $args );
* $mydata = $report->get_data(); * $mydata = $report->get_data();
* *
* @package WooCommerce Admin/Classes * @package WooCommerce Admin/Classes
*/ */
namespace Automattic\WooCommerce\Admin\API\Reports\Orders;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
use \Automattic\WooCommerce\Admin\API\Reports\Query as ReportsQuery;
/** /**
* WC_Admin_Reports_Orders_Query * API\Reports\Orders\Query
*/ */
class WC_Admin_Reports_Orders_Query extends WC_Admin_Reports_Query { class Query extends ReportsQuery {
/** /**
* Get order data based on the current query vars. * Get order data based on the current query vars.
@ -33,7 +37,7 @@ class WC_Admin_Reports_Orders_Query extends WC_Admin_Reports_Query {
*/ */
public function get_data() { public function get_data() {
$args = apply_filters( 'woocommerce_reports_orders_query_args', $this->get_query_vars() ); $args = apply_filters( 'woocommerce_reports_orders_query_args', $this->get_query_vars() );
$data_store = WC_Data_Store::load( 'report-orders' ); $data_store = \WC_Data_Store::load( 'report-orders' );
$results = $data_store->get_data( $args ); $results = $data_store->get_data( $args );
return apply_filters( 'woocommerce_reports_orders_select_query', $results, $args ); return apply_filters( 'woocommerce_reports_orders_select_query', $results, $args );
} }

View File

@ -7,15 +7,19 @@
* @package WooCommerce Admin/API * @package WooCommerce Admin/API
*/ */
namespace Automattic\WooCommerce\Admin\API\Reports\Orders\Stats;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
use \Automattic\WooCommerce\Admin\API\Reports\ParameterException;
/** /**
* REST API Reports orders stats controller class. * REST API Reports orders stats controller class.
* *
* @package WooCommerce/API * @package WooCommerce/API
* @extends WC_Admin_REST_Reports_Controller * @extends \Automattic\WooCommerce\Admin\API\Reports\Controller
*/ */
class WC_Admin_REST_Reports_Orders_Stats_Controller extends WC_Admin_REST_Reports_Controller { class Controller extends \Automattic\WooCommerce\Admin\API\Reports\Controller {
/** /**
* Endpoint namespace. * Endpoint namespace.
@ -70,11 +74,11 @@ class WC_Admin_REST_Reports_Orders_Stats_Controller extends WC_Admin_REST_Report
*/ */
public function get_items( $request ) { public function get_items( $request ) {
$query_args = $this->prepare_reports_query( $request ); $query_args = $this->prepare_reports_query( $request );
$orders_query = new WC_Admin_Reports_Orders_Stats_Query( $query_args ); $orders_query = new Query( $query_args );
try { try {
$report_data = $orders_query->get_data(); $report_data = $orders_query->get_data();
} catch ( WC_Admin_Reports_Parameter_Exception $e ) { } catch ( ParameterException $e ) {
return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); return new \WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
} }
$out_data = array( $out_data = array(

View File

@ -1,16 +1,22 @@
<?php <?php
/** /**
* WC_Admin_Reports_Orders_Stats_Data_Store class file. * API\Reports\Orders\Stats\DataStore class file.
* *
* @package WooCommerce Admin/Classes * @package WooCommerce Admin/Classes
*/ */
namespace Automattic\WooCommerce\Admin\API\Reports\Orders\Stats;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
use \Automattic\WooCommerce\Admin\API\Reports\DataStore as ReportsDataStore;
use \Automattic\WooCommerce\Admin\API\Reports\DataStoreInterface;
use \Automattic\WooCommerce\Admin\API\Reports\TimeInterval;
/** /**
* WC_Admin_Reports_Orders_Stats_Data_Store. * API\Reports\Orders\Stats\DataStore.
*/ */
class WC_Admin_Reports_Orders_Stats_Data_Store extends WC_Admin_Reports_Data_Store implements WC_Admin_Reports_Data_Store_Interface { class DataStore extends ReportsDataStore implements DataStoreInterface {
/** /**
* Table used to get the data. * Table used to get the data.
@ -202,8 +208,8 @@ class WC_Admin_Reports_Orders_Stats_Data_Store extends WC_Admin_Reports_Data_Sto
'page' => 1, 'page' => 1,
'order' => 'DESC', 'order' => 'DESC',
'orderby' => 'date', 'orderby' => 'date',
'before' => WC_Admin_Reports_Interval::default_before(), 'before' => TimeInterval::default_before(),
'after' => WC_Admin_Reports_Interval::default_after(), 'after' => TimeInterval::default_after(),
'interval' => 'week', 'interval' => 'week',
'fields' => '*', 'fields' => '*',
'segmentby' => '', 'segmentby' => '',
@ -270,10 +276,10 @@ class WC_Admin_Reports_Orders_Stats_Data_Store extends WC_Admin_Reports_Data_Sto
$unique_products = $this->get_unique_product_count( $totals_query['from_clause'], $totals_query['where_time_clause'], $totals_query['where_clause'] ); $unique_products = $this->get_unique_product_count( $totals_query['from_clause'], $totals_query['where_time_clause'], $totals_query['where_clause'] );
$totals[0]['products'] = $unique_products; $totals[0]['products'] = $unique_products;
$segmenting = new WC_Admin_Reports_Orders_Stats_Segmenting( $query_args, $this->report_columns ); $segmenter = new Segmenter( $query_args, $this->report_columns );
$unique_coupons = $this->get_unique_coupon_count( $totals_query['from_clause'], $totals_query['where_time_clause'], $totals_query['where_clause'] ); $unique_coupons = $this->get_unique_coupon_count( $totals_query['from_clause'], $totals_query['where_time_clause'], $totals_query['where_clause'] );
$totals[0]['coupons_count'] = $unique_coupons; $totals[0]['coupons_count'] = $unique_coupons;
$totals[0]['segments'] = $segmenting->get_totals_segments( $totals_query, $table_name ); $totals[0]['segments'] = $segmenter->get_totals_segments( $totals_query, $table_name );
$totals = (object) $this->cast_numbers( $totals[0] ); $totals = (object) $this->cast_numbers( $totals[0] );
$db_intervals = $wpdb->get_col( $db_intervals = $wpdb->get_col(
@ -292,7 +298,7 @@ class WC_Admin_Reports_Orders_Stats_Data_Store extends WC_Admin_Reports_Data_Sto
); // WPCS: cache ok, DB call ok, , unprepared SQL ok. ); // WPCS: cache ok, DB call ok, , unprepared SQL ok.
$db_interval_count = count( $db_intervals ); $db_interval_count = count( $db_intervals );
$expected_interval_count = WC_Admin_Reports_Interval::intervals_between( $query_args['after'], $query_args['before'], $query_args['interval'] ); $expected_interval_count = TimeInterval::intervals_between( $query_args['after'], $query_args['before'], $query_args['interval'] );
$total_pages = (int) ceil( $expected_interval_count / $intervals_query['per_page'] ); $total_pages = (int) ceil( $expected_interval_count / $intervals_query['per_page'] );
if ( $query_args['page'] < 1 || $query_args['page'] > $total_pages ) { if ( $query_args['page'] < 1 || $query_args['page'] > $total_pages ) {
@ -327,7 +333,7 @@ class WC_Admin_Reports_Orders_Stats_Data_Store extends WC_Admin_Reports_Data_Sto
); // WPCS: cache ok, DB call ok, unprepared SQL ok. ); // WPCS: cache ok, DB call ok, unprepared SQL ok.
if ( null === $intervals ) { if ( null === $intervals ) {
return new WP_Error( 'woocommerce_reports_revenue_result_failed', __( 'Sorry, fetching revenue data failed.', 'woocommerce-admin' ) ); return new \WP_Error( 'woocommerce_reports_revenue_result_failed', __( 'Sorry, fetching revenue data failed.', 'woocommerce-admin' ) );
} }
if ( isset( $intervals[0] ) ) { if ( isset( $intervals[0] ) ) {
@ -343,14 +349,14 @@ class WC_Admin_Reports_Orders_Stats_Data_Store extends WC_Admin_Reports_Data_Sto
'page_no' => (int) $query_args['page'], 'page_no' => (int) $query_args['page'],
); );
if ( WC_Admin_Reports_Interval::intervals_missing( $expected_interval_count, $db_interval_count, $intervals_query['per_page'], $query_args['page'], $query_args['order'], $query_args['orderby'], count( $intervals ) ) ) { if ( TimeInterval::intervals_missing( $expected_interval_count, $db_interval_count, $intervals_query['per_page'], $query_args['page'], $query_args['order'], $query_args['orderby'], count( $intervals ) ) ) {
$this->fill_in_missing_intervals( $db_intervals, $query_args['adj_after'], $query_args['adj_before'], $query_args['interval'], $data ); $this->fill_in_missing_intervals( $db_intervals, $query_args['adj_after'], $query_args['adj_before'], $query_args['interval'], $data );
$this->sort_intervals( $data, $query_args['orderby'], $query_args['order'] ); $this->sort_intervals( $data, $query_args['orderby'], $query_args['order'] );
$this->remove_extra_records( $data, $query_args['page'], $intervals_query['per_page'], $db_interval_count, $expected_interval_count, $query_args['orderby'], $query_args['order'] ); $this->remove_extra_records( $data, $query_args['page'], $intervals_query['per_page'], $db_interval_count, $expected_interval_count, $query_args['orderby'], $query_args['order'] );
} else { } else {
$this->update_interval_boundary_dates( $query_args['after'], $query_args['before'], $query_args['interval'], $data->intervals ); $this->update_interval_boundary_dates( $query_args['after'], $query_args['before'], $query_args['interval'], $data->intervals );
} }
$segmenting->add_intervals_segments( $data, $intervals_query, $table_name ); $segmenter->add_intervals_segments( $data, $intervals_query, $table_name );
$this->create_interval_subtotals( $data->intervals ); $this->create_interval_subtotals( $data->intervals );
wp_cache_set( $cache_key, $data, $this->cache_group ); wp_cache_set( $cache_key, $data, $this->cache_group );
@ -565,13 +571,13 @@ class WC_Admin_Reports_Orders_Stats_Data_Store extends WC_Admin_Reports_Data_Sto
* @return bool * @return bool
*/ */
public static function is_returning_customer( $order ) { public static function is_returning_customer( $order ) {
$customer_id = WC_Admin_Reports_Customers_Data_Store::get_existing_customer_id_from_order( $order ); $customer_id = \Automattic\WooCommerce\Admin\API\Reports\Customers\DataStore::get_existing_customer_id_from_order( $order );
if ( ! $customer_id ) { if ( ! $customer_id ) {
return false; return false;
} }
$oldest_orders = WC_Admin_Reports_Customers_Data_Store::get_oldest_orders( $customer_id ); $oldest_orders = \Automattic\WooCommerce\Admin\API\Reports\Customers\DataStore::get_oldest_orders( $customer_id );
if ( empty( $oldest_orders ) ) { if ( empty( $oldest_orders ) ) {
return false; return false;

View File

@ -11,18 +11,22 @@
* 'coupons' => array(138), * 'coupons' => array(138),
* 'status_in' => array('completed'), * 'status_in' => array('completed'),
* ); * );
* $report = new WC_Admin_Reports_Orders_Stats_Query( $args ); * $report = new \Automattic\WooCommerce\Admin\API\Reports\Orders\Stats\Query( $args );
* $mydata = $report->get_data(); * $mydata = $report->get_data();
* *
* @package WooCommerce Admin/Classes * @package WooCommerce Admin/Classes
*/ */
namespace Automattic\WooCommerce\Admin\API\Reports\Orders\Stats;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
use \Automattic\WooCommerce\Admin\API\Reports\Query as ReportsQuery;
/** /**
* WC_Admin_Reports_Orders_Stats_Query * API\Reports\Orders\Stats\Query
*/ */
class WC_Admin_Reports_Orders_Stats_Query extends WC_Admin_Reports_Query { class Query extends ReportsQuery {
/** /**
* Valid fields for Orders report. * Valid fields for Orders report.
@ -53,7 +57,7 @@ class WC_Admin_Reports_Orders_Stats_Query extends WC_Admin_Reports_Query {
public function get_data() { public function get_data() {
$args = apply_filters( 'woocommerce_reports_orders_stats_query_args', $this->get_query_vars() ); $args = apply_filters( 'woocommerce_reports_orders_stats_query_args', $this->get_query_vars() );
$data_store = WC_Data_Store::load( 'report-orders-stats' ); $data_store = \WC_Data_Store::load( 'report-orders-stats' );
$results = $data_store->get_data( $args ); $results = $data_store->get_data( $args );
return apply_filters( 'woocommerce_reports_orders_stats_select_query', $results, $args ); return apply_filters( 'woocommerce_reports_orders_stats_select_query', $results, $args );
} }

View File

@ -5,12 +5,17 @@
* @package WooCommerce Admin/Classes * @package WooCommerce Admin/Classes
*/ */
namespace Automattic\WooCommerce\Admin\API\Reports\Orders\Stats;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
use \Automattic\WooCommerce\Admin\API\Reports\Segmenter as ReportsSegmenter;
use \Automattic\WooCommerce\Admin\API\Reports\ParameterException;
/** /**
* Date & time interval and numeric range handling class for Reporting API. * Date & time interval and numeric range handling class for Reporting API.
*/ */
class WC_Admin_Reports_Orders_Stats_Segmenting extends WC_Admin_Reports_Segmenting { class Segmenter extends ReportsSegmenter {
/** /**
* Returns SELECT clause statements to be used for product-related product-level segmenting query (e.g. products sold, revenue from product X when segmenting by category). * Returns SELECT clause statements to be used for product-related product-level segmenting query (e.g. products sold, revenue from product X when segmenting by category).
@ -337,7 +342,7 @@ class WC_Admin_Reports_Orders_Stats_Segmenting extends WC_Admin_Reports_Segmenti
* @param string $table_name Name of main SQL table for the data store (used as basis for JOINS). * @param string $table_name Name of main SQL table for the data store (used as basis for JOINS).
* *
* @return array * @return array
* @throws WC_Admin_Reports_Parameter_Exception In case of segmenting by variations, when no parent product is specified. * @throws \Automattic\WooCommerce\Admin\API\Reports\ParameterException In case of segmenting by variations, when no parent product is specified.
*/ */
protected function get_segments( $type, $query_params, $table_name ) { protected function get_segments( $type, $query_params, $table_name ) {
global $wpdb; global $wpdb;
@ -366,7 +371,7 @@ class WC_Admin_Reports_Orders_Stats_Segmenting extends WC_Admin_Reports_Segmenti
$segments = $this->get_product_related_segments( $type, $segmenting_selections, $segmenting_from, $segmenting_where, $segmenting_groupby, $segmenting_dimension_name, $table_name, $query_params, $unique_orders_table ); $segments = $this->get_product_related_segments( $type, $segmenting_selections, $segmenting_from, $segmenting_where, $segmenting_groupby, $segmenting_dimension_name, $table_name, $query_params, $unique_orders_table );
} elseif ( 'variation' === $this->query_args['segmentby'] ) { } elseif ( 'variation' === $this->query_args['segmentby'] ) {
if ( ! isset( $this->query_args['product_includes'] ) || count( $this->query_args['product_includes'] ) !== 1 ) { if ( ! isset( $this->query_args['product_includes'] ) || count( $this->query_args['product_includes'] ) !== 1 ) {
throw new WC_Admin_Reports_Parameter_Exception( 'wc_admin_reports_invalid_segmenting_variation', __( 'product_includes parameter need to specify exactly one product when segmenting by variation.', 'woocommerce-admin' ) ); throw new ParameterException( 'wc_admin_reports_invalid_segmenting_variation', __( 'product_includes parameter need to specify exactly one product when segmenting by variation.', 'woocommerce-admin' ) );
} }
$segmenting_selections = array( $segmenting_selections = array(

View File

@ -7,9 +7,11 @@
* @package WooCommerce Admin * @package WooCommerce Admin
*/ */
namespace Automattic\WooCommerce\Admin\API\Reports;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
/** /**
* WC_Admin_Reports_Parameter_Exception class. * API\Reports\ParameterException class.
*/ */
class WC_Admin_Reports_Parameter_Exception extends WC_Data_Exception {} class ParameterException extends \WC_Data_Exception {}

View File

@ -7,6 +7,8 @@
* @package WooCommerce Admin/API * @package WooCommerce Admin/API
*/ */
namespace Automattic\WooCommerce\Admin\API\Reports\PerformanceIndicators;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
/** /**
@ -15,7 +17,7 @@ defined( 'ABSPATH' ) || exit;
* @package WooCommerce/API * @package WooCommerce/API
* @extends WC_REST_Reports_Controller * @extends WC_REST_Reports_Controller
*/ */
class WC_Admin_REST_Reports_Performance_Indicators_Controller extends WC_REST_Reports_Controller { class Controller extends \WC_REST_Reports_Controller {
/** /**
* Endpoint namespace. * Endpoint namespace.
@ -68,7 +70,7 @@ class WC_Admin_REST_Reports_Performance_Indicators_Controller extends WC_REST_Re
'/' . $this->rest_base, '/' . $this->rest_base,
array( array(
array( array(
'methods' => WP_REST_Server::READABLE, 'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_items' ), 'callback' => array( $this, 'get_items' ),
'permission_callback' => array( $this, 'get_items_permissions_check' ), 'permission_callback' => array( $this, 'get_items_permissions_check' ),
'args' => $this->get_collection_params(), 'args' => $this->get_collection_params(),
@ -82,7 +84,7 @@ class WC_Admin_REST_Reports_Performance_Indicators_Controller extends WC_REST_Re
'/' . $this->rest_base . '/allowed', '/' . $this->rest_base . '/allowed',
array( array(
array( array(
'methods' => WP_REST_Server::READABLE, 'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_allowed_items' ), 'callback' => array( $this, 'get_allowed_items' ),
'permission_callback' => array( $this, 'get_items_permissions_check' ), 'permission_callback' => array( $this, 'get_items_permissions_check' ),
'args' => $this->get_collection_params(), 'args' => $this->get_collection_params(),
@ -117,7 +119,7 @@ class WC_Admin_REST_Reports_Performance_Indicators_Controller extends WC_REST_Re
return true; return true;
} }
$request = new WP_REST_Request( 'GET', '/wc/v4/reports' ); $request = new \WP_REST_Request( 'GET', '/wc/v4/reports' );
$response = rest_do_request( $request ); $response = rest_do_request( $request );
if ( is_wp_error( $response ) ) { if ( is_wp_error( $response ) ) {
@ -125,7 +127,7 @@ class WC_Admin_REST_Reports_Performance_Indicators_Controller extends WC_REST_Re
} }
if ( 200 !== $response->get_status() ) { if ( 200 !== $response->get_status() ) {
return new WP_Error( 'woocommerce_reports_performance_indicators_result_failed', __( 'Sorry, fetching performance indicators failed.', 'woocommerce-admin' ) ); return new \WP_Error( 'woocommerce_reports_performance_indicators_result_failed', __( 'Sorry, fetching performance indicators failed.', 'woocommerce-admin' ) );
} }
$endpoints = $response->get_data(); $endpoints = $response->get_data();
@ -133,7 +135,7 @@ class WC_Admin_REST_Reports_Performance_Indicators_Controller extends WC_REST_Re
foreach ( $endpoints as $endpoint ) { foreach ( $endpoints as $endpoint ) {
if ( '/stats' === substr( $endpoint['slug'], -6 ) ) { if ( '/stats' === substr( $endpoint['slug'], -6 ) ) {
$request = new WP_REST_Request( 'OPTIONS', $endpoint['path'] ); $request = new \WP_REST_Request( 'OPTIONS', $endpoint['path'] );
$response = rest_do_request( $request ); $response = rest_do_request( $request );
if ( is_wp_error( $response ) ) { if ( is_wp_error( $response ) ) {
@ -272,7 +274,7 @@ class WC_Admin_REST_Reports_Performance_Indicators_Controller extends WC_REST_Re
$query_args = $this->prepare_reports_query( $request ); $query_args = $this->prepare_reports_query( $request );
if ( empty( $query_args['stats'] ) ) { if ( empty( $query_args['stats'] ) ) {
return new WP_Error( 'woocommerce_reports_performance_indicators_empty_query', __( 'A list of stats to query must be provided.', 'woocommerce-admin' ), 400 ); return new \WP_Error( 'woocommerce_reports_performance_indicators_empty_query', __( 'A list of stats to query must be provided.', 'woocommerce-admin' ), 400 );
} }
$stats = array(); $stats = array();
@ -288,7 +290,7 @@ class WC_Admin_REST_Reports_Performance_Indicators_Controller extends WC_REST_Re
} }
$request_url = $this->endpoints[ $report ]; $request_url = $this->endpoints[ $report ];
$request = new WP_REST_Request( 'GET', $request_url ); $request = new \WP_REST_Request( 'GET', $request_url );
$request->set_param( 'before', $query_args['before'] ); $request->set_param( 'before', $query_args['before'] );
$request->set_param( 'after', $query_args['after'] ); $request->set_param( 'after', $query_args['after'] );
@ -371,7 +373,7 @@ class WC_Admin_REST_Reports_Performance_Indicators_Controller extends WC_REST_Re
/** /**
* Prepare links for the request. * Prepare links for the request.
* *
* @param WC_Admin_Reports_Query $object Object data. * @param \Automattic\WooCommerce\Admin\API\Reports\Query $object Object data.
* @return array * @return array
*/ */
protected function prepare_links( $object ) { protected function prepare_links( $object ) {

View File

@ -7,6 +7,8 @@
* @package WooCommerce Admin/API * @package WooCommerce Admin/API
*/ */
namespace Automattic\WooCommerce\Admin\API\Reports\Products;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
/** /**
@ -15,7 +17,7 @@ defined( 'ABSPATH' ) || exit;
* @package WooCommerce/API * @package WooCommerce/API
* @extends WC_REST_Reports_Controller * @extends WC_REST_Reports_Controller
*/ */
class WC_Admin_REST_Reports_Products_Controller extends WC_REST_Reports_Controller { class Controller extends \WC_REST_Reports_Controller {
/** /**
* Endpoint namespace. * Endpoint namespace.
@ -60,7 +62,7 @@ class WC_Admin_REST_Reports_Products_Controller extends WC_REST_Reports_Controll
} }
} }
$reports = new WC_Admin_Reports_Products_Query( $args ); $reports = new Query( $args );
$products_data = $reports->get_data(); $products_data = $reports->get_data();
$data = array(); $data = array();

View File

@ -1,16 +1,22 @@
<?php <?php
/** /**
* WC_Admin_Reports_Products_Data_Store class file. * API\Reports\Products\DataStore class file.
* *
* @package WooCommerce Admin/Classes * @package WooCommerce Admin/Classes
*/ */
namespace Automattic\WooCommerce\Admin\API\Reports\Products;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
use \Automattic\WooCommerce\Admin\API\Reports\DataStore as ReportsDataStore;
use \Automattic\WooCommerce\Admin\API\Reports\DataStoreInterface;
use \Automattic\WooCommerce\Admin\API\Reports\TimeInterval;
/** /**
* WC_Admin_Reports_Products_Data_Store. * API\Reports\Products\DataStore.
*/ */
class WC_Admin_Reports_Products_Data_Store extends WC_Admin_Reports_Data_Store implements WC_Admin_Reports_Data_Store_Interface { class DataStore extends ReportsDataStore implements DataStoreInterface {
/** /**
* Table used to get the data. * Table used to get the data.
@ -187,7 +193,7 @@ class WC_Admin_Reports_Products_Data_Store extends WC_Admin_Reports_Data_Store i
$product_names = array(); $product_names = array();
foreach ( $products_data as $key => $product_data ) { foreach ( $products_data as $key => $product_data ) {
$extended_info = new ArrayObject(); $extended_info = new \ArrayObject();
if ( $query_args['extended_info'] ) { if ( $query_args['extended_info'] ) {
$product_id = $product_data['product_id']; $product_id = $product_data['product_id'];
$product = wc_get_product( $product_id ); $product = wc_get_product( $product_id );
@ -255,8 +261,8 @@ class WC_Admin_Reports_Products_Data_Store extends WC_Admin_Reports_Data_Store i
'page' => 1, 'page' => 1,
'order' => 'DESC', 'order' => 'DESC',
'orderby' => 'date', 'orderby' => 'date',
'before' => WC_Admin_Reports_Interval::default_before(), 'before' => TimeInterval::default_before(),
'after' => WC_Admin_Reports_Interval::default_after(), 'after' => TimeInterval::default_after(),
'fields' => '*', 'fields' => '*',
'categories' => array(), 'categories' => array(),
'product_includes' => array(), 'product_includes' => array(),
@ -429,7 +435,7 @@ class WC_Admin_Reports_Products_Data_Store extends WC_Admin_Reports_Data_Store i
'customer_id' => $order->get_report_customer_id(), 'customer_id' => $order->get_report_customer_id(),
'product_qty' => $product_qty, 'product_qty' => $product_qty,
'product_net_revenue' => $net_revenue, 'product_net_revenue' => $net_revenue,
'date_created' => $order->get_date_created( 'edit' )->date( WC_Admin_Reports_Interval::$sql_datetime_format ), 'date_created' => $order->get_date_created( 'edit' )->date( TimeInterval::$sql_datetime_format ),
'coupon_amount' => $coupon_amount, 'coupon_amount' => $coupon_amount,
'tax_amount' => $tax_amount, 'tax_amount' => $tax_amount,
'shipping_amount' => $shipping_amount, 'shipping_amount' => $shipping_amount,

View File

@ -10,18 +10,22 @@
* 'categories' => array(15, 18), * 'categories' => array(15, 18),
* 'products' => array(1,2,3) * 'products' => array(1,2,3)
* ); * );
* $report = new WC_Admin_Reports_Products_Query( $args ); * $report = new \Automattic\WooCommerce\Admin\API\Reports\Products\Query( $args );
* $mydata = $report->get_data(); * $mydata = $report->get_data();
* *
* @package WooCommerce Admin/Classes * @package WooCommerce Admin/Classes
*/ */
namespace Automattic\WooCommerce\Admin\API\Reports\Products;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
use \Automattic\WooCommerce\Admin\API\Reports\Query as ReportsQuery;
/** /**
* WC_Admin_Reports_Products_Query * API\Reports\Products\Query
*/ */
class WC_Admin_Reports_Products_Query extends WC_Admin_Reports_Query { class Query extends ReportsQuery {
/** /**
* Valid fields for Products report. * Valid fields for Products report.
@ -40,7 +44,7 @@ class WC_Admin_Reports_Products_Query extends WC_Admin_Reports_Query {
public function get_data() { public function get_data() {
$args = apply_filters( 'woocommerce_reports_products_query_args', $this->get_query_vars() ); $args = apply_filters( 'woocommerce_reports_products_query_args', $this->get_query_vars() );
$data_store = WC_Data_Store::load( 'report-products' ); $data_store = \WC_Data_Store::load( 'report-products' );
$results = $data_store->get_data( $args ); $results = $data_store->get_data( $args );
return apply_filters( 'woocommerce_reports_products_select_query', $results, $args ); return apply_filters( 'woocommerce_reports_products_select_query', $results, $args );
} }

View File

@ -7,15 +7,19 @@
* @package WooCommerce Admin/API * @package WooCommerce Admin/API
*/ */
namespace Automattic\WooCommerce\Admin\API\Reports\Products\Stats;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
use \Automattic\WooCommerce\Admin\API\Reports\ParameterException;
/** /**
* REST API Reports products stats controller class. * REST API Reports products stats controller class.
* *
* @package WooCommerce/API * @package WooCommerce/API
* @extends WC_REST_Reports_Controller * @extends WC_REST_Reports_Controller
*/ */
class WC_Admin_REST_Reports_Products_Stats_Controller extends WC_REST_Reports_Controller { class Controller extends \WC_REST_Reports_Controller {
/** /**
* Endpoint namespace. * Endpoint namespace.
@ -75,11 +79,11 @@ class WC_Admin_REST_Reports_Products_Stats_Controller extends WC_REST_Reports_Co
} }
} }
$query = new WC_Admin_Reports_Products_Stats_Query( $query_args ); $query = new Query( $query_args );
try { try {
$report_data = $query->get_data(); $report_data = $query->get_data();
} catch ( WC_Admin_Reports_Parameter_Exception $e ) { } catch ( ParameterException $e ) {
return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); return new \WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
} }
$out_data = array( $out_data = array(
@ -285,9 +289,9 @@ class WC_Admin_REST_Reports_Products_Stats_Controller extends WC_REST_Reports_Co
*/ */
public function set_default_report_data( $results ) { public function set_default_report_data( $results ) {
if ( empty( $results ) ) { if ( empty( $results ) ) {
$results = new stdClass(); $results = new \stdClass();
$results->total = 0; $results->total = 0;
$results->totals = new stdClass(); $results->totals = new \stdClass();
$results->totals->items_sold = 0; $results->totals->items_sold = 0;
$results->totals->net_revenue = 0; $results->totals->net_revenue = 0;
$results->totals->orders_count = 0; $results->totals->orders_count = 0;

View File

@ -1,17 +1,22 @@
<?php <?php
/** /**
* WC_Admin_Reports_Products_Stats_Data_Store class file. * API\Reports\Products\Stats\DataStore class file.
* *
* @package WooCommerce Admin/Classes * @package WooCommerce Admin/Classes
*/ */
namespace Automattic\WooCommerce\Admin\API\Reports\Products\Stats;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
use \Automattic\WooCommerce\Admin\API\Reports\Products\DataStore as ProductsDataStore;
use \Automattic\WooCommerce\Admin\API\Reports\DataStoreInterface;
use \Automattic\WooCommerce\Admin\API\Reports\TimeInterval;
/** /**
* WC_Reports_Products_Stats_Data_Store. * API\Reports\Products\Stats\DataStore.
*/ */
class WC_Admin_Reports_Products_Stats_Data_Store extends WC_Admin_Reports_Products_Data_Store implements WC_Admin_Reports_Data_Store_Interface { class DataStore extends ProductsDataStore implements DataStoreInterface {
/** /**
* Mapping columns to data type to return correct response types. * Mapping columns to data type to return correct response types.
@ -110,8 +115,8 @@ class WC_Admin_Reports_Products_Stats_Data_Store extends WC_Admin_Reports_Produc
'page' => 1, 'page' => 1,
'order' => 'DESC', 'order' => 'DESC',
'orderby' => 'date', 'orderby' => 'date',
'before' => WC_Admin_Reports_Interval::default_before(), 'before' => TimeInterval::default_before(),
'after' => WC_Admin_Reports_Interval::default_after(), 'after' => TimeInterval::default_after(),
'fields' => '*', 'fields' => '*',
'categories' => array(), 'categories' => array(),
'interval' => 'week', 'interval' => 'week',
@ -144,7 +149,7 @@ class WC_Admin_Reports_Products_Stats_Data_Store extends WC_Admin_Reports_Produc
); // WPCS: cache ok, DB call ok, , unprepared SQL ok. ); // WPCS: cache ok, DB call ok, , unprepared SQL ok.
$db_interval_count = count( $db_intervals ); $db_interval_count = count( $db_intervals );
$expected_interval_count = WC_Admin_Reports_Interval::intervals_between( $query_args['after'], $query_args['before'], $query_args['interval'] ); $expected_interval_count = TimeInterval::intervals_between( $query_args['after'], $query_args['before'], $query_args['interval'] );
$total_pages = (int) ceil( $expected_interval_count / $intervals_query['per_page'] ); $total_pages = (int) ceil( $expected_interval_count / $intervals_query['per_page'] );
if ( $query_args['page'] < 1 || $query_args['page'] > $total_pages ) { if ( $query_args['page'] < 1 || $query_args['page'] > $total_pages ) {
return array(); return array();
@ -165,11 +170,11 @@ class WC_Admin_Reports_Products_Stats_Data_Store extends WC_Admin_Reports_Produc
ARRAY_A ARRAY_A
); // WPCS: cache ok, DB call ok, unprepared SQL ok. ); // WPCS: cache ok, DB call ok, unprepared SQL ok.
$segmenter = new WC_Admin_Reports_Products_Stats_Segmenting( $query_args, $this->report_columns ); $segmenter = new Segmenter( $query_args, $this->report_columns );
$totals[0]['segments'] = $segmenter->get_totals_segments( $totals_query, $table_name ); $totals[0]['segments'] = $segmenter->get_totals_segments( $totals_query, $table_name );
if ( null === $totals ) { if ( null === $totals ) {
return new WP_Error( 'woocommerce_reports_products_stats_result_failed', __( 'Sorry, fetching revenue data failed.', 'woocommerce-admin' ) ); return new \WP_Error( 'woocommerce_reports_products_stats_result_failed', __( 'Sorry, fetching revenue data failed.', 'woocommerce-admin' ) );
} }
if ( '' !== $selections ) { if ( '' !== $selections ) {
@ -197,7 +202,7 @@ class WC_Admin_Reports_Products_Stats_Data_Store extends WC_Admin_Reports_Produc
); // WPCS: cache ok, DB call ok, unprepared SQL ok. ); // WPCS: cache ok, DB call ok, unprepared SQL ok.
if ( null === $intervals ) { if ( null === $intervals ) {
return new WP_Error( 'woocommerce_reports_products_stats_result_failed', __( 'Sorry, fetching revenue data failed.', 'woocommerce-admin' ) ); return new \WP_Error( 'woocommerce_reports_products_stats_result_failed', __( 'Sorry, fetching revenue data failed.', 'woocommerce-admin' ) );
} }
$totals = (object) $this->cast_numbers( $totals[0] ); $totals = (object) $this->cast_numbers( $totals[0] );
@ -210,7 +215,7 @@ class WC_Admin_Reports_Products_Stats_Data_Store extends WC_Admin_Reports_Produc
'page_no' => (int) $query_args['page'], 'page_no' => (int) $query_args['page'],
); );
if ( WC_Admin_Reports_Interval::intervals_missing( $expected_interval_count, $db_interval_count, $intervals_query['per_page'], $query_args['page'], $query_args['order'], $query_args['orderby'], count( $intervals ) ) ) { if ( TimeInterval::intervals_missing( $expected_interval_count, $db_interval_count, $intervals_query['per_page'], $query_args['page'], $query_args['order'], $query_args['orderby'], count( $intervals ) ) ) {
$this->fill_in_missing_intervals( $db_intervals, $query_args['adj_after'], $query_args['adj_before'], $query_args['interval'], $data ); $this->fill_in_missing_intervals( $db_intervals, $query_args['adj_after'], $query_args['adj_before'], $query_args['interval'], $data );
$this->sort_intervals( $data, $query_args['orderby'], $query_args['order'] ); $this->sort_intervals( $data, $query_args['orderby'], $query_args['order'] );
$this->remove_extra_records( $data, $query_args['page'], $intervals_query['per_page'], $db_interval_count, $expected_interval_count, $query_args['orderby'], $query_args['order'] ); $this->remove_extra_records( $data, $query_args['page'], $intervals_query['per_page'], $db_interval_count, $expected_interval_count, $query_args['orderby'], $query_args['order'] );

View File

@ -10,18 +10,22 @@
* 'categories' => array(15, 18), * 'categories' => array(15, 18),
* 'product_ids' => array(1,2,3) * 'product_ids' => array(1,2,3)
* ); * );
* $report = new WC_Admin_Reports_Products_Stats_Query( $args ); * $report = new \Automattic\WooCommerce\Admin\API\Reports\Products\Stats\Query( $args );
* $mydata = $report->get_data(); * $mydata = $report->get_data();
* *
* @package WooCommerce Admin/Classes * @package WooCommerce Admin/Classes
*/ */
namespace Automattic\WooCommerce\Admin\API\Reports\Products\Stats;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
use \Automattic\WooCommerce\Admin\API\Reports\Query as ReportsQuery;
/** /**
* WC_Admin_Reports_Products_Query * API\Reports\Products\Stats\Query
*/ */
class WC_Admin_Reports_Products_Stats_Query extends WC_Admin_Reports_Query { class Query extends ReportsQuery {
/** /**
* Valid fields for Products report. * Valid fields for Products report.
@ -40,7 +44,7 @@ class WC_Admin_Reports_Products_Stats_Query extends WC_Admin_Reports_Query {
public function get_data() { public function get_data() {
$args = apply_filters( 'woocommerce_reports_products_stats_query_args', $this->get_query_vars() ); $args = apply_filters( 'woocommerce_reports_products_stats_query_args', $this->get_query_vars() );
$data_store = WC_Data_Store::load( 'report-products-stats' ); $data_store = \WC_Data_Store::load( 'report-products-stats' );
$results = $data_store->get_data( $args ); $results = $data_store->get_data( $args );
return apply_filters( 'woocommerce_reports_products_stats_select_query', $results, $args ); return apply_filters( 'woocommerce_reports_products_stats_select_query', $results, $args );
} }

View File

@ -5,12 +5,17 @@
* @package WooCommerce Admin/Classes * @package WooCommerce Admin/Classes
*/ */
namespace Automattic\WooCommerce\Admin\API\Reports\Products\Stats;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
use \Automattic\WooCommerce\Admin\API\Reports\Segmenter as ReportsSegmenter;
use \Automattic\WooCommerce\Admin\API\Reports\ParameterException;
/** /**
* Date & time interval and numeric range handling class for Reporting API. * Date & time interval and numeric range handling class for Reporting API.
*/ */
class WC_Admin_Reports_Products_Stats_Segmenting extends WC_Admin_Reports_Segmenting { class Segmenter extends ReportsSegmenter {
/** /**
* Returns SELECT clause statements to be used for product-related product-level segmenting query (e.g. products sold, revenue from product X when segmenting by category). * Returns SELECT clause statements to be used for product-related product-level segmenting query (e.g. products sold, revenue from product X when segmenting by category).
@ -132,7 +137,7 @@ class WC_Admin_Reports_Products_Stats_Segmenting extends WC_Admin_Reports_Segmen
* @param string $table_name Name of main SQL table for the data store (used as basis for JOINS). * @param string $table_name Name of main SQL table for the data store (used as basis for JOINS).
* *
* @return array * @return array
* @throws WC_Admin_Reports_Parameter_Exception In case of segmenting by variations, when no parent product is specified. * @throws \Automattic\WooCommerce\Admin\API\Reports\ParameterException In case of segmenting by variations, when no parent product is specified.
*/ */
protected function get_segments( $type, $query_params, $table_name ) { protected function get_segments( $type, $query_params, $table_name ) {
global $wpdb; global $wpdb;
@ -158,7 +163,7 @@ class WC_Admin_Reports_Products_Stats_Segmenting extends WC_Admin_Reports_Segmen
$segments = $this->get_product_related_segments( $type, $segmenting_selections, $segmenting_from, $segmenting_where, $segmenting_groupby, $segmenting_dimension_name, $table_name, $query_params, $unique_orders_table ); $segments = $this->get_product_related_segments( $type, $segmenting_selections, $segmenting_from, $segmenting_where, $segmenting_groupby, $segmenting_dimension_name, $table_name, $query_params, $unique_orders_table );
} elseif ( 'variation' === $this->query_args['segmentby'] ) { } elseif ( 'variation' === $this->query_args['segmentby'] ) {
if ( ! isset( $this->query_args['product_includes'] ) || count( $this->query_args['product_includes'] ) !== 1 ) { if ( ! isset( $this->query_args['product_includes'] ) || count( $this->query_args['product_includes'] ) !== 1 ) {
throw new WC_Admin_Reports_Parameter_Exception( 'wc_admin_reports_invalid_segmenting_variation', __( 'product_includes parameter need to specify exactly one product when segmenting by variation.', 'woocommerce-admin' ) ); throw new ParameterException( 'wc_admin_reports_invalid_segmenting_variation', __( 'product_includes parameter need to specify exactly one product when segmenting by variation.', 'woocommerce-admin' ) );
} }
$segmenting_selections = array( $segmenting_selections = array(

View File

@ -5,12 +5,14 @@
* @package WooCommerce Admin/Classes * @package WooCommerce Admin/Classes
*/ */
namespace Automattic\WooCommerce\Admin\API\Reports;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
/** /**
* WC_Admin_Reports_Query * Admin\API\Reports\Query
*/ */
abstract class WC_Admin_Reports_Query extends WC_Object_Query { abstract class Query extends \WC_Object_Query {
/** /**
* Get report data matching the current query vars. * Get report data matching the current query vars.
@ -19,7 +21,7 @@ abstract class WC_Admin_Reports_Query extends WC_Object_Query {
*/ */
public function get_data() { public function get_data() {
/* translators: %s: Method name */ /* translators: %s: Method name */
return new WP_Error( 'invalid-method', sprintf( __( "Method '%s' not implemented. Must be overridden in subclass.", 'woocommerce-admin' ), __METHOD__ ), array( 'status' => 405 ) ); return new \WP_Error( 'invalid-method', sprintf( __( "Method '%s' not implemented. Must be overridden in subclass.", 'woocommerce-admin' ), __METHOD__ ), array( 'status' => 405 ) );
} }
} }

View File

@ -8,18 +8,22 @@
* 'after' => '2018-07-05 00:00:00', * 'after' => '2018-07-05 00:00:00',
* 'interval' => 'week', * 'interval' => 'week',
* ); * );
* $report = new WC_Admin_Reports_Revenue_Query( $args ); * $report = new \Automattic\WooCommerce\Admin\API\Reports\Revenue\Query( $args );
* $mydata = $report->get_data(); * $mydata = $report->get_data();
* *
* @package WooCommerce Admin/Classes * @package WooCommerce Admin/Classes
*/ */
namespace Automattic\WooCommerce\Admin\API\Reports\Revenue;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
use \Automattic\WooCommerce\Admin\API\Reports\Query as ReportsQuery;
/** /**
* WC_Admin_Reports_Revenue_Query * API\Reports\Revenue\Query
*/ */
class WC_Admin_Reports_Revenue_Query extends WC_Admin_Reports_Query { class Query extends ReportsQuery {
/** /**
* Valid fields for Revenue report. * Valid fields for Revenue report.
@ -57,7 +61,7 @@ class WC_Admin_Reports_Revenue_Query extends WC_Admin_Reports_Query {
public function get_data() { public function get_data() {
$args = apply_filters( 'woocommerce_reports_revenue_query_args', $this->get_query_vars() ); $args = apply_filters( 'woocommerce_reports_revenue_query_args', $this->get_query_vars() );
$data_store = WC_Data_Store::load( 'report-revenue-stats' ); $data_store = \WC_Data_Store::load( 'report-revenue-stats' );
$results = $data_store->get_data( $args ); $results = $data_store->get_data( $args );
return apply_filters( 'woocommerce_reports_revenue_select_query', $results, $args ); return apply_filters( 'woocommerce_reports_revenue_select_query', $results, $args );
} }

View File

@ -7,15 +7,20 @@
* @package WooCommerce Admin/API * @package WooCommerce Admin/API
*/ */
namespace Automattic\WooCommerce\Admin\API\Reports\Revenue\Stats;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
use \Automattic\WooCommerce\Admin\API\Reports\Revenue\Query as RevenueQuery;
use \Automattic\WooCommerce\Admin\API\Reports\ParameterException;
/** /**
* REST API Reports revenue stats controller class. * REST API Reports revenue stats controller class.
* *
* @package WooCommerce/API * @package WooCommerce/API
* @extends WC_REST_Reports_Controller * @extends WC_REST_Reports_Controller
*/ */
class WC_Admin_REST_Reports_Revenue_Stats_Controller extends WC_REST_Reports_Controller { class Controller extends \WC_REST_Reports_Controller {
/** /**
* Endpoint namespace. * Endpoint namespace.
@ -59,11 +64,11 @@ class WC_Admin_REST_Reports_Revenue_Stats_Controller extends WC_REST_Reports_Con
*/ */
public function get_items( $request ) { public function get_items( $request ) {
$query_args = $this->prepare_reports_query( $request ); $query_args = $this->prepare_reports_query( $request );
$reports_revenue = new WC_Admin_Reports_Revenue_Query( $query_args ); $reports_revenue = new RevenueQuery( $query_args );
try { try {
$report_data = $reports_revenue->get_data(); $report_data = $reports_revenue->get_data();
} catch ( WC_Admin_Reports_Parameter_Exception $e ) { } catch ( ParameterException $e ) {
return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); return new \WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
} }
$out_data = array( $out_data = array(

View File

@ -5,12 +5,17 @@
* @package WooCommerce Admin/Classes * @package WooCommerce Admin/Classes
*/ */
namespace Automattic\WooCommerce\Admin\API\Reports;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
use \Automattic\WooCommerce\Admin\API\Reports\Coupons\DataStore as CouponsDataStore;
use \Automattic\WooCommerce\Admin\API\Reports\Taxes\Stats\DataStore as TaxesStatsDataStore;
/** /**
* Date & time interval and numeric range handling class for Reporting API. * Date & time interval and numeric range handling class for Reporting API.
*/ */
class WC_Admin_Reports_Segmenting { class Segmenter {
/** /**
* Array of all segment ids. * Array of all segment ids.
@ -41,7 +46,7 @@ class WC_Admin_Reports_Segmenting {
protected $report_columns = array(); protected $report_columns = array();
/** /**
* WC_Admin_Reports_Segmenting constructor. * Constructor.
* *
* @param array $query_args Query arguments supplied by the user for data store. * @param array $query_args Query arguments supplied by the user for data store.
* @param array $report_columns Report columns lookup from data store. * @param array $report_columns Report columns lookup from data store.
@ -375,7 +380,7 @@ class WC_Admin_Reports_Segmenting {
if ( isset( $this->query_args['coupons'] ) ) { if ( isset( $this->query_args['coupons'] ) ) {
$args['include'] = $this->query_args['coupons']; $args['include'] = $this->query_args['coupons'];
} }
$coupons = WC_Admin_Reports_Coupons_Data_Store::get_coupons( $args ); $coupons = CouponsDataStore::get_coupons( $args );
$segments = wp_list_pluck( $coupons, 'ID' ); $segments = wp_list_pluck( $coupons, 'ID' );
$segment_labels = wp_list_pluck( $coupons, 'post_title', 'ID' ); $segment_labels = wp_list_pluck( $coupons, 'post_title', 'ID' );
$segment_labels = array_map( 'wc_format_coupon_code', $segment_labels ); $segment_labels = array_map( 'wc_format_coupon_code', $segment_labels );
@ -388,12 +393,12 @@ class WC_Admin_Reports_Segmenting {
if ( isset( $this->query_args['taxes'] ) ) { if ( isset( $this->query_args['taxes'] ) ) {
$args['include'] = $this->query_args['taxes']; $args['include'] = $this->query_args['taxes'];
} }
$taxes = WC_Admin_Reports_Taxes_Stats_Data_Store::get_taxes( $args ); $taxes = TaxesStatsDataStore::get_taxes( $args );
foreach ( $taxes as $tax ) { foreach ( $taxes as $tax ) {
$id = $tax['tax_rate_id']; $id = $tax['tax_rate_id'];
$segments[] = $id; $segments[] = $id;
$segment_labels[ $id ] = WC_Tax::get_rate_code( (object) $tax ); $segment_labels[ $id ] = \WC_Tax::get_rate_code( (object) $tax );
} }
} else { } else {
// Catch all default. // Catch all default.

View File

@ -7,6 +7,8 @@
* @package WooCommerce Admin/API * @package WooCommerce Admin/API
*/ */
namespace Automattic\WooCommerce\Admin\API\Reports\Stock;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
/** /**
@ -15,7 +17,7 @@ defined( 'ABSPATH' ) || exit;
* @package WooCommerce/API * @package WooCommerce/API
* @extends WC_REST_Reports_Controller * @extends WC_REST_Reports_Controller
*/ */
class WC_Admin_REST_Reports_Stock_Controller extends WC_REST_Reports_Controller { class Controller extends \WC_REST_Reports_Controller {
/** /**
* Endpoint namespace. * Endpoint namespace.
@ -77,14 +79,14 @@ class WC_Admin_REST_Reports_Stock_Controller extends WC_REST_Reports_Controller
* @return array * @return array
*/ */
protected function get_products( $query_args ) { protected function get_products( $query_args ) {
$query = new WP_Query(); $query = new \WP_Query();
$result = $query->query( $query_args ); $result = $query->query( $query_args );
$total_posts = $query->found_posts; $total_posts = $query->found_posts;
if ( $total_posts < 1 ) { if ( $total_posts < 1 ) {
// Out-of-bounds, run the query again without LIMIT for total count. // Out-of-bounds, run the query again without LIMIT for total count.
unset( $query_args['paged'] ); unset( $query_args['paged'] );
$count_query = new WP_Query(); $count_query = new \WP_Query();
$count_query->query( $query_args ); $count_query->query( $query_args );
$total_posts = $count_query->found_posts; $total_posts = $count_query->found_posts;
} }

View File

@ -7,6 +7,8 @@
* @package WooCommerce Admin/API * @package WooCommerce Admin/API
*/ */
namespace Automattic\WooCommerce\Admin\API\Reports\Stock\Stats;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
/** /**
@ -15,7 +17,7 @@ defined( 'ABSPATH' ) || exit;
* @package WooCommerce/API * @package WooCommerce/API
* @extends WC_REST_Reports_Controller * @extends WC_REST_Reports_Controller
*/ */
class WC_Admin_REST_Reports_Stock_Stats_Controller extends WC_REST_Reports_Controller { class Controller extends \WC_REST_Reports_Controller {
/** /**
* Endpoint namespace. * Endpoint namespace.
@ -38,7 +40,7 @@ class WC_Admin_REST_Reports_Stock_Stats_Controller extends WC_REST_Reports_Contr
* @return array|WP_Error * @return array|WP_Error
*/ */
public function get_items( $request ) { public function get_items( $request ) {
$stock_query = new WC_Admin_Reports_Stock_Stats_Query(); $stock_query = new Query();
$report_data = $stock_query->get_data(); $report_data = $stock_query->get_data();
$out_data = array( $out_data = array(
'totals' => $report_data, 'totals' => $report_data,

View File

@ -1,16 +1,21 @@
<?php <?php
/** /**
* WC_Admin_Reports_Stock_Stats_Data_Store class file. * API\Reports\Stock\Stats\DataStore class file.
* *
* @package WooCommerce Admin/Classes * @package WooCommerce Admin/Classes
*/ */
namespace Automattic\WooCommerce\Admin\API\Reports\Stock\Stats;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
use \Automattic\WooCommerce\Admin\API\Reports\DataStore as ReportsDataStore;
use \Automattic\WooCommerce\Admin\API\Reports\DataStoreInterface;
/** /**
* WC_Reports_Stock_Stats_Data_Store. * API\Reports\Stock\Stats\DataStore.
*/ */
class WC_Admin_Reports_Stock_Stats_Data_Store extends WC_Admin_Reports_Data_Store implements WC_Admin_Reports_Data_Store_Interface { class DataStore extends ReportsDataStore implements DataStoreInterface {
/** /**
* Get stock counts for the whole store. * Get stock counts for the whole store.
@ -122,7 +127,7 @@ class WC_Admin_Reports_Stock_Stats_Data_Store extends WC_Admin_Reports_Data_Stor
private function get_product_count() { private function get_product_count() {
$query_args = array(); $query_args = array();
$query_args['post_type'] = array( 'product', 'product_variation' ); $query_args['post_type'] = array( 'product', 'product_variation' );
$query = new WP_Query(); $query = new \WP_Query();
$query->query( $query_args ); $query->query( $query_args );
return intval( $query->found_posts ); return intval( $query->found_posts );
} }

View File

@ -2,18 +2,22 @@
/** /**
* Class for stock stats report querying * Class for stock stats report querying
* *
* $report = new WC_Admin_Reports_Stock__Stats_Query(); * $report = new \Automattic\WooCommerce\Admin\API\Reports\Stock\Stats\Query();
* $mydata = $report->get_data(); * $mydata = $report->get_data();
* *
* @package WooCommerce Admin/Classes * @package WooCommerce Admin/Classes
*/ */
namespace Automattic\WooCommerce\Admin\API\Reports\Stock\Stats;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
use \Automattic\WooCommerce\Admin\API\Reports\Query as ReportsQuery;
/** /**
* WC_Admin_Reports_Stock_Stats_Query * API\Reports\Stock\Stats\Query
*/ */
class WC_Admin_Reports_Stock_Stats_Query extends WC_Admin_Reports_Query { class Query extends ReportsQuery {
/** /**
* Get product data based on the current query vars. * Get product data based on the current query vars.
@ -21,7 +25,7 @@ class WC_Admin_Reports_Stock_Stats_Query extends WC_Admin_Reports_Query {
* @return array * @return array
*/ */
public function get_data() { public function get_data() {
$data_store = WC_Data_Store::load( 'report-stock-stats' ); $data_store = \WC_Data_Store::load( 'report-stock-stats' );
$results = $data_store->get_data(); $results = $data_store->get_data();
return apply_filters( 'woocommerce_reports_stock_stats_query', $results ); return apply_filters( 'woocommerce_reports_stock_stats_query', $results );
} }

View File

@ -7,6 +7,8 @@
* @package WooCommerce Admin/API * @package WooCommerce Admin/API
*/ */
namespace Automattic\WooCommerce\Admin\API\Reports\Taxes;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
/** /**
@ -15,7 +17,7 @@ defined( 'ABSPATH' ) || exit;
* @package WooCommerce/API * @package WooCommerce/API
* @extends WC_REST_Reports_Controller * @extends WC_REST_Reports_Controller
*/ */
class WC_Admin_REST_Reports_Taxes_Controller extends WC_REST_Reports_Controller { class Controller extends \WC_REST_Reports_Controller {
/** /**
* Endpoint namespace. * Endpoint namespace.
@ -58,7 +60,7 @@ class WC_Admin_REST_Reports_Taxes_Controller extends WC_REST_Reports_Controller
*/ */
public function get_items( $request ) { public function get_items( $request ) {
$query_args = $this->prepare_reports_query( $request ); $query_args = $this->prepare_reports_query( $request );
$taxes_query = new WC_Admin_Reports_Taxes_Query( $query_args ); $taxes_query = new Query( $query_args );
$report_data = $taxes_query->get_data(); $report_data = $taxes_query->get_data();
$data = array(); $data = array();

View File

@ -1,16 +1,22 @@
<?php <?php
/** /**
* WC_Admin_Reports_Taxes_Data_Store class file. * API\Reports\Taxes\DataStore class file.
* *
* @package WooCommerce Admin/Classes * @package WooCommerce Admin/Classes
*/ */
namespace Automattic\WooCommerce\Admin\API\Reports\Taxes;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
use \Automattic\WooCommerce\Admin\API\Reports\DataStore as ReportsDataStore;
use \Automattic\WooCommerce\Admin\API\Reports\DataStoreInterface;
use \Automattic\WooCommerce\Admin\API\Reports\TimeInterval;
/** /**
* WC_Admin_Reports_Taxes_Data_Store. * API\Reports\Taxes\DataStore.
*/ */
class WC_Admin_Reports_Taxes_Data_Store extends WC_Admin_Reports_Data_Store implements WC_Admin_Reports_Data_Store_Interface { class DataStore extends ReportsDataStore implements DataStoreInterface {
/** /**
* Table used to get the data. * Table used to get the data.
@ -167,8 +173,8 @@ class WC_Admin_Reports_Taxes_Data_Store extends WC_Admin_Reports_Data_Store impl
'page' => 1, 'page' => 1,
'order' => 'DESC', 'order' => 'DESC',
'orderby' => 'tax_rate_id', 'orderby' => 'tax_rate_id',
'before' => WC_Admin_Reports_Interval::default_before(), 'before' => TimeInterval::default_before(),
'after' => WC_Admin_Reports_Interval::default_after(), 'after' => TimeInterval::default_after(),
'fields' => '*', 'fields' => '*',
'taxes' => array(), 'taxes' => array(),
); );
@ -325,7 +331,7 @@ class WC_Admin_Reports_Taxes_Data_Store extends WC_Admin_Reports_Data_Store impl
$wpdb->prefix . self::TABLE_NAME, $wpdb->prefix . self::TABLE_NAME,
array( array(
'order_id' => $order->get_id(), 'order_id' => $order->get_id(),
'date_created' => $order->get_date_created( 'edit' )->date( WC_Admin_Reports_Interval::$sql_datetime_format ), 'date_created' => $order->get_date_created( 'edit' )->date( TimeInterval::$sql_datetime_format ),
'tax_rate_id' => $tax_item->get_rate_id(), 'tax_rate_id' => $tax_item->get_rate_id(),
'shipping_tax' => $tax_item->get_shipping_tax_total(), 'shipping_tax' => $tax_item->get_shipping_tax_total(),
'order_tax' => $tax_item->get_tax_total(), 'order_tax' => $tax_item->get_tax_total(),

View File

@ -9,18 +9,22 @@
* 'page' => 2, * 'page' => 2,
* 'taxes' => array(1,2,3) * 'taxes' => array(1,2,3)
* ); * );
* $report = new WC_Admin_Reports_Taxes_Query( $args ); * $report = new \Automattic\WooCommerce\Admin\API\Reports\Taxes\Query( $args );
* $mydata = $report->get_data(); * $mydata = $report->get_data();
* *
* @package WooCommerce Admin/Classes * @package WooCommerce Admin/Classes
*/ */
namespace Automattic\WooCommerce\Admin\API\Reports\Taxes;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
use \Automattic\WooCommerce\Admin\API\Reports\Query as ReportsQuery;
/** /**
* WC_Admin_Reports_Taxes_Query * API\Reports\Taxes\Query
*/ */
class WC_Admin_Reports_Taxes_Query extends WC_Admin_Reports_Query { class Query extends ReportsQuery {
/** /**
* Valid fields for Taxes report. * Valid fields for Taxes report.
@ -39,7 +43,7 @@ class WC_Admin_Reports_Taxes_Query extends WC_Admin_Reports_Query {
public function get_data() { public function get_data() {
$args = apply_filters( 'woocommerce_reports_taxes_query_args', $this->get_query_vars() ); $args = apply_filters( 'woocommerce_reports_taxes_query_args', $this->get_query_vars() );
$data_store = WC_Data_Store::load( 'report-taxes' ); $data_store = \WC_Data_Store::load( 'report-taxes' );
$results = $data_store->get_data( $args ); $results = $data_store->get_data( $args );
return apply_filters( 'woocommerce_reports_taxes_select_query', $results, $args ); return apply_filters( 'woocommerce_reports_taxes_select_query', $results, $args );
} }

View File

@ -7,6 +7,8 @@
* @package WooCommerce Admin/API * @package WooCommerce Admin/API
*/ */
namespace Automattic\WooCommerce\Admin\API\Reports\Taxes\Stats;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
/** /**
@ -15,7 +17,7 @@ defined( 'ABSPATH' ) || exit;
* @package WooCommerce/API * @package WooCommerce/API
* @extends WC_REST_Reports_Controller * @extends WC_REST_Reports_Controller
*/ */
class WC_Admin_REST_Reports_Taxes_Stats_Controller extends WC_REST_Reports_Controller { class Controller extends \WC_REST_Reports_Controller {
/** /**
* Endpoint namespace. * Endpoint namespace.
@ -46,9 +48,9 @@ class WC_Admin_REST_Reports_Taxes_Stats_Controller extends WC_REST_Reports_Contr
*/ */
public function set_default_report_data( $results ) { public function set_default_report_data( $results ) {
if ( empty( $results ) ) { if ( empty( $results ) ) {
$results = new stdClass(); $results = new \stdClass();
$results->total = 0; $results->total = 0;
$results->totals = new stdClass(); $results->totals = new \stdClass();
$results->totals->tax_codes = 0; $results->totals->tax_codes = 0;
$results->totals->total_tax = 0; $results->totals->total_tax = 0;
$results->totals->order_tax = 0; $results->totals->order_tax = 0;
@ -90,7 +92,7 @@ class WC_Admin_REST_Reports_Taxes_Stats_Controller extends WC_REST_Reports_Contr
*/ */
public function get_items( $request ) { public function get_items( $request ) {
$query_args = $this->prepare_reports_query( $request ); $query_args = $this->prepare_reports_query( $request );
$taxes_query = new WC_Admin_Reports_Taxes_Stats_Query( $query_args ); $taxes_query = new Query( $query_args );
$report_data = $taxes_query->get_data(); $report_data = $taxes_query->get_data();
$out_data = array( $out_data = array(

View File

@ -1,17 +1,22 @@
<?php <?php
/** /**
* WC_Admin_Reports_Taxes_Stats_Data_Store class file. * API\Reports\Taxes\Stats\DataStore class file.
* *
* @package WooCommerce Admin/Classes * @package WooCommerce Admin/Classes
*/ */
namespace Automattic\WooCommerce\Admin\API\Reports\Taxes\Stats;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
use \Automattic\WooCommerce\Admin\API\Reports\DataStore as ReportsDataStore;
use \Automattic\WooCommerce\Admin\API\Reports\DataStoreInterface;
use \Automattic\WooCommerce\Admin\API\Reports\TimeInterval;
/** /**
* WC_Reports_Taxes_Stats_Data_Store. * API\Reports\Taxes\Stats\DataStore.
*/ */
class WC_Admin_Reports_Taxes_Stats_Data_Store extends WC_Admin_Reports_Data_Store implements WC_Admin_Reports_Data_Store_Interface { class DataStore extends ReportsDataStore implements DataStoreInterface {
/** /**
* Table used to get the data. * Table used to get the data.
@ -152,8 +157,8 @@ class WC_Admin_Reports_Taxes_Stats_Data_Store extends WC_Admin_Reports_Data_Stor
'page' => 1, 'page' => 1,
'order' => 'DESC', 'order' => 'DESC',
'orderby' => 'tax_rate_id', 'orderby' => 'tax_rate_id',
'before' => WC_Admin_Reports_Interval::default_before(), 'before' => TimeInterval::default_before(),
'after' => WC_Admin_Reports_Interval::default_after(), 'after' => TimeInterval::default_after(),
'fields' => '*', 'fields' => '*',
'taxes' => array(), 'taxes' => array(),
); );
@ -194,7 +199,7 @@ class WC_Admin_Reports_Taxes_Stats_Data_Store extends WC_Admin_Reports_Data_Stor
); // WPCS: cache ok, DB call ok, unprepared SQL ok. ); // WPCS: cache ok, DB call ok, unprepared SQL ok.
$db_interval_count = count( $db_intervals ); $db_interval_count = count( $db_intervals );
$expected_interval_count = WC_Admin_Reports_Interval::intervals_between( $query_args['after'], $query_args['before'], $query_args['interval'] ); $expected_interval_count = TimeInterval::intervals_between( $query_args['after'], $query_args['before'], $query_args['interval'] );
$total_pages = (int) ceil( $expected_interval_count / $intervals_query['per_page'] ); $total_pages = (int) ceil( $expected_interval_count / $intervals_query['per_page'] );
if ( $query_args['page'] < 1 || $query_args['page'] > $total_pages ) { if ( $query_args['page'] < 1 || $query_args['page'] > $total_pages ) {
@ -217,9 +222,9 @@ class WC_Admin_Reports_Taxes_Stats_Data_Store extends WC_Admin_Reports_Data_Stor
); // WPCS: cache ok, DB call ok, unprepared SQL ok. ); // WPCS: cache ok, DB call ok, unprepared SQL ok.
if ( null === $totals ) { if ( null === $totals ) {
return new WP_Error( 'woocommerce_reports_taxes_stats_result_failed', __( 'Sorry, fetching revenue data failed.', 'woocommerce-admin' ) ); return new \WP_Error( 'woocommerce_reports_taxes_stats_result_failed', __( 'Sorry, fetching revenue data failed.', 'woocommerce-admin' ) );
} }
$segmenter = new WC_Admin_Reports_Taxes_Stats_Segmenting( $query_args, $this->report_columns ); $segmenter = new Segmenter( $query_args, $this->report_columns );
$totals[0]['segments'] = $segmenter->get_totals_segments( $totals_query, $table_name ); $totals[0]['segments'] = $segmenter->get_totals_segments( $totals_query, $table_name );
$this->update_intervals_sql_params( $intervals_query, $query_args, $db_interval_count, $expected_interval_count, $table_name ); $this->update_intervals_sql_params( $intervals_query, $query_args, $db_interval_count, $expected_interval_count, $table_name );
@ -251,7 +256,7 @@ class WC_Admin_Reports_Taxes_Stats_Data_Store extends WC_Admin_Reports_Data_Stor
); // WPCS: cache ok, DB call ok, unprepared SQL ok. ); // WPCS: cache ok, DB call ok, unprepared SQL ok.
if ( null === $intervals ) { if ( null === $intervals ) {
return new WP_Error( 'woocommerce_reports_taxes_stats_result_failed', __( 'Sorry, fetching tax data failed.', 'woocommerce-admin' ) ); return new \WP_Error( 'woocommerce_reports_taxes_stats_result_failed', __( 'Sorry, fetching tax data failed.', 'woocommerce-admin' ) );
} }
$totals = (object) $this->cast_numbers( $totals[0] ); $totals = (object) $this->cast_numbers( $totals[0] );
@ -264,7 +269,7 @@ class WC_Admin_Reports_Taxes_Stats_Data_Store extends WC_Admin_Reports_Data_Stor
'page_no' => (int) $query_args['page'], 'page_no' => (int) $query_args['page'],
); );
if ( WC_Admin_Reports_Interval::intervals_missing( $expected_interval_count, $db_interval_count, $intervals_query['per_page'], $query_args['page'], $query_args['order'], $query_args['orderby'], count( $intervals ) ) ) { if ( TimeInterval::intervals_missing( $expected_interval_count, $db_interval_count, $intervals_query['per_page'], $query_args['page'], $query_args['order'], $query_args['orderby'], count( $intervals ) ) ) {
$this->fill_in_missing_intervals( $db_intervals, $query_args['adj_after'], $query_args['adj_before'], $query_args['interval'], $data ); $this->fill_in_missing_intervals( $db_intervals, $query_args['adj_after'], $query_args['adj_before'], $query_args['interval'], $data );
$this->sort_intervals( $data, $query_args['orderby'], $query_args['order'] ); $this->sort_intervals( $data, $query_args['orderby'], $query_args['order'] );
$this->remove_extra_records( $data, $query_args['page'], $intervals_query['per_page'], $db_interval_count, $expected_interval_count, $query_args['orderby'], $query_args['order'] ); $this->remove_extra_records( $data, $query_args['page'], $intervals_query['per_page'], $db_interval_count, $expected_interval_count, $query_args['orderby'], $query_args['order'] );

View File

@ -10,18 +10,22 @@
* 'categories' => array(15, 18), * 'categories' => array(15, 18),
* 'product_ids' => array(1,2,3) * 'product_ids' => array(1,2,3)
* ); * );
* $report = new WC_Admin_Reports_Taxes_Stats_Query( $args ); * $report = new \Automattic\WooCommerce\Admin\API\Reports\Taxes\Stats\Query( $args );
* $mydata = $report->get_data(); * $mydata = $report->get_data();
* *
* @package WooCommerce Admin/Classes * @package WooCommerce Admin/Classes
*/ */
namespace Automattic\WooCommerce\Admin\API\Reports\Taxes\Stats;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
use \Automattic\WooCommerce\Admin\API\Reports\Query as ReportsQuery;
/** /**
* WC_Admin_Reports_Taxes_Query * API\Reports\Taxes\Stats\Query
*/ */
class WC_Admin_Reports_Taxes_Stats_Query extends WC_Admin_Reports_Query { class Query extends ReportsQuery {
/** /**
* Valid fields for Taxes report. * Valid fields for Taxes report.
@ -40,7 +44,7 @@ class WC_Admin_Reports_Taxes_Stats_Query extends WC_Admin_Reports_Query {
public function get_data() { public function get_data() {
$args = apply_filters( 'woocommerce_reports_taxes_stats_query_args', $this->get_query_vars() ); $args = apply_filters( 'woocommerce_reports_taxes_stats_query_args', $this->get_query_vars() );
$data_store = WC_Data_Store::load( 'report-taxes-stats' ); $data_store = \WC_Data_Store::load( 'report-taxes-stats' );
$results = $data_store->get_data( $args ); $results = $data_store->get_data( $args );
return apply_filters( 'woocommerce_reports_taxes_stats_select_query', $results, $args ); return apply_filters( 'woocommerce_reports_taxes_stats_select_query', $results, $args );
} }

View File

@ -5,12 +5,16 @@
* @package WooCommerce Admin/Classes * @package WooCommerce Admin/Classes
*/ */
namespace Automattic\WooCommerce\Admin\API\Reports\Taxes\Stats;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
use \Automattic\WooCommerce\Admin\API\Reports\Segmenter as ReportsSegmenter;
/** /**
* Date & time interval and numeric range handling class for Reporting API. * Date & time interval and numeric range handling class for Reporting API.
*/ */
class WC_Admin_Reports_Taxes_Stats_Segmenting extends WC_Admin_Reports_Segmenting { class Segmenter extends ReportsSegmenter {
/** /**
* Returns SELECT clause statements to be used for order-related order-level segmenting query (e.g. tax_rate_id). * Returns SELECT clause statements to be used for order-related order-level segmenting query (e.g. tax_rate_id).
@ -124,7 +128,7 @@ class WC_Admin_Reports_Taxes_Stats_Segmenting extends WC_Admin_Reports_Segmentin
* @param string $table_name Name of main SQL table for the data store (used as basis for JOINS). * @param string $table_name Name of main SQL table for the data store (used as basis for JOINS).
* *
* @return array * @return array
* @throws WC_Admin_Reports_Parameter_Exception In case of segmenting by variations, when no parent product is specified. * @throws \Automattic\WooCommerce\Admin\API\Reports\ParameterException In case of segmenting by variations, when no parent product is specified.
*/ */
protected function get_segments( $type, $query_params, $table_name ) { protected function get_segments( $type, $query_params, $table_name ) {
if ( ! isset( $this->query_args['segmentby'] ) || '' === $this->query_args['segmentby'] ) { if ( ! isset( $this->query_args['segmentby'] ) || '' === $this->query_args['segmentby'] ) {

View File

@ -5,12 +5,14 @@
* @package WooCommerce Admin/Classes * @package WooCommerce Admin/Classes
*/ */
namespace Automattic\WooCommerce\Admin\API\Reports;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
/** /**
* Date & time interval and numeric range handling class for Reporting API. * Date & time interval and numeric range handling class for Reporting API.
*/ */
class WC_Admin_Reports_Interval { class TimeInterval {
/** /**
* Format string for ISO DateTime formatter. * Format string for ISO DateTime formatter.
@ -33,8 +35,8 @@ class WC_Admin_Reports_Interval {
* @return DateTime * @return DateTime
*/ */
public static function convert_local_datetime_to_gmt( $datetime_string ) { public static function convert_local_datetime_to_gmt( $datetime_string ) {
$datetime = new DateTime( $datetime_string, new DateTimeZone( wc_timezone_string() ) ); $datetime = new \DateTime( $datetime_string, new \DateTimeZone( wc_timezone_string() ) );
$datetime->setTimezone( new DateTimeZone( 'GMT' ) ); $datetime->setTimezone( new \DateTimeZone( 'GMT' ) );
return $datetime; return $datetime;
} }
@ -44,10 +46,10 @@ class WC_Admin_Reports_Interval {
* @return DateTime * @return DateTime
*/ */
public static function default_before() { public static function default_before() {
$datetime = new WC_DateTime(); $datetime = new \WC_DateTime();
// Set local timezone or offset. // Set local timezone or offset.
if ( get_option( 'timezone_string' ) ) { if ( get_option( 'timezone_string' ) ) {
$datetime->setTimezone( new DateTimeZone( wc_timezone_string() ) ); $datetime->setTimezone( new \DateTimeZone( wc_timezone_string() ) );
} else { } else {
$datetime->set_utc_offset( wc_timezone_offset() ); $datetime->set_utc_offset( wc_timezone_offset() );
} }
@ -63,11 +65,11 @@ class WC_Admin_Reports_Interval {
$now = time(); $now = time();
$week_back = $now - WEEK_IN_SECONDS; $week_back = $now - WEEK_IN_SECONDS;
$datetime = new WC_DateTime(); $datetime = new \WC_DateTime();
$datetime->setTimestamp( $week_back ); $datetime->setTimestamp( $week_back );
// Set local timezone or offset. // Set local timezone or offset.
if ( get_option( 'timezone_string' ) ) { if ( get_option( 'timezone_string' ) ) {
$datetime->setTimezone( new DateTimeZone( wc_timezone_string() ) ); $datetime->setTimezone( new \DateTimeZone( wc_timezone_string() ) );
} else { } else {
$datetime->set_utc_offset( wc_timezone_offset() ); $datetime->set_utc_offset( wc_timezone_offset() );
} }
@ -147,7 +149,7 @@ class WC_Admin_Reports_Interval {
* @return int * @return int
*/ */
public static function simple_week_number( $datetime, $first_day_of_week ) { public static function simple_week_number( $datetime, $first_day_of_week ) {
$beg_of_year_day = new DateTime( "{$datetime->format('Y')}-01-01" ); $beg_of_year_day = new \DateTime( "{$datetime->format('Y')}-01-01" );
$adj_day_beg_of_year = ( (int) $beg_of_year_day->format( 'w' ) - $first_day_of_week + 7 ) % 7; $adj_day_beg_of_year = ( (int) $beg_of_year_day->format( 'w' ) - $first_day_of_week + 7 ) % 7;
$days_since_start_of_year = (int) $datetime->format( 'z' ) + 1; $days_since_start_of_year = (int) $datetime->format( 'z' ) + 1;
@ -158,7 +160,7 @@ class WC_Admin_Reports_Interval {
* Returns ISO 8601 week number for the DateTime, if week starts on Monday, * Returns ISO 8601 week number for the DateTime, if week starts on Monday,
* otherwise returns simple week number. * otherwise returns simple week number.
* *
* @see WC_Admin_Reports_Interval::simple_week_number() * @see TimeInterval::simple_week_number()
* *
* @param DateTime $datetime Local date for which the week number is to be calculated. * @param DateTime $datetime Local date for which the week number is to be calculated.
* @param int $first_day_of_week 0 for Sunday to 6 for Saturday. * @param int $first_day_of_week 0 for Sunday to 6 for Saturday.
@ -288,9 +290,9 @@ class WC_Admin_Reports_Interval {
$hours_offset_timestamp --; $hours_offset_timestamp --;
} }
$hours_offset_time = new DateTime(); $hours_offset_time = new \DateTime();
$hours_offset_time->setTimestamp( $hours_offset_timestamp ); $hours_offset_time->setTimestamp( $hours_offset_timestamp );
$hours_offset_time->setTimezone( new DateTimeZone( wc_timezone_string() ) ); $hours_offset_time->setTimezone( new \DateTimeZone( wc_timezone_string() ) );
return $hours_offset_time; return $hours_offset_time;
} }
@ -312,9 +314,9 @@ class WC_Admin_Reports_Interval {
$next_day_timestamp --; $next_day_timestamp --;
} }
$next_day = new DateTime(); $next_day = new \DateTime();
$next_day->setTimestamp( $next_day_timestamp ); $next_day->setTimestamp( $next_day_timestamp );
$next_day->setTimezone( new DateTimeZone( wc_timezone_string() ) ); $next_day->setTimezone( new \DateTimeZone( wc_timezone_string() ) );
return $next_day; return $next_day;
} }
@ -357,7 +359,7 @@ class WC_Admin_Reports_Interval {
$month = (int) $datetime->format( 'm' ); $month = (int) $datetime->format( 'm' );
if ( $reversed ) { if ( $reversed ) {
$beg_of_month_datetime = new DateTime( "$year-$month-01 00:00:00", new DateTimeZone( wc_timezone_string() ) ); $beg_of_month_datetime = new \DateTime( "$year-$month-01 00:00:00", new \DateTimeZone( wc_timezone_string() ) );
$timestamp = (int) $beg_of_month_datetime->format( 'U' ); $timestamp = (int) $beg_of_month_datetime->format( 'U' );
$end_of_prev_month_timestamp = $timestamp - 1; $end_of_prev_month_timestamp = $timestamp - 1;
$datetime->setTimestamp( $end_of_prev_month_timestamp ); $datetime->setTimestamp( $end_of_prev_month_timestamp );
@ -368,7 +370,7 @@ class WC_Admin_Reports_Interval {
$year ++; $year ++;
} }
$day = '01'; $day = '01';
$datetime = new DateTime( "$year-$month-$day 00:00:00", new DateTimeZone( wc_timezone_string() ) ); $datetime = new \DateTime( "$year-$month-$day 00:00:00", new \DateTimeZone( wc_timezone_string() ) );
} }
return $datetime; return $datetime;
@ -424,7 +426,7 @@ class WC_Admin_Reports_Interval {
} }
break; break;
} }
$datetime = new DateTime( "$year-$month-01 00:00:00", new DateTimeZone( wc_timezone_string() ) ); $datetime = new \DateTime( "$year-$month-01 00:00:00", new \DateTimeZone( wc_timezone_string() ) );
if ( $reversed ) { if ( $reversed ) {
$timestamp = (int) $datetime->format( 'U' ); $timestamp = (int) $datetime->format( 'U' );
$end_of_prev_month_timestamp = $timestamp - 1; $end_of_prev_month_timestamp = $timestamp - 1;
@ -448,13 +450,13 @@ class WC_Admin_Reports_Interval {
$day = '01'; $day = '01';
if ( $reversed ) { if ( $reversed ) {
$datetime = new DateTime( "$year-$month-$day 00:00:00", new DateTimeZone( wc_timezone_string() ) ); $datetime = new \DateTime( "$year-$month-$day 00:00:00", new \DateTimeZone( wc_timezone_string() ) );
$timestamp = (int) $datetime->format( 'U' ); $timestamp = (int) $datetime->format( 'U' );
$end_of_prev_year_timestamp = $timestamp - 1; $end_of_prev_year_timestamp = $timestamp - 1;
$datetime->setTimestamp( $end_of_prev_year_timestamp ); $datetime->setTimestamp( $end_of_prev_year_timestamp );
} else { } else {
$year += $year_increment; $year += $year_increment;
$datetime = new DateTime( "$year-$month-$day 00:00:00", new DateTimeZone( wc_timezone_string() ) ); $datetime = new \DateTime( "$year-$month-$day 00:00:00", new \DateTimeZone( wc_timezone_string() ) );
} }
return $datetime; return $datetime;
@ -591,7 +593,7 @@ class WC_Admin_Reports_Interval {
*/ */
public static function rest_validate_between_numeric_arg( $value, $request, $param ) { public static function rest_validate_between_numeric_arg( $value, $request, $param ) {
if ( ! wp_is_numeric_array( $value ) ) { if ( ! wp_is_numeric_array( $value ) ) {
return new WP_Error( return new \WP_Error(
'rest_invalid_param', 'rest_invalid_param',
/* translators: 1: parameter name */ /* translators: 1: parameter name */
sprintf( __( '%1$s is not a numerically indexed array.', 'woocommerce-admin' ), $param ) sprintf( __( '%1$s is not a numerically indexed array.', 'woocommerce-admin' ), $param )
@ -603,7 +605,7 @@ class WC_Admin_Reports_Interval {
! is_numeric( $value[0] ) || ! is_numeric( $value[0] ) ||
! is_numeric( $value[1] ) ! is_numeric( $value[1] )
) { ) {
return new WP_Error( return new \WP_Error(
'rest_invalid_param', 'rest_invalid_param',
/* translators: %s: parameter name */ /* translators: %s: parameter name */
sprintf( __( '%s must contain 2 numbers.', 'woocommerce-admin' ), $param ) sprintf( __( '%s must contain 2 numbers.', 'woocommerce-admin' ), $param )
@ -623,7 +625,7 @@ class WC_Admin_Reports_Interval {
*/ */
public static function rest_validate_between_date_arg( $value, $request, $param ) { public static function rest_validate_between_date_arg( $value, $request, $param ) {
if ( ! wp_is_numeric_array( $value ) ) { if ( ! wp_is_numeric_array( $value ) ) {
return new WP_Error( return new \WP_Error(
'rest_invalid_param', 'rest_invalid_param',
/* translators: 1: parameter name */ /* translators: 1: parameter name */
sprintf( __( '%1$s is not a numerically indexed array.', 'woocommerce-admin' ), $param ) sprintf( __( '%1$s is not a numerically indexed array.', 'woocommerce-admin' ), $param )
@ -635,7 +637,7 @@ class WC_Admin_Reports_Interval {
! rest_parse_date( $value[0] ) || ! rest_parse_date( $value[0] ) ||
! rest_parse_date( $value[1] ) ! rest_parse_date( $value[1] )
) { ) {
return new WP_Error( return new \WP_Error(
'rest_invalid_param', 'rest_invalid_param',
/* translators: %s: parameter name */ /* translators: %s: parameter name */
sprintf( __( '%s must contain 2 valid dates.', 'woocommerce-admin' ), $param ) sprintf( __( '%s must contain 2 valid dates.', 'woocommerce-admin' ), $param )

View File

@ -7,6 +7,8 @@
* @package WooCommerce Admin/API * @package WooCommerce Admin/API
*/ */
namespace Automattic\WooCommerce\Admin\API\Reports\Variations;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
/** /**
@ -15,7 +17,7 @@ defined( 'ABSPATH' ) || exit;
* @package WooCommerce/API * @package WooCommerce/API
* @extends WC_REST_Reports_Controller * @extends WC_REST_Reports_Controller
*/ */
class WC_Admin_REST_Reports_Variations_Controller extends WC_REST_Reports_Controller { class Controller extends \WC_REST_Reports_Controller {
/** /**
* Endpoint namespace. * Endpoint namespace.
@ -60,7 +62,7 @@ class WC_Admin_REST_Reports_Variations_Controller extends WC_REST_Reports_Contro
} }
} }
$reports = new WC_Admin_Reports_Variations_Query( $args ); $reports = new Query( $args );
$products_data = $reports->get_data(); $products_data = $reports->get_data();
$data = array(); $data = array();

View File

@ -1,16 +1,22 @@
<?php <?php
/** /**
* WC_Admin_Reports_Products_Data_Store class file. * API\Reports\Variations\DataStore class file.
* *
* @package WooCommerce Admin/Classes * @package WooCommerce Admin/Classes
*/ */
namespace Automattic\WooCommerce\Admin\API\Reports\Variations;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
use \Automattic\WooCommerce\Admin\API\Reports\DataStore as ReportsDataStore;
use \Automattic\WooCommerce\Admin\API\Reports\DataStoreInterface;
use \Automattic\WooCommerce\Admin\API\Reports\TimeInterval;
/** /**
* WC_Admin_Reports_Products_Data_Store. * API\Reports\Variations\DataStore.
*/ */
class WC_Admin_Reports_Variations_Data_Store extends WC_Admin_Reports_Data_Store implements WC_Admin_Reports_Data_Store_Interface { class DataStore extends ReportsDataStore implements DataStoreInterface {
/** /**
* Table used to get the data. * Table used to get the data.
@ -167,7 +173,7 @@ class WC_Admin_Reports_Variations_Data_Store extends WC_Admin_Reports_Data_Store
*/ */
protected function include_extended_info( &$products_data, $query_args ) { protected function include_extended_info( &$products_data, $query_args ) {
foreach ( $products_data as $key => $product_data ) { foreach ( $products_data as $key => $product_data ) {
$extended_info = new ArrayObject(); $extended_info = new \ArrayObject();
if ( $query_args['extended_info'] ) { if ( $query_args['extended_info'] ) {
$extended_attributes = apply_filters( 'woocommerce_rest_reports_variations_extended_attributes', $this->extended_attributes, $product_data ); $extended_attributes = apply_filters( 'woocommerce_rest_reports_variations_extended_attributes', $this->extended_attributes, $product_data );
$product = wc_get_product( $product_data['product_id'] ); $product = wc_get_product( $product_data['product_id'] );
@ -226,8 +232,8 @@ class WC_Admin_Reports_Variations_Data_Store extends WC_Admin_Reports_Data_Store
'page' => 1, 'page' => 1,
'order' => 'DESC', 'order' => 'DESC',
'orderby' => 'date', 'orderby' => 'date',
'before' => WC_Admin_Reports_Interval::default_before(), 'before' => TimeInterval::default_before(),
'after' => WC_Admin_Reports_Interval::default_after(), 'after' => TimeInterval::default_after(),
'fields' => '*', 'fields' => '*',
'products' => array(), 'products' => array(),
'variations' => array(), 'variations' => array(),

View File

@ -10,18 +10,22 @@
* 'categories' => array(15, 18), * 'categories' => array(15, 18),
* 'products' => array(1,2,3) * 'products' => array(1,2,3)
* ); * );
* $report = new WC_Admin_Reports_Products_Query( $args ); * $report = new \Automattic\WooCommerce\Admin\API\Reports\Variations\Query( $args );
* $mydata = $report->get_data(); * $mydata = $report->get_data();
* *
* @package WooCommerce Admin/Classes * @package WooCommerce Admin/Classes
*/ */
namespace Automattic\WooCommerce\Admin\API\Reports\Variations;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
use \Automattic\WooCommerce\Admin\API\Reports\Query as ReportsQuery;
/** /**
* WC_Admin_Reports_Products_Query * API\Reports\Variations\Query
*/ */
class WC_Admin_Reports_Variations_Query extends WC_Admin_Reports_Query { class Query extends ReportsQuery {
/** /**
* Valid fields for Products report. * Valid fields for Products report.
@ -40,7 +44,7 @@ class WC_Admin_Reports_Variations_Query extends WC_Admin_Reports_Query {
public function get_data() { public function get_data() {
$args = apply_filters( 'woocommerce_reports_variations_query_args', $this->get_query_vars() ); $args = apply_filters( 'woocommerce_reports_variations_query_args', $this->get_query_vars() );
$data_store = WC_Data_Store::load( 'report-variations' ); $data_store = \WC_Data_Store::load( 'report-variations' );
$results = $data_store->get_data( $args ); $results = $data_store->get_data( $args );
return apply_filters( 'woocommerce_reports_variations_select_query', $results, $args ); return apply_filters( 'woocommerce_reports_variations_select_query', $results, $args );
} }

View File

@ -7,6 +7,8 @@
* @package WooCommerce Admin/API * @package WooCommerce Admin/API
*/ */
namespace Automattic\WooCommerce\Admin\API;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
/** /**
@ -15,7 +17,7 @@ defined( 'ABSPATH' ) || exit;
* @package WooCommerce Admin/API * @package WooCommerce Admin/API
* @extends WC_REST_Setting_Options_Controller * @extends WC_REST_Setting_Options_Controller
*/ */
class WC_Admin_REST_Setting_Options_Controller extends WC_REST_Setting_Options_Controller { class SettingOptions extends \WC_REST_Setting_Options_Controller {
/** /**
* Endpoint namespace. * Endpoint namespace.

View File

@ -7,6 +7,8 @@
* @package WooCommerce Admin/API * @package WooCommerce Admin/API
*/ */
namespace Automattic\WooCommerce\Admin\API;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
/** /**
@ -15,7 +17,7 @@ defined( 'ABSPATH' ) || exit;
* @package WooCommerce Admin/API * @package WooCommerce Admin/API
* @extends WC_REST_Taxes_Controller * @extends WC_REST_Taxes_Controller
*/ */
class WC_Admin_REST_Taxes_Controller extends WC_REST_Taxes_Controller { class Taxes extends \WC_REST_Taxes_Controller {
/** /**
* Endpoint namespace. * Endpoint namespace.

View File

@ -7,15 +7,20 @@
* @package WooCommerce Admin/API * @package WooCommerce Admin/API
*/ */
namespace Automattic\WooCommerce\Admin\API;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\Overrides\ThemeUpgrader;
use Automattic\WooCommerce\Admin\Overrides\ThemeUpgraderSkin;
/** /**
* Themes controller. * Themes controller.
* *
* @package WooCommerce Admin/API * @package WooCommerce Admin/API
* @extends WC_REST_Data_Controller * @extends WC_REST_Data_Controller
*/ */
class WC_Admin_REST_Themes_Controller extends WC_REST_Data_Controller { class Themes extends \WC_REST_Data_Controller {
/** /**
* Endpoint namespace. * Endpoint namespace.
* *
@ -39,7 +44,7 @@ class WC_Admin_REST_Themes_Controller extends WC_REST_Data_Controller {
'/' . $this->rest_base, '/' . $this->rest_base,
array( array(
array( array(
'methods' => WP_REST_Server::EDITABLE, 'methods' => \WP_REST_Server::EDITABLE,
'callback' => array( $this, 'upload_theme' ), 'callback' => array( $this, 'upload_theme' ),
'permission_callback' => array( $this, 'upload_theme_permissions_check' ), 'permission_callback' => array( $this, 'upload_theme_permissions_check' ),
'args' => $this->get_collection_params(), 'args' => $this->get_collection_params(),
@ -57,7 +62,7 @@ class WC_Admin_REST_Themes_Controller extends WC_REST_Data_Controller {
*/ */
public function upload_theme_permissions_check( $request ) { public function upload_theme_permissions_check( $request ) {
if ( ! current_user_can( 'upload_themes' ) ) { if ( ! current_user_can( 'upload_themes' ) ) {
return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you are not allowed to install themes on this site.', 'woocommerce-admin' ), array( 'status' => rest_authorization_required_code() ) ); return new \WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you are not allowed to install themes on this site.', 'woocommerce-admin' ), array( 'status' => rest_authorization_required_code() ) );
} }
return true; return true;
@ -71,17 +76,15 @@ class WC_Admin_REST_Themes_Controller extends WC_REST_Data_Controller {
*/ */
public function upload_theme( $request ) { public function upload_theme( $request ) {
if ( ! isset( $_FILES['pluginzip'] ) || ! is_uploaded_file( $_FILES['pluginzip']['tmp_name'] ) || ! is_file( $_FILES['pluginzip']['tmp_name'] ) ) { // WPCS: sanitization ok. if ( ! isset( $_FILES['pluginzip'] ) || ! is_uploaded_file( $_FILES['pluginzip']['tmp_name'] ) || ! is_file( $_FILES['pluginzip']['tmp_name'] ) ) { // WPCS: sanitization ok.
return new WP_Error( 'woocommerce_rest_invalid_file', __( 'Specified file failed upload test.', 'woocommerce-admin' ) ); return new \WP_Error( 'woocommerce_rest_invalid_file', __( 'Specified file failed upload test.', 'woocommerce-admin' ) );
} }
include_once ABSPATH . 'wp-admin/includes/file.php'; include_once ABSPATH . 'wp-admin/includes/file.php';
include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
include_once WC_ADMIN_ABSPATH . 'includes/class-wc-admin-theme-upgrader.php';
include_once WC_ADMIN_ABSPATH . 'includes/class-wc-admin-theme-upgrader-skin.php';
$_GET['package'] = true; $_GET['package'] = true;
$file_upload = new File_Upload_Upgrader( 'pluginzip', 'package' ); $file_upload = new \File_Upload_Upgrader( 'pluginzip', 'package' );
$upgrader = new WC_Admin_Theme_Upgrader( new WC_Admin_Theme_Upgrader_Skin() ); $upgrader = new ThemeUpgrader( new ThemeUpgraderSkin() );
$install = $upgrader->install( $file_upload->package ); $install = $upgrader->install( $file_upload->package );
if ( $install || is_wp_error( $install ) ) { if ( $install || is_wp_error( $install ) ) {

View File

@ -6,12 +6,18 @@
* @package Woocommerce Admin * @package Woocommerce Admin
*/ */
namespace Automattic\WooCommerce\Admin;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
use \Automattic\WooCommerce\Admin\Notes\WC_Admin_Notes_Giving_Feedback_Notes;
use \Automattic\WooCommerce\Admin\Notes\WC_Admin_Notes_Mobile_App;
use \Automattic\WooCommerce\Admin\Notes\WC_Admin_Notes_New_Sales_Record;
/** /**
* WC_Admin_Events Class. * WC_Admin_Events Class.
*/ */
class WC_Admin_Events { class Events {
/** /**
* The single instance of the class. * The single instance of the class.
* *
@ -56,5 +62,3 @@ class WC_Admin_Events {
WC_Admin_Notes_Mobile_App::possibly_add_mobile_app_note(); WC_Admin_Notes_Mobile_App::possibly_add_mobile_app_note();
} }
} }
WC_Admin_Events::instance()->init();

View File

@ -0,0 +1,316 @@
<?php
/**
* WooCommerce Admin: Feature plugin main class.
*
* @package WooCommerce Admin
*/
namespace Automattic\WooCommerce\Admin;
defined( 'ABSPATH' ) || exit;
use \Automattic\WooCommerce\Admin\Notes\WC_Admin_Notes;
use \Automattic\WooCommerce\Admin\Notes\WC_Admin_Notes_Historical_Data;
use \Automattic\WooCommerce\Admin\Notes\WC_Admin_Notes_Order_Milestones;
use \Automattic\WooCommerce\Admin\Notes\WC_Admin_Notes_Welcome_Message;
use \Automattic\WooCommerce\Admin\Notes\WC_Admin_Notes_Woo_Subscriptions_Notes;;
/**
* Feature plugin main class.
*
* @internal This file will not be bundled with woo core, only the feature plugin.
* @internal Note this is not called WC_Admin due to a class already existing in core with that name.
*/
class FeaturePlugin {
/**
* The single instance of the class.
*
* @var object
*/
protected static $instance = null;
/**
* Constructor
*
* @return void
*/
protected function __construct() {}
/**
* Get class instance.
*
* @return object Instance.
*/
final public static function instance() {
if ( null === static::$instance ) {
static::$instance = new static();
}
return static::$instance;
}
/**
* Init the feature plugin, only if we can detect both Gutenberg and WooCommerce.
*/
public function init() {
$this->define_constants();
register_activation_hook( WC_ADMIN_PLUGIN_FILE, array( $this, 'on_activation' ) );
register_deactivation_hook( WC_ADMIN_PLUGIN_FILE, array( $this, 'on_deactivation' ) );
add_action( 'plugins_loaded', array( $this, 'on_plugins_loaded' ) );
add_filter( 'action_scheduler_store_class', array( $this, 'replace_actionscheduler_store_class' ) );
}
/**
* Install DB and create cron events when activated.
*
* @return void
*/
public function on_activation() {
Install::create_tables();
Install::create_events();
}
/**
* Remove WooCommerce Admin scheduled actions on deactivate.
*
* @return void
*/
public function on_deactivation() {
// Check if we are deactivating due to dependencies not being satisfied.
// If WooCommerce is disabled we can't include files that depend upon it.
if ( ! $this->check_dependencies() ) {
return;
}
$this->includes();
ReportsSync::clear_queued_actions();
WC_Admin_Notes::clear_queued_actions();
wp_clear_scheduled_hook( 'wc_admin_daily' );
}
/**
* Setup plugin once all other plugins are loaded.
*
* @return void
*/
public function on_plugins_loaded() {
$this->load_plugin_textdomain();
if ( ! $this->check_dependencies() ) {
add_action( 'admin_init', array( $this, 'deactivate_self' ) );
add_action( 'admin_notices', array( $this, 'render_dependencies_notice' ) );
return;
}
if ( ! $this->check_build() ) {
add_action( 'admin_notices', array( $this, 'render_build_notice' ) );
}
$this->includes();
$this->hooks();
}
/**
* Define Constants.
*/
protected function define_constants() {
$this->define( 'WC_ADMIN_APP', 'wc-admin-app' );
$this->define( 'WC_ADMIN_ABSPATH', dirname( __DIR__ ) . '/' );
$this->define( 'WC_ADMIN_DIST_JS_FOLDER', 'dist/' );
$this->define( 'WC_ADMIN_DIST_CSS_FOLDER', 'dist/' );
$this->define( 'WC_ADMIN_FEATURES_PATH', WC_ADMIN_ABSPATH . 'includes/features/' );
$this->define( 'WC_ADMIN_PLUGIN_FILE', WC_ADMIN_ABSPATH . 'woocommerce-admin.php' );
// WARNING: Do not directly edit this version number constant.
// It is updated as part of the prebuild process from the package.json value.
$this->define( 'WC_ADMIN_VERSION_NUMBER', '0.16.0' );
}
/**
* Load Localisation files.
*/
protected function load_plugin_textdomain() {
load_plugin_textdomain( 'woocommerce-admin', false, basename( dirname( __DIR__ ) ) . '/languages' );
}
/**
* Include WC Admin classes.
*/
public function includes() {
// Initialize the WC API extensions.
ReportsSync::init();
Install::init();
Events::instance()->init();
new API\Init();
ReportExporter::init();
// CRUD classes.
WC_Admin_Notes::init();
// Admin note providers.
// @todo These should be bundled in the features/ folder, but loading them from there currently has a load order issue.
new WC_Admin_Notes_Woo_Subscriptions_Notes();
new WC_Admin_Notes_Historical_Data();
new WC_Admin_Notes_Order_Milestones();
new WC_Admin_Notes_Welcome_Message();
}
/**
* Filter in our ActionScheduler Store class.
*
* @param string $store_class ActionScheduler Store class name.
* @return string ActionScheduler Store class name.
*/
public function replace_actionscheduler_store_class( $store_class ) {
// Don't override any other overrides.
if ( 'ActionScheduler_wpPostStore' !== $store_class ) {
return $store_class;
}
return 'Automattic\WooCommerce\Admin\Overrides\WPPostStore';
}
/**
* Removes core hooks in favor of our local feature plugin handlers.
*
* @see WC_Admin_Library::__construct()
*/
protected function hooks() {
remove_action( 'init', array( 'WC_Admin_Library', 'load_features' ) );
remove_action( 'admin_enqueue_scripts', array( 'WC_Admin_Library', 'register_scripts' ) );
remove_action( 'admin_enqueue_scripts', array( 'WC_Admin_Library', 'load_scripts' ), 15 );
remove_action( 'woocommerce_components_settings', array( 'WC_Admin_Library', 'add_component_settings' ) );
remove_filter( 'admin_body_class', array( 'WC_Admin_Library', 'add_admin_body_classes' ) );
remove_action( 'admin_menu', array( 'WC_Admin_Library', 'register_page_handler' ) );
remove_filter( 'admin_title', array( 'WC_Admin_Library', 'update_admin_title' ) );
remove_action( 'rest_api_init', array( 'WC_Admin_Library', 'register_user_data' ) );
remove_action( 'in_admin_header', array( 'WC_Admin_Library', 'embed_page_header' ) );
remove_filter( 'woocommerce_settings_groups', array( 'WC_Admin_Library', 'add_settings_group' ) );
remove_filter( 'woocommerce_settings-wc_admin', array( 'WC_Admin_Library', 'add_settings' ) );
remove_action( 'admin_head', array( 'WC_Admin_Library', 'update_link_structure' ), 20 );
new Loader();
add_filter( 'woocommerce_admin_features', array( $this, 'replace_supported_features' ) );
add_action( 'admin_menu', array( $this, 'register_devdocs_page' ) );
}
/**
* Returns true if all dependencies for the wc-admin plugin are loaded.
*
* @return bool
*/
protected function check_dependencies() {
$woocommerce_minimum_met = class_exists( 'WooCommerce' ) && version_compare( WC_VERSION, '3.6', '>=' );
if ( ! $woocommerce_minimum_met ) {
return false;
}
$wordpress_version = get_bloginfo( 'version' );
return version_compare( $wordpress_version, '5.2.0', '>=' );
}
/**
* Returns true if build file exists.
*
* @return bool
*/
protected function check_build() {
return file_exists( plugin_dir_path( __DIR__ ) . '/dist/app/index.js' );
}
/**
* Deactivates this plugin.
*/
public function deactivate_self() {
deactivate_plugins( plugin_basename( WC_ADMIN_PLUGIN_FILE ) );
unset( $_GET['activate'] );
}
/**
* Notify users of the plugin requirements.
*/
public function render_dependencies_notice() {
// The notice varies by WordPress version.
$wordpress_version = get_bloginfo( 'version' );
$has_valid_wp_version = version_compare( $wordpress_version, '5.2.0', '>=' );
if ( $has_valid_wp_version ) {
$message = sprintf(
/* translators: URL of WooCommerce plugin */
__( 'The WooCommerce Admin feature plugin requires <a href="%s">WooCommerce</a> 3.6 or greater to be installed and active.', 'woocommerce-admin' ),
'https://wordpress.org/plugins/woocommerce/'
);
} else {
$message = sprintf(
/* translators: 1: URL of WordPress.org, 2: URL of WooCommerce plugin */
__( 'The WooCommerce Admin feature plugin requires both <a href="%1$s">WordPress</a> 5.2 or greater and <a href="%2$s">WooCommerce</a> 3.6 or greater to be installed and active.', 'woocommerce-admin' ),
'https://wordpress.org/',
'https://wordpress.org/plugins/woocommerce/'
);
}
printf( '<div class="error"><p>%s</p></div>', $message ); /* WPCS: xss ok. */
}
/**
* Notify users that the plugin needs to be built.
*/
public function render_build_notice() {
$message_one = __( 'You have installed a development version of WooCommerce Admin which requires files to be built. From the plugin directory, run <code>npm install</code> to install dependencies, <code>npm run build</code> to build the files.', 'woocommerce-admin' );
$message_two = sprintf(
/* translators: 1: URL of GitHub Repository build page */
__( 'Or you can download a pre-built version of the plugin by visiting <a href="%1$s">the releases page in the repository</a>.', 'woocommerce-admin' ),
'https://github.com/woocommerce/woocommerce-admin/releases'
);
printf( '<div class="error"><p>%s %s</p></div>', $message_one, $message_two ); /* WPCS: xss ok. */
}
/**
* Overwrites the allowed features array using a local `feature-config.php` file.
*
* @param array $features Array of feature slugs.
*/
public function replace_supported_features( $features ) {
$feature_config = apply_filters( 'wc_admin_get_feature_config', wc_admin_get_feature_config() );
$features = array_keys( array_filter( $feature_config ) );
return $features;
}
/**
* Adds a menu item for the wc-admin devdocs.
*/
public function register_devdocs_page() {
if ( Loader::is_feature_enabled( 'devdocs' ) && defined( 'WP_DEBUG' ) && WP_DEBUG && defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) {
wc_admin_register_page(
array(
'title' => 'DevDocs',
'parent' => 'woocommerce',
'path' => '/devdocs',
)
);
}
}
/**
* Define constant if not already set.
*
* @param string $name Constant name.
* @param string|bool $value Constant value.
*/
protected function define( $name, $value ) {
if ( ! defined( $name ) ) {
define( $name, $value );
}
}
/**
* Prevent cloning.
*/
private function __clone() {}
/**
* Prevent unserializing.
*/
private function __wakeup() {}
}

View File

@ -6,14 +6,19 @@
* @package Woocommerce Admin * @package Woocommerce Admin
*/ */
namespace Automattic\WooCommerce\Admin\Features;
use Automattic\WooCommerce\Admin\Notes\WC_Admin_Notes;
use Automattic\WooCommerce\Admin\Notes\WC_Admin_Notes_Settings_Notes;
/** /**
* Contains backend logic for the activity panel feature. * Contains backend logic for the activity panel feature.
*/ */
class WC_Admin_Activity_Panel { class ActivityPanels {
/** /**
* Class instance. * Class instance.
* *
* @var WC_Admin_Activity_Panel instance * @var ActivityPanels instance
*/ */
protected static $instance = null; protected static $instance = null;
@ -32,7 +37,7 @@ class WC_Admin_Activity_Panel {
*/ */
public function __construct() { public function __construct() {
add_filter( 'wc_admin_get_user_data_fields', array( $this, 'add_user_data_fields' ) ); add_filter( 'wc_admin_get_user_data_fields', array( $this, 'add_user_data_fields' ) );
add_action( 'woocommerce_components_settings', array( $this, 'component_settings' ), 20 ); // Run after WC_Admin_Loader. add_action( 'woocommerce_components_settings', array( $this, 'component_settings' ), 20 ); // Run after Automattic\WooCommerce\Admin\Loader.
add_action( 'woocommerce_updated', array( $this, 'woocommerce_updated_note' ) ); add_action( 'woocommerce_updated', array( $this, 'woocommerce_updated_note' ) );
} }
@ -69,5 +74,3 @@ class WC_Admin_Activity_Panel {
WC_Admin_Notes_Settings_Notes::add_notes_for_settings_that_have_moved(); WC_Admin_Notes_Settings_Notes::add_notes_for_settings_that_have_moved();
} }
} }
new WC_Admin_Activity_Panel();

View File

@ -6,14 +6,16 @@
* @package Woocommerce Admin * @package Woocommerce Admin
*/ */
namespace Automattic\WooCommerce\Admin\Features;
/** /**
* Contains backend logic for the Analytics feature. * Contains backend logic for the Analytics feature.
*/ */
class WC_Admin_Analytics { class Analytics {
/** /**
* Class instance. * Class instance.
* *
* @var WC_Admin_Analytics instance * @var Analytics instance
*/ */
protected static $instance = null; protected static $instance = null;
@ -152,5 +154,3 @@ class WC_Admin_Analytics {
} }
} }
} }
new WC_Admin_Analytics();

Some files were not shown because too many files have changed in this diff Show More