PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 3.7.5
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v3.7.5
3.7.5 3.7.4 3.7.3 3.7.2 1-final 3.7.1 3.7.0 3.6.8 3.6.7 3.6.6 3.6.5 3.6.4 3.6.3 3.6.2 3.6.1 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 3.1.10 All 111 releases
templately / includes / API / Sites.php

Sites.php in Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! 3.7.5, at includes/API/Sites.php

101 lines 3.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Templately\API;
4
5 use WP_REST_Request;
6 use WP_Error;
7 use Templately\Utils\Helper;
8 use Templately\Utils\Options;
9
10 /**
11 * Sites API Class
12 *
13 * Handles site-related API endpoints including site migration.
14 */
15 class Sites extends API {
16
17 /**
18 * Register REST API routes
19 */
20 public function register_routes() {
21 $this->post('sites/migrate', [$this, 'migrate_site']);
22 }
23
24 /**
25 * Migrate site connection from old URL to new URL
26 *
27 * Calls the Templately API to transfer site connection from a previously
28 * connected site to the current site.
29 *
30 * @return array|WP_Error Response data or error
31 */
32 public function migrate_site() {
33 $api_key = $this->utils('options')->get('api_key');
34
35 if (empty($api_key)) {
36 return $this->error(
37 'missing_api_key',
38 __('API key is required for site migration.', 'templately'),
39 'sites/migrate',
40 401
41 );
42 }
43
44 $new_url = home_url('/');
45 $user = $this->utils('options')->get('user');
46 $old_url = isset($user['site_url']) ? base64_decode( $user['site_url'] ) : '';
47
48 // Build the API URL
49 $api_url = Helper::get_api_url('v1/migrate/sites');
50
51 // Make the API request
52 $response = wp_remote_request($api_url, [
53 'method' => 'POST',
54 'timeout' => 30,
55 'headers' => [
56 'Authorization' => 'Bearer ' . $api_key,
57 'Content-Type' => 'application/json',
58 'Accept' => 'application/json',
59 'x-templately-url' => $new_url,
60 'x-templately-version' => defined('TEMPLATELY_VERSION') ? TEMPLATELY_VERSION : '1.0.0',
61 ],
62 'body' => json_encode([
63 'old_url' => $old_url,
64 'new_url' => $new_url,
65 ]),
66 ]);
67
68 if (is_wp_error($response)) {
69 return $this->error(
70 'api_request_failed',
71 $response->get_error_message(),
72 'sites/migrate',
73 500
74 );
75 }
76
77 $response_code = wp_remote_retrieve_response_code($response);
78 $response_body = json_decode(wp_remote_retrieve_body($response), true);
79
80 if ($response_code !== 200) {
81 $error_message = $response_body['message'] ?? __('Site migration failed.', 'templately');
82 return $this->error(
83 'migration_failed',
84 $error_message,
85 'sites/migrate',
86 $response_code
87 );
88 }
89
90 // Clear the disconnection status and update site_url on successful migration
91 Helper::clear_site_disconnection();
92
93 // Return success response
94 return $this->success([
95 'status' => 'success',
96 'message' => __('Site successfully migrated. You can now import templates.', 'templately'),
97 'data' => $response_body,
98 ]);
99 }
100 }
101