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3.2.1 6.3.0-beta.1 3.2.2 6.3.0-rc.1 3.2.3 6.3.0-rc.2 3.2.4 6.3.1 3.2.5 6.3.2 3.2.6 6.4.0 3.3.0 6.4.0-beta.1 3.3.1 6.4.0-rc.1 3.3.2 6.4.1 3.3.2-rc.1 6.4.2 3.3.3 6.5.0 3.3.4 6.5.0-beta.1 3.3.5 6.5.0-rc.1 3.3.6 6.5.0-rc.2 3.4.0 6.5.1 3.4.0-beta.1 6.5.2 3.4.0-rc.2 6.6.0 3.4.1 6.6.0-beta.1 3.4.2 6.6.0-rc.1 3.4.3 6.6.0-rc.2 3.4.4 6.6.1 3.4.5 6.6.2 3.4.6 6.7.0 3.4.7 6.7.0-beta.1 3.4.8 6.7.0-beta.2 3.5.0 6.7.0-rc.1 3.5.0-beta.1 6.7.1 3.5.0-rc.1 6.8.0 3.5.0-rc.2 6.8.0-beta.1 3.5.1 6.8.0-beta.2 3.5.10 6.8.0-rc.1 3.5.2 6.8.1 3.5.3 6.8.2 3.5.4 6.8.3 3.5.5 6.9.0 3.5.6 6.9.0-beta.1 3.5.7 6.9.0-beta.2 3.5.8 6.9.0-rc.1 3.5.9 6.9.1 3.6.0 6.9.2 3.6.0-beta.1 6.9.3 3.6.0-rc.1 6.9.4 3.6.0-rc.2 6.9.5 3.6.0-rc.3 7.0.0 3.6.1 7.0.0-beta.1 3.6.2 7.0.0-beta.2 3.6.3 7.0.0-beta.3 3.6.4 7.0.0-rc.1 3.6.5 7.0.0-rc.2 3.6.6 7.0.1 3.6.7 7.0.2 3.7.0 7.1.0 3.7.0-beta.1 7.1.0-beta.1 3.7.0-rc.1 7.1.0-beta.2 3.7.0-rc.2 7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / src / Admin / ReportsSync.php
woocommerce / src / Admin Last commit date
API 3 months ago BlockTemplates 2 years ago Composer 2 years ago DateTimeProvider 4 years ago Features 3 months ago Marketing 1 year ago Notes 4 months ago Overrides 9 months ago PluginsInstallLoggers 1 year ago PluginsProvider 4 years ago RemoteInboxNotifications 1 year ago RemoteSpecs 1 year ago Schedulers 1 year ago DataSourcePoller.php 2 years ago DeprecatedClassFacade.php 1 year ago FeaturePlugin.php 4 years ago Loader.php 2 years ago PageController.php 3 months ago PluginsHelper.php 11 months ago PluginsInstaller.php 4 years ago ReportCSVEmail.php 1 year ago ReportCSVExporter.php 1 year ago ReportExporter.php 3 years ago ReportsSync.php 3 months ago WCAdminHelper.php 1 year ago
ReportsSync.php
216 lines
1 <?php
2 /**
3 * Report table sync related functions and actions.
4 */
5
6 namespace Automattic\WooCommerce\Admin;
7
8 defined( 'ABSPATH' ) || exit;
9
10 use Automattic\WooCommerce\Internal\Admin\Schedulers\CustomersScheduler;
11 use Automattic\WooCommerce\Internal\Admin\Schedulers\OrdersScheduler;
12 use Automattic\WooCommerce\Internal\Admin\Schedulers\ImportScheduler;
13
14 /**
15 * ReportsSync Class.
16 */
17 class ReportsSync {
18 /**
19 * Hook in sync methods.
20 */
21 public static function init() {
22 // Initialize scheduler hooks.
23 foreach ( self::get_schedulers() as $scheduler ) {
24 $scheduler::init();
25 }
26 add_action( 'woocommerce_update_product', array( __CLASS__, 'clear_stock_count_cache' ) );
27 add_action( 'woocommerce_new_product', array( __CLASS__, 'clear_stock_count_cache' ) );
28 add_action( 'update_option_woocommerce_notify_low_stock_amount', array( __CLASS__, 'clear_stock_count_cache' ) );
29 add_action( 'update_option_woocommerce_notify_no_stock_amount', array( __CLASS__, 'clear_stock_count_cache' ) );
30 add_action( 'trashed_post', array( __CLASS__, 'maybe_clear_stock_count_cache_for_post' ) );
31 add_action( 'untrashed_post', array( __CLASS__, 'maybe_clear_stock_count_cache_for_post' ) );
32 add_action( 'delete_post', array( __CLASS__, 'maybe_clear_stock_count_cache_for_post' ) );
33 }
34
35 /**
36 * Get classes for syncing data.
37 *
38 * @return array
39 * @throws \Exception Throws exception when invalid data is found.
40 */
41 public static function get_schedulers() {
42 $schedulers = apply_filters(
43 'woocommerce_analytics_report_schedulers',
44 array(
45 new CustomersScheduler(),
46 new OrdersScheduler(),
47 )
48 );
49
50 foreach ( $schedulers as $scheduler ) {
51 if ( ! is_subclass_of( $scheduler, 'Automattic\WooCommerce\Internal\Admin\Schedulers\ImportScheduler' ) ) {
52 throw new \Exception( __( 'Report sync schedulers should be derived from the Automattic\WooCommerce\Internal\Admin\Schedulers\ImportScheduler class.', 'woocommerce' ) );
53 }
54 }
55
56 return $schedulers;
57 }
58
59 /**
60 * Returns true if an import is in progress.
61 *
62 * @return bool
63 */
64 public static function is_importing() {
65 foreach ( self::get_schedulers() as $scheduler ) {
66 if ( $scheduler::is_importing() ) {
67 return true;
68 }
69 }
70 return false;
71 }
72
73 /**
74 * Regenerate data for reports.
75 *
76 * @param int|bool $days Number of days to import.
77 * @param bool $skip_existing Skip existing records.
78 * @return string
79 */
80 public static function regenerate_report_data( $days, $skip_existing ) {
81 if ( self::is_importing() ) {
82 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' ) );
83 }
84
85 self::reset_import_stats( $days, $skip_existing );
86 foreach ( self::get_schedulers() as $scheduler ) {
87 $scheduler::schedule_action( 'import_batch_init', array( $days, $skip_existing ) );
88 }
89
90 /**
91 * Fires when report data regeneration begins.
92 *
93 * @param int|bool $days Number of days to import.
94 * @param bool $skip_existing Skip existing records.
95 */
96 do_action( 'woocommerce_analytics_regenerate_init', $days, $skip_existing );
97
98 return __( 'Report table data is being rebuilt. Please allow some time for data to fully populate.', 'woocommerce' );
99 }
100
101 /**
102 * Update the import stat totals and counts.
103 *
104 * @param int|bool $days Number of days to import.
105 * @param bool $skip_existing Skip existing records.
106 */
107 public static function reset_import_stats( $days, $skip_existing ) {
108 $import_stats = get_option( ImportScheduler::IMPORT_STATS_OPTION, array() );
109 $totals = self::get_import_totals( $days, $skip_existing );
110
111 foreach ( self::get_schedulers() as $scheduler ) {
112 $import_stats[ $scheduler::$name ]['imported'] = 0;
113 $import_stats[ $scheduler::$name ]['total'] = $totals[ $scheduler::$name ];
114 }
115
116 // Update imported from date if older than previous.
117 $previous_import_date = isset( $import_stats['imported_from'] ) ? $import_stats['imported_from'] : null;
118 $current_import_date = $days ? gmdate( 'Y-m-d 00:00:00', time() - ( DAY_IN_SECONDS * $days ) ) : -1;
119
120 if ( ! $previous_import_date || -1 === $current_import_date || new \DateTime( $previous_import_date ) > new \DateTime( $current_import_date ) ) {
121 $import_stats['imported_from'] = $current_import_date;
122 }
123
124 update_option( ImportScheduler::IMPORT_STATS_OPTION, $import_stats );
125 }
126
127 /**
128 * Get stats for current import.
129 *
130 * @return array
131 */
132 public static function get_import_stats() {
133 $import_stats = get_option( ImportScheduler::IMPORT_STATS_OPTION, array() );
134 $import_stats['is_importing'] = self::is_importing();
135
136 return $import_stats;
137 }
138
139 /**
140 * Get the import totals for all syncs.
141 *
142 * @param int|bool $days Number of days to import.
143 * @param bool $skip_existing Skip existing records.
144 * @return array
145 */
146 public static function get_import_totals( $days, $skip_existing ) {
147 $totals = array();
148
149 foreach ( self::get_schedulers() as $scheduler ) {
150 $items = $scheduler::get_items( 1, 1, $days, $skip_existing );
151 $totals[ $scheduler::$name ] = $items->total;
152 }
153
154 return $totals;
155 }
156
157 /**
158 * Clears all queued actions.
159 */
160 public static function clear_queued_actions() {
161 foreach ( self::get_schedulers() as $scheduler ) {
162 $scheduler::clear_queued_actions();
163 }
164 }
165
166 /**
167 * Delete all data for reports.
168 *
169 * @return string
170 */
171 public static function delete_report_data() {
172 // Cancel all pending import jobs.
173 self::clear_queued_actions();
174
175 foreach ( self::get_schedulers() as $scheduler ) {
176 $scheduler::schedule_action( 'delete_batch_init', array() );
177 }
178
179 // Delete import options.
180 delete_option( ImportScheduler::IMPORT_STATS_OPTION );
181
182 return __( 'Report table data is being deleted.', 'woocommerce' );
183 }
184
185 /**
186 * Clear the stock count cache for a post if it's a product or product variation.
187 *
188 * Handles trashed_post, untrashed_post, and delete_post hooks.
189 *
190 * @internal
191 *
192 * @param int $post_id The post ID.
193 */
194 public static function maybe_clear_stock_count_cache_for_post( $post_id ): void {
195 $post = get_post( $post_id );
196 if ( $post && in_array( $post->post_type, array( 'product', 'product_variation' ), true ) ) {
197 self::clear_stock_count_cache( $post_id );
198 }
199 }
200
201 /**
202 * Clear the count cache when products are added or updated, or when
203 * the no/low stock options are changed.
204 *
205 * @param int $id Post/product ID.
206 */
207 public static function clear_stock_count_cache( $id ) {
208 delete_transient( 'wc_admin_stock_count_lowstock' );
209 delete_transient( 'wc_admin_product_count' );
210 $status_options = wc_get_product_stock_status_options();
211 foreach ( $status_options as $status => $label ) {
212 delete_transient( 'wc_admin_stock_count_' . $status );
213 }
214 }
215 }
216