Scope => the stages it runs, in pipeline order. */ private const STAGES = array( self::PLUGINS => array( 'plugins' ), self::THEMES => array( 'themes' ), self::MEDIA => array( 'media' ), self::DATABASE => array( 'prepare', 'swap' ), self::FULL => array( 'prepare', 'media', 'plugins', 'themes', 'swap' ), ); /** @var string[] Every tool: each removes or replaces something a network shares between its sites. */ private const NETWORK_SHARED = array( self::PLUGINS, self::THEMES, self::MEDIA, self::DATABASE, self::FULL ); /** * Whether this tool must not run here. Plugins and themes are shared by every site on a network, and the * database reset swaps the base-prefix tables, which hold the network's users, its plugin activation * (active_sitewide_plugins) and the settings of the other sites, and the media clean-up empties * wp-content/uploads, which on a network also holds every other site's files (uploads/sites/N). * None of that is one site's to wipe, so every tool is refused on a network. */ public static function blockedOnNetwork( string $scope ): bool { return is_multisite() && in_array( $scope, self::NETWORK_SHARED, true ); } public static function networkMessage( string $scope = '' ): string { if ( self::MEDIA === $scope ) { return __( 'Media Clean-Up is disabled on multisite networks. It empties wp-content/uploads, which on a network also holds the files of every other site, so it cannot be run from a single site.', 'migratex' ); } if ( self::DATABASE === $scope ) { return __( 'Database Reset is disabled on multisite networks. It replaces the shared network tables (users, network-activated plugins and the settings of every other site), so it cannot be run from a single site. Export and restore a single site with MigrateX instead, or reset from the command line with the whole network in mind.', 'migratex' ); } return __( 'Plugins and themes are shared by every site on a multisite network, so they cannot be purged or reset from a single site. Manage them from the Network Admin instead.', 'migratex' ); } /** * @return string[] */ public static function all(): array { return array_keys( self::STAGES ); } public static function isValid( string $scope ): bool { return isset( self::STAGES[ $scope ] ); } public static function runs( string $scope, string $stage ): bool { return in_array( $stage, self::STAGES[ $scope ] ?? array(), true ); } /** * Entry names inside wp-content/plugins that a reset must leave alone: this plugin, and the * folder index. When the plugin is a single file at the top of the folder, that file. * * @return string[] */ public static function pluginsToKeep(): array { $own = Exclusions::ownFolder(); return array( '.' === $own ? basename( plugin_basename( MIGRATEX_FILE ) ) : $own, 'index.php' ); } /** * The plugin file WordPress lists as active ("folder/migratex.php"). */ public static function ownPluginFile(): string { return plugin_basename( MIGRATEX_FILE ); } /** * Entry names inside wp-content/uploads that a reset must leave alone: the backups folder, * which holds every archive and the state of running jobs. * * @return string[] */ public static function uploadsToKeep(): array { return array_values( array_unique( array( basename( Paths::root() ), 'migratex-backups' ) ) ); } public static function pluginsDir(): string { return Paths::content() . '/plugins'; } public static function themesDir(): string { return Paths::content() . '/themes'; } public static function uploadsDir(): string { $uploads = wp_upload_dir( null, false ); return untrailingslashit( wp_normalize_path( $uploads['basedir'] ) ); } /** * The theme a reset falls back to: WordPress's own default if it is installed, otherwise the * newest bundled "twenty..." theme. Null when there is none, in which case a theme reset must * not delete anything, since the site would be left with no theme at all. */ public static function defaultTheme(): ?string { $dir = self::themesDir(); if ( defined( 'WP_DEFAULT_THEME' ) && is_dir( $dir . '/' . WP_DEFAULT_THEME ) && is_file( $dir . '/' . WP_DEFAULT_THEME . '/style.css' ) ) { return (string) WP_DEFAULT_THEME; } $found = array(); foreach ( (array) glob( $dir . '/twenty*', GLOB_ONLYDIR ) as $path ) { if ( is_file( $path . '/style.css' ) && ! is_link( $path ) ) { $found[] = basename( $path ); } } if ( ! $found ) { return null; } rsort( $found, SORT_STRING ); return $found[0]; } }