Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 39
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
BlueprintsService
0.00% covered (danger)
0.00%
0 / 39
0.00% covered (danger)
0.00%
0 / 7
306
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
 fetch_blueprints
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
 import_blueprint
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
 install_required_plugins
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
12
 get_blueprint_by_slug
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_blueprint_by_resources_url
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_blueprint_by_property
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
42
1<?php
2/**
3 * Blueprints Service
4 *
5 * @package NewfoldLabs\WP\Module\Onboarding\Services\Blueprints
6 */
7
8namespace NewfoldLabs\WP\Module\Onboarding\Services\Blueprints;
9
10use NewfoldLabs\WP\Module\Onboarding\Services\SiteTypes\EcommerceSiteTypeService;
11use NewfoldLabs\WP\Module\Onboarding\Services\ReduxStateService;
12use NewfoldLabs\WP\Module\Patterns\Services\PluginService as PatternsPluginService;
13
14/**
15 * Blueprints Service
16 */
17class BlueprintsService {
18
19    /**
20     * The URL of the blueprints service.
21     *
22     * @var string
23     */
24    private static $blueprints_service_url = 'https://patterns.hiive.cloud/api/v1/blueprints/';
25
26    /**
27     * The redux blueprints data.
28     *
29     * @var array
30     */
31    private $blueprints_data = array();
32
33    public function __construct() {
34        $this->blueprints_data = ReduxStateService::get( 'blueprints' );
35    }
36
37    /**
38     * Fetch the blueprints from the service.
39     *
40     * @return array|WP_Error The blueprints array or a WP_Error if the request fails.
41     */
42    public static function fetch_blueprints() {
43        $response = wp_remote_get( self::$blueprints_service_url );
44        if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
45            return new \WP_Error( 'blueprints_service_error', 'Failed to get blueprints from the service' );
46        }
47        $response   = json_decode( wp_remote_retrieve_body( $response ), true );
48        $blueprints = $response['data'];
49        return $blueprints;
50    }
51
52    /**
53     * Import a blueprint.
54     *
55     * @param string $selected_blueprint_slug The slug of the selected blueprint.
56     * @return bool|WP_Error True if the blueprint is imported, WP_Error if there is an error.
57     */
58    public function import_blueprint( string $selected_blueprint_slug ) {
59        $selected_blueprint = $this->get_blueprint_by_slug( $selected_blueprint_slug );
60        if ( empty( $selected_blueprint ) ) {
61            return new \WP_Error( 'import_blueprint_error', 'Blueprint not found' );
62        }
63
64        $selected_blueprint_resources_url = $selected_blueprint['resources_url'];
65
66        $blueprint_import_service = new BlueprintImportService();
67        $import_result            = $blueprint_import_service->import( $selected_blueprint_resources_url );
68
69        return $import_result;
70    }
71
72    /**
73     * Install the required plugins for a blueprint.
74     *
75     * @param string $selected_blueprint_slug The slug of the selected blueprint.
76     * @return bool|WP_Error True if the plugins are installed, WP_Error if there is an error.
77     */
78    public function install_required_plugins( string $selected_blueprint_slug ) {
79        $selected_blueprint = $this->get_blueprint_by_slug( $selected_blueprint_slug );
80        if ( empty( $selected_blueprint ) ) {
81            return new \WP_Error( 'install_blueprint_required_plugins_error', 'Blueprint not found' );
82        }
83
84        // Enable Jetpack Forms module.
85        PatternsPluginService::enable_jetpack_forms_module();
86
87        // Install WooCommerce plugins (background process).
88        $selected_blueprint_type = $selected_blueprint['type'];
89        if ( 'ecommerce' === $selected_blueprint_type ) {
90            // Install WooCommerce plugins (background process).
91            EcommerceSiteTypeService::install_ecommerce_plugins();
92        }
93
94        return true;
95    }
96
97    /**
98     * Get a blueprint by slug.
99     *
100     * @param string $slug The slug of the blueprint.
101     * @return array|null The blueprint array or null if not found.
102     */
103    public function get_blueprint_by_slug( string $slug ) {
104        return $this->get_blueprint_by_property( 'slug', $slug );
105    }
106
107    /**
108     * Get a blueprint by resources URL.
109     *
110     * @param string $resources_url The resources URL of the blueprint.
111     * @return array|null The blueprint array or null if not found.
112     */
113    public function get_blueprint_by_resources_url( string $resources_url ) {
114        return $this->get_blueprint_by_property( 'resources_url', $resources_url );
115    }
116
117    /**
118     * Get a blueprint by property.
119     *
120     * @param string $property The property of the blueprint.
121     * @param string $value The value of the property.
122     * @return array|null The blueprint array or null if not found.
123     */
124    public function get_blueprint_by_property( string $property, string $value ) {
125        if (
126            empty( $this->blueprints_data ) ||
127            ! isset( $this->blueprints_data['blueprints'] ) ||
128            ! is_array( $this->blueprints_data['blueprints'] ) ||
129            empty( $this->blueprints_data['blueprints'] )
130        ) {
131            return null;
132        }
133
134        $blueprints = $this->blueprints_data['blueprints'];
135
136        $target_blueprint = array_filter(
137            $blueprints,
138            function( $blueprint ) use ( $property, $value ) {
139                return $blueprint[$property] === $value;
140            }
141        );
142        if ( empty( $target_blueprint ) ) {
143            return null;
144        }
145
146        return array_values( $target_blueprint )[0];
147    }
148}