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