Database
1 day ago
AbstractStagingSetup.php
1 day ago
DirectoryScanner.php
1 day ago
FileCopier.php
1 day ago
LegacyOptionsCache.php
1 day ago
StagingEngine.php
1 day ago
StagingSetup.php
1 day ago
TableScanner.php
1 day ago
TableScanner.php
164 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Staging\Service; |
| 4 | |
| 5 | use WPStaging\Framework\Adapter\Database; |
| 6 | use WPStaging\Framework\Collection\Collection; |
| 7 | use WPStaging\Framework\Database\TableDto; |
| 8 | use WPStaging\Framework\Database\TableService; |
| 9 | use WPStaging\Framework\TemplateEngine\TemplateEngine; |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | class TableScanner |
| 15 | { |
| 16 | |
| 17 | const STAGING_TABLE_PREFIX = 'wpstg'; |
| 18 | |
| 19 | |
| 20 | const FILTER_SHOW_STAGING_TABLES = 'wpstg_show_staging_tables_in_staging_setup'; |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | protected $templateEngine; |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | protected $database; |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | protected $tableService; |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | protected $stagingSetup; |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | protected $excludedTables = []; |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | protected $currentTables = []; |
| 51 | |
| 52 | public function __construct(TemplateEngine $templateEngine, Database $database, TableService $tableService) |
| 53 | { |
| 54 | $this->templateEngine = $templateEngine; |
| 55 | $this->database = $database; |
| 56 | $this->tableService = $tableService; |
| 57 | } |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | public function setStagingSetup(AbstractStagingSetup $stagingSetup) |
| 63 | { |
| 64 | $this->stagingSetup = $stagingSetup; |
| 65 | } |
| 66 | |
| 67 | public function renderTablesSelection() |
| 68 | { |
| 69 | $this->scanTables(); |
| 70 | $result = $this->templateEngine->render('staging/_partials/tables-selection.php', [ |
| 71 | 'dbPrefix' => $this->database->getPrefix(), |
| 72 | 'stagingSetup' => $this->stagingSetup, |
| 73 | 'stagingSiteDto' => $this->stagingSetup->getStagingSiteDto(), |
| 74 | 'tables' => $this->currentTables, |
| 75 | 'excludedTables' => $this->excludedTables, |
| 76 | ]); |
| 77 | |
| 78 | echo $result; // phpcs:ignore |
| 79 | } |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | protected function scanTables() |
| 85 | { |
| 86 | |
| 87 | |
| 88 | |
| 89 | $tables = $this->tableService->findAllTableStatus(); |
| 90 | $dbPrefix = $this->database->getPrefix(); |
| 91 | $showStagingTables = $this->shouldShowStagingTables($dbPrefix); |
| 92 | |
| 93 | |
| 94 | $this->excludedTables = []; |
| 95 | $this->currentTables = []; |
| 96 | |
| 97 | foreach ($tables as $table) { |
| 98 | $tableName = $table->getName(); |
| 99 | if ($table->getIsView() || !$this->shouldRenderTable($dbPrefix, $tableName, $showStagingTables)) { |
| 100 | continue; |
| 101 | } |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | if ($this->isTableExcluded($dbPrefix, $tableName)) { |
| 107 | $this->excludedTables[] = $tableName; |
| 108 | } |
| 109 | |
| 110 | $this->currentTables[] = $table; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | protected function shouldRenderTable(string $dbPrefix, string $tableName, bool $showStagingTables): bool |
| 115 | { |
| 116 | if ($showStagingTables) { |
| 117 | return true; |
| 118 | } |
| 119 | |
| 120 | if (!$this->isStagingTable($tableName)) { |
| 121 | return true; |
| 122 | } |
| 123 | |
| 124 | return $this->isCurrentSiteTable($dbPrefix, $tableName); |
| 125 | } |
| 126 | |
| 127 | protected function shouldShowStagingTables(string $dbPrefix): bool |
| 128 | { |
| 129 | return (bool) apply_filters(self::FILTER_SHOW_STAGING_TABLES, false, $dbPrefix, $this->stagingSetup); |
| 130 | } |
| 131 | |
| 132 | protected function isStagingTable(string $tableName): bool |
| 133 | { |
| 134 | return strpos($tableName, self::STAGING_TABLE_PREFIX) === 0; |
| 135 | } |
| 136 | |
| 137 | protected function isCurrentSiteTable(string $dbPrefix, string $tableName): bool |
| 138 | { |
| 139 | return !empty($dbPrefix) && strpos($tableName, $dbPrefix) === 0; |
| 140 | } |
| 141 | |
| 142 | protected function isTableExcluded(string $dbPrefix, string $tableName): bool |
| 143 | { |
| 144 | if ((!empty($dbPrefix) && strpos($tableName, $dbPrefix) !== 0)) { |
| 145 | return true; |
| 146 | } |
| 147 | |
| 148 | if (!$this->isMultisiteMainSite()) { |
| 149 | return false; |
| 150 | } |
| 151 | |
| 152 | if ($this->stagingSetup->getStagingSiteDto()->getNetworkClone()) { |
| 153 | return false; |
| 154 | } |
| 155 | |
| 156 | return preg_match('/^' . $dbPrefix . '\d+_/', $tableName); |
| 157 | } |
| 158 | |
| 159 | protected function isMultisiteMainSite(): bool |
| 160 | { |
| 161 | return is_multisite() && is_main_site(); |
| 162 | } |
| 163 | } |
| 164 |