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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 2 years ago BlockTemplates 2 years ago Composer 2 years ago DateTimeProvider 4 years ago Features 2 years ago Marketing 2 years ago Notes 3 years ago Overrides 3 years ago PluginsInstallLoggers 2 years ago PluginsProvider 4 years ago RemoteInboxNotifications 2 years ago Schedulers 4 years ago DataSourcePoller.php 3 years ago DeprecatedClassFacade.php 2 years ago FeaturePlugin.php 4 years ago Loader.php 4 years ago PageController.php 3 years ago PluginsHelper.php 2 years ago PluginsInstaller.php 4 years ago ReportCSVEmail.php 2 years ago ReportCSVExporter.php 3 years ago ReportExporter.php 3 years ago ReportsSync.php 3 years ago WCAdminHelper.php 4 years ago
ReportsSync.php
197 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 }
31
32 /**
33 * Get classes for syncing data.
34 *
35 * @return array
36 * @throws \Exception Throws exception when invalid data is found.
37 */
38 public static function get_schedulers() {
39 $schedulers = apply_filters(
40 'woocommerce_analytics_report_schedulers',
41 array(
42 new CustomersScheduler(),
43 new OrdersScheduler(),
44 )
45 );
46
47 foreach ( $schedulers as $scheduler ) {
48 if ( ! is_subclass_of( $scheduler, 'Automattic\WooCommerce\Internal\Admin\Schedulers\ImportScheduler' ) ) {
49 throw new \Exception( __( 'Report sync schedulers should be derived from the Automattic\WooCommerce\Internal\Admin\Schedulers\ImportScheduler class.', 'woocommerce' ) );
50 }
51 }
52
53 return $schedulers;
54 }
55
56 /**
57 * Returns true if an import is in progress.
58 *
59 * @return bool
60 */
61 public static function is_importing() {
62 foreach ( self::get_schedulers() as $scheduler ) {
63 if ( $scheduler::is_importing() ) {
64 return true;
65 }
66 }
67 return false;
68 }
69
70 /**
71 * Regenerate data for reports.
72 *
73 * @param int|bool $days Number of days to import.
74 * @param bool $skip_existing Skip existing records.
75 * @return string
76 */
77 public static function regenerate_report_data( $days, $skip_existing ) {
78 if ( self::is_importing() ) {
79 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' ) );
80 }
81
82 self::reset_import_stats( $days, $skip_existing );
83 foreach ( self::get_schedulers() as $scheduler ) {
84 $scheduler::schedule_action( 'import_batch_init', array( $days, $skip_existing ) );
85 }
86
87 /**
88 * Fires when report data regeneration begins.
89 *
90 * @param int|bool $days Number of days to import.
91 * @param bool $skip_existing Skip existing records.
92 */
93 do_action( 'woocommerce_analytics_regenerate_init', $days, $skip_existing );
94
95 return __( 'Report table data is being rebuilt. Please allow some time for data to fully populate.', 'woocommerce' );
96 }
97
98 /**
99 * Update the import stat totals and counts.
100 *
101 * @param int|bool $days Number of days to import.
102 * @param bool $skip_existing Skip existing records.
103 */
104 public static function reset_import_stats( $days, $skip_existing ) {
105 $import_stats = get_option( ImportScheduler::IMPORT_STATS_OPTION, array() );
106 $totals = self::get_import_totals( $days, $skip_existing );
107
108 foreach ( self::get_schedulers() as $scheduler ) {
109 $import_stats[ $scheduler::$name ]['imported'] = 0;
110 $import_stats[ $scheduler::$name ]['total'] = $totals[ $scheduler::$name ];
111 }
112
113 // Update imported from date if older than previous.
114 $previous_import_date = isset( $import_stats['imported_from'] ) ? $import_stats['imported_from'] : null;
115 $current_import_date = $days ? gmdate( 'Y-m-d 00:00:00', time() - ( DAY_IN_SECONDS * $days ) ) : -1;
116
117 if ( ! $previous_import_date || -1 === $current_import_date || new \DateTime( $previous_import_date ) > new \DateTime( $current_import_date ) ) {
118 $import_stats['imported_from'] = $current_import_date;
119 }
120
121 update_option( ImportScheduler::IMPORT_STATS_OPTION, $import_stats );
122 }
123
124 /**
125 * Get stats for current import.
126 *
127 * @return array
128 */
129 public static function get_import_stats() {
130 $import_stats = get_option( ImportScheduler::IMPORT_STATS_OPTION, array() );
131 $import_stats['is_importing'] = self::is_importing();
132
133 return $import_stats;
134 }
135
136 /**
137 * Get the import totals for all syncs.
138 *
139 * @param int|bool $days Number of days to import.
140 * @param bool $skip_existing Skip existing records.
141 * @return array
142 */
143 public static function get_import_totals( $days, $skip_existing ) {
144 $totals = array();
145
146 foreach ( self::get_schedulers() as $scheduler ) {
147 $items = $scheduler::get_items( 1, 1, $days, $skip_existing );
148 $totals[ $scheduler::$name ] = $items->total;
149 }
150
151 return $totals;
152 }
153
154 /**
155 * Clears all queued actions.
156 */
157 public static function clear_queued_actions() {
158 foreach ( self::get_schedulers() as $scheduler ) {
159 $scheduler::clear_queued_actions();
160 }
161 }
162
163 /**
164 * Delete all data for reports.
165 *
166 * @return string
167 */
168 public static function delete_report_data() {
169 // Cancel all pending import jobs.
170 self::clear_queued_actions();
171
172 foreach ( self::get_schedulers() as $scheduler ) {
173 $scheduler::schedule_action( 'delete_batch_init', array() );
174 }
175
176 // Delete import options.
177 delete_option( ImportScheduler::IMPORT_STATS_OPTION );
178
179 return __( 'Report table data is being deleted.', 'woocommerce' );
180 }
181
182 /**
183 * Clear the count cache when products are added or updated, or when
184 * the no/low stock options are changed.
185 *
186 * @param int $id Post/product ID.
187 */
188 public static function clear_stock_count_cache( $id ) {
189 delete_transient( 'wc_admin_stock_count_lowstock' );
190 delete_transient( 'wc_admin_product_count' );
191 $status_options = wc_get_product_stock_status_options();
192 foreach ( $status_options as $status => $label ) {
193 delete_transient( 'wc_admin_stock_count_' . $status );
194 }
195 }
196 }
197