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