From 95e80929ca7edb824d0f5735be28ac825546511d Mon Sep 17 00:00:00 2001 From: Progi1984 Date: Mon, 4 Apr 2022 16:56:27 +0200 Subject: [PATCH] Moved some code from footer to head tag --- classes/Database/Install.php | 1 - classes/Hook/HookDisplayHeader.php | 57 +++++++++++++++++++--- classes/Hook/HookDisplayHome.php | 78 ------------------------------ ps_googleanalytics.php | 11 ----- tests/phpstan/phpstan-1.7.1.2.neon | 1 + tests/phpstan/phpstan-1.7.2.5.neon | 1 + tests/phpstan/phpstan-1.7.3.4.neon | 1 + tests/phpstan/phpstan-1.7.4.4.neon | 1 + tests/phpstan/phpstan-1.7.5.1.neon | 1 + tests/phpstan/phpstan-1.7.6.neon | 1 + upgrade/upgrade-4.1.3.php | 30 ++++++++++++ 11 files changed, 87 insertions(+), 96 deletions(-) delete mode 100644 classes/Hook/HookDisplayHome.php create mode 100644 upgrade/upgrade-4.1.3.php diff --git a/classes/Database/Install.php b/classes/Database/Install.php index 7a5c78a..962b6d3 100644 --- a/classes/Database/Install.php +++ b/classes/Database/Install.php @@ -101,7 +101,6 @@ public function registerHooks() return $this->module->registerHook('displayHeader') && $this->module->registerHook('displayAdminOrder') && $this->module->registerHook('displayFooter') && - $this->module->registerHook('displayHome') && $this->module->registerHook('displayFooterProduct') && $this->module->registerHook('displayOrderConfirmation') && $this->module->registerHook('actionProductCancel') && diff --git a/classes/Hook/HookDisplayHeader.php b/classes/Hook/HookDisplayHeader.php index 4a85da1..30bd02a 100644 --- a/classes/Hook/HookDisplayHeader.php +++ b/classes/Hook/HookDisplayHeader.php @@ -20,16 +20,31 @@ namespace PrestaShop\Module\Ps_Googleanalytics\Hooks; +use Category; use Configuration; use Context; +use Customer; +use PrestaShop\Module\Ps_Googleanalytics\GoogleAnalyticsTools; +use PrestaShop\Module\Ps_Googleanalytics\Handler\GanalyticsJsHandler; +use PrestaShop\Module\Ps_Googleanalytics\Handler\ModuleHandler; +use PrestaShop\Module\Ps_Googleanalytics\Wrapper\ProductWrapper; use Ps_Googleanalytics; use Shop; use Tools; class HookDisplayHeader implements HookInterface { + /** + * @var Ps_Googleanalytics + */ private $module; + /** + * @var Context + */ private $context; + /** + * @var mixed + */ private $params; /** @@ -44,9 +59,7 @@ public function __construct(Ps_Googleanalytics $module, Context $context) } /** - * run - * - * @return void|string + * @return false|string */ public function run() { @@ -62,8 +75,9 @@ public function run() $userId = null; $gaCrossdomainEnabled = false; - if (Configuration::get('GA_USERID_ENABLED') && - $this->context->customer && $this->context->customer->isLogged() + if (Configuration::get('GA_USERID_ENABLED') + && $this->context->customer instanceof Customer + && $this->context->customer->isLogged() ) { $userId = (int) $this->context->customer->id; } @@ -91,6 +105,37 @@ public function run() return $this->module->display( $this->module->getLocalPath() . $this->module->name, 'ps_googleanalytics.tpl' + ) . $this->displayGaTag(); + } + + private function displayGaTag() + { + $moduleHandler = new ModuleHandler(); + $gaTools = new GoogleAnalyticsTools(); + $gaTagHandler = new GanalyticsJsHandler($this->module, $this->context); + $gaScripts = ''; + + // Home featured products + if ($moduleHandler->isModuleEnabledAndHookedOn('ps_featuredproducts', 'displayHome')) { + $category = new Category($this->context->shop->getCategory(), $this->context->language->id); + $productWrapper = new ProductWrapper($this->context); + $homeFeaturedProducts = $productWrapper->wrapProductList( + $category->getProducts( + (int) $this->context->language->id, + 1, + (Configuration::get('HOME_FEATURED_NBR') ? (int) Configuration::get('HOME_FEATURED_NBR') : 8), + 'position' + ), + [], + true + ); + $gaScripts .= $gaTools->addProductImpression($homeFeaturedProducts) . $gaTools->addProductClick($homeFeaturedProducts); + } + + $this->module->js_state = 1; + + return $gaTagHandler->generate( + $gaTools->filter($gaScripts, $this->module->filterable) ); } @@ -101,7 +146,7 @@ public function run() */ public function setParams($params) { - $this->module->params = $params; + $this->params = $params; } /** diff --git a/classes/Hook/HookDisplayHome.php b/classes/Hook/HookDisplayHome.php deleted file mode 100644 index 0d0fa7a..0000000 --- a/classes/Hook/HookDisplayHome.php +++ /dev/null @@ -1,78 +0,0 @@ - - * @copyright 2007-2020 PrestaShop SA and Contributors - * @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0) - * International Registered Trademark & Property of PrestaShop SA - */ - -namespace PrestaShop\Module\Ps_Googleanalytics\Hooks; - -use Category; -use Configuration; -use Context; -use PrestaShop\Module\Ps_Googleanalytics\GoogleAnalyticsTools; -use PrestaShop\Module\Ps_Googleanalytics\Handler\GanalyticsJsHandler; -use PrestaShop\Module\Ps_Googleanalytics\Handler\ModuleHandler; -use PrestaShop\Module\Ps_Googleanalytics\Wrapper\ProductWrapper; -use Ps_Googleanalytics; - -class HookDisplayHome implements HookInterface -{ - private $module; - private $context; - - public function __construct(Ps_Googleanalytics $module, Context $context) - { - $this->module = $module; - $this->context = $context; - } - - /** - * run - * - * @return string - */ - public function run() - { - $moduleHandler = new ModuleHandler(); - $gaTools = new GoogleAnalyticsTools(); - $gaTagHandler = new GanalyticsJsHandler($this->module, $this->context); - $gaScripts = ''; - - // Home featured products - if ($moduleHandler->isModuleEnabledAndHookedOn('ps_featuredproducts', 'displayHome')) { - $category = new Category($this->context->shop->getCategory(), $this->context->language->id); - $productWrapper = new ProductWrapper($this->context); - $homeFeaturedProducts = $productWrapper->wrapProductList( - $category->getProducts( - (int) $this->context->language->id, - 1, - (Configuration::get('HOME_FEATURED_NBR') ? (int) Configuration::get('HOME_FEATURED_NBR') : 8), - 'position' - ), - [], - true - ); - $gaScripts .= $gaTools->addProductImpression($homeFeaturedProducts) . $gaTools->addProductClick($homeFeaturedProducts); - } - - $this->module->js_state = 1; - - return $gaTagHandler->generate( - $gaTools->filter($gaScripts, $this->module->filterable) - ); - } -} diff --git a/ps_googleanalytics.php b/ps_googleanalytics.php index b9fa4f5..3315e6c 100755 --- a/ps_googleanalytics.php +++ b/ps_googleanalytics.php @@ -116,17 +116,6 @@ public function hookDisplayFooter() return $hook->run(); } - /** - * Homepage hook. - * This function is run to manage analytics for product list associated to home featured, news products and best sellers Modules - */ - public function hookDisplayHome() - { - $hook = new PrestaShop\Module\Ps_Googleanalytics\Hooks\HookDisplayHome($this, $this->context); - - return $hook->run(); - } - /** * Product page footer hook * This function is run to load JS for product details view diff --git a/tests/phpstan/phpstan-1.7.1.2.neon b/tests/phpstan/phpstan-1.7.1.2.neon index df6b779..ef8b110 100644 --- a/tests/phpstan/phpstan-1.7.1.2.neon +++ b/tests/phpstan/phpstan-1.7.1.2.neon @@ -4,3 +4,4 @@ includes: parameters: ignoreErrors: - '#PrestaShop\\Module\\Ps_Googleanalytics\\Handler\\ModuleHandler::uninstallModule\(\) calls parent::uninstall\(\) but PrestaShop\\Module\\Ps_Googleanalytics\\Handler\\ModuleHandler does not extend any class.#' + - '#Parameter \#1 \$idCategory of class Category constructor expects null, int given.#' diff --git a/tests/phpstan/phpstan-1.7.2.5.neon b/tests/phpstan/phpstan-1.7.2.5.neon index df6b779..ef8b110 100644 --- a/tests/phpstan/phpstan-1.7.2.5.neon +++ b/tests/phpstan/phpstan-1.7.2.5.neon @@ -4,3 +4,4 @@ includes: parameters: ignoreErrors: - '#PrestaShop\\Module\\Ps_Googleanalytics\\Handler\\ModuleHandler::uninstallModule\(\) calls parent::uninstall\(\) but PrestaShop\\Module\\Ps_Googleanalytics\\Handler\\ModuleHandler does not extend any class.#' + - '#Parameter \#1 \$idCategory of class Category constructor expects null, int given.#' diff --git a/tests/phpstan/phpstan-1.7.3.4.neon b/tests/phpstan/phpstan-1.7.3.4.neon index df6b779..ef8b110 100644 --- a/tests/phpstan/phpstan-1.7.3.4.neon +++ b/tests/phpstan/phpstan-1.7.3.4.neon @@ -4,3 +4,4 @@ includes: parameters: ignoreErrors: - '#PrestaShop\\Module\\Ps_Googleanalytics\\Handler\\ModuleHandler::uninstallModule\(\) calls parent::uninstall\(\) but PrestaShop\\Module\\Ps_Googleanalytics\\Handler\\ModuleHandler does not extend any class.#' + - '#Parameter \#1 \$idCategory of class Category constructor expects null, int given.#' diff --git a/tests/phpstan/phpstan-1.7.4.4.neon b/tests/phpstan/phpstan-1.7.4.4.neon index df6b779..ef8b110 100644 --- a/tests/phpstan/phpstan-1.7.4.4.neon +++ b/tests/phpstan/phpstan-1.7.4.4.neon @@ -4,3 +4,4 @@ includes: parameters: ignoreErrors: - '#PrestaShop\\Module\\Ps_Googleanalytics\\Handler\\ModuleHandler::uninstallModule\(\) calls parent::uninstall\(\) but PrestaShop\\Module\\Ps_Googleanalytics\\Handler\\ModuleHandler does not extend any class.#' + - '#Parameter \#1 \$idCategory of class Category constructor expects null, int given.#' diff --git a/tests/phpstan/phpstan-1.7.5.1.neon b/tests/phpstan/phpstan-1.7.5.1.neon index df6b779..ef8b110 100644 --- a/tests/phpstan/phpstan-1.7.5.1.neon +++ b/tests/phpstan/phpstan-1.7.5.1.neon @@ -4,3 +4,4 @@ includes: parameters: ignoreErrors: - '#PrestaShop\\Module\\Ps_Googleanalytics\\Handler\\ModuleHandler::uninstallModule\(\) calls parent::uninstall\(\) but PrestaShop\\Module\\Ps_Googleanalytics\\Handler\\ModuleHandler does not extend any class.#' + - '#Parameter \#1 \$idCategory of class Category constructor expects null, int given.#' diff --git a/tests/phpstan/phpstan-1.7.6.neon b/tests/phpstan/phpstan-1.7.6.neon index df6b779..ef8b110 100644 --- a/tests/phpstan/phpstan-1.7.6.neon +++ b/tests/phpstan/phpstan-1.7.6.neon @@ -4,3 +4,4 @@ includes: parameters: ignoreErrors: - '#PrestaShop\\Module\\Ps_Googleanalytics\\Handler\\ModuleHandler::uninstallModule\(\) calls parent::uninstall\(\) but PrestaShop\\Module\\Ps_Googleanalytics\\Handler\\ModuleHandler does not extend any class.#' + - '#Parameter \#1 \$idCategory of class Category constructor expects null, int given.#' diff --git a/upgrade/upgrade-4.1.3.php b/upgrade/upgrade-4.1.3.php new file mode 100644 index 0000000..699eea2 --- /dev/null +++ b/upgrade/upgrade-4.1.3.php @@ -0,0 +1,30 @@ + + * @copyright Since 2007 PrestaShop SA and Contributors + * @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 + */ +if (!defined('_PS_VERSION_')) { + exit; +} + +/** + * @param Ps_Googleanalytics $object + */ +function upgrade_module_4_1_3($object) +{ + return $object->unregisterHook('displayHome'); +}