Skip to content

Commit

Permalink
Merge pull request #1521 from algolia/release/3.13.3
Browse files Browse the repository at this point in the history
release/3.13.3
  • Loading branch information
cammonro authored May 29, 2024
2 parents c80d0c5 + dc9b68a commit 63566f3
Show file tree
Hide file tree
Showing 20 changed files with 234 additions and 85 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ composer.lock
vendor/
.php_cs.cache
.vscode
.idea
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
# CHANGE LOG

## 3.13.3

### Updates
- Updated CSP whitelist
- Updated code to show current version of AlgoliaSearch extension in Magento admin
- Updated code to make compatible with PHP 7
- Updated code and merged community submitted PRs
- Updated code for consistent auth tokens
- Updated code for Autocomplete highlights

### Bug Fixes
- Fixed issues with Recommend items in mobile view
- Fixed issue related to decompoundedAttributes admin config error
- Fixed issue with Analytics Overview date format bug


## 3.13.2

### Updates
Expand Down
4 changes: 2 additions & 2 deletions Helper/AlgoliaHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ public function mergeSettings($indexName, $settings, $mergeSettingsFrom = '')
} catch (\Exception $e) {
}

$removes = ['slaves', 'replicas'];
$removes = ['slaves', 'replicas', 'decompoundedAttributes'];

if (isset($settings['attributesToIndex'])) {
$settings['searchableAttributes'] = $settings['attributesToIndex'];
Expand Down Expand Up @@ -963,4 +963,4 @@ protected function getAlgoliaFiltersArrayWithoutCurrentRefinement($filters, $nee

return $filters;
}
}
}
33 changes: 23 additions & 10 deletions Helper/AnalyticsHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Algolia\AlgoliaSearch\Config\AnalyticsConfig;
use Algolia\AlgoliaSearch\DataProvider\Analytics\IndexEntityDataProvider;
use Algolia\AlgoliaSearch\RequestOptions\RequestOptionsFactory;
use Magento\Framework\Locale\ResolverInterface;

class AnalyticsHelper
{
Expand All @@ -14,9 +15,6 @@ class AnalyticsHelper
public const ANALYTICS_FILTER_PATH = '/2/filters';
public const ANALYTICS_CLICKS_PATH = '/2/clicks';

/** @var AlgoliaHelper */
private $algoliaHelper;

/** @var ConfigHelper */
private $configHelper;

Expand All @@ -26,6 +24,12 @@ class AnalyticsHelper
/** @var Logger */
private $logger;

/** @var ResolverInterface */
private $localeResolver;

public const DATE_FORMAT_PICKER = 'dd MMM yyyy';
public const DATE_FORMAT_API = 'Y-m-d';

private $searches;
private $users;
private $rateOfNoResults;
Expand Down Expand Up @@ -58,25 +62,21 @@ class AnalyticsHelper
protected $region;

/**
* @param AlgoliaHelper $algoliaHelper
* @param ConfigHelper $configHelper
* @param IndexEntityDataProvider $entityHelper
* @param Logger $logger
* @param string $region
* @param ResolverInterface $localeResolver
*/
public function __construct(
AlgoliaHelper $algoliaHelper,
ConfigHelper $configHelper,
IndexEntityDataProvider $entityHelper,
Logger $logger,
string $region = 'us'
ResolverInterface $localeResolver
) {
$this->algoliaHelper = $algoliaHelper;
$this->configHelper = $configHelper;

$this->entityHelper = $entityHelper;

$this->logger = $logger;
$this->localeResolver = $localeResolver;
$this->region = $this->configHelper->getAnalyticsRegion();
}

Expand Down Expand Up @@ -371,4 +371,17 @@ public function getErrors()
{
return $this->errors;
}

/**
* @param string $timezone
* @return \IntlDateFormatter
*/
public function getAnalyticsDatePickerFormatter(string $timezone): \IntlDateFormatter
{
$locale = $this->localeResolver->getLocale();
$dateFormatter = new \IntlDateFormatter($locale, \IntlDateFormatter::NONE, \IntlDateFormatter::NONE, $timezone);
$dateFormatter->setPattern(self::DATE_FORMAT_PICKER);
return $dateFormatter;
}

}
32 changes: 21 additions & 11 deletions Helper/Configuration/NoticeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class NoticeHelper extends \Magento\Framework\App\Helper\AbstractHelper
'getClickAnalyticsNotice',
'getPersonalizationNotice',
'getRecommendNotice',
'getCookieConfigurationNotice',
'getCookieConfigurationNotice',
];

/** @var array[] */
Expand Down Expand Up @@ -157,23 +157,33 @@ protected function getMsiNotice()
];
}

