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2.0-rc.1 3.0.9 6.2.0-rc.2 3.1.0 6.2.1 3.1.1 6.2.2 3.1.2 6.2.3 3.2.0 6.3.0 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 4 days ago BlockTemplates 4 days ago Composer 2 years ago DateTimeProvider 4 years ago Features 4 days ago Marketing 1 year ago Notes 1 month ago Overrides 4 days ago PluginsInstallLoggers 1 year ago PluginsProvider 4 years ago RemoteInboxNotifications 1 month ago RemoteSpecs 2 months ago Schedulers 1 year ago Settings 4 days ago DataSourcePoller.php 2 years ago DeprecatedClassFacade.php 1 year ago FeaturePlugin.php 4 years ago Loader.php 2 years ago PageController.php 1 month ago PluginsHelper.php 4 days ago PluginsInstaller.php 4 years ago ReportCSVEmail.php 1 year ago ReportCSVExporter.php 1 year ago ReportExporter.php 3 years ago ReportsSync.php 4 days ago WCAdminHelper.php 4 days ago
ReportsSync.php
224 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 // A full (non-windowed) import covers the orders whose failed-import
87 // records were dropped due to the storage cap, so reset the overflow
88 // counter. Windowed imports may not reach those orders, so the counter
89 // is kept to keep the UI warning accurate.
90 if ( false === $days ) {
91 OrdersScheduler::reset_failed_order_imports_overflow();
92 }
93 foreach ( self::get_schedulers() as $scheduler ) {
94 $scheduler::schedule_action( 'import_batch_init', array( $days, $skip_existing ) );
95 }
96
97 /**
98 * Fires when report data regeneration begins.
99 *
100 * @param int|bool $days Number of days to import.
101 * @param bool $skip_existing Skip existing records.
102 */
103 do_action( 'woocommerce_analytics_regenerate_init', $days, $skip_existing );
104
105 return __( 'Report table data is being rebuilt. Please allow some time for data to fully populate.', 'woocommerce' );
106 }
107
108 /**
109 * Update the import stat totals and counts.
110 *
111 * @param int|bool $days Number of days to import.
112 * @param bool $skip_existing Skip existing records.
113 */
114 public static function reset_import_stats( $days, $skip_existing ) {
115 $import_stats = get_option( ImportScheduler::IMPORT_STATS_OPTION, array() );
116 $totals = self::get_import_totals( $days, $skip_existing );
117
118 foreach ( self::get_schedulers() as $scheduler ) {
119 $import_stats[ $scheduler::$name ]['imported'] = 0;
120 $import_stats[ $scheduler::$name ]['total'] = $totals[ $scheduler::$name ];
121 }
122
123 // Update imported from date if older than previous.
124 $previous_import_date = isset( $import_stats['imported_from'] ) ? $import_stats['imported_from'] : null;
125 $current_import_date = $days ? gmdate( 'Y-m-d 00:00:00', time() - ( DAY_IN_SECONDS * $days ) ) : -1;
126
127 if ( ! $previous_import_date || -1 === $current_import_date || new \DateTime( $previous_import_date ) > new \DateTime( $current_import_date ) ) {
128 $import_stats['imported_from'] = $current_import_date;
129 }
130
131 update_option( ImportScheduler::IMPORT_STATS_OPTION, $import_stats );
132 }
133
134 /**
135 * Get stats for current import.
136 *
137 * @return array
138 */
139 public static function get_import_stats() {
140 $import_stats = get_option( ImportScheduler::IMPORT_STATS_OPTION, array() );
141 $import_stats['is_importing'] = self::is_importing();
142
143 return $import_stats;
144 }
145
146 /**
147 * Get the import totals for all syncs.
148 *
149 * @param int|bool $days Number of days to import.
150 * @param bool $skip_existing Skip existing records.
151 * @return array
152 */
153 public static function get_import_totals( $days, $skip_existing ) {
154 $totals = array();
155
156 foreach ( self::get_schedulers() as $scheduler ) {
157 $items = $scheduler::get_items( 1, 1, $days, $skip_existing );
158 $totals[ $scheduler::$name ] = $items->total;
159 }
160
161 return $totals;
162 }
163
164 /**
165 * Clears all queued actions.
166 */
167 public static function clear_queued_actions() {
168 foreach ( self::get_schedulers() as $scheduler ) {
169 $scheduler::clear_queued_actions();
170 }
171 }
172
173 /**
174 * Delete all data for reports.
175 *
176 * @return string
177 */
178 public static function delete_report_data() {
179 // Cancel all pending import jobs.
180 self::clear_queued_actions();
181
182 foreach ( self::get_schedulers() as $scheduler ) {
183 $scheduler::schedule_action( 'delete_batch_init', array() );
184 }
185
186 // Delete import options.
187 delete_option( ImportScheduler::IMPORT_STATS_OPTION );
188 delete_option( OrdersScheduler::FAILED_ORDER_IMPORTS_OPTION );
189
190 return __( 'Report table data is being deleted.', 'woocommerce' );
191 }
192
193 /**
194 * Clear the stock count cache for a post if it's a product or product variation.
195 *
196 * Handles trashed_post, untrashed_post, and delete_post hooks.
197 *
198 * @internal
199 *
200 * @param int $post_id The post ID.
201 */
202 public static function maybe_clear_stock_count_cache_for_post( $post_id ): void {
203 $post = get_post( $post_id );
204 if ( $post && in_array( $post->post_type, array( 'product', 'product_variation' ), true ) ) {
205 self::clear_stock_count_cache( $post_id );
206 }
207 }
208
209 /**
210 * Clear the count cache when products are added or updated, or when
211 * the no/low stock options are changed.
212 *
213 * @param int $id Post/product ID.
214 */
215 public static function clear_stock_count_cache( $id ) {
216 delete_transient( 'wc_admin_stock_count_lowstock' );
217 delete_transient( 'wc_admin_product_count' );
218 $status_options = wc_get_product_stock_status_options();
219 foreach ( $status_options as $status => $label ) {
220 delete_transient( 'wc_admin_stock_count_' . $status );
221 }
222 }
223 }
224