PluginProbe
Elementor Website Builder – more than just a page builder / 3.6.4
Elementor Website Builder – more than just a page builder v3.6.4
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 / core / app / modules / onboarding / module.php

module.php in Elementor Website Builder – more than just a page builder 3.6.4, at core/app/modules/onboarding/module.php

502 lines 13.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Elementor\Core\App\Modules\Onboarding;
3
4 use Automatic_Upgrader_Skin;
5 use Elementor\Core\Base\Module as BaseModule;
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;
9 use Elementor\Plugin;
10 use Elementor\Tracker;
11 use Plugin_Upgrader;
12
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit; // Exit if accessed directly
15 }
16
17 /**
18 * Onboarding Module
19 *
20 * Responsible for initializing Elementor App functionality
21 *
22 * @since 3.6.0
23 */
24 class Module extends BaseModule {
25
26 const VERSION = '1.0.0';
27 const ONBOARDING_OPTION = 'elementor_onboarded';
28
29 /**
30 * Get name.
31 *
32 * @since 3.6.0
33 * @access public
34 *
35 * @return string
36 */
37 public function get_name() {
38 return 'onboarding';
39 }
40
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 ) {
50 return;
51 }
52
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 ] );
61
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', '' );
65
66 $hello_theme = wp_get_theme( 'hello-elementor' );
67 $hello_theme_errors = is_object( $hello_theme->errors() ) ? $hello_theme->errors()->errors : [];
68
69 /** @var Library $library */
70 $library = Plugin::$instance->common->get_component( 'connect' )->get_app( 'library' );
71
72 Plugin::$instance->app->set_settings( 'onboarding', [
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(),
84 'urls' => [
85 'kitLibrary' => Plugin::$instance->app->get_base_url() . '#/kit-library?order[direction]=desc&order[by]=featuredIndex',
86 'createNewPage' => Plugin::$instance->documents->get_create_new_post_url(),
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 'signUp' => $library->get_admin_url( 'authorize', [
95 'utm_source' => 'onboarding-wizard',
96 'utm_campaign' => 'connect-account',
97 'utm_medium' => 'wp-dash',
98 'utm_term' => self::VERSION,
99 'source' => 'generic',
100 'screen_hint' => 'signup',
101 ] ),
102 'uploadPro' => Plugin::$instance->app->get_base_url() . '#/onboarding/uploadAndInstallPro?mode=popup',
103 ],
104 'siteLogo' => [
105 'id' => $custom_site_logo_id,
106 'url' => $custom_logo_src ? $custom_logo_src[0] : '',
107 ],
108 'utms' => [
109 'connectTopBar' => '&utm_content=top-bar',
110 'connectCta' => '&utm_content=cta-button',
111 'connectCtaLink' => '&utm_content=cta-link',
112 'downloadPro' => '?utm_source=onboarding-wizard&utm_campaign=my-account-subscriptions&utm_medium=wp-dash&utm_content=import-pro-plugin&utm_term=' . self::VERSION,
113 ],
114 'nonce' => wp_create_nonce( 'onboarding' ),
115 ] );
116 }
117
118 /**
119 * Get Permission Error Response
120 *
121 * Returns the response that is returned when the user's capabilities are not sufficient for performing an action.
122 *
123 * @since 3.6.4
124 *
125 * @return array
126 */
127 private function get_permission_error_response() {
128 return [
129 'status' => 'error',
130 'payload' => [
131 'error_message' => __( 'you are not allowed to perform this action', 'elementor' ),
132 ],
133 ];
134 }
135
136 /**
137 * Set Opt In
138 *
139 * This method opts the user into sharing non-sensitive usage data with Elementor.
140 *
141 * @since 3.6.0
142 *
143 * @return array
144 */
145 private function set_usage_data_opt_in() {
146 Tracker::set_opt_in( true );
147
148 return [
149 'status' => 'success',
150 'payload' => [
151 'usageDataShared' => true,
152 ],
153 ];
154 }
155
156 /**
157 * Maybe Update Site Logo
158 *
159 * If a new name is provided, it will be updated as the Site Name.
160 *
161 * @since 3.6.0
162 *
163 * @return array
164 */
165 private function maybe_update_site_name() {
166 $problem_error = [
167 'status' => 'error',
168 'payload' => [
169 'error_message' => 'There was a problem setting your site name',
170 ],
171 ];
172
173 // phpcs:ignore WordPress.Security.NonceVerification.Missing
174 if ( empty( $_POST['data'] ) ) {
175 return $problem_error;
176 }
177
178 // phpcs:ignore WordPress.Security.NonceVerification.Missing
179 $data = json_decode( stripslashes( $_POST['data'] ), true );
180
181 if ( ! isset( $data['siteName'] ) ) {
182 return $problem_error;
183 }
184
185 /**
186 * Onboarding Site Name
187 *
188 * Filters the new site name passed by the user to update in Elementor's onboarding process.
189 * Elementor runs `esc_html()` on the Site Name passed by the user for security reasons. If a user wants to
190 * include special characters in their site name, they can use this filter to override it.
191 *
192 * @since 3.6.0
193 *
194 * @param string Escaped new site name
195 */
196 $new_site_name = apply_filters( 'elementor/onboarding/site-name', $data['siteName'] );
197
198 // The site name is sanitized in `update_options()`
199 update_option( 'blogname', $new_site_name );
200
201 return [
202 'status' => 'success',
203 'payload' => [
204 'siteNameUpdated' => true,
205 ],
206 ];
207 }
208
209 /**
210 * Maybe Update Site Logo
211 *
212 * If an image attachment ID is provided, it will be updated as the Site Logo Theme Mod.
213 *
214 * @since 3.6.0
215 *
216 * @return array
217 */
218 private function maybe_update_site_logo() {
219 if ( ! current_user_can( 'edit_theme_options' ) ) {
220 return $this->get_permission_error_response();
221 }
222
223 $data_error = [
224 'status' => 'error',
225 'payload' => [
226 'error_message' => esc_html__( 'There was a problem setting your site logo', 'elementor' ),
227 ],
228 ];
229
230 // phpcs:ignore WordPress.Security.NonceVerification.Missing
231 if ( empty( $_POST['data'] ) ) {
232 return $data_error;
233 }
234
235 // phpcs:ignore WordPress.Security.NonceVerification.Missing
236 $data = json_decode( stripslashes( $_POST['data'] ), true );
237
238 // If there is no attachment ID passed or it is not a valid ID, exit here.
239 if ( empty( $data['attachmentId'] ) ) {
240 return $data_error;
241 }
242
243 $absint_attachment_id = absint( $data['attachmentId'] );
244
245 if ( 0 === $absint_attachment_id ) {
246 return $data_error;
247 }
248
249 $attachment_url = wp_get_attachment_url( $data['attachmentId'] );
250
251 // Check if the attachment exists. If it does not, exit here.
252 if ( ! $attachment_url ) {
253 return $data_error;
254 }
255
256 set_theme_mod( 'custom_logo', $absint_attachment_id );
257
258 return [
259 'status' => 'success',
260 'payload' => [
261 'siteLogoUpdated' => true,
262 ],
263 ];
264 }
265
266 /**
267 * Maybe Upload Logo Image
268 *
269 * If an image file upload is provided, and it passes validation, it will be uploaded to the site's Media Library.
270 *
271 * @since 3.6.0
272 *
273 * @return array
274 */
275 private function maybe_upload_logo_image() {
276 $error_message = __( 'There was a problem uploading your file', 'elementor' );
277
278 // phpcs:ignore WordPress.Security.NonceVerification.Missing
279 if ( empty( $_FILES['fileToUpload'] ) || ! is_array( $_FILES['fileToUpload'] ) ) {
280 return [
281 'status' => 'error',
282 'payload' => [
283 'error_message' => $error_message,
284 ],
285 ];
286 }
287
288 // If the user has allowed it, set the Request's state as an "Elementor Upload" request, in order to add
289 // support for non-standard file uploads.
290 if ( 'image/svg+xml' === $_FILES['fileToUpload']['type'] ) {
291 if ( Uploads_Manager::are_unfiltered_uploads_enabled() ) {
292 Plugin::$instance->uploads_manager->set_elementor_upload_state( true );
293 } else {
294 wp_send_json_error( 'To upload SVG files, you must allow uploading unfiltered files.' );
295 }
296 }
297
298 // If the image is an SVG file, sanitation is performed during the import (upload) process.
299 $image_attachment = Plugin::$instance->templates_manager->get_import_images_instance()->import( $_FILES['fileToUpload'] );
300
301 if ( 'image/svg+xml' === $_FILES['fileToUpload']['type'] && Uploads_Manager::are_unfiltered_uploads_enabled() ) {
302 // Reset Upload state.
303 Plugin::$instance->uploads_manager->set_elementor_upload_state( false );
304 }
305
306 if ( $image_attachment && ! is_wp_error( $image_attachment ) ) {
307 $result = [
308 'status' => 'success',
309 'payload' => [
310 'imageAttachment' => $image_attachment,
311 ],
312 ];
313 } else {
314 $result = [
315 'status' => 'error',
316 'payload' => [
317 'error_message' => $error_message,
318 ],
319 ];
320 }
321
322 return $result;
323 }
324
325 /**
326 * Activate Hello Theme
327 *
328 * @since 3.6.0
329 *
330 * @return array
331 */
332 private function maybe_activate_hello_theme() {
333 if ( ! current_user_can( 'switch_themes' ) ) {
334 return $this->get_permission_error_response();
335 }
336
337 switch_theme( 'hello-elementor' );
338
339 return [
340 'status' => 'success',
341 'payload' => [
342 'helloThemeActivated' => true,
343 ],
344 ];
345 }
346
347 /**
348 * Upload and Install Elementor Pro
349 *
350 * @since 3.6.0
351 *
352 * @return array
353 */
354 private function upload_and_install_pro() {
355 if ( ! current_user_can( 'install_plugins' ) || ! current_user_can( 'activate_plugins' ) ) {
356 return $this->get_permission_error_response();
357 }
358
359 $error_message = __( 'There was a problem uploading your file', 'elementor' );
360
361 // phpcs:ignore WordPress.Security.NonceVerification.Missing
362 if ( empty( $_FILES['fileToUpload'] ) || ! is_array( $_FILES['fileToUpload'] ) ) {
363 return [
364 'status' => 'error',
365 'payload' => [
366 'error_message' => $error_message,
367 ],
368 ];
369 }
370
371 $result = [];
372
373 if ( ! class_exists( 'Automatic_Upgrader_Skin' ) ) {
374 require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
375 }
376
377 $skin = new Automatic_Upgrader_Skin();
378 $upgrader = new Plugin_Upgrader( $skin );
379 $upload_result = $upgrader->install( $_FILES['fileToUpload']['tmp_name'], [ 'overwrite_package' => false ] );
380
381 if ( ! $upload_result || is_wp_error( $upload_result ) ) {
382 $result = [
383 'status' => 'error',
384 'payload' => [
385 'error_message' => $error_message,
386 ],
387 ];
388 } else {
389 $activated = activate_plugin( WP_PLUGIN_DIR . '/elementor-pro/elementor-pro.php', false, false, true );
390
391 if ( ! is_wp_error( $activated ) ) {
392 $result = [
393 'status' => 'success',
394 'payload' => [
395 'elementorProInstalled' => true,
396 ],
397 ];
398 } else {
399 $result = [
400 'status' => 'error',
401 'payload' => [
402 'error_message' => $error_message,
403 'elementorProInstalled' => false,
404 ],
405 ];
406 }
407 }
408
409 return $result;
410 }
411
412 private function maybe_update_onboarding_db_option() {
413 $db_option = get_option( self::ONBOARDING_OPTION );
414
415 if ( ! $db_option ) {
416 update_option( self::ONBOARDING_OPTION, true );
417 }
418
419 return [
420 'status' => 'success',
421 'payload' => 'onboarding DB',
422 ];
423 }
424
425 /**
426 * Maybe Handle Ajax
427 *
428 * This method checks if there are any AJAX actions being
429 * @since 3.6.0
430 *
431 * @return array|null
432 */
433 private function maybe_handle_ajax() {
434 $result = [];
435
436 // phpcs:ignore WordPress.Security.NonceVerification.Missing
437 switch ( $_POST['action'] ) {
438 case 'elementor_update_site_name':
439 // If no value is passed for any reason, no need ot update the site name.
440 $result = $this->maybe_update_site_name();
441 break;
442 case 'elementor_update_site_logo':
443 $result = $this->maybe_update_site_logo();
444 break;
445 case 'elementor_upload_site_logo':
446 $result = $this->maybe_upload_logo_image();
447 break;
448 case 'elementor_update_data_sharing':
449 $result = $this->set_usage_data_opt_in();
450 break;
451 case 'elementor_activate_hello_theme':
452 $result = $this->maybe_activate_hello_theme();
453 break;
454 case 'elementor_upload_and_install_pro':
455 $result = $this->upload_and_install_pro();
456 break;
457 case 'elementor_update_onboarding_option':
458 $result = $this->maybe_update_onboarding_db_option();
459 }
460
461 if ( ! empty( $result ) ) {
462 if ( 'success' === $result['status'] ) {
463 wp_send_json_success( $result['payload'] );
464 } else {
465 wp_send_json_error( $result['payload'] );
466 }
467 }
468 }
469
470 public function __construct() {
471 add_action( 'elementor/init', function() {
472 // Only load when viewing the onboarding app.
473 if ( Plugin::$instance->app->is_current() ) {
474 $this->set_onboarding_settings();
475 // Needed for installing the Hello Elementor theme.
476 wp_enqueue_script( 'updates' );
477 // Needed for uploading Logo from WP Media Library.
478 wp_enqueue_media();
479
480 Plugin::$instance->app->set_settings( 'disable_dark_theme', true );
481 }
482 }, 12 );
483
484 // Needed for uploading Logo from WP Media Library. The 'admin_menu' hook is used because it runs before
485 // 'admin_init', and the App triggers printing footer scripts on 'admin_init' at priority 0.
486 add_action( 'admin_menu', function() {
487 add_action( 'wp_print_footer_scripts', 'wp_print_media_templates' );
488 } );
489
490 add_action( 'admin_init', function() {
491 if ( wp_doing_ajax() &&
492 isset( $_POST['action'] ) &&
493 isset( $_POST['_nonce'] ) &&
494 wp_verify_nonce( $_POST['_nonce'], Ajax::NONCE_KEY ) &&
495 current_user_can( 'manage_options' )
496 ) {
497 $this->maybe_handle_ajax();
498 }
499 } );
500 }
501 }
502