PluginProbe
Hostinger Tools / 3.0.41
Hostinger Tools v3.0.41
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.41, at includes/Rest/SettingsRoutes.php

312 lines 9.0 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
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 );
52
53 $hostinger_plugin_settings = get_option( HOSTINGER_PLUGIN_SETTINGS_OPTION, array() );
54
55 if ( empty( $this->plugin_settings->get_plugin_settings()->get_bypass_code() ) ) {
56 if ( empty( $hostinger_plugin_settings['bypass_code'] ) ) {
57 $hostinger_plugin_settings['bypass_code'] = Helper::generate_bypass_code( 16 );
58 }
59 }
60
61 $plugin_settings = $this->plugin_settings->get_plugin_settings()->to_array();
62
63 $response = array(
64 'data' => array_merge( $data, $plugin_settings ),
65 );
66
67 $response = new \WP_REST_Response( $response );
68
69 $response->set_headers( array( 'Cache-Control' => 'no-cache' ) );
70
71 $response->set_status( \WP_Http::OK );
72
73 return $response;
74 }
75
76 /**
77 * @param \WP_REST_Request $request
78 *
79 * @return \WP_REST_Response
80 */
81 public function regenerate_bypass_code( \WP_REST_Request $request ): \WP_REST_Response {
82 $settings = $this->plugin_settings->get_plugin_settings();
83
84 $settings->set_bypass_code( Helper::generate_bypass_code( 16 ) );
85
86 $new_settings = $settings->to_array();
87
88 $new_plugin_options = new PluginOptions( $new_settings );
89
90 $response = new \WP_REST_Response( array( 'data' => $this->plugin_settings->save_plugin_settings( $new_plugin_options )->to_array() ) );
91
92 $response->set_headers( array( 'Cache-Control' => 'no-cache' ) );
93
94 $response->set_status( \WP_Http::OK );
95
96 return $response;
97 }
98
99 /**
100 * @param \WP_REST_Request $request
101 *
102 * @return \WP_REST_Response
103 */
104 public function update_settings( \WP_REST_Request $request ): \WP_REST_Response {
105 $settings = $this->plugin_settings->get_plugin_settings();
106
107 $new_settings = array();
108
109 $parameters = $request->get_params();
110
111 foreach ( $settings->to_array() as $field_key => $field_value ) {
112 if ( $field_key === 'bypass_code' ) {
113 $new_settings[ $field_key ] = $field_value;
114 continue;
115 }
116
117 if ( isset( $parameters[ $field_key ] ) ) {
118 $new_settings[ $field_key ] = ! empty( $parameters[ $field_key ] );
119 } else {
120 $new_settings[ $field_key ] = $field_value;
121 }
122
123 if ( $this->has_changed( $field_key, $new_settings[ $field_key ] ) ) {
124 do_action( "hostinger_tools_setting_{$field_key}_update", $new_settings[ $field_key ] );
125 }
126 }
127
128 $new_plugin_options = new PluginOptions( $new_settings );
129
130 $response = new \WP_REST_Response( array( 'data' => $this->plugin_settings->save_plugin_settings( $new_plugin_options )->to_array() ) );
131
132 $this->update_urls( $new_plugin_options );
133
134 $response->set_headers( array( 'Cache-Control' => 'no-cache' ) );
135
136 $response->set_status( \WP_Http::OK );
137
138 if ( has_action( 'litespeed_purge_all' ) ) {
139 do_action( 'litespeed_purge_all' );
140 }
141
142 return $response;
143 }
144 /** PHPCS:enable */
145 /**
146 * @param PluginOptions $plugin_options
147 *
148 * @return bool
149 */
150 private function update_urls( PluginOptions $plugin_options ): bool {
151 $siteurl = get_option( 'siteurl' );
152 $home = get_option( 'home' );
153
154 if ( empty( $siteurl ) || empty( $home ) ) {
155 return false;
156 }
157
158 if ( $plugin_options->get_force_https() ) {
159 $siteurl = str_replace( 'http://', 'https://', $siteurl );
160 $home = str_replace( 'http://', 'https://', $home );
161 }
162
163 if ( $this->is_eligible_www_redirect() ) {
164 if ( $plugin_options->get_force_www() ) {
165 $siteurl = $this->add_www_to_url( $siteurl );
166 $home = $this->add_www_to_url( $home );
167 } else {
168 $siteurl = str_replace( 'www.', '', $siteurl );
169 $home = str_replace( 'www.', '', $home );
170 }
171 }
172
173 update_option( 'siteurl', $siteurl );
174 update_option( 'home', $home );
175
176 return true;
177 }
178
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 );
186
187 if ( isset( $parsed_url['host'] ) ) {
188 $host = $parsed_url['host'];
189
190 if ( strpos( $host, 'www.' ) !== 0 ) {
191 $host = 'www.' . $host;
192 }
193
194 $parsed_url['host'] = $host;
195
196 return $this->rebuild_url( $parsed_url );
197 }
198
199 return $url;
200 }
201
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'] : '';
213
214 return "$scheme$host$path$query$fragment";
215 }
216
217 /**
218 * @return string
219 */
220 private function get_latest_wordpress_version(): string {
221 $newest_wordpress_version = get_transient( 'hostinger_newest_wordpress_version' );
222
223 if ( $newest_wordpress_version !== false ) {
224 return $newest_wordpress_version;
225 }
226
227 $newest_wordpress_version = $this->fetch_wordpress_version();
228
229 if ( ! empty( $newest_wordpress_version ) ) {
230 set_transient( 'hostinger_newest_wordpress_version', $newest_wordpress_version, 86400 );
231
232 return $newest_wordpress_version;
233 }
234
235 return '';
236 }
237
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 );
244
245 if ( is_wp_error( $response ) ) {
246 return '';
247 }
248
249 $response_body = wp_remote_retrieve_body( $response );
250
251 if ( $response_body === false ) {
252 return '';
253 }
254
255 $data = json_decode( $response_body, true );
256
257 if ( json_last_error() !== JSON_ERROR_NONE || empty( $data['offers'][0]['current'] ) ) {
258 return '';
259 }
260
261 return $data['offers'][0]['current'];
262 }
263
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' );
269
270 if ( $is_eligible_www_redirect !== false ) {
271 return $is_eligible_www_redirect;
272 }
273
274 $domain = str_replace( 'www.', '', get_option( 'siteurl' ) );
275 $www_domain = $this->add_www_to_url( $domain );
276
277 $is_eligible_www_redirect = $this->check_domain_records( $domain, $www_domain );
278
279 if ( isset( $is_eligible_www_redirect ) ) {
280 set_transient( 'hostinger_is_eligible_www_redirect', $is_eligible_www_redirect, 120 );
281
282 return $is_eligible_www_redirect;
283 }
284
285 return '';
286 }
287
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();
294
295 if ( ! array_key_exists( $field, $settings ) ) {
296 return false;
297 }
298
299 return $new_value !== $settings[ $field ];
300 }
301
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 }
311 }
312