2019-02-08 20:52:38 +00:00
< ? php
/**
* Report table sync related functions and actions .
*/
2019-08-01 22:06:00 +00:00
namespace Automattic\WooCommerce\Admin ;
2019-02-08 20:52:38 +00:00
2019-08-01 22:06:00 +00:00
defined ( 'ABSPATH' ) || exit ;
2019-07-31 17:24:01 +00:00
2020-01-02 16:00:37 +00:00
use Automattic\WooCommerce\Admin\Schedulers\CustomersScheduler ;
use Automattic\WooCommerce\Admin\Schedulers\OrdersScheduler ;
2020-01-17 00:08:29 +00:00
use Automattic\WooCommerce\Admin\Schedulers\ImportScheduler ;
2019-08-05 18:14:25 +00:00
2019-02-08 20:52:38 +00:00
/**
2019-08-12 21:52:09 +00:00
* ReportsSync Class .
2019-02-08 20:52:38 +00:00
*/
2019-08-12 21:52:09 +00:00
class ReportsSync {
2019-02-08 20:52:38 +00:00
/**
2020-01-02 16:00:37 +00:00
* Hook in sync methods .
2019-02-21 19:44:35 +00:00
*/
2020-01-02 16:00:37 +00:00
public static function init () {
// Initialize scheduler hooks.
foreach ( self :: get_schedulers () as $scheduler ) {
$scheduler :: init ();
}
add_action ( 'woocommerce_update_product' , array ( __CLASS__ , 'clear_stock_count_cache' ) );
add_action ( 'woocommerce_new_product' , array ( __CLASS__ , 'clear_stock_count_cache' ) );
add_action ( 'update_option_woocommerce_notify_low_stock_amount' , array ( __CLASS__ , 'clear_stock_count_cache' ) );
add_action ( 'update_option_woocommerce_notify_no_stock_amount' , array ( __CLASS__ , 'clear_stock_count_cache' ) );
}
2019-02-21 19:44:35 +00:00
2019-02-08 20:52:38 +00:00
/**
2020-01-02 16:00:37 +00:00
* Get classes for syncing data .
2019-02-08 20:52:38 +00:00
*
2020-01-02 16:00:37 +00:00
* @ return array
* @ throws \Exception Throws exception when invalid data is found .
2019-02-08 20:52:38 +00:00
*/
2020-01-02 16:00:37 +00:00
public static function get_schedulers () {
$schedulers = apply_filters (
'woocommerce_analytics_report_schedulers' ,
array (
new CustomersScheduler (),
new OrdersScheduler (),
)
);
2019-02-08 20:52:38 +00:00
2020-01-02 16:00:37 +00:00
foreach ( $schedulers as $scheduler ) {
if ( ! is_subclass_of ( $scheduler , 'Automattic\WooCommerce\Admin\Schedulers\ImportScheduler' ) ) {
throw new \Exception ( __ ( 'Report sync schedulers should be derived from the Automattic\WooCommerce\Admin\Schedulers\ImportScheduler class.' , 'woocommerce-admin' ) );
}
2019-02-08 20:52:38 +00:00
}
2020-01-02 16:00:37 +00:00
return $schedulers ;
2019-02-08 20:52:38 +00:00
}
/**
2020-01-02 16:00:37 +00:00
* Returns true if an import is in progress .
2019-02-08 20:52:38 +00:00
*
2020-01-02 16:00:37 +00:00
* @ return bool
2019-02-08 20:52:38 +00:00
*/
2020-01-02 16:00:37 +00:00
public static function is_importing () {
foreach ( self :: get_schedulers () as $scheduler ) {
if ( $scheduler :: is_importing () ) {
return true ;
}
}
return false ;
2019-02-08 20:52:38 +00:00
}
/**
* Regenerate data for reports .
2019-04-10 09:51:12 +00:00
*
2019-04-15 08:07:43 +00:00
* @ param int | bool $days Number of days to import .
2019-04-10 09:51:12 +00:00
* @ param bool $skip_existing Skip exisiting records .
* @ return string
2019-02-08 20:52:38 +00:00
*/
2019-04-10 09:51:12 +00:00
public static function regenerate_report_data ( $days , $skip_existing ) {
2019-06-13 15:32:58 +00:00
if ( self :: is_importing () ) {
2019-08-01 22:06:00 +00:00
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' ) );
2019-06-13 15:32:58 +00:00
}
2019-05-13 02:30:07 +00:00
self :: reset_import_stats ( $days , $skip_existing );
2020-01-02 16:00:37 +00:00
foreach ( self :: get_schedulers () as $scheduler ) {
$scheduler :: schedule_action ( 'import_batch_init' , array ( $days , $skip_existing ) );
}
2019-03-06 05:32:05 +00:00
2020-01-09 02:11:39 +00:00
/**
* Fires when report data regeneration begins .
*
* @ param int | bool $days Number of days to import .
* @ param bool $skip_existing Skip exisiting records .
*/
do_action ( 'woocommerce_analytics_regenerate_init' , $days , $skip_existing );
2019-03-13 17:14:02 +00:00
return __ ( 'Report table data is being rebuilt. Please allow some time for data to fully populate.' , 'woocommerce-admin' );
2019-02-08 20:52:38 +00:00
}
2019-05-13 02:30:07 +00:00
/**
* Update the import stat totals and counts .
*
* @ param int | bool $days Number of days to import .
* @ param bool $skip_existing Skip exisiting records .
*/
public static function reset_import_stats ( $days , $skip_existing ) {
2020-01-17 00:08:29 +00:00
$import_stats = get_option ( ImportScheduler :: IMPORT_STATS_OPTION , array () );
2020-01-02 16:00:37 +00:00
$totals = self :: get_import_totals ( $days , $skip_existing );
foreach ( self :: get_schedulers () as $scheduler ) {
$import_stats [ $scheduler :: $name ][ 'imported' ] = 0 ;
$import_stats [ $scheduler :: $name ][ 'total' ] = $totals [ $scheduler :: $name ];
}
2019-05-13 02:30:07 +00:00
// Update imported from date if older than previous.
2020-01-02 16:00:37 +00:00
$previous_import_date = isset ( $import_stats [ 'imported_from' ] ) ? $import_stats [ 'imported_from' ] : null ;
$current_import_date = $days ? gmdate ( 'Y-m-d 00:00:00' , time () - ( DAY_IN_SECONDS * $days ) ) : - 1 ;
2019-05-13 02:30:07 +00:00
2019-08-01 22:06:00 +00:00
if ( ! $previous_import_date || - 1 === $current_import_date || new \DateTime ( $previous_import_date ) > new \DateTime ( $current_import_date ) ) {
2020-01-02 16:00:37 +00:00
$import_stats [ 'imported_from' ] = $current_import_date ;
2019-05-13 02:30:07 +00:00
}
2020-01-02 16:00:37 +00:00
2020-01-17 00:08:29 +00:00
update_option ( ImportScheduler :: IMPORT_STATS_OPTION , $import_stats );
2019-05-13 02:30:07 +00:00
}
/**
2020-01-02 16:00:37 +00:00
* Get stats for current import .
2019-05-13 02:30:07 +00:00
*
* @ return array
*/
2020-01-02 16:00:37 +00:00
public static function get_import_stats () {
2020-01-17 00:08:29 +00:00
$import_stats = get_option ( ImportScheduler :: IMPORT_STATS_OPTION , array () );
2020-01-02 16:00:37 +00:00
$import_stats [ 'is_importing' ] = self :: is_importing ();
2019-05-13 02:30:07 +00:00
2020-01-02 16:00:37 +00:00
return $import_stats ;
2019-05-13 02:30:07 +00:00
}
/**
2020-01-02 16:00:37 +00:00
* Get the import totals for all syncs .
2019-05-13 02:30:07 +00:00
*
2020-01-02 16:00:37 +00:00
* @ param int | bool $days Number of days to import .
* @ param bool $skip_existing Skip exisiting records .
* @ return array
2019-05-13 02:30:07 +00:00
*/
2020-01-02 16:00:37 +00:00
public static function get_import_totals ( $days , $skip_existing ) {
$totals = array ();
foreach ( self :: get_schedulers () as $scheduler ) {
$items = $scheduler :: get_items ( 1 , 1 , $days , $skip_existing );
$totals [ $scheduler :: $name ] = $items -> total ;
}
2019-05-13 02:30:07 +00:00
2020-01-02 16:00:37 +00:00
return $totals ;
2019-05-13 02:30:07 +00:00
}
2019-02-28 21:47:58 +00:00
/**
* Clears all queued actions .
*/
public static function clear_queued_actions () {
2020-01-02 16:00:37 +00:00
foreach ( self :: get_schedulers () as $scheduler ) {
$scheduler :: clear_queued_actions ();
2019-02-28 21:47:58 +00:00
}
}
2019-02-08 20:52:38 +00:00
/**
2019-04-15 08:07:43 +00:00
* Delete all data for reports .
*
* @ return string
*/
public static function delete_report_data () {
// Cancel all pending import jobs.
self :: clear_queued_actions ();
2020-01-02 16:00:37 +00:00
foreach ( self :: get_schedulers () as $scheduler ) {
$scheduler :: schedule_action ( 'delete_batch_init' , array () );
}
2019-04-15 08:07:43 +00:00
2019-06-11 12:47:53 +00:00
// Delete import options.
2020-01-17 00:08:29 +00:00
delete_option ( ImportScheduler :: IMPORT_STATS_OPTION );
2019-06-11 12:47:53 +00:00
2019-04-15 08:07:43 +00:00
return __ ( 'Report table data is being deleted.' , 'woocommerce-admin' );
}
2019-11-12 15:34:07 +00:00
/**
* Clear the count cache when products are added or updated , or when
* the no / low stock options are changed .
*
* @ param int $id Post / product ID .
*/
public static function clear_stock_count_cache ( $id ) {
delete_transient ( 'wc_admin_stock_count_lowstock' );
delete_transient ( 'wc_admin_product_count' );
$status_options = wc_get_product_stock_status_options ();
foreach ( $status_options as $status => $label ) {
delete_transient ( 'wc_admin_stock_count_' . $status );
}
}
2019-02-08 20:52:38 +00:00
}