Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 54 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
PatternsController | |
0.00% |
0 / 54 |
|
0.00% |
0 / 3 |
72 | |
0.00% |
0 / 1 |
register_routes | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
2 | |||
get_pattern_args | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
2 | |||
get_pattern | |
0.00% |
0 / 30 |
|
0.00% |
0 / 1 |
42 |
1 | <?php |
2 | |
3 | namespace NewfoldLabs\WP\Module\Onboarding\RestApi\Themes; |
4 | |
5 | use NewfoldLabs\WP\Module\Onboarding\Data\Patterns; |
6 | use NewfoldLabs\WP\Module\Onboarding\Permissions; |
7 | |
8 | /** |
9 | * Class PatternsController |
10 | */ |
11 | class PatternsController extends \WP_REST_Controller { |
12 | |
13 | |
14 | /** |
15 | * The namespace of this controller's route. |
16 | * |
17 | * @var string |
18 | */ |
19 | protected $namespace = 'newfold-onboarding/v1'; |
20 | |
21 | /** |
22 | * The base of this controller's route. |
23 | * |
24 | * @var string |
25 | */ |
26 | protected $rest_base = '/patterns'; |
27 | |
28 | /** |
29 | * Registers REST routes for this controller class. |
30 | */ |
31 | public function register_routes() { |
32 | |
33 | register_rest_route( |
34 | $this->namespace, |
35 | $this->rest_base, |
36 | array( |
37 | array( |
38 | 'methods' => \WP_REST_Server::READABLE, |
39 | 'callback' => array( $this, 'get_pattern' ), |
40 | 'args' => $this->get_pattern_args(), |
41 | 'permission_callback' => array( Permissions::class, 'rest_is_authorized_admin' ), |
42 | ), |
43 | ) |
44 | ); |
45 | } |
46 | |
47 | /** |
48 | * Checks the type of the patterns. |
49 | * |
50 | * @return array |
51 | */ |
52 | public function get_pattern_args() { |
53 | return array( |
54 | 'slug' => array( |
55 | 'type' => 'string', |
56 | ), |
57 | 'step' => array( |
58 | 'type' => 'string', |
59 | ), |
60 | 'squash' => array( |
61 | 'type' => 'boolean', |
62 | 'default' => false, |
63 | ), |
64 | ); |
65 | } |
66 | |
67 | /** |
68 | * Retrieves the patterns approved by the Onboarding Module. |
69 | * |
70 | * @param \WP_REST_Request $request WP Rest Response object |
71 | * @return \WP_Rest_Response|\WP_Error |
72 | */ |
73 | public function get_pattern( \WP_REST_Request $request ) { |
74 | $step = $request->get_param( 'step' ); |
75 | $squash = $request->get_param( 'squash' ); |
76 | $slug = $request->get_param( 'slug' ); |
77 | |
78 | if ( ! $step && ! $slug ) { |
79 | return new \WP_Error( |
80 | 'missing_params', |
81 | __( 'Pattern identifier (slug) or step name (step) required.', 'wp-module-onboarding' ), |
82 | array( 'status' => 400 ) |
83 | ); |
84 | } |
85 | |
86 | if ( $step ) { |
87 | $step_patterns = Patterns::get_theme_step_patterns_from_step( $step, $squash ); |
88 | if ( ! $step_patterns ) { |
89 | return new \WP_Error( |
90 | 'no_patterns_found', |
91 | __( 'No Patterns Found.', 'wp-module-onboarding' ), |
92 | array( 'status' => 404 ) |
93 | ); |
94 | } |
95 | |
96 | return new \WP_REST_Response( |
97 | $step_patterns |
98 | ); |
99 | } |
100 | |
101 | $pattern = Patterns::get_pattern_from_slug( $slug ); |
102 | if ( ! $pattern ) { |
103 | return new \WP_Error( |
104 | 'no_pattern_found', |
105 | __( 'No Pattern Found.', 'wp-module-onboarding' ), |
106 | array( 'status' => 404 ) |
107 | ); |
108 | } |
109 | |
110 | return new \WP_REST_Response( |
111 | $pattern |
112 | ); |
113 | } |
114 | } |