woocommerce/plugins/woocommerce-admin/src/API/Init.php

163 lines
6.9 KiB
PHP
Raw Normal View History

<?php
2018-09-18 19:41:45 +00:00
/**
* REST API bootstrap.
*/
namespace Automattic\WooCommerce\Admin\API;
2018-09-18 19:41:45 +00:00
defined( 'ABSPATH' ) || exit;
use \Automattic\WooCommerce\Admin\Loader;
2019-08-12 18:11:28 +00:00
2018-09-18 19:41:45 +00:00
/**
* Init class.
2018-09-18 19:41:45 +00:00
*/
class Init {
/**
* The single instance of the class.
*
* @var object
*/
protected static $instance = null;
/**
* Get class instance.
*
* @return object Instance.
*/
final public static function instance() {
if ( null === static::$instance ) {
static::$instance = new static();
}
return static::$instance;
}
2018-09-18 19:41:45 +00:00
/**
* Boostrap REST API.
*/
public function __construct() {
// Hook in data stores.
add_filter( 'woocommerce_data_stores', array( __CLASS__, 'add_data_stores' ) );
// REST API extensions init.
add_action( 'rest_api_init', array( $this, 'rest_api_init' ) );
// Add currency symbol to orders endpoint response.
add_filter( 'woocommerce_rest_prepare_shop_order_object', array( __CLASS__, 'add_currency_symbol_to_order_response' ) );
}
2018-09-18 19:41:45 +00:00
/**
* Init REST API.
*/
public function rest_api_init() {
$controllers = array(
2019-08-02 18:17:59 +00:00
'Automattic\WooCommerce\Admin\API\Notes',
'Automattic\WooCommerce\Admin\API\NoteActions',
2019-08-02 18:54:09 +00:00
'Automattic\WooCommerce\Admin\API\Coupons',
'Automattic\WooCommerce\Admin\API\Customers',
'Automattic\WooCommerce\Admin\API\Data',
'Automattic\WooCommerce\Admin\API\DataCountries',
'Automattic\WooCommerce\Admin\API\DataDownloadIPs',
'Automattic\WooCommerce\Admin\API\Leaderboards',
'Automattic\WooCommerce\Admin\API\Marketing',
'Automattic\WooCommerce\Admin\API\MarketingOverview',
'Automattic\WooCommerce\Admin\API\Options',
2019-08-02 18:54:09 +00:00
'Automattic\WooCommerce\Admin\API\Orders',
'Automattic\WooCommerce\Admin\API\Products',
'Automattic\WooCommerce\Admin\API\ProductAttributes',
'Automattic\WooCommerce\Admin\API\ProductAttributeTerms',
2019-08-02 18:54:09 +00:00
'Automattic\WooCommerce\Admin\API\ProductCategories',
'Automattic\WooCommerce\Admin\API\ProductVariations',
'Automattic\WooCommerce\Admin\API\ProductReviews',
'Automattic\WooCommerce\Admin\API\ProductVariations',
'Automattic\WooCommerce\Admin\API\Reports\Controller',
2019-08-02 18:54:09 +00:00
'Automattic\WooCommerce\Admin\API\SettingOptions',
'Automattic\WooCommerce\Admin\API\Reports\Import\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Export\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Products\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Variations\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Products\Stats\Controller',
Add Variations Report (https://github.com/woocommerce/woocommerce-admin/pull/5167) * Add initial Variations Report to analytics feature. * Restrict query to variations when not specifying any product IDs. * Add route to get variations without specifying a parent. * Move variations table component to variations report directory. * Add missing LIMIT clause to variations report query. * Remove broken features from Variations table. * Add Variations report controller to CSV emailer. * Add initial Variation Stats endpoint, based on Product Stats. * Hook Variations Report components up to new stats endpoint. * Hook attribute filter up to variations report queries. * Remove variation title filter usage. See: https://github.com/woocommerce/woocommerce-admin/pull/5100 * Use filtered separator in variation name formatting. * Add "single variation" to variations report, fix autocompleter API request. * Fix segmentation by variation. * Add comparison to variations report. * Always include manually specified variations in report results. * Fix variations report table comparison mode. The ReportTable component expects the `filter` query param. * Fixing styling of compare button without table search component. * Add variation filter to Orders report. * Link orders count to orders report filtered by variation. * Orders report: include variation attributes in product names. * Further style tweaks for variations report download button. * Add variations filter to order stats query. * Clean up "category includes" login in REST controllers. Prep for "category excludes" in the Variations report. * Support category exclusion in report filters. * Fix filter param used by the variation report table component. * Add category filter to variations report. * Fix initial selected ReportTable rows when using non-default compareParam. * Add a new autocompleter for variable products. * Add products filter to variations report. * Fix tests. * Handle variation IDs that are no longer found. * Add documentation. * Use getSetting() instead of directly accessing window properties in client code. * Fix ordering Variations by SKU.
2020-09-25 13:57:48 +00:00
'Automattic\WooCommerce\Admin\API\Reports\Variations\Stats\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Revenue\Stats\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Orders\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Orders\Stats\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Categories\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Taxes\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Taxes\Stats\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Coupons\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Coupons\Stats\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Stock\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Stock\Stats\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Downloads\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Downloads\Stats\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Customers\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Customers\Stats\Controller',
2019-08-02 18:54:09 +00:00
'Automattic\WooCommerce\Admin\API\Taxes',
'Automattic\WooCommerce\Admin\API\Themes',
'Automattic\WooCommerce\Admin\API\Plugins',
'Automattic\WooCommerce\Admin\API\OnboardingProfile',
'Automattic\WooCommerce\Admin\API\OnboardingTasks',
'Automattic\WooCommerce\Admin\API\OnboardingThemes',
'Automattic\WooCommerce\Admin\API\NavigationFavorites',
);
// The performance indicators controller must be registered last, after other /stats endpoints have been registered.
$controllers[] = 'Automattic\WooCommerce\Admin\API\Reports\PerformanceIndicators\Controller';
$controllers = apply_filters( 'woocommerce_admin_rest_controllers', $controllers );
foreach ( $controllers as $controller ) {
$this->$controller = new $controller();
$this->$controller->register_routes();
}
}
/**
2018-09-18 19:41:45 +00:00
* 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(
Add Variations Report (https://github.com/woocommerce/woocommerce-admin/pull/5167) * Add initial Variations Report to analytics feature. * Restrict query to variations when not specifying any product IDs. * Add route to get variations without specifying a parent. * Move variations table component to variations report directory. * Add missing LIMIT clause to variations report query. * Remove broken features from Variations table. * Add Variations report controller to CSV emailer. * Add initial Variation Stats endpoint, based on Product Stats. * Hook Variations Report components up to new stats endpoint. * Hook attribute filter up to variations report queries. * Remove variation title filter usage. See: https://github.com/woocommerce/woocommerce-admin/pull/5100 * Use filtered separator in variation name formatting. * Add "single variation" to variations report, fix autocompleter API request. * Fix segmentation by variation. * Add comparison to variations report. * Always include manually specified variations in report results. * Fix variations report table comparison mode. The ReportTable component expects the `filter` query param. * Fixing styling of compare button without table search component. * Add variation filter to Orders report. * Link orders count to orders report filtered by variation. * Orders report: include variation attributes in product names. * Further style tweaks for variations report download button. * Add variations filter to order stats query. * Clean up "category includes" login in REST controllers. Prep for "category excludes" in the Variations report. * Support category exclusion in report filters. * Fix filter param used by the variation report table component. * Add category filter to variations report. * Fix initial selected ReportTable rows when using non-default compareParam. * Add a new autocompleter for variable products. * Add products filter to variations report. * Fix tests. * Handle variation IDs that are no longer found. * Add documentation. * Use getSetting() instead of directly accessing window properties in client code. * Fix ordering Variations by SKU.
2020-09-25 13:57:48 +00:00
'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-variations-stats' => 'Automattic\WooCommerce\Admin\API\Reports\Variations\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 formatAmount().
*
* @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;
}
}