PluginProbe
Elementor Website Builder – more than just a page builder / 3.13.1
Elementor Website Builder – more than just a page builder v3.13.1
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.13.1, at app/modules/onboarding/module.php

486 lines 13.3 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 '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',
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 ] );
117 }
118
119 /**
120 * Get Permission Error Response
121 *
122 * Returns the response that is returned when the user's capabilities are not sufficient for performing an action.
123 *
124 * @since 3.6.4
125 *
126 * @return array
127 */
128 private function get_permission_error_response() {
129 return [
130 'status' => 'error',
131 'payload' => [
132 'error_message' => esc_html__( 'you are not allowed to perform this action', 'elementor' ),
133 ],
134 ];
135 }
136
137 /**
138 * Maybe Update Site Logo
139 *
140 * If a new name is provided, it will be updated as the Site Name.
141 *
142 * @since 3.6.0
143 *
144 * @return array
145 */
146 private function maybe_update_site_name() {
147 $problem_error = [
148 'status' => 'error',
149 'payload' => [
150 'error_message' => esc_html__( 'There was a problem setting your site name', 'elementor' ),
151 ],
152 ];
153
154 // phpcs:ignore WordPress.Security.NonceVerification.Missing
155 if ( empty( $_POST['data'] ) ) {
156 return $problem_error;
157 }
158
159 // phpcs:ignore WordPress.Security.NonceVerification.Missing
160 $data = json_decode( Utils::get_super_global_value( $_POST, 'data' ), true );
161
162 if ( ! isset( $data['siteName'] ) ) {
163 return $problem_error;
164 }
165
166 /**
167 * Onboarding Site Name
168 *
169 * Filters the new site name passed by the user to update in Elementor's onboarding process.
170 * Elementor runs `esc_html()` on the Site Name passed by the user for security reasons. If a user wants to
171 * include special characters in their site name, they can use this filter to override it.
172 *
173 * @since 3.6.0
174 *
175 * @param string Escaped new site name
176 */
177 $new_site_name = apply_filters( 'elementor/onboarding/site-name', $data['siteName'] );
178
179 // The site name is sanitized in `update_options()`
180 update_option( 'blogname', $new_site_name );
181
182 return [
183 'status' => 'success',
184 'payload' => [
185 'siteNameUpdated' => true,
186 ],
187 ];
188 }
189
190 /**
191 * Maybe Update Site Logo
192 *
193 * If an image attachment ID is provided, it will be updated as the Site Logo Theme Mod.
194 *
195 * @since 3.6.0
196 *
197 * @return array
198 */
199 private function maybe_update_site_logo() {
200 if ( ! current_user_can( 'edit_theme_options' ) ) {
201 return $this->get_permission_error_response();
202 }
203
204 $data_error = [
205 'status' => 'error',
206 'payload' => [
207 'error_message' => esc_html__( 'There was a problem setting your site logo', 'elementor' ),
208 ],
209 ];
210
211 // phpcs:ignore WordPress.Security.NonceVerification.Missing
212 if ( empty( $_POST['data'] ) ) {
213 return $data_error;
214 }
215
216 // phpcs:ignore WordPress.Security.NonceVerification.Missing
217 $data = json_decode( Utils::get_super_global_value( $_POST, 'data' ), true );
218
219 // If there is no attachment ID passed or it is not a valid ID, exit here.
220 if ( empty( $data['attachmentId'] ) ) {
221 return $data_error;
222 }
223
224 $absint_attachment_id = absint( $data['attachmentId'] );
225
226 if ( 0 === $absint_attachment_id ) {
227 return $data_error;
228 }
229
230 $attachment_url = wp_get_attachment_url( $data['attachmentId'] );
231
232 // Check if the attachment exists. If it does not, exit here.
233 if ( ! $attachment_url ) {
234 return $data_error;
235 }
236
237 set_theme_mod( 'custom_logo', $absint_attachment_id );
238
239 return [
240 'status' => 'success',
241 'payload' => [
242 'siteLogoUpdated' => true,
243 ],
244 ];
245 }
246
247 /**
248 * Maybe Upload Logo Image
249 *
250 * If an image file upload is provided, and it passes validation, it will be uploaded to the site's Media Library.
251 *
252 * @since 3.6.0
253 *
254 * @return array
255 */
256 private function maybe_upload_logo_image() {
257 $error_message = esc_html__( 'There was a problem uploading your file', 'elementor' );
258
259 $file = Utils::get_super_global_value( $_FILES, 'fileToUpload' );
260
261 // phpcs:ignore WordPress.Security.NonceVerification.Missing
262 if ( ! is_array( $file ) || empty( $file['type'] ) ) {
263 return [
264 'status' => 'error',
265 'payload' => [
266 'error_message' => $error_message,
267 ],
268 ];
269 }
270
271 // If the user has allowed it, set the Request's state as an "Elementor Upload" request, in order to add
272 // support for non-standard file uploads.
273 if ( 'image/svg+xml' === $file['type'] ) {
274 if ( Uploads_Manager::are_unfiltered_uploads_enabled() ) {
275 Plugin::$instance->uploads_manager->set_elementor_upload_state( true );
276 } else {
277 wp_send_json_error( 'To upload SVG files, you must allow uploading unfiltered files.' );
278 }
279 }
280
281 // If the image is an SVG file, sanitation is performed during the import (upload) process.
282 $image_attachment = Plugin::$instance->templates_manager->get_import_images_instance()->import( $file );
283
284 if ( 'image/svg+xml' === $file['type'] && Uploads_Manager::are_unfiltered_uploads_enabled() ) {
285 // Reset Upload state.
286 Plugin::$instance->uploads_manager->set_elementor_upload_state( false );
287 }
288
289 if ( $image_attachment && ! is_wp_error( $image_attachment ) ) {
290 $result = [
291 'status' => 'success',
292 'payload' => [
293 'imageAttachment' => $image_attachment,
294 ],
295 ];
296 } else {
297 $result = [
298 'status' => 'error',
299 'payload' => [
300 'error_message' => $error_message,
301 ],
302 ];
303 }
304
305 return $result;
306 }
307
308 /**
309 * Activate Hello Theme
310 *
311 * @since 3.6.0
312 *
313 * @return array
314 */
315 private function maybe_activate_hello_theme() {
316 if ( ! current_user_can( 'switch_themes' ) ) {
317 return $this->get_permission_error_response();
318 }
319
320 switch_theme( 'hello-elementor' );
321
322 return [
323 'status' => 'success',
324 'payload' => [
325 'helloThemeActivated' => true,
326 ],
327 ];
328 }
329
330 /**
331 * Upload and Install Elementor Pro
332 *
333 * @since 3.6.0
334 *
335 * @return array
336 */
337 private function upload_and_install_pro() {
338 if ( ! current_user_can( 'install_plugins' ) || ! current_user_can( 'activate_plugins' ) ) {
339 return $this->get_permission_error_response();
340 }
341
342 $error_message = esc_html__( 'There was a problem uploading your file', 'elementor' );
343
344 $file = Utils::get_super_global_value( $_FILES, 'fileToUpload' ) ?? [];
345
346 // phpcs:ignore WordPress.Security.NonceVerification.Missing
347 if ( ! is_array( $file ) || empty( $file['type'] ) ) {
348 return [
349 'status' => 'error',
350 'payload' => [
351 'error_message' => $error_message,
352 ],
353 ];
354 }
355
356 $result = [];
357
358 if ( ! class_exists( 'Automatic_Upgrader_Skin' ) ) {
359 require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
360 }
361
362 $skin = new Automatic_Upgrader_Skin();
363 $upgrader = new Plugin_Upgrader( $skin );
364 $upload_result = $upgrader->install( $file['tmp_name'], [ 'overwrite_package' => false ] );
365
366 if ( ! $upload_result || is_wp_error( $upload_result ) ) {
367 $result = [
368 'status' => 'error',
369 'payload' => [
370 'error_message' => $error_message,
371 ],
372 ];
373 } else {
374 $activated = activate_plugin( WP_PLUGIN_DIR . '/elementor-pro/elementor-pro.php', false, false, true );
375
376 if ( ! is_wp_error( $activated ) ) {
377 $result = [
378 'status' => 'success',
379 'payload' => [
380 'elementorProInstalled' => true,
381 ],
382 ];
383 } else {
384 $result = [
385 'status' => 'error',
386 'payload' => [
387 'error_message' => $error_message,
388 'elementorProInstalled' => false,
389 ],
390 ];
391 }
392 }
393
394 return $result;
395 }
396
397 private function maybe_update_onboarding_db_option() {
398 $db_option = get_option( self::ONBOARDING_OPTION );
399
400 if ( ! $db_option ) {
401 update_option( self::ONBOARDING_OPTION, true );
402 }
403
404 return [
405 'status' => 'success',
406 'payload' => 'onboarding DB',
407 ];
408 }
409
410 /**
411 * Maybe Handle Ajax
412 *
413 * This method checks if there are any AJAX actions being
414 * @since 3.6.0
415 *
416 * @return array|null
417 */
418 private function maybe_handle_ajax() {
419 $result = [];
420
421 // phpcs:ignore WordPress.Security.NonceVerification.Missing
422 switch ( Utils::get_super_global_value( $_POST, 'action' ) ) {
423 case 'elementor_update_site_name':
424 // If no value is passed for any reason, no need ot update the site name.
425 $result = $this->maybe_update_site_name();
426 break;
427 case 'elementor_update_site_logo':
428 $result = $this->maybe_update_site_logo();
429 break;
430 case 'elementor_upload_site_logo':
431 $result = $this->maybe_upload_logo_image();
432 break;
433 case 'elementor_activate_hello_theme':
434 $result = $this->maybe_activate_hello_theme();
435 break;
436 case 'elementor_upload_and_install_pro':
437 $result = $this->upload_and_install_pro();
438 break;
439 case 'elementor_update_onboarding_option':
440 $result = $this->maybe_update_onboarding_db_option();
441 }
442
443 if ( ! empty( $result ) ) {
444 if ( 'success' === $result['status'] ) {
445 wp_send_json_success( $result['payload'] );
446 } else {
447 wp_send_json_error( $result['payload'] );
448 }
449 }
450 }
451
452 public function __construct() {
453 add_action( 'elementor/init', function() {
454 // Only load when viewing the onboarding app.
455 if ( Plugin::$instance->app->is_current() ) {
456 $this->set_onboarding_settings();
457 // Needed for installing the Hello Elementor theme.
458 wp_enqueue_script( 'updates' );
459 // Needed for uploading Logo from WP Media Library.
460 wp_enqueue_media();
461 }
462 }, 12 );
463
464 // Needed for uploading Logo from WP Media Library. The 'admin_menu' hook is used because it runs before
465 // 'admin_init', and the App triggers printing footer scripts on 'admin_init' at priority 0.
466 add_action( 'admin_menu', function () {
467 add_action( 'wp_print_footer_scripts', function () {
468 if ( function_exists( 'wp_print_media_templates' ) ) {
469 wp_print_media_templates();
470 }
471 } );
472 } );
473
474 add_action( 'admin_init', function() {
475 if ( wp_doing_ajax() &&
476 isset( $_POST['action'] ) &&
477 isset( $_POST['_nonce'] ) &&
478 wp_verify_nonce( Utils::get_super_global_value( $_POST, '_nonce' ), Ajax::NONCE_KEY ) &&
479 current_user_can( 'manage_options' )
480 ) {
481 $this->maybe_handle_ajax();
482 }
483 } );
484 }
485 }
486