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