2019-04-11 18:31:31 +00:00
< ? php
/**
* WooCommerce Analytics .
* NOTE : DO NOT edit this file in WooCommerce core , this is generated from woocommerce - admin .
*/
2019-08-01 18:17:08 +00:00
namespace Automattic\WooCommerce\Admin\Features ;
2020-06-03 20:42:30 +00:00
use Automattic\WooCommerce\Admin\Loader ;
2020-06-16 13:47:58 +00:00
use Automattic\WooCommerce\Admin\API\Reports\Cache ;
2020-06-03 20:42:30 +00:00
2019-04-11 18:31:31 +00:00
/**
* Contains backend logic for the Analytics feature .
*/
2019-08-12 18:50:22 +00:00
class Analytics {
2020-06-16 13:47:58 +00:00
/**
* Clear cache tool identifier .
*/
const CACHE_TOOL_ID = 'clear_woocommerce_analytics_cache' ;
2019-04-11 18:31:31 +00:00
/**
* Class instance .
*
2019-08-12 21:52:09 +00:00
* @ var Analytics instance
2019-04-11 18:31:31 +00:00
*/
protected static $instance = null ;
/**
* Get class instance .
*/
public static function get_instance () {
if ( ! self :: $instance ) {
self :: $instance = new self ();
}
return self :: $instance ;
}
/**
* Hook into WooCommerce .
*/
public function __construct () {
add_filter ( 'woocommerce_component_settings_preload_endpoints' , array ( $this , 'add_preload_endpoints' ) );
2019-12-17 13:36:22 +00:00
add_filter ( 'woocommerce_admin_get_user_data_fields' , array ( $this , 'add_user_data_fields' ) );
2019-04-11 18:31:31 +00:00
add_action ( 'admin_menu' , array ( $this , 'register_pages' ) );
2020-06-16 13:47:58 +00:00
add_filter ( 'woocommerce_debug_tools' , array ( $this , 'register_cache_clear_tool' ) );
2019-04-11 18:31:31 +00:00
}
/**
* Preload data from the countries endpoint .
*
* @ param array $endpoints Array of preloaded endpoints .
* @ return array
*/
public function add_preload_endpoints ( $endpoints ) {
2019-11-12 18:15:55 +00:00
$endpoints [ 'countries' ] = '/wc-analytics/data/countries' ;
2019-04-11 18:31:31 +00:00
return $endpoints ;
}
/**
* Adds fields so that we can store user preferences for the columns to display on a report .
*
* @ param array $user_data_fields User data fields .
* @ return array
*/
public function add_user_data_fields ( $user_data_fields ) {
return array_merge (
$user_data_fields ,
array (
'categories_report_columns' ,
'coupons_report_columns' ,
'customers_report_columns' ,
'orders_report_columns' ,
'products_report_columns' ,
'revenue_report_columns' ,
'taxes_report_columns' ,
'variations_report_columns' ,
)
);
}
2020-06-16 13:47:58 +00:00
/**
* Register the cache clearing tool on the WooCommerce > Status > Tools page .
*
* @ param array $debug_tools Available debug tool registrations .
* @ return array Filtered debug tool registrations .
*/
public function register_cache_clear_tool ( $debug_tools ) {
$settings_url = add_query_arg (
array (
'page' => 'wc-admin' ,
'path' => '/analytics/settings' ,
),
get_admin_url ( null , 'admin.php' )
);
$debug_tools [ self :: CACHE_TOOL_ID ] = array (
'name' => __ ( 'Clear analytics cache' , 'woocommerce-admin' ),
'button' => __ ( 'Clear' , 'woocommerce-admin' ),
'desc' => sprintf (
/* translators: 1: opening link tag, 2: closing tag */
2020-11-06 18:08:30 +00:00
__ ( 'This tool will reset the cached values used in WooCommerce Analytics. If numbers still look off, try %1$sReimporting Historical Data%2$s.' , 'woocommerce-admin' ),
2020-06-16 13:47:58 +00:00
'<a href="' . esc_url ( $settings_url ) . '">' ,
'</a>'
),
'callback' => array ( $this , 'run_clear_cache_tool' ),
);
return $debug_tools ;
}
2019-04-11 18:31:31 +00:00
/**
* Registers report pages .
*/
public function register_pages () {
2020-11-05 14:00:04 +00:00
$report_pages = self :: get_report_pages ();
foreach ( $report_pages as $report_page ) {
if ( ! is_null ( $report_page ) ) {
wc_admin_register_page ( $report_page );
}
}
}
2020-10-16 01:31:14 +00:00
2020-11-05 14:00:04 +00:00
/**
* Get report pages .
*/
public static function get_report_pages () {
2020-10-07 21:10:37 +00:00
$overview_page = array (
2020-11-05 14:00:04 +00:00
'id' => 'woocommerce-analytics' ,
'title' => __ ( 'Analytics' , 'woocommerce-admin' ),
'path' => '/analytics/overview' ,
'icon' => 'dashicons-chart-bar' ,
'position' => 56 , // After WooCommerce & Product menu items.
2020-10-07 21:10:37 +00:00
);
2020-09-14 23:44:46 +00:00
$report_pages = array (
2020-10-07 21:10:37 +00:00
$overview_page ,
2020-09-14 23:44:46 +00:00
array (
2020-11-05 14:00:04 +00:00
'id' => 'woocommerce-analytics-overview' ,
'title' => __ ( 'Overview' , 'woocommerce-admin' ),
'parent' => 'woocommerce-analytics' ,
'path' => '/analytics/overview' ,
'nav_args' => array (
'order' => 10 ,
'parent' => 'analytics' ,
),
2020-10-16 01:31:14 +00:00
),
array (
2020-11-05 14:00:04 +00:00
'id' => 'woocommerce-analytics-products' ,
'title' => __ ( 'Products' , 'woocommerce-admin' ),
'parent' => 'woocommerce-analytics' ,
'path' => '/analytics/products' ,
'nav_args' => array (
'order' => 20 ,
'parent' => 'analytics' ,
),
2020-09-14 23:44:46 +00:00
),
2019-05-07 18:43:18 +00:00
array (
2020-11-05 14:00:04 +00:00
'id' => 'woocommerce-analytics-revenue' ,
'title' => __ ( 'Revenue' , 'woocommerce-admin' ),
'parent' => 'woocommerce-analytics' ,
'path' => '/analytics/revenue' ,
'nav_args' => array (
'order' => 30 ,
'parent' => 'analytics' ,
),
2019-04-17 02:31:55 +00:00
),
2019-04-11 18:31:31 +00:00
array (
2020-11-05 14:00:04 +00:00
'id' => 'woocommerce-analytics-orders' ,
'title' => __ ( 'Orders' , 'woocommerce-admin' ),
'parent' => 'woocommerce-analytics' ,
'path' => '/analytics/orders' ,
'nav_args' => array (
'order' => 40 ,
'parent' => 'analytics' ,
),
2019-04-17 02:31:55 +00:00
),
2020-09-25 13:57:48 +00:00
array (
2020-11-05 14:00:04 +00:00
'id' => 'woocommerce-analytics-variations' ,
'title' => __ ( 'Variations' , 'woocommerce-admin' ),
'parent' => 'woocommerce-analytics' ,
'path' => '/analytics/variations' ,
'nav_args' => array (
'order' => 50 ,
'parent' => 'analytics' ,
),
2020-09-25 13:57:48 +00:00
),
2019-04-11 18:31:31 +00:00
array (
2020-11-05 14:00:04 +00:00
'id' => 'woocommerce-analytics-categories' ,
'title' => __ ( 'Categories' , 'woocommerce-admin' ),
'parent' => 'woocommerce-analytics' ,
'path' => '/analytics/categories' ,
'nav_args' => array (
'order' => 60 ,
'parent' => 'analytics' ,
),
2019-04-17 02:31:55 +00:00
),
2019-04-11 18:31:31 +00:00
array (
2020-11-05 14:00:04 +00:00
'id' => 'woocommerce-analytics-coupons' ,
'title' => __ ( 'Coupons' , 'woocommerce-admin' ),
'parent' => 'woocommerce-analytics' ,
'path' => '/analytics/coupons' ,
'nav_args' => array (
'order' => 70 ,
'parent' => 'analytics' ,
),
2019-04-17 02:31:55 +00:00
),
2019-04-11 18:31:31 +00:00
array (
2020-11-05 14:00:04 +00:00
'id' => 'woocommerce-analytics-taxes' ,
'title' => __ ( 'Taxes' , 'woocommerce-admin' ),
'parent' => 'woocommerce-analytics' ,
'path' => '/analytics/taxes' ,
'nav_args' => array (
'order' => 80 ,
'parent' => 'analytics' ,
),
2019-04-17 02:31:55 +00:00
),
2019-04-11 18:31:31 +00:00
array (
2020-11-05 14:00:04 +00:00
'id' => 'woocommerce-analytics-downloads' ,
'title' => __ ( 'Downloads' , 'woocommerce-admin' ),
'parent' => 'woocommerce-analytics' ,
'path' => '/analytics/downloads' ,
'nav_args' => array (
'order' => 90 ,
'parent' => 'analytics' ,
),
2019-04-17 02:31:55 +00:00
),
'yes' === get_option ( 'woocommerce_manage_stock' ) ? array (
2020-11-05 14:00:04 +00:00
'id' => 'woocommerce-analytics-stock' ,
'title' => __ ( 'Stock' , 'woocommerce-admin' ),
'parent' => 'woocommerce-analytics' ,
'path' => '/analytics/stock' ,
'nav_args' => array (
'order' => 100 ,
'parent' => 'analytics' ,
),
2019-04-17 02:31:55 +00:00
) : null ,
2019-04-11 18:31:31 +00:00
array (
2020-11-05 14:00:04 +00:00
'id' => 'woocommerce-analytics-customers' ,
'title' => __ ( 'Customers' , 'woocommerce-admin' ),
'parent' => 'woocommerce' ,
'path' => '/customers' ,
'nav_args' => array (
'is_top_level' => true ,
'order' => 50 ,
),
2019-04-17 02:31:55 +00:00
),
2019-04-11 18:31:31 +00:00
array (
2020-11-05 14:00:04 +00:00
'id' => 'woocommerce-analytics-settings' ,
'title' => __ ( 'Settings' , 'woocommerce-admin' ),
'parent' => 'woocommerce-analytics' ,
'path' => '/analytics/settings' ,
'nav_args' => array (
'title' => __ ( 'Analytics' , 'woocommerce-admin' ),
'parent' => 'settings' ,
),
2019-04-17 02:31:55 +00:00
),
2019-04-11 18:31:31 +00:00
);
2019-04-17 02:31:55 +00:00
2020-11-05 14:00:04 +00:00
return apply_filters ( 'woocommerce_analytics_report_menu_items' , $report_pages );
2019-04-11 18:31:31 +00:00
}
2020-06-16 13:47:58 +00:00
/**
* " Clear " analytics cache by invalidating it .
*/
public function run_clear_cache_tool () {
Cache :: invalidate ();
return __ ( 'Analytics cache cleared.' , 'woocommerce-admin' );
}
2019-04-11 18:31:31 +00:00
}