PluginProbe
Elementor Website Builder – more than just a page builder / 3.26.0-dev4
Elementor Website Builder – more than just a page builder v3.26.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
← All changes | app/modules/onboarding/module.php +394 -252 4.2.43.26.0-dev4 View file →
@@ -1,352 +1,494 @@
1 1 <?php
2 -
3 2 namespace Elementor\App\Modules\Onboarding;
4 3
5 -use Elementor\App\Modules\Onboarding\Data\Controller;
6 -use Elementor\App\Modules\Onboarding\Storage\Entities\User_Choices;
7 -use Elementor\App\Modules\Onboarding\Storage\Entities\User_Progress;
8 -use Elementor\App\Modules\Onboarding\Storage\Onboarding_Progress_Manager;
4 +use Automatic_Upgrader_Skin;
9 5 use Elementor\Core\Base\Module as BaseModule;
10 -use Elementor\Core\Settings\Manager as SettingsManager;
11 -use Elementor\Includes\EditorAssetsAPI;
6 +use Elementor\Core\Common\Modules\Ajax\Module as Ajax;
7 +use Elementor\Core\Common\Modules\Connect\Apps\Library;
8 +use Elementor\Core\Files\Uploads_Manager;
12 9 use Elementor\Plugin;
13 10 use Elementor\Utils;
11 +use Plugin_Upgrader;
14 12
15 13 if ( ! defined( 'ABSPATH' ) ) {
16 - exit;
14 + exit; // Exit if accessed directly
17 15 }
18 16
17 +/**
18 + * Onboarding Module
19 + *
20 + * Responsible for initializing Elementor App functionality
21 + *
22 + * @since 3.6.0
23 + */
19 24 class Module extends BaseModule {
20 25
21 - const VERSION = '2.0.0';
22 - const ASSETS_BASE_URL = 'https://assets.elementor.com/onboarding/v1/strings/';
26 + const VERSION = '1.0.0';
23 27 const ONBOARDING_OPTION = 'elementor_onboarded';
24 28
25 - const SUPPORTED_LOCALES = [
26 - 'de_DE' => 'de',
27 - 'es_ES' => 'es',
28 - 'fr_FR' => 'fr',
29 - 'he_IL' => 'he',
30 - 'id_ID' => 'id',
31 - 'it_IT' => 'it',
32 - 'nl_NL' => 'nl',
33 - 'pl_PL' => 'pl',
34 - 'pt_BR' => 'pt',
35 - 'tr_TR' => 'tr',
36 - ];
37 -
38 - private Onboarding_Progress_Manager $progress_manager;
39 -
40 - public function get_name(): string {
29 + /**
30 + * Get name.
31 + *
32 + * @since 3.6.0
33 + * @access public
34 + *
35 + * @return string
36 + */
37 + public function get_name() {
41 38 return 'onboarding';
42 39 }
43 40
44 - public static function has_user_finished_onboarding(): bool {
45 - return (bool) get_option( self::ONBOARDING_OPTION );
46 - }
47 -
48 - public function __construct() {
49 - $this->progress_manager = Onboarding_Progress_Manager::instance();
50 -
51 - Plugin::instance()->data_manager_v2->register_controller( new Controller() );
52 -
53 - add_action( 'elementor/init', [ $this, 'on_elementor_init' ], 12 );
54 -
55 - if ( $this->should_show_starter() ) {
56 - add_filter( 'elementor/editor/localize_settings', [ $this, 'add_starter_settings' ] );
57 - add_filter( 'elementor/editor/v2/packages', [ $this, 'add_starter_packages' ] );
58 - add_action( 'elementor/editor/v2/styles/enqueue', [ $this, 'enqueue_fonts' ] );
59 - add_action( 'elementor/preview/enqueue_styles', [ $this, 'enqueue_starter_preview_css' ] );
60 - }
61 - }
62 -
63 - public function on_elementor_init(): void {
64 - if ( ! Plugin::instance()->app->is_current() ) {
41 + /**
42 + * Set Onboarding Settings
43 + *
44 + * Creates an array of module settings that is localized into the JS App config.
45 + *
46 + * @since 3.6.0
47 + */
48 + private function set_onboarding_settings() {
49 + if ( ! Plugin::$instance->common ) {
65 50 return;
66 51 }
67 52
68 - $this->set_onboarding_settings();
69 - $this->enqueue_fonts();
70 - }
53 + // Get the published pages and posts
54 + $pages_and_posts = new \WP_Query( [
55 + 'post_type' => [ 'page', 'post' ],
56 + 'post_status' => 'publish',
57 + 'update_post_meta_cache' => false,
58 + 'update_post_term_cache' => false,
59 + 'no_found_rows' => true,
60 + ] );
71 61
72 - public function enqueue_fonts(): void {
73 - wp_enqueue_style(
74 - 'elementor-onboarding-fonts',
75 - 'https://fonts.googleapis.com/css2?family=Poppins:wght@500&display=swap',
76 - [],
77 - ELEMENTOR_VERSION
78 - );
79 - }
62 + $custom_site_logo_id = get_theme_mod( 'custom_logo' );
63 + $custom_logo_src = wp_get_attachment_image_src( $custom_site_logo_id, 'full' );
64 + $site_name = get_option( 'blogname', '' );
80 65
81 - public function enqueue_starter_preview_css(): void {
82 - $css = '
83 - #site-header,
84 - .page-header { display: var(--e-starter-header-display, none); }
85 - ';
66 + $hello_theme = wp_get_theme( 'hello-elementor' );
67 + $hello_theme_errors = is_object( $hello_theme->errors() ) ? $hello_theme->errors()->errors : [];
86 68
87 - wp_register_style( 'elementor-starter-preview', false );
88 - wp_enqueue_style( 'elementor-starter-preview' );
89 - wp_add_inline_style( 'elementor-starter-preview', $css );
90 - }
69 + /** @var Library $library */
70 + $library = Plugin::$instance->common->get_component( 'connect' )->get_app( 'library' );
91 71
92 - public function progress_manager(): Onboarding_Progress_Manager {
93 - return $this->progress_manager;
94 - }
95 -
96 - private function set_onboarding_settings(): void {
97 - if ( ! Plugin::instance()->common ) {
98 - return;
99 - }
100 -
101 - $progress = $this->progress_manager->get_progress();
102 - $choices = $this->progress_manager->get_choices();
103 - $steps = $this->get_steps_config();
104 -
105 - // If the user previously selected a theme but it's no longer the active theme,
106 - // clear the theme selection so the user can re-select.
107 - $this->maybe_invalidate_theme_selection( $progress, $choices );
108 -
109 - $is_connected = $this->is_user_connected();
110 -
111 72 Plugin::$instance->app->set_settings( 'onboarding', [
112 - 'version' => self::VERSION,
113 - 'restUrl' => rest_url( 'elementor/v1/onboarding/' ),
114 - 'nonce' => wp_create_nonce( 'wp_rest' ),
115 - 'progress' => $this->validate_progress_for_steps( $progress, $steps ),
116 - 'choices' => $choices->to_array(),
117 - 'hadUnexpectedExit' => $progress->had_unexpected_exit( self::has_user_finished_onboarding() ),
118 - 'isConnected' => $is_connected,
119 - 'userName' => $this->get_user_display_name(),
120 - 'steps' => $steps,
121 - 'uiTheme' => $this->get_ui_theme_preference(),
122 - 'translations' => $this->get_translated_strings(),
123 - 'shouldShowProInstallScreen' => $is_connected ? $this->should_show_pro_install_screen() : false,
124 - 'isHelloThemeActive' => $this->is_hello_theme_active(),
73 + 'eventPlacement' => 'Onboarding wizard',
74 + 'onboardingAlreadyRan' => get_option( self::ONBOARDING_OPTION ),
75 + 'onboardingVersion' => self::VERSION,
76 + 'isLibraryConnected' => $library->is_connected(),
77 + // Used to check if the Hello Elementor theme is installed but not activated.
78 + 'helloInstalled' => empty( $hello_theme_errors['theme_not_found'] ),
79 + 'helloActivated' => 'hello-elementor' === get_option( 'template' ),
80 + // The "Use Hello theme on my site" checkbox should be checked by default only if this condition is met.
81 + 'helloOptOut' => count( $pages_and_posts->posts ) < 5,
82 + 'siteName' => esc_html( $site_name ),
83 + 'isUnfilteredFilesEnabled' => Uploads_Manager::are_unfiltered_uploads_enabled(),
125 84 'urls' => [
126 - 'dashboard' => admin_url(),
127 - 'editor' => admin_url( 'edit.php?post_type=elementor_library' ),
128 - 'connect' => $this->get_connect_url(),
129 - 'signUp' => $this->get_connect_url( 'signup' ),
130 - 'comparePlans' => 'https://go.elementor.com/go-pro-onboarding-editor-features-step-upgrade/',
85 + 'kitLibrary' => Plugin::$instance->app->get_base_url() . '#/kit-library?order[direction]=desc&order[by]=featuredIndex',
131 86 'createNewPage' => Plugin::$instance->documents->get_create_new_post_url(),
132 - 'upgradeUrl' => 'https://go.elementor.com/go-pro-onboarding-editor-header-upgrade/',
87 + 'connect' => $library->get_admin_url( 'authorize', [
88 + 'utm_source' => 'onboarding-wizard',
89 + 'utm_campaign' => 'connect-account',
90 + 'utm_medium' => 'wp-dash',
91 + 'utm_term' => self::VERSION,
92 + 'source' => 'generic',
93 + ] ),
94 + 'upgrade' => 'https://go.elementor.com/go-pro-onboarding-wizard-upgrade/',
95 + 'signUp' => $library->get_admin_url( 'authorize', [
96 + 'utm_source' => 'onboarding-wizard',
97 + 'utm_campaign' => 'connect-account',
98 + 'utm_medium' => 'wp-dash',
99 + 'utm_term' => self::VERSION,
100 + 'source' => 'generic',
101 + 'screen_hint' => 'signup',
102 + ] ),
103 + 'uploadPro' => Plugin::$instance->app->get_base_url() . '#/onboarding/uploadAndInstallPro?mode=popup',
133 104 ],
105 + 'siteLogo' => [
106 + 'id' => $custom_site_logo_id,
107 + 'url' => $custom_logo_src ? $custom_logo_src[0] : '',
108 + ],
109 + 'utms' => [
110 + 'connectTopBar' => '&utm_content=top-bar',
111 + 'connectCta' => '&utm_content=cta-button',
112 + 'connectCtaLink' => '&utm_content=cta-link',
113 + 'downloadPro' => '?utm_source=onboarding-wizard&utm_campaign=my-account-subscriptions&utm_medium=wp-dash&utm_content=import-pro-plugin&utm_term=' . self::VERSION,
114 + ],
115 + 'nonce' => wp_create_nonce( 'onboarding' ),
116 + 'experiment' => Plugin::$instance->experiments->is_feature_active( 'e_onboarding' ),
134 117 ] );
135 118 }
136 119
137 - private function validate_progress_for_steps( User_Progress $progress, array $steps ): array {
138 - $progress_data = $progress->to_array();
139 - $step_ids = array_column( $steps, 'id' );
140 - $step_count = count( $steps );
141 - $fallback_step_id = $steps[0]['id'] ?? 'site_features';
142 - $current_step_index = $progress->get_current_step_index() ?? 0;
143 - $current_step_id = $progress->get_current_step_id() ?? $fallback_step_id;
120 + /**
121 + * Get Permission Error Response
122 + *
123 + * Returns the response that is returned when the user's capabilities are not sufficient for performing an action.
124 + *
125 + * @since 3.6.4
126 + *
127 + * @return array
128 + */
129 + private function get_permission_error_response() {
130 + return [
131 + 'status' => 'error',
132 + 'payload' => [
133 + 'error_message' => esc_html__( 'You do not have permission to perform this action.', 'elementor' ),
134 + ],
135 + ];
136 + }
144 137
145 - $is_invalid_step_index = $current_step_index < 0 || $current_step_index >= $step_count;
146 - $is_invalid_step_id = ! in_array( $current_step_id, $step_ids, true );
138 + /**
139 + * Maybe Update Site Logo
140 + *
141 + * If a new name is provided, it will be updated as the Site Name.
142 + *
143 + * @since 3.6.0
144 + *
145 + * @return array
146 + */
147 + private function maybe_update_site_name() {
148 + $problem_error = [
149 + 'status' => 'error',
150 + 'payload' => [
151 + 'error_message' => esc_html__( 'There was a problem setting your site name.', 'elementor' ),
152 + ],
153 + ];
147 154
148 - if ( $is_invalid_step_index || $is_invalid_step_id ) {
149 - $current_step_id = $fallback_step_id;
150 - $current_step_index = 0;
155 + // phpcs:ignore WordPress.Security.NonceVerification.Missing
156 + if ( empty( $_POST['data'] ) ) {
157 + return $problem_error;
151 158 }
152 159
153 - $progress_data['current_step_id'] = $current_step_id;
154 - $progress_data['current_step_index'] = $current_step_index;
155 - $progress_data['completed_steps'] = array_values( array_filter(
156 - $progress->get_completed_steps(),
157 - static fn( $step_id ) => in_array( $step_id, $step_ids, true )
158 - ) );
160 + // phpcs:ignore WordPress.Security.NonceVerification.Missing
161 + $data = json_decode( Utils::get_super_global_value( $_POST, 'data' ), true );
159 162
160 - return $progress_data;
161 - }
163 + if ( ! isset( $data['siteName'] ) ) {
164 + return $problem_error;
165 + }
162 166
163 - private function is_user_connected(): bool {
164 - $library = $this->get_library_app();
167 + /**
168 + * Onboarding Site Name
169 + *
170 + * Filters the new site name passed by the user to update in Elementor's onboarding process.
171 + * Elementor runs `esc_html()` on the Site Name passed by the user for security reasons. If a user wants to
172 + * include special characters in their site name, they can use this filter to override it.
173 + *
174 + * @since 3.6.0
175 + *
176 + * @param string Escaped new site name
177 + */
178 + $new_site_name = apply_filters( 'elementor/onboarding/site-name', $data['siteName'] );
165 179
166 - return $library ? $library->is_connected() : false;
180 + // The site name is sanitized in `update_options()`
181 + update_option( 'blogname', $new_site_name );
182 +
183 + return [
184 + 'status' => 'success',
185 + 'payload' => [
186 + 'siteNameUpdated' => true,
187 + ],
188 + ];
167 189 }
168 190
169 - private function get_connect_url( string $screen_hint = '' ): string {
170 - $library = $this->get_library_app();
171 -
172 - if ( ! $library ) {
173 - return '';
191 + /**
192 + * Maybe Update Site Logo
193 + *
194 + * If an image attachment ID is provided, it will be updated as the Site Logo Theme Mod.
195 + *
196 + * @since 3.6.0
197 + *
198 + * @return array
199 + */
200 + private function maybe_update_site_logo() {
201 + if ( ! current_user_can( 'edit_theme_options' ) ) {
202 + return $this->get_permission_error_response();
174 203 }
175 204
176 - return $library->get_admin_url( 'authorize', [
177 - 'utm_source' => 'onboarding-wizard',
178 - 'utm_campaign' => 'connect-account',
179 - 'utm_medium' => 'wp-dash',
180 - 'utm_term' => self::VERSION,
181 - 'source' => 'generic',
182 - 'screen_hint' => $screen_hint,
183 - ] ) ?? '';
184 - }
205 + $data_error = [
206 + 'status' => 'error',
207 + 'payload' => [
208 + 'error_message' => esc_html__( 'There was a problem setting your site logo.', 'elementor' ),
209 + ],
210 + ];
185 211
186 - private function get_library_app() {
187 - $connect = Plugin::instance()->common->get_component( 'connect' );
188 -
189 - if ( ! $connect ) {
190 - return null;
212 + // phpcs:ignore WordPress.Security.NonceVerification.Missing
213 + if ( empty( $_POST['data'] ) ) {
214 + return $data_error;
191 215 }
192 216
193 - return $connect->get_app( 'library' );
194 - }
217 + // phpcs:ignore WordPress.Security.NonceVerification.Missing
218 + $data = json_decode( Utils::get_super_global_value( $_POST, 'data' ), true );
195 219
196 - public static function should_show_pro_install_screen(): bool {
197 - if ( Utils::has_pro() || Utils::is_pro_installed_and_not_active() ) {
198 - return false;
220 + // If there is no attachment ID passed or it is not a valid ID, exit here.
221 + if ( empty( $data['attachmentId'] ) ) {
222 + return $data_error;
199 223 }
200 224
201 - $connect = Plugin::$instance->common->get_component( 'connect' );
225 + $absint_attachment_id = absint( $data['attachmentId'] );
202 226
203 - if ( ! $connect ) {
204 - return false;
227 + if ( 0 === $absint_attachment_id ) {
228 + return $data_error;
205 229 }
206 230
207 - $pro_install_app = $connect->get_app( 'pro-install' );
231 + $attachment_url = wp_get_attachment_url( $data['attachmentId'] );
208 232
209 - if ( ! $pro_install_app || ! $pro_install_app->is_connected() ) {
210 - return false;
233 + // Check if the attachment exists. If it does not, exit here.
234 + if ( ! $attachment_url ) {
235 + return $data_error;
211 236 }
212 237
213 - $download_link = $pro_install_app->get_download_link();
238 + set_theme_mod( 'custom_logo', $absint_attachment_id );
214 239
215 - return ! empty( $download_link );
240 + return [
241 + 'status' => 'success',
242 + 'payload' => [
243 + 'siteLogoUpdated' => true,
244 + ],
245 + ];
216 246 }
217 247
218 - private function get_ui_theme_preference(): string {
219 - $editor_preferences = SettingsManager::get_settings_managers( 'editorPreferences' );
248 + /**
249 + * Maybe Upload Logo Image
250 + *
251 + * If an image file upload is provided, and it passes validation, it will be uploaded to the site's Media Library.
252 + *
253 + * @since 3.6.0
254 + *
255 + * @return array
256 + */
257 + private function maybe_upload_logo_image() {
258 + $error_message = esc_html__( 'There was a problem uploading your file.', 'elementor' );
220 259
221 - $ui_theme = $editor_preferences->get_model()->get_settings( 'ui_theme' );
260 + $file = Utils::get_super_global_value( $_FILES, 'fileToUpload' );
222 261
223 - return $ui_theme ? $ui_theme : 'auto';
224 - }
262 + // phpcs:ignore WordPress.Security.NonceVerification.Missing
263 + if ( ! is_array( $file ) || empty( $file['type'] ) ) {
264 + return [
265 + 'status' => 'error',
266 + 'payload' => [
267 + 'error_message' => $error_message,
268 + ],
269 + ];
270 + }
225 271
226 - private function get_user_display_name(): string {
227 - $library = $this->get_library_app();
228 -
229 - if ( ! $library || ! $library->is_connected() ) {
230 - return '';
272 + // If the user has allowed it, set the Request's state as an "Elementor Upload" request, in order to add
273 + // support for non-standard file uploads.
274 + if ( 'image/svg+xml' === $file['type'] ) {
275 + if ( Uploads_Manager::are_unfiltered_uploads_enabled() ) {
276 + Plugin::$instance->uploads_manager->set_elementor_upload_state( true );
277 + } else {
278 + wp_send_json_error( 'To upload SVG files, you must allow uploading unfiltered files.' );
279 + }
231 280 }
232 281
233 - $user = $library->get( 'user' );
282 + // If the image is an SVG file, sanitation is performed during the import (upload) process.
283 + $image_attachment = Plugin::$instance->templates_manager->get_import_images_instance()->import( $file );
234 284
235 - return $user->first_name ?? '';
236 - }
285 + if ( 'image/svg+xml' === $file['type'] && Uploads_Manager::are_unfiltered_uploads_enabled() ) {
286 + // Reset Upload state.
287 + Plugin::$instance->uploads_manager->set_elementor_upload_state( false );
288 + }
237 289
238 - public function should_show_starter(): bool {
239 - $progress = $this->progress_manager->get_progress();
290 + if ( $image_attachment && ! is_wp_error( $image_attachment ) ) {
291 + $result = [
292 + 'status' => 'success',
293 + 'payload' => [
294 + 'imageAttachment' => $image_attachment,
295 + ],
296 + ];
297 + } else {
298 + $result = [
299 + 'status' => 'error',
300 + 'payload' => [
301 + 'error_message' => $error_message,
302 + ],
303 + ];
304 + }
240 305
241 - return self::VERSION === get_option( self::ONBOARDING_OPTION ) && ! $progress->is_starter_dismissed();
306 + return $result;
242 307 }
243 308
244 - public function add_starter_packages( array $packages ): array {
245 - $packages[] = 'editor-starter';
309 + /**
310 + * Activate Hello Theme
311 + *
312 + * @since 3.6.0
313 + *
314 + * @return array
315 + */
316 + private function maybe_activate_hello_theme() {
317 + if ( ! current_user_can( 'switch_themes' ) ) {
318 + return $this->get_permission_error_response();
319 + }
246 320
247 - return $packages;
248 - }
321 + switch_theme( 'hello-elementor' );
249 322
250 - public function add_starter_settings( array $settings ): array {
251 - $settings['starter'] = [
252 - 'restPath' => 'elementor/v1/onboarding/user-progress',
253 - 'aiPlannerUrl' => 'https://planner.elementor.com/home.html',
254 - 'kitLibraryUrl' => Plugin::$instance->app->get_base_url() . '#/kit-library',
323 + return [
324 + 'status' => 'success',
325 + 'payload' => [
326 + 'helloThemeActivated' => true,
327 + ],
255 328 ];
256 -
257 - return $settings;
258 329 }
259 330
260 - private function maybe_invalidate_theme_selection( User_Progress $progress, User_Choices $choices ): void {
261 - $selected_theme = $choices->get_theme_selection();
262 -
263 - if ( empty( $selected_theme ) ) {
264 - return;
331 + /**
332 + * Upload and Install Elementor Pro
333 + *
334 + * @since 3.6.0
335 + *
336 + * @return array
337 + */
338 + private function upload_and_install_pro() {
339 + if ( ! current_user_can( 'install_plugins' ) || ! current_user_can( 'activate_plugins' ) ) {
340 + return $this->get_permission_error_response();
265 341 }
266 342
267 - $active_theme = get_stylesheet();
343 + $error_message = esc_html__( 'There was a problem uploading your file.', 'elementor' );
268 344
269 - if ( $active_theme !== $selected_theme ) {
270 - $completed = $this->filter_out_theme_selection_step( $progress->get_completed_steps() );
271 - $progress->set_completed_steps( $completed );
272 - $this->progress_manager->save_progress( $progress );
345 + $file = Utils::get_super_global_value( $_FILES, 'fileToUpload' ) ?? [];
273 346
274 - $choices->set_theme_selection( null );
275 - $this->progress_manager->save_choices( $choices );
347 + // phpcs:ignore WordPress.Security.NonceVerification.Missing
348 + if ( ! is_array( $file ) || empty( $file['type'] ) ) {
349 + return [
350 + 'status' => 'error',
351 + 'payload' => [
352 + 'error_message' => $error_message,
353 + ],
354 + ];
276 355 }
277 - }
278 356
279 - private function filter_out_theme_selection_step( array $steps ): array {
280 - return array_values( array_filter( $steps, function ( $step ) {
281 - return 'theme_selection' !== $step;
282 - } ) );
283 - }
357 + $result = [];
284 358
285 - private function get_translated_strings(): array {
286 - $locale = $this->get_onboarding_locale();
359 + if ( ! class_exists( 'Automatic_Upgrader_Skin' ) ) {
360 + require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
361 + }
287 362
288 - $api = new EditorAssetsAPI( [
289 - EditorAssetsAPI::ASSETS_DATA_URL => self::ASSETS_BASE_URL . $locale . '.json',
290 - EditorAssetsAPI::ASSETS_DATA_TRANSIENT_KEY => '_elementor_onboarding_strings_' . $locale,
291 - EditorAssetsAPI::ASSETS_DATA_KEY => 'translations',
292 - ] );
363 + $skin = new Automatic_Upgrader_Skin();
364 + $upgrader = new Plugin_Upgrader( $skin );
365 + $upload_result = $upgrader->install( $file['tmp_name'], [ 'overwrite_package' => false ] );
293 366
294 - return $api->get_assets_data();
295 - }
367 + if ( ! $upload_result || is_wp_error( $upload_result ) ) {
368 + $result = [
369 + 'status' => 'error',
370 + 'payload' => [
371 + 'error_message' => $error_message,
372 + ],
373 + ];
374 + } else {
375 + $activated = activate_plugin( WP_PLUGIN_DIR . '/elementor-pro/elementor-pro.php', false, false, true );
296 376
297 - private function get_onboarding_locale(): string {
298 - static $flipped_locales = null;
299 -
300 - if ( null === $flipped_locales ) {
301 - $flipped_locales = array_flip( self::SUPPORTED_LOCALES );
377 + if ( ! is_wp_error( $activated ) ) {
378 + $result = [
379 + 'status' => 'success',
380 + 'payload' => [
381 + 'elementorProInstalled' => true,
382 + ],
383 + ];
384 + } else {
385 + $result = [
386 + 'status' => 'error',
387 + 'payload' => [
388 + 'error_message' => $error_message,
389 + 'elementorProInstalled' => false,
390 + ],
391 + ];
392 + }
302 393 }
303 394
304 - $user_locale = get_user_locale();
395 + return $result;
396 + }
305 397
306 - if ( isset( self::SUPPORTED_LOCALES[ $user_locale ] ) ) {
307 - return $user_locale;
308 - }
398 + private function maybe_update_onboarding_db_option() {
399 + $db_option = get_option( self::ONBOARDING_OPTION );
309 400
310 - $locale = substr( $user_locale, 0, 2 );
311 -
312 - if ( isset( $flipped_locales[ $locale ] ) ) {
313 - return $flipped_locales[ $locale ];
401 + if ( ! $db_option ) {
402 + update_option( self::ONBOARDING_OPTION, true );
314 403 }
315 404
316 - return 'en';
405 + return [
406 + 'status' => 'success',
407 + 'payload' => 'onboarding DB',
408 + ];
317 409 }
318 410
319 - private function get_steps_config(): array {
320 - $steps = [
321 - [
322 - 'id' => 'theme_selection',
323 - 'label' => __( 'Start with a theme that fits your needs', 'elementor' ),
324 - 'type' => 'single',
325 - ],
326 - ];
411 + /**
412 + * Maybe Handle Ajax
413 + *
414 + * This method checks if there are any AJAX actions being
415 + * @since 3.6.0
416 + *
417 + * @return array|null
418 + */
419 + private function maybe_handle_ajax() {
420 + $result = [];
327 421
328 - if ( ! self::is_elementor_pro_installed() ) {
329 - $steps[] = [
330 - 'id' => 'site_features',
331 - 'label' => __( 'What do you want to include in your site?', 'elementor' ),
332 - 'type' => 'multiple',
333 - ];
422 + // phpcs:ignore WordPress.Security.NonceVerification.Missing
423 + switch ( Utils::get_super_global_value( $_POST, 'action' ) ) {
424 + case 'elementor_update_site_name':
425 + // If no value is passed for any reason, no need to update the site name.
426 + $result = $this->maybe_update_site_name();
427 + break;
428 + case 'elementor_update_site_logo':
429 + $result = $this->maybe_update_site_logo();
430 + break;
431 + case 'elementor_upload_site_logo':
432 + $result = $this->maybe_upload_logo_image();
433 + break;
434 + case 'elementor_activate_hello_theme':
435 + $result = $this->maybe_activate_hello_theme();
436 + break;
437 + case 'elementor_upload_and_install_pro':
438 + $result = $this->upload_and_install_pro();
439 + break;
440 + case 'elementor_update_onboarding_option':
441 + $result = $this->maybe_update_onboarding_db_option();
442 + break;
443 + case 'elementor_save_onboarding_features':
444 + // phpcs:ignore WordPress.Security.NonceVerification.Missing
445 + $result = $this->get_component( 'features_usage' )->save_onboarding_features( Utils::get_super_global_value( $_POST, 'data' ) ?? [] );
334 446 }
335 447
336 - return apply_filters( 'elementor/onboarding/steps', $steps );
448 + if ( ! empty( $result ) ) {
449 + if ( 'success' === $result['status'] ) {
450 + wp_send_json_success( $result['payload'] );
451 + } else {
452 + wp_send_json_error( $result['payload'] );
453 + }
454 + }
337 455 }
338 456
339 - private static function is_elementor_pro_installed(): bool {
340 - $is_pro_installed = Utils::has_pro();
341 - return (bool) apply_filters( 'elementor/onboarding/is_elementor_pro_installed', $is_pro_installed );
342 - }
457 + public function __construct() {
458 + $this->add_component( 'features_usage', new Features_Usage() );
343 459
344 - private function is_hello_theme_active(): bool {
345 - $active_theme = get_stylesheet();
346 - $is_active = 0 === strpos( $active_theme, 'hello-' );
460 + add_action( 'elementor/init', function() {
461 + // Only load when viewing the onboarding app.
462 + if ( Plugin::$instance->app->is_current() ) {
463 + $this->set_onboarding_settings();
464 + // Needed for installing the Hello Elementor theme.
465 + wp_enqueue_script( 'updates' );
466 + // Needed for uploading Logo from WP Media Library.
467 + wp_enqueue_media();
468 + }
469 + }, 12 );
347 470
348 - $is_active = (bool) apply_filters( 'elementor/onboarding/is_hello_theme_active', $is_active );
471 + // Needed for uploading Logo from WP Media Library. The 'admin_menu' hook is used because it runs before
472 + // 'admin_init', and the App triggers printing footer scripts on 'admin_init' at priority 0.
473 + add_action( 'admin_menu', function () {
474 + add_action( 'wp_print_footer_scripts', function () {
475 + if ( function_exists( 'wp_print_media_templates' ) ) {
476 + wp_print_media_templates();
477 + }
478 + } );
479 + } );
349 480
350 - return (bool) apply_filters( 'elementor/onboarding/is_elementor_theme_active', $is_active );
481 + add_action( 'admin_init', function() {
482 + if ( wp_doing_ajax() &&
483 + isset( $_POST['action'] ) &&
484 + isset( $_POST['_nonce'] ) &&
485 + wp_verify_nonce( Utils::get_super_global_value( $_POST, '_nonce' ), Ajax::NONCE_KEY ) &&
486 + current_user_can( 'manage_options' )
487 + ) {
488 + $this->maybe_handle_ajax();
489 + }
490 + } );
491 +
492 + $this->get_component( 'features_usage' )->register();
351 493 }
352 494 }