$oldSite) { // Migrate old site to new format if (!array_key_exists($oldSiteSlug, $allStagingSites)) { $allStagingSites[$oldSiteSlug] = $oldSite; continue; } // If key exists and path matches, skip if ($allStagingSites[$oldSiteSlug]['path'] === $oldSite['path']) { continue; } // Migrate old site to new format when site slug exists in both options $i = 0; do { $oldSiteSlug = $oldSiteSlug . '_' . $i; } while (array_key_exists($oldSiteSlug, $allStagingSites)); $allStagingSites[$oldSiteSlug] = $oldSite; } if (update_option(self::STAGING_SITES_OPTION, $allStagingSites)) { // Keep a backup just in case update_option(self::BACKUP_STAGING_SITES_OPTION, $oldSitesOption, false); delete_option(self::OLD_STAGING_SITES_OPTION); } } /** * Will try getting staging sites from new option * If that is empty, will get staging sites from old option * * @return array */ public function tryGettingStagingSites() { $stagingSites = get_option(self::STAGING_SITES_OPTION, []); if (!empty($stagingSites)) { return $stagingSites; } return get_option(self::OLD_STAGING_SITES_OPTION, []); } /** * Update staging sites option * * @param array $stagingSites * @return bool */ public function updateStagingSites($stagingSites) { return update_option(self::STAGING_SITES_OPTION, $stagingSites); } /** * Upgrade the staging site data structure, add the missing cloneName, if not present */ public function addMissingCloneNameUpgradeStructure() { $isAdded = get_option(self::MISSING_CLONE_NAME_ROUTINE_EXECUTED, false); if ($isAdded) { return; } // Current options $sites = $this->tryGettingStagingSites(); // Early bail if no sites if (empty($sites)) { update_option(self::MISSING_CLONE_NAME_ROUTINE_EXECUTED, true); return; } // Add missing cloneName if not exists foreach ($sites as $key => $site) { if (isset($sites[$key]['cloneName'])) { continue; } $sites[$key]['cloneName'] = $sites[$key]['directoryName']; } $this->updateStagingSites($sites); update_option(self::MISSING_CLONE_NAME_ROUTINE_EXECUTED, true); } /** * Sanitize the clone name to be used as directory * * @param string $cloneName * @return string */ public function sanitizeDirectoryName($cloneName) { $cloneDirectoryName = preg_replace("#\W+#", '-', strtolower($cloneName)); return substr($cloneDirectoryName, 0, 16); } /** * Return false if site not exists else return reason behind existing * * @param string $directoryName * @return bool|string */ public function isCloneExists($directoryName) { $cloneDirectoryPath = trailingslashit(get_home_path()) . $directoryName; if (!wpstg_is_empty_dir($cloneDirectoryPath)) { return sprintf(__("Warning: Use another site name! Clone destination directory %s already exists and is not empty. As default, WP STAGING uses the site name as subdirectory for the clone.", 'wp-staging'), $cloneDirectoryPath); } $stagingSites = $this->tryGettingStagingSites(); foreach ($stagingSites as $site) { if ($site['directoryName'] === $directoryName) { return __("Site name is already in use, please choose another name for the staging site.", "wp-staging"); } } return false; } /** * @return array */ public function getStagingDirectories(): array { $stagingSites = $this->tryGettingStagingSites(); return wp_list_pluck($stagingSites, 'path'); } }