Controllers
4 weeks ago
DataStores
4 weeks ago
Dispatchers
2 months ago
Entities
3 months ago
Exceptions
4 months ago
Notifications
4 weeks ago
Services
4 weeks ago
Traits
4 weeks ago
Triggers
4 weeks ago
Validators
3 months ago
PushNotifications.php
5 days ago
PushNotifications.php
182 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Automattic\WooCommerce\Internal\PushNotifications; |
| 6 | |
| 7 | defined( 'ABSPATH' ) || exit; |
| 8 | |
| 9 | use Automattic\Jetpack\Connection\Manager as JetpackConnectionManager; |
| 10 | use Automattic\WooCommerce\Internal\PushNotifications\Controllers\NotificationPreferencesRestController; |
| 11 | use Automattic\WooCommerce\Internal\PushNotifications\Controllers\PushNotificationRestController; |
| 12 | use Automattic\WooCommerce\Internal\PushNotifications\Controllers\PushTokenRestController; |
| 13 | use Automattic\WooCommerce\Internal\PushNotifications\Entities\PushToken; |
| 14 | use Automattic\WooCommerce\Internal\PushNotifications\Services\NotificationProcessor; |
| 15 | use Automattic\WooCommerce\Internal\PushNotifications\Services\NotificationRetryHandler; |
| 16 | use Automattic\WooCommerce\Internal\PushNotifications\Services\PendingNotificationStore; |
| 17 | use Automattic\WooCommerce\Internal\PushNotifications\Triggers\NewOrderNotificationTrigger; |
| 18 | use Automattic\WooCommerce\Internal\PushNotifications\Triggers\NewReviewNotificationTrigger; |
| 19 | use Automattic\WooCommerce\Internal\PushNotifications\Triggers\StockNotificationRecoveryHandler; |
| 20 | use Automattic\WooCommerce\Internal\PushNotifications\Triggers\StockNotificationTrigger; |
| 21 | use Automattic\WooCommerce\Proxies\LegacyProxy; |
| 22 | use WC_Logger; |
| 23 | use Exception; |
| 24 | |
| 25 | /** |
| 26 | * WC Push Notifications |
| 27 | * |
| 28 | * Class for setting up the WooCommerce-driven push notifications. |
| 29 | * |
| 30 | * @since 10.4.0 |
| 31 | */ |
| 32 | class PushNotifications { |
| 33 | /** |
| 34 | * Feature name for the push notifications feature. |
| 35 | */ |
| 36 | const FEATURE_NAME = 'push_notifications'; |
| 37 | |
| 38 | /** |
| 39 | * Roles that can receive push notifications. |
| 40 | * |
| 41 | * This will be used to gate functionality access to just these roles. |
| 42 | */ |
| 43 | const ROLES_WITH_PUSH_NOTIFICATIONS_ENABLED = array( |
| 44 | 'administrator', |
| 45 | 'shop_manager', |
| 46 | ); |
| 47 | |
| 48 | /** |
| 49 | * 'Memoized' enablement flag. |
| 50 | * |
| 51 | * @var bool|null |
| 52 | */ |
| 53 | private ?bool $enabled = null; |
| 54 | |
| 55 | /** |
| 56 | * Registers initialisation tasks to the `init` hook. |
| 57 | * |
| 58 | * @return void |
| 59 | * |
| 60 | * @since 10.4.0 |
| 61 | */ |
| 62 | public function register(): void { |
| 63 | add_action( 'init', array( $this, 'on_init' ) ); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Loads the push notifications class. |
| 68 | * |
| 69 | * @return void |
| 70 | * |
| 71 | * @since 10.6.0 |
| 72 | */ |
| 73 | public function on_init(): void { |
| 74 | if ( ! $this->should_be_enabled() ) { |
| 75 | return; |
| 76 | } |
| 77 | |
| 78 | $this->register_post_types(); |
| 79 | |
| 80 | wc_get_container()->get( PendingNotificationStore::class )->register(); |
| 81 | |
| 82 | ( new PushTokenRestController() )->register(); |
| 83 | ( new PushNotificationRestController() )->register(); |
| 84 | ( new NotificationPreferencesRestController() )->register(); |
| 85 | ( new NewOrderNotificationTrigger() )->register(); |
| 86 | ( new NewReviewNotificationTrigger() )->register(); |
| 87 | ( new StockNotificationTrigger() )->register(); |
| 88 | ( new StockNotificationRecoveryHandler() )->register(); |
| 89 | |
| 90 | wc_get_container()->get( NotificationProcessor::class )->register(); |
| 91 | wc_get_container()->get( NotificationRetryHandler::class )->register(); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Registers the push token custom post type. |
| 96 | * |
| 97 | * @since 10.5.0 |
| 98 | * @return void |
| 99 | */ |
| 100 | public function register_post_types(): void { |
| 101 | register_post_type( |
| 102 | PushToken::POST_TYPE, |
| 103 | array( |
| 104 | 'labels' => array( |
| 105 | 'name' => __( 'Push Tokens', 'woocommerce' ), |
| 106 | 'singular_name' => __( 'Push Token', 'woocommerce' ), |
| 107 | ), |
| 108 | 'public' => false, |
| 109 | 'publicly_queryable' => false, |
| 110 | 'show_ui' => false, |
| 111 | 'show_in_menu' => false, |
| 112 | 'query_var' => false, |
| 113 | 'rewrite' => false, |
| 114 | 'capability_type' => 'post', |
| 115 | 'has_archive' => false, |
| 116 | 'hierarchical' => false, |
| 117 | 'supports' => array( 'author' ), |
| 118 | 'can_export' => false, |
| 119 | 'delete_with_user' => true, |
| 120 | ) |
| 121 | ); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Determines if local push notification functionality should be enabled. |
| 126 | * Push notifications require Jetpack to be connected. Memoize the value so |
| 127 | * we only check once per request. |
| 128 | * |
| 129 | * @return bool |
| 130 | * |
| 131 | * @since 10.4.0 |
| 132 | */ |
| 133 | public function should_be_enabled(): bool { |
| 134 | if ( null !== $this->enabled ) { |
| 135 | return $this->enabled; |
| 136 | } |
| 137 | |
| 138 | $feature_disabled = wc_string_to_bool( |
| 139 | /** |
| 140 | * Filters whether enhanced push notifications should be disabled. |
| 141 | * |
| 142 | * The feature was previously controlled by a now-deprecated feature |
| 143 | * flag. It is now enabled by default for all compatible users, but this |
| 144 | * filter lets a store force it off (e.g. to fall back to Jetpack Sync |
| 145 | * if something isn't working). The feature also requires a Jetpack |
| 146 | * connection, which is checked separately below. |
| 147 | * |
| 148 | * @since 10.9.2 |
| 149 | * |
| 150 | * @param bool $disabled Whether enhanced push notifications are disabled. Defaults to false. |
| 151 | */ |
| 152 | apply_filters( 'woocommerce_enhanced_push_notifications_disabled', false ) |
| 153 | ); |
| 154 | |
| 155 | if ( $feature_disabled ) { |
| 156 | $this->enabled = false; |
| 157 | return $this->enabled; |
| 158 | } |
| 159 | |
| 160 | try { |
| 161 | $proxy = wc_get_container()->get( LegacyProxy::class ); |
| 162 | |
| 163 | $this->enabled = ( |
| 164 | class_exists( JetpackConnectionManager::class ) |
| 165 | && $proxy->get_instance_of( JetpackConnectionManager::class )->is_connected() |
| 166 | ); |
| 167 | } catch ( Exception $e ) { |
| 168 | $logger = wc_get_container()->get( LegacyProxy::class )->call_function( 'wc_get_logger' ); |
| 169 | |
| 170 | if ( $logger instanceof WC_Logger ) { |
| 171 | $logger->error( |
| 172 | 'Error determining if PushNotifications feature should be enabled: ' . $e->getMessage() |
| 173 | ); |
| 174 | } |
| 175 | |
| 176 | $this->enabled = false; |
| 177 | } |
| 178 | |
| 179 | return $this->enabled; |
| 180 | } |
| 181 | } |
| 182 |