Skip to content

Commit

Permalink
Merge pull request PrestaShop#888 from M0rgan01/drop_rtl_config
Browse files Browse the repository at this point in the history
Automatic detection of rtl regeneration
  • Loading branch information
M0rgan01 authored Sep 18, 2024
2 parents 812a9da + 8dd6d04 commit 45ef54b
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 59 deletions.
26 changes: 14 additions & 12 deletions classes/Analytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class Analytics
private $anonymousId;

/**
* @var array<string, mixed>
* @var array<int, array<string, mixed>>
*/
private $properties;

Expand All @@ -62,19 +62,18 @@ class Analytics
private $state;

/**
* @param string $anonymousUserId
* @param array{'properties'?: array<string, mixed>} $options
* @param array{'properties'?: array<int, array<string, mixed>>} $options
*/
public function __construct(
UpgradeConfiguration $upgradeConfiguration,
State $state,
$anonymousUserId,
string $anonymousUserId,
array $options
) {
$this->upgradeConfiguration = $upgradeConfiguration;
$this->state = $state;

$this->anonymousId = hash('sha256', $anonymousUserId, false);
$this->anonymousId = hash('sha256', $anonymousUserId);
$this->properties = $options['properties'] ?? [];

if ($this->hasOptedOut()) {
Expand All @@ -87,10 +86,8 @@ public function __construct(
/**
* @param string $event
* @param self::WITH_*_PROPERTIES $propertiesType
*
* @return void
*/
public function track($event, $propertiesType = self::WITH_COMMON_PROPERTIES)
public function track(string $event, $propertiesType = self::WITH_COMMON_PROPERTIES): void
{
if ($this->hasOptedOut()) {
return;
Expand All @@ -108,7 +105,7 @@ public function track($event, $propertiesType = self::WITH_COMMON_PROPERTIES)
*
* @return array<string, mixed>
*/
public function getProperties($type)
public function getProperties($type): array
{
switch ($type) {
case self::WITH_UPGRADE_PROPERTIES:
Expand All @@ -120,25 +117,30 @@ public function getProperties($type)
'backup_images' => $this->upgradeConfiguration->shouldBackupImages(),
'disable_non_native_modules' => $this->upgradeConfiguration->shouldDeactivateCustomModules(),
'switch_to_default_theme' => $this->upgradeConfiguration->shouldSwitchToDefaultTheme(),
'regenerate_rtl_stylesheet' => $this->upgradeConfiguration->shouldUpdateRTLFiles(),
'keep_customized_email_templates' => $this->upgradeConfiguration->shouldKeepMails(),
];
$upgradeProperties = $this->properties[self::WITH_UPGRADE_PROPERTIES] ?? [];
$additionalProperties = array_merge($upgradeProperties, $additionalProperties);
break;
case self::WITH_ROLLBACK_PROPERTIES:
$additionalProperties = [
'from_ps_version' => $this->properties['ps_version'] ?? null,
'from_ps_version' => $this->properties[self::WITH_COMMON_PROPERTIES]['ps_version'] ?? null,
'to_ps_version' => $this->state->getRestoreVersion(),
];
$rollbackProperties = $this->properties[self::WITH_ROLLBACK_PROPERTIES] ?? [];
$additionalProperties = array_merge($rollbackProperties, $additionalProperties);
break;
default:
$additionalProperties = [];
}

$commonProperties = $this->properties[self::WITH_COMMON_PROPERTIES] ?? [];

return [
'anonymousId' => $this->anonymousId,
'channel' => 'browser',
'properties' => array_merge(
$this->properties,
$commonProperties,
$additionalProperties,
[
'module' => 'autoupgrade',
Expand Down
8 changes: 0 additions & 8 deletions classes/Parameters/UpgradeConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,14 +162,6 @@ public function shouldSwitchToDefaultTheme(): bool
return (bool) $this->get('PS_AUTOUP_CHANGE_DEFAULT_THEME');
}

/**
* @return bool True if we should update RTL files
*/
public function shouldUpdateRTLFiles(): bool
{
return (bool) $this->get('PS_AUTOUP_UPDATE_RTL_FILES');
}

public static function isOverrideAllowed(): bool
{
return (bool) Configuration::get('PS_DISABLE_OVERRIDES');
Expand Down
10 changes: 0 additions & 10 deletions classes/Twig/Form/UpgradeOptionsForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,6 @@ public function __construct(Translator $translator, FormRenderer $formRenderer)
'desc' => $translator->trans('This will change your theme: your shop will then use the default theme of the version of PrestaShop you are upgrading to.'),
],

'PS_AUTOUP_UPDATE_RTL_FILES' => [
'title' => $translator->trans('Regenerate RTL stylesheet'),
'cast' => 'intval',
'validation' => 'isBool',
'defaultValue' => '1',
'type' => 'bool',
'desc' => $translator->trans(
'If enabled, any RTL-specific files that you might have added to all your themes might be deleted by the created stylesheet.'),
],

'PS_AUTOUP_KEEP_MAILS' => [
'title' => $translator->trans('Keep the customized email templates'),
'cast' => 'intval',
Expand Down
29 changes: 25 additions & 4 deletions classes/UpgradeContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,10 +257,15 @@ public function getAnalytics(): Analytics
$this->getState(),
$this->getProperty(self::WORKSPACE_PATH), [
'properties' => [
'ps_version' => $this->getProperty(self::PS_VERSION),
'php_version' => VersionUtils::getHumanReadableVersionOf(PHP_VERSION_ID),
'autoupgrade_version' => $this->getPrestaShopConfiguration()->getModuleVersion(),
'disable_all_overrides' => class_exists('\Configuration', false) ? UpgradeConfiguration::isOverrideAllowed() : null,
Analytics::WITH_COMMON_PROPERTIES => [
'ps_version' => $this->getProperty(self::PS_VERSION),
'php_version' => VersionUtils::getHumanReadableVersionOf(PHP_VERSION_ID),
'autoupgrade_version' => $this->getPrestaShopConfiguration()->getModuleVersion(),
],
Analytics::WITH_UPGRADE_PROPERTIES => [
'disable_all_overrides' => class_exists('\Configuration', false) ? UpgradeConfiguration::isOverrideAllowed() : null,
'regenerate_rtl_stylesheet' => class_exists('\Language', false) ? $this->shouldUpdateRTLFiles() : null,
],
],
]);
}
Expand Down Expand Up @@ -722,4 +727,20 @@ public function resetOpcache(): void

opcache_reset();
}

/**
* @return bool True if we should update RTL files
*/
public function shouldUpdateRTLFiles(): bool
{
$languages = \Language::getLanguages(false);

foreach ($languages as $lang) {
if ($lang['is_rtl']) {
return true;
}
}

return false;
}
}
5 changes: 4 additions & 1 deletion classes/UpgradeTools/CoreUpgrader/CoreUpgrader.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use Cache;
use Exception;
use InvalidArgumentException;
use Language;
use ParseError;
use PrestaShop\Module\AutoUpgrade\Exceptions\UpgradeException;
use PrestaShop\Module\AutoUpgrade\Log\LoggerInterface;
Expand Down Expand Up @@ -806,7 +807,9 @@ protected function switchToDefaultTheme(): void

protected function updateRTLFiles(): void
{
if (!$this->container->getUpgradeConfiguration()->shouldUpdateRTLFiles()) {
if (!$this->container->shouldUpdateRTLFiles()) {
$this->logger->info($this->container->getTranslator()->trans('No RTL language detected, skipping RTL file update.'));

return;
}

Expand Down
8 changes: 0 additions & 8 deletions controllers/admin/AdminSelfUpgradeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,14 +233,6 @@ private function _setFields()
'type' => 'bool',
'desc' => $this->trans('Enable or disable all classes and controllers overrides.'),
],
'PS_AUTOUP_UPDATE_RTL_FILES' => [
'title' => $this->trans('Regenerate RTL stylesheet'),
'cast' => 'intval',
'validation' => 'isBool',
'defaultValue' => '1',
'type' => 'bool',
'desc' => $this->trans('If enabled, any RTL-specific files that you might have added to all your themes might be deleted by the created stylesheet.'),
],
'PS_AUTOUP_CHANGE_DEFAULT_THEME' => [
'title' => $this->trans('Switch to the default theme'),
'cast' => 'intval',
Expand Down
32 changes: 16 additions & 16 deletions tests/unit/AnalyticsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,34 +52,36 @@ public function testProperties()
'somePathToAutoupgradeModule',
[
'properties' => [
'ps_version' => '8.8.8',
'php_version' => '6.0.8',
'autoupgrade_version' => '9.8.7',
'disable_all_overrides' => true,
Analytics::WITH_COMMON_PROPERTIES => [
'ps_version' => '8.8.8',
'php_version' => '6.0.8',
'autoupgrade_version' => '9.8.7',
],
Analytics::WITH_UPGRADE_PROPERTIES => [
'disable_all_overrides' => true,
'regenerate_rtl_stylesheet' => false,
],
],
]
);

$this->assertEquals([
'anonymousId' => '3cbc0821f904fd952a8526f17b9b92a8abde4b394a66c9171cf35c9beb2b4784',
'channel' => 'browser',
'properties' => array_merge(
[
'properties' => [
'ps_version' => '8.8.8',
'php_version' => '6.0.8',
'autoupgrade_version' => '9.8.7',
'disable_all_overrides' => true,
'module' => 'autoupgrade',
]),
],
],
$analytics->getProperties(Analytics::WITH_COMMON_PROPERTIES)
);

$this->assertEquals([
'anonymousId' => '3cbc0821f904fd952a8526f17b9b92a8abde4b394a66c9171cf35c9beb2b4784',
'channel' => 'browser',
'properties' => array_merge(
[
'properties' => [
'ps_version' => '8.8.8',
'php_version' => '6.0.8',
'autoupgrade_version' => '9.8.7',
Expand All @@ -93,27 +95,25 @@ public function testProperties()
'backup_images' => false,
'disable_non_native_modules' => false,
'switch_to_default_theme' => true,
'regenerate_rtl_stylesheet' => false,
'keep_customized_email_templates' => false,
]),
'regenerate_rtl_stylesheet' => false,
],
],
$analytics->getProperties(Analytics::WITH_UPGRADE_PROPERTIES)
);

$this->assertEquals([
'anonymousId' => '3cbc0821f904fd952a8526f17b9b92a8abde4b394a66c9171cf35c9beb2b4784',
'channel' => 'browser',
'properties' => array_merge(
[
'properties' => [
'ps_version' => '8.8.8',
'php_version' => '6.0.8',
'autoupgrade_version' => '9.8.7',
'disable_all_overrides' => true,
'module' => 'autoupgrade',

'from_ps_version' => '8.8.8',
'to_ps_version' => '1.2.3',
]),
],
],
$analytics->getProperties(Analytics::WITH_ROLLBACK_PROPERTIES)
);
Expand Down

0 comments on commit 45ef54b

Please sign in to comment.