module.php
362 lines
| 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 | '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_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'; |
| 142 | |
| 143 | $is_invalid_step_index = $current_step_index < 0 || $current_step_index >= $step_count; |
| 144 | |
| 145 | if ( $is_invalid_step_index ) { |
| 146 | $current_step_id = $steps[0]['id']; |
| 147 | $current_step_index = 0; |
| 148 | } |
| 149 | |
| 150 | $progress_data['current_step_id'] = $current_step_id; |
| 151 | $progress_data['current_step_index'] = $current_step_index; |
| 152 | |
| 153 | return $progress_data; |
| 154 | } |
| 155 | |
| 156 | private function is_user_connected(): bool { |
| 157 | $library = $this->get_library_app(); |
| 158 | |
| 159 | return $library ? $library->is_connected() : false; |
| 160 | } |
| 161 | |
| 162 | private function get_connect_url( string $screen_hint = '' ): string { |
| 163 | $library = $this->get_library_app(); |
| 164 | |
| 165 | if ( ! $library ) { |
| 166 | return ''; |
| 167 | } |
| 168 | |
| 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 | } |
| 178 | |
| 179 | private function get_library_app() { |
| 180 | $connect = Plugin::instance()->common->get_component( 'connect' ); |
| 181 | |
| 182 | if ( ! $connect ) { |
| 183 | return null; |
| 184 | } |
| 185 | |
| 186 | return $connect->get_app( 'library' ); |
| 187 | } |
| 188 | |
| 189 | public static function should_show_pro_install_screen(): bool { |
| 190 | if ( self::is_elementor_pro_installed() ) { |
| 191 | return false; |
| 192 | } |
| 193 | |
| 194 | $connect = Plugin::$instance->common->get_component( 'connect' ); |
| 195 | |
| 196 | if ( ! $connect ) { |
| 197 | return false; |
| 198 | } |
| 199 | |
| 200 | $pro_install_app = $connect->get_app( 'pro-install' ); |
| 201 | |
| 202 | if ( ! $pro_install_app || ! $pro_install_app->is_connected() ) { |
| 203 | return false; |
| 204 | } |
| 205 | |
| 206 | $download_link = $pro_install_app->get_download_link(); |
| 207 | |
| 208 | return ! empty( $download_link ); |
| 209 | } |
| 210 | |
| 211 | private function get_ui_theme_preference(): string { |
| 212 | $editor_preferences = SettingsManager::get_settings_managers( 'editorPreferences' ); |
| 213 | |
| 214 | $ui_theme = $editor_preferences->get_model()->get_settings( 'ui_theme' ); |
| 215 | |
| 216 | return $ui_theme ? $ui_theme : 'auto'; |
| 217 | } |
| 218 | |
| 219 | private function get_user_display_name(): string { |
| 220 | $library = $this->get_library_app(); |
| 221 | |
| 222 | if ( ! $library || ! $library->is_connected() ) { |
| 223 | return ''; |
| 224 | } |
| 225 | |
| 226 | $user = $library->get( 'user' ); |
| 227 | |
| 228 | return $user->first_name ?? ''; |
| 229 | } |
| 230 | |
| 231 | public function should_show_starter(): bool { |
| 232 | $progress = $this->progress_manager->get_progress(); |
| 233 | |
| 234 | return self::VERSION === get_option( self::ONBOARDING_OPTION ) && ! $progress->is_starter_dismissed(); |
| 235 | } |
| 236 | |
| 237 | public function add_starter_packages( array $packages ): array { |
| 238 | $packages[] = 'editor-starter'; |
| 239 | |
| 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', |
| 248 | ]; |
| 249 | |
| 250 | return $settings; |
| 251 | } |
| 252 | |
| 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; |
| 258 | } |
| 259 | |
| 260 | $active_theme = get_stylesheet(); |
| 261 | |
| 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 ); |
| 266 | |
| 267 | $choices->set_theme_selection( null ); |
| 268 | $this->progress_manager->save_choices( $choices ); |
| 269 | } |
| 270 | } |
| 271 | |
| 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 | } |
| 277 | |
| 278 | private function get_translated_strings(): array { |
| 279 | $locale = $this->get_onboarding_locale(); |
| 280 | |
| 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 | ] ); |
| 286 | |
| 287 | return $api->get_assets_data(); |
| 288 | } |
| 289 | |
| 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 ); |
| 295 | } |
| 296 | |
| 297 | $user_locale = get_user_locale(); |
| 298 | |
| 299 | if ( isset( self::SUPPORTED_LOCALES[ $user_locale ] ) ) { |
| 300 | return $user_locale; |
| 301 | } |
| 302 | |
| 303 | $locale = substr( $user_locale, 0, 2 ); |
| 304 | |
| 305 | if ( isset( $flipped_locales[ $locale ] ) ) { |
| 306 | return $flipped_locales[ $locale ]; |
| 307 | } |
| 308 | |
| 309 | return 'en'; |
| 310 | } |
| 311 | |
| 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 | ]; |
| 330 | |
| 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 | ]; |
| 337 | } |
| 338 | |
| 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 | ]; |
| 345 | } |
| 346 | |
| 347 | return apply_filters( 'elementor/onboarding/steps', $steps ); |
| 348 | } |
| 349 | |
| 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 | } |
| 354 | |
| 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 ); |
| 358 | |
| 359 | return (bool) apply_filters( 'elementor/onboarding/is_elementor_theme_active', $is_active ); |
| 360 | } |
| 361 | } |
| 362 |