Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 57 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
| BlueprintsController | |
0.00% |
0 / 57 |
|
0.00% |
0 / 6 |
90 | |
0.00% |
0 / 1 |
| register_routes | |
0.00% |
0 / 31 |
|
0.00% |
0 / 1 |
2 | |||
| install_required_plugins_args | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
| import_blueprint_args | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
| get_blueprints | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| install_required_plugins | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| import_blueprint | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Blueprints Controller |
| 4 | * |
| 5 | * @package NewfoldLabs\WP\Module\Onboarding\RestApi |
| 6 | */ |
| 7 | |
| 8 | namespace NewfoldLabs\WP\Module\Onboarding\RestApi; |
| 9 | |
| 10 | use NewfoldLabs\WP\Module\Onboarding\Permissions; |
| 11 | use NewfoldLabs\WP\Module\Onboarding\Services\Blueprints\BlueprintsService; |
| 12 | |
| 13 | class BlueprintsController { |
| 14 | |
| 15 | /** |
| 16 | * This is the REST API namespace that will be used for our custom API |
| 17 | * |
| 18 | * @var string |
| 19 | */ |
| 20 | protected $namespace = 'newfold-onboarding/v1'; |
| 21 | |
| 22 | /** |
| 23 | * This is the REST endpoint |
| 24 | * |
| 25 | * @var string |
| 26 | */ |
| 27 | protected $rest_base = '/blueprints'; |
| 28 | |
| 29 | /** |
| 30 | * Register the routes for the blueprints controller. |
| 31 | * |
| 32 | * @return void |
| 33 | */ |
| 34 | public function register_routes() { |
| 35 | \register_rest_route( |
| 36 | $this->namespace, |
| 37 | $this->rest_base . '/get-blueprints', |
| 38 | array( |
| 39 | array( |
| 40 | 'methods' => \WP_REST_Server::CREATABLE, |
| 41 | 'callback' => array( $this, 'get_blueprints' ), |
| 42 | 'permission_callback' => array( Permissions::class, 'rest_is_authorized_admin' ), |
| 43 | ), |
| 44 | ) |
| 45 | ); |
| 46 | \register_rest_route( |
| 47 | $this->namespace, |
| 48 | $this->rest_base . '/install-required-plugins', |
| 49 | array( |
| 50 | 'methods' => \WP_REST_Server::CREATABLE, |
| 51 | 'callback' => array( $this, 'install_required_plugins' ), |
| 52 | 'args' => $this->install_required_plugins_args(), |
| 53 | 'permission_callback' => array( Permissions::class, 'rest_is_authorized_admin' ), |
| 54 | ) |
| 55 | ); |
| 56 | \register_rest_route( |
| 57 | $this->namespace, |
| 58 | $this->rest_base . '/import-blueprint', |
| 59 | array( |
| 60 | 'methods' => \WP_REST_Server::CREATABLE, |
| 61 | 'callback' => array( $this, 'import_blueprint' ), |
| 62 | 'args' => $this->import_blueprint_args(), |
| 63 | 'permission_callback' => array( Permissions::class, 'rest_is_authorized_admin' ), |
| 64 | ) |
| 65 | ); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Get the arguments for the install required plugins route. |
| 70 | * |
| 71 | * @return array The arguments. |
| 72 | */ |
| 73 | public function install_required_plugins_args() { |
| 74 | return array( |
| 75 | 'selected_blueprint_slug' => array( |
| 76 | 'type' => 'string', |
| 77 | 'required' => true, |
| 78 | ), |
| 79 | ); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Get the arguments for the import blueprint route. |
| 84 | * |
| 85 | * @return array The arguments. |
| 86 | */ |
| 87 | public function import_blueprint_args() { |
| 88 | return array( |
| 89 | 'selected_blueprint_slug' => array( |
| 90 | 'type' => 'string', |
| 91 | 'required' => true, |
| 92 | ), |
| 93 | ); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Get the blueprints. |
| 98 | * |
| 99 | * @param \WP_REST_Request $request The request object. |
| 100 | * @return \WP_REST_Response The response object. |
| 101 | */ |
| 102 | public function get_blueprints( \WP_REST_Request $request ): \WP_REST_Response { |
| 103 | $blueprints = BlueprintsService::fetch_blueprints(); |
| 104 | if ( is_wp_error( $blueprints ) ) { |
| 105 | return new \WP_REST_Response( $blueprints->get_error_message(), 500 ); |
| 106 | } |
| 107 | |
| 108 | return new \WP_REST_Response( $blueprints, 200 ); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Install the required plugins for a blueprint. |
| 113 | * |
| 114 | * @param \WP_REST_Request $request The request object. |
| 115 | * @return \WP_REST_Response The response object. |
| 116 | */ |
| 117 | public function install_required_plugins( \WP_REST_Request $request ): \WP_REST_Response { |
| 118 | $selected_blueprint_slug = $request->get_param( 'selected_blueprint_slug' ); |
| 119 | $result = (new BlueprintsService())->install_required_plugins( $selected_blueprint_slug ); |
| 120 | if ( is_wp_error( $result ) ) { |
| 121 | return new \WP_REST_Response( $result->get_error_message(), 500 ); |
| 122 | } |
| 123 | |
| 124 | return new \WP_REST_Response( 'Installed required plugins', 202 ); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Import a blueprint. |
| 129 | * |
| 130 | * @param \WP_REST_Request $request The request object. |
| 131 | * @return \WP_REST_Response The response object. |
| 132 | */ |
| 133 | public function import_blueprint( \WP_REST_Request $request ): \WP_REST_Response { |
| 134 | $selected_blueprint_slug = $request->get_param( 'selected_blueprint_slug' ); |
| 135 | $result = (new BlueprintsService())->import_blueprint( $selected_blueprint_slug ); |
| 136 | if ( is_wp_error( $result ) ) { |
| 137 | return new \WP_REST_Response( $result->get_error_message(), 500 ); |
| 138 | } |
| 139 | |
| 140 | return new \WP_REST_Response( 'Blueprint imported successfully', 200 ); |
| 141 | } |
| 142 | } |