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

360 lines 10.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Elementor\App\Modules\Onboarding;
4
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;
10 use Elementor\Core\Base\Module as BaseModule;
11 use Elementor\Core\Settings\Manager as SettingsManager;
12 use Elementor\Includes\EditorAssetsAPI;
13 use Elementor\Plugin;
14 use Elementor\Utils;
15
16 if ( ! defined( 'ABSPATH' ) ) {
17 exit;
18 }
19
20 class Module extends BaseModule {
21
22 const VERSION = '2.0.0';
23 const ASSETS_BASE_URL = 'https://assets.elementor.com/onboarding/v1/strings/';
24 const ONBOARDING_OPTION = 'elementor_onboarded';
25
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 ];
38
39 private Onboarding_Progress_Manager $progress_manager;
40
41 public function get_name(): string {
42 return 'onboarding';
43 }
44
45 public static function has_user_finished_onboarding(): bool {
46 return (bool) get_option( self::ONBOARDING_OPTION );
47 }
48
49 public function __construct() {
50 $this->progress_manager = Onboarding_Progress_Manager::instance();
51
52 Plugin::instance()->data_manager_v2->register_controller( new Controller() );
53
54 add_action( 'elementor/init', [ $this, 'on_elementor_init' ], 12 );
55
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 }
62 }
63
64 public function on_elementor_init(): void {
65 if ( ! Plugin::instance()->app->is_current() ) {
66 return;
67 }
68
69 $this->set_onboarding_settings();
70 $this->enqueue_fonts();
71 }
72
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 );
80 }
81
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 );
91 }
92
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 ) {
99 return;
100 }
101
102 $progress = $this->progress_manager->get_progress();
103 $choices = $this->progress_manager->get_choices();
104 $steps = $this->get_steps_config();
105
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 );
109
110 $is_connected = $this->is_user_connected();
111
112 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,
125 'urls' => [
126 'dashboard' => admin_url(),
127 'editor' => admin_url( 'edit.php?post_type=elementor_library' ),
128 'connect' => $this->get_connect_url(),
129 'comparePlans' => 'https://go.elementor.com/go-pro-onboarding-editor-features-step-upgrade/',
130 'createNewPage' => Plugin::$instance->documents->get_create_new_post_url(),
131 'upgradeUrl' => 'https://go.elementor.com/go-pro-onboarding-editor-header-upgrade/',
132 ],
133 ] );
134 }
135
136 private function validate_progress_for_steps( User_Progress $progress, array $steps ): array {
137 $progress_data = $progress->to_array();
138 $step_count = count( $steps );
139 $current_step_index = $progress->get_current_step_index() ?? 0;
140 $current_step_id = $progress->get_current_step_id() ?? $steps[0]['id'] ?? 'building_for';
141
142 $is_invalid_step_index = $current_step_index < 0 || $current_step_index >= $step_count;
143
144 if ( $is_invalid_step_index ) {
145 $current_step_id = $steps[0]['id'];
146 $current_step_index = 0;
147 }
148
149 $progress_data['current_step_id'] = $current_step_id;
150 $progress_data['current_step_index'] = $current_step_index;
151
152 return $progress_data;
153 }
154
155 private function is_user_connected(): bool {
156 $library = $this->get_library_app();
157
158 return $library ? $library->is_connected() : false;
159 }
160
161 private function get_connect_url(): string {
162 $library = $this->get_library_app();
163
164 if ( ! $library ) {
165 return '';
166 }
167
168 return $library->get_admin_url( 'authorize', [
169 'utm_source' => 'onboarding-wizard',
170 'utm_campaign' => 'connect-account',
171 'utm_medium' => 'wp-dash',
172 'utm_term' => self::VERSION,
173 'source' => 'generic',
174 ] ) ?? '';
175 }
176
177 private function get_library_app() {
178 $connect = Plugin::instance()->common->get_component( 'connect' );
179
180 if ( ! $connect ) {
181 return null;
182 }
183
184 return $connect->get_app( 'library' );
185 }
186
187 public static function should_show_pro_install_screen(): bool {
188 if ( self::is_elementor_pro_installed() ) {
189 return false;
190 }
191
192 $connect = Plugin::$instance->common->get_component( 'connect' );
193
194 if ( ! $connect ) {
195 return false;
196 }
197
198 $pro_install_app = $connect->get_app( 'pro-install' );
199
200 if ( ! $pro_install_app || ! $pro_install_app->is_connected() ) {
201 return false;
202 }
203
204 $download_link = $pro_install_app->get_download_link();
205
206 return ! empty( $download_link );
207 }
208
209 private function get_ui_theme_preference(): string {
210 $editor_preferences = SettingsManager::get_settings_managers( 'editorPreferences' );
211
212 $ui_theme = $editor_preferences->get_model()->get_settings( 'ui_theme' );
213
214 return $ui_theme ? $ui_theme : 'auto';
215 }
216
217 private function get_user_display_name(): string {
218 $library = $this->get_library_app();
219
220 if ( ! $library || ! $library->is_connected() ) {
221 return '';
222 }
223
224 $user = $library->get( 'user' );
225
226 return $user->first_name ?? '';
227 }
228
229 public function should_show_starter(): bool {
230 $progress = $this->progress_manager->get_progress();
231
232 return self::VERSION === get_option( self::ONBOARDING_OPTION ) && ! $progress->is_starter_dismissed();
233 }
234
235 public function add_starter_packages( array $packages ): array {
236 $packages[] = 'editor-starter';
237
238 return $packages;
239 }
240
241 public function add_starter_settings( array $settings ): array {
242 $settings['starter'] = [
243 'restPath' => 'elementor/v1/onboarding/user-progress',
244 'aiPlannerUrl' => 'https://planner.elementor.com/home.html',
245 'kitLibraryUrl' => Plugin::$instance->app->get_base_url() . '#/kit-library',
246 ];
247
248 return $settings;
249 }
250
251 private function maybe_invalidate_theme_selection( User_Progress $progress, User_Choices $choices ): void {
252 $selected_theme = $choices->get_theme_selection();
253
254 if ( empty( $selected_theme ) ) {
255 return;
256 }
257
258 $active_theme = get_stylesheet();
259
260 if ( $active_theme !== $selected_theme ) {
261 $completed = $this->filter_out_theme_selection_step( $progress->get_completed_steps() );
262 $progress->set_completed_steps( $completed );
263 $this->progress_manager->save_progress( $progress );
264
265 $choices->set_theme_selection( null );
266 $this->progress_manager->save_choices( $choices );
267 }
268 }
269
270 private function filter_out_theme_selection_step( array $steps ): array {
271 return array_values( array_filter( $steps, function ( $step ) {
272 return 'theme_selection' !== $step;
273 } ) );
274 }
275
276 private function get_translated_strings(): array {
277 $locale = $this->get_onboarding_locale();
278
279 $api = new EditorAssetsAPI( [
280 EditorAssetsAPI::ASSETS_DATA_URL => self::ASSETS_BASE_URL . $locale . '.json',
281 EditorAssetsAPI::ASSETS_DATA_TRANSIENT_KEY => '_elementor_onboarding_strings_' . $locale,
282 EditorAssetsAPI::ASSETS_DATA_KEY => 'translations',
283 ] );
284
285 return $api->get_assets_data();
286 }
287
288 private function get_onboarding_locale(): string {
289 static $flipped_locales = null;
290
291 if ( null === $flipped_locales ) {
292 $flipped_locales = array_flip( self::SUPPORTED_LOCALES );
293 }
294
295 $user_locale = get_user_locale();
296
297 if ( isset( self::SUPPORTED_LOCALES[ $user_locale ] ) ) {
298 return $user_locale;
299 }
300
301 $locale = substr( $user_locale, 0, 2 );
302
303 if ( isset( $flipped_locales[ $locale ] ) ) {
304 return $flipped_locales[ $locale ];
305 }
306
307 return 'en';
308 }
309
310 private function get_steps_config(): array {
311 $steps = [
312 [
313 'id' => 'building_for',
314 'label' => __( 'Who are you building for?', 'elementor' ),
315 'type' => 'single',
316 ],
317 [
318 'id' => 'site_about',
319 'label' => __( 'What is your site about?', 'elementor' ),
320 'type' => 'multiple',
321 ],
322 [
323 'id' => 'experience_level',
324 'label' => __( 'How much experience do you have with Elementor?', 'elementor' ),
325 'type' => 'single',
326 ],
327 ];
328
329 if ( ! $this->is_elementor_theme_active() ) {
330 $steps[] = [
331 'id' => 'theme_selection',
332 'label' => __( 'Start with a theme that fits your needs', 'elementor' ),
333 'type' => 'single',
334 ];
335 }
336
337 if ( ! self::is_elementor_pro_installed() ) {
338 $steps[] = [
339 'id' => 'site_features',
340 'label' => __( 'What do you want to include in your site?', 'elementor' ),
341 'type' => 'multiple',
342 ];
343 }
344
345 return apply_filters( 'elementor/onboarding/steps', $steps );
346 }
347
348 private static function is_elementor_pro_installed(): bool {
349 $is_pro_installed = Utils::has_pro() || Utils::is_pro_installed_and_not_active();
350 return (bool) apply_filters( 'elementor/onboarding/is_elementor_pro_installed', $is_pro_installed );
351 }
352
353 private function is_elementor_theme_active(): bool {
354 $active_theme = get_stylesheet();
355 $is_active = in_array( $active_theme, Install_Theme::ALLOWED_THEMES, true );
356
357 return (bool) apply_filters( 'elementor/onboarding/is_elementor_theme_active', $is_active );
358 }
359 }
360