Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
AppService
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 3
30
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 start
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 complete
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace NewfoldLabs\WP\Module\Onboarding\Services;
4
5use NewfoldLabs\WP\Module\Onboarding\Data\Events;
6use NewfoldLabs\WP\Module\Onboarding\Data\Options;
7use NewfoldLabs\WP\Module\Onboarding\Data\Services\PreviewsService;
8use NewfoldLabs\WP\Module\Onboarding\Data\Services\SiteGenService as LegacySiteGenService;
9
10use function NewfoldLabs\WP\ModuleLoader\container;
11
12/**
13 * App Service for handling onboarding application lifecycle.
14 *
15 * This service manages the start and completion of the onboarding process,
16 * including initialization of required services, publishing selected content,
17 * and saving site information for other modules to access.
18 */
19class AppService {
20
21    /**
22     * App Service constructor.
23     *
24     * @return AppService
25     */
26    public function __construct() {
27        return $this;
28    }
29
30    /**
31     * Initialize the App Service.
32     *
33     * @return void
34     */
35    public function start(): void {
36        // Disable SSO redirect.
37        update_option( Options::get_option_name( 'redirect' ), '0' );
38        // If Onboarding is running for the first time...
39        if ( StatusService::handle_started() ) {
40            // Trash sample page.
41            LegacySiteGenService::trash_sample_page();
42
43            // Initialize services.
44            SettingsService::initialize();
45            PluginService::initialize();
46        }
47    }
48
49    /**
50     * Complete onboarding.
51     *
52     * @param string $selected_sitegen_homepage The selected sitegen homepage to publish.
53     * @return void
54     * @throws \Exception When homepage publishing fails.
55     */
56    public function complete( string $selected_sitegen_homepage ): void {
57        // Publish selected homepage.
58        $result = ( new SiteGenService() )->publish_homepage( $selected_sitegen_homepage );
59        if ( \is_wp_error( $result ) ) {
60            throw new \Exception( esc_html( $result->get_error_message() ) );
61        }
62        // Trash Preview pages.
63        PreviewsService::trash_preview_pages();
64
65        // Mark onboarding as completed.
66        StatusService::handle_completed();
67
68        // Purge all caches.
69        container()->get( 'cachePurger' )->purge_all();
70
71        // Create a survey to collect feedback.
72        container()->get( 'survey' )->create_toast_survey(
73            Events::get_category()[0] . '_sitegen_pulse',
74            'customer_satisfaction_survey',
75            array(
76                'label_key' => 'value',
77            ),
78            __( 'Help us improve', 'wp-module-onboarding' ),
79            __( 'How satisfied were you with the ease of creating your website?', 'wp-module-onboarding' ),
80        );
81    }
82}