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 |