Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 31
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
AppService
0.00% covered (danger)
0.00%
0 / 31
0.00% covered (danger)
0.00%
0 / 4
42
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 / 4
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
 complete_blueprint
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
2
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        // If Onboarding is running for the first time...
37        if ( StatusService::handle_started() ) {
38            // Trash sample page.
39            LegacySiteGenService::trash_sample_page();
40
41            // Initialize services.
42            SettingsService::initialize();
43            PluginService::initialize();
44        }
45    }
46
47    /**
48     * Complete onboarding.
49     *
50     * @param string $selected_sitegen_homepage The selected sitegen homepage to publish.
51     * @return void
52     * @throws \Exception When homepage publishing fails.
53     */
54    public function complete( string $selected_sitegen_homepage ): void {
55        // Publish selected homepage.
56        $result = ( new SiteGenService() )->publish_homepage( $selected_sitegen_homepage );
57        if ( \is_wp_error( $result ) ) {
58            throw new \Exception( esc_html( $result->get_error_message() ) );
59        }
60        // Trash Preview pages.
61        PreviewsService::trash_preview_pages();
62
63        // Mark onboarding as completed.
64        StatusService::handle_completed();
65
66        // Purge all caches.
67        container()->get( 'cachePurger' )->purge_all();
68
69        // Create a survey to collect feedback.
70        container()->get( 'survey' )->create_toast_survey(
71            Events::get_category()[0] . '_sitegen_pulse',
72            'customer_satisfaction_survey',
73            array(
74                'label_key' => 'value',
75            ),
76            __( 'Help us improve', 'wp-module-onboarding' ),
77            __( 'How satisfied were you with the ease of creating your website?', 'wp-module-onboarding' ),
78        );
79    }
80
81    /**
82     * Complete blueprint onboarding.
83     *
84     * @return void
85     */
86    public function complete_blueprint(): void {
87        // Mark onboarding as completed.
88        StatusService::handle_completed();
89
90        // Purge all caches.
91        container()->get( 'cachePurger' )->purge_all();
92
93        // Create a survey to collect feedback.
94        container()->get( 'survey' )->create_toast_survey(
95            Events::get_category()[0] . '_sitegen_pulse',
96            'customer_satisfaction_survey',
97            array(
98                'label_key' => 'value',
99            ),
100            __( 'Help us improve', 'wp-module-onboarding' ),
101            __( 'How satisfied were you with the ease of creating your website?', 'wp-module-onboarding' ),
102        );
103    }
104}