protected function getVersionNotice()
protected function getVersionNotice(): void
{
$currentVersion = $this->configHelper->getExtensionVersion();
$newVersion = $this->getNewVersionNotification();
if ($newVersion === null) {
if (!$currentVersion && !$newVersion) {
return;
}

$noticeTitle = 'Algolia Extension update';
$noticeContent = 'You are using old version of Algolia extension. Latest version of the extension is v <b>' . $newVersion['version'] . '</b><br />
$notice = [
'selector' => '.entry-edit',
'method' => 'before'
];

if ($newVersion) {
$noticeTitle = 'Algolia extension update';
$noticeContent = 'You are using an old version of Algolia extension. Latest version of the extension is v <b>' . $newVersion['version'] . '</b><br />
It is highly recommended to update your version to avoid any unexpected issues and to get new features.<br />
See details on our <a target="_blank" href="' . $newVersion['url'] . '">Github repository</a>.';
$notice['message'] = $this->formatNotice($noticeTitle, $noticeContent);
}
else {
$noticeTitle = 'Algolia extension version';
$noticeContent = "You are using version <strong>$currentVersion</strong> of the Algolia Magento integration.";
$notice['message'] = $this->formatNotice($noticeTitle, $noticeContent, 'icon-bulb');
}

$this->notices[] = [
'selector' => '.entry-edit',
'method' => 'before',
'message' => $this->formatNotice($noticeTitle, $noticeContent),
];
$this->notices[] = $notice;
}

protected function getClickAnalyticsNotice()
Expand Down Expand Up @@ -208,7 +218,7 @@ protected function getClickAnalyticsNotice()
'message' => $noticeContent,
];
}

