PluginProbe
WP Synchro – The Ultimate WordPress Migration Tool / trunk
WP Synchro – The Ultimate WordPress Migration Tool vtrunk
1.16.1 1.16.0 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.1.0 1.10.0 1.11.0 1.11.1 1.11.2 1.11.3 1.11.4 1.11.5 1.12.0 1.13.0 1.14.0 1.15.0 1.2.0 1.3.0 1.3.1 1.3.2 All 45 releases
wpsynchro / src / API / SaveMigration.php

SaveMigration.php in WP Synchro – The Ultimate WordPress Migration Tool trunk, at src/API/SaveMigration.php

100 lines 3.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Save migration
5 */
6
7 namespace WPSynchro\API;
8
9 use WPSynchro\Migration\Migration;
10 use WPSynchro\Migration\MigrationFactory;
11 use WPSynchro\Schedule\ScheduleFactory;
12 use WPSynchro\Utilities\CommonFunctions;
13 use WPSynchro\Files\Location;
14
15 class SaveMigration extends WPSynchroService
16 {
17 public function service()
18 {
19 $nonce = $_GET['nonce'] ?? '';
20 if (!wp_verify_nonce($nonce, 'wpsynchro-addedit')) {
21 \http_response_code(401);
22 return;
23 }
24
25 // Extract parameters
26 $body = $this->getRequestBody();
27 $parameters = json_decode($body);
28
29 // If json is invalid for some reason
30 if (is_null($parameters)) {
31 \http_response_code(400);
32 return;
33 }
34
35 // We can receive migration data from the wizard or from the advanced add/edit page, so we need to map it to a Migration object
36 if (isset($parameters->type) && $parameters->type === "simple") {
37 // Simple migration from the wizard
38 $migration = $this->mapFromWizard($parameters);
39 } else {
40 // Advanced migration from the add/edit page
41 $migration = Migration::map($parameters);
42 }
43
44
45 if (strlen($migration->id) == 0) {
46 $migration->id = uniqid();
47 }
48
49 // Sanitize
50 $migration->sanitize();
51
52 // Save
53 $migration_factory = MigrationFactory::getInstance();
54 $migration_factory->addMigration($migration);
55
56
57 // Update all the scheduled migrations according to our migrations
58 if (CommonFunctions::isPremiumVersion()) {
59 $all_migrations = $migration_factory->getAllMigrations();
60 $schedule_factory = ScheduleFactory::getInstance();
61 $schedule_factory->synchronizeWithMigrations($all_migrations);
62 }
63
64 // Return the sanitized version
65 echo json_encode($migration);
66 }
67
68 private function mapFromWizard(object $parameters): Migration
69 {
70 $migration = new Migration();
71 $migration->sync_preset = "none";
72
73 $generated_name = __("Migrate from %s", "wpsynchro");
74 if ($parameters->migrate_type === "push") {
75 $generated_name = __("Migrate to %s", "wpsynchro");
76 }
77 $generated_name = sprintf($generated_name, $parameters->remote_server_url);
78
79 $migration->name = $generated_name;
80 $migration->site_url = $parameters->remote_server_url;
81 $migration->access_key = $parameters->remote_server_key;
82 $migration->sync_database = $parameters->migrate_db;
83 $migration->searchreplaces_regenerate = true;
84 $migration->sync_files = $parameters->migrate_files;
85 $migration->files_ask_user_for_confirm = true;
86 $migration->type = $parameters->migrate_type;
87 $migration->db_make_backup = $parameters->backup_db;
88
89 // If migrate files is done, we add the web root as a location
90 if ($migration->sync_files) {
91 $loc = new Location();
92 $loc->base = "webroot";
93 $loc->path = "/";
94 $migration->file_locations[] = $loc;
95 }
96
97 return $migration;
98 }
99 }
100