$value) { $arguments[] = '--' . $key . ($value === true ? '' : '=' . $value); } foreach (array('allow-root', 'quiet') as $flag) { if (WP_CLI::has_config($flag) && WP_CLI::get_config($flag) === true) { $arguments[] = '--' . $flag; } } if (WP_CLI::get_config('color') === false) { $arguments[] = '--no-color'; } foreach (array('url', 'user') as $key) { $value = WP_CLI::get_config($key); if (is_string($value) && $value !== '') { $arguments[] = '--' . $key . '=' . $value; } } $command = 'wp ' . implode(' ', array_map('escapeshellarg', array_slice($arguments, 1))); // CLI-only diagnostic; Command::terminal() escapes controls before output, not HTML here. // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped throw new RuntimeException('La importación necesita un arranque explícito sin plugins del destino. No se ha modificado el sitio; ejecuta directamente: ' . $command); } ################################################ # # bootstrap # # Usa el arranque nativo de WP-CLI y carga el trabajador protegido después de WordPress. # ################################################ public static function bootstrap(array $roots, array $context): string { $root_data = var_export(json_encode($roots, JSON_THROW_ON_ERROR), true); $context_data = var_export(json_encode($context, JSON_THROW_ON_ERROR), true); $code = 'if (defined("ABSPATH") || WP_CLI::get_config("skip-plugins") !== true || WP_CLI::get_config("skip-themes") !== true || defined("WPMU_PLUGIN_DIR")) { WP_CLI::error("Se requiere un arranque nuevo con --skip-plugins y --skip-themes, sin WPMU_PLUGIN_DIR predefinido."); }'; $code .= 'define("DCTSOLLERS_MIGRATION_ISOLATED", true);'; $code .= 'define("DCTSOLLERS_MIGRATION_ROOTS", json_decode(' . $root_data . ', true, 512, JSON_THROW_ON_ERROR));'; $code .= 'define("DCTSOLLERS_MIGRATION_CONTEXT", json_decode(' . $context_data . ', true, 512, JSON_THROW_ON_ERROR));'; $code .= 'define("WPMU_PLUGIN_DIR", sys_get_temp_dir() . "/dctsollers-unused-mu-" . bin2hex(random_bytes(16)));'; $code .= self::config_guard(); $code .= 'WP_CLI::add_wp_hook("enable_loading_object_cache_dropin", static fn(): bool => false);'; $code .= 'WP_CLI::add_wp_hook("enable_loading_advanced_cache_dropin", static fn(): bool => false);'; $code .= 'WP_CLI::add_hook("after_wp_load", static function (): void { require ' . var_export(__DIR__ . '/import-bootstrap.php', true) . '; });'; return $code; } ################################################ # # config_guard # # Tolera solo la redefinición deliberada de la raíz mu durante la carga de wp-config. # ################################################ private static function config_guard(): string { $code = <<<'PHP' WP_CLI::add_hook('before_wp_config_load', static function (): void { $active = true; $previous = null; $handler = static function (int $severity, string $message, string $file, int $line) use (&$active, &$previous) { /* PHP constants are immutable: isolation intentionally overrides only this setting from wp-config. */ if ($active && $severity === E_WARNING && $message === 'Constant WPMU_PLUGIN_DIR already defined') { return true; } return $previous === null ? false : $previous($severity, $message, $file, $line); }; $previous = set_error_handler($handler); WP_CLI::add_hook('after_wp_config_load', static function () use ($handler, &$active): void { $active = false; $current = set_error_handler($handler); restore_error_handler(); if ($current === $handler) { restore_error_handler(); } }); }); PHP; return str_replace("\n", ' ', $code); } ################################################ # # assert_isolated # # Detiene la restauración si una configuración externa ha cargado código que podría borrarse. # ################################################ private static function assert_isolated(): void { $self = realpath(dirname(__DIR__, 2)) . '/'; $roots = DCTSollers_Migration_Locations::roots(); unset($roots['']); foreach (get_included_files() as $file) { if (str_starts_with($file, $self)) { continue; } foreach ($roots as $root) { $path = realpath($root); if ($path !== false && str_starts_with($file, $path . '/')) { throw new RuntimeException('El arranque aislado ha cargado código de plugins, mu-plugins o temas del destino. Revisa wp-config.php y los drop-ins antes de importar.'); } } } } }