plugin = $plugin; $this->menu = $menu; } public function register(): void { add_action( 'admin_enqueue_scripts', array( $this, 'enqueue' ) ); } public function enqueue( string $hook_suffix ): void { $page = $this->menu->pageForHook( $hook_suffix ); if ( null === $page ) { return; } $url = MIGRATEX_URL . 'assets/'; wp_enqueue_style( 'migratex-admin', $url . 'css/admin.css', array(), $this->version( 'css/admin.css' ) ); wp_register_script( 'migratex-hash', $url . 'js/hash.js', array(), $this->version( 'js/hash.js' ), true ); wp_register_script( 'migratex-api', $url . 'js/api.js', array(), $this->version( 'js/api.js' ), true ); wp_register_script( 'migratex-progress', $url . 'js/progress.js', array( 'migratex-api', 'migratex-ui' ), $this->version( 'js/progress.js' ), true ); wp_register_script( 'migratex-pump', $url . 'js/pump.js', array( 'migratex-api', 'migratex-progress' ), $this->version( 'js/pump.js' ), true ); wp_register_script( 'migratex-feedback', $url . 'js/feedback.js', array( 'migratex-api', 'migratex-ui' ), $this->version( 'js/feedback.js' ), true ); wp_register_script( 'migratex-contact', $url . 'js/contact.js', array( 'migratex-api' ), $this->version( 'js/contact.js' ), true ); wp_register_script( 'migratex-select', $url . 'js/select.js', array(), $this->version( 'js/select.js' ), true ); wp_register_script( 'migratex-ui', $url . 'js/ui.js', array(), $this->version( 'js/ui.js' ), true ); wp_register_script( 'migratex-exporter', $url . 'js/exporter.js', array( 'migratex-pump', 'migratex-ui' ), $this->version( 'js/exporter.js' ), true ); wp_register_script( 'migratex-findreplace', $url . 'js/findreplace.js', array( 'migratex-api', 'migratex-ui' ), $this->version( 'js/findreplace.js' ), true ); wp_register_script( 'migratex-restore', $url . 'js/restore.js', array( 'migratex-pump', 'migratex-ui', 'migratex-hash', 'migratex-findreplace' ), $this->version( 'js/restore.js' ), true ); $scripts = array( Menu::SLUG_EXPORT => array( 'migratex-export', 'js/export.js', array( 'migratex-exporter' ) ), Menu::SLUG_IMPORT => array( 'migratex-import', 'js/import.js', array( 'migratex-restore' ) ), Menu::SLUG_BACKUPS => array( 'migratex-backups', 'js/backups.js', array( 'migratex-restore' ) ), Menu::SLUG_SCHEDULES => array( 'migratex-schedules', 'js/schedules.js', array( 'migratex-api', 'migratex-ui' ) ), Menu::SLUG_REMOTE => array( 'migratex-remote', 'js/remote.js', array( 'migratex-restore' ) ), Menu::SLUG_RESET => array( 'migratex-reset', 'js/reset.js', array( 'migratex-exporter', 'migratex-restore' ) ), ); list( $handle, $file, $deps ) = $scripts[ $page ]; // The config must exist before any of the scripts run. It travels inside one array: wp_localize_script() // turns a top-level scalar into a string, and the numbers and booleans in it must stay numbers and booleans. wp_localize_script( 'migratex-api', 'MigrateXBoot', array( 'config' => $this->config( $page ) ) ); wp_enqueue_script( $handle, $url . $file, $deps, $this->version( $file ), true ); wp_enqueue_script( 'migratex-feedback' ); wp_enqueue_script( 'migratex-contact' ); wp_enqueue_script( 'migratex-select' ); // every screen has at least the sidebar's feedback select. } /** * The asset's modification time, so a changed file gets a new URL and browsers, LiteSpeed, * Cloudflare and reverse proxies stop serving the old copy. Falls back to the plugin version. * * @param string $relative Path inside the assets folder, e.g. "css/admin.css". */ private function version( string $relative ): string { $path = MIGRATEX_DIR . 'assets/' . $relative; $mtime = file_exists( $path ) ? filemtime( $path ) : false; return false !== $mtime ? (string) $mtime : MIGRATEX_VERSION; } /** * @return array */ private function config( string $page ): array { // A restore can be started from Import or from Backups, and the Reset Hub looks for its own job. $types = array( Menu::SLUG_EXPORT => JobState::TYPE_EXPORT, Menu::SLUG_RESET => JobState::TYPE_RESET, ); $type = $types[ $page ] ?? JobState::TYPE_IMPORT; $active = $this->plugin->jobStore()->findActive( $type, get_current_user_id() ); return array( 'page' => $page, 'restUrl' => esc_url_raw( rest_url( MIGRATEX_REST_NAMESPACE . '/' ) ), 'ajaxUrl' => admin_url( 'admin-ajax.php' ), 'ajaxPrefix' => 'migratex_', 'nonce' => Guard::createNonce(), 'ajaxNonce' => Guard::createAjaxNonce(), 'chunkSize' => Environment::chunkBytes(), 'archiveExt' => MIGRATEX_ARCHIVE_EXT, 'siteName' => get_bloginfo( 'name' ), 'downloadUrl' => Download::url( '__FILE__' ), 'permalinksUrl' => admin_url( 'options-permalink.php' ), 'backupsUrl' => admin_url( 'admin.php?page=' . Menu::SLUG_BACKUPS ), 'activeJob' => $active ? $active->toArray() : null, 'feedback' => Contact::feedbackClient(), 'i18n' => $this->strings(), ); } /** * @return array */ private function strings(): array { return array( // Generic. 'cancel' => __( 'Cancel', 'migratex' ), 'cancelling' => __( 'Cancelling…', 'migratex' ), 'close' => __( 'Close', 'migratex' ), 'proceed' => __( 'Proceed', 'migratex' ), 'tryAgain' => __( 'Try again', 'migratex' ), 'resume' => __( 'Resume', 'migratex' ), 'connectionProblem' => __( 'Connection problem', 'migratex' ), 'connectionHelp' => __( 'The server did not answer. Your progress is saved and the job can continue.', 'migratex' ), 'sessionExpired' => __( 'Your session expired. Reload this page and sign in again.', 'migratex' ), 'tables' => __( 'tables', 'migratex' ), 'rows' => __( 'rows', 'migratex' ), 'files' => __( 'files', 'migratex' ), 'table' => __( 'Table', 'migratex' ), 'chunk' => __( 'Chunk', 'migratex' ), 'of' => __( 'of', 'migratex' ), 'step' => __( 'Step', 'migratex' ), 'offline' => __( 'You appear to be offline. The upload will continue when the connection returns.', 'migratex' ), 'leavePrompt' => __( 'A migration is running. Leaving now will pause it.', 'migratex' ), // Feedback form in the sidebar. 'copied' => __( 'Copied', 'migratex' ), 'copyFailed' => __( 'Copy failed', 'migratex' ), 'feedbackSend' => __( 'Send Feedback', 'migratex' ), 'feedbackSending' => __( 'Sending...', 'migratex' ), 'feedbackSent' => __( 'Thank you! Your feedback has been sent.', 'migratex' ), 'feedbackBadEmail' => __( 'Please enter a valid email address so we can reply.', 'migratex' ), 'feedbackNoMessage' => __( 'Please write a message.', 'migratex' ), 'feedbackFailed' => __( 'Could not send your feedback from this site. You can send the same message by email or WhatsApp instead.', 'migratex' ), 'feedbackRate' => __( 'You have sent several messages in the last hour. Please wait a while, or use WhatsApp or email instead.', 'migratex' ), 'feedbackUnavailable' => __( 'The feedback could not be sent from this site. Please reach out via WhatsApp or email above.', 'migratex' ), 'feedbackFallbackEmail' => __( 'Send by email', 'migratex' ), 'feedbackFallbackWhatsapp' => __( 'Send on WhatsApp', 'migratex' ), 'feedbackNetwork' => __( 'Could not reach the server. Check your connection and try again.', 'migratex' ), // Schedules. 'schedSaved' => __( 'Schedule saved.', 'migratex' ), 'schedSaving' => __( 'Saving…', 'migratex' ), 'schedSave' => __( 'Save Changes', 'migratex' ), 'schedFailed' => __( 'The schedule could not be saved.', 'migratex' ), 'schedNotScheduled' => __( 'Not scheduled', 'migratex' ), 'schedBadEmail' => __( 'Please enter one valid email address for notifications.', 'migratex' ), 'schedBadLimit' => __( 'Maximum backups to retain must be a whole number from 1 to 365.', 'migratex' ), 'toastClose' => __( 'Dismiss', 'migratex' ), // Reset Hub. 'resetTypeLabel' => __( 'Type {word} to confirm:', 'migratex' ), 'resetIrreversible' => __( 'This cannot be undone. Create a backup first if you might need anything back.', 'migratex' ), 'resetRunning' => __( 'Resetting your site', 'migratex' ), 'resetStarting' => __( 'Starting…', 'migratex' ), 'resetDone' => __( 'Reset complete', 'migratex' ), 'resetFailed' => __( 'Reset failed', 'migratex' ), 'resetReload' => __( 'Reload page', 'migratex' ), 'resetRemoved' => __( 'Removed', 'migratex' ), 'resetBackupFirst' => __( 'Back up first', 'migratex' ), 'resetPluginsTitle' => __( 'Purge all plugins?', 'migratex' ), 'resetPluginsWhat' => __( 'This deletes every installed plugin except MigrateX, which stays active.', 'migratex' ), 'resetPluginsDone' => __( 'Every plugin except MigrateX has been removed.', 'migratex' ), 'resetThemesTitle' => __( 'Reset all themes?', 'migratex' ), 'resetThemesWhat' => __( 'This deletes every theme and reactivates the default WordPress theme.', 'migratex' ), 'resetThemesDone' => __( 'Every theme except the default one has been removed, and the default theme is active.', 'migratex' ), 'resetMediaTitle' => __( 'Clean up the media library?', 'migratex' ), 'resetMediaWhat' => __( 'This permanently deletes every file in the media library (wp-content/uploads). Your MigrateX backups are kept.', 'migratex' ), 'resetMediaDone' => __( 'The media library is empty. Your backups were not touched.', 'migratex' ), 'resetDatabaseTitle' => __( 'Reset the database?', 'migratex' ), 'resetDatabaseWhat' => __( 'This wipes every post, page, comment, setting and user except you, and installs WordPress fresh. It also clears the list of active plugins: every plugin except MigrateX is deactivated, and you will have to reactivate the ones you want from the Plugins screen. You stay signed in, with the same username, e-mail and password.', 'migratex' ), 'resetDatabaseDone' => __( 'The database is fresh. You are still signed in with the same login. All other plugins are now deactivated; reactivate them from the Plugins screen.', 'migratex' ), 'resetFullTitle' => __( 'Reset the whole site?', 'migratex' ), 'resetFullWhat' => __( 'This removes all plugins, themes, media and database content, and returns WordPress to its factory defaults. MigrateX (the only plugin left active) and your backups are kept.', 'migratex' ), 'resetFullDone' => __( 'The site is back to factory defaults. MigrateX and your backups are untouched.', 'migratex' ), // Export. {done}, {total}, {file}, {size} are filled in by the script. 'exportTitle' => __( 'Exporting your site', 'migratex' ), 'exportStarting' => __( 'Preparing export...', 'migratex' ), 'tickDatabase' => __( 'Archiving database...', 'migratex' ), 'tickScan' => __( 'Scanning files...', 'migratex' ), 'tickFiles' => __( 'Archiving files [{done}/{total}]...', 'migratex' ), 'tickFilesNoTotal' => __( 'Archiving files...', 'migratex' ), 'tickPackage' => __( 'Creating archive package...', 'migratex' ), 'exportFailed' => __( 'Export failed', 'migratex' ), 'exportReady' => __( 'Your export is ready', 'migratex' ), 'exportReadyHelp' => __( 'Keep this file safe. Restore it on any WordPress site running MigrateX to bring everything back.', 'migratex' ), 'downloadButton' => __( 'DOWNLOAD {file} ({size})', 'migratex' ), // Restore (upload import and restore from the Backups screen). 'restoreWarnTitle' => __( 'Restore this backup?', 'migratex' ), 'restoreWarning' => __( 'The restoration process will overwrite your database, media, plugins, and themes. Do you want to proceed?', 'migratex' ), 'restoreTitle' => __( 'Restoring your site', 'migratex' ), 'restoreHelp' => __( 'Keep this tab open until the restore finishes.', 'migratex' ), 'uploading' => __( 'Uploading backup...', 'migratex' ), 'stepUpload' => __( 'Upload the backup', 'migratex' ), 'stepAssemble' => __( 'Verify the backup', 'migratex' ), 'stepExtract' => __( 'Extract the files', 'migratex' ), 'stepValidate' => __( 'Check compatibility', 'migratex' ), 'stepDatabase' => __( 'Restore the database', 'migratex' ), 'stepReplace' => __( 'Update site addresses', 'migratex' ), 'stepFiles' => __( 'Restore media files', 'migratex' ), 'stepFinalize' => __( 'Switch over and install themes and plugins', 'migratex' ), 'restoreDone' => __( 'Your data has been imported successfully!', 'migratex' ), 'restoreDoneHelp' => __( 'One last thing: WordPress needs to rebuild its permalinks. Save them twice to be sure.', 'migratex' ), 'permaStep1' => __( 'Open the Permalinks page with the button below and click Save Changes.', 'migratex' ), 'permaStep2' => __( 'Click Save Changes a second time.', 'migratex' ), 'permaButton' => __( 'Go to Permalinks (options-permalink.php)', 'migratex' ), 'restoreFailed' => __( 'Restore failed', 'migratex' ), 'restoreFailedHelp' => __( 'Your site was not changed.', 'migratex' ), 'restoreFailedFiles' => __( 'Your database was not changed, but some media files may already have been replaced.', 'migratex' ), 'restoreFailedSwapped' => __( 'The new database is already live, so this cannot be undone. Some themes or plugins were not copied; the message above says where the files were kept.', 'migratex' ), 'resumeHelp' => __( 'The server has saved its progress, so the restore can carry on from where it stopped.', 'migratex' ), 'resumeUploadHelp' => __( 'A browser must be uploading the file for it to continue. Cancel it to start a new one.', 'migratex' ), // Import screen. 'wrongType' => __( 'That does not look like a MigrateX backup. Choose a file ending in .wpmig.', 'migratex' ), // Backups screen. 'confirmDelete' => __( 'Delete this backup permanently?', 'migratex' ), 'delete' => __( 'Delete', 'migratex' ), 'deleting' => __( 'Deleting…', 'migratex' ), 'noBackups' => __( 'No backups yet.', 'migratex' ), // Find & replace rules for a restore. 'frTitle' => __( 'Advanced Database Find & Replace (Optional)', 'migratex' ), 'frHelp' => __( 'Replace text in the restored database, for example an old address, a staging path or a CDN name. Plain text, matched exactly (capital and small letters are different). It is replaced inside posts, settings and block editor data without breaking them. The site address is already updated for you.', 'migratex' ), 'frAddRow' => __( 'Add Row', 'migratex' ), 'frFind' => __( 'Find string', 'migratex' ), 'frReplace' => __( 'Replace with', 'migratex' ), 'frRemove' => __( 'Remove this row', 'migratex' ), 'frExact' => __( 'Exact', 'migratex' ), 'frExactHelp' => __( 'Keep leading and trailing spaces as typed (they are trimmed otherwise)', 'migratex' ), 'frRule' => __( 'Rule {n}:', 'migratex' ), 'frFindEmpty' => __( '"Find" cannot be empty.', 'migratex' ), 'frFindSpaces' => __( '"Find" cannot be only spaces.', 'migratex' ), 'frFindShort' => __( '"Find" needs at least {n} characters, or it would match almost everything.', 'migratex' ), 'frTooLong' => __( 'Each side can be at most {n} characters.', 'migratex' ), 'frSame' => __( '"Find" and "Replace with" are the same, so the rule does nothing.', 'migratex' ), 'frDuplicate' => __( 'An earlier rule already searches for this text.', 'migratex' ), // Live progress. 'progressShowLog' => __( 'Show live activity log', 'migratex' ), 'progressLogTitle' => __( 'Live activity log', 'migratex' ), 'progressCopy' => __( 'Copy log', 'migratex' ), 'progressCopied' => __( 'Log copied.', 'migratex' ), 'progressActive' => __( 'working', 'migratex' ), 'progressUpdated' => __( 'last update {s}s ago', 'migratex' ), 'progressStall' => __( 'The server is busy with a large file or query (no update for {s} seconds). This can be normal. Please keep this tab open.', 'migratex' ), 'progressLost' => __( 'Cannot reach the server for the live view. The work itself carries on; retrying…', 'migratex' ), 'phaseDatabase' => __( 'Database', 'migratex' ), 'phaseFiles' => __( 'Files', 'migratex' ), 'phaseCompression' => __( 'Compression', 'migratex' ), 'phaseVerification' => __( 'Verification', 'migratex' ), 'phaseComplete' => __( 'Complete', 'migratex' ), 'phaseError' => __( 'Error', 'migratex' ), // Site-to-Site. 'remoteGenerate' => __( 'Generate Connection Key', 'migratex' ), 'remoteWorking' => __( 'Working…', 'migratex' ), 'remoteKeyReady' => __( 'Key ready. Copy it now: it is shown only once.', 'migratex' ), 'remoteConnected' => __( 'The other site has connected and is pulling the backup.', 'migratex' ), 'remoteRefused' => __( 'The last request from the other site was refused:', 'migratex' ), 'remoteRetrying' => __( 'The source site did not answer; trying again', 'migratex' ), 'remoteEnded' => __( 'The key is no longer active.', 'migratex' ), 'remoteRevoked' => __( 'Key revoked.', 'migratex' ), 'remoteExpired' => __( 'The key has expired.', 'migratex' ), 'remoteCopied' => __( 'Key copied.', 'migratex' ), 'remoteNeedKey' => __( 'Paste the connection key first.', 'migratex' ), 'remotePulling' => __( 'Connecting…', 'migratex' ), 'remoteFromSite' => __( 'Receiving from another site', 'migratex' ), 'downloadPreparing' => __( 'Preparing…', 'migratex' ), ); } }