SettingsRoutes.php
316 lines
| 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 | do_action( 'hostinger_tools_settings_saved', $new_settings ); |
| 145 | |
| 146 | return $response; |
| 147 | } |
| 148 | /** PHPCS:enable */ |
| 149 | /** |
| 150 | * @param PluginOptions $plugin_options |
| 151 | * |
| 152 | * @return bool |
| 153 | */ |
| 154 | private function update_urls( PluginOptions $plugin_options ): bool { |
| 155 | $siteurl = get_option( 'siteurl' ); |
| 156 | $home = get_option( 'home' ); |
| 157 | |
| 158 | if ( empty( $siteurl ) || empty( $home ) ) { |
| 159 | return false; |
| 160 | } |
| 161 | |
| 162 | if ( $plugin_options->get_force_https() ) { |
| 163 | $siteurl = str_replace( 'http://', 'https://', $siteurl ); |
| 164 | $home = str_replace( 'http://', 'https://', $home ); |
| 165 | } |
| 166 | |
| 167 | if ( $this->is_eligible_www_redirect() ) { |
| 168 | if ( $plugin_options->get_force_www() ) { |
| 169 | $siteurl = $this->add_www_to_url( $siteurl ); |
| 170 | $home = $this->add_www_to_url( $home ); |
| 171 | } else { |
| 172 | $siteurl = str_replace( 'www.', '', $siteurl ); |
| 173 | $home = str_replace( 'www.', '', $home ); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | update_option( 'siteurl', $siteurl ); |
| 178 | update_option( 'home', $home ); |
| 179 | |
| 180 | return true; |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * @param string $url |
| 185 | * |
| 186 | * @return mixed |
| 187 | */ |
| 188 | private function add_www_to_url( string $url ): string { |
| 189 | $parsed_url = wp_parse_url( $url ); |
| 190 | |
| 191 | if ( isset( $parsed_url['host'] ) ) { |
| 192 | $host = $parsed_url['host']; |
| 193 | |
| 194 | if ( strpos( $host, 'www.' ) !== 0 ) { |
| 195 | $host = 'www.' . $host; |
| 196 | } |
| 197 | |
| 198 | $parsed_url['host'] = $host; |
| 199 | |
| 200 | return $this->rebuild_url( $parsed_url ); |
| 201 | } |
| 202 | |
| 203 | return $url; |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * @param array $params |
| 208 | * |
| 209 | * @return string |
| 210 | */ |
| 211 | private function rebuild_url( array $params ): string { |
| 212 | $scheme = isset( $params['scheme'] ) ? $params['scheme'] . '://' : ''; |
| 213 | $host = isset( $params['host'] ) ? $params['host'] : ''; |
| 214 | $path = isset( $params['path'] ) ? $params['path'] : ''; |
| 215 | $query = isset( $params['query'] ) ? '?' . $params['query'] : ''; |
| 216 | $fragment = isset( $params['fragment'] ) ? '#' . $params['fragment'] : ''; |
| 217 | |
| 218 | return "$scheme$host$path$query$fragment"; |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * @return string |
| 223 | */ |
| 224 | private function get_latest_wordpress_version(): string { |
| 225 | $newest_wordpress_version = get_transient( 'hostinger_newest_wordpress_version' ); |
| 226 | |
| 227 | if ( $newest_wordpress_version !== false ) { |
| 228 | return $newest_wordpress_version; |
| 229 | } |
| 230 | |
| 231 | $newest_wordpress_version = $this->fetch_wordpress_version(); |
| 232 | |
| 233 | if ( ! empty( $newest_wordpress_version ) ) { |
| 234 | set_transient( 'hostinger_newest_wordpress_version', $newest_wordpress_version, 86400 ); |
| 235 | |
| 236 | return $newest_wordpress_version; |
| 237 | } |
| 238 | |
| 239 | return ''; |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * @return string |
| 244 | */ |
| 245 | private function fetch_wordpress_version(): string { |
| 246 | $url = 'https://api.wordpress.org/core/version-check/1.7/'; |
| 247 | $response = wp_remote_get( $url ); |
| 248 | |
| 249 | if ( is_wp_error( $response ) ) { |
| 250 | return ''; |
| 251 | } |
| 252 | |
| 253 | $response_body = wp_remote_retrieve_body( $response ); |
| 254 | |
| 255 | if ( $response_body === false ) { |
| 256 | return ''; |
| 257 | } |
| 258 | |
| 259 | $data = json_decode( $response_body, true ); |
| 260 | |
| 261 | if ( json_last_error() !== JSON_ERROR_NONE || empty( $data['offers'][0]['current'] ) ) { |
| 262 | return ''; |
| 263 | } |
| 264 | |
| 265 | return $data['offers'][0]['current']; |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * @return bool |
| 270 | */ |
| 271 | private function is_eligible_www_redirect(): bool { |
| 272 | $is_eligible_www_redirect = get_transient( 'hostinger_is_eligible_www_redirect' ); |
| 273 | |
| 274 | if ( $is_eligible_www_redirect !== false ) { |
| 275 | return $is_eligible_www_redirect; |
| 276 | } |
| 277 | |
| 278 | $domain = str_replace( 'www.', '', get_option( 'siteurl' ) ); |
| 279 | $www_domain = $this->add_www_to_url( $domain ); |
| 280 | |
| 281 | $is_eligible_www_redirect = $this->check_domain_records( $domain, $www_domain ); |
| 282 | |
| 283 | if ( isset( $is_eligible_www_redirect ) ) { |
| 284 | set_transient( 'hostinger_is_eligible_www_redirect', $is_eligible_www_redirect, 120 ); |
| 285 | |
| 286 | return $is_eligible_www_redirect; |
| 287 | } |
| 288 | |
| 289 | return ''; |
| 290 | } |
| 291 | |
| 292 | /** |
| 293 | * Check if the field has changed. |
| 294 | */ |
| 295 | private function has_changed( string $field, mixed $new_value ): bool { |
| 296 | $settings = $this->plugin_settings->get_plugin_settings(); |
| 297 | $settings = $settings->to_array(); |
| 298 | |
| 299 | if ( ! array_key_exists( $field, $settings ) ) { |
| 300 | return false; |
| 301 | } |
| 302 | |
| 303 | return $new_value !== $settings[ $field ]; |
| 304 | } |
| 305 | |
| 306 | /** |
| 307 | * @param string $domain_a |
| 308 | * @param string $domain_b |
| 309 | * |
| 310 | * @return bool |
| 311 | */ |
| 312 | public function check_domain_records( string $domain_a, string $domain_b ): bool { |
| 313 | return ( gethostbyname( $domain_a ) === gethostbyname( $domain_b ) ); |
| 314 | } |
| 315 | } |
| 316 |