PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 3.8.0
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v3.8.0
3.8.0 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 All 112 releases
templately / modules / full-site-import / REST / GlobalSettings.php

GlobalSettings.php in Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! 3.8.0, at modules/full-site-import/REST/GlobalSettings.php

80 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Templately\Modules\FullSiteImport\REST;
4
5 use Templately\API\API;
6 use Templately\Modules\FullSiteImport\Utils\PackInfoFetcher;
7 use Templately\Utils\Helper;
8 use WP_Error;
9 use WP_REST_Request;
10
11 /**
12 * REST replacement for the `wp_ajax_templately_pack_import_global_settings` action
13 * (FR-018, specs/025-full-site-import/spec.md). Fetches a pack's manifest-declared
14 * color/typography/customizer settings WITHOUT importing any posts, attachments,
15 * templates, or terms — used by the settings-only import mode and by single-import's
16 * "merge pack settings into the inserted template" flow.
17 *
18 * The old ajax action (`Ajax\PackInfoController::import_global_settings()`) is kept
19 * running as a deprecated shim (see module.php) — browsers with an already-cached old
20 * JS bundle would otherwise break on the next request after this route ships.
21 */
22 class GlobalSettings extends API {
23
24 /**
25 * The ajax path required `install_plugins` AND `install_themes` (the same
26 * capability pair `FullSiteImport::add_ajax_action()` enforces for every
27 * `wp_ajax_templately_pack_*` action) plus a valid `templately_nonce`. REST
28 * requests carry their own nonce (`X-WP-Nonce`, verified by WP core before
29 * permission_callback even runs), so the equivalent protection here is: the
30 * same capability pair, ON TOP OF the base API class's `permission_check()`
31 * (delete_posts + a connected/verified account `api_key`) — net STRICTER than
32 * the ajax path, never weaker.
33 */
34 public function permission_check( WP_REST_Request $request ) {
35 $this->request = $request;
36
37 if ( ! current_user_can( 'install_plugins' ) || ! current_user_can( 'install_themes' ) ) {
38 return new WP_Error(
39 'rest_forbidden',
40 __( 'Sorry, you are not allowed to import global settings.', 'templately' ),
41 [ 'status' => rest_authorization_required_code() ]
42 );
43 }
44
45 return parent::permission_check( $request );
46 }
47
48 public function register_routes() {
49 $this->post( 'import/global-settings', [ $this, 'import_global_settings' ], [
50 'id' => [
51 'required' => true,
52 ],
53 'isAi' => [
54 'required' => false,
55 ],
56 ] );
57 }
58
59 public function import_global_settings() {
60 $id = $this->get_param( 'id', 0, 'absint' );
61 $isAi = Helper::sanitize( $this->get_param( 'isAi', false, null ), 'boolean' );
62
63 if ( empty( $id ) ) {
64 return $this->error( 'invalid_id', __( 'A valid pack id is required.', 'templately' ), 'import/global-settings', 400 );
65 }
66
67 $data = ( new PackInfoFetcher() )->fetch( $id, $isAi );
68 if ( is_wp_error( $data ) ) {
69 return $this->error( 'api_error', $data->get_error_message(), 'import/global-settings', 500 );
70 }
71
72 $settings = [];
73 if ( isset( $data['data']['settings'] ) ) {
74 $settings = json_decode( $data['data']['settings'], true );
75 }
76
77 return $this->success( [ 'settings' => $settings ] );
78 }
79 }
80