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

353 lines 10.4 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\Storage\Entities\User_Choices;
7 use Elementor\App\Modules\Onboarding\Storage\Entities\User_Progress;
8 use Elementor\App\Modules\Onboarding\Storage\Onboarding_Progress_Manager;
9 use Elementor\Core\Base\Module as BaseModule;
10 use Elementor\Core\Settings\Manager as SettingsManager;
11 use Elementor\Includes\EditorAssetsAPI;
12 use Elementor\Plugin;
13 use Elementor\Utils;
14
15 if ( ! defined( 'ABSPATH' ) ) {
16 exit;
17 }
18
19 class Module extends BaseModule {
20
21 const VERSION = '2.0.0';
22 const ASSETS_BASE_URL = 'https://assets.elementor.com/onboarding/v1/strings/';
23 const ONBOARDING_OPTION = 'elementor_onboarded';
24
25 const SUPPORTED_LOCALES = [
26 'de_DE' => 'de',
27 'es_ES' => 'es',
28 'fr_FR' => 'fr',
29 'he_IL' => 'he',
30 'id_ID' => 'id',
31 'it_IT' => 'it',
32 'nl_NL' => 'nl',
33 'pl_PL' => 'pl',
34 'pt_BR' => 'pt',
35 'tr_TR' => 'tr',
36 ];
37
38 private Onboarding_Progress_Manager $progress_manager;
39
40 public function get_name(): string {
41 return 'onboarding';
42 }
43
44 public static function has_user_finished_onboarding(): bool {
45 return (bool) get_option( self::ONBOARDING_OPTION );
46 }
47
48 public function __construct() {
49 $this->progress_manager = Onboarding_Progress_Manager::instance();
50
51 Plugin::instance()->data_manager_v2->register_controller( new Controller() );
52
53 add_action( 'elementor/init', [ $this, 'on_elementor_init' ], 12 );
54
55 if ( $this->should_show_starter() ) {
56 add_filter( 'elementor/editor/localize_settings', [ $this, 'add_starter_settings' ] );
57 add_filter( 'elementor/editor/v2/packages', [ $this, 'add_starter_packages' ] );
58 add_action( 'elementor/editor/v2/styles/enqueue', [ $this, 'enqueue_fonts' ] );
59 add_action( 'elementor/preview/enqueue_styles', [ $this, 'enqueue_starter_preview_css' ] );
60 }
61 }
62
63 public function on_elementor_init(): void {
64 if ( ! Plugin::instance()->app->is_current() ) {
65 return;
66 }
67
68 $this->set_onboarding_settings();
69 $this->enqueue_fonts();
70 }
71
72 public function enqueue_fonts(): void {
73 wp_enqueue_style(
74 'elementor-onboarding-fonts',
75 'https://fonts.googleapis.com/css2?family=Poppins:wght@500&display=swap',
76 [],
77 ELEMENTOR_VERSION
78 );
79 }
80
81 public function enqueue_starter_preview_css(): void {
82 $css = '
83 #site-header,
84 .page-header { display: var(--e-starter-header-display, none); }
85 ';
86
87 wp_register_style( 'elementor-starter-preview', false );
88 wp_enqueue_style( 'elementor-starter-preview' );
89 wp_add_inline_style( 'elementor-starter-preview', $css );
90 }
91
92 public function progress_manager(): Onboarding_Progress_Manager {
93 return $this->progress_manager;
94 }
95
96 private function set_onboarding_settings(): void {
97 if ( ! Plugin::instance()->common ) {
98 return;
99 }
100
101 $progress = $this->progress_manager->get_progress();
102 $choices = $this->progress_manager->get_choices();
103 $steps = $this->get_steps_config();
104
105 // If the user previously selected a theme but it's no longer the active theme,
106 // clear the theme selection so the user can re-select.
107 $this->maybe_invalidate_theme_selection( $progress, $choices );
108
109 $is_connected = $this->is_user_connected();
110
111 Plugin::$instance->app->set_settings( 'onboarding', [
112 'version' => self::VERSION,
113 'restUrl' => rest_url( 'elementor/v1/onboarding/' ),
114 'nonce' => wp_create_nonce( 'wp_rest' ),
115 'progress' => $this->validate_progress_for_steps( $progress, $steps ),
116 'choices' => $choices->to_array(),
117 'hadUnexpectedExit' => $progress->had_unexpected_exit( self::has_user_finished_onboarding() ),
118 'isConnected' => $is_connected,
119 'userName' => $this->get_user_display_name(),
120 'steps' => $steps,
121 'uiTheme' => $this->get_ui_theme_preference(),
122 'translations' => $this->get_translated_strings(),
123 'shouldShowProInstallScreen' => $is_connected ? $this->should_show_pro_install_screen() : false,
124 'isHelloThemeActive' => $this->is_hello_theme_active(),
125 '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/',
131 'createNewPage' => Plugin::$instance->documents->get_create_new_post_url(),
132 'upgradeUrl' => 'https://go.elementor.com/go-pro-onboarding-editor-header-upgrade/',
133 ],
134 ] );
135 }
136
137 private function validate_progress_for_steps( User_Progress $progress, array $steps ): array {
138 $progress_data = $progress->to_array();
139 $step_ids = array_column( $steps, 'id' );
140 $step_count = count( $steps );
141 $fallback_step_id = $steps[0]['id'] ?? 'site_features';
142 $current_step_index = $progress->get_current_step_index() ?? 0;
143 $current_step_id = $progress->get_current_step_id() ?? $fallback_step_id;
144
145 $is_invalid_step_index = $current_step_index < 0 || $current_step_index >= $step_count;
146 $is_invalid_step_id = ! in_array( $current_step_id, $step_ids, true );
147
148 if ( $is_invalid_step_index || $is_invalid_step_id ) {
149 $current_step_id = $fallback_step_id;
150 $current_step_index = 0;
151 }
152
153 $progress_data['current_step_id'] = $current_step_id;
154 $progress_data['current_step_index'] = $current_step_index;
155 $progress_data['completed_steps'] = array_values( array_filter(
156 $progress->get_completed_steps(),
157 static fn( $step_id ) => in_array( $step_id, $step_ids, true )
158 ) );
159
160 return $progress_data;
161 }
162
163 private function is_user_connected(): bool {
164 $library = $this->get_library_app();
165
166 return $library ? $library->is_connected() : false;
167 }
168
169 private function get_connect_url( string $screen_hint = '' ): string {
170 $library = $this->get_library_app();
171
172 if ( ! $library ) {
173 return '';
174 }
175
176 return $library->get_admin_url( 'authorize', [
177 'utm_source' => 'onboarding-wizard',
178 'utm_campaign' => 'connect-account',
179 'utm_medium' => 'wp-dash',
180 'utm_term' => self::VERSION,
181 'source' => 'generic',
182 'screen_hint' => $screen_hint,
183 ] ) ?? '';
184 }
185
186 private function get_library_app() {
187 $connect = Plugin::instance()->common->get_component( 'connect' );
188
189 if ( ! $connect ) {
190 return null;
191 }
192
193 return $connect->get_app( 'library' );
194 }
195
196 public static function should_show_pro_install_screen(): bool {
197 if ( Utils::has_pro() || Utils::is_pro_installed_and_not_active() ) {
198 return false;
199 }
200
201 $connect = Plugin::$instance->common->get_component( 'connect' );
202
203 if ( ! $connect ) {
204 return false;
205 }
206
207 $pro_install_app = $connect->get_app( 'pro-install' );
208
209 if ( ! $pro_install_app || ! $pro_install_app->is_connected() ) {
210 return false;
211 }
212
213 $download_link = $pro_install_app->get_download_link();
214
215 return ! empty( $download_link );
216 }
217
218 private function get_ui_theme_preference(): string {
219 $editor_preferences = SettingsManager::get_settings_managers( 'editorPreferences' );
220
221 $ui_theme = $editor_preferences->get_model()->get_settings( 'ui_theme' );
222
223 return $ui_theme ? $ui_theme : 'auto';
224 }
225
226 private function get_user_display_name(): string {
227 $library = $this->get_library_app();
228
229 if ( ! $library || ! $library->is_connected() ) {
230 return '';
231 }
232
233 $user = $library->get( 'user' );
234
235 return $user->first_name ?? '';
236 }
237
238 public function should_show_starter(): bool {
239 $progress = $this->progress_manager->get_progress();
240
241 return self::VERSION === get_option( self::ONBOARDING_OPTION ) && ! $progress->is_starter_dismissed();
242 }
243
244 public function add_starter_packages( array $packages ): array {
245 $packages[] = 'editor-starter';
246
247 return $packages;
248 }
249
250 public function add_starter_settings( array $settings ): array {
251 $settings['starter'] = [
252 'restPath' => 'elementor/v1/onboarding/user-progress',
253 'aiPlannerUrl' => 'https://planner.elementor.com/home.html',
254 'kitLibraryUrl' => Plugin::$instance->app->get_base_url() . '#/kit-library',
255 ];
256
257 return $settings;
258 }
259
260 private function maybe_invalidate_theme_selection( User_Progress $progress, User_Choices $choices ): void {
261 $selected_theme = $choices->get_theme_selection();
262
263 if ( empty( $selected_theme ) ) {
264 return;
265 }
266
267 $active_theme = get_stylesheet();
268
269 if ( $active_theme !== $selected_theme ) {
270 $completed = $this->filter_out_theme_selection_step( $progress->get_completed_steps() );
271 $progress->set_completed_steps( $completed );
272 $this->progress_manager->save_progress( $progress );
273
274 $choices->set_theme_selection( null );
275 $this->progress_manager->save_choices( $choices );
276 }
277 }
278
279 private function filter_out_theme_selection_step( array $steps ): array {
280 return array_values( array_filter( $steps, function ( $step ) {
281 return 'theme_selection' !== $step;
282 } ) );
283 }
284
285 private function get_translated_strings(): array {
286 $locale = $this->get_onboarding_locale();
287
288 $api = new EditorAssetsAPI( [
289 EditorAssetsAPI::ASSETS_DATA_URL => self::ASSETS_BASE_URL . $locale . '.json',
290 EditorAssetsAPI::ASSETS_DATA_TRANSIENT_KEY => '_elementor_onboarding_strings_' . $locale,
291 EditorAssetsAPI::ASSETS_DATA_KEY => 'translations',
292 ] );
293
294 return $api->get_assets_data();
295 }
296
297 private function get_onboarding_locale(): string {
298 static $flipped_locales = null;
299
300 if ( null === $flipped_locales ) {
301 $flipped_locales = array_flip( self::SUPPORTED_LOCALES );
302 }
303
304 $user_locale = get_user_locale();
305
306 if ( isset( self::SUPPORTED_LOCALES[ $user_locale ] ) ) {
307 return $user_locale;
308 }
309
310 $locale = substr( $user_locale, 0, 2 );
311
312 if ( isset( $flipped_locales[ $locale ] ) ) {
313 return $flipped_locales[ $locale ];
314 }
315
316 return 'en';
317 }
318
319 private function get_steps_config(): array {
320 $steps = [
321 [
322 'id' => 'theme_selection',
323 'label' => __( 'Start with a theme that fits your needs', 'elementor' ),
324 'type' => 'single',
325 ],
326 ];
327
328 if ( ! self::is_elementor_pro_installed() ) {
329 $steps[] = [
330 'id' => 'site_features',
331 'label' => __( 'What do you want to include in your site?', 'elementor' ),
332 'type' => 'multiple',
333 ];
334 }
335
336 return apply_filters( 'elementor/onboarding/steps', $steps );
337 }
338
339 private static function is_elementor_pro_installed(): bool {
340 $is_pro_installed = Utils::has_pro();
341 return (bool) apply_filters( 'elementor/onboarding/is_elementor_pro_installed', $is_pro_installed );
342 }
343
344 private function is_hello_theme_active(): bool {
345 $active_theme = get_stylesheet();
346 $is_active = 0 === strpos( $active_theme, 'hello-' );
347
348 $is_active = (bool) apply_filters( 'elementor/onboarding/is_hello_theme_active', $is_active );
349
350 return (bool) apply_filters( 'elementor/onboarding/is_elementor_theme_active', $is_active );
351 }
352 }
353