From 1ac7f62642935f967af0a4a387f858561a82a02c Mon Sep 17 00:00:00 2001 From: PoulainMaxime Date: Thu, 12 Dec 2024 09:47:23 +0100 Subject: [PATCH 1/5] feat: resolve compatibility prestashop v9 --- .../AdminAjaxPsxMktgWithGoogleController.php | 177 +++++++++--------- views/templates/admin/app.tpl | 10 + 2 files changed, 96 insertions(+), 91 deletions(-) diff --git a/controllers/admin/AdminAjaxPsxMktgWithGoogleController.php b/controllers/admin/AdminAjaxPsxMktgWithGoogleController.php index f5f90f1fe..7a071d8f1 100644 --- a/controllers/admin/AdminAjaxPsxMktgWithGoogleController.php +++ b/controllers/admin/AdminAjaxPsxMktgWithGoogleController.php @@ -156,43 +156,40 @@ public function displayAjax() $this->listMatchingProductsFromFilters($inputs); break; default: - http_response_code(400); - $this->ajaxDie(json_encode(['success' => false, 'message' => $this->l('Action is missing or incorrect.')])); + $this->render(json_encode(['success' => false, 'message' => $this->module->getTranslator('Action is missing or incorrect.')]), 400); } } private function setWebsiteVerificationMeta(array $inputs) { if (!isset($inputs['websiteVerificationMeta'])) { - http_response_code(400); - $this->ajaxDie(json_encode([ + $this->render(json_encode([ 'success' => false, 'message' => 'Missing Meta key', - ])); + ]), 400); } $websiteVerificationMeta = $inputs['websiteVerificationMeta']; $this->configurationAdapter->deleteByName(Config::PSX_MKTG_WITH_GOOGLE_WEBSITE_VERIFICATION_META); if ($websiteVerificationMeta === false) { - $this->ajaxDie(json_encode(['success' => true, 'method' => 'delete'])); + $this->render(json_encode(['success' => true, 'method' => 'delete']), 200); } else { // base64 encoded to avoid prestashop sanitization $this->configurationAdapter->updateValue( Config::PSX_MKTG_WITH_GOOGLE_WEBSITE_VERIFICATION_META, base64_encode($websiteVerificationMeta) ); - $this->ajaxDie(json_encode(['success' => true, 'method' => 'insert'])); + $this->render(json_encode(['success' => true, 'method' => 'insert']), 200); } } private function setGMCInformations(array $inputs) { if (!isset($inputs['gmcInformations'])) { - http_response_code(400); - $this->ajaxDie(json_encode([ + $this->render(json_encode([ 'success' => false, 'message' => 'missing gmc informations', - ])); + ]), 400); } $informations = $inputs['gmcInformations']; @@ -201,17 +198,17 @@ private function setGMCInformations(array $inputs) $informations['id'] ); - $this->ajaxDie(json_encode(['success' => true])); + $this->render(json_encode(['success' => true]), 200); } private function getProductsReadyToSync(array $inputs) { - $this->ajaxDie(json_encode([ + $this->render(json_encode([ 'total' => $this->productRepository->getProductsTotal( $this->context->shop->id, ['onlyActive' => true] ), - ])); + ]), 200); } private function getShopConfigurationForGMC() @@ -240,7 +237,7 @@ private function getShopConfigurationForGMC() $data['store']['region'] = State::getNameById($this->configurationAdapter->get('PS_SHOP_STATE_ID')); } - $this->ajaxDie(json_encode($data)); + $this->render(json_encode($data), 200); } private function getCarrierValues() @@ -250,7 +247,7 @@ private function getCarrierValues() $carrierLines = $carrierDataProvider->getFormattedData(); - $this->ajaxDie(json_encode($carrierLines)); + $this->render(json_encode($carrierLines), 200); } private function getWebsiteRequirementStatus() @@ -258,19 +255,18 @@ private function getWebsiteRequirementStatus() $requirements = json_decode($this->configurationAdapter->get(Config::PSX_MKTG_WITH_GOOGLE_WEBSITE_REQUIREMENTS_STATUS)) ?: []; - $this->ajaxDie(json_encode([ + $this->render(json_encode([ 'requirements' => $requirements, - ])); + ]), 200); } private function setWebsiteRequirementStatus(array $inputs) { if (!isset($inputs['requirements']) || !is_array($inputs['requirements'])) { - http_response_code(400); - $this->ajaxDie(json_encode([ + $this->render(json_encode([ 'success' => false, 'message' => 'Missing requirements key or value must be an array', - ])); + ]), 400); } $requirements = $inputs['requirements']; @@ -286,10 +282,10 @@ private function setWebsiteRequirementStatus(array $inputs) foreach ($requirements as $value) { if (!in_array($value, $allowedKeys)) { - $this->ajaxDie(json_encode([ + $this->render(json_encode([ 'success' => false, 'message' => 'Unknown requirement key ' . $value, - ])); + ]), 400); } } @@ -298,7 +294,7 @@ private function setWebsiteRequirementStatus(array $inputs) json_encode($requirements) ); - $this->ajaxDie(json_encode(['success' => true])); + $this->render(json_encode(['success' => true]), 200); } public function getShopConfigurationForAds() @@ -306,13 +302,13 @@ public function getShopConfigurationForAds() $defaultTimeZone = date_default_timezone_get(); $timeZone = new DateTime('now', new DateTimeZone($defaultTimeZone)); - $this->ajaxDie(json_encode([ + $this->render(json_encode([ 'timezone' => [ 'offset' => $timeZone->format('P'), 'text' => $defaultTimeZone, ], 'currency' => $this->currencyRepository->getShopCurrency()['isoCode'], - ])); + ]), 200); } private function getRemarketingTagsStatus() @@ -325,20 +321,19 @@ private function getRemarketingTagsStatus() null // Default value ); - $this->ajaxDie(json_encode([ + $this->render(json_encode([ 'remarketingTagsStatus' => (bool) $this->configurationAdapter->get(Config::PSX_MKTG_WITH_GOOGLE_REMARKETING_STATUS), 'enhancedConversionStatus' => $enhancedConversionStatus !== null ? (bool) $enhancedConversionStatus : null, - ])); + ]), 200); } private function toggleRemarketingTags(array $inputs) { if (!isset($inputs['isRemarketingEnabled']) || !isset($inputs['tagSnippet'])) { - http_response_code(400); - $this->ajaxDie(json_encode([ + $this->render(json_encode([ 'success' => false, 'message' => 'Missing isRemarketingEnabled or tagSnippet key', - ])); + ]), 400); } if ((bool) $inputs['isRemarketingEnabled']) { @@ -354,17 +349,16 @@ private function toggleRemarketingTags(array $inputs) $this->configurationAdapter->deleteByName(Config::PSX_MKTG_WITH_GOOGLE_REMARKETING_STATUS); $this->configurationAdapter->deleteByName(Config::PSX_MKTG_WITH_GOOGLE_REMARKETING_TAG); } - $this->ajaxDie(json_encode(['success' => true])); + $this->render(json_encode(['success' => true]), 200); } private function setEnhancedConversions(array $inputs) { if (!isset($inputs['enable'])) { - http_response_code(400); - $this->ajaxDie(json_encode([ + $this->render(json_encode([ 'success' => false, 'message' => 'Missing enable key', - ])); + ]), 400); } /** @var EnhancedConversionToggle $enhancedConversionToggle */ $enhancedConversionToggle = $this->module->getService(EnhancedConversionToggle::class); @@ -375,38 +369,35 @@ private function setEnhancedConversions(array $inputs) $success = $enhancedConversionToggle->disable(); } - $this->ajaxDie(json_encode([ + $this->render(json_encode([ 'success' => $success, - ])); + ]), 200); } private function setConversionActionLabel(array $inputs) { if (!isset($inputs['conversionActions'])) { - http_response_code(400); - $this->ajaxDie(json_encode([ + $this->render(json_encode([ 'success' => false, 'message' => 'Missing conversionActions key', - ])); + ]), 400); } $newTags = []; foreach ($inputs['conversionActions'] as $index => $conversionAction) { if (!isset($conversionAction['category']) || !isset($conversionAction['tag'])) { - http_response_code(400); - $this->ajaxDie(json_encode([ + $this->render(json_encode([ 'success' => false, 'message' => 'Missing category or tag key at index ' . $index, - ])); + ]), 400); } if (!in_array($conversionAction['category'], Config::REMARKETING_CONVERSION_LABELS)) { - http_response_code(400); - $this->ajaxDie(json_encode([ + $this->render(json_encode([ 'success' => false, 'message' => 'Unhandled conversion category at index ' . $index, - ])); + ]), 400); } $newTags[$conversionAction['category']] = $conversionAction['tag']; } @@ -420,7 +411,7 @@ private function setConversionActionLabel(array $inputs) ) ) ); - $this->ajaxDie(json_encode(['success' => true])); + $this->render(json_encode(['success' => true]), 200); } private function getConversionActionLabels() @@ -433,9 +424,9 @@ private function getConversionActionLabels() 'tag' => $tag, ]; } - $this->ajaxDie(json_encode([ + $this->render(json_encode([ 'conversionActionLabels' => $labels, - ])); + ]), 200); } private function getDebugData() @@ -456,21 +447,22 @@ private function getDebugData() ); } - $this->ajaxDie(json_encode([ + $this->render(json_encode([ 'urlEventBusHealthCheck' => $this->context->link->getModuleLink( 'ps_eventbus', 'apiHealthCheck' ), 'typesOfSync' => $typesOfSync, - ])); + ]), 200); } public function getShopAttributes() { - $this->ajaxDie( + $this->render( json_encode( $this->attributesRepository->getAllAttributes() - ) + ), + 200 ); } @@ -481,11 +473,10 @@ public function getShopAttributes() public function getProductFilterOptions(array $inputs) { if (!isset($inputs['kind'])) { - http_response_code(400); - $this->ajaxDie(json_encode([ + $this->render(json_encode([ 'success' => false, 'message' => 'Missing kind key', - ])); + ]), 400); } $attributeKind = $inputs['kind']; @@ -498,28 +489,27 @@ public function getProductFilterOptions(array $inputs) /** @var OptionsProviderInterface $optionsProvider */ $optionsProvider = $this->module->getService($providerName); - $this->ajaxDie( + $this->render( json_encode( $optionsProvider->getOptions() - ) + ), + 200 ); } catch (InvalidArgumentException $e) { - http_response_code(400); - $this->ajaxDie(json_encode([ + $this->render(json_encode([ 'success' => false, 'message' => $e->getMessage(), - ])); + ]), 400); } } public function countMatchingProductsFromFilters(array $inputs) { if (!isset($inputs['filters'])) { - http_response_code(400); - $this->ajaxDie(json_encode([ + $this->render(json_encode([ 'success' => false, 'message' => 'Missing filters key', - ])); + ]), 400); } $filters = $inputs['filters']; @@ -527,21 +517,21 @@ public function countMatchingProductsFromFilters(array $inputs) /** @var ProductEnumerator $productEnumerator */ $productEnumerator = $this->module->getService(ProductEnumerator::class); - $this->ajaxDie( + $this->render( json_encode([ 'numberOfProducts' => $productEnumerator->countProductsMatchingFilters($filters), - ]) + ]), + 200 ); } public function listMatchingProductsFromFilters(array $inputs) { if (!isset($inputs['filters'])) { - http_response_code(400); - $this->ajaxDie(json_encode([ + $this->render(json_encode([ 'success' => false, 'message' => 'Missing filters key', - ])); + ]), 400); } $filters = $inputs['filters']; @@ -549,37 +539,29 @@ public function listMatchingProductsFromFilters(array $inputs) /** @var ProductEnumerator $productEnumerator */ $productEnumerator = $this->module->getService(ProductEnumerator::class); - $this->ajaxDie( - json_encode($productEnumerator->listProductsMatchingFilters($filters, [])) + $this->render( + json_encode($productEnumerator->listProductsMatchingFilters($filters, [])), + 200 ); } private function getModuleStatus(array $inputs) { if (!isset($inputs['moduleName'])) { - http_response_code(400); - $this->ajaxDie(json_encode([ + $this->render(json_encode([ 'success' => false, 'message' => 'Missing moduleName key', - ])); + ]), 400); } - $this->ajaxDie( + $this->render( json_encode( (new ModuleRepository($inputs['moduleName']))->getInformationsAboutModule() - ) + ), + 200 ); } - /** - * {@inheritdoc} - */ - protected function ajaxDie($value = null, $controller = null, $method = null) - { - header('Content-Type: application/json'); - parent::ajaxDie($value, $controller, $method); - } - /** * Retrieve the faq */ @@ -587,14 +569,15 @@ public function retrieveFaq() { $faq = new Faq($this->module->module_key, _PS_VERSION_, $this->context->language->iso_code); - $this->ajaxDie( + $this->render( json_encode( [ 'faq' => $faq->getFaq(), 'doc' => $this->getUserDocumentation(), 'contactUs' => 'support-google@prestashop.com', ] - ) + ), + 200 ); } @@ -618,17 +601,17 @@ private function getUserDocumentation() private function registerHook(array $inputs) { if (!isset($inputs['hookName'])) { - http_response_code(400); - $this->ajaxDie(json_encode([ + $this->render(json_encode([ 'success' => false, 'message' => 'Missing hookName key', - ])); + ]), 400); } - $this->ajaxDie( + $this->render( json_encode([ 'success' => $this->module->registerHook($inputs['hookName']), - ]) + ]), + 200 ); } @@ -650,4 +633,16 @@ private function checkFileExist($docUrl) return $retcode < 400; } + + /** + * {@inheritdoc} + */ + private function render($response, $code) + { + header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0'); + header('Content-Type: application/json;charset=utf-8'); + header("HTTP/1.1 $code"); + + parent::ajaxRender($response); + } } diff --git a/views/templates/admin/app.tpl b/views/templates/admin/app.tpl index 40cd83d94..39520eba0 100644 --- a/views/templates/admin/app.tpl +++ b/views/templates/admin/app.tpl @@ -40,3 +40,13 @@ display: none; } + + From e7618850d42d7b4046456254fddd2ada8f9be3bd Mon Sep 17 00:00:00 2001 From: PoulainMaxime Date: Thu, 12 Dec 2024 09:57:17 +0100 Subject: [PATCH 2/5] feat: add conditionnal to use new method only on v9 --- controllers/admin/AdminAjaxPsxMktgWithGoogleController.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/controllers/admin/AdminAjaxPsxMktgWithGoogleController.php b/controllers/admin/AdminAjaxPsxMktgWithGoogleController.php index 7a071d8f1..a7260e027 100644 --- a/controllers/admin/AdminAjaxPsxMktgWithGoogleController.php +++ b/controllers/admin/AdminAjaxPsxMktgWithGoogleController.php @@ -643,6 +643,11 @@ private function render($response, $code) header('Content-Type: application/json;charset=utf-8'); header("HTTP/1.1 $code"); - parent::ajaxRender($response); + if (_PS_VERSION_ === '9.0.0') { + parent::ajaxRender($response); + exit; + } + + parent::ajaxDie($response, null, null); } } From 554da4ff3839d26e7cf8bc592b38ca242177c401 Mon Sep 17 00:00:00 2001 From: PoulainMaxime Date: Thu, 12 Dec 2024 10:01:20 +0100 Subject: [PATCH 3/5] feat: use module translation --- controllers/admin/AdminAjaxPsxMktgWithGoogleController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/controllers/admin/AdminAjaxPsxMktgWithGoogleController.php b/controllers/admin/AdminAjaxPsxMktgWithGoogleController.php index a7260e027..25eab1aed 100644 --- a/controllers/admin/AdminAjaxPsxMktgWithGoogleController.php +++ b/controllers/admin/AdminAjaxPsxMktgWithGoogleController.php @@ -156,7 +156,7 @@ public function displayAjax() $this->listMatchingProductsFromFilters($inputs); break; default: - $this->render(json_encode(['success' => false, 'message' => $this->module->getTranslator('Action is missing or incorrect.')]), 400); + $this->render(json_encode(['success' => false, 'message' => $this->module->l('Action is missing or incorrect.')]), 400); } } From ee8f53ea6cbb45ae057420c168fdc4bc83b5d35a Mon Sep 17 00:00:00 2001 From: PoulainMaxime Date: Thu, 12 Dec 2024 10:32:43 +0100 Subject: [PATCH 4/5] feat: add comment on app.tpl --- views/templates/admin/app.tpl | 1 + 1 file changed, 1 insertion(+) diff --git a/views/templates/admin/app.tpl b/views/templates/admin/app.tpl index 39520eba0..04fd91030 100644 --- a/views/templates/admin/app.tpl +++ b/views/templates/admin/app.tpl @@ -42,6 +42,7 @@