Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 98 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
JetpackController | |
0.00% |
0 / 98 |
|
0.00% |
0 / 3 |
182 | |
0.00% |
0 / 1 |
register_routes | |
0.00% |
0 / 24 |
|
0.00% |
0 / 1 |
2 | |||
set_options | |
0.00% |
0 / 50 |
|
0.00% |
0 / 1 |
72 | |||
regenerate_critical_css | |
0.00% |
0 / 24 |
|
0.00% |
0 / 1 |
20 |
1 | <?php |
2 | namespace NewfoldLabs\WP\Module\Performance\RestApi; |
3 | |
4 | use Automattic\Jetpack_Boost\Lib\Critical_CSS\Data_Sync_Actions\Regenerate_CSS; |
5 | use NewfoldLabs\WP\Module\Performance\Permissions; |
6 | |
7 | /** |
8 | * Class JetpackController |
9 | * |
10 | * @package NewfoldLabs\WP\Module\Performance |
11 | */ |
12 | class JetpackController { |
13 | |
14 | /** |
15 | * The REST route namespace. |
16 | * |
17 | * @var string |
18 | */ |
19 | protected $namespace = 'newfold-performance/v1'; |
20 | |
21 | /** |
22 | * The REST route base. |
23 | * |
24 | * @var string |
25 | */ |
26 | protected $rest_base = '/jetpack'; |
27 | |
28 | /** |
29 | * Plugin slug. |
30 | * |
31 | * @var string |
32 | */ |
33 | protected $plugin_slug = 'jetpack-boost'; |
34 | |
35 | /** |
36 | * Register API routes. |
37 | * |
38 | * @return void |
39 | */ |
40 | public function register_routes() { |
41 | register_rest_route( |
42 | $this->namespace, |
43 | $this->rest_base . '/settings', |
44 | array( |
45 | array( |
46 | 'methods' => \WP_REST_Server::CREATABLE, |
47 | 'callback' => array( $this, 'set_options' ), |
48 | 'permission_callback' => function () { |
49 | return current_user_can( 'manage_options' ); |
50 | }, |
51 | ), |
52 | ) |
53 | ); |
54 | register_rest_route( |
55 | $this->namespace, |
56 | $this->rest_base . '/regenerate_critical_css', |
57 | array( |
58 | array( |
59 | 'methods' => \WP_REST_Server::CREATABLE, |
60 | 'callback' => array( $this, 'regenerate_critical_css' ), |
61 | 'permission_callback' => array( Permissions::class, 'rest_is_authorized_admin' ), |
62 | ), |
63 | ) |
64 | ); |
65 | } |
66 | |
67 | /** |
68 | * Set Jetpack options. |
69 | * |
70 | * @param WP_REST_Request $request The request object. |
71 | * @return WP_REST_Response |
72 | */ |
73 | public function set_options( $request ) { |
74 | try { |
75 | $params = $request->get_params(); |
76 | |
77 | if ( ! isset( $params['field'] ) || ! is_array( $params['field'] ) ) { |
78 | return new \WP_REST_Response( |
79 | array( |
80 | 'success' => false, |
81 | 'error' => __( "The parameter 'field' is missing or invalid.", 'wp-module-performance' ), |
82 | ), |
83 | 400 |
84 | ); |
85 | } |
86 | |
87 | $field = $params['field']; |
88 | |
89 | if ( ! isset( $field['id'], $field['value'] ) ) { |
90 | return new \WP_REST_Response( |
91 | array( |
92 | 'success' => false, |
93 | 'error' => __( "The fields 'id' and 'value' are required.", 'wp-module-performance' ), |
94 | ), |
95 | 400 |
96 | ); |
97 | } |
98 | |
99 | if ( 'critical-css-premium' === $field['id'] ) { |
100 | $field['id'] = 'critical-css'; |
101 | } |
102 | |
103 | $option_key = 'jetpack_boost_status_' . $field['id']; |
104 | $option_value = $field['value']; |
105 | |
106 | if ( in_array( $field['id'], array( 'minify-js-excludes', 'minify-css-excludes' ), true ) ) { |
107 | $option_key = 'jetpack_boost_ds_' . str_replace( '-', '_', $field['id'] ); |
108 | $option_value = explode( ',', $field['value'] ); |
109 | } |
110 | |
111 | $result = update_option( $option_key, $option_value ); |
112 | |
113 | if ( false === $result ) { |
114 | return new \WP_REST_Response( |
115 | array( |
116 | 'success' => false, |
117 | 'error' => __( 'An error occurred while updating the option.', 'wp-module-performance' ), |
118 | ), |
119 | 500 |
120 | ); |
121 | } |
122 | |
123 | // Success response. |
124 | return new \WP_REST_Response( |
125 | array( |
126 | 'success' => true, |
127 | 'updated_option' => $option_key, |
128 | 'updated_value' => $option_value, |
129 | ), |
130 | 200 |
131 | ); |
132 | } catch ( \Exception $e ) { |
133 | // Exceptions handling. |
134 | return new \WP_REST_Response( |
135 | array( |
136 | 'success' => false, |
137 | 'error' => __( 'An error occurred while updating the option.', 'wp-module-performance' ) . $e->getMessage(), |
138 | ), |
139 | 500 |
140 | ); |
141 | } |
142 | } |
143 | |
144 | /** |
145 | * Regenerate Critical CSS. |
146 | * |
147 | * @return \WP_REST_Response |
148 | */ |
149 | public function regenerate_critical_css() { |
150 | try { |
151 | $css = new Regenerate_CSS(); |
152 | $result = $css->handle( null, null ); |
153 | |
154 | if ( ! empty( $result['success'] ) && $result['success'] ) { |
155 | // Success response. |
156 | return new \WP_REST_Response( |
157 | array( |
158 | 'success' => true, |
159 | ), |
160 | 200 |
161 | ); |
162 | } else { |
163 | // Case where the process did not throw an exception but failed. |
164 | return new \WP_REST_Response( |
165 | array( |
166 | 'success' => false, |
167 | 'error' => __( 'Failed to regenerate critical CSS.', 'wp-module-performance' ), |
168 | ), |
169 | 400 // Bad Request |
170 | ); |
171 | } |
172 | } catch ( \Exception $e ) { |
173 | // Exception handling. |
174 | return new \WP_REST_Response( |
175 | array( |
176 | 'success' => false, |
177 | 'error' => __( 'An error occurred while regenerating critical CSS.', 'wp-module-performance' ) . $e->getMessage(), |
178 | ), |
179 | 500 // Internal Server Error |
180 | ); |
181 | } |
182 | } |
183 | } |