Only display two table names

[MAILPOET-6184]
This commit is contained in:
Pavel Dohnal
2024-08-12 10:18:44 +02:00
committed by Aschepikov
parent 12cdba005c
commit e9969b64ae
2 changed files with 44 additions and 3 deletions

View File

@ -11,6 +11,7 @@ use MailPoetVendor\Doctrine\ORM\EntityManager;
class DatabaseEngineNotice { class DatabaseEngineNotice {
const OPTION_NAME = 'database-engine-notice'; const OPTION_NAME = 'database-engine-notice';
const DISMISS_NOTICE_TIMEOUT_SECONDS = 15_552_000; // 6 months const DISMISS_NOTICE_TIMEOUT_SECONDS = 15_552_000; // 6 months
const MAX_TABLES_TO_DISPLAY = 2;
private WPFunctions $wp; private WPFunctions $wp;
@ -25,7 +26,6 @@ class DatabaseEngineNotice {
} }
//TODO: check only once a day //TODO: check only once a day
//TODO: display better list of table names (“wp_mailpoet_settings”, “wp_mailpoet_feature_flags”, and 7 more)
public function init($shouldDisplay): ?Notice { public function init($shouldDisplay): ?Notice {
if (!$shouldDisplay || $this->wp->getTransient(self::OPTION_NAME)) { if (!$shouldDisplay || $this->wp->getTransient(self::OPTION_NAME)) {
return null; return null;
@ -61,8 +61,8 @@ class DatabaseEngineNotice {
private function display(array $tablesWithIncorrectEngine): ?Notice { private function display(array $tablesWithIncorrectEngine): ?Notice {
// translators: %s is the list of the table names // translators: %s is the list of the table names
$errorString = __('Some of the MailPoet plugins tables are not using the InnoDB engine (%s). This may cause performance and compatibility issues. Please ensure all MailPoet tables are converted to use the InnoDB engine. For more information, check out [link]this guide[/link].', 'mailpoet'); $errorString = __('Some of the MailPoet plugins tables are not using the InnoDB engine (%s). This may cause performance and compatibility issues. Please ensure all MailPoet tables are converted to use the InnoDB engine. For more information, check out [link]this guide[/link].', 'mailpoet');
$tables = implode(", ", $tablesWithIncorrectEngine); $tables = $this->formatTableNames($tablesWithIncorrectEngine);
$errorString = sprintf($errorString, $tables); $errorString = sprintf($errorString, $tables);
$error = Helpers::replaceLinkTags($errorString, 'https://kb.mailpoet.com/article/200-solving-database-connection-issues#database-configuration', [ $error = Helpers::replaceLinkTags($errorString, 'https://kb.mailpoet.com/article/200-solving-database-connection-issues#database-configuration', [
'target' => '_blank', 'target' => '_blank',
@ -73,6 +73,23 @@ class DatabaseEngineNotice {
return Notice::displayWarning($error, $extraClasses, self::OPTION_NAME); return Notice::displayWarning($error, $extraClasses, self::OPTION_NAME);
} }
private function formatTableNames(array $tablesWithIncorrectEngine): string {
sort($tablesWithIncorrectEngine);
$tables = array_map(
fn($table) => "${table}",
array_slice($tablesWithIncorrectEngine, 0, self::MAX_TABLES_TO_DISPLAY)
);
$remainingTablesCount = count($tablesWithIncorrectEngine) - count($tables);
if ($remainingTablesCount > 0) {
// translators: %d is the number of remaining tables, the whole string will be: "table1, table2 and 3 more"
$tables[] = sprintf(__('and %d more', 'mailpoet'), $remainingTablesCount);
}
return implode(', ', $tables);
}
public function disable() { public function disable() {
$this->wp->setTransient(self::OPTION_NAME, true, self::DISMISS_NOTICE_TIMEOUT_SECONDS); $this->wp->setTransient(self::OPTION_NAME, true, self::DISMISS_NOTICE_TIMEOUT_SECONDS);
} }

View File

@ -2,7 +2,10 @@
namespace MailPoet\Util\Notices; namespace MailPoet\Util\Notices;
use MailPoet\Entities\FormEntity;
use MailPoet\Entities\NewsletterEntity;
use MailPoet\Entities\SegmentEntity; use MailPoet\Entities\SegmentEntity;
use MailPoet\Entities\SubscriberEntity;
use MailPoet\WP\Functions as WPFunctions; use MailPoet\WP\Functions as WPFunctions;
class DatabaseEngineNoticeTest extends \MailPoetTest { class DatabaseEngineNoticeTest extends \MailPoetTest {
@ -41,6 +44,27 @@ class DatabaseEngineNoticeTest extends \MailPoetTest {
verify($message)->stringContainsString($this->tableName); verify($message)->stringContainsString($this->tableName);
} }
public function testItDisplaysNoticeWithMultipleTables() {
$tables = [
$this->entityManager->getClassMetadata(FormEntity::class)->getTableName(),
$this->entityManager->getClassMetadata(NewsletterEntity::class)->getTableName(),
$this->tableName,
$this->entityManager->getClassMetadata(SubscriberEntity::class)->getTableName(),
];
foreach ($tables as $table) {
$this->entityManager->getConnection()->executeStatement("ALTER TABLE {$table} ENGINE = MyISAM;");
}
$result = $this->notice->init(true);
verify($result)->notEmpty();
$message = $result->getMessage();
verify($message)->stringContainsString('and 2 more');
verify($message)->stringContainsString('“' . $tables[0] . '”');
foreach ($tables as $table) {
$this->entityManager->getConnection()->executeStatement("ALTER TABLE {$table} ENGINE = INNODB;");
}
}
public function testItDoesntDisplayWhenDisabled() { public function testItDoesntDisplayWhenDisabled() {
$this->notice->disable(); $this->notice->disable();
$result = $this->notice->init(true); $result = $this->notice->init(true);