PluginProbe
Hostinger Tools / 3.0.3
Hostinger Tools v3.0.3
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.3, at includes/Rest/SettingsRoutes.php

286 lines 7.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\DefaultOptions;
12 use Hostinger\Helper;
13
14 /**
15 * Class for handling Settings Rest API
16 */
17 class SettingsRoutes {
18 /**
19 * @var PluginSettings plugin settings.
20 */
21 private PluginSettings $plugin_settings;
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 }
31
32 /**
33 * Return all stored plugin settings
34 *
35 * @param WP_REST_Request $request WordPress rest request.
36 *
37 * @return \WP_REST_Response
38 */
39
40 /** PHPCS:disable Generic.CodeAnalysis.UnusedFunctionParameter.Found */
41 public function get_settings( \WP_REST_Request $request ): \WP_REST_Response {
42 global $wp_version;
43
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 );
51
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 }
57
58 $plugin_settings = $this->plugin_settings->get_plugin_settings()->to_array();
59
60 $response = array(
61 'data' => array_merge( $data, $plugin_settings ),
62 );
63
64 $response = new \WP_REST_Response( $response );
65
66 $response->set_headers( array( 'Cache-Control' => 'no-cache' ) );
67
68 $response->set_status( \WP_Http::OK );
69
70 return $response;
71 }
72
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();
80
81 $settings->set_bypass_code( Helper::generate_bypass_code( 16 ) );
82
83 $new_settings = $settings->to_array();
84
85 $new_plugin_options = new PluginOptions( $new_settings );
86
87 $response = new \WP_REST_Response( array( 'data' => $this->plugin_settings->save_plugin_settings( $new_plugin_options )->to_array() ) );
88
89 $response->set_headers( array( 'Cache-Control' => 'no-cache' ) );
90
91 $response->set_status( \WP_Http::OK );
92
93 return $response;
94 }
95
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();
103
104 $new_settings = array();
105
106 $parameters = $request->get_params();
107
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 }
114
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 }
121
122 $new_plugin_options = new PluginOptions( $new_settings );
123
124 $response = new \WP_REST_Response( array( 'data' => $this->plugin_settings->save_plugin_settings( $new_plugin_options )->to_array() ) );
125
126 $this->update_urls( $new_plugin_options );
127
128 $response->set_headers( array( 'Cache-Control' => 'no-cache' ) );
129
130 $response->set_status( \WP_Http::OK );
131
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' );
143
144 if ( empty( $siteurl ) || empty( $home ) ) {
145 return false;
146 }
147
148 if ( $plugin_options->get_force_https() ) {
149 $siteurl = str_replace( 'http://', 'https://', $siteurl );
150 $home = str_replace( 'http://', 'https://', $home );
151 }
152
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 }
160
161 update_option( 'siteurl', $siteurl );
162 update_option( 'home', $home );
163
164 return true;
165 }
166
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 );
174
175 if ( isset( $parsed_url['host'] ) ) {
176 $host = $parsed_url['host'];
177
178 if ( strpos( $host, 'www.' ) !== 0 ) {
179 $host = 'www.' . $host;
180 }
181
182 $parsed_url['host'] = $host;
183
184 return $this->rebuild_url( $parsed_url );
185 }
186
187 return $url;
188 }
189
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'] : '';
201
202 return "$scheme$host$path$query$fragment";
203 }
204
205 /**
206 * @return string
207 */
208 private function get_latest_wordpress_version(): string {
209 $newest_wordpress_version = get_transient( 'hostinger_newest_wordpress_version' );
210
211 if ( $newest_wordpress_version !== false ) {
212 return $newest_wordpress_version;
213 }
214
215 $newest_wordpress_version = $this->fetch_wordpress_version();
216
217 if ( ! empty( $newest_wordpress_version ) ) {
218 set_transient( 'hostinger_newest_wordpress_version', $newest_wordpress_version, 86400 );
219
220 return $newest_wordpress_version;
221 }
222
223 return '';
224 }
225
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 );
232
233 if ( is_wp_error( $response ) ) {
234 return '';
235 }
236
237 $response_body = wp_remote_retrieve_body( $response );
238
239 if ( $response_body === false ) {
240 return '';
241 }
242
243 $data = json_decode( $response_body, true );
244
245 if ( json_last_error() !== JSON_ERROR_NONE || empty( $data['offers'][0]['current'] ) ) {
246 return '';
247 }
248
249 return $data['offers'][0]['current'];
250 }
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' );
257
258 if ( $is_eligible_www_redirect !== false ) {
259 return $is_eligible_www_redirect;
260 }
261
262 $domain = str_replace( 'www.', '', get_option( 'siteurl' ) );
263 $www_domain = $this->add_www_to_url( $domain );
264
265 $is_eligible_www_redirect = $this->check_domain_records( $domain, $www_domain );
266
267 if ( isset( $is_eligible_www_redirect ) ) {
268 set_transient( 'hostinger_is_eligible_www_redirect', $is_eligible_www_redirect, 120 );
269
270 return $is_eligible_www_redirect;
271 }
272
273 return '';
274 }
275
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 }
285 }
286