Spaces:
No application file
No application file
File size: 6,294 Bytes
d2897cd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
<?php
namespace Mautic\NotificationBundle\Helper;
use Doctrine\ORM\EntityManager;
use Mautic\CoreBundle\Helper\CoreParametersHelper;
use Mautic\CoreBundle\Twig\Helper\AssetsHelper;
use Mautic\LeadBundle\Entity\DoNotContact;
use Mautic\PluginBundle\Helper\IntegrationHelper;
use Symfony\Bundle\FrameworkBundle\Routing\Router;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
class NotificationHelper
{
public function __construct(
protected EntityManager $em,
protected AssetsHelper $assetsHelper,
protected CoreParametersHelper $coreParametersHelper,
protected IntegrationHelper $integrationHelper,
protected Router $router,
protected RequestStack $requestStack,
private \Mautic\LeadBundle\Model\DoNotContact $doNotContact
) {
}
/**
* @param string $notification
*
* @return bool
*/
public function unsubscribe($notification)
{
/** @var \Mautic\LeadBundle\Entity\LeadRepository $repo */
$repo = $this->em->getRepository(\Mautic\LeadBundle\Entity\Lead::class);
$lead = $repo->getLeadByEmail($notification);
return $this->doNotContact->addDncForContact($lead->getId(), 'notification', DoNotContact::UNSUBSCRIBED);
}
public function getHeaderScript()
{
if ($this->hasScript()) {
return 'MauticJS.insertScript(\'https://cdn.onesignal.com/sdks/OneSignalSDK.js\');
var OneSignal = OneSignal || [];';
}
}
public function getScript()
{
if ($this->hasScript()) {
$integration = $this->integrationHelper->getIntegrationObject('OneSignal');
if (!$integration || false === $integration->getIntegrationSettings()->getIsPublished()) {
return;
}
$settings = $integration->getIntegrationSettings();
$keys = $integration->getDecryptedApiKeys();
$supported = $settings->getSupportedFeatures();
$featureSettings = $settings->getFeatureSettings();
$appId = $keys['app_id'];
$safariWebId = $keys['safari_web_id'];
$welcomenotificationEnabled = in_array('welcome_notification_enabled', $supported);
$notificationSubdomainName = $featureSettings['subdomain_name'];
$leadAssociationUrl = $this->router->generate(
'mautic_subscribe_notification',
[],
UrlGeneratorInterface::ABSOLUTE_URL
);
$welcomenotificationText = '';
if (!$welcomenotificationEnabled) {
$welcomenotificationText = 'welcomeNotification: { "disable": true },';
}
$server = $this->requestStack->getCurrentRequest()->server;
$https = ('https' == parse_url($server->get('HTTP_REFERER'), PHP_URL_SCHEME)) ? true : false;
$subdomainName = '';
if (!$https && $notificationSubdomainName) {
$subdomainName = 'subdomainName: "'.$notificationSubdomainName.'",
httpPermissionRequest: {
enable: true,
useCustomModal: true
},';
}
$oneSignalInit = <<<JS
var scrpt = document.createElement('link');
scrpt.rel ='manifest';
scrpt.href ='/manifest.json';
var head = document.getElementsByTagName('head')[0];
head.appendChild(scrpt);
OneSignal.push(["init", {
appId: "{$appId}",
safari_web_id: "{$safariWebId}",
autoRegister: true,
{$welcomenotificationText}
{$subdomainName}
notifyButton: {
enable: false // Set to false to hide
}
}]);
var postUserIdToMautic = function(userId) {
var data = [];
data['osid'] = userId;
MauticJS.makeCORSRequest('GET', '{$leadAssociationUrl}', data);
};
OneSignal.push(function() {
OneSignal.getUserId(function(userId) {
if (! userId) {
OneSignal.on('subscriptionChange', function(isSubscribed) {
if (isSubscribed) {
OneSignal.getUserId(function(newUserId) {
postUserIdToMautic(newUserId);
});
}
});
} else {
postUserIdToMautic(userId);
}
});
// Just to be sure we've grabbed the ID
window.onbeforeunload = function() {
OneSignal.getUserId(function(userId) {
if (userId) {
postUserIdToMautic(userId);
}
});
};
});
JS;
if (!$https && $notificationSubdomainName) {
$oneSignalInit .= <<<'JS'
OneSignal.push(function() {
OneSignal.on('notificationPermissionChange', function(permissionChange) {
if(currentPermission == 'granted'){
setTimeout(function(){
OneSignal.registerForPushNotifications({httpPermissionRequest: true});
}, 100);
}
});
});
JS;
}
return $oneSignalInit;
}
}
private function hasScript(): bool
{
$landingPage = true;
$server = $this->requestStack->getCurrentRequest()->server;
$cookies = $this->requestStack->getCurrentRequest()->cookies;
// already exist
if ($cookies->get('mtc_osid')) {
return false;
}
if (!str_contains($server->get('HTTP_REFERER'), $this->coreParametersHelper->get('site_url'))) {
$landingPage = false;
}
$integration = $this->integrationHelper->getIntegrationObject('OneSignal');
if (!$integration || false === $integration->getIntegrationSettings()->getIsPublished()) {
return false;
}
$supportedFeatures = $integration->getIntegrationSettings()->getSupportedFeatures();
// disable on Landing pages
if (true === $landingPage && !in_array('landing_page_enabled', $supportedFeatures)) {
return false;
}
// disable on Landing pages
if (false === $landingPage && !in_array('tracking_page_enabled', $supportedFeatures)) {
return false;
}
return true;
}
}
|