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