PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.16.9
GiveWP – Donation Plugin and Fundraising Platform v4.16.9
4.16.9 4.16.8.1 4.16.8 4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 All 255 releases
← All changes | src/API/Endpoints/Migrations/RunMigration.php +203 -21 2.30.04.16.9 View file →
@@ -2,8 +2,13 @@
2 2
3 3 namespace Give\API\Endpoints\Migrations;
4 4
5 5 use Exception;
6 +use Give\Framework\Database\DB;
7 +use Give\Framework\Migrations\Contracts\BatchMigration;
8 +use Give\Framework\Migrations\Contracts\Migration;
9 +use Give\Framework\Migrations\Contracts\ReversibleMigration;
10 +use Give\Framework\Migrations\Controllers\BatchMigrationRunner;
6 11 use Give\Framework\Migrations\MigrationsRegister;
7 12 use Give\MigrationLog\MigrationLogFactory;
8 13 use Give\MigrationLog\MigrationLogStatus;
9 14 use WP_REST_Request;
@@ -10,18 +15,15 @@
10 15 use WP_REST_Response;
11 16
12 17 /**
13 18 * Class RunMigration
14 - * @package Give\API\Endpoints\Migrations
19 + * @package Give\API\Endpoints\Migrations
15 20 *
16 - * @since 2.10.0
21 + * @since 4.0.0 run batch migrations
22 + * @since 2.10.0
17 23 */
18 24 class RunMigration extends Endpoint
19 25 {
20 -
21 - /** @var string */
22 - protected $endpoint = 'migrations/run-migration';
23 -
24 26 /**
25 27 * @var MigrationsRegister
26 28 */
27 29 private $migrationRegister;
@@ -33,9 +35,9 @@
33 35
34 36 /**
35 37 * RunMigration constructor.
36 38 *
37 - * @param MigrationsRegister $migrationsRegister
39 + * @param MigrationsRegister $migrationsRegister
38 40 * @param MigrationLogFactory $migrationLogFactory
39 41 */
40 42 public function __construct(
41 43 MigrationsRegister $migrationsRegister,
@@ -51,18 +53,80 @@
51 53 public function registerRoute()
52 54 {
53 55 register_rest_route(
54 56 'give-api/v2',
55 - $this->endpoint,
57 + 'migrations/run-migration',
56 58 [
57 59 [
58 60 'methods' => 'POST',
59 - 'callback' => [$this, 'handleRequest'],
61 + 'callback' => [$this, 'runMigration'],
60 62 'permission_callback' => [$this, 'permissionsCheck'],
61 63 'args' => [
62 64 'id' => [
65 + 'type' => 'string',
66 + 'required' => true,
67 + ],
68 + ],
69 + ],
70 + 'schema' => [$this, 'getSchema'],
71 + ]
72 + );
73 +
74 + register_rest_route(
75 + 'give-api/v2',
76 + 'migrations/run-batch-migration',
77 + [
78 + [
79 + 'methods' => 'POST',
80 + 'callback' => [$this, 'runBatchMigration'],
81 + 'permission_callback' => [$this, 'permissionsCheck'],
82 + 'args' => [
83 + 'id' => [
84 + 'type' => 'string',
85 + 'required' => true,
86 + ],
87 + ],
88 + ],
89 + ]
90 + );
91 +
92 + register_rest_route(
93 + 'give-api/v2',
94 + 'migrations/reschedule-failed-actions',
95 + [
96 + [
97 + 'methods' => 'POST',
98 + 'callback' => [$this, 'rescheduleFailedActions'],
99 + 'permission_callback' => [$this, 'permissionsCheck'],
100 + 'args' => [
101 + 'id' => [
102 + 'type' => 'string',
103 + 'required' => true,
104 + ],
105 + ],
106 + ],
107 + ]
108 + );
109 +
110 + /**
111 + * @since 4.3.0
112 + */
113 + register_rest_route(
114 + 'give-api/v2',
115 + 'migrations/rollback-migration',
116 + [
117 + [
118 + 'methods' => 'POST',
119 + 'callback' => [$this, 'rollbackMigration'],
120 + 'permission_callback' => [$this, 'permissionsCheck'],
121 + 'args' => [
122 + 'id' => [
123 + 'type' => 'string',
124 + 'required' => true,
63 125 'validate_callback' => function ($param) {
64 - return ! empty(trim($param));
126 + $migrationClass = $this->migrationRegister->getMigration($param);
127 +
128 + return is_subclass_of($migrationClass, ReversibleMigration::class);
65 129 },
66 130 ],
67 131 ],
68 132 ],
@@ -93,35 +157,47 @@
93 157 * @param WP_REST_Request $request
94 158 *
95 159 * @return WP_REST_Response
96 160 */
97 - public function handleRequest(WP_REST_Request $request)
161 + public function runMigration(WP_REST_Request $request): WP_REST_Response
98 162 {
99 - global $wpdb;
100 163 $migrationId = $request->get_param('id');
101 164 $migrationLog = $this->migrationLogFactory->make($migrationId);
102 165
103 166 // Begin transaction
104 - $wpdb->query('START TRANSACTION');
167 + DB::beginTransaction();
105 168
106 169 try {
107 170 $migrationClass = $this->migrationRegister->getMigration($migrationId);
171 + /**
172 + * @var Migration $migration
173 + */
108 174 $migration = give($migrationClass);
109 175 $migration->run();
110 176 // Save migration status
111 - $migrationLog->setStatus(MigrationLogStatus::SUCCESS);
112 - $migrationLog->setError(null);
113 - $migrationLog->save();
177 + $migrationLog
178 + ->setStatus(MigrationLogStatus::SUCCESS)
179 + ->setError(null)
180 + ->save();
114 181
115 - $wpdb->query('COMMIT');
182 + DB::commit();
116 183
117 184 return new WP_REST_Response(['status' => true]);
118 185 } catch (Exception $exception) {
119 - $wpdb->query('ROLLBACK');
186 + DB::rollback();
120 187
121 - $migrationLog->setStatus(MigrationLogStatus::FAILED);
122 - $migrationLog->setError($exception);
123 - $migrationLog->save();
188 + $migrationLog
189 + ->setStatus(MigrationLogStatus::FAILED)
190 + ->setError([
191 + 'status' => __('Migration failed', 'give'),
192 + 'error' => [
193 + 'message' => $exception->getMessage(),
194 + 'code' => $exception->getCode(),
195 + 'file' => $exception->getFile(),
196 + 'line' => $exception->getLine(),
197 + ],
198 + ])
199 + ->save();
124 200 }
125 201
126 202 return new WP_REST_Response(
127 203 [
@@ -128,7 +204,113 @@
128 204 'status' => false,
129 205 'message' => $exception->getMessage(),
130 206 ]
131 207 );
208 + }
209 +
210 +
211 + /**
212 + * Run batch migration
213 + *
214 + * @since 4.0.0
215 + */
216 + public function runBatchMigration(WP_REST_Request $request): WP_REST_Response
217 + {
218 + $migrationId = $request->get_param('id');
219 + $migrationClass = $this->migrationRegister->getMigration($migrationId);
220 +
221 + if ( ! is_subclass_of($migrationClass, BatchMigration::class)) {
222 + return new WP_REST_Response([
223 + 'status' => false,
224 + 'message' => 'Migration is not an instance of ' . BatchMigration::class,
225 + ]);
226 + }
227 +
228 + try {
229 + // We are not running migration directly,
230 + // we just have to set migration status to PENDING and Migration Runner will handle it
231 + $migrationLog = $this->migrationLogFactory->make($migrationId);
232 + $migrationLog->setStatus(MigrationLogStatus::PENDING);
233 + $migrationLog->save();
234 + } catch (Exception $e) {
235 + return new WP_REST_Response([
236 + 'status' => false,
237 + 'message' => $e->getMessage(),
238 + ]);
239 + }
240 +
241 + return new WP_REST_Response(['status' => true]);
242 + }
243 +
244 + /**
245 + * Reschedule failed actions
246 + *
247 + * @since 4.0.0
248 + */
249 + public function rescheduleFailedActions(WP_REST_Request $request): WP_REST_Response
250 + {
251 + $migrationId = $request->get_param('id');
252 + $migrationClass = $this->migrationRegister->getMigration($migrationId);
253 + $migration = give($migrationClass);
254 +
255 + if ( ! is_subclass_of($migration, BatchMigration::class)) {
256 + return new WP_REST_Response([
257 + 'status' => false,
258 + 'message' => 'Migration is not an instance of ' . BatchMigration::class,
259 + ]);
260 + }
261 +
262 + try {
263 + (new BatchMigrationRunner($migration))->rescheduleFailedActions();
264 + } catch (Exception $e) {
265 + return new WP_REST_Response([
266 + 'status' => false,
267 + 'message' => $e->getMessage(),
268 + ]);
269 + }
270 +
271 + return new WP_REST_Response(['status' => true]);
272 + }
273 +
274 +
275 + /**
276 + * @since 4.3.0
277 + */
278 + public function rollbackMigration(WP_REST_Request $request): WP_REST_Response
279 + {
280 + $migrationId = $request->get_param('id');
281 + $migrationClass = $this->migrationRegister->getMigration($migrationId);
282 + $migration = give($migrationClass);
283 + $migrationLog = $this->migrationLogFactory->make($migrationId);
284 +
285 + if ($migration instanceof ReversibleMigration) {
286 + try {
287 + $migration->reverse();
288 + $migrationLog->setStatus(MigrationLogStatus::REVERSED);
289 + } catch (Exception $e) {
290 + $migrationLog
291 + ->setStatus(MigrationLogStatus::FAILED)
292 + ->setError([
293 + 'status' => __('Rollback failed', 'give'),
294 + 'error' => [
295 + 'message' => $e->getMessage(),
296 + 'code' => $e->getCode(),
297 + 'file' => $e->getFile(),
298 + 'line' => $e->getLine(),
299 + ],
300 + ]);
301 +
302 + return new WP_REST_Response([
303 + 'status' => false,
304 + 'message' => $e->getMessage(),
305 + ]);
306 + }
307 +
308 + $migrationLog->save();
309 +
310 + return new WP_REST_Response(['status' => true]);
311 + }
312 +
313 + return new WP_REST_Response(['status' => false]);
132 314 }
133 315
134 316 }