Automattic\WooCommerce\Internal\PushNotifications

PushNotifications::should_be_enabled │ public │ WC 10.4.0

Determines if local push notification functionality should be enabled. Push notifications require Jetpack to be connected. Memoize the value so we only check once per request.

Method of the class: PushNotifications{}

Returns

bool.

Usage

$PushNotifications = new PushNotifications();
$PushNotifications->should_be_enabled(): bool;

Changelog

Since 10.4.0 Introduced.

PushNotifications::should_be_enabled() code WC 11.1.2

public function should_be_enabled(): bool {
	if ( null !== $this->enabled ) {
		return $this->enabled;
	}

	$feature_disabled = wc_string_to_bool(
		/**
		 * Filters whether enhanced push notifications should be disabled.
		 *
		 * The feature was previously controlled by a now-deprecated feature
		 * flag. It is now enabled by default for all compatible users, but this
		 * filter lets a store force it off (e.g. to fall back to Jetpack Sync
		 * if something isn't working). The feature also requires a Jetpack
		 * connection, which is checked separately below.
		 *
		 * @since 10.9.2
		 *
		 * @param bool $disabled Whether enhanced push notifications are disabled. Defaults to false.
		 */
		apply_filters( 'woocommerce_enhanced_push_notifications_disabled', false )
	);

	if ( $feature_disabled ) {
		$this->enabled = false;
		return $this->enabled;
	}

	try {
		$proxy = wc_get_container()->get( LegacyProxy::class );

		$this->enabled = (
			class_exists( JetpackConnectionManager::class )
			&& $proxy->get_instance_of( JetpackConnectionManager::class )->is_connected()
		);
	} catch ( Exception $e ) {
		$logger = wc_get_container()->get( LegacyProxy::class )->call_function( 'wc_get_logger' );

		if ( $logger instanceof WC_Logger ) {
			$logger->error(
				'Error determining if PushNotifications feature should be enabled: ' . $e->getMessage()
			);
		}

		$this->enabled = false;
	}

	return $this->enabled;
}