PluginProbe
Hostinger Tools / 3.0.46
Hostinger Tools v3.0.46
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
hostinger / includes / Rest / SettingsRoutes.php

SettingsRoutes.php in Hostinger Tools 3.0.46, at includes/Rest/SettingsRoutes.php

314 lines 9.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Hostinger\Rest;
4
5 if ( ! defined( 'ABSPATH' ) ) {
6 die;
7 }
8
9 use Hostinger\Admin\Options\PluginOptions;
10 use Hostinger\Admin\PluginSettings;
11 use Hostinger\Helper;
12
13 /**
14 * Class for handling Settings Rest API
15 */
16 class SettingsRoutes {
17
18
19 /**
20 * @var PluginSettings plugin settings.
21 */
22 private PluginSettings $plugin_settings;
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 }
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 */
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();
45
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 );
54
55 $hostinger_plugin_settings = get_option( HOSTINGER_PLUGIN_SETTINGS_OPTION, array() );
56
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 }
62
63 $plugin_settings = $plugin_settings->to_array();
64
65 $response = array(
66 'data' => array_merge( $data, $plugin_settings ),
67 );
68
69 $response = new \WP_REST_Response( $response );
70
71 $response->set_headers( array( 'Cache-Control' => 'no-cache' ) );
72
73 $response->set_status( \WP_Http::OK );
74
75 return $response;
76 }
77
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
86 $settings->set_bypass_code( Helper::generate_bypass_code( 16 ) );
87
88 $new_settings = $settings->to_array();
89
90 $new_plugin_options = new PluginOptions( $new_settings );
91
92 $response = new \WP_REST_Response( array( 'data' => $this->plugin_settings->save_plugin_settings( $new_plugin_options )->to_array() ) );
93
94 $response->set_headers( array( 'Cache-Control' => 'no-cache' ) );
95
96 $response->set_status( \WP_Http::OK );
97
98 return $response;
99 }
100
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
109 $new_settings = array();
110
111 $parameters = $request->get_params();
112
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 }
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 }
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 }
129
130 $new_plugin_options = new PluginOptions( $new_settings );
131
132 $response = new \WP_REST_Response( array( 'data' => $this->plugin_settings->save_plugin_settings( $new_plugin_options )->to_array() ) );
133
134 $this->update_urls( $new_plugin_options );
135
136 $response->set_headers( array( 'Cache-Control' => 'no-cache' ) );
137
138 $response->set_status( \WP_Http::OK );
139
140 if ( has_action( 'litespeed_purge_all' ) ) {
141 do_action( 'litespeed_purge_all' );
142 }
143
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' );
155
156 if ( empty( $siteurl ) || empty( $home ) ) {
157 return false;
158 }
159
160 if ( $plugin_options->get_force_https() ) {
161 $siteurl = str_replace( 'http://', 'https://', $siteurl );
162 $home = str_replace( 'http://', 'https://', $home );
163 }
164
165 if ( $this->is_eligible_www_redirect() ) {
166 if ( $plugin_options->get_force_www() ) {
167 $siteurl = $this->add_www_to_url( $siteurl );
168 $home = $this->add_www_to_url( $home );
169 } else {
170 $siteurl = str_replace( 'www.', '', $siteurl );
171 $home = str_replace( 'www.', '', $home );
172 }
173 }
174
175 update_option( 'siteurl', $siteurl );
176 update_option( 'home', $home );
177
178 return true;
179 }
180
181 /**
182 * @param string $url
183 *
184 * @return mixed
185 */
186 private function add_www_to_url( string $url ): string {
187 $parsed_url = wp_parse_url( $url );
188
189 if ( isset( $parsed_url['host'] ) ) {
190 $host = $parsed_url['host'];
191
192 if ( strpos( $host, 'www.' ) !== 0 ) {
193 $host = 'www.' . $host;
194 }
195
196 $parsed_url['host'] = $host;
197
198 return $this->rebuild_url( $parsed_url );
199 }
200
201 return $url;
202 }
203
204 /**
205 * @param array $params
206 *
207 * @return string
208 */
209 private function rebuild_url( array $params ): string {
210 $scheme = isset( $params['scheme'] ) ? $params['scheme'] . '://' : '';
211 $host = isset( $params['host'] ) ? $params['host'] : '';
212 $path = isset( $params['path'] ) ? $params['path'] : '';
213 $query = isset( $params['query'] ) ? '?' . $params['query'] : '';
214 $fragment = isset( $params['fragment'] ) ? '#' . $params['fragment'] : '';
215
216 return "$scheme$host$path$query$fragment";
217 }
218
219 /**
220 * @return string
221 */
222 private function get_latest_wordpress_version(): string {
223 $newest_wordpress_version = get_transient( 'hostinger_newest_wordpress_version' );
224
225 if ( $newest_wordpress_version !== false ) {
226 return $newest_wordpress_version;
227 }
228
229 $newest_wordpress_version = $this->fetch_wordpress_version();
230
231 if ( ! empty( $newest_wordpress_version ) ) {
232 set_transient( 'hostinger_newest_wordpress_version', $newest_wordpress_version, 86400 );
233
234 return $newest_wordpress_version;
235 }
236
237 return '';
238 }
239
240 /**
241 * @return string
242 */
243 private function fetch_wordpress_version(): string {
244 $url = 'https://api.wordpress.org/core/version-check/1.7/';
245 $response = wp_remote_get( $url );
246
247 if ( is_wp_error( $response ) ) {
248 return '';
249 }
250
251 $response_body = wp_remote_retrieve_body( $response );
252
253 if ( $response_body === false ) {
254 return '';
255 }
256
257 $data = json_decode( $response_body, true );
258
259 if ( json_last_error() !== JSON_ERROR_NONE || empty( $data['offers'][0]['current'] ) ) {
260 return '';
261 }
262
263 return $data['offers'][0]['current'];
264 }
265
266 /**
267 * @return bool
268 */
269 private function is_eligible_www_redirect(): bool {
270 $is_eligible_www_redirect = get_transient( 'hostinger_is_eligible_www_redirect' );
271
272 if ( $is_eligible_www_redirect !== false ) {
273 return $is_eligible_www_redirect;
274 }
275
276 $domain = str_replace( 'www.', '', get_option( 'siteurl' ) );
277 $www_domain = $this->add_www_to_url( $domain );
278
279 $is_eligible_www_redirect = $this->check_domain_records( $domain, $www_domain );
280
281 if ( isset( $is_eligible_www_redirect ) ) {
282 set_transient( 'hostinger_is_eligible_www_redirect', $is_eligible_www_redirect, 120 );
283
284 return $is_eligible_www_redirect;
285 }
286
287 return '';
288 }
289
290 /**
291 * Check if the field has changed.
292 */
293 private function has_changed( string $field, mixed $new_value ): bool {
294 $settings = $this->plugin_settings->get_plugin_settings();
295 $settings = $settings->to_array();
296
297 if ( ! array_key_exists( $field, $settings ) ) {
298 return false;
299 }
300
301 return $new_value !== $settings[ $field ];
302 }
303
304 /**
305 * @param string $domain_a
306 * @param string $domain_b
307 *
308 * @return bool
309 */
310 public function check_domain_records( string $domain_a, string $domain_b ): bool {
311 return ( gethostbyname( $domain_a ) === gethostbyname( $domain_b ) );
312 }
313 }
314