first commit
This commit is contained in:
126
modules/psxmarketingwithgoogle/classes/Handler/ErrorHandler.php
Normal file
126
modules/psxmarketingwithgoogle/classes/Handler/ErrorHandler.php
Normal file
@@ -0,0 +1,126 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright since 2007 PrestaShop SA and Contributors
|
||||
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Academic Free License version 3.0
|
||||
* that is bundled with this package in the file LICENSE.md.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* https://opensource.org/licenses/AFL-3.0
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* @author PrestaShop SA and Contributors <contact@prestashop.com>
|
||||
* @copyright Since 2007 PrestaShop SA and Contributors
|
||||
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
|
||||
*/
|
||||
|
||||
namespace PrestaShop\Module\PsxMarketingWithGoogle\Handler;
|
||||
|
||||
use Exception;
|
||||
use Module;
|
||||
use PrestaShop\Module\PsxMarketingWithGoogle\Config\Config;
|
||||
use PrestaShop\PsAccountsInstaller\Installer\Facade\PsAccounts;
|
||||
use PsxMarketingWithGoogle;
|
||||
|
||||
/**
|
||||
* Handle Error.
|
||||
*/
|
||||
class ErrorHandler
|
||||
{
|
||||
/**
|
||||
* @var ModuleFilteredRavenClient
|
||||
*/
|
||||
protected $client;
|
||||
|
||||
/**
|
||||
* @var ErrorHandler
|
||||
*/
|
||||
private static $instance;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
/** @var PsxMarketingWithGoogle */
|
||||
$module = Module::getInstanceByName('psxmarketingwithgoogle');
|
||||
|
||||
$this->client = new ModuleFilteredRavenClient(
|
||||
Config::PSX_MKTG_WITH_GOOGLE_SENTRY_CREDENTIALS_PHP,
|
||||
[
|
||||
'level' => 'warning',
|
||||
'tags' => [
|
||||
'php_version' => phpversion(),
|
||||
'psxmarketingwithgoogle_version' => $module->version,
|
||||
'prestashop_version' => _PS_VERSION_,
|
||||
'psxmarketingwithgoogle_is_enabled' => \Module::isEnabled('psxmarketingwithgoogle'),
|
||||
'psxmarketingwithgoogle_is_installed' => \Module::isInstalled('psxmarketingwithgoogle'),
|
||||
],
|
||||
'release' => "v{$module->version}",
|
||||
]
|
||||
);
|
||||
|
||||
try {
|
||||
$psAccountsService = $module->getService(PsAccounts::class)->getPsAccountsService();
|
||||
$this->client->user_context([
|
||||
'id' => $psAccountsService->getShopUuidV4(),
|
||||
]);
|
||||
} catch (Exception $e) {
|
||||
// Do nothing
|
||||
}
|
||||
|
||||
// We use realpath to get errors even if module is behind a symbolic link
|
||||
$this->client->setAppPath(realpath(_PS_MODULE_DIR_ . $module->name . '/'));
|
||||
// - Do no not add the shop root folder, it will exclude everything even if specified in the app path.
|
||||
// - Excluding vendor/ avoids errors comming from one of your libraries library when called by another module.
|
||||
$this->client->setExcludedAppPaths([
|
||||
realpath(_PS_MODULE_DIR_ . $module->name . '/vendor/'),
|
||||
]);
|
||||
$this->client->setExcludedDomains(['127.0.0.1', 'localhost', '.local']);
|
||||
|
||||
if (version_compare(phpversion(), '7.4.0', '>=') && version_compare(_PS_VERSION_, '1.7.8.0', '<')) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->client->install();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Exception $error
|
||||
* @param mixed $code
|
||||
* @param bool|null $throw
|
||||
* @param array|null $data
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function handle($error, $code = null, $throw = true, $data = null)
|
||||
{
|
||||
$this->client->captureException($error, $data);
|
||||
if ($code && true === $throw) {
|
||||
http_response_code($code);
|
||||
throw $error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ErrorHandler
|
||||
*/
|
||||
public static function getInstance()
|
||||
{
|
||||
if (self::$instance === null) {
|
||||
self::$instance = new ErrorHandler();
|
||||
}
|
||||
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
private function __clone()
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright since 2007 PrestaShop SA and Contributors
|
||||
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Academic Free License version 3.0
|
||||
* that is bundled with this package in the file LICENSE.md.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* https://opensource.org/licenses/AFL-3.0
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* @author PrestaShop SA and Contributors <contact@prestashop.com>
|
||||
* @copyright Since 2007 PrestaShop SA and Contributors
|
||||
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
|
||||
*/
|
||||
|
||||
namespace PrestaShop\Module\PsxMarketingWithGoogle\Handler;
|
||||
|
||||
use Raven_Client;
|
||||
|
||||
/**
|
||||
* Inheritance allow us to check data generated by Raven and filter errors
|
||||
* that are not related to the module.
|
||||
* Raven does not filter errors by itself depending on the appPath and any
|
||||
* excludedAppPaths, but declares what phase of the stack trace is outside the app.
|
||||
* We use this data to allow each module filtering their own errors.
|
||||
*
|
||||
* IMPORTANT NOTE: This class is present is this module during the
|
||||
* stabilisation phase, and will be moved later in a library.
|
||||
*/
|
||||
class ModuleFilteredRavenClient extends Raven_Client
|
||||
{
|
||||
/**
|
||||
* @var string[]|null
|
||||
*/
|
||||
protected $excluded_domains;
|
||||
|
||||
public function capture($data, $stack = null, $vars = null)
|
||||
{
|
||||
/*
|
||||
Content of $data:
|
||||
array:2 [▼
|
||||
"exception" => array:1 [▼
|
||||
"values" => array:1 [▼
|
||||
0 => array:3 [▼
|
||||
"value" => "Class 'DogeInPsFacebook' not found"
|
||||
"type" => "Error"
|
||||
"stacktrace" => array:1 [▼
|
||||
"frames" => array:4 [▼
|
||||
0 => array:7 [▼
|
||||
"filename" => "index.php"
|
||||
"lineno" => 93
|
||||
"function" => null
|
||||
"pre_context" => array:5 [▶]
|
||||
"context_line" => " Dispatcher::getInstance()->dispatch();"
|
||||
"post_context" => array:2 [▶]
|
||||
"in_app" => false
|
||||
1 => array:3 [▼
|
||||
[Can be defined when a subexception is set]
|
||||
|
||||
*/
|
||||
if (!isset($data['exception']['values'][0]['stacktrace']['frames'])) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($this->isErrorFilteredByContext()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$allowCapture = false;
|
||||
foreach ($data['exception']['values'] as $errorValues) {
|
||||
$allowCapture = $allowCapture || $this->isErrorInApp($errorValues);
|
||||
}
|
||||
|
||||
if (!$allowCapture) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return parent::capture($data, $stack, $vars);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return self
|
||||
*/
|
||||
public function setExcludedDomains(array $domains)
|
||||
{
|
||||
$this->excluded_domains = $domains;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
private function isErrorInApp(array $data)
|
||||
{
|
||||
$atLeastOneFileIsInApp = false;
|
||||
foreach ($data['stacktrace']['frames'] as $frame) {
|
||||
$atLeastOneFileIsInApp = $atLeastOneFileIsInApp || ((isset($frame['in_app']) && $frame['in_app']));
|
||||
}
|
||||
|
||||
return $atLeastOneFileIsInApp;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check the conditions in which the error is thrown, so we can apply filters
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function isErrorFilteredByContext()
|
||||
{
|
||||
if ($this->excluded_domains && !empty($_SERVER['REMOTE_ADDR'])) {
|
||||
foreach ($this->excluded_domains as $domain) {
|
||||
if (strpos($_SERVER['REMOTE_ADDR'], $domain) !== false) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,198 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright since 2007 PrestaShop SA and Contributors
|
||||
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Academic Free License version 3.0
|
||||
* that is bundled with this package in the file LICENSE.md.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* https://opensource.org/licenses/AFL-3.0
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* @author PrestaShop SA and Contributors <contact@prestashop.com>
|
||||
* @copyright Since 2007 PrestaShop SA and Contributors
|
||||
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
|
||||
*/
|
||||
|
||||
namespace PrestaShop\Module\PsxMarketingWithGoogle\Handler;
|
||||
|
||||
use Context;
|
||||
use PrestaShop\Module\PsxMarketingWithGoogle\Adapter\ConfigurationAdapter;
|
||||
use PrestaShop\Module\PsxMarketingWithGoogle\Buffer\TemplateBuffer;
|
||||
use PrestaShop\Module\PsxMarketingWithGoogle\Config\Config;
|
||||
use PrestaShop\Module\PsxMarketingWithGoogle\Provider\CartEventDataProvider;
|
||||
use PrestaShop\Module\PsxMarketingWithGoogle\Provider\PageViewEventDataProvider;
|
||||
use PrestaShop\Module\PsxMarketingWithGoogle\Provider\PurchaseEventDataProvider;
|
||||
use PsxMarketingWithGoogle;
|
||||
|
||||
class RemarketingHookHandler
|
||||
{
|
||||
/**
|
||||
* @var ConfigurationAdapter
|
||||
*/
|
||||
protected $configurationAdapter;
|
||||
|
||||
/**
|
||||
* @var TemplateBuffer
|
||||
*/
|
||||
protected $templateBuffer;
|
||||
|
||||
/**
|
||||
* @var Context
|
||||
*/
|
||||
protected $context;
|
||||
|
||||
/**
|
||||
* @var PsxMarketingWithGoogle
|
||||
*/
|
||||
protected $module;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected $active;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $conversionLabels;
|
||||
|
||||
public function __construct(ConfigurationAdapter $configurationAdapter, TemplateBuffer $templateBuffer, Context $context, $module)
|
||||
{
|
||||
$this->configurationAdapter = $configurationAdapter;
|
||||
$this->templateBuffer = $templateBuffer;
|
||||
$this->context = $context;
|
||||
$this->module = $module;
|
||||
|
||||
$this->active = (bool) $this->configurationAdapter->get(Config::PSX_MKTG_WITH_GOOGLE_REMARKETING_STATUS)
|
||||
&& (bool) $this->configurationAdapter->get(Config::PSX_MKTG_WITH_GOOGLE_REMARKETING_TAG)
|
||||
&& in_array($this->context->controller->controller_type, ['front', 'modulefront']);
|
||||
|
||||
$this->conversionLabels = json_decode($this->configurationAdapter->get(Config::PSX_MKTG_WITH_GOOGLE_REMARKETING_CONVERSION_LABELS), true)
|
||||
?: [];
|
||||
|
||||
if ($this->active) {
|
||||
$this->templateBuffer->init($this->findIdentifierFromContext($context));
|
||||
}
|
||||
}
|
||||
|
||||
public function handleHook(string $hookName, array $data = []): string
|
||||
{
|
||||
if (!$this->active) {
|
||||
return '';
|
||||
}
|
||||
|
||||
switch ($hookName) {
|
||||
case 'hookDisplayTop':
|
||||
if (($sendTo = $this->getSendTo(Config::REMARKETING_CONVERSION_LABEL_PAGE_VIEW)) === null) {
|
||||
break;
|
||||
}
|
||||
|
||||
$eventData = $this->module->getService(PageViewEventDataProvider::class)->getEventData($sendTo);
|
||||
|
||||
if ($eventData === null) {
|
||||
break;
|
||||
}
|
||||
|
||||
$this->context->smarty->assign([
|
||||
'eventData' => $eventData,
|
||||
]);
|
||||
|
||||
$this->templateBuffer->add(
|
||||
$this->module->display($this->module->getfilePath(), '/views/templates/hook/gtagEvent.tpl')
|
||||
);
|
||||
break;
|
||||
case 'hookDisplayOrderConfirmation':
|
||||
if (($sendTo = $this->getSendTo(Config::REMARKETING_CONVERSION_LABEL_PURCHASE)) === null) {
|
||||
break;
|
||||
}
|
||||
|
||||
$this->context->smarty->assign([
|
||||
'eventData' => $this->module->getService(PurchaseEventDataProvider::class)->getEventData($sendTo, $data['order']),
|
||||
]);
|
||||
$this->templateBuffer->add(
|
||||
$this->module->display($this->module->getfilePath(), '/views/templates/hook/gtagEvent.tpl')
|
||||
);
|
||||
break;
|
||||
|
||||
case 'hookActionCartUpdateQuantityBefore':
|
||||
if ($data['operator'] !== 'up') {
|
||||
break;
|
||||
}
|
||||
if (($sendTo = $this->getSendTo(Config::REMARKETING_CONVERSION_LABEL_ADD_TO_CART)) === null) {
|
||||
break;
|
||||
}
|
||||
|
||||
$this->context->smarty->assign([
|
||||
'eventData' => $this->module->getService(CartEventDataProvider::class)->getEventData($sendTo, $data),
|
||||
]);
|
||||
$this->templateBuffer->add(
|
||||
$this->module->display($this->module->getfilePath(), '/views/templates/hook/gtagEvent.tpl')
|
||||
);
|
||||
break;
|
||||
}
|
||||
|
||||
if ($hookName === 'hookDisplayHeader') {
|
||||
return base64_decode($this->configurationAdapter->get(Config::PSX_MKTG_WITH_GOOGLE_REMARKETING_TAG));
|
||||
}
|
||||
|
||||
// Return the existing content in case we have a display hook
|
||||
if (strpos($hookName, 'Display') === 4 && !$this->isCurrentRequestAnAjax()) {
|
||||
return $this->templateBuffer->flush();
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
private function getSendTo($eventName)
|
||||
{
|
||||
if (!empty($this->conversionLabels[$eventName])) {
|
||||
return $this->conversionLabels[$eventName];
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
private function isCurrentRequestAnAjax()
|
||||
{
|
||||
/*
|
||||
* An ajax property is available in controllers
|
||||
* when the whole page template should not be generated.
|
||||
*/
|
||||
if ($this->context->controller->ajax) {
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
* In case the ajax property is not properly set, there is
|
||||
* another check available.
|
||||
*/
|
||||
if ($this->context->controller->isXmlHttpRequest()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
private function findIdentifierFromContext(Context $context)
|
||||
{
|
||||
if (!empty($context->customer->id_guest)) {
|
||||
return 'guest_' . $context->customer->id_guest;
|
||||
}
|
||||
if (!empty($context->cart->id)) {
|
||||
return 'cart_' . $context->cart->id;
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
}
|
||||
11
modules/psxmarketingwithgoogle/classes/Handler/index.php
Normal file
11
modules/psxmarketingwithgoogle/classes/Handler/index.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
|
||||
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
|
||||
|
||||
header('Cache-Control: no-store, no-cache, must-revalidate');
|
||||
header('Cache-Control: post-check=0, pre-check=0', false);
|
||||
header('Pragma: no-cache');
|
||||
|
||||
header('Location: ../');
|
||||
exit;
|
||||
Reference in New Issue
Block a user