Agentic
6 months ago
EmailImprovements
1 year ago
EmailPreview
1 month ago
Emails
2 weeks ago
ImportExport
1 year ago
Logging
2 weeks ago
Marketing
2 years ago
Notes
2 weeks ago
Onboarding
2 weeks ago
Orders
1 month ago
ProductForm
2 years ago
ProductReviews
2 months ago
RemoteFreeExtensions
2 months ago
Reports
2 weeks ago
Schedulers
2 weeks ago
Settings
2 weeks ago
Suggestions
2 weeks ago
WCPayPromotion
1 year ago
ActivityPanels.php
4 years ago
Analytics.php
2 weeks ago
CategoryLookup.php
4 years ago
Coupons.php
2 weeks ago
CouponsMovedTrait.php
7 months ago
CustomerEffortScoreTracks.php
9 months ago
Events.php
2 weeks ago
FeaturePlugin.php
2 weeks ago
Homescreen.php
2 weeks ago
Loader.php
1 month ago
Marketing.php
1 year ago
Marketplace.php
7 months ago
MobileAppBanner.php
4 years ago
OrderMilestoneEasterEgg.php
2 months ago
RemoteInboxNotifications.php
2 weeks ago
Settings.php
2 weeks ago
ShippingLabelBanner.php
1 year ago
ShippingLabelBannerDisplayRules.php
1 year ago
SiteHealth.php
2 weeks ago
Survey.php
4 years ago
SystemStatusReport.php
2 weeks ago
TaxSettingsRecommendations.php
1 month ago
Translations.php
1 year ago
WCAdminAssets.php
1 week ago
WCAdminSharedSettings.php
1 year ago
WCAdminUser.php
10 months ago
WcPayWelcomePage.php
1 year ago
Events.php
263 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Handle cron events. |
| 4 | */ |
| 5 | |
| 6 | namespace Automattic\WooCommerce\Internal\Admin; |
| 7 | |
| 8 | defined( 'ABSPATH' ) || exit; |
| 9 | |
| 10 | use Automattic\WooCommerce\Admin\RemoteInboxNotifications\RemoteInboxNotificationsEngine; |
| 11 | use Automattic\WooCommerce\Internal\Admin\Notes\CustomizeStoreWithBlocks; |
| 12 | use Automattic\WooCommerce\Internal\Admin\Notes\CustomizingProductCatalog; |
| 13 | use Automattic\WooCommerce\Internal\Admin\Notes\EditProductsOnTheMove; |
| 14 | use Automattic\WooCommerce\Internal\Admin\Notes\EmailImprovements; |
| 15 | use Automattic\WooCommerce\Internal\Admin\Notes\EUVATNumber; |
| 16 | use Automattic\WooCommerce\Internal\Admin\Notes\FirstProduct; |
| 17 | use Automattic\WooCommerce\Internal\Admin\Notes\InstallJPAndWCSPlugins; |
| 18 | use Automattic\WooCommerce\Internal\Admin\Notes\LaunchChecklist; |
| 19 | use Automattic\WooCommerce\Internal\Admin\Notes\MagentoMigration; |
| 20 | use Automattic\WooCommerce\Internal\Admin\Notes\ManageOrdersOnTheGo; |
| 21 | use Automattic\WooCommerce\Internal\Admin\Notes\MarketingJetpack; |
| 22 | use Automattic\WooCommerce\Internal\Admin\Notes\MigrateFromShopify; |
| 23 | use Automattic\WooCommerce\Internal\Admin\Notes\MobileApp; |
| 24 | use Automattic\WooCommerce\Internal\Admin\Notes\NewSalesRecord; |
| 25 | use Automattic\WooCommerce\Internal\Admin\Notes\OnboardingPayments; |
| 26 | use Automattic\WooCommerce\Internal\Admin\Notes\OnlineClothingStore; |
| 27 | use Automattic\WooCommerce\Internal\Admin\Notes\OrderMilestones; |
| 28 | use Automattic\WooCommerce\Internal\Admin\Notes\PaymentsMoreInfoNeeded; |
| 29 | use Automattic\WooCommerce\Internal\Admin\Notes\PaymentsRemindMeLater; |
| 30 | use Automattic\WooCommerce\Internal\Admin\Notes\PerformanceOnMobile; |
| 31 | use Automattic\WooCommerce\Internal\Admin\Notes\PersonalizeStore; |
| 32 | use Automattic\WooCommerce\Internal\Admin\Notes\RealTimeOrderAlerts; |
| 33 | use Automattic\WooCommerce\Internal\Admin\Notes\ScheduledUpdatesPromotion; |
| 34 | use Automattic\WooCommerce\Internal\Admin\Notes\SellingOnlineCourses; |
| 35 | use Automattic\WooCommerce\Internal\Admin\Notes\TrackingOptIn; |
| 36 | use Automattic\WooCommerce\Internal\Admin\Notes\UnsecuredReportFiles; |
| 37 | use Automattic\WooCommerce\Internal\Admin\Notes\WooCommercePayments; |
| 38 | use Automattic\WooCommerce\Internal\Admin\Notes\WooCommerceSubscriptions; |
| 39 | use Automattic\WooCommerce\Internal\Admin\Notes\WooSubscriptionsNotes; |
| 40 | use Automattic\WooCommerce\Internal\Admin\Schedulers\MailchimpScheduler; |
| 41 | use Automattic\WooCommerce\Admin\Notes\Note; |
| 42 | use Automattic\WooCommerce\Admin\Features\PaymentGatewaySuggestions\PaymentGatewaySuggestionsDataSourcePoller; |
| 43 | use Automattic\WooCommerce\Internal\Admin\RemoteFreeExtensions\RemoteFreeExtensionsDataSourcePoller; |
| 44 | |
| 45 | /** |
| 46 | * Events Class. |
| 47 | */ |
| 48 | class Events { |
| 49 | /** |
| 50 | * The single instance of the class. |
| 51 | * |
| 52 | * @var object |
| 53 | */ |
| 54 | protected static $instance = null; |
| 55 | |
| 56 | /** |
| 57 | * Constructor |
| 58 | * |
| 59 | * @return void |
| 60 | */ |
| 61 | protected function __construct() {} |
| 62 | |
| 63 | /** |
| 64 | * Array of note class to be added or updated. |
| 65 | * |
| 66 | * @var array |
| 67 | */ |
| 68 | private static $note_classes_to_added_or_updated = array( |
| 69 | CustomizeStoreWithBlocks::class, |
| 70 | CustomizingProductCatalog::class, |
| 71 | EditProductsOnTheMove::class, |
| 72 | EmailImprovements::class, |
| 73 | EUVATNumber::class, |
| 74 | FirstProduct::class, |
| 75 | LaunchChecklist::class, |
| 76 | MagentoMigration::class, |
| 77 | ManageOrdersOnTheGo::class, |
| 78 | MarketingJetpack::class, |
| 79 | MigrateFromShopify::class, |
| 80 | MobileApp::class, |
| 81 | NewSalesRecord::class, |
| 82 | OnboardingPayments::class, |
| 83 | OnlineClothingStore::class, |
| 84 | PaymentsMoreInfoNeeded::class, |
| 85 | PaymentsRemindMeLater::class, |
| 86 | PerformanceOnMobile::class, |
| 87 | PersonalizeStore::class, |
| 88 | RealTimeOrderAlerts::class, |
| 89 | ScheduledUpdatesPromotion::class, |
| 90 | TrackingOptIn::class, |
| 91 | WooCommercePayments::class, |
| 92 | WooCommerceSubscriptions::class, |
| 93 | ); |
| 94 | |
| 95 | /** |
| 96 | * The other note classes that are added in other places. |
| 97 | * |
| 98 | * @var array |
| 99 | */ |
| 100 | private static $other_note_classes = array( |
| 101 | InstallJPAndWCSPlugins::class, |
| 102 | OrderMilestones::class, |
| 103 | SellingOnlineCourses::class, |
| 104 | UnsecuredReportFiles::class, |
| 105 | WooSubscriptionsNotes::class, |
| 106 | ); |
| 107 | |
| 108 | |
| 109 | /** |
| 110 | * Get class instance. |
| 111 | * |
| 112 | * @return object Instance. |
| 113 | */ |
| 114 | final public static function instance() { |
| 115 | if ( null === static::$instance ) { |
| 116 | static::$instance = new static(); |
| 117 | } |
| 118 | return static::$instance; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Cron event handlers. |
| 123 | */ |
| 124 | public function init() { |
| 125 | add_action( 'wc_admin_daily', array( $this, 'do_wc_admin_daily' ) ); |
| 126 | add_filter( 'woocommerce_get_note_from_db', array( $this, 'get_note_from_db' ), 10, 1 ); |
| 127 | |
| 128 | // Initialize the WC_Notes_Refund_Returns Note to attach hook. |
| 129 | \WC_Notes_Refund_Returns::init(); |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Daily events to run. |
| 134 | * |
| 135 | * Note: Order_Milestones::possibly_add_note is hooked to this as well. |
| 136 | */ |
| 137 | public function do_wc_admin_daily() { |
| 138 | $this->possibly_add_notes(); |
| 139 | $this->possibly_delete_notes(); |
| 140 | $this->possibly_update_notes(); |
| 141 | $this->possibly_refresh_data_source_pollers(); |
| 142 | |
| 143 | if ( $this->is_remote_inbox_notifications_enabled() ) { |
| 144 | RemoteInboxNotificationsEngine::run(); |
| 145 | } |
| 146 | |
| 147 | ( new MailchimpScheduler() )->run(); |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Get note. |
| 152 | * |
| 153 | * @param Note $note_from_db The note object from the database. |
| 154 | */ |
| 155 | public function get_note_from_db( $note_from_db ) { |
| 156 | if ( ! $note_from_db instanceof Note || get_user_locale() === $note_from_db->get_locale() ) { |
| 157 | return $note_from_db; |
| 158 | } |
| 159 | |
| 160 | $note_classes = array_merge( self::$note_classes_to_added_or_updated, self::$other_note_classes ); |
| 161 | foreach ( $note_classes as $note_class ) { |
| 162 | if ( defined( "$note_class::NOTE_NAME" ) && $note_class::NOTE_NAME === $note_from_db->get_name() ) { |
| 163 | $note_from_class = method_exists( $note_class, 'get_note' ) ? $note_class::get_note() : null; |
| 164 | |
| 165 | if ( $note_from_class instanceof Note ) { |
| 166 | $note = clone $note_from_db; |
| 167 | $note->set_title( $note_from_class->get_title() ); |
| 168 | $note->set_content( $note_from_class->get_content() ); |
| 169 | $actions = $note_from_class->get_actions(); |
| 170 | foreach ( $actions as $action ) { |
| 171 | $matching_action = $note->get_action( $action->name ); |
| 172 | if ( $matching_action && $matching_action->id ) { |
| 173 | $action->id = $matching_action->id; |
| 174 | } |
| 175 | } |
| 176 | $note->set_actions( $actions ); |
| 177 | return $note; |
| 178 | } |
| 179 | break; |
| 180 | } |
| 181 | } |
| 182 | return $note_from_db; |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Adds notes that should be added. |
| 187 | */ |
| 188 | protected function possibly_add_notes() { |
| 189 | foreach ( self::$note_classes_to_added_or_updated as $note_class ) { |
| 190 | if ( method_exists( $note_class, 'possibly_add_note' ) ) { |
| 191 | $note_class::possibly_add_note(); |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Deletes notes that should be deleted. |
| 198 | */ |
| 199 | protected function possibly_delete_notes() { |
| 200 | PaymentsRemindMeLater::delete_if_not_applicable(); |
| 201 | PaymentsMoreInfoNeeded::delete_if_not_applicable(); |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Updates notes that should be updated. |
| 206 | */ |
| 207 | protected function possibly_update_notes() { |
| 208 | foreach ( self::$note_classes_to_added_or_updated as $note_class ) { |
| 209 | if ( method_exists( $note_class, 'possibly_update_note' ) ) { |
| 210 | $note_class::possibly_update_note(); |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * Checks if remote inbox notifications are enabled. |
| 217 | * |
| 218 | * @return bool Whether remote inbox notifications are enabled. |
| 219 | */ |
| 220 | protected function is_remote_inbox_notifications_enabled() { |
| 221 | // Check if the site has opted out of marketplace suggestions. |
| 222 | if ( get_option( 'woocommerce_show_marketplace_suggestions', 'yes' ) !== 'yes' ) { |
| 223 | return false; |
| 224 | } |
| 225 | |
| 226 | // All checks have passed. |
| 227 | return true; |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Checks if merchant email notifications are enabled. |
| 232 | * |
| 233 | * @return bool Whether merchant email notifications are enabled. |
| 234 | */ |
| 235 | protected function is_merchant_email_notifications_enabled() { |
| 236 | // Check if the feature flag is disabled. |
| 237 | if ( get_option( 'woocommerce_merchant_email_notifications', 'no' ) !== 'yes' ) { |
| 238 | return false; |
| 239 | } |
| 240 | |
| 241 | // All checks have passed. |
| 242 | return true; |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * Prime or fetch specs for the following DataSourcePollers on the wc_admin_daily cron job |
| 247 | * when their related transients are missing or expired: |
| 248 | * - PaymentGatewaySuggestionsDataSourcePoller |
| 249 | * - RemoteFreeExtensionsDataSourcePoller |
| 250 | */ |
| 251 | protected function possibly_refresh_data_source_pollers() { |
| 252 | $completed_tasks = get_option( 'woocommerce_task_list_tracked_completed_tasks', array() ); |
| 253 | |
| 254 | if ( ! in_array( 'payments', $completed_tasks, true ) && ! in_array( 'woocommerce-payments', $completed_tasks, true ) ) { |
| 255 | PaymentGatewaySuggestionsDataSourcePoller::get_instance()->get_specs_from_data_sources(); |
| 256 | } |
| 257 | |
| 258 | if ( ! in_array( 'store_details', $completed_tasks, true ) && ! in_array( 'marketing', $completed_tasks, true ) ) { |
| 259 | RemoteFreeExtensionsDataSourcePoller::get_instance()->get_specs_from_data_sources(); |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 |