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