Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/woocomme.../src/Admin
File: ReportsSync.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* Report table sync related functions and actions.
[2] Fix | Delete
*/
[3] Fix | Delete
[4] Fix | Delete
namespace Automattic\WooCommerce\Admin;
[5] Fix | Delete
[6] Fix | Delete
defined( 'ABSPATH' ) || exit;
[7] Fix | Delete
[8] Fix | Delete
use Automattic\WooCommerce\Internal\Admin\Schedulers\CustomersScheduler;
[9] Fix | Delete
use Automattic\WooCommerce\Internal\Admin\Schedulers\OrdersScheduler;
[10] Fix | Delete
use Automattic\WooCommerce\Internal\Admin\Schedulers\ImportScheduler;
[11] Fix | Delete
[12] Fix | Delete
/**
[13] Fix | Delete
* ReportsSync Class.
[14] Fix | Delete
*/
[15] Fix | Delete
class ReportsSync {
[16] Fix | Delete
/**
[17] Fix | Delete
* Hook in sync methods.
[18] Fix | Delete
*/
[19] Fix | Delete
public static function init() {
[20] Fix | Delete
// Initialize scheduler hooks.
[21] Fix | Delete
foreach ( self::get_schedulers() as $scheduler ) {
[22] Fix | Delete
$scheduler::init();
[23] Fix | Delete
}
[24] Fix | Delete
add_action( 'woocommerce_update_product', array( __CLASS__, 'clear_stock_count_cache' ) );
[25] Fix | Delete
add_action( 'woocommerce_new_product', array( __CLASS__, 'clear_stock_count_cache' ) );
[26] Fix | Delete
add_action( 'update_option_woocommerce_notify_low_stock_amount', array( __CLASS__, 'clear_stock_count_cache' ) );
[27] Fix | Delete
add_action( 'update_option_woocommerce_notify_no_stock_amount', array( __CLASS__, 'clear_stock_count_cache' ) );
[28] Fix | Delete
add_action( 'trashed_post', array( __CLASS__, 'maybe_clear_stock_count_cache_for_post' ) );
[29] Fix | Delete
add_action( 'untrashed_post', array( __CLASS__, 'maybe_clear_stock_count_cache_for_post' ) );
[30] Fix | Delete
add_action( 'delete_post', array( __CLASS__, 'maybe_clear_stock_count_cache_for_post' ) );
[31] Fix | Delete
}
[32] Fix | Delete
[33] Fix | Delete
/**
[34] Fix | Delete
* Get classes for syncing data.
[35] Fix | Delete
*
[36] Fix | Delete
* @return array
[37] Fix | Delete
* @throws \Exception Throws exception when invalid data is found.
[38] Fix | Delete
*/
[39] Fix | Delete
public static function get_schedulers() {
[40] Fix | Delete
$schedulers = apply_filters(
[41] Fix | Delete
'woocommerce_analytics_report_schedulers',
[42] Fix | Delete
array(
[43] Fix | Delete
new CustomersScheduler(),
[44] Fix | Delete
new OrdersScheduler(),
[45] Fix | Delete
)
[46] Fix | Delete
);
[47] Fix | Delete
[48] Fix | Delete
foreach ( $schedulers as $scheduler ) {
[49] Fix | Delete
if ( ! is_subclass_of( $scheduler, 'Automattic\WooCommerce\Internal\Admin\Schedulers\ImportScheduler' ) ) {
[50] Fix | Delete
throw new \Exception( __( 'Report sync schedulers should be derived from the Automattic\WooCommerce\Internal\Admin\Schedulers\ImportScheduler class.', 'woocommerce' ) );
[51] Fix | Delete
}
[52] Fix | Delete
}
[53] Fix | Delete
[54] Fix | Delete
return $schedulers;
[55] Fix | Delete
}
[56] Fix | Delete
[57] Fix | Delete
/**
[58] Fix | Delete
* Returns true if an import is in progress.
[59] Fix | Delete
*
[60] Fix | Delete
* @return bool
[61] Fix | Delete
*/
[62] Fix | Delete
public static function is_importing() {
[63] Fix | Delete
foreach ( self::get_schedulers() as $scheduler ) {
[64] Fix | Delete
if ( $scheduler::is_importing() ) {
[65] Fix | Delete
return true;
[66] Fix | Delete
}
[67] Fix | Delete
}
[68] Fix | Delete
return false;
[69] Fix | Delete
}
[70] Fix | Delete
[71] Fix | Delete
/**
[72] Fix | Delete
* Regenerate data for reports.
[73] Fix | Delete
*
[74] Fix | Delete
* @param int|bool $days Number of days to import.
[75] Fix | Delete
* @param bool $skip_existing Skip existing records.
[76] Fix | Delete
* @return string
[77] Fix | Delete
*/
[78] Fix | Delete
public static function regenerate_report_data( $days, $skip_existing ) {
[79] Fix | Delete
if ( self::is_importing() ) {
[80] Fix | Delete
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' ) );
[81] Fix | Delete
}
[82] Fix | Delete
[83] Fix | Delete
self::reset_import_stats( $days, $skip_existing );
[84] Fix | Delete
foreach ( self::get_schedulers() as $scheduler ) {
[85] Fix | Delete
$scheduler::schedule_action( 'import_batch_init', array( $days, $skip_existing ) );
[86] Fix | Delete
}
[87] Fix | Delete
[88] Fix | Delete
/**
[89] Fix | Delete
* Fires when report data regeneration begins.
[90] Fix | Delete
*
[91] Fix | Delete
* @param int|bool $days Number of days to import.
[92] Fix | Delete
* @param bool $skip_existing Skip existing records.
[93] Fix | Delete
*/
[94] Fix | Delete
do_action( 'woocommerce_analytics_regenerate_init', $days, $skip_existing );
[95] Fix | Delete
[96] Fix | Delete
return __( 'Report table data is being rebuilt. Please allow some time for data to fully populate.', 'woocommerce' );
[97] Fix | Delete
}
[98] Fix | Delete
[99] Fix | Delete
/**
[100] Fix | Delete
* Update the import stat totals and counts.
[101] Fix | Delete
*
[102] Fix | Delete
* @param int|bool $days Number of days to import.
[103] Fix | Delete
* @param bool $skip_existing Skip existing records.
[104] Fix | Delete
*/
[105] Fix | Delete
public static function reset_import_stats( $days, $skip_existing ) {
[106] Fix | Delete
$import_stats = get_option( ImportScheduler::IMPORT_STATS_OPTION, array() );
[107] Fix | Delete
$totals = self::get_import_totals( $days, $skip_existing );
[108] Fix | Delete
[109] Fix | Delete
foreach ( self::get_schedulers() as $scheduler ) {
[110] Fix | Delete
$import_stats[ $scheduler::$name ]['imported'] = 0;
[111] Fix | Delete
$import_stats[ $scheduler::$name ]['total'] = $totals[ $scheduler::$name ];
[112] Fix | Delete
}
[113] Fix | Delete
[114] Fix | Delete
// Update imported from date if older than previous.
[115] Fix | Delete
$previous_import_date = isset( $import_stats['imported_from'] ) ? $import_stats['imported_from'] : null;
[116] Fix | Delete
$current_import_date = $days ? gmdate( 'Y-m-d 00:00:00', time() - ( DAY_IN_SECONDS * $days ) ) : -1;
[117] Fix | Delete
[118] Fix | Delete
if ( ! $previous_import_date || -1 === $current_import_date || new \DateTime( $previous_import_date ) > new \DateTime( $current_import_date ) ) {
[119] Fix | Delete
$import_stats['imported_from'] = $current_import_date;
[120] Fix | Delete
}
[121] Fix | Delete
[122] Fix | Delete
update_option( ImportScheduler::IMPORT_STATS_OPTION, $import_stats );
[123] Fix | Delete
}
[124] Fix | Delete
[125] Fix | Delete
/**
[126] Fix | Delete
* Get stats for current import.
[127] Fix | Delete
*
[128] Fix | Delete
* @return array
[129] Fix | Delete
*/
[130] Fix | Delete
public static function get_import_stats() {
[131] Fix | Delete
$import_stats = get_option( ImportScheduler::IMPORT_STATS_OPTION, array() );
[132] Fix | Delete
$import_stats['is_importing'] = self::is_importing();
[133] Fix | Delete
[134] Fix | Delete
return $import_stats;
[135] Fix | Delete
}
[136] Fix | Delete
[137] Fix | Delete
/**
[138] Fix | Delete
* Get the import totals for all syncs.
[139] Fix | Delete
*
[140] Fix | Delete
* @param int|bool $days Number of days to import.
[141] Fix | Delete
* @param bool $skip_existing Skip existing records.
[142] Fix | Delete
* @return array
[143] Fix | Delete
*/
[144] Fix | Delete
public static function get_import_totals( $days, $skip_existing ) {
[145] Fix | Delete
$totals = array();
[146] Fix | Delete
[147] Fix | Delete
foreach ( self::get_schedulers() as $scheduler ) {
[148] Fix | Delete
$items = $scheduler::get_items( 1, 1, $days, $skip_existing );
[149] Fix | Delete
$totals[ $scheduler::$name ] = $items->total;
[150] Fix | Delete
}
[151] Fix | Delete
[152] Fix | Delete
return $totals;
[153] Fix | Delete
}
[154] Fix | Delete
[155] Fix | Delete
/**
[156] Fix | Delete
* Clears all queued actions.
[157] Fix | Delete
*/
[158] Fix | Delete
public static function clear_queued_actions() {
[159] Fix | Delete
foreach ( self::get_schedulers() as $scheduler ) {
[160] Fix | Delete
$scheduler::clear_queued_actions();
[161] Fix | Delete
}
[162] Fix | Delete
}
[163] Fix | Delete
[164] Fix | Delete
/**
[165] Fix | Delete
* Delete all data for reports.
[166] Fix | Delete
*
[167] Fix | Delete
* @return string
[168] Fix | Delete
*/
[169] Fix | Delete
public static function delete_report_data() {
[170] Fix | Delete
// Cancel all pending import jobs.
[171] Fix | Delete
self::clear_queued_actions();
[172] Fix | Delete
[173] Fix | Delete
foreach ( self::get_schedulers() as $scheduler ) {
[174] Fix | Delete
$scheduler::schedule_action( 'delete_batch_init', array() );
[175] Fix | Delete
}
[176] Fix | Delete
[177] Fix | Delete
// Delete import options.
[178] Fix | Delete
delete_option( ImportScheduler::IMPORT_STATS_OPTION );
[179] Fix | Delete
[180] Fix | Delete
return __( 'Report table data is being deleted.', 'woocommerce' );
[181] Fix | Delete
}
[182] Fix | Delete
[183] Fix | Delete
/**
[184] Fix | Delete
* Clear the stock count cache for a post if it's a product or product variation.
[185] Fix | Delete
*
[186] Fix | Delete
* Handles trashed_post, untrashed_post, and delete_post hooks.
[187] Fix | Delete
*
[188] Fix | Delete
* @internal
[189] Fix | Delete
*
[190] Fix | Delete
* @param int $post_id The post ID.
[191] Fix | Delete
*/
[192] Fix | Delete
public static function maybe_clear_stock_count_cache_for_post( $post_id ): void {
[193] Fix | Delete
$post = get_post( $post_id );
[194] Fix | Delete
if ( $post && in_array( $post->post_type, array( 'product', 'product_variation' ), true ) ) {
[195] Fix | Delete
self::clear_stock_count_cache( $post_id );
[196] Fix | Delete
}
[197] Fix | Delete
}
[198] Fix | Delete
[199] Fix | Delete
/**
[200] Fix | Delete
* Clear the count cache when products are added or updated, or when
[201] Fix | Delete
* the no/low stock options are changed.
[202] Fix | Delete
*
[203] Fix | Delete
* @param int $id Post/product ID.
[204] Fix | Delete
*/
[205] Fix | Delete
public static function clear_stock_count_cache( $id ) {
[206] Fix | Delete
delete_transient( 'wc_admin_stock_count_lowstock' );
[207] Fix | Delete
delete_transient( 'wc_admin_product_count' );
[208] Fix | Delete
$status_options = wc_get_product_stock_status_options();
[209] Fix | Delete
foreach ( $status_options as $status => $label ) {
[210] Fix | Delete
delete_transient( 'wc_admin_stock_count_' . $status );
[211] Fix | Delete
}
[212] Fix | Delete
}
[213] Fix | Delete
}
[214] Fix | Delete
[215] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function