PluginProbe
Hostinger Tools / 3.0.5
Hostinger Tools v3.0.5
3.0.77 3.0.76 3.0.75 3.0.74 3.0.73 3.0.72 3.0.71 3.0.70 3.0.69 3.0.68 3.0.67 3.0.66 1.8.1 1.8.2 1.8.3 1.9.1 1.9.4 1.9.5 1.9.6 1.9.7 1.9.8 1.9.9 2.0.0 2.0.1 2.0.4 All 108 releases
← All changes | includes/Rest/SettingsRoutes.php +205 -235 3.0.743.0.5 View file →
@@ -2,13 +2,14 @@
2 2
3 3 namespace Hostinger\Rest;
4 4
5 5 if ( ! defined( 'ABSPATH' ) ) {
6 - die;
6 + die;
7 7 }
8 8
9 9 use Hostinger\Admin\Options\PluginOptions;
10 10 use Hostinger\Admin\PluginSettings;
11 +use Hostinger\DefaultOptions;
11 12 use Hostinger\Helper;
12 13
13 14 /**
14 15 * Class for handling Settings Rest API
@@ -13,303 +14,272 @@
13 14 /**
14 15 * Class for handling Settings Rest API
15 16 */
16 17 class SettingsRoutes {
18 + /**
19 + * @var PluginSettings plugin settings.
20 + */
21 + private PluginSettings $plugin_settings;
17 22
23 + /**
24 + * Construct class with dependencies
25 + *
26 + * @param PluginSettings $plugin_settings instance.
27 + */
28 + public function __construct( PluginSettings $plugin_settings ) {
29 + $this->plugin_settings = $plugin_settings;
30 + }
18 31
19 - /**
20 - * @var PluginSettings plugin settings.
21 - */
22 - private PluginSettings $plugin_settings;
32 + /**
33 + * Return all stored plugin settings
34 + *
35 + * @param WP_REST_Request $request WordPress rest request.
36 + *
37 + * @return \WP_REST_Response
38 + */
23 39
24 - /**
25 - * Construct class with dependencies
26 - *
27 - * @param PluginSettings $plugin_settings instance.
28 - */
29 - public function __construct( PluginSettings $plugin_settings ) {
30 - $this->plugin_settings = $plugin_settings;
31 - }
40 + /** PHPCS:disable Generic.CodeAnalysis.UnusedFunctionParameter.Found */
41 + public function get_settings( \WP_REST_Request $request ): \WP_REST_Response {
42 + global $wp_version;
32 43
33 - /**
34 - * Return all stored plugin settings
35 - *
36 - * @param WP_REST_Request $request WordPress rest request.
37 - *
38 - * @return \WP_REST_Response
39 - */
44 + $data = array(
45 + 'newest_wp_version' => $this->get_latest_wordpress_version(),
46 + 'current_wp_version' => $wp_version,
47 + 'php_version' => phpversion(),
48 + 'newest_php_version' => '8.1', // Will be refactored.
49 + 'is_eligible_www_redirect' => $this->is_eligible_www_redirect(),
50 + );
40 51
41 - /** PHPCS:disable Generic.CodeAnalysis.UnusedFunctionParameter.Found */
42 - public function get_settings( \WP_REST_Request $request ): \WP_REST_Response {
43 - global $wp_version;
44 - $plugin_settings = $this->plugin_settings->get_plugin_settings();
52 + // If it is not set for some reason then set it.
53 + if ( empty( $this->plugin_settings->get_plugin_settings()->get_bypass_code() ) ) {
54 + $options = new DefaultOptions();
55 + $options->install_bypass_code();
56 + }
45 57
46 - $data = array(
47 - 'newest_wp_version' => $this->get_latest_wordpress_version(),
48 - 'current_wp_version' => $wp_version,
49 - 'php_version' => phpversion(),
50 - 'newest_php_version' => '8.2', // Will be refactored.
51 - 'is_eligible_www_redirect' => $this->is_eligible_www_redirect(),
52 - 'optin_mcp' => $plugin_settings->get_optin_mcp(),
53 - );
58 + $plugin_settings = $this->plugin_settings->get_plugin_settings()->to_array();
54 59
55 - $hostinger_plugin_settings = get_option( HOSTINGER_PLUGIN_SETTINGS_OPTION, array() );
60 + $response = array(
61 + 'data' => array_merge( $data, $plugin_settings ),
62 + );
56 63
57 - if ( empty( $plugin_settings->get_bypass_code() ) ) {
58 - if ( empty( $hostinger_plugin_settings['bypass_code'] ) ) {
59 - $hostinger_plugin_settings['bypass_code'] = Helper::generate_bypass_code( 16 );
60 - }
61 - }
64 + $response = new \WP_REST_Response( $response );
62 65
63 - $plugin_settings = $plugin_settings->to_array();
66 + $response->set_headers( array( 'Cache-Control' => 'no-cache' ) );
64 67
65 - $response = array(
66 - 'data' => array_merge( $data, $plugin_settings ),
67 - );
68 + $response->set_status( \WP_Http::OK );
68 69
69 - $response = new \WP_REST_Response( $response );
70 + return $response;
71 + }
70 72
71 - $response->set_headers( array( 'Cache-Control' => 'no-cache' ) );
73 + /**
74 + * @param \WP_REST_Request $request
75 + *
76 + * @return \WP_REST_Response
77 + */
78 + public function regenerate_bypass_code( \WP_REST_Request $request ): \WP_REST_Response {
79 + $settings = $this->plugin_settings->get_plugin_settings();
72 80
73 - $response->set_status( \WP_Http::OK );
81 + $settings->set_bypass_code( Helper::generate_bypass_code( 16 ) );
74 82
75 - return $response;
76 - }
83 + $new_settings = $settings->to_array();
77 84
78 - /**
79 - * @param \WP_REST_Request $request
80 - *
81 - * @return \WP_REST_Response
82 - */
83 - public function regenerate_bypass_code( \WP_REST_Request $request ): \WP_REST_Response {
84 - $settings = $this->plugin_settings->get_plugin_settings();
85 + $new_plugin_options = new PluginOptions( $new_settings );
85 86
86 - $settings->set_bypass_code( Helper::generate_bypass_code( 16 ) );
87 + $response = new \WP_REST_Response( array( 'data' => $this->plugin_settings->save_plugin_settings( $new_plugin_options )->to_array() ) );
87 88
88 - $new_settings = $settings->to_array();
89 + $response->set_headers( array( 'Cache-Control' => 'no-cache' ) );
89 90
90 - $new_plugin_options = new PluginOptions( $new_settings );
91 + $response->set_status( \WP_Http::OK );
91 92
92 - $response = new \WP_REST_Response( array( 'data' => $this->plugin_settings->save_plugin_settings( $new_plugin_options )->to_array() ) );
93 + return $response;
94 + }
93 95
94 - $response->set_headers( array( 'Cache-Control' => 'no-cache' ) );
96 + /**
97 + * @param \WP_REST_Request $request
98 + *
99 + * @return \WP_REST_Response
100 + */
101 + public function update_settings( \WP_REST_Request $request ): \WP_REST_Response {
102 + $settings = $this->plugin_settings->get_plugin_settings();
95 103
96 - $response->set_status( \WP_Http::OK );
104 + $new_settings = array();
97 105
98 - return $response;
99 - }
106 + $parameters = $request->get_params();
100 107
101 - /**
102 - * @param \WP_REST_Request $request
103 - *
104 - * @return \WP_REST_Response
105 - */
106 - public function update_settings( \WP_REST_Request $request ): \WP_REST_Response {
107 - $settings = $this->plugin_settings->get_plugin_settings();
108 + foreach ( $settings->to_array() as $field_key => $field_value ) {
109 + // Don't allow to change bypass code
110 + if ( $field_key === 'bypass_code' ) {
111 + $new_settings[ $field_key ] = $field_value;
112 + continue;
113 + }
108 114
109 - $new_settings = array();
115 + if ( isset( $parameters[ $field_key ] ) ) {
116 + $new_settings[ $field_key ] = ! empty( $parameters[ $field_key ] );
117 + } else {
118 + $new_settings[ $field_key ] = $field_value;
119 + }
120 + }
110 121
111 - $parameters = $request->get_params();
122 + $new_plugin_options = new PluginOptions( $new_settings );
112 123
113 - foreach ( $settings->to_array() as $field_key => $field_value ) {
114 - if ( $field_key === 'bypass_code' ) {
115 - $new_settings[ $field_key ] = $field_value;
116 - continue;
117 - }
124 + $response = new \WP_REST_Response( array( 'data' => $this->plugin_settings->save_plugin_settings( $new_plugin_options )->to_array() ) );
118 125
119 - if ( isset( $parameters[ $field_key ] ) ) {
120 - $new_settings[ $field_key ] = ! empty( $parameters[ $field_key ] );
121 - } else {
122 - $new_settings[ $field_key ] = $field_value;
123 - }
126 + $this->update_urls( $new_plugin_options );
124 127
125 - if ( $this->has_changed( $field_key, $new_settings[ $field_key ] ) ) {
126 - do_action( "hostinger_tools_setting_{$field_key}_update", $new_settings[ $field_key ] );
127 - }
128 - }
128 + $response->set_headers( array( 'Cache-Control' => 'no-cache' ) );
129 129
130 - $new_plugin_options = new PluginOptions( $new_settings );
130 + $response->set_status( \WP_Http::OK );
131 131
132 - $response = new \WP_REST_Response( array( 'data' => $this->plugin_settings->save_plugin_settings( $new_plugin_options )->to_array() ) );
132 + return $response;
133 + }
134 + /** PHPCS:enable */
135 + /**
136 + * @param PluginOptions $plugin_options
137 + *
138 + * @return bool
139 + */
140 + private function update_urls( PluginOptions $plugin_options ): bool {
141 + $siteurl = get_option( 'siteurl' );
142 + $home = get_option( 'home' );
133 143
134 - $this->update_urls( $new_plugin_options );
144 + if ( empty( $siteurl ) || empty( $home ) ) {
145 + return false;
146 + }
135 147
136 - $response->set_headers( array( 'Cache-Control' => 'no-cache' ) );
148 + if ( $plugin_options->get_force_https() ) {
149 + $siteurl = str_replace( 'http://', 'https://', $siteurl );
150 + $home = str_replace( 'http://', 'https://', $home );
151 + }
137 152
138 - $response->set_status( \WP_Http::OK );
153 + if ( $plugin_options->get_force_www() ) {
154 + $siteurl = $this->add_www_to_url( $siteurl );
155 + $home = $this->add_www_to_url( $home );
156 + } else {
157 + $siteurl = str_replace( 'www.', '', $siteurl );
158 + $home = str_replace( 'www.', '', $home );
159 + }
139 160
140 - if ( has_action( 'litespeed_purge_all' ) ) {
141 - do_action( 'litespeed_purge_all' );
142 - }
161 + update_option( 'siteurl', $siteurl );
162 + update_option( 'home', $home );
143 163
144 - do_action( 'hostinger_tools_settings_saved', $new_settings );
164 + return true;
165 + }
145 166
146 - return $response;
147 - }
148 - /** PHPCS:enable */
149 - /**
150 - * @param PluginOptions $plugin_options
151 - *
152 - * @return bool
153 - */
154 - private function update_urls( PluginOptions $plugin_options ): bool {
155 - $siteurl = get_option( 'siteurl' );
156 - $home = get_option( 'home' );
167 + /**
168 + * @param string $url
169 + *
170 + * @return mixed
171 + */
172 + private function add_www_to_url( string $url ): string {
173 + $parsed_url = wp_parse_url( $url );
157 174
158 - if ( empty( $siteurl ) || empty( $home ) ) {
159 - return false;
160 - }
175 + if ( isset( $parsed_url['host'] ) ) {
176 + $host = $parsed_url['host'];
161 177
162 - if ( $plugin_options->get_force_https() ) {
163 - $siteurl = str_replace( 'http://', 'https://', $siteurl );
164 - $home = str_replace( 'http://', 'https://', $home );
165 - }
178 + if ( strpos( $host, 'www.' ) !== 0 ) {
179 + $host = 'www.' . $host;
180 + }
166 181
167 - if ( $this->is_eligible_www_redirect() ) {
168 - if ( $plugin_options->get_force_www() ) {
169 - $siteurl = $this->add_www_to_url( $siteurl );
170 - $home = $this->add_www_to_url( $home );
171 - } else {
172 - $siteurl = str_replace( 'www.', '', $siteurl );
173 - $home = str_replace( 'www.', '', $home );
174 - }
175 - }
182 + $parsed_url['host'] = $host;
176 183
177 - update_option( 'siteurl', $siteurl );
178 - update_option( 'home', $home );
184 + return $this->rebuild_url( $parsed_url );
185 + }
179 186
180 - return true;
181 - }
187 + return $url;
188 + }
182 189
183 - /**
184 - * @param string $url
185 - *
186 - * @return mixed
187 - */
188 - private function add_www_to_url( string $url ): string {
189 - $parsed_url = wp_parse_url( $url );
190 + /**
191 + * @param array $params
192 + *
193 + * @return string
194 + */
195 + private function rebuild_url( array $params ): string {
196 + $scheme = isset( $params['scheme'] ) ? $params['scheme'] . '://' : '';
197 + $host = isset( $params['host'] ) ? $params['host'] : '';
198 + $path = isset( $params['path'] ) ? $params['path'] : '';
199 + $query = isset( $params['query'] ) ? '?' . $params['query'] : '';
200 + $fragment = isset( $params['fragment'] ) ? '#' . $params['fragment'] : '';
190 201
191 - if ( isset( $parsed_url['host'] ) ) {
192 - $host = $parsed_url['host'];
202 + return "$scheme$host$path$query$fragment";
203 + }
193 204
194 - if ( strpos( $host, 'www.' ) !== 0 ) {
195 - $host = 'www.' . $host;
196 - }
205 + /**
206 + * @return string
207 + */
208 + private function get_latest_wordpress_version(): string {
209 + $newest_wordpress_version = get_transient( 'hostinger_newest_wordpress_version' );
197 210
198 - $parsed_url['host'] = $host;
211 + if ( $newest_wordpress_version !== false ) {
212 + return $newest_wordpress_version;
213 + }
199 214
200 - return $this->rebuild_url( $parsed_url );
201 - }
215 + $newest_wordpress_version = $this->fetch_wordpress_version();
202 216
203 - return $url;
204 - }
217 + if ( ! empty( $newest_wordpress_version ) ) {
218 + set_transient( 'hostinger_newest_wordpress_version', $newest_wordpress_version, 86400 );
205 219
206 - /**
207 - * @param array $params
208 - *
209 - * @return string
210 - */
211 - private function rebuild_url( array $params ): string {
212 - $scheme = isset( $params['scheme'] ) ? $params['scheme'] . '://' : '';
213 - $host = isset( $params['host'] ) ? $params['host'] : '';
214 - $path = isset( $params['path'] ) ? $params['path'] : '';
215 - $query = isset( $params['query'] ) ? '?' . $params['query'] : '';
216 - $fragment = isset( $params['fragment'] ) ? '#' . $params['fragment'] : '';
220 + return $newest_wordpress_version;
221 + }
217 222
218 - return "$scheme$host$path$query$fragment";
219 - }
223 + return '';
224 + }
220 225
221 - /**
222 - * @return string
223 - */
224 - private function get_latest_wordpress_version(): string {
225 - $newest_wordpress_version = get_transient( 'hostinger_newest_wordpress_version' );
226 + /**
227 + * @return string
228 + */
229 + private function fetch_wordpress_version(): string {
230 + $url = 'https://api.wordpress.org/core/version-check/1.7/';
231 + $response = wp_remote_get( $url );
226 232
227 - if ( $newest_wordpress_version !== false ) {
228 - return $newest_wordpress_version;
229 - }
233 + if ( is_wp_error( $response ) ) {
234 + return '';
235 + }
230 236
231 - $newest_wordpress_version = $this->fetch_wordpress_version();
237 + $response_body = wp_remote_retrieve_body( $response );
232 238
233 - if ( ! empty( $newest_wordpress_version ) ) {
234 - set_transient( 'hostinger_newest_wordpress_version', $newest_wordpress_version, 86400 );
239 + if ( $response_body === false ) {
240 + return '';
241 + }
235 242
236 - return $newest_wordpress_version;
237 - }
243 + $data = json_decode( $response_body, true );
238 244
239 - return '';
240 - }
245 + if ( json_last_error() !== JSON_ERROR_NONE || empty( $data['offers'][0]['current'] ) ) {
246 + return '';
247 + }
241 248
242 - /**
243 - * @return string
244 - */
245 - private function fetch_wordpress_version(): string {
246 - $url = 'https://api.wordpress.org/core/version-check/1.7/';
247 - $response = wp_remote_get( $url );
249 + return $data['offers'][0]['current'];
250 + }
248 251
249 - if ( is_wp_error( $response ) ) {
250 - return '';
251 - }
252 + /**
253 + * @return bool
254 + */
255 + private function is_eligible_www_redirect(): bool {
256 + $is_eligible_www_redirect = get_transient( 'hostinger_is_eligible_www_redirect' );
252 257
253 - $response_body = wp_remote_retrieve_body( $response );
258 + if ( $is_eligible_www_redirect !== false ) {
259 + return $is_eligible_www_redirect;
260 + }
254 261
255 - if ( $response_body === false ) {
256 - return '';
257 - }
262 + $domain = str_replace( 'www.', '', get_option( 'siteurl' ) );
263 + $www_domain = $this->add_www_to_url( $domain );
258 264
259 - $data = json_decode( $response_body, true );
265 + $is_eligible_www_redirect = $this->check_domain_records( $domain, $www_domain );
260 266
261 - if ( json_last_error() !== JSON_ERROR_NONE || empty( $data['offers'][0]['current'] ) ) {
262 - return '';
263 - }
267 + if ( isset( $is_eligible_www_redirect ) ) {
268 + set_transient( 'hostinger_is_eligible_www_redirect', $is_eligible_www_redirect, 120 );
264 269
265 - return $data['offers'][0]['current'];
266 - }
270 + return $is_eligible_www_redirect;
271 + }
267 272
268 - /**
269 - * @return bool
270 - */
271 - private function is_eligible_www_redirect(): bool {
272 - $is_eligible_www_redirect = get_transient( 'hostinger_is_eligible_www_redirect' );
273 + return '';
274 + }
273 275
274 - if ( $is_eligible_www_redirect !== false ) {
275 - return $is_eligible_www_redirect;
276 - }
277 -
278 - $domain = str_replace( 'www.', '', get_option( 'siteurl' ) );
279 - $www_domain = $this->add_www_to_url( $domain );
280 -
281 - $is_eligible_www_redirect = $this->check_domain_records( $domain, $www_domain );
282 -
283 - if ( isset( $is_eligible_www_redirect ) ) {
284 - set_transient( 'hostinger_is_eligible_www_redirect', $is_eligible_www_redirect, 120 );
285 -
286 - return $is_eligible_www_redirect;
287 - }
288 -
289 - return '';
290 - }
291 -
292 - /**
293 - * Check if the field has changed.
294 - */
295 - private function has_changed( string $field, mixed $new_value ): bool {
296 - $settings = $this->plugin_settings->get_plugin_settings();
297 - $settings = $settings->to_array();
298 -
299 - if ( ! array_key_exists( $field, $settings ) ) {
300 - return false;
301 - }
302 -
303 - return $new_value !== $settings[ $field ];
304 - }
305 -
306 - /**
307 - * @param string $domain_a
308 - * @param string $domain_b
309 - *
310 - * @return bool
311 - */
312 - public function check_domain_records( string $domain_a, string $domain_b ): bool {
313 - return ( gethostbyname( $domain_a ) === gethostbyname( $domain_b ) );
314 - }
276 + /**
277 + * @param string $domain_a
278 + * @param string $domain_b
279 + *
280 + * @return bool
281 + */
282 + public function check_domain_records( string $domain_a, string $domain_b ): bool {
283 + return ( gethostbyname( $domain_a ) === gethostbyname( $domain_b ) );
284 + }
315 285 }