Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 37 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
PreviewsController | |
0.00% |
0 / 37 |
|
0.00% |
0 / 3 |
20 | |
0.00% |
0 / 1 |
register_routes | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
2 | |||
generate_snapshot_args | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
2 | |||
generate_snapshot | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | |
3 | namespace NewfoldLabs\WP\Module\Onboarding\RestApi; |
4 | |
5 | use NewfoldLabs\WP\Module\Onboarding\Permissions; |
6 | use NewfoldLabs\WP\Module\Onboarding\Data\Services\PreviewsService; |
7 | |
8 | /** |
9 | * PreviewsController class. |
10 | */ |
11 | class PreviewsController { |
12 | |
13 | /** |
14 | * The namespace of this controller's route. |
15 | * |
16 | * @var string |
17 | */ |
18 | protected $namespace = 'newfold-onboarding/v1'; |
19 | |
20 | /** |
21 | * The endpoint base |
22 | * |
23 | * @var string |
24 | */ |
25 | protected $rest_base = '/previews'; |
26 | |
27 | /** |
28 | * Registers rest routes for PreviewsController class. |
29 | * |
30 | * @return void |
31 | */ |
32 | public function register_routes() { |
33 | \register_rest_route( |
34 | $this->namespace, |
35 | $this->rest_base . '/snapshot', |
36 | array( |
37 | 'methods' => \WP_REST_Server::EDITABLE, |
38 | 'callback' => array( $this, 'generate_snapshot' ), |
39 | 'permission_callback' => array( Permissions::class, 'rest_is_authorized_admin' ), |
40 | 'args' => $this->generate_snapshot_args(), |
41 | ) |
42 | ); |
43 | } |
44 | |
45 | /** |
46 | * Args for Generating the snapshot. |
47 | * |
48 | * @return array |
49 | */ |
50 | public function generate_snapshot_args() { |
51 | return array( |
52 | 'content' => array( |
53 | 'required' => true, |
54 | 'type' => 'string', |
55 | ), |
56 | 'slug' => array( |
57 | 'required' => true, |
58 | 'type' => 'string', |
59 | ), |
60 | 'custom_styles' => array( |
61 | 'required' => false, |
62 | 'type' => 'string', |
63 | ), |
64 | ); |
65 | } |
66 | |
67 | /** |
68 | * Generates a snapshot. |
69 | * |
70 | * @param \WP_REST_Request $request The incoming request. |
71 | * @return \WP_REST_Response|\WP_Error |
72 | */ |
73 | public function generate_snapshot( \WP_REST_Request $request ): \WP_REST_Response { |
74 | $content = $request->get_param( 'content' ); |
75 | $slug = $request->get_param( 'slug' ); |
76 | $custom_styles = $request->get_param( 'custom_styles' ); |
77 | |
78 | $snapshot = PreviewsService::generate_snapshot( $content, $slug, $custom_styles ); |
79 | if ( is_wp_error( $snapshot ) ) { |
80 | return new \WP_REST_Response( |
81 | $snapshot->get_error_message(), |
82 | 500 |
83 | ); |
84 | } |
85 | |
86 | return new \WP_REST_Response( |
87 | $snapshot, |
88 | 200 |
89 | ); |
90 | } |
91 | } |