diff --git a/plugins/woocommerce-admin/bin/generate-feature-config.php b/plugins/woocommerce-admin/bin/generate-feature-config.php index 0fbe969a755..4dd23a67965 100644 --- a/plugins/woocommerce-admin/bin/generate-feature-config.php +++ b/plugins/woocommerce-admin/bin/generate-feature-config.php @@ -23,12 +23,14 @@ if ( 'core' !== $phase ) { $write = "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"; $config_file = fopen( 'includes/feature-config.php', 'w' ); diff --git a/plugins/woocommerce-admin/bin/travis.sh b/plugins/woocommerce-admin/bin/travis.sh index bd11a7fe063..c4f9f1076c1 100755 --- a/plugins/woocommerce-admin/bin/travis.sh +++ b/plugins/woocommerce-admin/bin/travis.sh @@ -2,11 +2,12 @@ # usage: travis.sh before|after if [ $1 == 'before' ]; then - + cd "$WP_CORE_DIR/wp-content/plugins/woocommerce-admin/" 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 + else + npm run build:feature-config + composer install --no-dev fi fi diff --git a/plugins/woocommerce-admin/composer.json b/plugins/woocommerce-admin/composer.json index 6a385364e2a..e6ba4804ec9 100644 --- a/plugins/woocommerce-admin/composer.json +++ b/plugins/woocommerce-admin/composer.json @@ -30,5 +30,18 @@ "phpcs": "Analyze code against the WordPress coding standards with PHP_CodeSniffer", "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/" + } } } diff --git a/plugins/woocommerce-admin/composer.lock b/plugins/woocommerce-admin/composer.lock index 50811361dc2..6a39548b8d9 100644 --- a/plugins/woocommerce-admin/composer.lock +++ b/plugins/woocommerce-admin/composer.lock @@ -1,7 +1,7 @@ { "_readme": [ "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" ], "content-hash": "c4e5044bed13f99e535b3d692d12005c", @@ -1163,7 +1163,6 @@ "mock", "xunit" ], - "abandoned": true, "time": "2018-08-09T05:50:03+00:00" }, { @@ -1970,12 +1969,12 @@ "version": "1.2.1", "source": { "type": "git", - "url": "https://github.com/WordPress/WordPress-Coding-Standards.git", + "url": "https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards.git", "reference": "f328bcafd97377e8e5e5d7b244d5ddbf301a3a5c" }, "dist": { "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", "shasum": "" }, diff --git a/plugins/woocommerce-admin/docs/examples/extensions/add-report/woocommerce-admin-add-report-example.php b/plugins/woocommerce-admin/docs/examples/extensions/add-report/woocommerce-admin-add-report-example.php index cd04a4e6e32..d010165de17 100644 --- a/plugins/woocommerce-admin/docs/examples/extensions/add-report/woocommerce-admin-add-report-example.php +++ b/plugins/woocommerce-admin/docs/examples/extensions/add-report/woocommerce-admin-add-report-example.php @@ -10,7 +10,7 @@ */ 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; } diff --git a/plugins/woocommerce-admin/docs/examples/extensions/dashboard-section/woocommerce-admin-dashboard-section.php b/plugins/woocommerce-admin/docs/examples/extensions/dashboard-section/woocommerce-admin-dashboard-section.php index 8a30c43ad46..ad639b4a15a 100644 --- a/plugins/woocommerce-admin/docs/examples/extensions/dashboard-section/woocommerce-admin-dashboard-section.php +++ b/plugins/woocommerce-admin/docs/examples/extensions/dashboard-section/woocommerce-admin-dashboard-section.php @@ -10,7 +10,7 @@ */ 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; } diff --git a/plugins/woocommerce-admin/docs/examples/extensions/table-column/woocommerce-admin-table-column.php b/plugins/woocommerce-admin/docs/examples/extensions/table-column/woocommerce-admin-table-column.php index a77417451c2..b79a135e319 100644 --- a/plugins/woocommerce-admin/docs/examples/extensions/table-column/woocommerce-admin-table-column.php +++ b/plugins/woocommerce-admin/docs/examples/extensions/table-column/woocommerce-admin-table-column.php @@ -10,7 +10,7 @@ */ 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; } diff --git a/plugins/woocommerce-admin/docs/feature-flags.md b/plugins/woocommerce-admin/docs/feature-flags.md index 4b3b828d3c0..5996178efca 100644 --- a/plugins/woocommerce-admin/docs/feature-flags.md +++ b/plugins/woocommerce-admin/docs/feature-flags.md @@ -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. -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' ); } ``` diff --git a/plugins/woocommerce-admin/docs/page-controller.md b/plugins/woocommerce-admin/docs/page-controller.md index 4614e2ba828..2d41e227aeb 100644 --- a/plugins/woocommerce-admin/docs/page-controller.md +++ b/plugins/woocommerce-admin/docs/page-controller.md @@ -13,7 +13,7 @@ Connecting pages uses five parameters to `wc_admin_connect_page()`: * `id` - Identifies the page with the controller. Required. * `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. * `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 -* 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 [registering Analytics Reports](../includes/features/analytics/class-wc-admin-analytics.php#L75). diff --git a/plugins/woocommerce-admin/includes/class-wc-admin-api-init.php b/plugins/woocommerce-admin/includes/class-wc-admin-api-init.php deleted file mode 100644 index c76cdad92dc..00000000000 --- a/plugins/woocommerce-admin/includes/class-wc-admin-api-init.php +++ /dev/null @@ -1,429 +0,0 @@ -$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[\w-]+)'] ) - && isset( $endpoints['/wc/v4/system_status/tools/(?P[\w-]+)'][3] ) - && isset( $endpoints['/wc/v4/system_status/tools/(?P[\w-]+)'][2] ) - && $endpoints['/wc/v4/system_status/tools/(?P[\w-]+)'][2]['callback'][0] instanceof WC_Admin_REST_System_Status_Tools_Controller - && $endpoints['/wc/v4/system_status/tools/(?P[\w-]+)'][3]['callback'][0] instanceof WC_Admin_REST_System_Status_Tools_Controller - ) { - $endpoints['/wc/v4/system_status/tools/(?P[\w-]+)'][0] = $endpoints['/wc/v4/system_status/tools/(?P[\w-]+)'][2]; - $endpoints['/wc/v4/system_status/tools/(?P[\w-]+)'][1] = $endpoints['/wc/v4/system_status/tools/(?P[\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[\d]+)'] ) - && isset( $endpoints['/wc/v4/orders/(?P[\d]+)'][5] ) - && isset( $endpoints['/wc/v4/orders/(?P[\d]+)'][4] ) - && isset( $endpoints['/wc/v4/orders/(?P[\d]+)'][3] ) - && $endpoints['/wc/v4/orders/(?P[\d]+)'][3]['callback'][0] instanceof WC_Admin_REST_Orders_Controller - && $endpoints['/wc/v4/orders/(?P[\d]+)'][4]['callback'][0] instanceof WC_Admin_REST_Orders_Controller - && $endpoints['/wc/v4/orders/(?P[\d]+)'][5]['callback'][0] instanceof WC_Admin_REST_Orders_Controller - ) { - $endpoints['/wc/v4/orders/(?P[\d]+)'][0] = $endpoints['/wc/v4/orders/(?P[\d]+)'][3]; - $endpoints['/wc/v4/orders/(?P[\d]+)'][1] = $endpoints['/wc/v4/orders/(?P[\d]+)'][4]; - $endpoints['/wc/v4/orders/(?P[\d]+)'][2] = $endpoints['/wc/v4/orders/(?P[\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[\d]+)'] ) - && isset( $endpoints['/wc/v4/products/(?P[\d]+)'][5] ) - && isset( $endpoints['/wc/v4/products/(?P[\d]+)'][4] ) - && isset( $endpoints['/wc/v4/products/(?P[\d]+)'][3] ) - && $endpoints['/wc/v4/products/(?P[\d]+)'][3]['callback'][0] instanceof WC_Admin_REST_Products_Controller - && $endpoints['/wc/v4/products/(?P[\d]+)'][4]['callback'][0] instanceof WC_Admin_REST_Products_Controller - && $endpoints['/wc/v4/products/(?P[\d]+)'][5]['callback'][0] instanceof WC_Admin_REST_Products_Controller - ) { - $endpoints['/wc/v4/products/(?P[\d]+)'][0] = $endpoints['/wc/v4/products/(?P[\d]+)'][3]; - $endpoints['/wc/v4/products/(?P[\d]+)'][1] = $endpoints['/wc/v4/products/(?P[\d]+)'][4]; - $endpoints['/wc/v4/products/(?P[\d]+)'][2] = $endpoints['/wc/v4/products/(?P[\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[\d]+)/variations'] ) - && isset( $endpoints['products/(?P[\d]+)/variations'][3] ) - && isset( $endpoints['products/(?P[\d]+)/variations'][2] ) - && $endpoints['products/(?P[\d]+)/variations'][2]['callback'][0] instanceof WC_Admin_REST_Product_Variations_Controller - && $endpoints['products/(?P[\d]+)/variations'][3]['callback'][0] instanceof WC_Admin_REST_Product_Variations_Controller - ) { - $endpoints['products/(?P[\d]+)/variations'][0] = $endpoints['products/(?P[\d]+)/variations'][2]; - $endpoints['products/(?P[\d]+)/variations'][1] = $endpoints['products/(?P[\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[\w-]+)'] ) - && isset( $endpoints['/wc/v4/settings/(?P[\w-]+)'][5] ) - && isset( $endpoints['/wc/v4/settings/(?P[\w-]+)'][4] ) - && isset( $endpoints['/wc/v4/settings/(?P[\w-]+)'][3] ) - && $endpoints['/wc/v4/settings/(?P[\w-]+)'][3]['callback'][0] instanceof WC_Admin_REST_Setting_Options_Controller - && $endpoints['/wc/v4/settings/(?P[\w-]+)'][4]['callback'][0] instanceof WC_Admin_REST_Setting_Options_Controller - && $endpoints['/wc/v4/settings/(?P[\w-]+)'][5]['callback'][0] instanceof WC_Admin_REST_Setting_Options_Controller - ) { - $endpoints['/wc/v4/settings/(?P[\w-]+)'][0] = $endpoints['/wc/v4/settings/(?P[\w-]+)'][3]; - $endpoints['/wc/v4/settings/(?P[\w-]+)'][1] = $endpoints['/wc/v4/settings/(?P[\w-]+)'][4]; - $endpoints['/wc/v4/settings/(?P[\w-]+)'][2] = $endpoints['/wc/v4/settings/(?P[\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(); diff --git a/plugins/woocommerce-admin/includes/page-controller/connect-existing-pages.php b/plugins/woocommerce-admin/includes/connect-existing-pages.php similarity index 100% rename from plugins/woocommerce-admin/includes/page-controller/connect-existing-pages.php rename to plugins/woocommerce-admin/includes/connect-existing-pages.php diff --git a/plugins/woocommerce-admin/includes/core-functions.php b/plugins/woocommerce-admin/includes/core-functions.php index 930221dd9e9..72edb9d508f 100644 --- a/plugins/woocommerce-admin/includes/core-functions.php +++ b/plugins/woocommerce-admin/includes/core-functions.php @@ -9,6 +9,8 @@ defined( 'ABSPATH' ) || exit; +use \Automattic\WooCommerce\Admin\Loader; + /** * Format a number using the decimal and thousands separator settings in WooCommerce. * @@ -16,7 +18,7 @@ defined( 'ABSPATH' ) || exit; * @return string */ function wc_admin_number_format( $number ) { - $currency_settings = WC_Admin_Loader::get_currency_settings(); + $currency_settings = Loader::get_currency_settings(); return number_format( $number, 0, diff --git a/plugins/woocommerce-admin/includes/page-controller/page-controller-functions.php b/plugins/woocommerce-admin/includes/page-controller-functions.php similarity index 54% rename from plugins/woocommerce-admin/includes/page-controller/page-controller-functions.php rename to plugins/woocommerce-admin/includes/page-controller-functions.php index 1f55296953d..8d6d68bdb96 100644 --- a/plugins/woocommerce-admin/includes/page-controller/page-controller-functions.php +++ b/plugins/woocommerce-admin/includes/page-controller-functions.php @@ -1,61 +1,63 @@ connect_page( $options ); } /** * 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 ) { - $controller = WC_Admin_Page_Controller::get_instance(); + $controller = PageController::get_instance(); $controller->register_page( $options ); } /** * 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. */ function wc_admin_is_connected_page() { - $controller = WC_Admin_Page_Controller::get_instance(); + $controller = PageController::get_instance(); return $controller->is_connected_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. */ function wc_admin_is_registered_page() { - $controller = WC_Admin_Page_Controller::get_instance(); + $controller = PageController::get_instance(); return $controller->is_registered_page(); } /** * 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). */ function wc_admin_get_breadcrumbs() { - $controller = WC_Admin_Page_Controller::get_instance(); + $controller = PageController::get_instance(); return $controller->get_breadcrumbs(); } diff --git a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-coupons-controller.php b/plugins/woocommerce-admin/src/API/Coupons.php similarity index 95% rename from plugins/woocommerce-admin/includes/api/class-wc-admin-rest-coupons-controller.php rename to plugins/woocommerce-admin/src/API/Coupons.php index dfde5b3ae8a..5107077bf45 100644 --- a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-coupons-controller.php +++ b/plugins/woocommerce-admin/src/API/Coupons.php @@ -7,6 +7,8 @@ * @package WooCommerce Admin/API */ +namespace Automattic\WooCommerce\Admin\API; + defined( 'ABSPATH' ) || exit; /** @@ -15,7 +17,7 @@ defined( 'ABSPATH' ) || exit; * @package WooCommerce Admin/API * @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. diff --git a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-customers-controller.php b/plugins/woocommerce-admin/src/API/Customers.php similarity index 81% rename from plugins/woocommerce-admin/includes/api/class-wc-admin-rest-customers-controller.php rename to plugins/woocommerce-admin/src/API/Customers.php index d7cc898c812..563cda4a4f8 100644 --- a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-customers-controller.php +++ b/plugins/woocommerce-admin/src/API/Customers.php @@ -7,15 +7,17 @@ * @package WooCommerce Admin/API */ +namespace Automattic\WooCommerce\Admin\API; + defined( 'ABSPATH' ) || exit; /** * Customers controller. * * @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. diff --git a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-data-controller.php b/plugins/woocommerce-admin/src/API/Data.php similarity index 90% rename from plugins/woocommerce-admin/includes/api/class-wc-admin-rest-data-controller.php rename to plugins/woocommerce-admin/src/API/Data.php index d83006728ce..729ca36b073 100644 --- a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-data-controller.php +++ b/plugins/woocommerce-admin/src/API/Data.php @@ -7,6 +7,8 @@ * @package WooCommerce Admin/API */ +namespace Automattic\WooCommerce\Admin\API; + defined( 'ABSPATH' ) || exit; /** @@ -15,7 +17,7 @@ defined( 'ABSPATH' ) || exit; * @package WooCommerce Admin/API * @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. diff --git a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-data-countries-controller.php b/plugins/woocommerce-admin/src/API/DataCountries.php similarity index 78% rename from plugins/woocommerce-admin/includes/api/class-wc-admin-rest-data-countries-controller.php rename to plugins/woocommerce-admin/src/API/DataCountries.php index f4f7ea0bda8..4916f67cbd5 100644 --- a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-data-countries-controller.php +++ b/plugins/woocommerce-admin/src/API/DataCountries.php @@ -7,6 +7,8 @@ * @package WooCommerce Admin/API */ +namespace Automattic\WooCommerce\Admin\API; + defined( 'ABSPATH' ) || exit; /** @@ -15,7 +17,7 @@ defined( 'ABSPATH' ) || exit; * @package WooCommerce Admin/API * @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. diff --git a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-data-download-ips-controller.php b/plugins/woocommerce-admin/src/API/DataDownloadIPs.php similarity index 91% rename from plugins/woocommerce-admin/includes/api/class-wc-admin-rest-data-download-ips-controller.php rename to plugins/woocommerce-admin/src/API/DataDownloadIPs.php index 8f264f0d417..b1a5a2c0563 100644 --- a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-data-download-ips-controller.php +++ b/plugins/woocommerce-admin/src/API/DataDownloadIPs.php @@ -7,6 +7,8 @@ * @package WooCommerce Admin/API */ +namespace Automattic\WooCommerce\Admin\API; + defined( 'ABSPATH' ) || exit; /** @@ -15,7 +17,7 @@ defined( 'ABSPATH' ) || exit; * @package WooCommerce Admin/API * @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. * @@ -41,7 +43,7 @@ class WC_Admin_REST_Data_Download_Ips_Controller extends WC_REST_Data_Controller '/' . $this->rest_base, array( array( - 'methods' => WP_REST_Server::READABLE, + 'methods' => \WP_REST_Server::READABLE, 'callback' => array( $this, 'get_items' ), '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 { - 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(); diff --git a/plugins/woocommerce-admin/src/API/Init.php b/plugins/woocommerce-admin/src/API/Init.php new file mode 100644 index 00000000000..862084277c7 --- /dev/null +++ b/plugins/woocommerce-admin/src/API/Init.php @@ -0,0 +1,322 @@ +$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[\w-]+)'] ) + && isset( $endpoints['/wc/v4/system_status/tools/(?P[\w-]+)'][3] ) + && isset( $endpoints['/wc/v4/system_status/tools/(?P[\w-]+)'][2] ) + && $endpoints['/wc/v4/system_status/tools/(?P[\w-]+)'][2]['callback'][0] instanceof WC_Admin_REST_System_Status_Tools_Controller + && $endpoints['/wc/v4/system_status/tools/(?P[\w-]+)'][3]['callback'][0] instanceof WC_Admin_REST_System_Status_Tools_Controller + ) { + $endpoints['/wc/v4/system_status/tools/(?P[\w-]+)'][0] = $endpoints['/wc/v4/system_status/tools/(?P[\w-]+)'][2]; + $endpoints['/wc/v4/system_status/tools/(?P[\w-]+)'][1] = $endpoints['/wc/v4/system_status/tools/(?P[\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[\d]+)'] ) + && isset( $endpoints['/wc/v4/orders/(?P[\d]+)'][5] ) + && isset( $endpoints['/wc/v4/orders/(?P[\d]+)'][4] ) + && isset( $endpoints['/wc/v4/orders/(?P[\d]+)'][3] ) + && $endpoints['/wc/v4/orders/(?P[\d]+)'][3]['callback'][0] instanceof \Automattic\WooCommerce\Admin\API\Orders + && $endpoints['/wc/v4/orders/(?P[\d]+)'][4]['callback'][0] instanceof \Automattic\WooCommerce\Admin\API\Orders + && $endpoints['/wc/v4/orders/(?P[\d]+)'][5]['callback'][0] instanceof \Automattic\WooCommerce\Admin\API\Orders + ) { + $endpoints['/wc/v4/orders/(?P[\d]+)'][0] = $endpoints['/wc/v4/orders/(?P[\d]+)'][3]; + $endpoints['/wc/v4/orders/(?P[\d]+)'][1] = $endpoints['/wc/v4/orders/(?P[\d]+)'][4]; + $endpoints['/wc/v4/orders/(?P[\d]+)'][2] = $endpoints['/wc/v4/orders/(?P[\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[\d]+)'] ) + && isset( $endpoints['/wc/v4/products/(?P[\d]+)'][5] ) + && isset( $endpoints['/wc/v4/products/(?P[\d]+)'][4] ) + && isset( $endpoints['/wc/v4/products/(?P[\d]+)'][3] ) + && $endpoints['/wc/v4/products/(?P[\d]+)'][3]['callback'][0] instanceof \Automattic\WooCommerce\Admin\API\Products + && $endpoints['/wc/v4/products/(?P[\d]+)'][4]['callback'][0] instanceof \Automattic\WooCommerce\Admin\API\Products + && $endpoints['/wc/v4/products/(?P[\d]+)'][5]['callback'][0] instanceof \Automattic\WooCommerce\Admin\API\Products + ) { + $endpoints['/wc/v4/products/(?P[\d]+)'][0] = $endpoints['/wc/v4/products/(?P[\d]+)'][3]; + $endpoints['/wc/v4/products/(?P[\d]+)'][1] = $endpoints['/wc/v4/products/(?P[\d]+)'][4]; + $endpoints['/wc/v4/products/(?P[\d]+)'][2] = $endpoints['/wc/v4/products/(?P[\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[\d]+)/variations'] ) + && isset( $endpoints['products/(?P[\d]+)/variations'][3] ) + && isset( $endpoints['products/(?P[\d]+)/variations'][2] ) + && $endpoints['products/(?P[\d]+)/variations'][2]['callback'][0] instanceof \Automattic\WooCommerce\Admin\API\ProductVariations + && $endpoints['products/(?P[\d]+)/variations'][3]['callback'][0] instanceof \Automattic\WooCommerce\Admin\API\ProductVariations + ) { + $endpoints['products/(?P[\d]+)/variations'][0] = $endpoints['products/(?P[\d]+)/variations'][2]; + $endpoints['products/(?P[\d]+)/variations'][1] = $endpoints['products/(?P[\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[\w-]+)'] ) + && isset( $endpoints['/wc/v4/settings/(?P[\w-]+)'][5] ) + && isset( $endpoints['/wc/v4/settings/(?P[\w-]+)'][4] ) + && isset( $endpoints['/wc/v4/settings/(?P[\w-]+)'][3] ) + && $endpoints['/wc/v4/settings/(?P[\w-]+)'][3]['callback'][0] instanceof \Automattic\WooCommerce\Admin\API\SettingOptions + && $endpoints['/wc/v4/settings/(?P[\w-]+)'][4]['callback'][0] instanceof \Automattic\WooCommerce\Admin\API\SettingOptions + && $endpoints['/wc/v4/settings/(?P[\w-]+)'][5]['callback'][0] instanceof \Automattic\WooCommerce\Admin\API\SettingOptions + ) { + $endpoints['/wc/v4/settings/(?P[\w-]+)'][0] = $endpoints['/wc/v4/settings/(?P[\w-]+)'][3]; + $endpoints['/wc/v4/settings/(?P[\w-]+)'][1] = $endpoints['/wc/v4/settings/(?P[\w-]+)'][4]; + $endpoints['/wc/v4/settings/(?P[\w-]+)'][2] = $endpoints['/wc/v4/settings/(?P[\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; + } +} diff --git a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-leaderboards-controller.php b/plugins/woocommerce-admin/src/API/Leaderboards.php similarity index 95% rename from plugins/woocommerce-admin/includes/api/class-wc-admin-rest-leaderboards-controller.php rename to plugins/woocommerce-admin/src/API/Leaderboards.php index 514be3adb95..f09a763a36e 100644 --- a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-leaderboards-controller.php +++ b/plugins/woocommerce-admin/src/API/Leaderboards.php @@ -7,15 +7,22 @@ * @package WooCommerce Admin/API */ +namespace Automattic\WooCommerce\Admin\API; + 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. * * @package WooCommerce Admin/API * @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. * @@ -39,7 +46,7 @@ class WC_Admin_REST_Leaderboards_Controller extends WC_REST_Data_Controller { '/' . $this->rest_base, array( array( - 'methods' => WP_REST_Server::READABLE, + 'methods' => \WP_REST_Server::READABLE, 'callback' => array( $this, 'get_items' ), 'permission_callback' => array( $this, 'get_items_permissions_check' ), 'args' => $this->get_collection_params(), @@ -53,7 +60,7 @@ class WC_Admin_REST_Leaderboards_Controller extends WC_REST_Data_Controller { '/' . $this->rest_base . '/allowed', array( array( - 'methods' => WP_REST_Server::READABLE, + 'methods' => \WP_REST_Server::READABLE, 'callback' => array( $this, 'get_allowed_items' ), '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. */ 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( array( '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. */ 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( array( '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. */ 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( array( '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. */ 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( array( 'orderby' => 'items_sold', diff --git a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-admin-note-action-controller.php b/plugins/woocommerce-admin/src/API/NoteActions.php similarity index 93% rename from plugins/woocommerce-admin/includes/api/class-wc-admin-rest-admin-note-action-controller.php rename to plugins/woocommerce-admin/src/API/NoteActions.php index 9a7e84c0996..42c72770246 100644 --- a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-admin-note-action-controller.php +++ b/plugins/woocommerce-admin/src/API/NoteActions.php @@ -7,15 +7,19 @@ * @package WooCommerce Admin/API */ +namespace Automattic\WooCommerce\Admin\API; + defined( 'ABSPATH' ) || exit; +use \Automattic\WooCommerce\Admin\Notes\WC_Admin_Notes; + /** * REST API Admin Note Action controller class. * * @package WooCommerce/API * @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. @@ -36,7 +40,7 @@ class WC_Admin_REST_Admin_Note_Action_Controller extends WC_Admin_REST_Admin_Not ), ), array( - 'methods' => WP_REST_Server::EDITABLE, + 'methods' => \WP_REST_Server::EDITABLE, 'callback' => array( $this, 'trigger_note_action' ), // @todo - double check these permissions for taking note actions. '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' ) ); if ( ! $note ) { - return new WP_Error( + return new \WP_Error( 'woocommerce_admin_notes_invalid_id', __( 'Sorry, there is no resource with that ID.', 'woocommerce-admin' ), array( 'status' => 404 ) @@ -75,7 +79,7 @@ class WC_Admin_REST_Admin_Note_Action_Controller extends WC_Admin_REST_Admin_Not } if ( ! $triggered_action ) { - return new WP_Error( + return new \WP_Error( 'woocommerce_admin_note_action_invalid_id', __( 'Sorry, there is no resource with that ID.', 'woocommerce-admin' ), array( 'status' => 404 ) diff --git a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-admin-notes-controller.php b/plugins/woocommerce-admin/src/API/Notes.php similarity index 94% rename from plugins/woocommerce-admin/includes/api/class-wc-admin-rest-admin-notes-controller.php rename to plugins/woocommerce-admin/src/API/Notes.php index 46189e4aaf7..21bbbac03a0 100644 --- a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-admin-notes-controller.php +++ b/plugins/woocommerce-admin/src/API/Notes.php @@ -7,15 +7,20 @@ * @package WooCommerce Admin/API */ +namespace Automattic\WooCommerce\Admin\API; + 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. * * @package WooCommerce/API * @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. @@ -40,7 +45,7 @@ class WC_Admin_REST_Admin_Notes_Controller extends WC_REST_CRUD_Controller { '/' . $this->rest_base, array( array( - 'methods' => WP_REST_Server::READABLE, + 'methods' => \WP_REST_Server::READABLE, 'callback' => array( $this, 'get_items' ), 'permission_callback' => array( $this, 'get_items_permissions_check' ), 'args' => $this->get_collection_params(), @@ -60,12 +65,12 @@ class WC_Admin_REST_Admin_Notes_Controller extends WC_REST_CRUD_Controller { ), ), array( - 'methods' => WP_REST_Server::READABLE, + 'methods' => \WP_REST_Server::READABLE, 'callback' => array( $this, 'get_item' ), 'permission_callback' => array( $this, 'get_item_permissions_check' ), ), array( - 'methods' => WP_REST_Server::EDITABLE, + 'methods' => \WP_REST_Server::EDITABLE, 'callback' => array( $this, 'update_item' ), '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' ) ); if ( ! $note ) { - return new WP_Error( + return new \WP_Error( 'woocommerce_admin_notes_invalid_id', __( 'Sorry, there is no resource with that ID.', 'woocommerce-admin' ), 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 ) { 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; @@ -181,7 +186,7 @@ class WC_Admin_REST_Admin_Notes_Controller extends WC_REST_CRUD_Controller { */ public function get_items_permissions_check( $request ) { 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; @@ -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' ) ); if ( ! $note ) { - return new WP_Error( + return new \WP_Error( 'woocommerce_admin_notes_invalid_id', __( 'Sorry, there is no resource with that ID.', 'woocommerce-admin' ), 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 ) { 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; } diff --git a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-onboarding-levels-controller.php b/plugins/woocommerce-admin/src/API/OnboardingLevels.php similarity index 97% rename from plugins/woocommerce-admin/includes/api/class-wc-admin-rest-onboarding-levels-controller.php rename to plugins/woocommerce-admin/src/API/OnboardingLevels.php index 29622add12e..8a16c9bafdf 100644 --- a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-onboarding-levels-controller.php +++ b/plugins/woocommerce-admin/src/API/OnboardingLevels.php @@ -7,6 +7,8 @@ * @package WooCommerce Admin/API */ +namespace Automattic\WooCommerce\Admin\API; + defined( 'ABSPATH' ) || exit; /** @@ -15,7 +17,7 @@ defined( 'ABSPATH' ) || exit; * @package WooCommerce Admin/API * @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. * @@ -39,7 +41,7 @@ class WC_Admin_REST_Onboarding_Levels_Controller extends WC_REST_Data_Controller '/' . $this->rest_base, array( array( - 'methods' => WP_REST_Server::READABLE, + 'methods' => \WP_REST_Server::READABLE, 'callback' => array( $this, 'get_items' ), 'permission_callback' => array( $this, 'get_items_permissions_check' ), ), diff --git a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-onboarding-plugins-controller.php b/plugins/woocommerce-admin/src/API/OnboardingPlugins.php similarity index 86% rename from plugins/woocommerce-admin/includes/api/class-wc-admin-rest-onboarding-plugins-controller.php rename to plugins/woocommerce-admin/src/API/OnboardingPlugins.php index b222447b6f0..af0d28fecac 100644 --- a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-onboarding-plugins-controller.php +++ b/plugins/woocommerce-admin/src/API/OnboardingPlugins.php @@ -7,6 +7,8 @@ * @package WooCommerce Admin/API */ +namespace Automattic\WooCommerce\Admin\API; + defined( 'ABSPATH' ) || exit; /** @@ -15,7 +17,7 @@ defined( 'ABSPATH' ) || exit; * @package WooCommerce Admin/API * @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. * @@ -39,7 +41,7 @@ class WC_Admin_REST_Onboarding_Plugins_Controller extends WC_REST_Data_Controlle '/' . $this->rest_base . '/install', array( array( - 'methods' => WP_REST_Server::EDITABLE, + 'methods' => \WP_REST_Server::EDITABLE, 'callback' => array( $this, 'install_plugin' ), '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', array( array( - 'methods' => WP_REST_Server::EDITABLE, + 'methods' => \WP_REST_Server::EDITABLE, 'callback' => array( $this, 'activate_plugin' ), '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', array( array( - 'methods' => WP_REST_Server::READABLE, + 'methods' => \WP_REST_Server::READABLE, 'callback' => array( $this, 'connect_jetpack' ), '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 ) { 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; } @@ -136,7 +138,7 @@ class WC_Admin_REST_Onboarding_Plugins_Controller extends WC_REST_Data_Controlle $allowed_plugins = $this->get_allowed_plugins(); $plugin = sanitize_title_with_dashes( $request['plugin'] ); 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'; @@ -170,14 +172,14 @@ class WC_Admin_REST_Onboarding_Plugins_Controller extends WC_REST_Data_Controlle ); 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 ); 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( @@ -197,7 +199,7 @@ class WC_Admin_REST_Onboarding_Plugins_Controller extends WC_REST_Data_Controlle $allowed_plugins = $this->get_allowed_plugins(); $plugin = sanitize_title_with_dashes( $request['plugin'] ); 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'; @@ -207,12 +209,12 @@ class WC_Admin_REST_Onboarding_Plugins_Controller extends WC_REST_Data_Controlle $installed_plugins = get_plugins(); 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 ); 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( @@ -229,7 +231,7 @@ class WC_Admin_REST_Onboarding_Plugins_Controller extends WC_REST_Data_Controlle */ public function connect_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' ); @@ -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. 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' ); - $request = WC_Helper_API::post( + $request = \WC_Helper_API::post( 'oauth/request_token', array( 'body' => array( @@ -304,7 +306,7 @@ class WC_Admin_REST_Onboarding_Plugins_Controller extends WC_REST_Data_Controlle 'secret' => rawurlencode( $secret ), 'wccom-from' => 'onboarding', ), - WC_Helper_API::url( 'oauth/authorize' ) + \WC_Helper_API::url( 'oauth/authorize' ) ); // 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. - $request = WC_Helper_API::post( + $request = \WC_Helper_API::post( 'oauth/access_token', 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 ); } - WC_Helper_Options::update( + \WC_Helper_Options::update( 'auth', array( '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() ) { - WC_Helper_Options::update( 'auth', array() ); + if ( ! \WC_Helper::_flush_authentication_cache() ) { + \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 ); } delete_transient( '_woocommerce_helper_subscriptions' ); - WC_Helper_Updater::flush_updates_cache(); + \WC_Helper_Updater::flush_updates_cache(); do_action( 'woocommerce_helper_connected' ); diff --git a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-onboarding-profile-controller.php b/plugins/woocommerce-admin/src/API/OnboardingProfile.php similarity index 93% rename from plugins/woocommerce-admin/includes/api/class-wc-admin-rest-onboarding-profile-controller.php rename to plugins/woocommerce-admin/src/API/OnboardingProfile.php index 2633bccf08a..9977e4d45c9 100644 --- a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-onboarding-profile-controller.php +++ b/plugins/woocommerce-admin/src/API/OnboardingProfile.php @@ -7,15 +7,19 @@ * @package WooCommerce Admin/API */ +namespace Automattic\WooCommerce\Admin\API; + defined( 'ABSPATH' ) || exit; +use Automattic\WooCommerce\Admin\Features\Onboarding; + /** * Onboarding Profile controller. * * @package WooCommerce Admin/API * @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. * @@ -39,7 +43,7 @@ class WC_Admin_REST_Onboarding_Profile_Controller extends WC_REST_Data_Controlle '/' . $this->rest_base, array( array( - 'methods' => WP_REST_Server::READABLE, + 'methods' => \WP_REST_Server::READABLE, 'callback' => array( $this, 'get_items' ), '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, array( array( - 'methods' => WP_REST_Server::EDITABLE, + 'methods' => \WP_REST_Server::EDITABLE, 'callback' => array( $this, 'update_items' ), 'permission_callback' => array( $this, 'update_items_permissions_check' ), '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 ) { 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; @@ -83,7 +87,7 @@ class WC_Admin_REST_Onboarding_Profile_Controller extends WC_REST_Data_Controlle */ public function update_items_permissions_check( $request ) { 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; @@ -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; } - $wccom_auth = WC_Helper_Options::get( 'auth' ); + $wccom_auth = \WC_Helper_Options::get( 'auth' ); $items['wccom_connected'] = empty( $wccom_auth['access_token'] ) ? false : true; $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', 'validate_callback' => 'rest_validate_request_arg', 'items' => array( - 'enum' => array_keys( WC_Admin_Onboarding::get_allowed_industries() ), + 'enum' => array_keys( Onboarding::get_allowed_industries() ), 'type' => 'string', ), ), @@ -244,7 +248,7 @@ class WC_Admin_REST_Onboarding_Profile_Controller extends WC_REST_Data_Controlle 'sanitize_callback' => 'wp_parse_slug_list', 'validate_callback' => 'rest_validate_request_arg', 'items' => array( - 'enum' => array_keys( WC_Admin_Onboarding::get_allowed_product_types() ), + 'enum' => array_keys( Onboarding::get_allowed_product_types() ), 'type' => 'string', ), ), diff --git a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-orders-controller.php b/plugins/woocommerce-admin/src/API/Orders.php similarity index 95% rename from plugins/woocommerce-admin/includes/api/class-wc-admin-rest-orders-controller.php rename to plugins/woocommerce-admin/src/API/Orders.php index e9f13eb08f3..e358202b407 100644 --- a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-orders-controller.php +++ b/plugins/woocommerce-admin/src/API/Orders.php @@ -7,6 +7,8 @@ * @package WooCommerce Admin/API */ +namespace Automattic\WooCommerce\Admin\API; + defined( 'ABSPATH' ) || exit; /** @@ -15,7 +17,7 @@ defined( 'ABSPATH' ) || exit; * @package WooCommerce Admin/API * @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. * diff --git a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-product-categories-controller.php b/plugins/woocommerce-admin/src/API/ProductCategories.php similarity index 76% rename from plugins/woocommerce-admin/includes/api/class-wc-admin-rest-product-categories-controller.php rename to plugins/woocommerce-admin/src/API/ProductCategories.php index 672cd0c22ca..efa01a517bc 100644 --- a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-product-categories-controller.php +++ b/plugins/woocommerce-admin/src/API/ProductCategories.php @@ -7,6 +7,8 @@ * @package WooCommerce Admin/API */ +namespace Automattic\WooCommerce\Admin\API; + defined( 'ABSPATH' ) || exit; /** @@ -15,7 +17,7 @@ defined( 'ABSPATH' ) || exit; * @package WooCommerce Admin/API * @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. * diff --git a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-product-reviews-controller.php b/plugins/woocommerce-admin/src/API/ProductReviews.php similarity index 91% rename from plugins/woocommerce-admin/includes/api/class-wc-admin-rest-product-reviews-controller.php rename to plugins/woocommerce-admin/src/API/ProductReviews.php index ba3fc9245f4..7ce0a811c25 100644 --- a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-product-reviews-controller.php +++ b/plugins/woocommerce-admin/src/API/ProductReviews.php @@ -7,6 +7,8 @@ * @package WooCommerce Admin/API */ +namespace Automattic\WooCommerce\Admin\API; + defined( 'ABSPATH' ) || exit; /** @@ -15,7 +17,7 @@ defined( 'ABSPATH' ) || exit; * @package WooCommerce Admin/API * @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. * diff --git a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-product-variations-controller.php b/plugins/woocommerce-admin/src/API/ProductVariations.php similarity index 78% rename from plugins/woocommerce-admin/includes/api/class-wc-admin-rest-product-variations-controller.php rename to plugins/woocommerce-admin/src/API/ProductVariations.php index 92f4d866a28..965be5f6088 100644 --- a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-product-variations-controller.php +++ b/plugins/woocommerce-admin/src/API/ProductVariations.php @@ -7,6 +7,8 @@ * @package WooCommerce Admin/API */ +namespace Automattic\WooCommerce\Admin\API; + defined( 'ABSPATH' ) || exit; /** @@ -15,7 +17,7 @@ defined( 'ABSPATH' ) || exit; * @package WooCommerce Admin/API * @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. * @@ -62,13 +64,13 @@ class WC_Admin_REST_Product_Variations_Controller extends WC_REST_Product_Variat * @return WP_Error|WP_REST_Response */ public function get_items( $request ) { - add_filter( 'posts_where', array( 'WC_Admin_REST_Products_Controller', '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_groupby', array( 'WC_Admin_REST_Products_Controller', 'add_wp_query_group_by' ), 10, 2 ); + add_filter( 'posts_where', array( 'Automattic\WooCommerce\Admin\API\Products', 'add_wp_query_filter' ), 10, 2 ); + add_filter( 'posts_join', array( 'Automattic\WooCommerce\Admin\API\Products', 'add_wp_query_join' ), 10, 2 ); + add_filter( 'posts_groupby', array( 'Automattic\WooCommerce\Admin\API\Products', 'add_wp_query_group_by' ), 10, 2 ); $response = parent::get_items( $request ); - remove_filter( 'posts_where', array( 'WC_Admin_REST_Products_Controller', 'add_wp_query_filter' ), 10 ); - remove_filter( 'posts_join', array( 'WC_Admin_REST_Products_Controller', 'add_wp_query_join' ), 10 ); - remove_filter( 'posts_groupby', array( 'WC_Admin_REST_Products_Controller', 'add_wp_query_group_by' ), 10 ); + remove_filter( 'posts_where', array( 'Automattic\WooCommerce\Admin\API\Products', 'add_wp_query_filter' ), 10 ); + remove_filter( 'posts_join', array( 'Automattic\WooCommerce\Admin\API\Products', 'add_wp_query_join' ), 10 ); + remove_filter( 'posts_groupby', array( 'Automattic\WooCommerce\Admin\API\Products', 'add_wp_query_group_by' ), 10 ); return $response; } diff --git a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-products-controller.php b/plugins/woocommerce-admin/src/API/Products.php similarity index 98% rename from plugins/woocommerce-admin/includes/api/class-wc-admin-rest-products-controller.php rename to plugins/woocommerce-admin/src/API/Products.php index 1ed532c6320..2c3b2363905 100644 --- a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-products-controller.php +++ b/plugins/woocommerce-admin/src/API/Products.php @@ -7,6 +7,8 @@ * @package WooCommerce Admin/API */ +namespace Automattic\WooCommerce\Admin\API; + defined( 'ABSPATH' ) || exit; /** @@ -15,7 +17,7 @@ defined( 'ABSPATH' ) || exit; * @package WooCommerce Admin/API * @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. diff --git a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-categories-controller.php b/plugins/woocommerce-admin/src/API/Reports/Categories/Controller.php similarity index 95% rename from plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-categories-controller.php rename to plugins/woocommerce-admin/src/API/Reports/Categories/Controller.php index 9d8161151db..847c2f0c134 100644 --- a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-categories-controller.php +++ b/plugins/woocommerce-admin/src/API/Reports/Categories/Controller.php @@ -7,15 +7,17 @@ * @package WooCommerce Admin/API */ +namespace Automattic\WooCommerce\Admin\API\Reports\Categories; + defined( 'ABSPATH' ) || exit; /** * REST API Reports categories controller class. * * @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. @@ -62,7 +64,7 @@ class WC_Admin_REST_Reports_Categories_Controller extends WC_Admin_REST_Reports_ */ public function get_items( $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(); 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 ) ) { - 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(); @@ -137,7 +139,7 @@ class WC_Admin_REST_Reports_Categories_Controller extends WC_Admin_REST_Reports_ /** * 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 */ protected function prepare_links( $object ) { diff --git a/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-categories-data-store.php b/plugins/woocommerce-admin/src/API/Reports/Categories/DataStore.php similarity index 93% rename from plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-categories-data-store.php rename to plugins/woocommerce-admin/src/API/Reports/Categories/DataStore.php index 4be8258d87c..bc17c97690e 100644 --- a/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-categories-data-store.php +++ b/plugins/woocommerce-admin/src/API/Reports/Categories/DataStore.php @@ -1,17 +1,22 @@ $category_data ) { - $extended_info = new ArrayObject(); + $extended_info = new \ArrayObject(); if ( $query_args['extended_info'] ) { $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, 'order' => 'DESC', 'orderby' => 'date', - 'before' => WC_Admin_Reports_Interval::default_before(), - 'after' => WC_Admin_Reports_Interval::default_after(), + 'before' => TimeInterval::default_before(), + 'after' => TimeInterval::default_after(), 'fields' => '*', 'categories' => array(), '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. 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 ); diff --git a/plugins/woocommerce-admin/includes/class-wc-admin-reports-categories-query.php b/plugins/woocommerce-admin/src/API/Reports/Categories/Query.php similarity index 73% rename from plugins/woocommerce-admin/includes/class-wc-admin-reports-categories-query.php rename to plugins/woocommerce-admin/src/API/Reports/Categories/Query.php index 3a96fe7647f..843007e5d60 100644 --- a/plugins/woocommerce-admin/includes/class-wc-admin-reports-categories-query.php +++ b/plugins/woocommerce-admin/src/API/Reports/Categories/Query.php @@ -10,18 +10,22 @@ * 'order' => 'desc', * 'orderby' => 'items_sold', * ); - * $report = new WC_Admin_Reports_Categories_Query( $args ); + * $report = new \Automattic\WooCommerce\Admin\API\Reports\Query( $args ); * $mydata = $report->get_data(); * * @package WooCommerce Admin/Classes */ +namespace Automattic\WooCommerce\Admin\API\Reports\Categories; + 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'; @@ -41,7 +45,7 @@ class WC_Admin_Reports_Categories_Query extends WC_Admin_Reports_Query { */ public function get_data() { $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 ); } diff --git a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-controller.php b/plugins/woocommerce-admin/src/API/Reports/Controller.php similarity index 95% rename from plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-controller.php rename to plugins/woocommerce-admin/src/API/Reports/Controller.php index 443e149c5c0..04748c7d31d 100644 --- a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-controller.php +++ b/plugins/woocommerce-admin/src/API/Reports/Controller.php @@ -7,6 +7,8 @@ * @package WooCommerce Admin/API */ +namespace Automattic\WooCommerce\Admin\API\Reports; + defined( 'ABSPATH' ) || exit; /** @@ -15,7 +17,7 @@ defined( 'ABSPATH' ) || exit; * @package WooCommerce Admin/API * @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. @@ -40,7 +42,7 @@ class WC_Admin_REST_Reports_Controller extends WC_REST_Reports_Controller { '/' . $this->rest_base, array( array( - 'methods' => WP_REST_Server::READABLE, + 'methods' => \WP_REST_Server::READABLE, 'callback' => array( $this, 'get_items' ), 'permission_callback' => array( $this, 'get_items_permissions_check' ), '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 ) { 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; diff --git a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-coupons-controller.php b/plugins/woocommerce-admin/src/API/Reports/Coupons/Controller.php similarity index 98% rename from plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-coupons-controller.php rename to plugins/woocommerce-admin/src/API/Reports/Coupons/Controller.php index 20a7f1020af..6ac18561368 100644 --- a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-coupons-controller.php +++ b/plugins/woocommerce-admin/src/API/Reports/Coupons/Controller.php @@ -7,6 +7,8 @@ * @package WooCommerce Admin/API */ +namespace Automattic\WooCommerce\Admin\API\Reports\Coupons; + defined( 'ABSPATH' ) || exit; /** @@ -15,7 +17,7 @@ defined( 'ABSPATH' ) || exit; * @package WooCommerce/API * @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. @@ -58,7 +60,7 @@ class WC_Admin_REST_Reports_Coupons_Controller extends WC_REST_Reports_Controlle */ public function get_items( $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(); $data = array(); diff --git a/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-coupons-data-store.php b/plugins/woocommerce-admin/src/API/Reports/Coupons/DataStore.php similarity index 92% rename from plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-coupons-data-store.php rename to plugins/woocommerce-admin/src/API/Reports/Coupons/DataStore.php index 4165a982a71..4c5d1266bac 100644 --- a/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-coupons-data-store.php +++ b/plugins/woocommerce-admin/src/API/Reports/Coupons/DataStore.php @@ -1,16 +1,22 @@ $coupon_datum ) { - $extended_info = new ArrayObject(); + $extended_info = new \ArrayObject(); if ( $query_args['extended_info'] ) { $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(); if ( null === $date_expires ) { $date_expires = ''; $date_expires_gmt = ''; } else { - $date_expires = $date_expires->format( WC_Admin_Reports_Interval::$iso_datetime_format ); - $date_expires_gmt = new DateTime( $date_expires ); + $date_expires = $date_expires->format( TimeInterval::$iso_datetime_format ); + $date_expires_gmt = new \DateTime( $date_expires ); $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(); @@ -193,10 +199,10 @@ class WC_Admin_Reports_Coupons_Data_Store extends WC_Admin_Reports_Data_Store im $date_created = ''; $date_created_gmt = ''; } else { - $date_created = $date_created->format( WC_Admin_Reports_Interval::$iso_datetime_format ); - $date_created_gmt = new DateTime( $date_created ); + $date_created = $date_created->format( TimeInterval::$iso_datetime_format ); + $date_created_gmt = new \DateTime( $date_created ); $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( @@ -229,8 +235,8 @@ class WC_Admin_Reports_Coupons_Data_Store extends WC_Admin_Reports_Data_Store im 'page' => 1, 'order' => 'DESC', 'orderby' => 'coupon_id', - 'before' => WC_Admin_Reports_Interval::default_before(), - 'after' => WC_Admin_Reports_Interval::default_after(), + 'before' => TimeInterval::default_before(), + 'after' => TimeInterval::default_after(), 'fields' => '*', 'coupons' => array(), '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, 'coupon_id' => $coupon_id, '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( '%d', diff --git a/plugins/woocommerce-admin/includes/class-wc-admin-reports-coupons-query.php b/plugins/woocommerce-admin/src/API/Reports/Coupons/Query.php similarity index 72% rename from plugins/woocommerce-admin/includes/class-wc-admin-reports-coupons-query.php rename to plugins/woocommerce-admin/src/API/Reports/Coupons/Query.php index 570c3a01013..ca66924accb 100644 --- a/plugins/woocommerce-admin/includes/class-wc-admin-reports-coupons-query.php +++ b/plugins/woocommerce-admin/src/API/Reports/Coupons/Query.php @@ -9,18 +9,22 @@ * 'page' => 2, * '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(); * * @package WooCommerce Admin/Classes */ +namespace Automattic\WooCommerce\Admin\API\Reports\Coupons; + 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. @@ -39,7 +43,7 @@ class WC_Admin_Reports_Coupons_Query extends WC_Admin_Reports_Query { public function get_data() { $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 ); return apply_filters( 'woocommerce_reports_coupons_select_query', $results, $args ); } diff --git a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-coupons-stats-controller.php b/plugins/woocommerce-admin/src/API/Reports/Coupons/Stats/Controller.php similarity index 96% rename from plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-coupons-stats-controller.php rename to plugins/woocommerce-admin/src/API/Reports/Coupons/Stats/Controller.php index fbc4b6383a6..355b9bdea17 100644 --- a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-coupons-stats-controller.php +++ b/plugins/woocommerce-admin/src/API/Reports/Coupons/Stats/Controller.php @@ -7,15 +7,19 @@ * @package WooCommerce Admin/API */ +namespace Automattic\WooCommerce\Admin\API\Reports\Coupons\Stats; + defined( 'ABSPATH' ) || exit; +use \Automattic\WooCommerce\Admin\API\Reports\ParameterException; + /** * REST API Reports coupons stats controller class. * * @package WooCommerce/API * @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. @@ -61,11 +65,11 @@ class WC_Admin_REST_Reports_Coupons_Stats_Controller extends WC_REST_Reports_Con */ public function get_items( $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 { $report_data = $coupons_query->get_data(); - } catch ( WC_Admin_Reports_Parameter_Exception $e ) { - return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); + } catch ( ParameterException $e ) { + return new \WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); } $out_data = array( diff --git a/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-coupons-stats-data-store.php b/plugins/woocommerce-admin/src/API/Reports/Coupons/Stats/DataStore.php similarity index 88% rename from plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-coupons-stats-data-store.php rename to plugins/woocommerce-admin/src/API/Reports/Coupons/Stats/DataStore.php index 9925ad7881f..8f441acac23 100644 --- a/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-coupons-stats-data-store.php +++ b/plugins/woocommerce-admin/src/API/Reports/Coupons/Stats/DataStore.php @@ -1,18 +1,21 @@ 1, 'order' => 'DESC', 'orderby' => 'date', - 'before' => WC_Admin_Reports_Interval::default_before(), - 'after' => WC_Admin_Reports_Interval::default_after(), + 'before' => TimeInterval::default_before(), + 'after' => TimeInterval::default_after(), 'fields' => '*', 'interval' => 'week', '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. $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'] ); if ( $query_args['page'] < 1 || $query_args['page'] > $total_pages ) { return $data; @@ -164,7 +167,7 @@ class WC_Admin_Reports_Coupons_Stats_Data_Store extends WC_Admin_Reports_Coupons if ( null === $totals ) { 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 = (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'], ); - 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->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'] ); diff --git a/plugins/woocommerce-admin/includes/class-wc-admin-reports-coupons-stats-query.php b/plugins/woocommerce-admin/src/API/Reports/Coupons/Stats/Query.php similarity index 71% rename from plugins/woocommerce-admin/includes/class-wc-admin-reports-coupons-stats-query.php rename to plugins/woocommerce-admin/src/API/Reports/Coupons/Stats/Query.php index 4dd05a81ccd..597019ded6a 100644 --- a/plugins/woocommerce-admin/includes/class-wc-admin-reports-coupons-stats-query.php +++ b/plugins/woocommerce-admin/src/API/Reports/Coupons/Stats/Query.php @@ -9,18 +9,22 @@ * 'page' => 2, * '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(); * * @package WooCommerce Admin/Classes */ +namespace Automattic\WooCommerce\Admin\API\Reports\Coupons\Stats; + 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. @@ -39,7 +43,7 @@ class WC_Admin_Reports_Coupons_Stats_Query extends WC_Admin_Reports_Query { public function get_data() { $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 ); return apply_filters( 'woocommerce_reports_coupons_select_query', $results, $args ); } diff --git a/plugins/woocommerce-admin/includes/class-wc-admin-reports-coupons-stats-segmenting.php b/plugins/woocommerce-admin/src/API/Reports/Coupons/Stats/Segmenter.php similarity index 95% rename from plugins/woocommerce-admin/includes/class-wc-admin-reports-coupons-stats-segmenting.php rename to plugins/woocommerce-admin/src/API/Reports/Coupons/Stats/Segmenter.php index 43c1f2e6ee2..b8ad220fcdd 100644 --- a/plugins/woocommerce-admin/includes/class-wc-admin-reports-coupons-stats-segmenting.php +++ b/plugins/woocommerce-admin/src/API/Reports/Coupons/Stats/Segmenter.php @@ -5,12 +5,17 @@ * @package WooCommerce Admin/Classes */ +namespace Automattic\WooCommerce\Admin\API\Reports\Coupons\Stats; + 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. */ -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). @@ -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). * * @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 ) { 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 ); } elseif ( 'variation' === $this->query_args['segmentby'] ) { 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( diff --git a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-customers-controller.php b/plugins/woocommerce-admin/src/API/Reports/Customers/Controller.php similarity index 95% rename from plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-customers-controller.php rename to plugins/woocommerce-admin/src/API/Reports/Customers/Controller.php index fc7ec557173..681c8946f9a 100644 --- a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-customers-controller.php +++ b/plugins/woocommerce-admin/src/API/Reports/Customers/Controller.php @@ -7,15 +7,19 @@ * @package WooCommerce Admin/API */ +namespace Automattic\WooCommerce\Admin\API\Reports\Customers; + defined( 'ABSPATH' ) || exit; +use \Automattic\WooCommerce\Admin\API\Reports\TimeInterval; + /** * REST API Reports customers controller class. * * @package WooCommerce/API * @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. @@ -71,9 +75,9 @@ class WC_Admin_REST_Reports_Customers_Controller extends WC_REST_Reports_Control $args['customers'] = $request['customers']; $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' ); - $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 ); return $args; @@ -87,7 +91,7 @@ class WC_Admin_REST_Reports_Customers_Controller extends WC_REST_Reports_Control */ public function get_items( $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(); $data = array(); @@ -431,7 +435,7 @@ class WC_Admin_REST_Reports_Customers_Controller extends WC_REST_Reports_Control $params['last_active_between'] = array( 'description' => __( 'Limit response to objects last active between two given ISO8601 compliant datetime.', 'woocommerce-admin' ), '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( '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( 'description' => __( 'Limit response to objects last active between two given ISO8601 compliant datetime.', 'woocommerce-admin' ), '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( '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( 'description' => __( 'Limit response to objects with an order count between two given integers.', 'woocommerce-admin' ), '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( '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( 'description' => __( 'Limit response to objects with a total order spend between two given numbers.', 'woocommerce-admin' ), '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( '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( 'description' => __( 'Limit response to objects with an average order spend between two given numbers.', 'woocommerce-admin' ), '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( 'description' => __( 'Limit response to objects with last order before (or at) a given ISO8601 compliant datetime.', 'woocommerce-admin' ), diff --git a/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-customers-data-store.php b/plugins/woocommerce-admin/src/API/Reports/Customers/DataStore.php similarity index 95% rename from plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-customers-data-store.php rename to plugins/woocommerce-admin/src/API/Reports/Customers/DataStore.php index 34edecac6f0..b4e6e85cf62 100644 --- a/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-customers-data-store.php +++ b/plugins/woocommerce-admin/src/API/Reports/Customers/DataStore.php @@ -1,16 +1,22 @@ format( WC_Admin_Reports_Interval::$sql_datetime_format ); + $datetime = new \DateTime( $query_args[ $before_arg ] ); + $datetime_str = $datetime->format( TimeInterval::$sql_datetime_format ); $subclauses[] = "{$column_name} <= '$datetime_str'"; } if ( ! empty( $query_args[ $after_arg ] ) ) { - $datetime = new DateTime( $query_args[ $after_arg ] ); - $datetime_str = $datetime->format( WC_Admin_Reports_Interval::$sql_datetime_format ); + $datetime = new \DateTime( $query_args[ $after_arg ] ); + $datetime_str = $datetime->format( TimeInterval::$sql_datetime_format ); $subclauses[] = "{$column_name} >= '$datetime_str'"; } @@ -347,8 +353,8 @@ class WC_Admin_Reports_Customers_Data_Store extends WC_Admin_Reports_Data_Store 'page' => 1, 'order' => 'DESC', 'orderby' => 'date_registered', - 'order_before' => WC_Admin_Reports_Interval::default_before(), - 'order_after' => WC_Admin_Reports_Interval::default_after(), + 'order_before' => TimeInterval::default_before(), + 'order_after' => TimeInterval::default_after(), 'fields' => '*', ); $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. if ( 0 !== $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['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[] = '%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 ) { global $wpdb; - $customer = new WC_Customer( $user_id ); + $customer = new \WC_Customer( $user_id ); if ( ! self::is_valid_customer( $user_id ) ) { 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' ), 'postcode' => $customer->get_billing_postcode( '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, ); $format = array( @@ -680,7 +686,7 @@ class WC_Admin_Reports_Customers_Data_Store extends WC_Admin_Reports_Data_Store * @return bool */ 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 ) ) { return false; diff --git a/plugins/woocommerce-admin/includes/class-wc-admin-reports-customers-query.php b/plugins/woocommerce-admin/src/API/Reports/Customers/Query.php similarity index 77% rename from plugins/woocommerce-admin/includes/class-wc-admin-reports-customers-query.php rename to plugins/woocommerce-admin/src/API/Reports/Customers/Query.php index 937a0bf7b42..c510c57dd18 100644 --- a/plugins/woocommerce-admin/includes/class-wc-admin-reports-customers-query.php +++ b/plugins/woocommerce-admin/src/API/Reports/Customers/Query.php @@ -10,18 +10,22 @@ * 'avg_order_value_min' => 100, * 'country' => 'GB', * ); - * $report = new WC_Admin_Reports_Customers_Query( $args ); + * $report = new \Automattic\WooCommerce\Admin\API\Reports\Customers\Query( $args ); * $mydata = $report->get_data(); * * @package WooCommerce Admin/Classes */ +namespace Automattic\WooCommerce\Admin\API\Reports\Customers; + 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. @@ -46,7 +50,7 @@ class WC_Admin_Reports_Customers_Query extends WC_Admin_Reports_Query { public function get_data() { $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 ); return apply_filters( 'woocommerce_reports_customers_select_query', $results, $args ); } diff --git a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-customers-stats-controller.php b/plugins/woocommerce-admin/src/API/Reports/Customers/Stats/Controller.php similarity index 93% rename from plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-customers-stats-controller.php rename to plugins/woocommerce-admin/src/API/Reports/Customers/Stats/Controller.php index 0012807e565..8c57d582807 100644 --- a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-customers-stats-controller.php +++ b/plugins/woocommerce-admin/src/API/Reports/Customers/Stats/Controller.php @@ -7,15 +7,19 @@ * @package WooCommerce Admin/API */ +namespace Automattic\WooCommerce\Admin\API\Reports\Customers\Stats; + defined( 'ABSPATH' ) || exit; +use \Automattic\WooCommerce\Admin\API\Reports\TimeInterval; + /** * REST API Reports customers stats controller class. * * @package WooCommerce/API * @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. * @@ -63,9 +67,9 @@ class WC_Admin_REST_Reports_Customers_Stats_Controller extends WC_REST_Reports_C $args['customers'] = $request['customers']; $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' ); - $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 ); return $args; @@ -79,7 +83,7 @@ class WC_Admin_REST_Reports_Customers_Stats_Controller extends WC_REST_Reports_C */ public function get_items( $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(); $out_data = array( '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( 'description' => __( 'Limit response to objects last active between two given ISO8601 compliant datetime.', 'woocommerce-admin' ), '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( '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( 'description' => __( 'Limit response to objects last active between two given ISO8601 compliant datetime.', 'woocommerce-admin' ), '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( '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( 'description' => __( 'Limit response to objects with an order count between two given integers.', 'woocommerce-admin' ), '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( '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( 'description' => __( 'Limit response to objects with a total order spend between two given numbers.', 'woocommerce-admin' ), '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( '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( 'description' => __( 'Limit response to objects with an average order spend between two given numbers.', 'woocommerce-admin' ), '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( 'description' => __( 'Limit response to objects with last order before (or at) a given ISO8601 compliant datetime.', 'woocommerce-admin' ), diff --git a/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-customers-stats-data-store.php b/plugins/woocommerce-admin/src/API/Reports/Customers/Stats/DataStore.php similarity index 89% rename from plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-customers-stats-data-store.php rename to plugins/woocommerce-admin/src/API/Reports/Customers/Stats/DataStore.php index 2feb0d98076..2457b4cb3ab 100644 --- a/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-customers-stats-data-store.php +++ b/plugins/woocommerce-admin/src/API/Reports/Customers/Stats/DataStore.php @@ -1,16 +1,21 @@ 100, * '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(); * * @package WooCommerce Admin/Classes */ +namespace Automattic\WooCommerce\Admin\API\Reports\Customers\Stats; + 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. @@ -46,7 +50,7 @@ class WC_Admin_Reports_Customers_Stats_Query extends WC_Admin_Reports_Query { public function get_data() { $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 ); return apply_filters( 'woocommerce_reports_customers_stats_select_query', $results, $args ); } diff --git a/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-data-store.php b/plugins/woocommerce-admin/src/API/Reports/DataStore.php similarity index 93% rename from plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-data-store.php rename to plugins/woocommerce-admin/src/API/Reports/DataStore.php index f38036c261c..46da2696846 100644 --- a/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-data-store.php +++ b/plugins/woocommerce-admin/src/API/Reports/DataStore.php @@ -1,18 +1,22 @@ intervals, 'time_interval' ) ); $db_intervals = array_flip( $db_intervals ); @@ -142,14 +146,14 @@ class WC_Admin_Reports_Data_Store { // @todo Should 'products' be in intervals? unset( $totals_arr['products'] ); while ( $start_datetime <= $end_datetime ) { - $next_start = WC_Admin_Reports_Interval::iterate( $start_datetime, $time_interval ); - $time_id = WC_Admin_Reports_Interval::time_interval_id( $time_interval, $start_datetime ); + $next_start = TimeInterval::iterate( $start_datetime, $time_interval ); + $time_id = TimeInterval::time_interval_id( $time_interval, $start_datetime ); // Either create fill-zero interval or use data from db. if ( $next_start > $end_datetime ) { $interval_end = $end_datetime->format( 'Y-m-d H:i:s' ); } else { $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->setTimezone( $local_tz ); $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. */ 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 ) { if ( isset( $query_args[ $query_arg_key ] ) && is_string( $query_args[ $query_arg_key ] ) ) { // 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. $datetime->setTimezone( $local_tz ); $query_args[ $query_arg_key ] = $datetime; @@ -310,7 +314,7 @@ class WC_Admin_Reports_Data_Store { if ( $db_interval_count === $expected_interval_count ) { return; } - $local_tz = new DateTimeZone( wc_timezone_string() ); + $local_tz = new \DateTimeZone( wc_timezone_string() ); if ( 'date' === strtolower( $query_args['orderby'] ) ) { // page X in request translates to slightly different dates in the db, in case some // records are missing from the db. @@ -327,7 +331,7 @@ class WC_Admin_Reports_Data_Store { $start_iteration = 0; 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 ++; } @@ -336,7 +340,7 @@ class WC_Admin_Reports_Data_Store { if ( $new_end_date > $latest_end_date ) { 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 ++; } if ( $new_end_date > $latest_end_date ) { @@ -358,7 +362,7 @@ class WC_Admin_Reports_Data_Store { $end_iteration = 0; 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 ++; } @@ -367,7 +371,7 @@ class WC_Admin_Reports_Data_Store { if ( $new_start_date < $earliest_start_date ) { 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 ++; } 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_before'] = $new_end_date; - $adj_after = $new_start_date->format( WC_Admin_Reports_Interval::$sql_datetime_format ); - $adj_before = $new_end_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( TimeInterval::$sql_datetime_format ); $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_after'"; @@ -458,7 +462,7 @@ class WC_Admin_Reports_Data_Store { * @return array */ 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 ); 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. */ 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 ) { - $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. $prev_start_timestamp = (int) $prev_start->format( 'U' ) + 1; $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' ); } - $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->setTimestamp( $next_end_timestamp ); if ( $end_datetime ) { @@ -537,15 +541,15 @@ class WC_Admin_Reports_Data_Store { */ protected function create_interval_subtotals( &$intervals ) { foreach ( $intervals as $key => $interval ) { - $start_gmt = WC_Admin_Reports_Interval::convert_local_datetime_to_gmt( $interval['date_start'] ); - $end_gmt = WC_Admin_Reports_Interval::convert_local_datetime_to_gmt( $interval['date_end'] ); + $start_gmt = TimeInterval::convert_local_datetime_to_gmt( $interval['date_start'] ); + $end_gmt = TimeInterval::convert_local_datetime_to_gmt( $interval['date_end'] ); // Move intervals result to subtotals object. $intervals[ $key ] = array( 'interval' => $interval['time_interval'], '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_gmt' => $end_gmt->format( WC_Admin_Reports_Interval::$sql_datetime_format ), + 'date_end_gmt' => $end_gmt->format( TimeInterval::$sql_datetime_format ), ); unset( $interval['interval'] ); @@ -573,9 +577,9 @@ class WC_Admin_Reports_Data_Store { if ( isset( $query_args['before'] ) && '' !== $query_args['before'] ) { 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 { - $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'"; @@ -583,9 +587,9 @@ class WC_Admin_Reports_Data_Store { if ( isset( $query_args['after'] ) && '' !== $query_args['after'] ) { 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 { - $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'"; } @@ -708,7 +712,7 @@ class WC_Admin_Reports_Data_Store { if ( isset( $query_args['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 ) ); diff --git a/plugins/woocommerce-admin/includes/interfaces/class-wc-admin-reports-data-store-interface.php b/plugins/woocommerce-admin/src/API/Reports/DataStoreInterface.php similarity index 81% rename from plugins/woocommerce-admin/includes/interfaces/class-wc-admin-reports-data-store-interface.php rename to plugins/woocommerce-admin/src/API/Reports/DataStoreInterface.php index b202cacbb2f..cf72f61d7fb 100644 --- a/plugins/woocommerce-admin/includes/interfaces/class-wc-admin-reports-data-store-interface.php +++ b/plugins/woocommerce-admin/src/API/Reports/DataStoreInterface.php @@ -5,6 +5,8 @@ * @package WooCommerce Admin/Interface */ +namespace Automattic\WooCommerce\Admin\API\Reports; + if ( ! defined( 'ABSPATH' ) ) { exit; } @@ -14,7 +16,7 @@ if ( ! defined( 'ABSPATH' ) ) { * * @since 3.5.0 */ -interface WC_Admin_Reports_Data_Store_Interface { +interface DataStoreInterface { /** * Get the data based on args. diff --git a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-downloads-controller.php b/plugins/woocommerce-admin/src/API/Reports/Downloads/Controller.php similarity index 97% rename from plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-downloads-controller.php rename to plugins/woocommerce-admin/src/API/Reports/Downloads/Controller.php index 2b23d52b27c..05de92ba36f 100644 --- a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-downloads-controller.php +++ b/plugins/woocommerce-admin/src/API/Reports/Downloads/Controller.php @@ -7,16 +7,17 @@ * @package WooCommerce Admin/API */ +namespace Automattic\WooCommerce\Admin\API\Reports\Downloads; + defined( 'ABSPATH' ) || exit; /** * REST API Reports downloads controller class. * * @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. * @@ -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(); $data = array(); @@ -109,7 +110,7 @@ class WC_Admin_REST_Reports_Downloads_Controller extends WC_Admin_REST_Reports_C $filename = basename( $file_path ); $response->data['file_name'] = apply_filters( 'woocommerce_file_download_filename', $filename, $product_id ); $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['order_number'] = $this->get_order_number( $data['order_id'] ); diff --git a/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-downloads-data-store.php b/plugins/woocommerce-admin/src/API/Reports/Downloads/DataStore.php similarity index 95% rename from plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-downloads-data-store.php rename to plugins/woocommerce-admin/src/API/Reports/Downloads/DataStore.php index 3341b251841..6f3cff518f1 100644 --- a/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-downloads-data-store.php +++ b/plugins/woocommerce-admin/src/API/Reports/Downloads/DataStore.php @@ -1,16 +1,22 @@ 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'"; } 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'"; } @@ -312,8 +318,8 @@ class WC_Admin_Reports_Downloads_Data_Store extends WC_Admin_Reports_Data_Store 'page' => 1, 'order' => 'DESC', 'orderby' => 'timestamp', - 'before' => WC_Admin_Reports_Interval::default_before(), - 'after' => WC_Admin_Reports_Interval::default_after(), + 'before' => TimeInterval::default_before(), + 'after' => TimeInterval::default_after(), 'fields' => '*', ); $query_args = wp_parse_args( $query_args, $defaults ); diff --git a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-downloads-files-controller.php b/plugins/woocommerce-admin/src/API/Reports/Downloads/Files/Controller.php similarity index 80% rename from plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-downloads-files-controller.php rename to plugins/woocommerce-admin/src/API/Reports/Downloads/Files/Controller.php index d0f54fbdc66..d84cab581e3 100644 --- a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-downloads-files-controller.php +++ b/plugins/woocommerce-admin/src/API/Reports/Downloads/Files/Controller.php @@ -7,6 +7,8 @@ * @package WooCommerce Admin/API */ +namespace Automattic\WooCommerce\Admin\API\Reports\Downloads\Files; + defined( 'ABSPATH' ) || exit; /** @@ -15,7 +17,7 @@ defined( 'ABSPATH' ) || exit; * @package WooCommerce/API * @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. diff --git a/plugins/woocommerce-admin/includes/class-wc-admin-reports-downloads-query.php b/plugins/woocommerce-admin/src/API/Reports/Downloads/Query.php similarity index 72% rename from plugins/woocommerce-admin/includes/class-wc-admin-reports-downloads-query.php rename to plugins/woocommerce-admin/src/API/Reports/Downloads/Query.php index aaeaa25c3c0..f40a2b6cbd2 100644 --- a/plugins/woocommerce-admin/includes/class-wc-admin-reports-downloads-query.php +++ b/plugins/woocommerce-admin/src/API/Reports/Downloads/Query.php @@ -9,18 +9,22 @@ * 'page' => 2, * '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(); * * @package WooCommerce Admin/Classes */ +namespace Automattic\WooCommerce\Admin\API\Reports\Downloads; + 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. @@ -39,7 +43,7 @@ class WC_Admin_Reports_Downloads_Query extends WC_Admin_Reports_Query { public function get_data() { $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 ); return apply_filters( 'woocommerce_reports_downloads_select_query', $results, $args ); } diff --git a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-downloads-stats-controller.php b/plugins/woocommerce-admin/src/API/Reports/Downloads/Stats/Controller.php similarity index 98% rename from plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-downloads-stats-controller.php rename to plugins/woocommerce-admin/src/API/Reports/Downloads/Stats/Controller.php index f627a57894f..beeb1c3cf35 100644 --- a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-downloads-stats-controller.php +++ b/plugins/woocommerce-admin/src/API/Reports/Downloads/Stats/Controller.php @@ -7,6 +7,8 @@ * @package WooCommerce Admin/API */ +namespace Automattic\WooCommerce\Admin\API\Reports\Downloads\Stats; + defined( 'ABSPATH' ) || exit; /** @@ -15,7 +17,7 @@ defined( 'ABSPATH' ) || exit; * @package WooCommerce/API * @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. @@ -67,7 +69,7 @@ class WC_Admin_REST_Reports_Downloads_Stats_Controller extends WC_REST_Reports_C */ public function get_items( $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(); $out_data = array( diff --git a/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-downloads-stats-data-store.php b/plugins/woocommerce-admin/src/API/Reports/Downloads/Stats/DataStore.php similarity index 85% rename from plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-downloads-stats-data-store.php rename to plugins/woocommerce-admin/src/API/Reports/Downloads/Stats/DataStore.php index 3c287272827..c1e7a1dcb15 100644 --- a/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-downloads-stats-data-store.php +++ b/plugins/woocommerce-admin/src/API/Reports/Downloads/Stats/DataStore.php @@ -1,16 +1,22 @@ 'date', 'fields' => '*', 'interval' => 'week', - 'before' => WC_Admin_Reports_Interval::default_before(), - 'after' => WC_Admin_Reports_Interval::default_after(), + 'before' => TimeInterval::default_before(), + 'after' => TimeInterval::default_after(), ); $query_args = wp_parse_args( $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 ); - $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'] ); if ( $query_args['page'] < 1 || $query_args['page'] > $total_pages ) { 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. 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 ) { @@ -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. 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] ); diff --git a/plugins/woocommerce-admin/includes/class-wc-admin-reports-downloads-stats-query.php b/plugins/woocommerce-admin/src/API/Reports/Downloads/Stats/Query.php similarity index 70% rename from plugins/woocommerce-admin/includes/class-wc-admin-reports-downloads-stats-query.php rename to plugins/woocommerce-admin/src/API/Reports/Downloads/Stats/Query.php index 21ea14fc7c7..7329428ca30 100644 --- a/plugins/woocommerce-admin/includes/class-wc-admin-reports-downloads-stats-query.php +++ b/plugins/woocommerce-admin/src/API/Reports/Downloads/Stats/Query.php @@ -5,12 +5,16 @@ * @package WooCommerce Admin/Classes */ +namespace Automattic\WooCommerce\Admin\API\Reports\Downloads\Stats; + 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. @@ -29,7 +33,7 @@ class WC_Admin_Reports_Downloads_Stats_Query extends WC_Admin_Reports_Query { public function get_data() { $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 ); return apply_filters( 'woocommerce_reports_downloads_stats_select_query', $results, $args ); } diff --git a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-export-controller.php b/plugins/woocommerce-admin/src/API/Reports/Export/Controller.php similarity index 88% rename from plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-export-controller.php rename to plugins/woocommerce-admin/src/API/Reports/Export/Controller.php index a9a9980d9dd..bafd020f5f4 100644 --- a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-export-controller.php +++ b/plugins/woocommerce-admin/src/API/Reports/Export/Controller.php @@ -9,15 +9,19 @@ * @package WooCommerce Admin/API */ +namespace Automattic\WooCommerce\Admin\API\Reports\Export; + defined( 'ABSPATH' ) || exit; +use \Automattic\WooCommerce\Admin\ReportExporter; + /** * Reports Export controller. * * @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. * @@ -41,7 +45,7 @@ class WC_Admin_REST_Reports_Export_Controller extends WC_Admin_REST_Reports_Cont '/' . $this->rest_base, array( array( - 'methods' => WP_REST_Server::EDITABLE, + 'methods' => \WP_REST_Server::EDITABLE, 'callback' => array( $this, 'export_items' ), 'permission_callback' => array( $this, 'get_items_permissions_check' ), '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[a-z0-9]+)/status', array( array( - 'methods' => WP_REST_Server::READABLE, + 'methods' => \WP_REST_Server::READABLE, 'callback' => array( $this, 'export_status' ), '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']; $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 ) { $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 ); @@ -192,10 +196,10 @@ class WC_Admin_REST_Reports_Export_Controller extends WC_Admin_REST_Reports_Cont public function export_status( $request ) { $report_type = $request['type']; $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 ) { - return new WP_Error( + return new \WP_Error( 'woocommerce_admin_reports_export_invalid_id', __( 'Sorry, there is no export with that ID.', 'woocommerce-admin' ), 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? if ( 100 === $percentage ) { $query_args = array( - 'action' => WC_Admin_Report_Exporter::DOWNLOAD_EXPORT_ACTION, + 'action' => ReportExporter::DOWNLOAD_EXPORT_ACTION, 'filename' => "wc-{$report_type}-report-export-{$export_id}", ); diff --git a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-import-controller.php b/plugins/woocommerce-admin/src/API/Reports/Import/Controller.php similarity index 88% rename from plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-import-controller.php rename to plugins/woocommerce-admin/src/API/Reports/Import/Controller.php index 1958d17aa39..5172a19a1ae 100644 --- a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-import-controller.php +++ b/plugins/woocommerce-admin/src/API/Reports/Import/Controller.php @@ -7,15 +7,19 @@ * @package WooCommerce Admin/API */ +namespace Automattic\WooCommerce\Admin\API\Reports\Import; + defined( 'ABSPATH' ) || exit; +use Automattic\WooCommerce\Admin\ReportsSync; + /** * Reports Imports controller. * * @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. * @@ -39,7 +43,7 @@ class WC_Admin_REST_Reports_Import_Controller extends WC_Admin_REST_Reports_Cont '/' . $this->rest_base, array( array( - 'methods' => WP_REST_Server::EDITABLE, + 'methods' => \WP_REST_Server::EDITABLE, 'callback' => array( $this, 'import_items' ), 'permission_callback' => array( $this, 'import_permissions_check' ), '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', array( array( - 'methods' => WP_REST_Server::EDITABLE, + 'methods' => \WP_REST_Server::EDITABLE, 'callback' => array( $this, 'cancel_import' ), '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', array( array( - 'methods' => WP_REST_Server::EDITABLE, + 'methods' => \WP_REST_Server::EDITABLE, 'callback' => array( $this, 'delete_imported_items' ), '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', array( array( - 'methods' => WP_REST_Server::READABLE, + 'methods' => \WP_REST_Server::READABLE, 'callback' => array( $this, 'get_import_status' ), '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', array( array( - 'methods' => WP_REST_Server::READABLE, + 'methods' => \WP_REST_Server::READABLE, 'callback' => array( $this, 'get_import_totals' ), 'permission_callback' => array( $this, 'import_permissions_check' ), '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 ) { 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; } @@ -119,7 +123,7 @@ class WC_Admin_REST_Reports_Import_Controller extends WC_Admin_REST_Reports_Cont */ public function import_items( $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 ) ) { $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 */ public function cancel_import( $request ) { - WC_Admin_Reports_Sync::clear_queued_actions(); + ReportsSync::clear_queued_actions(); $result = array( '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 */ public function delete_imported_items( $request ) { - $delete = WC_Admin_Reports_Sync::delete_report_data(); + $delete = ReportsSync::delete_report_data(); if ( is_wp_error( $delete ) ) { $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 ) { $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_count' => (int) get_option( 'wc_admin_import_customers_count', 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 ) { $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 ); $data = $this->prepare_response_for_collection( $response ); diff --git a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-orders-controller.php b/plugins/woocommerce-admin/src/API/Reports/Orders/Controller.php similarity index 98% rename from plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-orders-controller.php rename to plugins/woocommerce-admin/src/API/Reports/Orders/Controller.php index a5e9d881bb9..29c93f652d6 100644 --- a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-orders-controller.php +++ b/plugins/woocommerce-admin/src/API/Reports/Orders/Controller.php @@ -7,15 +7,17 @@ * @package WooCommerce Admin/API */ +namespace Automattic\WooCommerce\Admin\API\Reports\Orders; + defined( 'ABSPATH' ) || exit; /** * REST API Reports orders controller class. * * @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. @@ -69,7 +71,7 @@ class WC_Admin_REST_Reports_Orders_Controller extends WC_Admin_REST_Reports_Cont */ public function get_items( $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(); $data = array(); diff --git a/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-orders-data-store.php b/plugins/woocommerce-admin/src/API/Reports/Orders/DataStore.php similarity index 97% rename from plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-orders-data-store.php rename to plugins/woocommerce-admin/src/API/Reports/Orders/DataStore.php index 7ca1b7c4e8d..339b624dcbe 100644 --- a/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-orders-data-store.php +++ b/plugins/woocommerce-admin/src/API/Reports/Orders/DataStore.php @@ -1,16 +1,21 @@ array('failed'), * '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(); * * @package WooCommerce Admin/Classes */ +namespace Automattic\WooCommerce\Admin\API\Reports\Orders; + 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. @@ -33,7 +37,7 @@ class WC_Admin_Reports_Orders_Query extends WC_Admin_Reports_Query { */ public function get_data() { $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 ); return apply_filters( 'woocommerce_reports_orders_select_query', $results, $args ); } diff --git a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-orders-stats-controller.php b/plugins/woocommerce-admin/src/API/Reports/Orders/Stats/Controller.php similarity index 97% rename from plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-orders-stats-controller.php rename to plugins/woocommerce-admin/src/API/Reports/Orders/Stats/Controller.php index 73e9829264c..d81fad397a2 100644 --- a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-orders-stats-controller.php +++ b/plugins/woocommerce-admin/src/API/Reports/Orders/Stats/Controller.php @@ -7,15 +7,19 @@ * @package WooCommerce Admin/API */ +namespace Automattic\WooCommerce\Admin\API\Reports\Orders\Stats; + defined( 'ABSPATH' ) || exit; +use \Automattic\WooCommerce\Admin\API\Reports\ParameterException; + /** * REST API Reports orders stats controller class. * * @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. @@ -70,11 +74,11 @@ class WC_Admin_REST_Reports_Orders_Stats_Controller extends WC_Admin_REST_Report */ public function get_items( $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 { $report_data = $orders_query->get_data(); - } catch ( WC_Admin_Reports_Parameter_Exception $e ) { - return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); + } catch ( ParameterException $e ) { + return new \WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); } $out_data = array( diff --git a/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-orders-stats-data-store.php b/plugins/woocommerce-admin/src/API/Reports/Orders/Stats/DataStore.php similarity index 93% rename from plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-orders-stats-data-store.php rename to plugins/woocommerce-admin/src/API/Reports/Orders/Stats/DataStore.php index 281e8600b20..a42af30eb95 100644 --- a/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-orders-stats-data-store.php +++ b/plugins/woocommerce-admin/src/API/Reports/Orders/Stats/DataStore.php @@ -1,16 +1,22 @@ 1, 'order' => 'DESC', 'orderby' => 'date', - 'before' => WC_Admin_Reports_Interval::default_before(), - 'after' => WC_Admin_Reports_Interval::default_after(), + 'before' => TimeInterval::default_before(), + 'after' => TimeInterval::default_after(), 'interval' => 'week', 'fields' => '*', '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'] ); $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'] ); $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] ); $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. $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'] ); 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. 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] ) ) { @@ -343,14 +349,14 @@ class WC_Admin_Reports_Orders_Stats_Data_Store extends WC_Admin_Reports_Data_Sto '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->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'] ); } else { $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 ); 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 */ 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 ) { 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 ) ) { return false; diff --git a/plugins/woocommerce-admin/includes/class-wc-admin-reports-orders-stats-query.php b/plugins/woocommerce-admin/src/API/Reports/Orders/Stats/Query.php similarity index 77% rename from plugins/woocommerce-admin/includes/class-wc-admin-reports-orders-stats-query.php rename to plugins/woocommerce-admin/src/API/Reports/Orders/Stats/Query.php index 829ba9b552d..aade12b7054 100644 --- a/plugins/woocommerce-admin/includes/class-wc-admin-reports-orders-stats-query.php +++ b/plugins/woocommerce-admin/src/API/Reports/Orders/Stats/Query.php @@ -11,18 +11,22 @@ * 'coupons' => array(138), * '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(); * * @package WooCommerce Admin/Classes */ +namespace Automattic\WooCommerce\Admin\API\Reports\Orders\Stats; + 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. @@ -53,7 +57,7 @@ class WC_Admin_Reports_Orders_Stats_Query extends WC_Admin_Reports_Query { public function get_data() { $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 ); return apply_filters( 'woocommerce_reports_orders_stats_select_query', $results, $args ); } diff --git a/plugins/woocommerce-admin/includes/class-wc-admin-reports-orders-stats-segmenting.php b/plugins/woocommerce-admin/src/API/Reports/Orders/Stats/Segmenter.php similarity index 97% rename from plugins/woocommerce-admin/includes/class-wc-admin-reports-orders-stats-segmenting.php rename to plugins/woocommerce-admin/src/API/Reports/Orders/Stats/Segmenter.php index b349dfad337..34c87f00202 100644 --- a/plugins/woocommerce-admin/includes/class-wc-admin-reports-orders-stats-segmenting.php +++ b/plugins/woocommerce-admin/src/API/Reports/Orders/Stats/Segmenter.php @@ -5,12 +5,17 @@ * @package WooCommerce Admin/Classes */ +namespace Automattic\WooCommerce\Admin\API\Reports\Orders\Stats; + 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. */ -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). @@ -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). * * @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 ) { 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 ); } elseif ( 'variation' === $this->query_args['segmentby'] ) { 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( diff --git a/plugins/woocommerce-admin/includes/class-wc-admin-reports-parameter-exception.php b/plugins/woocommerce-admin/src/API/Reports/ParameterException.php similarity index 58% rename from plugins/woocommerce-admin/includes/class-wc-admin-reports-parameter-exception.php rename to plugins/woocommerce-admin/src/API/Reports/ParameterException.php index 75231f7d595..749ff3a6428 100644 --- a/plugins/woocommerce-admin/includes/class-wc-admin-reports-parameter-exception.php +++ b/plugins/woocommerce-admin/src/API/Reports/ParameterException.php @@ -7,9 +7,11 @@ * @package WooCommerce Admin */ +namespace Automattic\WooCommerce\Admin\API\Reports; + 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 {} diff --git a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-performance-indicators-controller.php b/plugins/woocommerce-admin/src/API/Reports/PerformanceIndicators/Controller.php similarity index 94% rename from plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-performance-indicators-controller.php rename to plugins/woocommerce-admin/src/API/Reports/PerformanceIndicators/Controller.php index ef109601420..0cbe9ea81e2 100644 --- a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-performance-indicators-controller.php +++ b/plugins/woocommerce-admin/src/API/Reports/PerformanceIndicators/Controller.php @@ -7,6 +7,8 @@ * @package WooCommerce Admin/API */ +namespace Automattic\WooCommerce\Admin\API\Reports\PerformanceIndicators; + defined( 'ABSPATH' ) || exit; /** @@ -15,7 +17,7 @@ defined( 'ABSPATH' ) || exit; * @package WooCommerce/API * @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. @@ -68,7 +70,7 @@ class WC_Admin_REST_Reports_Performance_Indicators_Controller extends WC_REST_Re '/' . $this->rest_base, array( array( - 'methods' => WP_REST_Server::READABLE, + 'methods' => \WP_REST_Server::READABLE, 'callback' => array( $this, 'get_items' ), 'permission_callback' => array( $this, 'get_items_permissions_check' ), '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', array( array( - 'methods' => WP_REST_Server::READABLE, + 'methods' => \WP_REST_Server::READABLE, 'callback' => array( $this, 'get_allowed_items' ), 'permission_callback' => array( $this, 'get_items_permissions_check' ), 'args' => $this->get_collection_params(), @@ -117,7 +119,7 @@ class WC_Admin_REST_Reports_Performance_Indicators_Controller extends WC_REST_Re 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 ); 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() ) { - 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(); @@ -133,7 +135,7 @@ class WC_Admin_REST_Reports_Performance_Indicators_Controller extends WC_REST_Re foreach ( $endpoints as $endpoint ) { 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 ); 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 ); 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(); @@ -288,7 +290,7 @@ class WC_Admin_REST_Reports_Performance_Indicators_Controller extends WC_REST_Re } $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( '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. * - * @param WC_Admin_Reports_Query $object Object data. + * @param \Automattic\WooCommerce\Admin\API\Reports\Query $object Object data. * @return array */ protected function prepare_links( $object ) { diff --git a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-products-controller.php b/plugins/woocommerce-admin/src/API/Reports/Products/Controller.php similarity index 98% rename from plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-products-controller.php rename to plugins/woocommerce-admin/src/API/Reports/Products/Controller.php index 89432475566..c6da2ec120b 100644 --- a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-products-controller.php +++ b/plugins/woocommerce-admin/src/API/Reports/Products/Controller.php @@ -7,6 +7,8 @@ * @package WooCommerce Admin/API */ +namespace Automattic\WooCommerce\Admin\API\Reports\Products; + defined( 'ABSPATH' ) || exit; /** @@ -15,7 +17,7 @@ defined( 'ABSPATH' ) || exit; * @package WooCommerce/API * @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. @@ -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(); $data = array(); diff --git a/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-products-data-store.php b/plugins/woocommerce-admin/src/API/Reports/Products/DataStore.php similarity index 96% rename from plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-products-data-store.php rename to plugins/woocommerce-admin/src/API/Reports/Products/DataStore.php index 157a7695da4..e69c28fafb7 100644 --- a/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-products-data-store.php +++ b/plugins/woocommerce-admin/src/API/Reports/Products/DataStore.php @@ -1,16 +1,22 @@ $product_data ) { - $extended_info = new ArrayObject(); + $extended_info = new \ArrayObject(); if ( $query_args['extended_info'] ) { $product_id = $product_data['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, 'order' => 'DESC', 'orderby' => 'date', - 'before' => WC_Admin_Reports_Interval::default_before(), - 'after' => WC_Admin_Reports_Interval::default_after(), + 'before' => TimeInterval::default_before(), + 'after' => TimeInterval::default_after(), 'fields' => '*', 'categories' => 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(), 'product_qty' => $product_qty, '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, 'tax_amount' => $tax_amount, 'shipping_amount' => $shipping_amount, diff --git a/plugins/woocommerce-admin/includes/class-wc-admin-reports-products-query.php b/plugins/woocommerce-admin/src/API/Reports/Products/Query.php similarity index 73% rename from plugins/woocommerce-admin/includes/class-wc-admin-reports-products-query.php rename to plugins/woocommerce-admin/src/API/Reports/Products/Query.php index 60d359b615a..39d27335f4d 100644 --- a/plugins/woocommerce-admin/includes/class-wc-admin-reports-products-query.php +++ b/plugins/woocommerce-admin/src/API/Reports/Products/Query.php @@ -10,18 +10,22 @@ * 'categories' => array(15, 18), * '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(); * * @package WooCommerce Admin/Classes */ +namespace Automattic\WooCommerce\Admin\API\Reports\Products; + 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. @@ -40,7 +44,7 @@ class WC_Admin_Reports_Products_Query extends WC_Admin_Reports_Query { public function get_data() { $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 ); return apply_filters( 'woocommerce_reports_products_select_query', $results, $args ); } diff --git a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-products-stats-controller.php b/plugins/woocommerce-admin/src/API/Reports/Products/Stats/Controller.php similarity index 96% rename from plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-products-stats-controller.php rename to plugins/woocommerce-admin/src/API/Reports/Products/Stats/Controller.php index fd9fd4bd390..e2d547e5f7a 100644 --- a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-products-stats-controller.php +++ b/plugins/woocommerce-admin/src/API/Reports/Products/Stats/Controller.php @@ -7,15 +7,19 @@ * @package WooCommerce Admin/API */ +namespace Automattic\WooCommerce\Admin\API\Reports\Products\Stats; + defined( 'ABSPATH' ) || exit; +use \Automattic\WooCommerce\Admin\API\Reports\ParameterException; + /** * REST API Reports products stats controller class. * * @package WooCommerce/API * @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. @@ -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 { $report_data = $query->get_data(); - } catch ( WC_Admin_Reports_Parameter_Exception $e ) { - return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); + } catch ( ParameterException $e ) { + return new \WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); } $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 ) { if ( empty( $results ) ) { - $results = new stdClass(); + $results = new \stdClass(); $results->total = 0; - $results->totals = new stdClass(); + $results->totals = new \stdClass(); $results->totals->items_sold = 0; $results->totals->net_revenue = 0; $results->totals->orders_count = 0; diff --git a/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-products-stats-data-store.php b/plugins/woocommerce-admin/src/API/Reports/Products/Stats/DataStore.php similarity index 85% rename from plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-products-stats-data-store.php rename to plugins/woocommerce-admin/src/API/Reports/Products/Stats/DataStore.php index 2057e85d89f..fe4339c207b 100644 --- a/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-products-stats-data-store.php +++ b/plugins/woocommerce-admin/src/API/Reports/Products/Stats/DataStore.php @@ -1,17 +1,22 @@ 1, 'order' => 'DESC', 'orderby' => 'date', - 'before' => WC_Admin_Reports_Interval::default_before(), - 'after' => WC_Admin_Reports_Interval::default_after(), + 'before' => TimeInterval::default_before(), + 'after' => TimeInterval::default_after(), 'fields' => '*', 'categories' => array(), '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. $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'] ); if ( $query_args['page'] < 1 || $query_args['page'] > $total_pages ) { return array(); @@ -165,11 +170,11 @@ class WC_Admin_Reports_Products_Stats_Data_Store extends WC_Admin_Reports_Produc ARRAY_A ); // 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 ); 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 ) { @@ -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. 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] ); @@ -210,7 +215,7 @@ class WC_Admin_Reports_Products_Stats_Data_Store extends WC_Admin_Reports_Produc '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->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'] ); diff --git a/plugins/woocommerce-admin/includes/class-wc-admin-reports-products-stats-query.php b/plugins/woocommerce-admin/src/API/Reports/Products/Stats/Query.php similarity index 72% rename from plugins/woocommerce-admin/includes/class-wc-admin-reports-products-stats-query.php rename to plugins/woocommerce-admin/src/API/Reports/Products/Stats/Query.php index a1e89de2f5c..960a3bf2ff6 100644 --- a/plugins/woocommerce-admin/includes/class-wc-admin-reports-products-stats-query.php +++ b/plugins/woocommerce-admin/src/API/Reports/Products/Stats/Query.php @@ -10,18 +10,22 @@ * 'categories' => array(15, 18), * '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(); * * @package WooCommerce Admin/Classes */ +namespace Automattic\WooCommerce\Admin\API\Reports\Products\Stats; + 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. @@ -40,7 +44,7 @@ class WC_Admin_Reports_Products_Stats_Query extends WC_Admin_Reports_Query { public function get_data() { $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 ); return apply_filters( 'woocommerce_reports_products_stats_select_query', $results, $args ); } diff --git a/plugins/woocommerce-admin/includes/class-wc-admin-reports-products-stats-segmenting.php b/plugins/woocommerce-admin/src/API/Reports/Products/Stats/Segmenter.php similarity index 93% rename from plugins/woocommerce-admin/includes/class-wc-admin-reports-products-stats-segmenting.php rename to plugins/woocommerce-admin/src/API/Reports/Products/Stats/Segmenter.php index e582d08dca2..70f5d92ca80 100644 --- a/plugins/woocommerce-admin/includes/class-wc-admin-reports-products-stats-segmenting.php +++ b/plugins/woocommerce-admin/src/API/Reports/Products/Stats/Segmenter.php @@ -5,12 +5,17 @@ * @package WooCommerce Admin/Classes */ +namespace Automattic\WooCommerce\Admin\API\Reports\Products\Stats; + 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. */ -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). @@ -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). * * @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 ) { 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 ); } elseif ( 'variation' === $this->query_args['segmentby'] ) { 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( diff --git a/plugins/woocommerce-admin/includes/class-wc-admin-reports-query.php b/plugins/woocommerce-admin/src/API/Reports/Query.php similarity index 51% rename from plugins/woocommerce-admin/includes/class-wc-admin-reports-query.php rename to plugins/woocommerce-admin/src/API/Reports/Query.php index 4fb0df6a3f1..d0237ea32f5 100644 --- a/plugins/woocommerce-admin/includes/class-wc-admin-reports-query.php +++ b/plugins/woocommerce-admin/src/API/Reports/Query.php @@ -5,12 +5,14 @@ * @package WooCommerce Admin/Classes */ +namespace Automattic\WooCommerce\Admin\API\Reports; + 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. @@ -19,7 +21,7 @@ abstract class WC_Admin_Reports_Query extends WC_Object_Query { */ public function get_data() { /* 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 ) ); } } diff --git a/plugins/woocommerce-admin/includes/class-wc-admin-reports-revenue-query.php b/plugins/woocommerce-admin/src/API/Reports/Revenue/Query.php similarity index 78% rename from plugins/woocommerce-admin/includes/class-wc-admin-reports-revenue-query.php rename to plugins/woocommerce-admin/src/API/Reports/Revenue/Query.php index d88c1d3c91e..45371cdf28f 100644 --- a/plugins/woocommerce-admin/includes/class-wc-admin-reports-revenue-query.php +++ b/plugins/woocommerce-admin/src/API/Reports/Revenue/Query.php @@ -8,18 +8,22 @@ * 'after' => '2018-07-05 00:00:00', * 'interval' => 'week', * ); - * $report = new WC_Admin_Reports_Revenue_Query( $args ); + * $report = new \Automattic\WooCommerce\Admin\API\Reports\Revenue\Query( $args ); * $mydata = $report->get_data(); * * @package WooCommerce Admin/Classes */ +namespace Automattic\WooCommerce\Admin\API\Reports\Revenue; + 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. @@ -57,7 +61,7 @@ class WC_Admin_Reports_Revenue_Query extends WC_Admin_Reports_Query { public function get_data() { $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 ); return apply_filters( 'woocommerce_reports_revenue_select_query', $results, $args ); } diff --git a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-revenue-stats-controller.php b/plugins/woocommerce-admin/src/API/Reports/Revenue/Stats/Controller.php similarity index 96% rename from plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-revenue-stats-controller.php rename to plugins/woocommerce-admin/src/API/Reports/Revenue/Stats/Controller.php index 5d0a3f8f743..571185eb82a 100644 --- a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-revenue-stats-controller.php +++ b/plugins/woocommerce-admin/src/API/Reports/Revenue/Stats/Controller.php @@ -7,15 +7,20 @@ * @package WooCommerce Admin/API */ +namespace Automattic\WooCommerce\Admin\API\Reports\Revenue\Stats; + 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. * * @package WooCommerce/API * @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. @@ -59,11 +64,11 @@ class WC_Admin_REST_Reports_Revenue_Stats_Controller extends WC_REST_Reports_Con */ public function get_items( $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 { $report_data = $reports_revenue->get_data(); - } catch ( WC_Admin_Reports_Parameter_Exception $e ) { - return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); + } catch ( ParameterException $e ) { + return new \WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); } $out_data = array( diff --git a/plugins/woocommerce-admin/includes/class-wc-admin-reports-segmenting.php b/plugins/woocommerce-admin/src/API/Reports/Segmenter.php similarity index 97% rename from plugins/woocommerce-admin/includes/class-wc-admin-reports-segmenting.php rename to plugins/woocommerce-admin/src/API/Reports/Segmenter.php index 4b761f2dde9..18a5f612888 100644 --- a/plugins/woocommerce-admin/includes/class-wc-admin-reports-segmenting.php +++ b/plugins/woocommerce-admin/src/API/Reports/Segmenter.php @@ -5,12 +5,17 @@ * @package WooCommerce Admin/Classes */ +namespace Automattic\WooCommerce\Admin\API\Reports; + 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. */ -class WC_Admin_Reports_Segmenting { +class Segmenter { /** * Array of all segment ids. @@ -41,7 +46,7 @@ class WC_Admin_Reports_Segmenting { 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 $report_columns Report columns lookup from data store. @@ -375,7 +380,7 @@ class WC_Admin_Reports_Segmenting { if ( isset( $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' ); $segment_labels = wp_list_pluck( $coupons, 'post_title', 'ID' ); $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'] ) ) { $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 ) { $id = $tax['tax_rate_id']; $segments[] = $id; - $segment_labels[ $id ] = WC_Tax::get_rate_code( (object) $tax ); + $segment_labels[ $id ] = \WC_Tax::get_rate_code( (object) $tax ); } } else { // Catch all default. diff --git a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-stock-controller.php b/plugins/woocommerce-admin/src/API/Reports/Stock/Controller.php similarity index 98% rename from plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-stock-controller.php rename to plugins/woocommerce-admin/src/API/Reports/Stock/Controller.php index 20d6716ada7..f3cf408c8b5 100644 --- a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-stock-controller.php +++ b/plugins/woocommerce-admin/src/API/Reports/Stock/Controller.php @@ -7,6 +7,8 @@ * @package WooCommerce Admin/API */ +namespace Automattic\WooCommerce\Admin\API\Reports\Stock; + defined( 'ABSPATH' ) || exit; /** @@ -15,7 +17,7 @@ defined( 'ABSPATH' ) || exit; * @package WooCommerce/API * @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. @@ -77,14 +79,14 @@ class WC_Admin_REST_Reports_Stock_Controller extends WC_REST_Reports_Controller * @return array */ protected function get_products( $query_args ) { - $query = new WP_Query(); + $query = new \WP_Query(); $result = $query->query( $query_args ); $total_posts = $query->found_posts; if ( $total_posts < 1 ) { // Out-of-bounds, run the query again without LIMIT for total count. unset( $query_args['paged'] ); - $count_query = new WP_Query(); + $count_query = new \WP_Query(); $count_query->query( $query_args ); $total_posts = $count_query->found_posts; } diff --git a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-stock-stats-controller.php b/plugins/woocommerce-admin/src/API/Reports/Stock/Stats/Controller.php similarity index 95% rename from plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-stock-stats-controller.php rename to plugins/woocommerce-admin/src/API/Reports/Stock/Stats/Controller.php index c4782dd8e9d..50ce02f501b 100644 --- a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-stock-stats-controller.php +++ b/plugins/woocommerce-admin/src/API/Reports/Stock/Stats/Controller.php @@ -7,6 +7,8 @@ * @package WooCommerce Admin/API */ +namespace Automattic\WooCommerce\Admin\API\Reports\Stock\Stats; + defined( 'ABSPATH' ) || exit; /** @@ -15,7 +17,7 @@ defined( 'ABSPATH' ) || exit; * @package WooCommerce/API * @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. @@ -38,7 +40,7 @@ class WC_Admin_REST_Reports_Stock_Stats_Controller extends WC_REST_Reports_Contr * @return array|WP_Error */ public function get_items( $request ) { - $stock_query = new WC_Admin_Reports_Stock_Stats_Query(); + $stock_query = new Query(); $report_data = $stock_query->get_data(); $out_data = array( 'totals' => $report_data, diff --git a/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-stock-stats-data-store.php b/plugins/woocommerce-admin/src/API/Reports/Stock/Stats/DataStore.php similarity index 92% rename from plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-stock-stats-data-store.php rename to plugins/woocommerce-admin/src/API/Reports/Stock/Stats/DataStore.php index e8d82567e11..34368dc70cc 100644 --- a/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-stock-stats-data-store.php +++ b/plugins/woocommerce-admin/src/API/Reports/Stock/Stats/DataStore.php @@ -1,16 +1,21 @@ query( $query_args ); return intval( $query->found_posts ); } diff --git a/plugins/woocommerce-admin/includes/class-wc-admin-reports-stock-stats-query.php b/plugins/woocommerce-admin/src/API/Reports/Stock/Stats/Query.php similarity index 54% rename from plugins/woocommerce-admin/includes/class-wc-admin-reports-stock-stats-query.php rename to plugins/woocommerce-admin/src/API/Reports/Stock/Stats/Query.php index 297c19ede46..b24c489f2c0 100644 --- a/plugins/woocommerce-admin/includes/class-wc-admin-reports-stock-stats-query.php +++ b/plugins/woocommerce-admin/src/API/Reports/Stock/Stats/Query.php @@ -2,18 +2,22 @@ /** * 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(); * * @package WooCommerce Admin/Classes */ +namespace Automattic\WooCommerce\Admin\API\Reports\Stock\Stats; + 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. @@ -21,7 +25,7 @@ class WC_Admin_Reports_Stock_Stats_Query extends WC_Admin_Reports_Query { * @return array */ 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(); return apply_filters( 'woocommerce_reports_stock_stats_query', $results ); } diff --git a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-taxes-controller.php b/plugins/woocommerce-admin/src/API/Reports/Taxes/Controller.php similarity index 98% rename from plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-taxes-controller.php rename to plugins/woocommerce-admin/src/API/Reports/Taxes/Controller.php index a471bf75351..96a6fa12835 100644 --- a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-taxes-controller.php +++ b/plugins/woocommerce-admin/src/API/Reports/Taxes/Controller.php @@ -7,6 +7,8 @@ * @package WooCommerce Admin/API */ +namespace Automattic\WooCommerce\Admin\API\Reports\Taxes; + defined( 'ABSPATH' ) || exit; /** @@ -15,7 +17,7 @@ defined( 'ABSPATH' ) || exit; * @package WooCommerce/API * @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. @@ -58,7 +60,7 @@ class WC_Admin_REST_Reports_Taxes_Controller extends WC_REST_Reports_Controller */ public function get_items( $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(); $data = array(); diff --git a/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-taxes-data-store.php b/plugins/woocommerce-admin/src/API/Reports/Taxes/DataStore.php similarity index 95% rename from plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-taxes-data-store.php rename to plugins/woocommerce-admin/src/API/Reports/Taxes/DataStore.php index 15d7c22f92a..6eb361c44c1 100644 --- a/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-taxes-data-store.php +++ b/plugins/woocommerce-admin/src/API/Reports/Taxes/DataStore.php @@ -1,16 +1,22 @@ 1, 'order' => 'DESC', 'orderby' => 'tax_rate_id', - 'before' => WC_Admin_Reports_Interval::default_before(), - 'after' => WC_Admin_Reports_Interval::default_after(), + 'before' => TimeInterval::default_before(), + 'after' => TimeInterval::default_after(), 'fields' => '*', '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, array( '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(), 'shipping_tax' => $tax_item->get_shipping_tax_total(), 'order_tax' => $tax_item->get_tax_total(), diff --git a/plugins/woocommerce-admin/includes/class-wc-admin-reports-taxes-query.php b/plugins/woocommerce-admin/src/API/Reports/Taxes/Query.php similarity index 73% rename from plugins/woocommerce-admin/includes/class-wc-admin-reports-taxes-query.php rename to plugins/woocommerce-admin/src/API/Reports/Taxes/Query.php index 7a22a8413a9..e4f9aa5c0d6 100644 --- a/plugins/woocommerce-admin/includes/class-wc-admin-reports-taxes-query.php +++ b/plugins/woocommerce-admin/src/API/Reports/Taxes/Query.php @@ -9,18 +9,22 @@ * 'page' => 2, * '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(); * * @package WooCommerce Admin/Classes */ +namespace Automattic\WooCommerce\Admin\API\Reports\Taxes; + 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. @@ -39,7 +43,7 @@ class WC_Admin_Reports_Taxes_Query extends WC_Admin_Reports_Query { public function get_data() { $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 ); return apply_filters( 'woocommerce_reports_taxes_select_query', $results, $args ); } diff --git a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-taxes-stats-controller.php b/plugins/woocommerce-admin/src/API/Reports/Taxes/Stats/Controller.php similarity index 97% rename from plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-taxes-stats-controller.php rename to plugins/woocommerce-admin/src/API/Reports/Taxes/Stats/Controller.php index c6c3b23cfb0..671e60581dd 100644 --- a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-taxes-stats-controller.php +++ b/plugins/woocommerce-admin/src/API/Reports/Taxes/Stats/Controller.php @@ -7,6 +7,8 @@ * @package WooCommerce Admin/API */ +namespace Automattic\WooCommerce\Admin\API\Reports\Taxes\Stats; + defined( 'ABSPATH' ) || exit; /** @@ -15,7 +17,7 @@ defined( 'ABSPATH' ) || exit; * @package WooCommerce/API * @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. @@ -46,9 +48,9 @@ class WC_Admin_REST_Reports_Taxes_Stats_Controller extends WC_REST_Reports_Contr */ public function set_default_report_data( $results ) { if ( empty( $results ) ) { - $results = new stdClass(); + $results = new \stdClass(); $results->total = 0; - $results->totals = new stdClass(); + $results->totals = new \stdClass(); $results->totals->tax_codes = 0; $results->totals->total_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 ) { $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(); $out_data = array( diff --git a/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-taxes-stats-data-store.php b/plugins/woocommerce-admin/src/API/Reports/Taxes/Stats/DataStore.php similarity index 88% rename from plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-taxes-stats-data-store.php rename to plugins/woocommerce-admin/src/API/Reports/Taxes/Stats/DataStore.php index 8a1abbb89e9..ec2dac18a78 100644 --- a/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-taxes-stats-data-store.php +++ b/plugins/woocommerce-admin/src/API/Reports/Taxes/Stats/DataStore.php @@ -1,17 +1,22 @@ 1, 'order' => 'DESC', 'orderby' => 'tax_rate_id', - 'before' => WC_Admin_Reports_Interval::default_before(), - 'after' => WC_Admin_Reports_Interval::default_after(), + 'before' => TimeInterval::default_before(), + 'after' => TimeInterval::default_after(), 'fields' => '*', '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. $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'] ); 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. 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 ); $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. 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] ); @@ -264,7 +269,7 @@ class WC_Admin_Reports_Taxes_Stats_Data_Store extends WC_Admin_Reports_Data_Stor '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->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'] ); diff --git a/plugins/woocommerce-admin/includes/class-wc-admin-reports-taxes-stats-query.php b/plugins/woocommerce-admin/src/API/Reports/Taxes/Stats/Query.php similarity index 73% rename from plugins/woocommerce-admin/includes/class-wc-admin-reports-taxes-stats-query.php rename to plugins/woocommerce-admin/src/API/Reports/Taxes/Stats/Query.php index 61a15ad139f..0f3326e2a26 100644 --- a/plugins/woocommerce-admin/includes/class-wc-admin-reports-taxes-stats-query.php +++ b/plugins/woocommerce-admin/src/API/Reports/Taxes/Stats/Query.php @@ -10,18 +10,22 @@ * 'categories' => array(15, 18), * '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(); * * @package WooCommerce Admin/Classes */ +namespace Automattic\WooCommerce\Admin\API\Reports\Taxes\Stats; + 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. @@ -40,7 +44,7 @@ class WC_Admin_Reports_Taxes_Stats_Query extends WC_Admin_Reports_Query { public function get_data() { $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 ); return apply_filters( 'woocommerce_reports_taxes_stats_select_query', $results, $args ); } diff --git a/plugins/woocommerce-admin/includes/class-wc-admin-reports-taxes-stats-segmenting.php b/plugins/woocommerce-admin/src/API/Reports/Taxes/Stats/Segmenter.php similarity index 94% rename from plugins/woocommerce-admin/includes/class-wc-admin-reports-taxes-stats-segmenting.php rename to plugins/woocommerce-admin/src/API/Reports/Taxes/Stats/Segmenter.php index 6f3bf61bada..dd68c16d0eb 100644 --- a/plugins/woocommerce-admin/includes/class-wc-admin-reports-taxes-stats-segmenting.php +++ b/plugins/woocommerce-admin/src/API/Reports/Taxes/Stats/Segmenter.php @@ -5,12 +5,16 @@ * @package WooCommerce Admin/Classes */ +namespace Automattic\WooCommerce\Admin\API\Reports\Taxes\Stats; + defined( 'ABSPATH' ) || exit; +use \Automattic\WooCommerce\Admin\API\Reports\Segmenter as ReportsSegmenter; + /** * 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). @@ -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). * * @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 ) { if ( ! isset( $this->query_args['segmentby'] ) || '' === $this->query_args['segmentby'] ) { diff --git a/plugins/woocommerce-admin/includes/class-wc-admin-reports-interval.php b/plugins/woocommerce-admin/src/API/Reports/TimeInterval.php similarity index 93% rename from plugins/woocommerce-admin/includes/class-wc-admin-reports-interval.php rename to plugins/woocommerce-admin/src/API/Reports/TimeInterval.php index ff7bc7c1a52..345ba348bc8 100644 --- a/plugins/woocommerce-admin/includes/class-wc-admin-reports-interval.php +++ b/plugins/woocommerce-admin/src/API/Reports/TimeInterval.php @@ -5,12 +5,14 @@ * @package WooCommerce Admin/Classes */ +namespace Automattic\WooCommerce\Admin\API\Reports; + defined( 'ABSPATH' ) || exit; /** * Date & time interval and numeric range handling class for Reporting API. */ -class WC_Admin_Reports_Interval { +class TimeInterval { /** * Format string for ISO DateTime formatter. @@ -33,8 +35,8 @@ class WC_Admin_Reports_Interval { * @return DateTime */ public static function convert_local_datetime_to_gmt( $datetime_string ) { - $datetime = new DateTime( $datetime_string, new DateTimeZone( wc_timezone_string() ) ); - $datetime->setTimezone( new DateTimeZone( 'GMT' ) ); + $datetime = new \DateTime( $datetime_string, new \DateTimeZone( wc_timezone_string() ) ); + $datetime->setTimezone( new \DateTimeZone( 'GMT' ) ); return $datetime; } @@ -44,10 +46,10 @@ class WC_Admin_Reports_Interval { * @return DateTime */ public static function default_before() { - $datetime = new WC_DateTime(); + $datetime = new \WC_DateTime(); // Set local timezone or offset. if ( get_option( 'timezone_string' ) ) { - $datetime->setTimezone( new DateTimeZone( wc_timezone_string() ) ); + $datetime->setTimezone( new \DateTimeZone( wc_timezone_string() ) ); } else { $datetime->set_utc_offset( wc_timezone_offset() ); } @@ -63,11 +65,11 @@ class WC_Admin_Reports_Interval { $now = time(); $week_back = $now - WEEK_IN_SECONDS; - $datetime = new WC_DateTime(); + $datetime = new \WC_DateTime(); $datetime->setTimestamp( $week_back ); // Set local timezone or offset. if ( get_option( 'timezone_string' ) ) { - $datetime->setTimezone( new DateTimeZone( wc_timezone_string() ) ); + $datetime->setTimezone( new \DateTimeZone( wc_timezone_string() ) ); } else { $datetime->set_utc_offset( wc_timezone_offset() ); } @@ -147,7 +149,7 @@ class WC_Admin_Reports_Interval { * @return int */ 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; $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, * 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 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_time = new DateTime(); + $hours_offset_time = new \DateTime(); $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; } @@ -312,9 +314,9 @@ class WC_Admin_Reports_Interval { $next_day_timestamp --; } - $next_day = new DateTime(); + $next_day = new \DateTime(); $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; } @@ -357,7 +359,7 @@ class WC_Admin_Reports_Interval { $month = (int) $datetime->format( 'm' ); 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' ); $end_of_prev_month_timestamp = $timestamp - 1; $datetime->setTimestamp( $end_of_prev_month_timestamp ); @@ -368,7 +370,7 @@ class WC_Admin_Reports_Interval { $year ++; } $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; @@ -424,7 +426,7 @@ class WC_Admin_Reports_Interval { } 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 ) { $timestamp = (int) $datetime->format( 'U' ); $end_of_prev_month_timestamp = $timestamp - 1; @@ -448,13 +450,13 @@ class WC_Admin_Reports_Interval { $day = '01'; 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' ); $end_of_prev_year_timestamp = $timestamp - 1; $datetime->setTimestamp( $end_of_prev_year_timestamp ); } else { $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; @@ -591,7 +593,7 @@ class WC_Admin_Reports_Interval { */ public static function rest_validate_between_numeric_arg( $value, $request, $param ) { if ( ! wp_is_numeric_array( $value ) ) { - return new WP_Error( + return new \WP_Error( 'rest_invalid_param', /* translators: 1: parameter name */ 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[1] ) ) { - return new WP_Error( + return new \WP_Error( 'rest_invalid_param', /* translators: %s: parameter name */ 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 ) { if ( ! wp_is_numeric_array( $value ) ) { - return new WP_Error( + return new \WP_Error( 'rest_invalid_param', /* translators: 1: parameter name */ 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[1] ) ) { - return new WP_Error( + return new \WP_Error( 'rest_invalid_param', /* translators: %s: parameter name */ sprintf( __( '%s must contain 2 valid dates.', 'woocommerce-admin' ), $param ) diff --git a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-variations-controller.php b/plugins/woocommerce-admin/src/API/Reports/Variations/Controller.php similarity index 98% rename from plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-variations-controller.php rename to plugins/woocommerce-admin/src/API/Reports/Variations/Controller.php index a64e187e9da..2fd8398c337 100644 --- a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-variations-controller.php +++ b/plugins/woocommerce-admin/src/API/Reports/Variations/Controller.php @@ -7,6 +7,8 @@ * @package WooCommerce Admin/API */ +namespace Automattic\WooCommerce\Admin\API\Reports\Variations; + defined( 'ABSPATH' ) || exit; /** @@ -15,7 +17,7 @@ defined( 'ABSPATH' ) || exit; * @package WooCommerce/API * @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. @@ -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(); $data = array(); diff --git a/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-variations-data-store.php b/plugins/woocommerce-admin/src/API/Reports/Variations/DataStore.php similarity index 95% rename from plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-variations-data-store.php rename to plugins/woocommerce-admin/src/API/Reports/Variations/DataStore.php index 2905ffa5f25..8605510845e 100644 --- a/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-variations-data-store.php +++ b/plugins/woocommerce-admin/src/API/Reports/Variations/DataStore.php @@ -1,16 +1,22 @@ $product_data ) { - $extended_info = new ArrayObject(); + $extended_info = new \ArrayObject(); if ( $query_args['extended_info'] ) { $extended_attributes = apply_filters( 'woocommerce_rest_reports_variations_extended_attributes', $this->extended_attributes, $product_data ); $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, 'order' => 'DESC', 'orderby' => 'date', - 'before' => WC_Admin_Reports_Interval::default_before(), - 'after' => WC_Admin_Reports_Interval::default_after(), + 'before' => TimeInterval::default_before(), + 'after' => TimeInterval::default_after(), 'fields' => '*', 'products' => array(), 'variations' => array(), diff --git a/plugins/woocommerce-admin/includes/class-wc-admin-reports-variations-query.php b/plugins/woocommerce-admin/src/API/Reports/Variations/Query.php similarity index 73% rename from plugins/woocommerce-admin/includes/class-wc-admin-reports-variations-query.php rename to plugins/woocommerce-admin/src/API/Reports/Variations/Query.php index cc9729e1268..ec09977ca3c 100644 --- a/plugins/woocommerce-admin/includes/class-wc-admin-reports-variations-query.php +++ b/plugins/woocommerce-admin/src/API/Reports/Variations/Query.php @@ -10,18 +10,22 @@ * 'categories' => array(15, 18), * '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(); * * @package WooCommerce Admin/Classes */ +namespace Automattic\WooCommerce\Admin\API\Reports\Variations; + 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. @@ -40,7 +44,7 @@ class WC_Admin_Reports_Variations_Query extends WC_Admin_Reports_Query { public function get_data() { $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 ); return apply_filters( 'woocommerce_reports_variations_select_query', $results, $args ); } diff --git a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-setting-options-controller.php b/plugins/woocommerce-admin/src/API/SettingOptions.php similarity index 77% rename from plugins/woocommerce-admin/includes/api/class-wc-admin-rest-setting-options-controller.php rename to plugins/woocommerce-admin/src/API/SettingOptions.php index bfa1a2fe6ec..64567cbc492 100644 --- a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-setting-options-controller.php +++ b/plugins/woocommerce-admin/src/API/SettingOptions.php @@ -7,6 +7,8 @@ * @package WooCommerce Admin/API */ +namespace Automattic\WooCommerce\Admin\API; + defined( 'ABSPATH' ) || exit; /** @@ -15,7 +17,7 @@ defined( 'ABSPATH' ) || exit; * @package WooCommerce Admin/API * @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. diff --git a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-taxes-controller.php b/plugins/woocommerce-admin/src/API/Taxes.php similarity index 98% rename from plugins/woocommerce-admin/includes/api/class-wc-admin-rest-taxes-controller.php rename to plugins/woocommerce-admin/src/API/Taxes.php index 06698aa8eb1..3a1560a7610 100644 --- a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-taxes-controller.php +++ b/plugins/woocommerce-admin/src/API/Taxes.php @@ -7,6 +7,8 @@ * @package WooCommerce Admin/API */ +namespace Automattic\WooCommerce\Admin\API; + defined( 'ABSPATH' ) || exit; /** @@ -15,7 +17,7 @@ defined( 'ABSPATH' ) || exit; * @package WooCommerce Admin/API * @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. diff --git a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-themes-controller.php b/plugins/woocommerce-admin/src/API/Themes.php similarity index 86% rename from plugins/woocommerce-admin/includes/api/class-wc-admin-rest-themes-controller.php rename to plugins/woocommerce-admin/src/API/Themes.php index df7782cfcb7..c9070624587 100644 --- a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-themes-controller.php +++ b/plugins/woocommerce-admin/src/API/Themes.php @@ -7,15 +7,20 @@ * @package WooCommerce Admin/API */ +namespace Automattic\WooCommerce\Admin\API; + defined( 'ABSPATH' ) || exit; +use Automattic\WooCommerce\Admin\Overrides\ThemeUpgrader; +use Automattic\WooCommerce\Admin\Overrides\ThemeUpgraderSkin; + /** * Themes controller. * * @package WooCommerce Admin/API * @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. * @@ -39,7 +44,7 @@ class WC_Admin_REST_Themes_Controller extends WC_REST_Data_Controller { '/' . $this->rest_base, array( array( - 'methods' => WP_REST_Server::EDITABLE, + 'methods' => \WP_REST_Server::EDITABLE, 'callback' => array( $this, 'upload_theme' ), 'permission_callback' => array( $this, 'upload_theme_permissions_check' ), '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 ) { 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; @@ -71,17 +76,15 @@ class WC_Admin_REST_Themes_Controller extends WC_REST_Data_Controller { */ 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. - 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/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; - $file_upload = new File_Upload_Upgrader( 'pluginzip', 'package' ); - $upgrader = new WC_Admin_Theme_Upgrader( new WC_Admin_Theme_Upgrader_Skin() ); + $file_upload = new \File_Upload_Upgrader( 'pluginzip', 'package' ); + $upgrader = new ThemeUpgrader( new ThemeUpgraderSkin() ); $install = $upgrader->install( $file_upload->package ); if ( $install || is_wp_error( $install ) ) { diff --git a/plugins/woocommerce-admin/includes/class-wc-admin-events.php b/plugins/woocommerce-admin/src/Events.php similarity index 80% rename from plugins/woocommerce-admin/includes/class-wc-admin-events.php rename to plugins/woocommerce-admin/src/Events.php index 851bb6d381a..2114cea02b6 100644 --- a/plugins/woocommerce-admin/includes/class-wc-admin-events.php +++ b/plugins/woocommerce-admin/src/Events.php @@ -6,12 +6,18 @@ * @package Woocommerce Admin */ +namespace Automattic\WooCommerce\Admin; + 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. */ -class WC_Admin_Events { +class Events { /** * 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_Events::instance()->init(); diff --git a/plugins/woocommerce-admin/src/FeaturePlugin.php b/plugins/woocommerce-admin/src/FeaturePlugin.php new file mode 100644 index 00000000000..040486d20a6 --- /dev/null +++ b/plugins/woocommerce-admin/src/FeaturePlugin.php @@ -0,0 +1,316 @@ +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 WooCommerce 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 WordPress 5.2 or greater and WooCommerce 3.6 or greater to be installed and active.', 'woocommerce-admin' ), + 'https://wordpress.org/', + 'https://wordpress.org/plugins/woocommerce/' + ); + } + printf( '

%s

', $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 npm install to install dependencies, npm run build 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 the releases page in the repository.', 'woocommerce-admin' ), + 'https://github.com/woocommerce/woocommerce-admin/releases' + ); + printf( '

%s %s

', $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() {} +} \ No newline at end of file diff --git a/plugins/woocommerce-admin/includes/features/activity-panel/class-wc-admin-activity-panel.php b/plugins/woocommerce-admin/src/Features/ActivityPanels.php similarity index 84% rename from plugins/woocommerce-admin/includes/features/activity-panel/class-wc-admin-activity-panel.php rename to plugins/woocommerce-admin/src/Features/ActivityPanels.php index 3beb1e33562..2e4ddaad8fc 100644 --- a/plugins/woocommerce-admin/includes/features/activity-panel/class-wc-admin-activity-panel.php +++ b/plugins/woocommerce-admin/src/Features/ActivityPanels.php @@ -6,14 +6,19 @@ * @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. */ -class WC_Admin_Activity_Panel { +class ActivityPanels { /** * Class instance. * - * @var WC_Admin_Activity_Panel instance + * @var ActivityPanels instance */ protected static $instance = null; @@ -32,7 +37,7 @@ class WC_Admin_Activity_Panel { */ public function __construct() { 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' ) ); } @@ -69,5 +74,3 @@ class WC_Admin_Activity_Panel { WC_Admin_Notes_Settings_Notes::add_notes_for_settings_that_have_moved(); } } - -new WC_Admin_Activity_Panel(); diff --git a/plugins/woocommerce-admin/includes/features/analytics/class-wc-admin-analytics.php b/plugins/woocommerce-admin/src/Features/Analytics.php similarity index 97% rename from plugins/woocommerce-admin/includes/features/analytics/class-wc-admin-analytics.php rename to plugins/woocommerce-admin/src/Features/Analytics.php index 62fe08f28ac..6646aa57f2b 100644 --- a/plugins/woocommerce-admin/includes/features/analytics/class-wc-admin-analytics.php +++ b/plugins/woocommerce-admin/src/Features/Analytics.php @@ -6,14 +6,16 @@ * @package Woocommerce Admin */ +namespace Automattic\WooCommerce\Admin\Features; + /** * Contains backend logic for the Analytics feature. */ -class WC_Admin_Analytics { +class Analytics { /** * Class instance. * - * @var WC_Admin_Analytics instance + * @var Analytics instance */ protected static $instance = null; @@ -152,5 +154,3 @@ class WC_Admin_Analytics { } } } - -new WC_Admin_Analytics(); diff --git a/plugins/woocommerce-admin/includes/features/analytics-dashboard/class-wc-admin-analytics-dashboard.php b/plugins/woocommerce-admin/src/Features/AnalyticsDashboard.php similarity index 96% rename from plugins/woocommerce-admin/includes/features/analytics-dashboard/class-wc-admin-analytics-dashboard.php rename to plugins/woocommerce-admin/src/Features/AnalyticsDashboard.php index 1296079bd36..ce9f4c64eca 100644 --- a/plugins/woocommerce-admin/includes/features/analytics-dashboard/class-wc-admin-analytics-dashboard.php +++ b/plugins/woocommerce-admin/src/Features/AnalyticsDashboard.php @@ -6,10 +6,12 @@ * @package Woocommerce Admin */ +namespace Automattic\WooCommerce\Admin\Features; + /** * Contains backend logic for the dashboard feature. */ -class WC_Admin_Analytics_Dashboard { +class AnalyticsDashboard { /** * Menu slug. */ @@ -18,7 +20,7 @@ class WC_Admin_Analytics_Dashboard { /** * Class instance. * - * @var WC_Admin_Analytics_Dashboard instance + * @var AnalyticsDashboard instance */ protected static $instance = null; @@ -117,5 +119,3 @@ class WC_Admin_Analytics_Dashboard { array_unshift( $submenu['woocommerce'], $menu ); } } - -new WC_Admin_Analytics_Dashboard(); diff --git a/plugins/woocommerce-admin/includes/features/onboarding/class-wc-admin-onboarding.php b/plugins/woocommerce-admin/src/Features/Onboarding.php similarity index 93% rename from plugins/woocommerce-admin/includes/features/onboarding/class-wc-admin-onboarding.php rename to plugins/woocommerce-admin/src/Features/Onboarding.php index 6d621fa2c06..e1c5b5c53f5 100644 --- a/plugins/woocommerce-admin/includes/features/onboarding/class-wc-admin-onboarding.php +++ b/plugins/woocommerce-admin/src/Features/Onboarding.php @@ -6,14 +6,18 @@ * @package Woocommerce Admin */ +namespace Automattic\WooCommerce\Admin\Features; + +use \Automattic\WooCommerce\Admin\Loader; + /** * Contains backend logic for the onboarding profile and checklist feature. */ -class WC_Admin_Onboarding { +class Onboarding { /** * Class instance. * - * @var WC_Admin_Onboarding instance + * @var Onboarding instance */ protected static $instance = null; @@ -49,8 +53,10 @@ class WC_Admin_Onboarding { return; } - $this->includes(); - add_action( 'woocommerce_components_settings', array( $this, 'component_settings' ), 20 ); // Run after WC_Admin_Loader. + // Include WC Admin Onboarding classes. + OnboardingTasks::get_instance(); + + add_action( 'woocommerce_components_settings', array( $this, 'component_settings' ), 20 ); // Run after Automattic\WooCommerce\Admin\Loader. add_action( 'woocommerce_theme_installed', array( $this, 'delete_themes_transient' ) ); add_action( 'after_switch_theme', array( $this, 'delete_themes_transient' ) ); add_action( 'current_screen', array( $this, 'update_help_tab' ), 60 ); @@ -59,13 +65,6 @@ class WC_Admin_Onboarding { add_filter( 'woocommerce_rest_prepare_themes', array( $this, 'add_uploaded_theme_data' ) ); } - /** - * Include WC Admin Onboarding classes. - */ - public function includes() { - require_once WC_ADMIN_FEATURES_PATH . 'onboarding/class-wc-admin-onboarding-tasks.php'; - } - /** * Returns true if the profiler should be displayed (not completed and not skipped). * @@ -233,9 +232,9 @@ class WC_Admin_Onboarding { } foreach ( $themes as $theme ) { - $directory = new RecursiveDirectoryIterator( $theme->theme_root . '/' . $theme->stylesheet ); - $iterator = new RecursiveIteratorIterator( $directory ); - $files = new RegexIterator( $iterator, '/^.+\.php$/i', RecursiveRegexIterator::GET_MATCH ); + $directory = new \RecursiveDirectoryIterator( $theme->theme_root . '/' . $theme->stylesheet ); + $iterator = new \RecursiveIteratorIterator( $directory ); + $files = new \RegexIterator( $iterator, '/^.+\.php$/i', \RecursiveRegexIterator::GET_MATCH ); foreach ( $files as $file ) { $content = file_get_contents( $file[0] ); @@ -302,7 +301,7 @@ class WC_Admin_Onboarding { $profile = get_option( 'wc_onboarding_profile', array() ); include_once WC_ABSPATH . 'includes/admin/helper/class-wc-helper-options.php'; - $wccom_auth = WC_Helper_Options::get( 'auth' ); + $wccom_auth = \WC_Helper_Options::get( 'auth' ); $profile['wccom_connected'] = empty( $wccom_auth['access_token'] ) ? false : true; $settings['onboarding'] = array( @@ -366,14 +365,14 @@ class WC_Admin_Onboarding { */ public static function reset_onboarding() { if ( - ! WC_Admin_Loader::is_admin_page() || + ! Loader::is_admin_page() || ! isset( $_GET['reset_onboarding'] ) || // WPCS: CSRF ok. 1 !== absint( $_GET['reset_onboarding'] ) // WPCS: CSRF ok. ) { return; } - $request = new WP_REST_Request( 'POST', '/wc-admin/v1/onboarding/profile' ); + $request = new \WP_REST_Request( 'POST', '/wc-admin/v1/onboarding/profile' ); $request->set_headers( array( 'content-type' => 'application/json' ) ); $request->set_body( wp_json_encode( @@ -386,5 +385,3 @@ class WC_Admin_Onboarding { $response = rest_do_request( $request ); } } - -WC_Admin_Onboarding::get_instance(); diff --git a/plugins/woocommerce-admin/includes/features/onboarding/class-wc-admin-onboarding-tasks.php b/plugins/woocommerce-admin/src/Features/OnboardingTasks.php similarity index 93% rename from plugins/woocommerce-admin/includes/features/onboarding/class-wc-admin-onboarding-tasks.php rename to plugins/woocommerce-admin/src/Features/OnboardingTasks.php index 84784c58d1f..eb84a88c02a 100644 --- a/plugins/woocommerce-admin/includes/features/onboarding/class-wc-admin-onboarding-tasks.php +++ b/plugins/woocommerce-admin/src/Features/OnboardingTasks.php @@ -6,14 +6,16 @@ * @package Woocommerce Admin */ +namespace Automattic\WooCommerce\Admin\Features; + /** * Contains the logic for completing onboarding tasks. */ -class WC_Admin_Onboarding_Tasks { +class OnboardingTasks { /** * Class instance. * - * @var WC_Admin_Onboarding_Tasks instance + * @var OnboardingTasks instance */ protected static $instance = null; @@ -45,7 +47,7 @@ class WC_Admin_Onboarding_Tasks { * Constructor */ public function __construct() { - add_action( 'woocommerce_components_settings', array( $this, 'component_settings' ), 30 ); // Run after WC_Admin_Onboarding. + add_action( 'woocommerce_components_settings', array( $this, 'component_settings' ), 30 ); // Run after Onboarding. add_action( 'admin_init', array( $this, 'set_active_task' ), 20 ); add_action( 'admin_init', array( $this, 'check_active_task_completion' ), 1 ); } @@ -127,5 +129,3 @@ class WC_Admin_Onboarding_Tasks { return false; } } - -WC_Admin_Onboarding_Tasks::get_instance(); diff --git a/plugins/woocommerce-admin/includes/class-wc-admin-install.php b/plugins/woocommerce-admin/src/Install.php similarity index 97% rename from plugins/woocommerce-admin/includes/class-wc-admin-install.php rename to plugins/woocommerce-admin/src/Install.php index f47cf4d861c..804c1222f29 100644 --- a/plugins/woocommerce-admin/includes/class-wc-admin-install.php +++ b/plugins/woocommerce-admin/src/Install.php @@ -5,12 +5,17 @@ * @package WooCommerce Admin/Classes */ +namespace Automattic\WooCommerce\Admin; + defined( 'ABSPATH' ) || exit; +use \Automattic\WooCommerce\Admin\Notes\WC_Admin_Notes_Historical_Data; +use \Automattic\WooCommerce\Admin\Notes\WC_Admin_Notes_Welcome_Message; + /** - * WC_Admin_Install Class. + * Install Class. */ -class WC_Admin_Install { +class Install { /** * Plugin version option name. */ @@ -288,5 +293,3 @@ class WC_Admin_Install { } } } - -WC_Admin_Install::init(); diff --git a/plugins/woocommerce-admin/includes/class-wc-admin-loader.php b/plugins/woocommerce-admin/src/Loader.php similarity index 89% rename from plugins/woocommerce-admin/includes/class-wc-admin-loader.php rename to plugins/woocommerce-admin/src/Loader.php index 6e88c48d882..12e1e6e5ed3 100644 --- a/plugins/woocommerce-admin/includes/class-wc-admin-loader.php +++ b/plugins/woocommerce-admin/src/Loader.php @@ -6,10 +6,12 @@ * @package Woocommerce Admin */ +namespace Automattic\WooCommerce\Admin; + /** - * WC_Admin_Loader Class. + * Loader Class. */ -class WC_Admin_Loader { +class Loader { /** * App entry point. */ @@ -18,7 +20,7 @@ class WC_Admin_Loader { /** * Class instance. * - * @var WC_Admin_Loader instance + * @var Loader instance */ protected static $instance = null; @@ -44,23 +46,23 @@ class WC_Admin_Loader { * Hooks added here should be removed in `wc_admin_initialize` via the feature plugin. */ public function __construct() { - add_action( 'init', array( 'WC_Admin_Loader', 'load_features' ) ); - add_action( 'admin_enqueue_scripts', array( 'WC_Admin_Loader', 'register_scripts' ) ); - add_action( 'admin_enqueue_scripts', array( 'WC_Admin_Loader', 'load_scripts' ), 15 ); - add_action( 'woocommerce_components_settings', array( 'WC_Admin_Loader', 'add_component_settings' ) ); - add_filter( 'admin_body_class', array( 'WC_Admin_Loader', 'add_admin_body_classes' ) ); - add_action( 'admin_menu', array( 'WC_Admin_Loader', 'register_page_handler' ) ); - add_filter( 'admin_title', array( 'WC_Admin_Loader', 'update_admin_title' ) ); - add_action( 'rest_api_init', array( 'WC_Admin_Loader', 'register_user_data' ) ); - add_action( 'in_admin_header', array( 'WC_Admin_Loader', 'embed_page_header' ) ); - add_filter( 'woocommerce_settings_groups', array( 'WC_Admin_Loader', 'add_settings_group' ) ); - add_filter( 'woocommerce_settings-wc_admin', array( 'WC_Admin_Loader', 'add_settings' ) ); - add_action( 'admin_head', array( 'WC_Admin_Loader', 'remove_notices' ) ); - add_action( 'admin_notices', array( 'WC_Admin_Loader', 'inject_before_notices' ) ); - add_action( 'admin_notices', array( 'WC_Admin_Loader', 'inject_after_notices' ), PHP_INT_MAX ); + add_action( 'init', array( __CLASS__, 'load_features' ) ); + add_action( 'admin_enqueue_scripts', array( __CLASS__, 'register_scripts' ) ); + add_action( 'admin_enqueue_scripts', array( __CLASS__, 'load_scripts' ), 15 ); + add_action( 'woocommerce_components_settings', array( __CLASS__, 'add_component_settings' ) ); + add_filter( 'admin_body_class', array( __CLASS__, 'add_admin_body_classes' ) ); + add_action( 'admin_menu', array( __CLASS__, 'register_page_handler' ) ); + add_filter( 'admin_title', array( __CLASS__, 'update_admin_title' ) ); + add_action( 'rest_api_init', array( __CLASS__, 'register_user_data' ) ); + add_action( 'in_admin_header', array( __CLASS__, 'embed_page_header' ) ); + add_filter( 'woocommerce_settings_groups', array( __CLASS__, 'add_settings_group' ) ); + add_filter( 'woocommerce_settings-wc_admin', array( __CLASS__, 'add_settings' ) ); + add_action( 'admin_head', array( __CLASS__, 'remove_notices' ) ); + add_action( 'admin_notices', array( __CLASS__, 'inject_before_notices' ) ); + add_action( 'admin_notices', array( __CLASS__, 'inject_after_notices' ), PHP_INT_MAX ); // priority is 20 to run after https://github.com/woocommerce/woocommerce/blob/a55ae325306fc2179149ba9b97e66f32f84fdd9c/includes/admin/class-wc-admin-menus.php#L165. - add_action( 'admin_head', array( 'WC_Admin_Loader', 'remove_app_entry_page_menu_item' ), 20 ); + add_action( 'admin_head', array( __CLASS__, 'remove_app_entry_page_menu_item' ), 20 ); /* * Remove the emoji script as it always defaults to replacing emojis with Twemoji images. @@ -127,17 +129,13 @@ class WC_Admin_Loader { * Class loader for enabled WooCommerce Admin features/sections. */ public static function load_features() { - require_once WC_ADMIN_ABSPATH . 'includes/page-controller/class-wc-admin-page-controller.php'; - require_once WC_ADMIN_ABSPATH . 'includes/page-controller/page-controller-functions.php'; - $features = self::get_features(); foreach ( $features as $feature ) { - $feature = strtolower( $feature ); - $file = WC_ADMIN_FEATURES_PATH . $feature . '/class-wc-admin-' . $feature . '.php'; - if ( file_exists( $file ) ) { - require_once $file; - $feature = ucfirst( $feature ); - self::$classes[] = 'WC_Admin_' . $feature; + $feature = str_replace( '-', '', ucwords( strtolower( $feature ), '-' ) ); + $feature = 'Automattic\\WooCommerce\\Admin\\Features\\' . $feature; + + if ( class_exists( $feature ) ) { + new $feature; } } } @@ -160,7 +158,7 @@ class WC_Admin_Loader { ); // Connect existing WooCommerce pages. - require_once WC_ADMIN_ABSPATH . 'includes/page-controller/connect-existing-pages.php'; + require_once WC_ADMIN_ABSPATH . 'includes/connect-existing-pages.php'; } /** @@ -542,7 +540,7 @@ class WC_Admin_Loader { $settings['notifyLowStockAmount'] = get_option( 'woocommerce_notify_low_stock_amount' ); // @todo On merge, once plugin images are added to core WooCommerce, `wcAdminAssetUrl` can be retired, // and `wcAssetUrl` can be used in its place throughout the codebase. - $settings['wcAdminAssetUrl'] = plugins_url( 'images/', plugin_dir_path( dirname( __FILE__ ) ) . 'woocommerce-admin.php' ); + $settings['wcAdminAssetUrl'] = plugins_url( 'images/', plugin_dir_path( dirname( __DIR__ ) ) . 'woocommerce-admin.php' ); if ( ! empty( $preload_data_endpoints ) ) { foreach ( $preload_data_endpoints as $key => $endpoint ) { @@ -637,7 +635,7 @@ class WC_Admin_Loader { * @return array */ public static function get_custom_settings( $settings ) { - $wc_rest_settings_options_controller = new WC_REST_Setting_Options_Controller(); + $wc_rest_settings_options_controller = new \WC_REST_Setting_Options_Controller(); $wc_admin_group_settings = $wc_rest_settings_options_controller->get_group_settings( 'wc_admin' ); $settings['wcAdminSettings'] = array(); @@ -685,15 +683,15 @@ class WC_Admin_Loader { 'user', 'woocommerce_meta', array( - 'get_callback' => array( 'WC_Admin_Loader', 'get_user_data_values' ), - 'update_callback' => array( 'WC_Admin_Loader', 'update_user_data_values' ), + 'get_callback' => array( __CLASS__, 'get_user_data_values' ), + 'update_callback' => array( __CLASS__, 'update_user_data_values' ), 'schema' => null, ) ); } /** - * For all the registered user data fields ( WC_Admin_Loader::get_user_data_fields ), fetch the data + * For all the registered user data fields ( Loader::get_user_data_fields ), fetch the data * for returning via the REST API. * * @param WP_User $user Current user. @@ -707,7 +705,7 @@ class WC_Admin_Loader { } /** - * For all the registered user data fields ( WC_Admin_Loader::get_user_data_fields ), update the data + * For all the registered user data fields ( Loader::get_user_data_fields ), update the data * for the REST API. * * @param array $values The new values for the meta. @@ -740,5 +738,3 @@ class WC_Admin_Loader { return apply_filters( 'wc_admin_get_user_data_fields', array() ); } } - -new WC_Admin_Loader(); diff --git a/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-notes-data-store.php b/plugins/woocommerce-admin/src/Notes/DataStore.php similarity index 97% rename from plugins/woocommerce-admin/includes/data-stores/class-wc-admin-notes-data-store.php rename to plugins/woocommerce-admin/src/Notes/DataStore.php index 130c6da904e..f62a0a0c42e 100644 --- a/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-notes-data-store.php +++ b/plugins/woocommerce-admin/src/Notes/DataStore.php @@ -5,12 +5,14 @@ * @package WooCommerce Admin/Classes */ +namespace Automattic\WooCommerce\Admin\Notes; + defined( 'ABSPATH' ) || exit; /** * WC Admin Note Data Store (Custom Tables) */ -class WC_Admin_Notes_Data_Store extends WC_Data_Store_WP implements WC_Object_Data_Store_Interface { +class DataStore extends \WC_Data_Store_WP implements \WC_Object_Data_Store_Interface { /** * Method to create a new note in the database. * @@ -110,7 +112,7 @@ class WC_Admin_Notes_Data_Store extends WC_Data_Store_WP implements WC_Object_Da */ do_action( 'woocommerce_admin_note_loaded', $note ); } else { - throw new Exception( __( 'Invalid data store for admin note.', 'woocommerce-admin' ) ); + throw new \Exception( __( 'Invalid data store for admin note.', 'woocommerce-admin' ) ); } } diff --git a/plugins/woocommerce-admin/includes/notes/class-wc-admin-note.php b/plugins/woocommerce-admin/src/Notes/WC_Admin_Note.php similarity index 98% rename from plugins/woocommerce-admin/includes/notes/class-wc-admin-note.php rename to plugins/woocommerce-admin/src/Notes/WC_Admin_Note.php index 0c71d404c6f..5e520d50abd 100644 --- a/plugins/woocommerce-admin/includes/notes/class-wc-admin-note.php +++ b/plugins/woocommerce-admin/src/Notes/WC_Admin_Note.php @@ -7,12 +7,14 @@ * @package WooCommerce Admin/Classes */ +namespace Automattic\WooCommerce\Admin\Notes; + defined( 'ABSPATH' ) || exit; /** * WC_Admin_Note class. */ -class WC_Admin_Note extends WC_Data { +class WC_Admin_Note extends \WC_Data { // Note types. const E_WC_ADMIN_NOTE_ERROR = 'error'; // used for presenting error conditions. @@ -53,7 +55,7 @@ class WC_Admin_Note extends WC_Data { 'title' => '-', 'content' => '-', 'icon' => 'info', - 'content_data' => new stdClass(), + 'content_data' => new \stdClass(), 'status' => self::E_WC_ADMIN_NOTE_UNACTIONED, 'source' => 'woocommerce', 'date_created' => '0000-00-00 00:00:00', @@ -76,7 +78,7 @@ class WC_Admin_Note extends WC_Data { $this->set_object_read( true ); } - $this->data_store = WC_Data_Store::load( 'admin-note' ); + $this->data_store = \WC_Data_Store::load( 'admin-note' ); if ( $this->get_id() > 0 ) { $this->data_store->read( $this ); } @@ -391,7 +393,7 @@ class WC_Admin_Note extends WC_Data { $allowed_type = false; // Make sure $content_data is stdClass Object or an array. - if ( ! ( $content_data instanceof stdClass ) ) { + if ( ! ( $content_data instanceof \stdClass ) ) { $this->error( 'admin_note_invalid_data', __( 'The admin note content_data prop must be an instance of stdClass.', 'woocommerce-admin' ) ); } diff --git a/plugins/woocommerce-admin/includes/notes/class-wc-admin-notes.php b/plugins/woocommerce-admin/src/Notes/WC_Admin_Notes.php similarity index 91% rename from plugins/woocommerce-admin/includes/notes/class-wc-admin-notes.php rename to plugins/woocommerce-admin/src/Notes/WC_Admin_Notes.php index a211b680ea0..36ec632f6b7 100644 --- a/plugins/woocommerce-admin/includes/notes/class-wc-admin-notes.php +++ b/plugins/woocommerce-admin/src/Notes/WC_Admin_Notes.php @@ -5,6 +5,8 @@ * @package WooCommerce Admin/Classes */ +namespace Automattic\WooCommerce\Admin\Notes; + if ( ! defined( 'ABSPATH' ) ) { exit; } @@ -58,7 +60,7 @@ class WC_Admin_Notes { * @return array Array of arrays. */ public static function get_notes( $context = 'edit', $args = array() ) { - $data_store = WC_Data_Store::load( 'admin-note' ); + $data_store = \WC_Data_Store::load( 'admin-note' ); $raw_notes = $data_store->get_notes( $args ); $notes = array(); foreach ( (array) $raw_notes as $raw_note ) { @@ -91,7 +93,7 @@ class WC_Admin_Notes { if ( false !== $note_id ) { try { return new WC_Admin_Note( $note_id ); - } catch ( Exception $e ) { + } catch ( \Exception $e ) { return false; } } @@ -106,7 +108,7 @@ class WC_Admin_Notes { * @return int */ public static function get_notes_count( $type = array(), $status = array() ) { - $data_store = WC_Data_Store::load( 'admin-note' ); + $data_store = \WC_Data_Store::load( 'admin-note' ); return $data_store->get_notes_count( $type, $status ); } @@ -116,7 +118,7 @@ class WC_Admin_Notes { * @param string $name Name to search for. */ public static function delete_notes_with_name( $name ) { - $data_store = WC_Data_Store::load( 'admin-note' ); + $data_store = \WC_Data_Store::load( 'admin-note' ); $note_ids = $data_store->get_notes_with_name( $name ); foreach ( (array) $note_ids as $note_id ) { $note = new WC_Admin_Note( $note_id ); @@ -128,13 +130,13 @@ class WC_Admin_Notes { * Clear note snooze status if the reminder date has been reached. */ public static function unsnooze_notes() { - $data_store = WC_Data_Store::load( 'admin-note' ); + $data_store = \WC_Data_Store::load( 'admin-note' ); $raw_notes = $data_store->get_notes( array( 'status' => array( WC_Admin_Note::E_WC_ADMIN_NOTE_SNOOZED ), ) ); - $now = new DateTime(); + $now = new \DateTime(); foreach ( $raw_notes as $raw_note ) { $note = new WC_Admin_Note( $raw_note ); @@ -170,9 +172,9 @@ class WC_Admin_Notes { * Clears all queued actions. */ public static function clear_queued_actions() { - $store = ActionScheduler::store(); + $store = \ActionScheduler::store(); - if ( is_a( $store, 'WC_Admin_ActionScheduler_WPPostStore' ) ) { + if ( is_a( $store, 'Automattic\WooCommerce\Admin\Overrides\WPPostStore' ) ) { // If we're using our data store, call our bespoke deletion method. $action_types = array( self::UNSNOOZE_HOOK ); $store->clear_pending_wcadmin_actions( $action_types ); @@ -181,5 +183,3 @@ class WC_Admin_Notes { } } } - -WC_Admin_Notes::init(); diff --git a/plugins/woocommerce-admin/includes/notes/class-wc-admin-notes-giving-feedback-notes.php b/plugins/woocommerce-admin/src/Notes/WC_Admin_Notes_Giving_Feedback_Notes.php similarity index 95% rename from plugins/woocommerce-admin/includes/notes/class-wc-admin-notes-giving-feedback-notes.php rename to plugins/woocommerce-admin/src/Notes/WC_Admin_Notes_Giving_Feedback_Notes.php index 83e1e2add84..bec83a58332 100644 --- a/plugins/woocommerce-admin/includes/notes/class-wc-admin-notes-giving-feedback-notes.php +++ b/plugins/woocommerce-admin/src/Notes/WC_Admin_Notes_Giving_Feedback_Notes.php @@ -7,6 +7,8 @@ * @package WooCommerce Admin */ +namespace Automattic\WooCommerce\Admin\Notes; + defined( 'ABSPATH' ) || exit; /** @@ -26,7 +28,7 @@ class WC_Admin_Notes_Giving_Feedback_Notes { protected static function possibly_add_admin_giving_feedback_note() { $name = 'wc-admin-store-notice-giving-feedback'; - $data_store = WC_Data_Store::load( 'admin-note' ); + $data_store = \WC_Data_Store::load( 'admin-note' ); // We already have this note? Then exit, we're done. $note_ids = $data_store->get_notes_with_name( $name ); diff --git a/plugins/woocommerce-admin/includes/notes/class-wc-admin-notes-historical-data.php b/plugins/woocommerce-admin/src/Notes/WC_Admin_Notes_Historical_Data.php similarity index 88% rename from plugins/woocommerce-admin/includes/notes/class-wc-admin-notes-historical-data.php rename to plugins/woocommerce-admin/src/Notes/WC_Admin_Notes_Historical_Data.php index 030f6296a67..0bb6c70c9e8 100644 --- a/plugins/woocommerce-admin/includes/notes/class-wc-admin-notes-historical-data.php +++ b/plugins/woocommerce-admin/src/Notes/WC_Admin_Notes_Historical_Data.php @@ -7,8 +7,12 @@ * @package WooCommerce Admin */ +namespace Automattic\WooCommerce\Admin\Notes; + defined( 'ABSPATH' ) || exit; +use \Automattic\WooCommerce\Admin\Install; + /** * WC_Admin_Notes_Historical_Data. */ @@ -19,13 +23,13 @@ class WC_Admin_Notes_Historical_Data { * Creates a note for regenerating historical data. */ public static function add_note() { - $is_upgrading = get_option( WC_Admin_Install::VERSION_OPTION ); + $is_upgrading = get_option( Install::VERSION_OPTION ); if ( $is_upgrading ) { return; } // First, see if orders exist and if we've already created this kind of note so we don't do it again. - $data_store = WC_Data_Store::load( 'admin-note' ); + $data_store = \WC_Data_Store::load( 'admin-note' ); $note_ids = $data_store->get_notes_with_name( self::NOTE_NAME ); $orders = wc_get_orders( array( @@ -56,5 +60,3 @@ class WC_Admin_Notes_Historical_Data { $note->save(); } } - -new WC_Admin_Notes_Historical_Data(); diff --git a/plugins/woocommerce-admin/includes/notes/class-wc-admin-notes-mobile-app.php b/plugins/woocommerce-admin/src/Notes/WC_Admin_Notes_Mobile_App.php similarity index 94% rename from plugins/woocommerce-admin/includes/notes/class-wc-admin-notes-mobile-app.php rename to plugins/woocommerce-admin/src/Notes/WC_Admin_Notes_Mobile_App.php index bf935f3e1c5..c2e7aeab6b1 100644 --- a/plugins/woocommerce-admin/includes/notes/class-wc-admin-notes-mobile-app.php +++ b/plugins/woocommerce-admin/src/Notes/WC_Admin_Notes_Mobile_App.php @@ -7,6 +7,8 @@ * @package WooCommerce Admin */ +namespace Automattic\WooCommerce\Admin\Notes; + defined( 'ABSPATH' ) || exit; /** @@ -38,7 +40,7 @@ class WC_Admin_Notes_Mobile_App { return; } - $data_store = WC_Data_Store::load( 'admin-note' ); + $data_store = \WC_Data_Store::load( 'admin-note' ); // We already have this note? Then exit, we're done. $note_ids = $data_store->get_notes_with_name( self::NOTE_NAME ); diff --git a/plugins/woocommerce-admin/includes/notes/class-wc-admin-notes-new-sales-record.php b/plugins/woocommerce-admin/src/Notes/WC_Admin_Notes_New_Sales_Record.php similarity index 96% rename from plugins/woocommerce-admin/includes/notes/class-wc-admin-notes-new-sales-record.php rename to plugins/woocommerce-admin/src/Notes/WC_Admin_Notes_New_Sales_Record.php index e73f37a211e..283b883e325 100644 --- a/plugins/woocommerce-admin/includes/notes/class-wc-admin-notes-new-sales-record.php +++ b/plugins/woocommerce-admin/src/Notes/WC_Admin_Notes_New_Sales_Record.php @@ -7,6 +7,8 @@ * @package WooCommerce Admin */ +namespace Automattic\WooCommerce\Admin\Notes; + defined( 'ABSPATH' ) || exit; /** @@ -24,7 +26,7 @@ class WC_Admin_Notes_New_Sales_Record { * @return floatval */ public static function sum_sales_for_date( $date ) { - $order_query = new WC_Order_Query( array( 'date_created' => $date ) ); + $order_query = new \WC_Order_Query( array( 'date_created' => $date ) ); $orders = $order_query->get_orders(); $total = 0; diff --git a/plugins/woocommerce-admin/includes/notes/class-wc-admin-notes-order-milestones.php b/plugins/woocommerce-admin/src/Notes/WC_Admin_Notes_Order_Milestones.php similarity index 99% rename from plugins/woocommerce-admin/includes/notes/class-wc-admin-notes-order-milestones.php rename to plugins/woocommerce-admin/src/Notes/WC_Admin_Notes_Order_Milestones.php index e41779c5637..ba389b286ab 100644 --- a/plugins/woocommerce-admin/includes/notes/class-wc-admin-notes-order-milestones.php +++ b/plugins/woocommerce-admin/src/Notes/WC_Admin_Notes_Order_Milestones.php @@ -7,6 +7,8 @@ * @package WooCommerce Admin */ +namespace Automattic\WooCommerce\Admin\Notes; + defined( 'ABSPATH' ) || exit; /** @@ -319,5 +321,3 @@ class WC_Admin_Notes_Order_Milestones { $note->save(); } } - -new WC_Admin_Notes_Order_Milestones(); diff --git a/plugins/woocommerce-admin/includes/notes/class-wc-admin-notes-settings-notes.php b/plugins/woocommerce-admin/src/Notes/WC_Admin_Notes_Settings_Notes.php similarity index 93% rename from plugins/woocommerce-admin/includes/notes/class-wc-admin-notes-settings-notes.php rename to plugins/woocommerce-admin/src/Notes/WC_Admin_Notes_Settings_Notes.php index 06c4b1a8c65..31caf056253 100644 --- a/plugins/woocommerce-admin/includes/notes/class-wc-admin-notes-settings-notes.php +++ b/plugins/woocommerce-admin/src/Notes/WC_Admin_Notes_Settings_Notes.php @@ -7,6 +7,8 @@ * @package WooCommerce Admin */ +namespace Automattic\WooCommerce\Admin\Notes; + defined( 'ABSPATH' ) || exit; /** @@ -26,7 +28,7 @@ class WC_Admin_Notes_Settings_Notes { protected static function possibly_add_notice_setting_moved_note() { $name = 'wc-admin-store-notice-setting-moved'; - $data_store = WC_Data_Store::load( 'admin-note' ); + $data_store = \WC_Data_Store::load( 'admin-note' ); // We already have this note? Then exit, we're done. $note_ids = $data_store->get_notes_with_name( $name ); diff --git a/plugins/woocommerce-admin/includes/notes/class-wc-admin-notes-welcome-message.php b/plugins/woocommerce-admin/src/Notes/WC_Admin_Notes_Welcome_Message.php similarity index 88% rename from plugins/woocommerce-admin/includes/notes/class-wc-admin-notes-welcome-message.php rename to plugins/woocommerce-admin/src/Notes/WC_Admin_Notes_Welcome_Message.php index 0df1e0c860a..3f048c21f36 100644 --- a/plugins/woocommerce-admin/includes/notes/class-wc-admin-notes-welcome-message.php +++ b/plugins/woocommerce-admin/src/Notes/WC_Admin_Notes_Welcome_Message.php @@ -7,7 +7,12 @@ * @package WooCommerce Admin */ +namespace Automattic\WooCommerce\Admin\Notes; + defined( 'ABSPATH' ) || exit; + +use \Automattic\WooCommerce\Admin\Install; + /** * WC_Admin_Notes_Welcome_Message. */ @@ -19,13 +24,13 @@ class WC_Admin_Notes_Welcome_Message { */ public static function add_welcome_note() { // Check if plugin is upgrading if yes then don't create this note. - $is_upgrading = get_option( WC_Admin_Install::VERSION_OPTION ); + $is_upgrading = get_option( Install::VERSION_OPTION ); if ( $is_upgrading ) { return; } // See if we've already created this kind of note so we don't do it again. - $data_store = WC_Data_Store::load( 'admin-note' ); + $data_store = \WC_Data_Store::load( 'admin-note' ); $note_ids = $data_store->get_notes_with_name( self::NOTE_NAME ); if ( ! empty( $note_ids ) ) { return; @@ -48,5 +53,3 @@ class WC_Admin_Notes_Welcome_Message { $note->save(); } } - -new WC_Admin_Notes_Welcome_Message(); diff --git a/plugins/woocommerce-admin/includes/notes/class-wc-admin-notes-woo-subscriptions-notes.php b/plugins/woocommerce-admin/src/Notes/WC_Admin_Notes_Woo_Subscriptions_Notes.php similarity index 97% rename from plugins/woocommerce-admin/includes/notes/class-wc-admin-notes-woo-subscriptions-notes.php rename to plugins/woocommerce-admin/src/Notes/WC_Admin_Notes_Woo_Subscriptions_Notes.php index 27a78d959a0..a97af6e4ae2 100644 --- a/plugins/woocommerce-admin/includes/notes/class-wc-admin-notes-woo-subscriptions-notes.php +++ b/plugins/woocommerce-admin/src/Notes/WC_Admin_Notes_Woo_Subscriptions_Notes.php @@ -7,6 +7,8 @@ * @package WooCommerce Admin */ +namespace Automattic\WooCommerce\Admin\Notes; + defined( 'ABSPATH' ) || exit; /** @@ -104,7 +106,7 @@ class WC_Admin_Notes_Woo_Subscriptions_Notes { */ public function check_connection() { if ( ! $this->is_connected() ) { - $data_store = WC_Data_Store::load( 'admin-note' ); + $data_store = \WC_Data_Store::load( 'admin-note' ); $note_ids = $data_store->get_notes_with_name( self::CONNECTION_NOTE_NAME ); if ( ! empty( $note_ids ) ) { // We already have a connection note. Exit early. @@ -122,7 +124,7 @@ class WC_Admin_Notes_Woo_Subscriptions_Notes { * @return bool */ public function is_connected() { - $auth = WC_Helper_Options::get( 'auth' ); + $auth = \WC_Helper_Options::get( 'auth' ); return ( ! empty( $auth['access_token'] ) ); } @@ -136,7 +138,7 @@ class WC_Admin_Notes_Woo_Subscriptions_Notes { return false; } - $auth = WC_Helper_Options::get( 'auth' ); + $auth = \WC_Helper_Options::get( 'auth' ); return absint( $auth['site_id'] ); } @@ -154,7 +156,7 @@ class WC_Admin_Notes_Woo_Subscriptions_Notes { $product_ids = array(); if ( $this->is_connected() ) { - $subscriptions = WC_Helper::get_subscriptions(); + $subscriptions = \WC_Helper::get_subscriptions(); foreach ( (array) $subscriptions as $subscription ) { if ( in_array( $site_id, $subscription['connections'], true ) ) { @@ -216,7 +218,7 @@ class WC_Admin_Notes_Woo_Subscriptions_Notes { public function prune_inactive_subscription_notes() { $active_product_ids = $this->get_subscription_active_product_ids(); - $data_store = WC_Data_Store::load( 'admin-note' ); + $data_store = \WC_Data_Store::load( 'admin-note' ); $note_ids = $data_store->get_notes_with_name( self::SUBSCRIPTION_NOTE_NAME ); foreach ( (array) $note_ids as $note_id ) { @@ -239,7 +241,7 @@ class WC_Admin_Notes_Woo_Subscriptions_Notes { public function find_note_for_product_id( $product_id ) { $product_id = intval( $product_id ); - $data_store = WC_Data_Store::load( 'admin-note' ); + $data_store = \WC_Data_Store::load( 'admin-note' ); $note_ids = $data_store->get_notes_with_name( self::SUBSCRIPTION_NOTE_NAME ); foreach ( (array) $note_ids as $note_id ) { $note = WC_Admin_Notes::get_note( $note_id ); @@ -418,7 +420,7 @@ class WC_Admin_Notes_Woo_Subscriptions_Notes { $this->prune_inactive_subscription_notes(); - $subscriptions = WC_Helper::get_subscriptions(); + $subscriptions = \WC_Helper::get_subscriptions(); $active_product_ids = $this->get_subscription_active_product_ids(); foreach ( (array) $subscriptions as $subscription ) { @@ -455,5 +457,3 @@ class WC_Admin_Notes_Woo_Subscriptions_Notes { } } } - -new WC_Admin_Notes_Woo_Subscriptions_Notes(); diff --git a/plugins/woocommerce-admin/includes/class-wc-admin-order.php b/plugins/woocommerce-admin/src/Overrides/Order.php similarity index 72% rename from plugins/woocommerce-admin/includes/class-wc-admin-order.php rename to plugins/woocommerce-admin/src/Overrides/Order.php index 705b82b3f19..c597f3a30e3 100644 --- a/plugins/woocommerce-admin/includes/class-wc-admin-order.php +++ b/plugins/woocommerce-admin/src/Overrides/Order.php @@ -7,16 +7,21 @@ * @package WooCommerce Admin/Classes */ +namespace Automattic\WooCommerce\Admin\Overrides; + defined( 'ABSPATH' ) || exit; +use \Automattic\WooCommerce\Admin\API\Reports\Customers\DataStore as CustomersDataStore; +use \Automattic\WooCommerce\Admin\API\Reports\Orders\Stats\DataStore as OrdersStatsDataStore; + /** - * WC_Admin_Order class. + * WC_Order subclass. */ -class WC_Admin_Order extends WC_Order { +class Order extends \WC_Order { /** * Order traits. */ - use WC_Admin_Order_Trait; + use OrderTraits; /** * Holds refund amounts and quantities for the order. @@ -26,14 +31,14 @@ class WC_Admin_Order extends WC_Order { protected $refunded_line_items; /** - * Add filter(s) required to hook WC_Admin_Order class to substitute WC_Order. + * Add filter(s) required to hook this class to substitute WC_Order. */ public static function add_filters() { add_filter( 'woocommerce_order_class', array( __CLASS__, 'order_class_name' ), 10, 3 ); } /** - * Filter function to swap class WC_Order for WC_Admin_Order in cases when it's suitable. + * Filter function to swap class WC_Order for this one in cases when it's suitable. * * @param string $classname Name of the class to be created. * @param string $order_type Type of order object to be created. @@ -42,8 +47,9 @@ class WC_Admin_Order extends WC_Order { * @return string */ public static function order_class_name( $classname, $order_type, $order_id ) { + // @todo - Only substitute class when necessary (during sync). if ( 'WC_Order' === $classname ) { - return 'WC_Admin_Order'; + return '\Automattic\WooCommerce\Admin\Overrides\Order'; } else { return $classname; } @@ -55,7 +61,7 @@ class WC_Admin_Order extends WC_Order { * @return int */ public function get_report_customer_id() { - return WC_Admin_Reports_Customers_Data_Store::get_or_create_customer_from_order( $this ); + return CustomersDataStore::get_or_create_customer_from_order( $this ); } /** @@ -64,7 +70,7 @@ class WC_Admin_Order extends WC_Order { * @return bool */ public function is_returning_customer() { - return WC_Admin_Reports_Orders_Stats_Data_Store::is_returning_customer( $this ); + return OrdersStatsDataStore::is_returning_customer( $this ); } /** diff --git a/plugins/woocommerce-admin/includes/class-wc-admin-order-refund.php b/plugins/woocommerce-admin/src/Overrides/OrderRefund.php similarity index 68% rename from plugins/woocommerce-admin/includes/class-wc-admin-order-refund.php rename to plugins/woocommerce-admin/src/Overrides/OrderRefund.php index f2450ec34ba..13ef80ba728 100644 --- a/plugins/woocommerce-admin/includes/class-wc-admin-order-refund.php +++ b/plugins/woocommerce-admin/src/Overrides/OrderRefund.php @@ -7,26 +7,30 @@ * @package WooCommerce Admin/Classes */ +namespace Automattic\WooCommerce\Admin\Overrides; + defined( 'ABSPATH' ) || exit; +use \Automattic\WooCommerce\Admin\API\Reports\Customers\DataStore as CustomersDataStore; + /** - * WC_Admin_Order_Refund class. + * WC_Order_Refund subclass. */ -class WC_Admin_Order_Refund extends WC_Order_Refund { +class OrderRefund extends \WC_Order_Refund { /** * Order traits. */ - use WC_Admin_Order_Trait; + use OrderTraits; /** - * Add filter(s) required to hook WC_Admin_Order_Refund class to substitute WC_Order_Refund. + * Add filter(s) required to hook this class to substitute WC_Order_Refund. */ public static function add_filters() { add_filter( 'woocommerce_order_class', array( __CLASS__, 'order_class_name' ), 10, 3 ); } /** - * Filter function to swap class WC_Order_Refund for WC_Admin_Order_Refund in cases when it's suitable. + * Filter function to swap class WC_Order_Refund for this one in cases when it's suitable. * * @param string $classname Name of the class to be created. * @param string $order_type Type of order object to be created. @@ -35,8 +39,9 @@ class WC_Admin_Order_Refund extends WC_Order_Refund { * @return string */ public static function order_class_name( $classname, $order_type, $order_id ) { + // @todo - Only substitute class when necessary (during sync). if ( 'WC_Order_Refund' === $classname ) { - return 'WC_Admin_Order_Refund'; + return '\Automattic\WooCommerce\Admin\Overrides\OrderRefund'; } else { return $classname; } @@ -54,7 +59,7 @@ class WC_Admin_Order_Refund extends WC_Order_Refund { return false; } - return WC_Admin_Reports_Customers_Data_Store::get_or_create_customer_from_order( $parent_order ); + return CustomersDataStore::get_or_create_customer_from_order( $parent_order ); } /** diff --git a/plugins/woocommerce-admin/includes/class-wc-admin-order-trait.php b/plugins/woocommerce-admin/src/Overrides/OrderTraits.php similarity index 96% rename from plugins/woocommerce-admin/includes/class-wc-admin-order-trait.php rename to plugins/woocommerce-admin/src/Overrides/OrderTraits.php index bee2b37b6c0..477cff9c4ed 100644 --- a/plugins/woocommerce-admin/includes/class-wc-admin-order-trait.php +++ b/plugins/woocommerce-admin/src/Overrides/OrderTraits.php @@ -7,12 +7,14 @@ * @package WooCommerce Admin/Classes */ +namespace Automattic\WooCommerce\Admin\Overrides; + defined( 'ABSPATH' ) || exit; /** - * WC_Admin_Order_Trait class. + * OrderTraits class. */ -trait WC_Admin_Order_Trait { +trait OrderTraits { /** * Calculate shipping amount for line item/product as a total shipping amount ratio based on quantity. * diff --git a/plugins/woocommerce-admin/includes/class-wc-admin-theme-upgrader.php b/plugins/woocommerce-admin/src/Overrides/ThemeUpgrader.php similarity index 93% rename from plugins/woocommerce-admin/includes/class-wc-admin-theme-upgrader.php rename to plugins/woocommerce-admin/src/Overrides/ThemeUpgrader.php index 6c9b9a64b43..a0b8aed4798 100644 --- a/plugins/woocommerce-admin/includes/class-wc-admin-theme-upgrader.php +++ b/plugins/woocommerce-admin/src/Overrides/ThemeUpgrader.php @@ -5,12 +5,14 @@ * @package WooCommerce Admin/Classes */ +namespace Automattic\WooCommerce\Admin\Overrides; + defined( 'ABSPATH' ) || exit; /** - * WC_Admin_Theme_Upgrader Class. + * Admin\Overrides\ThemeUpgrader Class. */ -class WC_Admin_Theme_Upgrader extends Theme_Upgrader { +class ThemeUpgrader extends \Theme_Upgrader { /** * Install a theme package. * diff --git a/plugins/woocommerce-admin/includes/class-wc-admin-theme-upgrader-skin.php b/plugins/woocommerce-admin/src/Overrides/ThemeUpgraderSkin.php similarity index 77% rename from plugins/woocommerce-admin/includes/class-wc-admin-theme-upgrader-skin.php rename to plugins/woocommerce-admin/src/Overrides/ThemeUpgraderSkin.php index bf533c39767..df2518c7e4e 100644 --- a/plugins/woocommerce-admin/includes/class-wc-admin-theme-upgrader-skin.php +++ b/plugins/woocommerce-admin/src/Overrides/ThemeUpgraderSkin.php @@ -5,12 +5,14 @@ * @package WooCommerce Admin/Classes */ +namespace Automattic\WooCommerce\Admin\Overrides; + defined( 'ABSPATH' ) || exit; /** - * WC_Admin_Theme_Upgrader_Skin Class. + * Admin\Overrides\ThemeUpgraderSkin Class. */ -class WC_Admin_Theme_Upgrader_Skin extends Theme_Upgrader_Skin { +class ThemeUpgraderSkin extends \Theme_Upgrader_Skin { /** * Hide the skin header display. */ diff --git a/plugins/woocommerce-admin/includes/class-wc-admin-actionscheduler-wppoststore.php b/plugins/woocommerce-admin/src/Overrides/WPPostStore.php similarity index 87% rename from plugins/woocommerce-admin/includes/class-wc-admin-actionscheduler-wppoststore.php rename to plugins/woocommerce-admin/src/Overrides/WPPostStore.php index b3bfe6178d6..4d2c1812e8c 100644 --- a/plugins/woocommerce-admin/includes/class-wc-admin-actionscheduler-wppoststore.php +++ b/plugins/woocommerce-admin/src/Overrides/WPPostStore.php @@ -5,10 +5,12 @@ * @package WooCommerce Admin/Classes */ +namespace Automattic\WooCommerce\Admin\Overrides; + /** * Class WC Admin Action Scheduler Store. */ -class WC_Admin_ActionScheduler_WPPostStore extends ActionScheduler_wpPostStore { +class WPPostStore extends \ActionScheduler_wpPostStore { /** * Action scheduler job priority (lower numbers are claimed first). */ @@ -24,7 +26,7 @@ class WC_Admin_ActionScheduler_WPPostStore extends ActionScheduler_wpPostStore { * @param DateTime $scheduled_date Action schedule. * @return array Post data array for usage in wp_insert_post(). */ - protected function create_post_array( ActionScheduler_Action $action, DateTime $scheduled_date = null ) { + protected function create_post_array( \ActionScheduler_Action $action, \DateTime $scheduled_date = null ) { $postdata = parent::create_post_array( $action, $scheduled_date ); if ( 0 === strpos( $postdata['post_title'], 'wc-admin_' ) ) { diff --git a/plugins/woocommerce-admin/includes/page-controller/class-wc-admin-page-controller.php b/plugins/woocommerce-admin/src/PageController.php similarity index 98% rename from plugins/woocommerce-admin/includes/page-controller/class-wc-admin-page-controller.php rename to plugins/woocommerce-admin/src/PageController.php index cf6f64df70e..1618096baab 100644 --- a/plugins/woocommerce-admin/includes/page-controller/class-wc-admin-page-controller.php +++ b/plugins/woocommerce-admin/src/PageController.php @@ -1,21 +1,25 @@ get_emails() ) ); $tabs_with_sections = apply_filters( diff --git a/plugins/woocommerce-admin/includes/export/class-wc-admin-report-csv-exporter.php b/plugins/woocommerce-admin/src/ReportCSVExporter.php similarity index 85% rename from plugins/woocommerce-admin/includes/export/class-wc-admin-report-csv-exporter.php rename to plugins/woocommerce-admin/src/ReportCSVExporter.php index 887952edcb9..98229d8ea27 100644 --- a/plugins/woocommerce-admin/includes/export/class-wc-admin-report-csv-exporter.php +++ b/plugins/woocommerce-admin/src/ReportCSVExporter.php @@ -5,6 +5,8 @@ * @package WooCommerce/Export */ +namespace Automattic\WooCommerce\Admin; + if ( ! defined( 'ABSPATH' ) ) { exit; } @@ -17,9 +19,9 @@ if ( ! class_exists( 'WC_CSV_Batch_Exporter', false ) ) { } /** - * WC_Admin_Report_CSV_Exporter Class. + * ReportCSVExporter Class. */ -class WC_Admin_Report_CSV_Exporter extends WC_CSV_Batch_Exporter { +class ReportCSVExporter extends \WC_CSV_Batch_Exporter { /** * Type of report being exported. * @@ -102,15 +104,15 @@ class WC_Admin_Report_CSV_Exporter extends WC_CSV_Batch_Exporter { */ protected function map_report_controller() { $controller_map = array( - 'products' => 'WC_Admin_REST_Reports_Products_Controller', - 'variations' => 'WC_Admin_REST_Reports_Variations_Controller', - 'orders' => 'WC_Admin_REST_Reports_Orders_Controller', - 'categories' => 'WC_Admin_REST_Reports_Categories_Controller', - 'taxes' => 'WC_Admin_REST_Reports_Taxes_Controller', - 'coupons' => 'WC_Admin_REST_Reports_Coupons_Controller', - 'stock' => 'WC_Admin_REST_Reports_Stock_Controller', - 'downloads' => 'WC_Admin_REST_Reports_Downloads_Controller', - 'customers' => 'WC_Admin_REST_Reports_Customers_Controller', + 'products' => 'Automattic\WooCommerce\Admin\API\Reports\Products\Controller', + 'variations' => 'Automattic\WooCommerce\Admin\API\Reports\Variations\Controller', + 'orders' => 'Automattic\WooCommerce\Admin\API\Reports\Orders\Controller', + 'categories' => 'Automattic\WooCommerce\Admin\API\Reports\Categories\Controller', + 'taxes' => 'Automattic\WooCommerce\Admin\API\Reports\Taxes\Controller', + 'coupons' => 'Automattic\WooCommerce\Admin\API\Reports\Coupons\Controller', + 'stock' => 'Automattic\WooCommerce\Admin\API\Reports\Stock\Controller', + 'downloads' => 'Automattic\WooCommerce\Admin\API\Reports\Downloads\Controller', + 'customers' => 'Automattic\WooCommerce\Admin\API\Reports\Customers\Controller', ); if ( isset( $controller_map[ $this->report_type ] ) ) { @@ -175,7 +177,7 @@ class WC_Admin_Report_CSV_Exporter extends WC_CSV_Batch_Exporter { * Prepare data for export. */ public function prepare_data_to_export() { - $request = new WP_REST_Request( 'GET', "/wc/v4/reports/{$this->report_type}" ); + $request = new \WP_REST_Request( 'GET', "/wc/v4/reports/{$this->report_type}" ); $params = $this->controller->get_collection_params(); $defaults = array(); diff --git a/plugins/woocommerce-admin/includes/export/class-wc-admin-report-exporter.php b/plugins/woocommerce-admin/src/ReportExporter.php similarity index 92% rename from plugins/woocommerce-admin/includes/export/class-wc-admin-report-exporter.php rename to plugins/woocommerce-admin/src/ReportExporter.php index cf592b29332..aad7e06bd21 100644 --- a/plugins/woocommerce-admin/includes/export/class-wc-admin-report-exporter.php +++ b/plugins/woocommerce-admin/src/ReportExporter.php @@ -5,14 +5,16 @@ * @package WooCommerce/Export */ +namespace Automattic\WooCommerce\Admin; + if ( ! defined( 'ABSPATH' ) ) { exit; } /** - * WC_Admin_Report_Exporter Class. + * ReportExporter Class. */ -class WC_Admin_Report_Exporter { +class ReportExporter { /** * Action hook for generating a report export. */ @@ -81,7 +83,7 @@ class WC_Admin_Report_Exporter { * @return int Number of items to export. */ public static function queue_report_export( $export_id, $report_type, $report_args = array() ) { - $exporter = new WC_Admin_Report_CSV_Exporter( $report_type, $report_args ); + $exporter = new ReportCSVExporter( $report_type, $report_args ); $exporter->prepare_data_to_export(); $total_rows = $exporter->get_total_rows(); @@ -93,7 +95,7 @@ class WC_Admin_Report_Exporter { $report_batch_args = array( $export_id, $report_type, $report_args ); if ( 0 < $num_batches ) { - WC_Admin_Reports_Sync::queue_batches( 1, $num_batches, self::REPORT_EXPORT_ACTION, $report_batch_args ); + ReportsSync::queue_batches( 1, $num_batches, self::REPORT_EXPORT_ACTION, $report_batch_args ); } return $total_rows; @@ -111,7 +113,7 @@ class WC_Admin_Report_Exporter { public static function report_export_action( $page_number, $export_id, $report_type, $report_args ) { $report_args['page'] = $page_number; - $exporter = new WC_Admin_Report_CSV_Exporter( $report_type, $report_args ); + $exporter = new ReportCSVExporter( $report_type, $report_args ); $exporter->set_filename( "wc-{$report_type}-report-export-{$export_id}" ); $exporter->generate_file(); @@ -175,11 +177,9 @@ class WC_Admin_Report_Exporter { self::DOWNLOAD_EXPORT_ACTION === wp_unslash( $_GET['action'] ) && // WPCS: input var ok, sanitization ok. current_user_can( 'view_woocommerce_reports' ) ) { - $exporter = new WC_Admin_Report_CSV_Exporter(); + $exporter = new ReportCSVExporter(); $exporter->set_filename( wp_unslash( $_GET['filename'] ) ); // WPCS: input var ok, sanitization ok. $exporter->export(); } } } - -WC_Admin_Report_Exporter::init(); diff --git a/plugins/woocommerce-admin/includes/class-wc-admin-reports-sync.php b/plugins/woocommerce-admin/src/ReportsSync.php similarity index 93% rename from plugins/woocommerce-admin/includes/class-wc-admin-reports-sync.php rename to plugins/woocommerce-admin/src/ReportsSync.php index c3f6e828e7a..3564bf2505a 100644 --- a/plugins/woocommerce-admin/includes/class-wc-admin-reports-sync.php +++ b/plugins/woocommerce-admin/src/ReportsSync.php @@ -5,12 +5,20 @@ * @package WooCommerce Admin/Classes */ +namespace Automattic\WooCommerce\Admin; + defined( 'ABSPATH' ) || exit; +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\Orders\Stats\DataStore as OrdersStatsDataStore; +use \Automattic\WooCommerce\Admin\API\Reports\Products\DataStore as ProductsDataStore; +use \Automattic\WooCommerce\Admin\API\Reports\Taxes\DataStore as TaxesDataStore; + /** - * WC_Admin_Reports_Sync Class. + * ReportsSync Class. */ -class WC_Admin_Reports_Sync { +class ReportsSync { /** * Action hook for reducing a range of batches down to single actions. */ @@ -124,7 +132,7 @@ class WC_Admin_Reports_Sync { */ public static function regenerate_report_data( $days, $skip_existing ) { if ( self::is_importing() ) { - return new WP_Error( 'wc_admin_import_in_progress', __( 'An import is already in progress. Please allow the previous import to complete before beginning a new one.', 'woocommerce-admin' ) ); + return new \WP_Error( 'wc_admin_import_in_progress', __( 'An import is already in progress. Please allow the previous import to complete before beginning a new one.', 'woocommerce-admin' ) ); } self::reset_import_stats( $days, $skip_existing ); @@ -151,7 +159,7 @@ class WC_Admin_Reports_Sync { $previous_import_date = get_option( 'wc_admin_imported_from_date' ); $current_import_date = $days ? date( 'Y-m-d 00:00:00', time() - ( DAY_IN_SECONDS * $days ) ) : -1; - if ( ! $previous_import_date || -1 === $current_import_date || new DateTime( $previous_import_date ) > new DateTime( $current_import_date ) ) { + if ( ! $previous_import_date || -1 === $current_import_date || new \DateTime( $previous_import_date ) > new \DateTime( $current_import_date ) ) { update_option( 'wc_admin_imported_from_date', $current_import_date ); } } @@ -205,9 +213,9 @@ class WC_Admin_Reports_Sync { * Clears all queued actions. */ public static function clear_queued_actions() { - $store = ActionScheduler::store(); + $store = \ActionScheduler::store(); - if ( is_a( $store, 'WC_Admin_ActionScheduler_WPPostStore' ) ) { + if ( is_a( $store, 'Automattic\WooCommerce\Admin\Overrides\WPPostStore' ) ) { // If we're using our data store, call our bespoke deletion method. $action_types = array( self::QUEUE_BATCH_ACTION, @@ -304,18 +312,18 @@ class WC_Admin_Reports_Sync { */ public static function orders_lookup_update_init() { // Activate WC_Order extension. - WC_Admin_Order::add_filters(); - WC_Admin_Order_Refund::add_filters(); + \Automattic\WooCommerce\Admin\Overrides\Order::add_filters(); + \Automattic\WooCommerce\Admin\Overrides\OrderRefund::add_filters(); // Order and refund data must be run on these hooks to ensure meta data is set. add_action( 'save_post', array( __CLASS__, 'schedule_single_order_import' ) ); add_action( 'woocommerce_refund_created', array( __CLASS__, 'schedule_single_order_import' ) ); - WC_Admin_Reports_Orders_Stats_Data_Store::init(); - WC_Admin_Reports_Customers_Data_Store::init(); - WC_Admin_Reports_Coupons_Data_Store::init(); - WC_Admin_Reports_Products_Data_Store::init(); - WC_Admin_Reports_Taxes_Data_Store::init(); + OrdersStatsDataStore::init(); + CustomersDataStore::init(); + CouponsDataStore::init(); + ProductsDataStore::init(); + TaxesDataStore::init(); } /** @@ -454,10 +462,10 @@ class WC_Admin_Reports_Sync { $result = array_sum( array( - WC_Admin_Reports_Orders_Stats_Data_Store::sync_order( $order_id ), - WC_Admin_Reports_Products_Data_Store::sync_order_products( $order_id ), - WC_Admin_Reports_Coupons_Data_Store::sync_order_coupons( $order_id ), - WC_Admin_Reports_Taxes_Data_Store::sync_order_taxes( $order_id ), + OrdersStatsDataStore::sync_order( $order_id ), + ProductsDataStore::sync_order_products( $order_id ), + CouponsDataStore::sync_order_coupons( $order_id ), + TaxesDataStore::sync_order_taxes( $order_id ), ) ); @@ -627,7 +635,7 @@ class WC_Admin_Reports_Sync { add_action( 'pre_user_query', array( __CLASS__, 'exclude_existing_customers_from_query' ) ); } - $customer_query = new WP_User_Query( $query_args ); + $customer_query = new \WP_User_Query( $query_args ); remove_action( 'pre_user_query', array( __CLASS__, 'exclude_existing_customers_from_query' ) ); @@ -704,7 +712,7 @@ class WC_Admin_Reports_Sync { foreach ( $customer_ids as $customer_id ) { // @todo Schedule single customer update if this fails? - WC_Admin_Reports_Customers_Data_Store::update_registered_customer( $customer_id ); + CustomersDataStore::update_registered_customer( $customer_id ); } $imported_count = get_option( 'wc_admin_import_customers_count', 0 ); @@ -749,7 +757,7 @@ class WC_Admin_Reports_Sync { ); foreach ( $customer_ids as $customer_id ) { - WC_Admin_Reports_Customers_Data_Store::delete_customer( $customer_id ); + CustomersDataStore::delete_customer( $customer_id ); } wc_admin_record_tracks_event( 'delete_import_data_job_complete', array( 'type' => 'customer' ) ); @@ -791,11 +799,9 @@ class WC_Admin_Reports_Sync { ); foreach ( $order_ids as $order_id ) { - WC_Admin_Reports_Orders_Stats_Data_Store::delete_order( $order_id ); + OrdersStatsDataStore::delete_order( $order_id ); } wc_admin_record_tracks_event( 'delete_import_data_job_complete', array( 'type' => 'order' ) ); } } - -WC_Admin_Reports_Sync::init(); diff --git a/plugins/woocommerce-admin/tests/api-init.php b/plugins/woocommerce-admin/tests/api-init.php index 29163dbedb1..e27ec7294f2 100644 --- a/plugins/woocommerce-admin/tests/api-init.php +++ b/plugins/woocommerce-admin/tests/api-init.php @@ -6,6 +6,9 @@ * @since 3.5.0 */ +use Automattic\WooCommerce\Admin\ReportsSync; +use \Automattic\WooCommerce\Admin\API\Reports\Orders\Stats\DataStore as OrdersStatsDataStore; + /** * Class WC_Tests_API_Init */ @@ -16,7 +19,7 @@ class WC_Tests_API_Init extends WC_REST_Unit_Test_Case { public function setUp() { parent::setUp(); $this->queue = new WC_Admin_Test_Action_Queue(); - WC_Admin_Reports_Sync::set_queue( $this->queue ); + ReportsSync::set_queue( $this->queue ); } /** @@ -24,7 +27,7 @@ class WC_Tests_API_Init extends WC_REST_Unit_Test_Case { */ public function tearDown() { parent::tearDown(); - WC_Admin_Reports_Sync::set_queue( null ); + ReportsSync::set_queue( null ); $this->queue->actions = array(); } @@ -39,7 +42,7 @@ class WC_Tests_API_Init extends WC_REST_Unit_Test_Case { if ( 0 === strpos( $query, 'REPLACE INTO' ) && - false !== strpos( $query, WC_Admin_Reports_Orders_Stats_Data_Store::TABLE_NAME ) + false !== strpos( $query, OrdersStatsDataStore::TABLE_NAME ) ) { remove_filter( 'query', array( $this, 'filter_order_query' ) ); return "DESCRIBE $wpdb->posts"; // Execute any random query. @@ -72,13 +75,13 @@ class WC_Tests_API_Init extends WC_REST_Unit_Test_Case { add_filter( 'query', array( $this, 'filter_order_query' ) ); // Initiate sync. - WC_Admin_Reports_Sync::orders_lookup_import_order( $order->get_id() ); + ReportsSync::orders_lookup_import_order( $order->get_id() ); // Verify that a retry job was scheduled. $this->assertCount( 1, $this->queue->actions ); $this->assertArraySubset( array( - 'hook' => WC_Admin_Reports_Sync::SINGLE_ORDER_IMPORT_ACTION, + 'hook' => ReportsSync::SINGLE_ORDER_IMPORT_ACTION, 'args' => array( $order->get_id() ), ), $this->queue->actions[0] diff --git a/plugins/woocommerce-admin/tests/api/admin-notes.php b/plugins/woocommerce-admin/tests/api/admin-notes.php index e6124dc735d..b46b9bb7001 100644 --- a/plugins/woocommerce-admin/tests/api/admin-notes.php +++ b/plugins/woocommerce-admin/tests/api/admin-notes.php @@ -5,6 +5,9 @@ * @package WooCommerce\Tests\API */ +use Automattic\WooCommerce\Admin\Notes\WC_Admin_Note; +use Automattic\WooCommerce\Admin\Notes\WC_Admin_Notes; + /** * Class WC_Tests_API_Admin_Notes */ diff --git a/plugins/woocommerce-admin/tests/api/onboarding-profile.php b/plugins/woocommerce-admin/tests/api/onboarding-profile.php index cf75477a02a..1baf66c889e 100644 --- a/plugins/woocommerce-admin/tests/api/onboarding-profile.php +++ b/plugins/woocommerce-admin/tests/api/onboarding-profile.php @@ -5,6 +5,8 @@ * @package WooCommerce Admin\Tests\API */ +use \Automattic\WooCommerce\Admin\API\OnboardingProfile; + /** * WC Tests API Onboarding Profile */ @@ -42,7 +44,7 @@ class WC_Tests_API_Onboarding_Profiles extends WC_REST_Unit_Test_Case { $this->assertEquals( 200, $response->get_status() ); - $properties = WC_Admin_REST_Onboarding_Profile_Controller::get_profile_properties(); + $properties = OnboardingProfile::get_profile_properties(); foreach ( $properties as $key => $property ) { $this->assertArrayHasKey( $key, $properties ); } diff --git a/plugins/woocommerce-admin/tests/api/orders.php b/plugins/woocommerce-admin/tests/api/orders.php index f6ec715c3a1..2d01f05fe24 100644 --- a/plugins/woocommerce-admin/tests/api/orders.php +++ b/plugins/woocommerce-admin/tests/api/orders.php @@ -5,6 +5,8 @@ * @package WooCommerce Admin\Tests\API */ +use \Automattic\WooCommerce\Admin\API\Reports\Orders\Stats\DataStore as OrdersStatsDataStore; + /** * WC Tests API Orders */ @@ -80,6 +82,6 @@ class WC_Tests_API_Orders extends WC_REST_Unit_Test_Case { clean_post_cache( $order->get_id() ); // Trigger an order sync on the refund which should handle the missing parent order. - $this->assertTrue( WC_Admin_Reports_Orders_Stats_Data_Store::sync_order( $refund->get_id() ) ); + $this->assertTrue( OrdersStatsDataStore::sync_order( $refund->get_id() ) ); } } diff --git a/plugins/woocommerce-admin/tests/api/reports-customers.php b/plugins/woocommerce-admin/tests/api/reports-customers.php index fb8af68af0a..d167191f5a3 100644 --- a/plugins/woocommerce-admin/tests/api/reports-customers.php +++ b/plugins/woocommerce-admin/tests/api/reports-customers.php @@ -6,6 +6,8 @@ * @since 3.5.0 */ +use \Automattic\WooCommerce\Admin\API\Reports\Customers\DataStore as CustomersDataStore; + /** * Reports Customers REST API Test Class * @@ -103,7 +105,7 @@ class WC_Tests_API_Reports_Customers extends WC_REST_Unit_Test_Case { * @since 3.5.0 */ public function test_update_registered_customer_with_bad_user_id() { - $result = WC_Admin_Reports_Customers_Data_Store::update_registered_customer( 2 ); + $result = CustomersDataStore::update_registered_customer( 2 ); $this->assertFalse( $result ); } @@ -424,7 +426,7 @@ class WC_Tests_API_Reports_Customers extends WC_REST_Unit_Test_Case { * Test that bad order params don't cause PHP errors when retrieving customers. */ public function test_customer_retrieval_from_order_bad_order() { - $this->assertFalse( WC_Admin_Reports_Customers_Data_Store::get_existing_customer_id_from_order( false ) ); - $this->assertFalse( WC_Admin_Reports_Customers_Data_Store::get_or_create_customer_from_order( false ) ); + $this->assertFalse( CustomersDataStore::get_existing_customer_id_from_order( false ) ); + $this->assertFalse( CustomersDataStore::get_or_create_customer_from_order( false ) ); } } diff --git a/plugins/woocommerce-admin/tests/api/reports-import.php b/plugins/woocommerce-admin/tests/api/reports-import.php index 21b46108022..fc40da4d1f6 100644 --- a/plugins/woocommerce-admin/tests/api/reports-import.php +++ b/plugins/woocommerce-admin/tests/api/reports-import.php @@ -5,6 +5,8 @@ * @package WooCommerce\Tests\API */ +use Automattic\WooCommerce\Admin\ReportsSync; + /** * Reports Import REST API Test Class * @@ -291,7 +293,7 @@ class WC_Tests_API_Reports_Import extends WC_REST_Unit_Test_Case { */ public function test_import_status() { // Delete any pending actions that weren't fully run. - WC_Admin_Reports_Sync::clear_queued_actions(); + ReportsSync::clear_queued_actions(); wp_set_current_user( $this->user ); diff --git a/plugins/woocommerce-admin/tests/api/reports-interval.php b/plugins/woocommerce-admin/tests/api/reports-interval.php index 9881ae69f8b..b6cc88afdd2 100644 --- a/plugins/woocommerce-admin/tests/api/reports-interval.php +++ b/plugins/woocommerce-admin/tests/api/reports-interval.php @@ -6,6 +6,8 @@ * @since x.x.0 */ +use \Automattic\WooCommerce\Admin\API\Reports\TimeInterval; + /** * Class WC_Tests_Reports_Interval_Stats */ @@ -30,31 +32,31 @@ class WC_Tests_Reports_Interval_Stats extends WC_Unit_Test_Case { */ public function test_quarter() { $datetime = new DateTime( '2017-12-31T00:00:00', self::$local_tz ); - $this->assertEquals( 4, WC_Admin_Reports_Interval::quarter( $datetime ) ); + $this->assertEquals( 4, TimeInterval::quarter( $datetime ) ); $datetime = new DateTime( '2018-01-01T00:00:00', self::$local_tz ); - $this->assertEquals( 1, WC_Admin_Reports_Interval::quarter( $datetime ) ); + $this->assertEquals( 1, TimeInterval::quarter( $datetime ) ); $datetime = new DateTime( '2018-03-31T23:59:59', self::$local_tz ); - $this->assertEquals( 1, WC_Admin_Reports_Interval::quarter( $datetime ) ); + $this->assertEquals( 1, TimeInterval::quarter( $datetime ) ); $datetime = new DateTime( '2018-04-01T00:00:00', self::$local_tz ); - $this->assertEquals( 2, WC_Admin_Reports_Interval::quarter( $datetime ) ); + $this->assertEquals( 2, TimeInterval::quarter( $datetime ) ); $datetime = new DateTime( '2018-06-30T23:59:59', self::$local_tz ); - $this->assertEquals( 2, WC_Admin_Reports_Interval::quarter( $datetime ) ); + $this->assertEquals( 2, TimeInterval::quarter( $datetime ) ); $datetime = new DateTime( '2018-07-01T00:00:00', self::$local_tz ); - $this->assertEquals( 3, WC_Admin_Reports_Interval::quarter( $datetime ) ); + $this->assertEquals( 3, TimeInterval::quarter( $datetime ) ); $datetime = new DateTime( '2018-09-30T23:59:59', self::$local_tz ); - $this->assertEquals( 3, WC_Admin_Reports_Interval::quarter( $datetime ) ); + $this->assertEquals( 3, TimeInterval::quarter( $datetime ) ); $datetime = new DateTime( '2018-10-01T00:00:00', self::$local_tz ); - $this->assertEquals( 4, WC_Admin_Reports_Interval::quarter( $datetime ) ); + $this->assertEquals( 4, TimeInterval::quarter( $datetime ) ); $datetime = new DateTime( '2018-12-31T23:59:59', self::$local_tz ); - $this->assertEquals( 4, WC_Admin_Reports_Interval::quarter( $datetime ) ); + $this->assertEquals( 4, TimeInterval::quarter( $datetime ) ); } /** @@ -337,7 +339,7 @@ class WC_Tests_Reports_Interval_Stats extends WC_Unit_Test_Case { foreach ( $expected_week_no as $date => $week_numbers ) { for ( $first_day_of_week = 1; $first_day_of_week <= 7; $first_day_of_week++ ) { $datetime = new DateTime( $date, self::$local_tz ); - $this->assertEquals( $expected_week_no[ $date ][ $first_day_of_week ], WC_Admin_Reports_Interval::simple_week_number( $datetime, $first_day_of_week ), "First day of week: $first_day_of_week; Date: $date" ); + $this->assertEquals( $expected_week_no[ $date ][ $first_day_of_week ], TimeInterval::simple_week_number( $datetime, $first_day_of_week ), "First day of week: $first_day_of_week; Date: $date" ); } } @@ -381,7 +383,7 @@ class WC_Tests_Reports_Interval_Stats extends WC_Unit_Test_Case { ); foreach ( $expected_week_no as $date => $week_numbers ) { $datetime = new DateTime( $date, self::$local_tz ); - $this->assertEquals( $expected_week_no[ $date ], WC_Admin_Reports_Interval::week_number( $datetime, 1 ), "ISO week number for date: $date" ); + $this->assertEquals( $expected_week_no[ $date ], TimeInterval::week_number( $datetime, 1 ), "ISO week number for date: $date" ); } } @@ -617,7 +619,7 @@ class WC_Tests_Reports_Interval_Stats extends WC_Unit_Test_Case { foreach ( $setting['intervals'] as $interval => $exp_value ) { $start_datetime = new DateTime( $setting['start'], self::$local_tz ); $end_datetime = new DateTime( $setting['end'], self::$local_tz ); - $this->assertEquals( $exp_value, WC_Admin_Reports_Interval::intervals_between( $start_datetime, $end_datetime, $interval ), "First Day of Week: {$setting['week_start']}; Start: {$setting['start']}; End: {$setting['end']}; Interval: {$interval}" ); + $this->assertEquals( $exp_value, TimeInterval::intervals_between( $start_datetime, $end_datetime, $interval ), "First Day of Week: {$setting['week_start']}; Start: {$setting['start']}; End: {$setting['end']}; Interval: {$interval}" ); } } } @@ -639,8 +641,8 @@ class WC_Tests_Reports_Interval_Stats extends WC_Unit_Test_Case { foreach ( $settings as $datetime_s => $setting ) { $datetime = new DateTime( $datetime_s, self::$local_tz ); foreach ( $setting as $reversed => $exp_value ) { - $result_dt = WC_Admin_Reports_Interval::next_hour_start( $datetime, $reversed ); - $this->assertEquals( $exp_value, $result_dt->format( WC_Admin_Reports_Interval::$iso_datetime_format ), __FUNCTION__ . ": DT: $datetime_s; R: $reversed" ); + $result_dt = TimeInterval::next_hour_start( $datetime, $reversed ); + $this->assertEquals( $exp_value, $result_dt->format( TimeInterval::$iso_datetime_format ), __FUNCTION__ . ": DT: $datetime_s; R: $reversed" ); } } } @@ -662,8 +664,8 @@ class WC_Tests_Reports_Interval_Stats extends WC_Unit_Test_Case { foreach ( $settings as $datetime_s => $setting ) { $datetime = new DateTime( $datetime_s, self::$local_tz ); foreach ( $setting as $reversed => $exp_value ) { - $result_dt = WC_Admin_Reports_Interval::next_day_start( $datetime, $reversed ); - $this->assertEquals( $exp_value, $result_dt->format( WC_Admin_Reports_Interval::$iso_datetime_format ), __FUNCTION__ . ": DT: $datetime_s; R: $reversed" ); + $result_dt = TimeInterval::next_day_start( $datetime, $reversed ); + $this->assertEquals( $exp_value, $result_dt->format( TimeInterval::$iso_datetime_format ), __FUNCTION__ . ": DT: $datetime_s; R: $reversed" ); } } } @@ -710,8 +712,8 @@ class WC_Tests_Reports_Interval_Stats extends WC_Unit_Test_Case { foreach ( $settings as $datetime_s => $setting ) { $datetime = new DateTime( $datetime_s, self::$local_tz ); foreach ( $setting as $reversed => $exp_value ) { - $result_dt = WC_Admin_Reports_Interval::next_week_start( $datetime, $reversed ); - $this->assertEquals( $exp_value, $result_dt->format( WC_Admin_Reports_Interval::$iso_datetime_format ), __FUNCTION__ . ": DT: $datetime_s; R: $reversed" ); + $result_dt = TimeInterval::next_week_start( $datetime, $reversed ); + $this->assertEquals( $exp_value, $result_dt->format( TimeInterval::$iso_datetime_format ), __FUNCTION__ . ": DT: $datetime_s; R: $reversed" ); } } } @@ -742,8 +744,8 @@ class WC_Tests_Reports_Interval_Stats extends WC_Unit_Test_Case { foreach ( $settings as $datetime_s => $setting ) { $datetime = new DateTime( $datetime_s, self::$local_tz ); foreach ( $setting as $reversed => $exp_value ) { - $result_dt = WC_Admin_Reports_Interval::next_week_start( $datetime, $reversed ); - $this->assertEquals( $exp_value, $result_dt->format( WC_Admin_Reports_Interval::$iso_datetime_format ), __FUNCTION__ . ": DT: $datetime_s; R: $reversed" ); + $result_dt = TimeInterval::next_week_start( $datetime, $reversed ); + $this->assertEquals( $exp_value, $result_dt->format( TimeInterval::$iso_datetime_format ), __FUNCTION__ . ": DT: $datetime_s; R: $reversed" ); } } } @@ -766,8 +768,8 @@ class WC_Tests_Reports_Interval_Stats extends WC_Unit_Test_Case { foreach ( $settings as $datetime_s => $setting ) { $datetime = new DateTime( $datetime_s, self::$local_tz ); foreach ( $setting as $reversed => $exp_value ) { - $result_dt = WC_Admin_Reports_Interval::next_month_start( $datetime, $reversed ); - $this->assertEquals( $exp_value, $result_dt->format( WC_Admin_Reports_Interval::$iso_datetime_format ), __FUNCTION__ . ": DT: $datetime_s; R: $reversed" ); + $result_dt = TimeInterval::next_month_start( $datetime, $reversed ); + $this->assertEquals( $exp_value, $result_dt->format( TimeInterval::$iso_datetime_format ), __FUNCTION__ . ": DT: $datetime_s; R: $reversed" ); } } } @@ -801,8 +803,8 @@ class WC_Tests_Reports_Interval_Stats extends WC_Unit_Test_Case { foreach ( $settings as $datetime_s => $setting ) { $datetime = new DateTime( $datetime_s, self::$local_tz ); foreach ( $setting as $reversed => $exp_value ) { - $result_dt = WC_Admin_Reports_Interval::next_quarter_start( $datetime, $reversed ); - $this->assertEquals( $exp_value, $result_dt->format( WC_Admin_Reports_Interval::$iso_datetime_format ), __FUNCTION__ . ": DT: $datetime_s; R: $reversed" ); + $result_dt = TimeInterval::next_quarter_start( $datetime, $reversed ); + $this->assertEquals( $exp_value, $result_dt->format( TimeInterval::$iso_datetime_format ), __FUNCTION__ . ": DT: $datetime_s; R: $reversed" ); } } } @@ -828,8 +830,8 @@ class WC_Tests_Reports_Interval_Stats extends WC_Unit_Test_Case { foreach ( $settings as $datetime_s => $setting ) { $datetime = new DateTime( $datetime_s, self::$local_tz ); foreach ( $setting as $reversed => $exp_value ) { - $result_dt = WC_Admin_Reports_Interval::next_year_start( $datetime, $reversed ); - $this->assertEquals( $exp_value, $result_dt->format( WC_Admin_Reports_Interval::$iso_datetime_format ), __FUNCTION__ . ": DT: $datetime_s; R: $reversed" ); + $result_dt = TimeInterval::next_year_start( $datetime, $reversed ); + $this->assertEquals( $exp_value, $result_dt->format( TimeInterval::$iso_datetime_format ), __FUNCTION__ . ": DT: $datetime_s; R: $reversed" ); } } } @@ -846,7 +848,7 @@ class WC_Tests_Reports_Interval_Stats extends WC_Unit_Test_Case { 'f_between' => array( 10, 12 ), // not in params, skipped. ); $params = array( 'a', 'b', 'c', 'd' ); - $result = WC_Admin_Reports_Interval::normalize_between_params( $request, $params, false ); + $result = TimeInterval::normalize_between_params( $request, $params, false ); $expected = array( 'b_min' => 1, 'b_max' => 5, @@ -869,7 +871,7 @@ class WC_Tests_Reports_Interval_Stats extends WC_Unit_Test_Case { 'f_between' => array( 10, 12 ), // not in params, skipped. ); $params = array( 'a', 'b', 'c', 'd' ); - $result = WC_Admin_Reports_Interval::normalize_between_params( $request, $params, true ); + $result = TimeInterval::normalize_between_params( $request, $params, true ); $expected = array( 'b_after' => 1, 'b_before' => 5, @@ -885,17 +887,17 @@ class WC_Tests_Reports_Interval_Stats extends WC_Unit_Test_Case { */ public function test_rest_validate_between_numeric_arg() { $this->assertWPError( - WC_Admin_Reports_Interval::rest_validate_between_numeric_arg( 'not array', null, 'param' ), + TimeInterval::rest_validate_between_numeric_arg( 'not array', null, 'param' ), 'param is not a numerically indexed array.' ); $this->assertWPError( - WC_Admin_Reports_Interval::rest_validate_between_numeric_arg( array( 1 ), null, 'param' ), + TimeInterval::rest_validate_between_numeric_arg( array( 1 ), null, 'param' ), 'param must contain 2 numbers.' ); $this->assertTrue( - WC_Admin_Reports_Interval::rest_validate_between_numeric_arg( array( 1, 2 ), null, 'param' ) + TimeInterval::rest_validate_between_numeric_arg( array( 1, 2 ), null, 'param' ) ); } @@ -904,22 +906,22 @@ class WC_Tests_Reports_Interval_Stats extends WC_Unit_Test_Case { */ public function rest_validate_between_date_arg() { $this->assertWPError( - WC_Admin_Reports_Interval::rest_validate_between_date_arg( 'not array', null, 'param' ), + TimeInterval::rest_validate_between_date_arg( 'not array', null, 'param' ), 'param is not a numerically indexed array.' ); $this->assertWPError( - WC_Admin_Reports_Interval::rest_validate_between_date_arg( array( '2019-01-01T00:00:00' ), null, 'param' ), + TimeInterval::rest_validate_between_date_arg( array( '2019-01-01T00:00:00' ), null, 'param' ), 'param must contain 2 valid dates.' ); $this->assertWPError( - WC_Admin_Reports_Interval::rest_validate_between_date_arg( array( 'not a valid date' ), null, 'param' ), + TimeInterval::rest_validate_between_date_arg( array( 'not a valid date' ), null, 'param' ), 'param must contain 2 valid dates.' ); $this->assertTrue( - WC_Admin_Reports_Interval::rest_validate_between_date_arg( array( '2019-01-01T00:00:00', '2019-01-15T00:00:00' ), null, 'param' ) + TimeInterval::rest_validate_between_date_arg( array( '2019-01-01T00:00:00', '2019-01-15T00:00:00' ), null, 'param' ) ); } } diff --git a/plugins/woocommerce-admin/tests/api/reports-orders.php b/plugins/woocommerce-admin/tests/api/reports-orders.php index c697b0593ee..56da4764897 100644 --- a/plugins/woocommerce-admin/tests/api/reports-orders.php +++ b/plugins/woocommerce-admin/tests/api/reports-orders.php @@ -6,6 +6,8 @@ * @since 3.5.0 */ +use \Automattic\WooCommerce\Admin\API\Reports\Customers\DataStore as CustomersDataStore; + /** * Reports Orders REST API Test Class * @@ -69,7 +71,7 @@ class WC_Tests_API_Reports_Orders extends WC_REST_Unit_Test_Case { WC_Helper_Queue::run_all_pending(); - $expected_customer_id = WC_Admin_Reports_Customers_Data_Store::get_customer_id_by_user_id( 1 ); + $expected_customer_id = CustomersDataStore::get_customer_id_by_user_id( 1 ); $response = $this->server->dispatch( new WP_REST_Request( 'GET', $this->endpoint ) ); $reports = $response->get_data(); diff --git a/plugins/woocommerce-admin/tests/api/reports-stock-stats.php b/plugins/woocommerce-admin/tests/api/reports-stock-stats.php index a4e0e95d54e..a670839c2ae 100644 --- a/plugins/woocommerce-admin/tests/api/reports-stock-stats.php +++ b/plugins/woocommerce-admin/tests/api/reports-stock-stats.php @@ -94,6 +94,14 @@ class WC_Tests_API_Reports_Stock_Stats extends WC_REST_Unit_Test_Case { $backorder_stock->set_stock_status( 'onbackorder' ); $backorder_stock->save(); + // Clear caches + delete_transient( 'wc_admin_stock_count_lowstock' ); + delete_transient( 'wc_admin_stock_count_outofstock' ); + delete_transient( 'wc_admin_stock_count_onbackorder' ); + delete_transient( 'wc_admin_stock_count_lowstock' ); + delete_transient( 'wc_admin_stock_count_instock' ); + delete_transient( 'wc_admin_product_count' ); + $request = new WP_REST_Request( 'GET', $this->endpoint ); $response = $this->server->dispatch( $request ); $reports = $response->get_data(); diff --git a/plugins/woocommerce-admin/tests/batch-queue.php b/plugins/woocommerce-admin/tests/batch-queue.php index 266cfa975f6..446960649a1 100644 --- a/plugins/woocommerce-admin/tests/batch-queue.php +++ b/plugins/woocommerce-admin/tests/batch-queue.php @@ -6,6 +6,8 @@ * @since 3.5.0 */ +use Automattic\WooCommerce\Admin\ReportsSync; + /** * Reports Generation Batch Queue Test Class * @@ -43,11 +45,11 @@ class WC_Tests_Reports_Regenerate_Batching extends WC_REST_Unit_Test_Case { */ public function filter_batch_size( $batch_size, $action ) { switch ( $action ) { - case WC_Admin_Reports_Sync::QUEUE_BATCH_ACTION: + case ReportsSync::QUEUE_BATCH_ACTION: return $this->queue_batch_size; - case WC_Admin_Reports_Sync::CUSTOMERS_IMPORT_BATCH_ACTION: + case ReportsSync::CUSTOMERS_IMPORT_BATCH_ACTION: return $this->customers_batch_size; - case WC_Admin_Reports_Sync::ORDERS_BATCH_ACTION: + case ReportsSync::ORDERS_BATCH_ACTION: return $this->orders_batch_size; default: return 1; @@ -60,7 +62,7 @@ class WC_Tests_Reports_Regenerate_Batching extends WC_REST_Unit_Test_Case { public function setUp() { parent::setUp(); $this->queue = new WC_Admin_Test_Action_Queue(); - WC_Admin_Reports_Sync::set_queue( $this->queue ); + ReportsSync::set_queue( $this->queue ); add_filter( 'wc_admin_report_regenerate_batch_size', array( $this, 'filter_batch_size' ), 10, 2 ); } @@ -69,7 +71,7 @@ class WC_Tests_Reports_Regenerate_Batching extends WC_REST_Unit_Test_Case { */ public function tearDown() { parent::tearDown(); - WC_Admin_Reports_Sync::set_queue( null ); + ReportsSync::set_queue( null ); $this->queue->actions = array(); remove_filter( 'wc_admin_report_regenerate_batch_size', array( $this, 'filter_batch_size' ), 10, 2 ); } @@ -83,20 +85,20 @@ class WC_Tests_Reports_Regenerate_Batching extends WC_REST_Unit_Test_Case { $chunk_size = (int) ceil( $num_batches / $this->queue_batch_size ); $num_chunks = (int) ceil( $num_batches / $chunk_size ); - WC_Admin_Reports_Sync::queue_batches( 1, $num_batches, WC_Admin_Reports_Sync::CUSTOMERS_IMPORT_BATCH_ACTION ); + ReportsSync::queue_batches( 1, $num_batches, ReportsSync::CUSTOMERS_IMPORT_BATCH_ACTION ); $this->assertCount( $num_chunks, $this->queue->actions ); $this->assertArraySubset( array( - 'hook' => WC_Admin_Reports_Sync::QUEUE_BATCH_ACTION, - 'args' => array( 1, $chunk_size, WC_Admin_Reports_Sync::CUSTOMERS_IMPORT_BATCH_ACTION ), + 'hook' => ReportsSync::QUEUE_BATCH_ACTION, + 'args' => array( 1, $chunk_size, ReportsSync::CUSTOMERS_IMPORT_BATCH_ACTION ), ), $this->queue->actions[0] ); $this->assertArraySubset( array( - 'hook' => WC_Admin_Reports_Sync::QUEUE_BATCH_ACTION, - 'args' => array( 247, 247, WC_Admin_Reports_Sync::CUSTOMERS_IMPORT_BATCH_ACTION ), + 'hook' => ReportsSync::QUEUE_BATCH_ACTION, + 'args' => array( 247, 247, ReportsSync::CUSTOMERS_IMPORT_BATCH_ACTION ), ), $this->queue->actions[ $num_chunks - 1 ] ); @@ -109,19 +111,19 @@ class WC_Tests_Reports_Regenerate_Batching extends WC_REST_Unit_Test_Case { $num_customers = 45; // 45 / 5 = 9 batches (which is less than the batch queue size) $num_batches = ceil( $num_customers / $this->customers_batch_size ); - WC_Admin_Reports_Sync::queue_batches( 1, $num_batches, WC_Admin_Reports_Sync::CUSTOMERS_IMPORT_BATCH_ACTION ); + ReportsSync::queue_batches( 1, $num_batches, ReportsSync::CUSTOMERS_IMPORT_BATCH_ACTION ); $this->assertCount( 9, $this->queue->actions ); $this->assertArraySubset( array( - 'hook' => WC_Admin_Reports_Sync::CUSTOMERS_IMPORT_BATCH_ACTION, + 'hook' => ReportsSync::CUSTOMERS_IMPORT_BATCH_ACTION, 'args' => array( 1 ), ), $this->queue->actions[0] ); $this->assertArraySubset( array( - 'hook' => WC_Admin_Reports_Sync::CUSTOMERS_IMPORT_BATCH_ACTION, + 'hook' => ReportsSync::CUSTOMERS_IMPORT_BATCH_ACTION, 'args' => array( 9 ), ), $this->queue->actions[8] @@ -133,15 +135,15 @@ class WC_Tests_Reports_Regenerate_Batching extends WC_REST_Unit_Test_Case { */ public function test_queue_dependent_action() { // reset back to using a real queue. - WC_Admin_Reports_Sync::set_queue( null ); + ReportsSync::set_queue( null ); // insert a blocking job. - WC_Admin_Reports_Sync::queue()->schedule_single( time(), 'blocking_job', array( 'stuff' ), WC_Admin_Reports_Sync::QUEUE_GROUP ); + ReportsSync::queue()->schedule_single( time(), 'blocking_job', array( 'stuff' ), ReportsSync::QUEUE_GROUP ); // queue an action that depends on blocking job. - WC_Admin_Reports_Sync::queue_dependent_action( 'dependent_action', array(), 'blocking_job' ); + ReportsSync::queue_dependent_action( 'dependent_action', array(), 'blocking_job' ); // verify that the action was properly blocked. $this->assertEmpty( - WC_Admin_Reports_Sync::queue()->search( + ReportsSync::queue()->search( array( 'hook' => 'dependent_action', ) @@ -150,20 +152,20 @@ class WC_Tests_Reports_Regenerate_Batching extends WC_REST_Unit_Test_Case { // verify that a follow up action was queued. $this->assertCount( 1, - WC_Admin_Reports_Sync::queue()->search( + ReportsSync::queue()->search( array( - 'hook' => WC_Admin_Reports_Sync::QUEUE_DEPEDENT_ACTION, + 'hook' => ReportsSync::QUEUE_DEPEDENT_ACTION, 'args' => array( 'dependent_action', array(), 'blocking_job' ), ) ) ); // queue an action that isn't blocked. - WC_Admin_Reports_Sync::queue_dependent_action( 'another_dependent_action', array(), 'nonexistant_blocking_job' ); + ReportsSync::queue_dependent_action( 'another_dependent_action', array(), 'nonexistant_blocking_job' ); // verify that the dependent action was queued. $this->assertCount( 1, - WC_Admin_Reports_Sync::queue()->search( + ReportsSync::queue()->search( array( 'hook' => 'another_dependent_action', ) @@ -171,16 +173,16 @@ class WC_Tests_Reports_Regenerate_Batching extends WC_REST_Unit_Test_Case { ); // verify that no follow up action was queued. $this->assertEmpty( - WC_Admin_Reports_Sync::queue()->search( + ReportsSync::queue()->search( array( - 'hook' => WC_Admin_Reports_Sync::QUEUE_DEPEDENT_ACTION, + 'hook' => ReportsSync::QUEUE_DEPEDENT_ACTION, 'args' => array( 'another_dependent_action', array(), 'nonexistant_blocking_job' ), ) ) ); // clean up. - WC_Admin_Reports_Sync::queue()->cancel_all( 'another_dependent_action' ); - WC_Admin_Reports_Sync::queue()->cancel_all( WC_Admin_Reports_Sync::QUEUE_DEPEDENT_ACTION ); + ReportsSync::queue()->cancel_all( 'another_dependent_action' ); + ReportsSync::queue()->cancel_all( ReportsSync::QUEUE_DEPEDENT_ACTION ); } } diff --git a/plugins/woocommerce-admin/tests/bootstrap.php b/plugins/woocommerce-admin/tests/bootstrap.php index 1cad33e2fd8..5624bbb8910 100755 --- a/plugins/woocommerce-admin/tests/bootstrap.php +++ b/plugins/woocommerce-admin/tests/bootstrap.php @@ -37,10 +37,8 @@ function wc_admin_install() { define( 'WC_REMOVE_ALL_DATA', true ); // Initialize the WC API extensions. - require_once dirname( dirname( __FILE__ ) ) . '/includes/class-wc-admin-install.php'; - - WC_Admin_Install::create_tables(); - WC_Admin_Install::create_events(); + \Automattic\WooCommerce\Admin\Install::create_tables(); + \Automattic\WooCommerce\Admin\Install::create_events(); // Reload capabilities after install, see https://core.trac.wordpress.org/ticket/28374. $GLOBALS['wp_roles'] = null; // WPCS: override ok. @@ -93,6 +91,7 @@ function _manually_load_plugin() { update_option( 'woocommerce_calc_taxes', 'yes' ); require_once wc_dir() . '/woocommerce.php'; + require dirname( __DIR__ ) . '/vendor/autoload.php'; wc_admin_install(); diff --git a/plugins/woocommerce-admin/tests/framework/helpers/class-wc-helper-admin-notes.php b/plugins/woocommerce-admin/tests/framework/helpers/class-wc-helper-admin-notes.php index 059871d4f78..3a384fdc502 100644 --- a/plugins/woocommerce-admin/tests/framework/helpers/class-wc-helper-admin-notes.php +++ b/plugins/woocommerce-admin/tests/framework/helpers/class-wc-helper-admin-notes.php @@ -5,6 +5,8 @@ * @package WooCommerce\Tests\Framework\Helpers */ +use Automattic\WooCommerce\Admin\Notes\WC_Admin_Note; + /** * Class WC_Helper_Admin_Notes. * diff --git a/plugins/woocommerce-admin/tests/framework/helpers/class-wc-helper-reports.php b/plugins/woocommerce-admin/tests/framework/helpers/class-wc-helper-reports.php index 9bc4bcb43c7..f6faf8abc8c 100644 --- a/plugins/woocommerce-admin/tests/framework/helpers/class-wc-helper-reports.php +++ b/plugins/woocommerce-admin/tests/framework/helpers/class-wc-helper-reports.php @@ -17,9 +17,9 @@ class WC_Helper_Reports { */ public static function reset_stats_dbs() { global $wpdb; - $wpdb->query( "DELETE FROM $wpdb->prefix" . WC_Admin_Reports_Orders_Stats_Data_Store::TABLE_NAME ); // @codingStandardsIgnoreLine. - $wpdb->query( "DELETE FROM $wpdb->prefix" . WC_Admin_Reports_Products_Data_Store::TABLE_NAME ); // @codingStandardsIgnoreLine. - $wpdb->query( "DELETE FROM $wpdb->prefix" . WC_Admin_Reports_Coupons_Data_Store::TABLE_NAME ); // @codingStandardsIgnoreLine. - $wpdb->query( "DELETE FROM $wpdb->prefix" . WC_Admin_Reports_Customers_Data_Store::TABLE_NAME ); // @codingStandardsIgnoreLine. + $wpdb->query( "DELETE FROM $wpdb->prefix" . \Automattic\WooCommerce\Admin\API\Reports\Orders\Stats\DataStore::TABLE_NAME ); // @codingStandardsIgnoreLine. + $wpdb->query( "DELETE FROM $wpdb->prefix" . \Automattic\WooCommerce\Admin\API\Reports\Products\DataStore::TABLE_NAME ); // @codingStandardsIgnoreLine. + $wpdb->query( "DELETE FROM $wpdb->prefix" . \Automattic\WooCommerce\Admin\API\Reports\Coupons\DataStore::TABLE_NAME ); // @codingStandardsIgnoreLine. + $wpdb->query( "DELETE FROM $wpdb->prefix" . \Automattic\WooCommerce\Admin\API\Reports\Customers\DataStore::TABLE_NAME ); // @codingStandardsIgnoreLine. } } diff --git a/plugins/woocommerce-admin/tests/queue-priority.php b/plugins/woocommerce-admin/tests/queue-priority.php index 683b0ad3a0b..c482d64c22d 100644 --- a/plugins/woocommerce-admin/tests/queue-priority.php +++ b/plugins/woocommerce-admin/tests/queue-priority.php @@ -6,6 +6,9 @@ * @since 3.5.0 */ +use Automattic\WooCommerce\Admin\ReportsSync; +use Automattic\WooCommerce\Admin\Overrides\WPPostStore; + /** * Reports Generation Batch Queue Prioritizaion Test Class * @@ -37,13 +40,13 @@ class WC_Tests_Reports_Queue_Prioritization extends WC_REST_Unit_Test_Case { * Test that we're setting a priority on our actions. */ public function test_queue_action_sets_priority() { - WC_Admin_Reports_Sync::queue()->schedule_single( time(), WC_Admin_Reports_Sync::SINGLE_ORDER_IMPORT_ACTION ); + ReportsSync::queue()->schedule_single( time(), ReportsSync::SINGLE_ORDER_IMPORT_ACTION ); - $actions = WC_Admin_Reports_Sync::queue()->search( + $actions = ReportsSync::queue()->search( array( 'status' => 'pending', 'claimed' => false, - 'hook' => WC_Admin_Reports_Sync::SINGLE_ORDER_IMPORT_ACTION, + 'hook' => ReportsSync::SINGLE_ORDER_IMPORT_ACTION, ) ); @@ -53,9 +56,9 @@ class WC_Tests_Reports_Queue_Prioritization extends WC_REST_Unit_Test_Case { $action_id = $action_ids[0]; $action = get_post( $action_id ); - $this->assertEquals( WC_Admin_ActionScheduler_wpPostStore::JOB_PRIORITY, $action->menu_order ); + $this->assertEquals( WPPostStore::JOB_PRIORITY, $action->menu_order ); - WC_Admin_Reports_Sync::queue()->cancel_all( WC_Admin_Reports_Sync::SINGLE_ORDER_IMPORT_ACTION ); + ReportsSync::queue()->cancel_all( ReportsSync::SINGLE_ORDER_IMPORT_ACTION ); } } diff --git a/plugins/woocommerce-admin/tests/reports/class-wc-tests-reports-coupons-stats.php b/plugins/woocommerce-admin/tests/reports/class-wc-tests-reports-coupons-stats.php index 2c6de71d4ae..646471ea71b 100644 --- a/plugins/woocommerce-admin/tests/reports/class-wc-tests-reports-coupons-stats.php +++ b/plugins/woocommerce-admin/tests/reports/class-wc-tests-reports-coupons-stats.php @@ -5,6 +5,9 @@ * @package WooCommerce\Tests\Coupons-stats */ +use \Automattic\WooCommerce\Admin\API\Reports\Coupons\Stats\DataStore as CouponsStatsDataStore; +use \Automattic\WooCommerce\Admin\API\Reports\Coupons\Stats\Query as CouponsStatsQuery; + /** * Class WC_Tests_Reports_Coupons_Stats */ @@ -56,7 +59,7 @@ class WC_Tests_Reports_Coupons_Stats extends WC_Unit_Test_Case { WC_Helper_Queue::run_all_pending(); - $data_store = new WC_Admin_Reports_Coupons_Stats_Data_Store(); + $data_store = new CouponsStatsDataStore(); $start_time = date( 'Y-m-d 00:00:00', $order->get_date_created()->getOffsetTimestamp() ); $end_time = date( 'Y-m-d 23:59:59', $order->get_date_created()->getOffsetTimestamp() ); $args = array( @@ -98,7 +101,7 @@ class WC_Tests_Reports_Coupons_Stats extends WC_Unit_Test_Case { $this->assertEquals( $expected_data, $data ); // Test retrieving the stats through the query class. - $query = new WC_Admin_Reports_Coupons_Stats_Query( $args ); + $query = new CouponsStatsQuery( $args ); $this->assertEquals( $expected_data, $query->get_data() ); } } diff --git a/plugins/woocommerce-admin/tests/reports/class-wc-tests-reports-coupons.php b/plugins/woocommerce-admin/tests/reports/class-wc-tests-reports-coupons.php index df46ff152ef..cd8d342c81a 100644 --- a/plugins/woocommerce-admin/tests/reports/class-wc-tests-reports-coupons.php +++ b/plugins/woocommerce-admin/tests/reports/class-wc-tests-reports-coupons.php @@ -5,6 +5,11 @@ * @package WooCommerce\Tests\Coupons */ +use \Automattic\WooCommerce\Admin\ReportCSVExporter; +use \Automattic\WooCommerce\Admin\API\Reports\Coupons\DataStore as CouponsDataStore; +use \Automattic\WooCommerce\Admin\API\Reports\Coupons\Query as CouponsQuery; +use \Automattic\WooCommerce\Admin\API\Reports\TimeInterval; + /** * Class WC_Tests_Reports_Coupons */ @@ -56,7 +61,7 @@ class WC_Tests_Reports_Coupons extends WC_Unit_Test_Case { WC_Helper_Queue::run_all_pending(); - $data_store = new WC_Admin_Reports_Coupons_Data_Store(); + $data_store = new CouponsDataStore(); $start_time = date( 'Y-m-d 00:00:00', $order->get_date_created()->getOffsetTimestamp() ); $end_time = date( 'Y-m-d 23:59:59', $order->get_date_created()->getOffsetTimestamp() ); $args = array( @@ -92,7 +97,7 @@ class WC_Tests_Reports_Coupons extends WC_Unit_Test_Case { $this->assertEquals( $expected_data, $data ); // Test retrieving the stats through the query class. - $query = new WC_Admin_Reports_Coupons_Query( $args ); + $query = new CouponsQuery( $args ); $this->assertEquals( $expected_data, $query->get_data() ); // Test order by orders_count DESC. @@ -182,8 +187,8 @@ class WC_Tests_Reports_Coupons extends WC_Unit_Test_Case { $c1_date_created_gmt = new DateTime( $c1_date_created ); $c1_date_created_gmt->setTimezone( $gmt_timezone ); - $c1_date_created = $c1_date_created->format( WC_Admin_Reports_Interval::$iso_datetime_format ); - $c1_date_created_gmt = $c1_date_created_gmt->format( WC_Admin_Reports_Interval::$iso_datetime_format ); + $c1_date_created = $c1_date_created->format( TimeInterval::$iso_datetime_format ); + $c1_date_created_gmt = $c1_date_created_gmt->format( TimeInterval::$iso_datetime_format ); } $c1_date_expires = $coupon_1->get_date_expires(); @@ -194,8 +199,8 @@ class WC_Tests_Reports_Coupons extends WC_Unit_Test_Case { $c1_date_expires_gmt = new DateTime( $c1_date_expires ); $c1_date_expires_gmt->setTimezone( $gmt_timezone ); - $c1_date_expires = $c1_date_expires->format( WC_Admin_Reports_Interval::$iso_datetime_format ); - $c1_date_expires_gmt = $c1_date_expires_gmt->format( WC_Admin_Reports_Interval::$iso_datetime_format ); + $c1_date_expires = $c1_date_expires->format( TimeInterval::$iso_datetime_format ); + $c1_date_expires_gmt = $c1_date_expires_gmt->format( TimeInterval::$iso_datetime_format ); } $coupon_1_response = array( @@ -220,8 +225,8 @@ class WC_Tests_Reports_Coupons extends WC_Unit_Test_Case { $c2_date_created_gmt = new DateTime( $c2_date_created ); $c2_date_created_gmt->setTimezone( $gmt_timezone ); - $c2_date_created = $c2_date_created->format( WC_Admin_Reports_Interval::$iso_datetime_format ); - $c2_date_created_gmt = $c2_date_created_gmt->format( WC_Admin_Reports_Interval::$iso_datetime_format ); + $c2_date_created = $c2_date_created->format( TimeInterval::$iso_datetime_format ); + $c2_date_created_gmt = $c2_date_created_gmt->format( TimeInterval::$iso_datetime_format ); } $c2_date_expires = $coupon_2->get_date_expires(); @@ -232,8 +237,8 @@ class WC_Tests_Reports_Coupons extends WC_Unit_Test_Case { $c2_date_expires_gmt = new DateTime( $c2_date_expires ); $c2_date_expires_gmt->setTimezone( $gmt_timezone ); - $c2_date_expires = $c2_date_expires->format( WC_Admin_Reports_Interval::$iso_datetime_format ); - $c2_date_expires_gmt = $c2_date_expires_gmt->format( WC_Admin_Reports_Interval::$iso_datetime_format ); + $c2_date_expires = $c2_date_expires->format( TimeInterval::$iso_datetime_format ); + $c2_date_expires_gmt = $c2_date_expires_gmt->format( TimeInterval::$iso_datetime_format ); } $coupon_2_response = array( @@ -316,7 +321,7 @@ class WC_Tests_Reports_Coupons extends WC_Unit_Test_Case { do_action( 'rest_api_init' ); // Run the export and compare values. - $export = new WC_Admin_Report_CSV_Exporter( 'coupons', $args ); + $export = new ReportCSVExporter( 'coupons', $args ); $export->generate_file(); $actual_csv = $export->get_file(); diff --git a/plugins/woocommerce-admin/tests/reports/class-wc-tests-reports-orders-stats.php b/plugins/woocommerce-admin/tests/reports/class-wc-tests-reports-orders-stats.php index 65ebd3475fd..ac0bd9dec49 100644 --- a/plugins/woocommerce-admin/tests/reports/class-wc-tests-reports-orders-stats.php +++ b/plugins/woocommerce-admin/tests/reports/class-wc-tests-reports-orders-stats.php @@ -5,6 +5,10 @@ * @package WooCommerce\Tests\Orders */ +use \Automattic\WooCommerce\Admin\API\Reports\Orders\Stats\DataStore as OrdersStatsDataStore; +use \Automattic\WooCommerce\Admin\API\Reports\Orders\Stats\Query as OrdersStatsQuery; +use \Automattic\WooCommerce\Admin\API\Reports\TimeInterval; + /** * Class WC_Tests_Reports_Orders_Stats */ @@ -46,7 +50,7 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case { WC_Helper_Queue::run_all_pending(); - $data_store = new WC_Admin_Reports_Orders_Stats_Data_Store(); + $data_store = new OrdersStatsDataStore(); $start_time = date( 'Y-m-d H:00:00', $order->get_date_created()->getOffsetTimestamp() ); $end_time = date( 'Y-m-d H:59:59', $order->get_date_created()->getOffsetTimestamp() ); @@ -108,7 +112,7 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case { $this->assertEquals( $expected_stats, json_decode( wp_json_encode( $data_store->get_data( $args ) ), true ) ); // Test retrieving the stats through the query class. - $query = new WC_Admin_Reports_Orders_Stats_Query( $args ); + $query = new OrdersStatsQuery( $args ); $expected_stats = array( 'totals' => array( 'net_revenue' => 68, @@ -189,7 +193,7 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case { WC_Helper_Queue::run_all_pending(); - $data_store = new WC_Admin_Reports_Orders_Stats_Data_Store(); + $data_store = new OrdersStatsDataStore(); $start_time = date( 'Y-m-d H:00:00', $order->get_date_created()->getOffsetTimestamp() ); $end_time = date( 'Y-m-d H:59:59', $order->get_date_created()->getOffsetTimestamp() ); @@ -354,7 +358,7 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case { WC_Helper_Queue::run_all_pending(); - $data_store = new WC_Admin_Reports_Orders_Stats_Data_Store(); + $data_store = new OrdersStatsDataStore(); $start_time = date( 'Y-m-d H:00:00', $order->get_date_created()->getOffsetTimestamp() ); $end_time = date( 'Y-m-d H:59:59', $order->get_date_created()->getOffsetTimestamp() ); @@ -643,7 +647,7 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case { WC_Helper_Queue::run_all_pending(); - $data_store = new WC_Admin_Reports_Orders_Stats_Data_Store(); + $data_store = new OrdersStatsDataStore(); // Test for the current hour. $current_hour_start = new DateTime(); @@ -653,8 +657,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case { $current_hour_end->setTimestamp( $report_start_time + HOUR_IN_SECONDS - 1 ); $query_args = array( - 'after' => $current_hour_start->format( WC_Admin_Reports_Interval::$sql_datetime_format ), - 'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ), + 'after' => $current_hour_start->format( TimeInterval::$sql_datetime_format ), + 'before' => $current_hour_end->format( TimeInterval::$sql_datetime_format ), 'interval' => 'hour', ); @@ -894,7 +898,7 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case { WC_Helper_Queue::run_all_pending(); - $data_store = new WC_Admin_Reports_Orders_Stats_Data_Store(); + $data_store = new OrdersStatsDataStore(); // Tests for before & after set to current hour. $current_hour_start = new DateTime(); @@ -912,8 +916,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case { // 24 orders without coupons, 48 with coupons: 24 with $1 coupon and 24 with $2 coupon. // shipping is $10 per order. $query_args = array( - 'after' => $current_hour_start->format( WC_Admin_Reports_Interval::$sql_datetime_format ), - 'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ), + 'after' => $current_hour_start->format( TimeInterval::$sql_datetime_format ), + 'before' => $current_hour_end->format( TimeInterval::$sql_datetime_format ), 'interval' => 'hour', ); @@ -988,8 +992,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case { // * Order status filter // ** Status is, positive filter for 2 statuses, i.e. all orders. $query_args = array( - 'after' => $current_hour_start->format( WC_Admin_Reports_Interval::$sql_datetime_format ), - 'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ), + 'after' => $current_hour_start->format( TimeInterval::$sql_datetime_format ), + 'before' => $current_hour_end->format( TimeInterval::$sql_datetime_format ), 'interval' => 'hour', 'status_is' => array( $order_status_1, @@ -1001,8 +1005,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case { // ** Status is, positive filter for 1 status -> half orders. $query_args = array( - 'after' => $current_hour_start->format( WC_Admin_Reports_Interval::$sql_datetime_format ), - 'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ), + 'after' => $current_hour_start->format( TimeInterval::$sql_datetime_format ), + 'before' => $current_hour_end->format( TimeInterval::$sql_datetime_format ), 'interval' => 'hour', 'status_is' => array( $order_status_1, @@ -1079,8 +1083,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case { // ** Status is not, negative filter for 1 status -> half orders. $query_args = array( - 'after' => $current_hour_start->format( WC_Admin_Reports_Interval::$sql_datetime_format ), - 'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ), + 'after' => $current_hour_start->format( TimeInterval::$sql_datetime_format ), + 'before' => $current_hour_end->format( TimeInterval::$sql_datetime_format ), 'interval' => 'hour', 'status_is_not' => array( $order_status_2, @@ -1157,8 +1161,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case { // ** Status is not, negative filter for 2 statuses -> no orders. $query_args = array( - 'after' => $current_hour_start->format( WC_Admin_Reports_Interval::$sql_datetime_format ), - 'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ), + 'after' => $current_hour_start->format( TimeInterval::$sql_datetime_format ), + 'before' => $current_hour_end->format( TimeInterval::$sql_datetime_format ), 'interval' => 'hour', 'status_is_not' => array( $order_status_1, @@ -1217,8 +1221,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case { // ** Status is + Status is not, positive filter for 2 statuses, negative for 1 -> half of orders. $query_args = array( - 'after' => $current_hour_start->format( WC_Admin_Reports_Interval::$sql_datetime_format ), - 'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ), + 'after' => $current_hour_start->format( TimeInterval::$sql_datetime_format ), + 'before' => $current_hour_end->format( TimeInterval::$sql_datetime_format ), 'interval' => 'hour', 'status_is' => array( $order_status_1, @@ -1300,8 +1304,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case { // * Product filter // ** Product includes, positive filter for 2 products, i.e. 2 orders out of 3. $query_args = array( - 'after' => $current_hour_start->format( WC_Admin_Reports_Interval::$sql_datetime_format ), - 'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ), + 'after' => $current_hour_start->format( TimeInterval::$sql_datetime_format ), + 'before' => $current_hour_end->format( TimeInterval::$sql_datetime_format ), 'interval' => 'hour', 'product_includes' => array( $product_1->get_id(), @@ -1377,8 +1381,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case { // ** Product includes, positive filter for 1 product, 1/3 of orders $query_args = array( - 'after' => $current_hour_start->format( WC_Admin_Reports_Interval::$sql_datetime_format ), - 'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ), + 'after' => $current_hour_start->format( TimeInterval::$sql_datetime_format ), + 'before' => $current_hour_end->format( TimeInterval::$sql_datetime_format ), 'interval' => 'hour', 'product_includes' => array( $product_3->get_id(), @@ -1452,8 +1456,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case { // ** Product excludes, negative filter for 1 product, 2/3 of orders. $query_args = array( - 'after' => $current_hour_start->format( WC_Admin_Reports_Interval::$sql_datetime_format ), - 'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ), + 'after' => $current_hour_start->format( TimeInterval::$sql_datetime_format ), + 'before' => $current_hour_end->format( TimeInterval::$sql_datetime_format ), 'interval' => 'hour', 'product_excludes' => array( $product_1->get_id(), @@ -1528,8 +1532,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case { // ** Product excludes, negative filter for 2 products, 1/3 of orders $query_args = array( - 'after' => $current_hour_start->format( WC_Admin_Reports_Interval::$sql_datetime_format ), - 'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ), + 'after' => $current_hour_start->format( TimeInterval::$sql_datetime_format ), + 'before' => $current_hour_end->format( TimeInterval::$sql_datetime_format ), 'interval' => 'hour', 'product_excludes' => array( $product_1->get_id(), @@ -1603,8 +1607,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case { // ** Product includes + product excludes, positive filter for 2 products, negative for 1 -> 1/3 of orders, only orders with product 2 and product 2 + product 4 $query_args = array( - 'after' => $current_hour_start->format( WC_Admin_Reports_Interval::$sql_datetime_format ), - 'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ), + 'after' => $current_hour_start->format( TimeInterval::$sql_datetime_format ), + 'before' => $current_hour_end->format( TimeInterval::$sql_datetime_format ), 'interval' => 'hour', 'product_includes' => array( $product_1->get_id(), @@ -1682,8 +1686,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case { // * Coupon filters // ** Coupon includes, positive filter for 2 coupons, i.e. 2/3 of orders. $query_args = array( - 'after' => $current_hour_start->format( WC_Admin_Reports_Interval::$sql_datetime_format ), - 'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ), + 'after' => $current_hour_start->format( TimeInterval::$sql_datetime_format ), + 'before' => $current_hour_end->format( TimeInterval::$sql_datetime_format ), 'interval' => 'hour', 'coupon_includes' => array( $coupon_1->get_id(), @@ -1761,8 +1765,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case { // ** Coupon includes, positive filter for 1 coupon, 1/3 of orders $query_args = array( - 'after' => $current_hour_start->format( WC_Admin_Reports_Interval::$sql_datetime_format ), - 'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ), + 'after' => $current_hour_start->format( TimeInterval::$sql_datetime_format ), + 'before' => $current_hour_end->format( TimeInterval::$sql_datetime_format ), 'interval' => 'hour', 'coupon_includes' => array( $coupon_1->get_id(), @@ -1839,8 +1843,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case { // ** Coupon excludes, negative filter for 1 coupon, 2/3 of orders $query_args = array( - 'after' => $current_hour_start->format( WC_Admin_Reports_Interval::$sql_datetime_format ), - 'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ), + 'after' => $current_hour_start->format( TimeInterval::$sql_datetime_format ), + 'before' => $current_hour_end->format( TimeInterval::$sql_datetime_format ), 'interval' => 'hour', 'coupon_excludes' => array( $coupon_1->get_id(), @@ -1917,8 +1921,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case { // ** Coupon excludes, negative filter for 2 coupons, 1/3 of orders $query_args = array( - 'after' => $current_hour_start->format( WC_Admin_Reports_Interval::$sql_datetime_format ), - 'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ), + 'after' => $current_hour_start->format( TimeInterval::$sql_datetime_format ), + 'before' => $current_hour_end->format( TimeInterval::$sql_datetime_format ), 'interval' => 'hour', 'coupon_excludes' => array( $coupon_1->get_id(), @@ -1996,8 +2000,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case { // ** Coupon includes + coupon excludes, positive filter for 2 coupon, negative for 1, 1/3 orders $query_args = array( - 'after' => $current_hour_start->format( WC_Admin_Reports_Interval::$sql_datetime_format ), - 'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ), + 'after' => $current_hour_start->format( TimeInterval::$sql_datetime_format ), + 'before' => $current_hour_end->format( TimeInterval::$sql_datetime_format ), 'interval' => 'hour', 'coupon_includes' => array( $coupon_1->get_id(), @@ -2079,8 +2083,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case { // * Customer filters // ** Customer new $query_args = array( - 'after' => $current_hour_start->format( WC_Admin_Reports_Interval::$sql_datetime_format ), - 'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ), + 'after' => $current_hour_start->format( TimeInterval::$sql_datetime_format ), + 'before' => $current_hour_end->format( TimeInterval::$sql_datetime_format ), 'interval' => 'hour', 'customer' => 'new', ); @@ -2143,8 +2147,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case { // ** Customer returning $query_args = array( - 'after' => $current_hour_start->format( WC_Admin_Reports_Interval::$sql_datetime_format ), // I don't think this makes sense.... date( 'Y-m-d H:i:s', $orders[0]->get_date_created()->getOffsetTimestamp() + 1 ), // Date after initial order to get a returning customer. - 'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ), + 'after' => $current_hour_start->format( TimeInterval::$sql_datetime_format ), // I don't think this makes sense.... date( 'Y-m-d H:i:s', $orders[0]->get_date_created()->getOffsetTimestamp() + 1 ), // Date after initial order to get a returning customer. + 'before' => $current_hour_end->format( TimeInterval::$sql_datetime_format ), 'interval' => 'hour', 'customer' => 'returning', ); @@ -2190,8 +2194,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case { 'intervals' => array( array( 'interval' => $current_hour_start->format( 'Y-m-d H' ), - 'date_start' => $current_hour_start->format( WC_Admin_Reports_Interval::$sql_datetime_format ), - 'date_start_gmt' => $current_hour_start->format( WC_Admin_Reports_Interval::$sql_datetime_format ), + 'date_start' => $current_hour_start->format( TimeInterval::$sql_datetime_format ), + 'date_start_gmt' => $current_hour_start->format( TimeInterval::$sql_datetime_format ), 'date_end' => $current_hour_end->format( 'Y-m-d H:i:s' ), 'date_end_gmt' => $current_hour_end->format( 'Y-m-d H:i:s' ), 'subtotals' => array( @@ -2222,8 +2226,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case { // Combinations: match all // status_is + product_includes. $query_args = array( - 'after' => $current_hour_start->format( WC_Admin_Reports_Interval::$sql_datetime_format ), - 'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ), + 'after' => $current_hour_start->format( TimeInterval::$sql_datetime_format ), + 'before' => $current_hour_end->format( TimeInterval::$sql_datetime_format ), 'interval' => 'hour', 'status_is' => array( $order_status_1, @@ -2299,8 +2303,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case { // status_is + coupon_includes. $query_args = array( - 'after' => $current_hour_start->format( WC_Admin_Reports_Interval::$sql_datetime_format ), - 'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ), + 'after' => $current_hour_start->format( TimeInterval::$sql_datetime_format ), + 'before' => $current_hour_end->format( TimeInterval::$sql_datetime_format ), 'interval' => 'hour', 'status_is' => array( $order_status_1, @@ -2380,8 +2384,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case { // product_includes + coupon_includes. $query_args = array( - 'after' => $current_hour_start->format( WC_Admin_Reports_Interval::$sql_datetime_format ), - 'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ), + 'after' => $current_hour_start->format( TimeInterval::$sql_datetime_format ), + 'before' => $current_hour_end->format( TimeInterval::$sql_datetime_format ), 'interval' => 'hour', 'product_includes' => array( $product_1->get_id(), @@ -2457,8 +2461,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case { // status_is + product_includes + coupon_includes. $query_args = array( - 'after' => $current_hour_start->format( WC_Admin_Reports_Interval::$sql_datetime_format ), - 'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ), + 'after' => $current_hour_start->format( TimeInterval::$sql_datetime_format ), + 'before' => $current_hour_end->format( TimeInterval::$sql_datetime_format ), 'interval' => 'hour', 'status_is' => array( $order_status_1, @@ -2537,8 +2541,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case { // status_is + status_is_not + product_includes. $query_args = array( - 'after' => $current_hour_start->format( WC_Admin_Reports_Interval::$sql_datetime_format ), - 'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ), + 'after' => $current_hour_start->format( TimeInterval::$sql_datetime_format ), + 'before' => $current_hour_end->format( TimeInterval::$sql_datetime_format ), 'interval' => 'hour', 'status_is' => array( $order_status_1, @@ -2621,8 +2625,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case { // status_is + status_is_not + product_includes + product_excludes. $query_args = array( - 'after' => $current_hour_start->format( WC_Admin_Reports_Interval::$sql_datetime_format ), - 'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ), + 'after' => $current_hour_start->format( TimeInterval::$sql_datetime_format ), + 'before' => $current_hour_end->format( TimeInterval::$sql_datetime_format ), 'interval' => 'hour', 'status_is' => array( $order_status_1, @@ -2706,8 +2710,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case { // status_is + status_is_not + product_includes + product_excludes + coupon_includes. $query_args = array( - 'after' => $current_hour_start->format( WC_Admin_Reports_Interval::$sql_datetime_format ), - 'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ), + 'after' => $current_hour_start->format( TimeInterval::$sql_datetime_format ), + 'before' => $current_hour_end->format( TimeInterval::$sql_datetime_format ), 'interval' => 'hour', 'status_is' => array( $order_status_1, @@ -2793,8 +2797,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case { // status_is + status_is_not + product_includes + product_excludes + coupon_includes + coupon_excludes. $query_args = array( - 'after' => $current_hour_start->format( WC_Admin_Reports_Interval::$sql_datetime_format ), - 'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ), + 'after' => $current_hour_start->format( TimeInterval::$sql_datetime_format ), + 'before' => $current_hour_end->format( TimeInterval::$sql_datetime_format ), 'interval' => 'hour', 'status_is' => array( $order_status_1, @@ -2885,8 +2889,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case { // Combinations: match any // status_is + status_is_not, all orders. $query_args = array( - 'after' => $current_hour_start->format( WC_Admin_Reports_Interval::$sql_datetime_format ), - 'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ), + 'after' => $current_hour_start->format( TimeInterval::$sql_datetime_format ), + 'before' => $current_hour_end->format( TimeInterval::$sql_datetime_format ), 'interval' => 'hour', 'match' => 'any', 'status_is' => array( @@ -2967,8 +2971,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case { // status_is OR product_includes. $query_args = array( - 'after' => $current_hour_start->format( WC_Admin_Reports_Interval::$sql_datetime_format ), - 'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ), + 'after' => $current_hour_start->format( TimeInterval::$sql_datetime_format ), + 'before' => $current_hour_end->format( TimeInterval::$sql_datetime_format ), 'interval' => 'hour', 'match' => 'any', 'status_is' => array( @@ -3049,8 +3053,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case { // status_is OR coupon_includes. $query_args = array( - 'after' => $current_hour_start->format( WC_Admin_Reports_Interval::$sql_datetime_format ), - 'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ), + 'after' => $current_hour_start->format( TimeInterval::$sql_datetime_format ), + 'before' => $current_hour_end->format( TimeInterval::$sql_datetime_format ), 'interval' => 'hour', 'match' => 'any', 'status_is' => array( @@ -3131,8 +3135,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case { // status_is OR coupon_excludes. $query_args = array( - 'after' => $current_hour_start->format( WC_Admin_Reports_Interval::$sql_datetime_format ), - 'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ), + 'after' => $current_hour_start->format( TimeInterval::$sql_datetime_format ), + 'before' => $current_hour_end->format( TimeInterval::$sql_datetime_format ), 'interval' => 'hour', 'match' => 'any', 'status_is' => array( @@ -3213,8 +3217,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case { // product_includes OR coupon_includes. $query_args = array( - 'after' => $current_hour_start->format( WC_Admin_Reports_Interval::$sql_datetime_format ), - 'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ), + 'after' => $current_hour_start->format( TimeInterval::$sql_datetime_format ), + 'before' => $current_hour_end->format( TimeInterval::$sql_datetime_format ), 'interval' => 'hour', 'match' => 'any', 'product_includes' => array( @@ -3295,8 +3299,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case { // status_is OR product_includes OR coupon_includes. $query_args = array( - 'after' => $current_hour_start->format( WC_Admin_Reports_Interval::$sql_datetime_format ), - 'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ), + 'after' => $current_hour_start->format( TimeInterval::$sql_datetime_format ), + 'before' => $current_hour_end->format( TimeInterval::$sql_datetime_format ), 'interval' => 'hour', 'match' => 'any', 'status_is' => array( @@ -3380,8 +3384,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case { // status_is OR status_is_not OR product_includes. $query_args = array( - 'after' => $current_hour_start->format( WC_Admin_Reports_Interval::$sql_datetime_format ), - 'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ), + 'after' => $current_hour_start->format( TimeInterval::$sql_datetime_format ), + 'before' => $current_hour_end->format( TimeInterval::$sql_datetime_format ), 'interval' => 'hour', 'match' => 'any', 'status_is' => array( @@ -3468,8 +3472,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case { // status_is OR status_is_not OR product_includes OR product_excludes. $query_args = array( - 'after' => $current_hour_start->format( WC_Admin_Reports_Interval::$sql_datetime_format ), - 'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ), + 'after' => $current_hour_start->format( TimeInterval::$sql_datetime_format ), + 'before' => $current_hour_end->format( TimeInterval::$sql_datetime_format ), 'interval' => 'hour', 'match' => 'any', 'status_is' => array( @@ -3556,8 +3560,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case { // status_is OR status_is_not OR product_includes OR product_excludes OR coupon_includes. $query_args = array( - 'after' => $current_hour_start->format( WC_Admin_Reports_Interval::$sql_datetime_format ), - 'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ), + 'after' => $current_hour_start->format( TimeInterval::$sql_datetime_format ), + 'before' => $current_hour_end->format( TimeInterval::$sql_datetime_format ), 'interval' => 'hour', 'match' => 'any', 'status_is' => array( @@ -3647,8 +3651,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case { // status_is OR status_is_not OR product_includes OR product_excludes OR coupon_includes OR coupon_excludes. $query_args = array( - 'after' => $current_hour_start->format( WC_Admin_Reports_Interval::$sql_datetime_format ), - 'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ), + 'after' => $current_hour_start->format( TimeInterval::$sql_datetime_format ), + 'before' => $current_hour_end->format( TimeInterval::$sql_datetime_format ), 'interval' => 'hour', 'match' => 'any', 'status_is' => array( @@ -3744,10 +3748,10 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case { /** * Test if lookup tables are cleaned after delete an order. * - * @covers WC_Admin_Reports_Orders_Stats_Data_Store::delete_order - * @covers WC_Admin_Reports_Products_Data_Store::sync_on_order_delete - * @covers WC_Admin_Reports_Coupons_Data_Store::sync_on_order_delete - * @covers WC_Admin_Reports_Taxes_Data_Store::sync_on_order_delete + * @covers \Automattic\WooCommerce\Admin\API\Reports\Orders\Stats\DataStore::delete_order + * @covers \Automattic\WooCommerce\Admin\API\Reports\Products\DataStore::sync_on_order_delete + * @covers \Automattic\WooCommerce\Admin\API\Reports\Coupons\DataStore::sync_on_order_delete + * @covers \Automattic\WooCommerce\Admin\API\Reports\Taxes\DataStore::sync_on_order_delete */ public function test_order_deletion() { global $wpdb; @@ -3938,7 +3942,7 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case { WC_Helper_Queue::run_all_pending(); - $data_store = new WC_Admin_Reports_Orders_Stats_Data_Store(); + $data_store = new OrdersStatsDataStore(); // Tests for before & after set to current hour. $now = new DateTime(); @@ -3967,8 +3971,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case { $i3_end->setTimestamp( $i3_end_timestamp ); $query_args = array( - 'after' => $two_hours_back->format( WC_Admin_Reports_Interval::$sql_datetime_format ), - 'before' => $now->format( WC_Admin_Reports_Interval::$sql_datetime_format ), + 'after' => $two_hours_back->format( TimeInterval::$sql_datetime_format ), + 'before' => $now->format( TimeInterval::$sql_datetime_format ), 'interval' => 'hour', 'segmentby' => 'product', ); @@ -4502,7 +4506,7 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case { WC_Helper_Queue::run_all_pending(); - $data_store = new WC_Admin_Reports_Orders_Stats_Data_Store(); + $data_store = new OrdersStatsDataStore(); // Tests for before & after set to current hour. $current_hour_start = new DateTime(); @@ -4518,8 +4522,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case { // Test for current hour--only 1 hour visible. // DESC. $query_args = array( - 'after' => $current_hour_start->format( WC_Admin_Reports_Interval::$sql_datetime_format ), - 'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ), + 'after' => $current_hour_start->format( TimeInterval::$sql_datetime_format ), + 'before' => $current_hour_end->format( TimeInterval::$sql_datetime_format ), 'interval' => 'hour', 'orderby' => 'date', 'order' => 'desc', @@ -4584,8 +4588,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case { // ASC -- only 1 interval, so should be the same. $query_args = array( - 'after' => $current_hour_start->format( WC_Admin_Reports_Interval::$sql_datetime_format ), - 'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ), + 'after' => $current_hour_start->format( TimeInterval::$sql_datetime_format ), + 'before' => $current_hour_end->format( TimeInterval::$sql_datetime_format ), 'interval' => 'hour', 'orderby' => 'date', 'order' => 'asc', @@ -4608,8 +4612,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case { $minus_5_hours->setTimestamp( $now_timestamp - $hour_offset * HOUR_IN_SECONDS ); $query_args = array( - 'after' => $minus_5_hours->format( WC_Admin_Reports_Interval::$sql_datetime_format ), - 'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ), + 'after' => $minus_5_hours->format( TimeInterval::$sql_datetime_format ), + 'before' => $current_hour_end->format( TimeInterval::$sql_datetime_format ), 'interval' => 'hour', 'orderby' => 'date', 'order' => 'desc', @@ -4724,8 +4728,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case { // ASC -- reverse the intervals array, but numbers stay the same. $query_args = array( - 'after' => $minus_5_hours->format( WC_Admin_Reports_Interval::$sql_datetime_format ), - 'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ), + 'after' => $minus_5_hours->format( TimeInterval::$sql_datetime_format ), + 'before' => $current_hour_end->format( TimeInterval::$sql_datetime_format ), 'interval' => 'hour', 'orderby' => 'date', 'order' => 'asc', @@ -4751,8 +4755,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case { $minus_9_hours->setTimestamp( $now_timestamp - $hour_offset * HOUR_IN_SECONDS ); $query_args = array( - 'after' => $minus_9_hours->format( WC_Admin_Reports_Interval::$sql_datetime_format ), - 'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ), + 'after' => $minus_9_hours->format( TimeInterval::$sql_datetime_format ), + 'before' => $current_hour_end->format( TimeInterval::$sql_datetime_format ), 'interval' => 'hour', 'orderby' => 'date', 'order' => 'desc', @@ -4864,8 +4868,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case { // ASC -- same values, just reverse order of intervals. $query_args = array( - 'after' => $minus_9_hours->format( WC_Admin_Reports_Interval::$sql_datetime_format ), - 'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ), + 'after' => $minus_9_hours->format( TimeInterval::$sql_datetime_format ), + 'before' => $current_hour_end->format( TimeInterval::$sql_datetime_format ), 'interval' => 'hour', 'orderby' => 'date', 'order' => 'asc', @@ -4900,8 +4904,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case { // Page 1. $query_args = array( - 'after' => $minus_10_hours->format( WC_Admin_Reports_Interval::$sql_datetime_format ), - 'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ), + 'after' => $minus_10_hours->format( TimeInterval::$sql_datetime_format ), + 'before' => $current_hour_end->format( TimeInterval::$sql_datetime_format ), 'interval' => 'hour', 'orderby' => 'date', 'order' => 'desc', @@ -5015,8 +5019,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case { // Page 2. $query_args = array( - 'after' => $minus_10_hours->format( WC_Admin_Reports_Interval::$sql_datetime_format ), - 'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ), + 'after' => $minus_10_hours->format( TimeInterval::$sql_datetime_format ), + 'before' => $current_hour_end->format( TimeInterval::$sql_datetime_format ), 'interval' => 'hour', 'orderby' => 'date', 'order' => 'desc', @@ -5069,8 +5073,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case { // Page 1. $query_args = array( - 'after' => $minus_10_hours->format( WC_Admin_Reports_Interval::$sql_datetime_format ), - 'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ), + 'after' => $minus_10_hours->format( TimeInterval::$sql_datetime_format ), + 'before' => $current_hour_end->format( TimeInterval::$sql_datetime_format ), 'interval' => 'hour', 'orderby' => 'date', 'order' => 'asc', @@ -5106,8 +5110,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case { // Page 2. $query_args = array( - 'after' => $minus_10_hours->format( WC_Admin_Reports_Interval::$sql_datetime_format ), - 'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ), + 'after' => $minus_10_hours->format( TimeInterval::$sql_datetime_format ), + 'before' => $current_hour_end->format( TimeInterval::$sql_datetime_format ), 'interval' => 'hour', 'orderby' => 'date', 'order' => 'asc', @@ -5275,7 +5279,7 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case { global $wpdb; $res = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}wc_order_stats" ); - $data_store = new WC_Admin_Reports_Orders_Stats_Data_Store(); + $data_store = new OrdersStatsDataStore(); // Tests for before & after set to current hour. // (this sets minutes for current hour to 0, seconds are left as they arem e.g. 15:23:43 becomes 15:00:43). @@ -5293,8 +5297,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case { // Test 1: only one hour visible, so only 1 interval in the response, no real ordering. // DESC. $query_args = array( - 'after' => $current_hour_start->format( WC_Admin_Reports_Interval::$sql_datetime_format ), - 'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ), + 'after' => $current_hour_start->format( TimeInterval::$sql_datetime_format ), + 'before' => $current_hour_end->format( TimeInterval::$sql_datetime_format ), 'interval' => 'hour', 'orderby' => 'orders_count', 'order' => 'desc', @@ -5360,8 +5364,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case { // ASC -- only 1 interval, so should be the same. $query_args = array( - 'after' => $current_hour_start->format( WC_Admin_Reports_Interval::$sql_datetime_format ), - 'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ), + 'after' => $current_hour_start->format( TimeInterval::$sql_datetime_format ), + 'before' => $current_hour_end->format( TimeInterval::$sql_datetime_format ), 'interval' => 'hour', 'orderby' => 'orders_count', 'order' => 'asc', @@ -5383,8 +5387,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case { $minus_5_hours->setTimestamp( $now_timestamp - $hour_offset * HOUR_IN_SECONDS ); $query_args = array( - 'after' => $minus_5_hours->format( WC_Admin_Reports_Interval::$sql_datetime_format ), - 'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ), + 'after' => $minus_5_hours->format( TimeInterval::$sql_datetime_format ), + 'before' => $current_hour_end->format( TimeInterval::$sql_datetime_format ), 'interval' => 'hour', 'orderby' => 'orders_count', 'order' => 'desc', @@ -5531,8 +5535,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case { array_push( $expected_stats['intervals'], $to_be_last ); $query_args = array( - 'after' => $minus_5_hours->format( WC_Admin_Reports_Interval::$sql_datetime_format ), - 'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ), + 'after' => $minus_5_hours->format( TimeInterval::$sql_datetime_format ), + 'before' => $current_hour_end->format( TimeInterval::$sql_datetime_format ), 'interval' => 'hour', 'orderby' => 'orders_count', 'order' => 'asc', @@ -5552,8 +5556,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case { $minus_9_hours->setTimestamp( $now_timestamp - $hour_offset * HOUR_IN_SECONDS ); $query_args = array( - 'after' => $minus_9_hours->format( WC_Admin_Reports_Interval::$sql_datetime_format ), - 'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ), + 'after' => $minus_9_hours->format( TimeInterval::$sql_datetime_format ), + 'before' => $current_hour_end->format( TimeInterval::$sql_datetime_format ), 'interval' => 'hour', 'orderby' => 'orders_count', 'order' => 'desc', @@ -5697,8 +5701,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case { array_push( $expected_stats['intervals'], $to_be_last ); $query_args = array( - 'after' => $minus_9_hours->format( WC_Admin_Reports_Interval::$sql_datetime_format ), - 'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ), + 'after' => $minus_9_hours->format( TimeInterval::$sql_datetime_format ), + 'before' => $current_hour_end->format( TimeInterval::$sql_datetime_format ), 'interval' => 'hour', 'orderby' => 'orders_count', 'order' => 'asc', @@ -5719,8 +5723,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case { // Page 1. $query_args = array( - 'after' => $minus_10_hours->format( WC_Admin_Reports_Interval::$sql_datetime_format ), - 'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ), + 'after' => $minus_10_hours->format( TimeInterval::$sql_datetime_format ), + 'before' => $current_hour_end->format( TimeInterval::$sql_datetime_format ), 'interval' => 'hour', 'orderby' => 'orders_count', 'order' => 'desc', @@ -5849,8 +5853,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case { // Page 2. $query_args = array( - 'after' => $minus_10_hours->format( WC_Admin_Reports_Interval::$sql_datetime_format ), - 'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ), + 'after' => $minus_10_hours->format( TimeInterval::$sql_datetime_format ), + 'before' => $current_hour_end->format( TimeInterval::$sql_datetime_format ), 'interval' => 'hour', 'orderby' => 'orders_count', 'order' => 'desc', @@ -5903,8 +5907,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case { // Page 1. $query_args = array( - 'after' => $minus_10_hours->format( WC_Admin_Reports_Interval::$sql_datetime_format ), - 'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ), + 'after' => $minus_10_hours->format( TimeInterval::$sql_datetime_format ), + 'before' => $current_hour_end->format( TimeInterval::$sql_datetime_format ), 'interval' => 'hour', 'orderby' => 'orders_count', 'order' => 'asc', @@ -5941,8 +5945,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case { // Page 2. $query_args = array( - 'after' => $minus_10_hours->format( WC_Admin_Reports_Interval::$sql_datetime_format ), - 'before' => $current_hour_end->format( WC_Admin_Reports_Interval::$sql_datetime_format ), + 'after' => $minus_10_hours->format( TimeInterval::$sql_datetime_format ), + 'before' => $current_hour_end->format( TimeInterval::$sql_datetime_format ), 'interval' => 'hour', 'orderby' => 'orders_count', 'order' => 'asc', @@ -6002,7 +6006,7 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case { $product->set_regular_price( 25 ); $product->save(); - $data_store = new WC_Admin_Reports_Orders_Stats_Data_Store(); + $data_store = new OrdersStatsDataStore(); // All empty in the beginning. $query_args = array( @@ -6113,7 +6117,7 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case { $product->set_regular_price( 25 ); $product->save(); - $data_store = new WC_Admin_Reports_Orders_Stats_Data_Store(); + $data_store = new OrdersStatsDataStore(); // All empty in the beginning. $query_args = array( diff --git a/plugins/woocommerce-admin/tests/reports/class-wc-tests-reports-products.php b/plugins/woocommerce-admin/tests/reports/class-wc-tests-reports-products.php index d02e4e48dc5..ec02f0a3ac6 100644 --- a/plugins/woocommerce-admin/tests/reports/class-wc-tests-reports-products.php +++ b/plugins/woocommerce-admin/tests/reports/class-wc-tests-reports-products.php @@ -6,6 +6,9 @@ * @todo Finish up unit testing to verify bug-free product reports. */ +use \Automattic\WooCommerce\Admin\API\Reports\Products\DataStore as ProductsDataStore; +use \Automattic\WooCommerce\Admin\API\Reports\Products\Query as ProductsQuery; + /** * Reports product stats tests class * @@ -40,7 +43,7 @@ class WC_Tests_Reports_Products extends WC_Unit_Test_Case { WC_Helper_Queue::run_all_pending(); - $data_store = new WC_Admin_Reports_Products_Data_Store(); + $data_store = new ProductsDataStore(); $start_time = date( 'Y-m-d H:00:00', $order->get_date_created()->getOffsetTimestamp() ); $end_time = date( 'Y-m-d H:00:00', $order->get_date_created()->getOffsetTimestamp() + HOUR_IN_SECONDS ); $args = array( @@ -67,7 +70,7 @@ class WC_Tests_Reports_Products extends WC_Unit_Test_Case { $this->assertEquals( $expected_data, $data ); // Test retrieving the stats through the query class. - $query = new WC_Admin_Reports_Products_Query( $args ); + $query = new ProductsQuery( $args ); $this->assertEquals( $expected_data, $query->get_data() ); } @@ -117,7 +120,7 @@ class WC_Tests_Reports_Products extends WC_Unit_Test_Case { WC_Helper_Queue::run_all_pending(); - $data_store = new WC_Admin_Reports_Products_Data_Store(); + $data_store = new ProductsDataStore(); $start_time = date( 'Y-m-d H:00:00', $order->get_date_created()->getOffsetTimestamp() ); $end_time = date( 'Y-m-d H:00:00', $order_2->get_date_created()->getOffsetTimestamp() + HOUR_IN_SECONDS ); // Test retrieving the stats through the data store, default order by date/time desc. @@ -183,7 +186,7 @@ class WC_Tests_Reports_Products extends WC_Unit_Test_Case { $this->assertEquals( $expected_data, $data ); // Test retrieving the stats through the query class. - $query = new WC_Admin_Reports_Products_Query( $args ); + $query = new ProductsQuery( $args ); $this->assertEquals( $expected_data, $query->get_data() ); } @@ -218,7 +221,7 @@ class WC_Tests_Reports_Products extends WC_Unit_Test_Case { WC_Helper_Queue::run_all_pending(); - $data_store = new WC_Admin_Reports_Products_Data_Store(); + $data_store = new ProductsDataStore(); $start_time = date( 'Y-m-d H:00:00', $order->get_date_created()->getOffsetTimestamp() ); $end_time = date( 'Y-m-d H:00:00', $order->get_date_created()->getOffsetTimestamp() + HOUR_IN_SECONDS ); $args = array( @@ -299,7 +302,7 @@ class WC_Tests_Reports_Products extends WC_Unit_Test_Case { WC_Helper_Queue::run_all_pending(); - $data_store = new WC_Admin_Reports_Products_Data_Store(); + $data_store = new ProductsDataStore(); $start_time = date( 'Y-m-d H:00:00', $order->get_date_created()->getOffsetTimestamp() ); $end_time = date( 'Y-m-d H:00:00', $order->get_date_created()->getOffsetTimestamp() + HOUR_IN_SECONDS ); $args = array( @@ -381,7 +384,7 @@ class WC_Tests_Reports_Products extends WC_Unit_Test_Case { WC_Helper_Queue::run_all_pending(); - $data_store = new WC_Admin_Reports_Products_Data_Store(); + $data_store = new ProductsDataStore(); $start_time = date( 'Y-m-d H:00:00', $order->get_date_created()->getOffsetTimestamp() ); $end_time = date( 'Y-m-d H:00:00', $order->get_date_created()->getOffsetTimestamp() + HOUR_IN_SECONDS ); $args = array( @@ -408,7 +411,7 @@ class WC_Tests_Reports_Products extends WC_Unit_Test_Case { $this->assertEquals( $expected_data, $data ); // Test retrieving the stats through the query class. - $query = new WC_Admin_Reports_Products_Query( $args ); + $query = new ProductsQuery( $args ); $this->assertEquals( $expected_data, $query->get_data() ); } } diff --git a/plugins/woocommerce-admin/tests/reports/class-wc-tests-reports-revenue-stats.php b/plugins/woocommerce-admin/tests/reports/class-wc-tests-reports-revenue-stats.php index 7bcce7aa1f0..77186ebec61 100644 --- a/plugins/woocommerce-admin/tests/reports/class-wc-tests-reports-revenue-stats.php +++ b/plugins/woocommerce-admin/tests/reports/class-wc-tests-reports-revenue-stats.php @@ -6,6 +6,9 @@ * @todo Finish up unit testing to verify bug-free order reports. */ +use \Automattic\WooCommerce\Admin\API\Reports\Orders\Stats\DataStore as OrdersStatsDataStore; +use \Automattic\WooCommerce\Admin\API\Reports\Revenue\Query as RevenueQuery; + /** * Class WC_Admin_Tests_Reports_Revenue_Stats */ @@ -43,7 +46,7 @@ class WC_Admin_Tests_Reports_Revenue_Stats extends WC_Unit_Test_Case { WC_Helper_Queue::run_all_pending(); // /reports/revenue/stats is mapped to Orders_Data_Store. - $data_store = new WC_Admin_Reports_Orders_Stats_Data_Store(); + $data_store = new OrdersStatsDataStore(); $start_time = date( 'Y-m-d H:00:00', $order->get_date_created()->getOffsetTimestamp() ); $end_time = date( 'Y-m-d H:59:59', $order->get_date_created()->getOffsetTimestamp() ); @@ -144,7 +147,7 @@ class WC_Admin_Tests_Reports_Revenue_Stats extends WC_Unit_Test_Case { 'pages' => 1, 'page_no' => 1, ); - $query = new WC_Admin_Reports_Revenue_Query( $args ); + $query = new RevenueQuery( $args ); $this->assertEquals( $expected_stats, json_decode( wp_json_encode( $query->get_data() ), true ) ); } } diff --git a/plugins/woocommerce-admin/tests/reports/class-wc-tests-reports-variations.php b/plugins/woocommerce-admin/tests/reports/class-wc-tests-reports-variations.php index fd44d4e957f..e42dcdaf8ba 100644 --- a/plugins/woocommerce-admin/tests/reports/class-wc-tests-reports-variations.php +++ b/plugins/woocommerce-admin/tests/reports/class-wc-tests-reports-variations.php @@ -6,6 +6,9 @@ * @todo Finish up unit testing to verify bug-free order reports. */ +use \Automattic\WooCommerce\Admin\API\Reports\Variations\DataStore as VariationsDataStore; +use \Automattic\WooCommerce\Admin\API\Reports\Variations\Query as VariationsQuery; + /** * Reports order stats tests class. * @@ -41,7 +44,7 @@ class WC_Tests_Reports_Variations extends WC_Unit_Test_Case { WC_Helper_Queue::run_all_pending(); - $data_store = new WC_Admin_Reports_Variations_Data_Store(); + $data_store = new VariationsDataStore(); $start_time = date( 'Y-m-d H:00:00', $order->get_date_created()->getOffsetTimestamp() ); $end_time = date( 'Y-m-d H:00:00', $order->get_date_created()->getOffsetTimestamp() + HOUR_IN_SECONDS ); $args = array( @@ -69,7 +72,7 @@ class WC_Tests_Reports_Variations extends WC_Unit_Test_Case { $this->assertEquals( $expected_data, $data ); // Test retrieving the stats through the query class. - $query = new WC_Admin_Reports_Variations_Query( $args ); + $query = new VariationsQuery( $args ); $this->assertEquals( $expected_data, $query->get_data() ); } @@ -110,7 +113,7 @@ class WC_Tests_Reports_Variations extends WC_Unit_Test_Case { WC_Helper_Queue::run_all_pending(); - $data_store = new WC_Admin_Reports_Variations_Data_Store(); + $data_store = new VariationsDataStore(); $start_time = date( 'Y-m-d H:00:00', $order->get_date_created()->getOffsetTimestamp() ); $end_time = date( 'Y-m-d H:00:00', $order->get_date_created()->getOffsetTimestamp() + HOUR_IN_SECONDS ); $args = array( diff --git a/plugins/woocommerce-admin/uninstall.php b/plugins/woocommerce-admin/uninstall.php index 23410555484..8cc33f23a64 100644 --- a/plugins/woocommerce-admin/uninstall.php +++ b/plugins/woocommerce-admin/uninstall.php @@ -7,6 +7,8 @@ defined( 'WP_UNINSTALL_PLUGIN' ) || exit; +use \Automattic\WooCommerce\Admin\Install; + require_once dirname( __FILE__ ) . '/woocommerce-admin.php'; -require_once dirname( __FILE__ ) . '/includes/class-wc-admin-install.php'; -WC_Admin_Install::delete_table_data(); + +Install::delete_table_data(); diff --git a/plugins/woocommerce-admin/woocommerce-admin.php b/plugins/woocommerce-admin/woocommerce-admin.php index 25aa420e40b..c0cacbeceb8 100755 --- a/plugins/woocommerce-admin/woocommerce-admin.php +++ b/plugins/woocommerce-admin/woocommerce-admin.php @@ -19,324 +19,51 @@ defined( 'ABSPATH' ) || exit; +use \Automattic\WooCommerce\Admin\FeaturePlugin; + /** - * Feature plugin main class. + * Autoload packages. * - * @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. + * We want to fail gracefully if `composer install` has not been executed yet, so we are checking for the autoloader. + * If the autoloader is not present, let's log the failure and display a nice admin notice. */ -class WC_Admin_Feature_Plugin { - /** - * 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() { - require_once WC_ADMIN_ABSPATH . 'includes/class-wc-admin-install.php'; - WC_Admin_Install::create_tables(); - WC_Admin_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(); - WC_Admin_Reports_Sync::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( __FILE__ ) . '/' ); - $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', __FILE__ ); - // 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( __FILE__ ) ) . '/languages' ); - } - - /** - * Include WC Admin classes. - */ - public function includes() { - require_once WC_ADMIN_ABSPATH . 'includes/core-functions.php'; - - // Initialize the WC API extensions. - require_once WC_ADMIN_ABSPATH . 'includes/class-wc-admin-reports-sync.php'; - require_once WC_ADMIN_ABSPATH . 'includes/class-wc-admin-install.php'; - require_once WC_ADMIN_ABSPATH . 'includes/class-wc-admin-events.php'; - require_once WC_ADMIN_ABSPATH . 'includes/class-wc-admin-api-init.php'; - require_once WC_ADMIN_ABSPATH . 'includes/export/class-wc-admin-report-csv-exporter.php'; - require_once WC_ADMIN_ABSPATH . 'includes/export/class-wc-admin-report-exporter.php'; - - // Data triggers. - require_once WC_ADMIN_ABSPATH . 'includes/data-stores/class-wc-admin-notes-data-store.php'; - - // CRUD classes. - require_once WC_ADMIN_ABSPATH . 'includes/notes/class-wc-admin-note.php'; - require_once WC_ADMIN_ABSPATH . 'includes/notes/class-wc-admin-notes.php'; - - // Admin note providers. - // @todo These should be bundled in the features/ folder, but loading them from there currently has a load order issue. - require_once WC_ADMIN_ABSPATH . 'includes/notes/class-wc-admin-notes-new-sales-record.php'; - require_once WC_ADMIN_ABSPATH . 'includes/notes/class-wc-admin-notes-settings-notes.php'; - require_once WC_ADMIN_ABSPATH . 'includes/notes/class-wc-admin-notes-giving-feedback-notes.php'; - require_once WC_ADMIN_ABSPATH . 'includes/notes/class-wc-admin-notes-woo-subscriptions-notes.php'; - require_once WC_ADMIN_ABSPATH . 'includes/notes/class-wc-admin-notes-historical-data.php'; - require_once WC_ADMIN_ABSPATH . 'includes/notes/class-wc-admin-notes-order-milestones.php'; - require_once WC_ADMIN_ABSPATH . 'includes/notes/class-wc-admin-notes-mobile-app.php'; - require_once WC_ADMIN_ABSPATH . 'includes/notes/class-wc-admin-notes-welcome-message.php'; - } - - /** - * 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; - } - - // Include our store class here instead of wc_admin_plugins_loaded() - // because ActionScheduler is hooked into `plugins_loaded` at a - // much higher priority. - require_once WC_ADMIN_ABSPATH . '/includes/class-wc-admin-actionscheduler-wppoststore.php'; - - return 'WC_Admin_ActionScheduler_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 ); - - require_once WC_ADMIN_ABSPATH . 'includes/class-wc-admin-loader.php'; - - 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( __FILE__ ) . '/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 WooCommerce 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 WordPress 5.2 or greater and WooCommerce 3.6 or greater to be installed and active.', 'woocommerce-admin' ), - 'https://wordpress.org/', - 'https://wordpress.org/plugins/woocommerce/' - ); - } - printf( '

%s

', $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 npm install to install dependencies, npm run build 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 the releases page in the repository.', 'woocommerce-admin' ), - 'https://github.com/woocommerce/woocommerce-admin/releases' +$autoloader = __DIR__ . '/vendor/autoload.php'; +if ( is_readable( $autoloader ) ) { + require $autoloader; +} else { + if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { + error_log( // phpcs:ignore + sprintf( + /* translators: 1: composer command. 2: plugin directory */ + esc_html__( 'Your installation of the WooCommerce Admin feature plugin is incomplete. Please run %1$s within the %2$s directory.', 'woocommerce-admin' ), + '`composer install`', + '`' . esc_html( str_replace( ABSPATH, '', __DIR__ ) ) . '`' + ) ); - printf( '

%s %s

', $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. + * Outputs an admin notice if composer install has not been ran. */ - public function replace_supported_features( $features ) { - if ( ! function_exists( 'wc_admin_get_feature_config' ) ) { - require_once WC_ADMIN_ABSPATH . '/includes/feature-config.php'; + add_action( + 'admin_notices', + function() { + ?> +
+

+ composer install', + '' . esc_html( str_replace( ABSPATH, '', __DIR__ ) ) . '' + ); + ?> +

+
+ '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() {} + ); + return; } -WC_Admin_Feature_Plugin::instance()->init(); +FeaturePlugin::instance()->init();