| 1 |
<?php |
| 2 |
namespace Hostinger\Rest; |
| 3 |
|
| 4 |
use Hostinger\Admin\Options\PluginOptions; |
| 5 |
use Hostinger\Admin\PluginSettings; |
| 6 |
use Hostinger\DefaultOptions; |
| 7 |
use Hostinger\Helper; |
| 8 |
|
| 9 |
/** |
| 10 |
* Avoid possibility to get file accessed directly |
| 11 |
*/ |
| 12 |
if ( ! defined( 'ABSPATH' ) ) { |
| 13 |
die; |
| 14 |
} |
| 15 |
|
| 16 |
/** |
| 17 |
* Class for handling Settings Rest API |
| 18 |
*/ |
| 19 |
class SettingsRoutes { |
| 20 |
/** |
| 21 |
* @var PluginSettings plugin settings. |
| 22 |
*/ |
| 23 |
private PluginSettings $plugin_settings; |
| 24 |
|
| 25 |
/** |
| 26 |
* Construct class with dependencies |
| 27 |
* |
| 28 |
* @param PluginSettings $plugin_settings instance. |
| 29 |
*/ |
| 30 |
public function __construct( PluginSettings $plugin_settings ) { |
| 31 |
$this->plugin_settings = $plugin_settings; |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Return all stored plugin settings |
| 36 |
* |
| 37 |
* @param WP_REST_Request $request WordPress rest request. |
| 38 |
* |
| 39 |
* @return \WP_REST_Response |
| 40 |
*/ |
| 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 |
|
| 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 |
$parsedUrl = parse_url( $url ); |
| 174 |
|
| 175 |
if ( isset( $parsedUrl['host'] ) ) { |
| 176 |
$host = $parsedUrl['host']; |
| 177 |
|
| 178 |
if ( strpos($host, 'www.') !== 0 ) { |
| 179 |
$host = 'www.' . $host; |
| 180 |
} |
| 181 |
|
| 182 |
$parsedUrl['host'] = $host; |
| 183 |
|
| 184 |
return $this->rebuild_url($parsedUrl); |
| 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 |
|
| 232 |
$response = file_get_contents($url); |
| 233 |
|
| 234 |
if ($response !== false) { |
| 235 |
$data = json_decode($response); |
| 236 |
|
| 237 |
return !empty($data->offers[0]->current) ? $data->offers[0]->current : ''; |
| 238 |
} |
| 239 |
|
| 240 |
return ''; |
| 241 |
} |
| 242 |
|
| 243 |
/** |
| 244 |
* @return bool |
| 245 |
*/ |
| 246 |
private function is_eligible_www_redirect(): bool { |
| 247 |
$is_eligible_www_redirect = get_transient('hostinger_is_eligible_www_redirect'); |
| 248 |
|
| 249 |
if ($is_eligible_www_redirect !== false) { |
| 250 |
return $is_eligible_www_redirect; |
| 251 |
} |
| 252 |
|
| 253 |
$domain = str_replace('www.', '', get_option( 'siteurl' )); |
| 254 |
$www_domain = $this->add_www_to_url( $domain ); |
| 255 |
|
| 256 |
$is_eligible_www_redirect = $this->check_domain_records($domain, $www_domain); |
| 257 |
|
| 258 |
if(isset($is_eligible_www_redirect)) { |
| 259 |
set_transient('hostinger_is_eligible_www_redirect', $is_eligible_www_redirect, 120); |
| 260 |
|
| 261 |
return $is_eligible_www_redirect; |
| 262 |
} |
| 263 |
|
| 264 |
return ''; |
| 265 |
} |
| 266 |
|
| 267 |
/** |
| 268 |
* @param string $domain_a |
| 269 |
* @param string $domain_b |
| 270 |
* |
| 271 |
* @return bool |
| 272 |
*/ |
| 273 |
private function check_domain_records(string $domain_a, string $domain_b): bool { |
| 274 |
return (gethostbyname($domain_a) === gethostbyname($domain_b)); |
| 275 |
} |
| 276 |
} |