PluginProbe
Hostinger Tools / 3.0.34
Hostinger Tools v3.0.34
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.34, at includes/Rest/SettingsRoutes.php

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