PluginProbe
Elementor Website Builder – more than just a page builder / 3.22.2
Elementor Website Builder – more than just a page builder v3.22.2
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 / module.php

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

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