AfterMigrationNotice.php
2 years ago
BlackFridayNotice.php
2 months ago
ChangedTrackingNotice.php
3 years ago
DatabaseEngineNotice.php
1 year ago
DeprecatedFilterNotice.php
4 years ago
DisabledMailFunctionNotice.php
2 months ago
DisabledWPCronNotice.php
1 year ago
EmailWithInvalidSegmentNotice.php
3 years ago
HeadersAlreadySentNotice.php
2 months ago
InactiveSubscribersNotice.php
2 years ago
PHPVersionWarnings.php
2 months ago
PendingApprovalNotice.php
2 years ago
PermanentNotices.php
2 months ago
PremiumFeaturesAvailableNotice.php
2 years ago
SenderDomainAuthenticationNotices.php
5 months ago
SendingQueueBodyCleanupNotice.php
2 months ago
StuckPostNotificationNotice.php
2 months ago
UnauthorizedEmailInNewslettersNotice.php
5 months ago
UnauthorizedEmailNotice.php
5 months ago
WooCommerceVersionWarning.php
1 year ago
WordPressPlaygroundNotice.php
1 year ago
index.php
3 years ago
DatabaseEngineNotice.php
118 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Util\Notices; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Config\Env; |
| 9 | use MailPoet\Doctrine\WPDB\Connection; |
| 10 | use MailPoet\Util\Helpers; |
| 11 | use MailPoet\WP\Functions as WPFunctions; |
| 12 | use MailPoet\WP\Notice; |
| 13 | use MailPoetVendor\Doctrine\ORM\EntityManager; |
| 14 | |
| 15 | class DatabaseEngineNotice { |
| 16 | const OPTION_NAME = 'database-engine-notice'; |
| 17 | const DISMISS_NOTICE_TIMEOUT_SECONDS = 15_552_000; // 6 months |
| 18 | const CACHE_TIMEOUT_SECONDS = 86_400; // 1 day |
| 19 | const MAX_TABLES_TO_DISPLAY = 2; |
| 20 | |
| 21 | private WPFunctions $wp; |
| 22 | |
| 23 | private EntityManager $entityManager; |
| 24 | |
| 25 | public function __construct( |
| 26 | WPFunctions $wp, |
| 27 | EntityManager $entityManager |
| 28 | ) { |
| 29 | $this->wp = $wp; |
| 30 | $this->entityManager = $entityManager; |
| 31 | } |
| 32 | |
| 33 | public function init($shouldDisplay): ?Notice { |
| 34 | if (!$shouldDisplay || Connection::isSQLite() || $this->wp->getTransient(self::OPTION_NAME)) { |
| 35 | return null; |
| 36 | } |
| 37 | |
| 38 | try { |
| 39 | $tablesWithIncorrectEngine = $this->checkTableEngines(); |
| 40 | if ($tablesWithIncorrectEngine === []) { |
| 41 | return null; |
| 42 | } |
| 43 | |
| 44 | return $this->display($tablesWithIncorrectEngine); |
| 45 | } catch (\Exception $e) { |
| 46 | return null; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Returns a list of table names that are not using the InnoDB engine. |
| 52 | */ |
| 53 | private function checkTableEngines(): array { |
| 54 | $cacheKey = self::OPTION_NAME . '-cache'; |
| 55 | $cachedTables = $this->wp->getTransient($cacheKey); |
| 56 | if (is_array($cachedTables)) { |
| 57 | return $cachedTables; |
| 58 | } |
| 59 | |
| 60 | $tables = $this->loadTablesWithIncorrectEngines(); |
| 61 | |
| 62 | $this->wp->setTransient($cacheKey, $tables, self::CACHE_TIMEOUT_SECONDS); |
| 63 | return $tables; |
| 64 | } |
| 65 | |
| 66 | private function loadTablesWithIncorrectEngines(): array { |
| 67 | $data = $this->entityManager->getConnection()->executeQuery( |
| 68 | 'SHOW TABLE STATUS WHERE Name LIKE :prefix', |
| 69 | [ |
| 70 | 'prefix' => Env::$dbPrefix . '_%', |
| 71 | ] |
| 72 | )->fetchAllAssociative(); |
| 73 | |
| 74 | return array_map( |
| 75 | fn($row) => $row['Name'], |
| 76 | array_filter( |
| 77 | $data, |
| 78 | fn($row) => isset($row['Engine']) && is_string($row['Engine']) && (strtolower($row['Engine']) !== 'innodb') |
| 79 | ) |
| 80 | ); |
| 81 | } |
| 82 | |
| 83 | private function display(array $tablesWithIncorrectEngine): Notice { |
| 84 | // translators: %s is the list of the table names |
| 85 | $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'); |
| 86 | $tables = $this->formatTableNames($tablesWithIncorrectEngine); |
| 87 | $errorString = sprintf($errorString, $tables); |
| 88 | $error = Helpers::replaceLinkTags($errorString, 'https://kb.mailpoet.com/article/200-solving-database-connection-issues#database-configuration', [ |
| 89 | 'target' => '_blank', |
| 90 | ]); |
| 91 | |
| 92 | $extraClasses = 'mailpoet-dismissible-notice is-dismissible'; |
| 93 | |
| 94 | return Notice::displayWarning($error, $extraClasses, self::OPTION_NAME); |
| 95 | } |
| 96 | |
| 97 | private function formatTableNames(array $tablesWithIncorrectEngine): string { |
| 98 | sort($tablesWithIncorrectEngine); |
| 99 | |
| 100 | $tables = array_map( |
| 101 | fn($table) => "“{$table}”", |
| 102 | array_slice($tablesWithIncorrectEngine, 0, self::MAX_TABLES_TO_DISPLAY) |
| 103 | ); |
| 104 | |
| 105 | $remainingTablesCount = count($tablesWithIncorrectEngine) - count($tables); |
| 106 | if ($remainingTablesCount > 0) { |
| 107 | // translators: %d is the number of remaining tables, the whole string will be: "table1, table2 and 3 more" |
| 108 | $tables[] = sprintf(__('and %d more', 'mailpoet'), $remainingTablesCount); |
| 109 | } |
| 110 | |
| 111 | return implode(', ', $tables); |
| 112 | } |
| 113 | |
| 114 | public function disable() { |
| 115 | $this->wp->setTransient(self::OPTION_NAME, true, self::DISMISS_NOTICE_TIMEOUT_SECONDS); |
| 116 | } |
| 117 | } |
| 118 |