protected function getCookieConfigurationNotice()
{
$noticeContent = '';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public function __construct(
Rule $rule,
ProductFactory $productloader,
ScopedProductTierPriceManagementInterface $productTierPrice,
Logger $logger,
Logger $logger
) {
$this->configHelper = $configHelper;
$this->customerGroupCollectionFactory = $customerGroupCollectionFactory;
Expand Down
9 changes: 8 additions & 1 deletion Helper/InsightsHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ class InsightsHelper

public const ALGOLIA_CUSTOMER_USER_TOKEN_COOKIE_NAME = 'aa-search';

/**
* Up to 129 chars per https://www.algolia.com/doc/rest-api/insights/#method-param-usertoken
* But capping at legacy 64 chars for backward compat
*/
/** @var int */
public const ALGOLIA_USER_TOKEN_MAX_LENGTH = 64;

/** @var ConfigHelper */
private $configHelper;

Expand All @@ -25,7 +32,7 @@ class InsightsHelper

/** @var SessionManagerInterface */
private $sessionManager;

/** @var CookieManagerInterface */
private $cookieManager;

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Algolia Search & Discovery extension for Magento 2
==================================================

![Latest version](https://img.shields.io/badge/latest-3.13.2-green)
![Latest version](https://img.shields.io/badge/latest-3.13.3-green)
![Magento 2](https://img.shields.io/badge/Magento-2.4.x-orange)

![PHP](https://img.shields.io/badge/PHP-8.2%2C8.1%2C7.4-blue)
Expand Down
77 changes: 60 additions & 17 deletions ViewModel/Adminhtml/Analytics/Overview.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Algolia\AlgoliaSearch\ViewModel\Adminhtml\BackendView;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Locale\ResolverInterface;
use Magento\Store\Api\Data\StoreInterface;

class Overview implements \Magento\Framework\View\Element\Block\ArgumentInterface
Expand All @@ -26,24 +27,30 @@ class Overview implements \Magento\Framework\View\Element\Block\ArgumentInterfac
/** @var IndexEntityDataProvider */
private $indexEntityDataProvider;

/** @var ResolverInterface */
private $localeResolver;

/** @var array */
private $analyticsParams = [];

/**
* Index constructor.
* Index constructor.
*
* @param BackendView $backendView
* @param AnalyticsHelper $analyticsHelper
* @param IndexEntityDataProvider $indexEntityDataProvider
* @param ResolverInterface $localeResolver
*/
public function __construct(
BackendView $backendView,
AnalyticsHelper $analyticsHelper,
IndexEntityDataProvider $indexEntityDataProvider
IndexEntityDataProvider $indexEntityDataProvider,
ResolverInterface $localeResolver
) {
$this->backendView = $backendView;
$this->analyticsHelper = $analyticsHelper;
$this->indexEntityDataProvider = $indexEntityDataProvider;
$this->localeResolver = $localeResolver;
}

/**
Expand All @@ -54,9 +61,13 @@ public function getBackendView()
return $this->backendView;
}

public function getTimeZone()
/**
* @return string
* @throws NoSuchEntityException
*/
public function getTimeZone(): string
{
return $this->backendView->getDateTime()->getConfigTimezone(
return (string) $this->backendView->getDateTime()->getConfigTimezone(
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
$this->getStore()->getId()
);
Expand All @@ -81,18 +92,16 @@ public function getIndexName()
*
* @return array
*/
public function getAnalyticsParams($additional = [])
public function getAnalyticsParams(array $additional = []): array
{
if (empty($this->analyticsParams)) {
$params = ['index' => $this->getIndexName()];
if ($formData = $this->getBackendView()->getBackendSession()->getAlgoliaAnalyticsFormData()) {
$dateTime = $this->getBackendView()->getDateTime();
$timeZone = $this->getTimeZone();
if (isset($formData['from']) && $formData['from'] !== '') {
$params['startDate'] = $dateTime->date($formData['from'], $timeZone, true, false)->format('Y-m-d');
if (!empty($formData['from'])) {
$params['startDate'] = $this->formatFormSubmittedDate($formData['from']);
}
if (isset($formData['to']) && $formData['to'] !== '') {
$params['endDate'] = $dateTime->date($formData['to'], $timeZone, true, false)->format('Y-m-d');
if (!empty($formData['to'])) {
$params['endDate'] = $this->formatFormSubmittedDate($formData['to']);
}
}

Expand All @@ -102,6 +111,39 @@ public function getAnalyticsParams($additional = [])
return array_merge($this->analyticsParams, $additional);
}

/**
* @param string $dateString
* @return string
* @throws NoSuchEntityException
*/
protected function formatFormSubmittedDate(string $dateString): string
{
$timezone = $this->getTimeZone();
$dateTime = $this->parseFormSubmittedDate($dateString, $timezone);
return $dateTime->format(AnalyticsHelper::DATE_FORMAT_API);
}

/**
* @param string|null $dateString
* @param string|null $timezone
* @return \DateTime
* @throws NoSuchEntityException
*/
protected function parseFormSubmittedDate(string $dateString = null, string $timezone = null): \DateTime
{
if (empty($timezone)) {
$timezone = $this->getTimeZone();
}

if (empty($dateString)) {
return new \DateTime('now', new \DateTimeZone($timezone));
}

$dateFormatter = $this->analyticsHelper->getAnalyticsDatePickerFormatter($timezone);
$parsedDate = $dateFormatter->parse($dateString);
return (new \DateTime('now', new \DateTimeZone($timezone)))->setTimestamp($parsedDate);
}

public function getTotalCountOfSearches()
{
return $this->analyticsHelper->getTotalCountOfSearches($this->getAnalyticsParams());
Expand Down Expand Up @@ -306,14 +348,15 @@ public function getNoResultSearches()
*
* @return bool
*/
public function checkIsValidDateRange()
public function checkIsValidDateRange(): bool
{
if ($formData = $this->getBackendView()->getBackendSession()->getAlgoliaAnalyticsFormData()) {
if (isset($formData['from']) && !empty($formData['from'])) {
$dateTime = $this->getBackendView()->getDateTime();
$timeZone = $this->getTimeZone();
$startDate = $dateTime->date($formData['from'], $timeZone, true, false);
$diff = date_diff($startDate, $dateTime->date(null, $timeZone, true, null));
if (!empty($formData['from'])) {
$timezone = $this->getTimeZone();

$startDate = $this->parseFormSubmittedDate($formData['from'], $timezone);
$now = $this->parseFormSubmittedDate(null, $timezone);
$diff = date_diff($startDate, $now);

if ($diff->days > $this->getAnalyticRetentionDays()) {
return false;
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Algolia Search & Discovery extension for Magento 2",
"type": "magento2-module",
"license": ["MIT"],
"version": "3.13.2",
"version": "3.13.3",
"require": {
"magento/framework": "~102.0|~103.0",
"algolia/algoliasearch-client-php": "3.3.2",
Expand Down
1 change: 1 addition & 0 deletions etc/csp_whitelist.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<value id="algolia" type="host">*.algolia.com</value>
<value id="algolia-fallback" type="host">*.algolianet.com</value>
<value id="algolia-insights-io" type="host">*.insights.algolia.io</value>
<value id="algolia-insights-io-bare" type="host">insights.algolia.io</value>
</values>
</policy>
</policies>
Expand Down
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Algolia_AlgoliaSearch" setup_version="3.13.2">
<module name="Algolia_AlgoliaSearch" setup_version="3.13.3">
<sequence>
<module name="Magento_Theme"/>
<module name="Magento_Backend"/>
Expand Down
2 changes: 1 addition & 1 deletion view/adminhtml/web/js/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ require(
pageWarning += '</div>';

var pageWarningSynonyms = '<div class="algolia_dashboard_warning algolia_dashboard_warning_page">';
pageWarningSynonyms += '<p>Configurations related to Synonyms have been deprecated from the Magento dashboard. We advise you to configure synonyms from the Algolia dashboard.</p>';
pageWarningSynonyms += '<p>Configurations related to Synonyms have been removed from the Magento dashboard. We advise you to configure synonyms from the Algolia dashboard.</p>';
pageWarningSynonyms += '</div>';

for (var i=0; i < pageIds.length; i++) {
Expand Down
Loading

0 comments on commit 63566f3

Please sign in to comment.