diff --git a/mailpoet/lib/Util/Notices/DatabaseEngineNotice.php b/mailpoet/lib/Util/Notices/DatabaseEngineNotice.php index fd8fd9c4eb..d2a644338f 100644 --- a/mailpoet/lib/Util/Notices/DatabaseEngineNotice.php +++ b/mailpoet/lib/Util/Notices/DatabaseEngineNotice.php @@ -11,6 +11,7 @@ use MailPoetVendor\Doctrine\ORM\EntityManager; class DatabaseEngineNotice { const OPTION_NAME = 'database-engine-notice'; const DISMISS_NOTICE_TIMEOUT_SECONDS = 15_552_000; // 6 months + const MAX_TABLES_TO_DISPLAY = 2; private WPFunctions $wp; @@ -25,7 +26,6 @@ class DatabaseEngineNotice { } //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 { if (!$shouldDisplay || $this->wp->getTransient(self::OPTION_NAME)) { return null; @@ -61,8 +61,8 @@ class DatabaseEngineNotice { private function display(array $tablesWithIncorrectEngine): ?Notice { // translators: %s is the list of the table names - $errorString = __('Some of the MailPoet plugin’s 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); + $errorString = __('Some of the MailPoet plugin’s 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 = $this->formatTableNames($tablesWithIncorrectEngine); $errorString = sprintf($errorString, $tables); $error = Helpers::replaceLinkTags($errorString, 'https://kb.mailpoet.com/article/200-solving-database-connection-issues#database-configuration', [ 'target' => '_blank', @@ -73,6 +73,23 @@ class DatabaseEngineNotice { 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() { $this->wp->setTransient(self::OPTION_NAME, true, self::DISMISS_NOTICE_TIMEOUT_SECONDS); } diff --git a/mailpoet/tests/integration/Util/Notices/DatabaseEngineNoticeTest.php b/mailpoet/tests/integration/Util/Notices/DatabaseEngineNoticeTest.php index 548fd0702e..a25e75dadf 100644 --- a/mailpoet/tests/integration/Util/Notices/DatabaseEngineNoticeTest.php +++ b/mailpoet/tests/integration/Util/Notices/DatabaseEngineNoticeTest.php @@ -2,7 +2,10 @@ namespace MailPoet\Util\Notices; +use MailPoet\Entities\FormEntity; +use MailPoet\Entities\NewsletterEntity; use MailPoet\Entities\SegmentEntity; +use MailPoet\Entities\SubscriberEntity; use MailPoet\WP\Functions as WPFunctions; class DatabaseEngineNoticeTest extends \MailPoetTest { @@ -41,6 +44,27 @@ class DatabaseEngineNoticeTest extends \MailPoetTest { 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() { $this->notice->disable(); $result = $this->notice->init(true);