settingsController = $settingsController; $this->cookies = $cookies; $this->shortcodes = $shortcodes; $this->linkShortcodeCategory = $linkShortcodeCategory; } /** * @param \stdClass|null $data */ public function track($data) { if (!$data || empty($data->link)) { return $this->abort(); } /** @var SubscriberEntity $subscriber */ $subscriber = $data->subscriber; /** @var SendingQueueEntity $queue */ $queue = $data->queue; /** @var NewsletterEntity $newsletter */ $newsletter = $data->newsletter; /** @var NewsletterLinkEntity $link */ $link = $data->link; $wpUserPreview = ($data->preview && ($subscriber->isWPUser())); // log statistics only if the action did not come from // a WP user previewing the newsletter if (!$wpUserPreview) { $statisticsClicks = StatisticsClicks::createOrUpdateClickCount( $link->getId(), $subscriber->getId(), $newsletter->getId(), $queue->getId() ); $this->sendRevenueCookie($statisticsClicks); $this->sendAbandonedCartCookie($subscriber); // track open event $openEvent = new Opens(); $openEvent->track($data, $displayImage = false); } $url = $this->processUrl($link->getUrl(), $newsletter, $subscriber, $queue, $wpUserPreview); $this->redirectToUrl($url); } private function sendRevenueCookie(StatisticsClicks $clicks) { if ($this->settingsController->get('woocommerce.accept_cookie_revenue_tracking.enabled')) { $this->cookies->set( self::REVENUE_TRACKING_COOKIE_NAME, [ 'statistics_clicks' => $clicks->id, 'created_at' => time(), ], [ 'expires' => time() + self::REVENUE_TRACKING_COOKIE_EXPIRY, 'path' => '/', ] ); } } private function sendAbandonedCartCookie($subscriber) { if ($this->settingsController->get('woocommerce.accept_cookie_revenue_tracking.enabled')) { $this->cookies->set( self::ABANDONED_CART_COOKIE_NAME, [ 'subscriber_id' => $subscriber->getId(), ], [ 'expires' => time() + self::ABANDONED_CART_COOKIE_EXPIRY, 'path' => '/', ] ); } } public function processUrl( string $url, NewsletterEntity $newsletter, SubscriberEntity $subscriber, SendingQueueEntity $queue, bool $wpUserPreview ) { if (preg_match('/\[link:(?P.*?)\]/', $url, $shortcode)) { if (!$shortcode['action']) $this->abort(); $url = $this->linkShortcodeCategory->processShortcodeAction( $shortcode['action'], $newsletter, $subscriber, $queue, $wpUserPreview ); } else { $this->shortcodes->setQueue($queue); $this->shortcodes->setNewsletter($newsletter); $this->shortcodes->setSubscriber($subscriber); $this->shortcodes->setWpUserPreview($wpUserPreview); $url = $this->shortcodes->replace($url); } return $url; } public function abort() { global $wp_query;// phpcs:ignore Squiz.NamingConventions.ValidVariableName.NotCamelCaps WPFunctions::get()->statusHeader(404); $wp_query->set_404();// phpcs:ignore Squiz.NamingConventions.ValidVariableName.NotCamelCaps WPFunctions::get()->getTemplatePart((string)404); exit; } public function redirectToUrl($url) { header('Location: ' . $url, true, 302); exit; } }