PluginProbe
Elementor Website Builder – more than just a page builder / 4.0.0-dev4
Elementor Website Builder – more than just a page builder v4.0.0-dev4
4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 4.0.7 All 451 releases
elementor / app / modules / onboarding / data / endpoints / install-theme.php

install-theme.php in Elementor Website Builder – more than just a page builder 4.0.0-dev4, at app/modules/onboarding/data/endpoints/install-theme.php

104 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Elementor\App\Modules\Onboarding\Data\Endpoints;
4
5 use Elementor\Data\V2\Base\Endpoint as Endpoint_Base;
6 use WP_REST_Server;
7
8 if ( ! defined( 'ABSPATH' ) ) {
9 exit;
10 }
11
12 class Install_Theme extends Endpoint_Base {
13
14 const ALLOWED_THEMES = [ 'hello-elementor', 'hello-biz' ];
15
16 public function get_name(): string {
17 return 'install-theme';
18 }
19
20 public function get_format(): string {
21 return 'onboarding';
22 }
23
24 protected function register(): void {
25 parent::register();
26
27 $this->register_items_route( WP_REST_Server::CREATABLE );
28 }
29
30 public function create_items( $request ) {
31 $permission = $this->check_permission();
32 if ( is_wp_error( $permission ) ) {
33 return $permission;
34 }
35
36 $params = $request->get_json_params();
37 $theme_slug = $params['theme_slug'] ?? '';
38
39 if ( empty( $theme_slug ) || ! in_array( $theme_slug, self::ALLOWED_THEMES, true ) ) {
40 return new \WP_Error(
41 'invalid_theme',
42 __( 'Invalid or unsupported theme.', 'elementor' ),
43 [ 'status' => 400 ]
44 );
45 }
46
47 if ( ! current_user_can( 'install_themes' ) || ! current_user_can( 'switch_themes' ) ) {
48 return new \WP_Error(
49 'insufficient_permissions',
50 __( 'You do not have permission to install themes.', 'elementor' ),
51 [ 'status' => 403 ]
52 );
53 }
54
55 $theme = wp_get_theme( $theme_slug );
56
57 if ( ! $theme->exists() ) {
58 if ( ! function_exists( 'request_filesystem_credentials' ) ) {
59 require_once ABSPATH . 'wp-admin/includes/file.php';
60 }
61
62 if ( ! class_exists( '\Theme_Upgrader' ) ) {
63 require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
64 }
65
66 if ( ! class_exists( '\WP_Ajax_Upgrader_Skin' ) ) {
67 require_once ABSPATH . 'wp-admin/includes/class-wp-ajax-upgrader-skin.php';
68 }
69
70 $skin = new \WP_Ajax_Upgrader_Skin();
71 $upgrader = new \Theme_Upgrader( $skin );
72 $result = $upgrader->install( "https://downloads.wordpress.org/theme/{$theme_slug}.latest-stable.zip" );
73
74 if ( is_wp_error( $result ) || ! $result ) {
75 return new \WP_Error(
76 'theme_install_failed',
77 __( 'Failed to install the theme.', 'elementor' ),
78 [ 'status' => 500 ]
79 );
80 }
81 }
82
83 switch_theme( $theme_slug );
84
85 return [
86 'data' => [
87 'success' => true,
88 'message' => 'theme_installed',
89 ],
90 ];
91 }
92
93 private function check_permission() {
94 if ( ! current_user_can( 'manage_options' ) ) {
95 return new \WP_Error(
96 'rest_forbidden',
97 __( 'Sorry, you are not allowed to access onboarding data.', 'elementor' ),
98 [ 'status' => 403 ]
99 );
100 }
101 return true;
102 }
103 }
104