| @@ -1,22 +1,23 @@ | ||
| 1 | 1 | <?php |
| 2 | +namespace Hostinger\Rest; | |
| 2 | 3 | |
| 3 | -namespace Hostinger\Rest; | |
| 4 | +use Hostinger\Admin\Options\PluginOptions; | |
| 5 | +use Hostinger\Admin\PluginSettings; | |
| 6 | +use Hostinger\DefaultOptions; | |
| 7 | +use Hostinger\Helper; | |
| 4 | 8 | |
| 9 | +/** | |
| 10 | + * Avoid possibility to get file accessed directly | |
| 11 | + */ | |
| 5 | 12 | if ( ! defined( 'ABSPATH' ) ) { |
| 6 | 13 | die; |
| 7 | 14 | } |
| 8 | 15 | |
| 9 | -use Hostinger\Admin\Options\PluginOptions; | |
| 10 | -use Hostinger\Admin\PluginSettings; | |
| 11 | -use Hostinger\Helper; | |
| 12 | - | |
| 13 | 16 | /** |
| 14 | 17 | * Class for handling Settings Rest API |
| 15 | 18 | */ |
| 16 | 19 | class SettingsRoutes { |
| 17 | - | |
| 18 | - | |
| 19 | 20 | /** |
| 20 | 21 | * @var PluginSettings plugin settings. |
| 21 | 22 | */ |
| 22 | 23 | private PluginSettings $plugin_settings; |
| @@ -36,40 +37,34 @@ | ||
| 36 | 37 | * @param WP_REST_Request $request WordPress rest request. |
| 37 | 38 | * |
| 38 | 39 | * @return \WP_REST_Response |
| 39 | 40 | */ |
| 40 | - | |
| 41 | - /** PHPCS:disable Generic.CodeAnalysis.UnusedFunctionParameter.Found */ | |
| 42 | 41 | public function get_settings( \WP_REST_Request $request ): \WP_REST_Response { |
| 43 | 42 | global $wp_version; |
| 44 | - $plugin_settings = $this->plugin_settings->get_plugin_settings(); | |
| 45 | 43 | |
| 46 | 44 | $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. | |
| 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. | |
| 51 | 49 | 'is_eligible_www_redirect' => $this->is_eligible_www_redirect(), |
| 52 | - 'optin_mcp' => $plugin_settings->get_optin_mcp(), | |
| 53 | 50 | ); |
| 54 | 51 | |
| 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 | - } | |
| 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(); | |
| 61 | 56 | } |
| 62 | 57 | |
| 63 | - $plugin_settings = $plugin_settings->to_array(); | |
| 58 | + $plugin_settings = $this->plugin_settings->get_plugin_settings()->to_array(); | |
| 64 | 59 | |
| 65 | 60 | $response = array( |
| 66 | - 'data' => array_merge( $data, $plugin_settings ), | |
| 61 | + 'data' => array_merge($data, $plugin_settings) | |
| 67 | 62 | ); |
| 68 | 63 | |
| 69 | 64 | $response = new \WP_REST_Response( $response ); |
| 70 | 65 | |
| 71 | - $response->set_headers( array( 'Cache-Control' => 'no-cache' ) ); | |
| 66 | + $response->set_headers(array('Cache-Control' => 'no-cache')); | |
| 72 | 67 | |
| 73 | 68 | $response->set_status( \WP_Http::OK ); |
| 74 | 69 | |
| 75 | 70 | return $response; |
| @@ -90,9 +85,9 @@ | ||
| 90 | 85 | $new_plugin_options = new PluginOptions( $new_settings ); |
| 91 | 86 | |
| 92 | 87 | $response = new \WP_REST_Response( array( 'data' => $this->plugin_settings->save_plugin_settings( $new_plugin_options )->to_array() ) ); |
| 93 | 88 | |
| 94 | - $response->set_headers( array( 'Cache-Control' => 'no-cache' ) ); | |
| 89 | + $response->set_headers(array('Cache-Control' => 'no-cache')); | |
| 95 | 90 | |
| 96 | 91 | $response->set_status( \WP_Http::OK ); |
| 97 | 92 | |
| 98 | 93 | return $response; |
| @@ -110,22 +105,19 @@ | ||
| 110 | 105 | |
| 111 | 106 | $parameters = $request->get_params(); |
| 112 | 107 | |
| 113 | 108 | foreach ( $settings->to_array() as $field_key => $field_value ) { |
| 114 | - if ( $field_key === 'bypass_code' ) { | |
| 109 | + // Don't allow to change bypass code | |
| 110 | + if($field_key == 'bypass_code') { | |
| 115 | 111 | $new_settings[ $field_key ] = $field_value; |
| 116 | 112 | continue; |
| 117 | 113 | } |
| 118 | 114 | |
| 119 | 115 | if ( isset( $parameters[ $field_key ] ) ) { |
| 120 | - $new_settings[ $field_key ] = ! empty( $parameters[ $field_key ] ); | |
| 116 | + $new_settings[ $field_key ] = !empty( $parameters[ $field_key ] ); | |
| 121 | 117 | } else { |
| 122 | 118 | $new_settings[ $field_key ] = $field_value; |
| 123 | 119 | } |
| 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 | 120 | } |
| 129 | 121 | |
| 130 | 122 | $new_plugin_options = new PluginOptions( $new_settings ); |
| 131 | 123 | |
| @@ -132,21 +124,15 @@ | ||
| 132 | 124 | $response = new \WP_REST_Response( array( 'data' => $this->plugin_settings->save_plugin_settings( $new_plugin_options )->to_array() ) ); |
| 133 | 125 | |
| 134 | 126 | $this->update_urls( $new_plugin_options ); |
| 135 | 127 | |
| 136 | - $response->set_headers( array( 'Cache-Control' => 'no-cache' ) ); | |
| 128 | + $response->set_headers(array('Cache-Control' => 'no-cache')); | |
| 137 | 129 | |
| 138 | 130 | $response->set_status( \WP_Http::OK ); |
| 139 | 131 | |
| 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 | 132 | return $response; |
| 147 | 133 | } |
| 148 | - /** PHPCS:enable */ | |
| 134 | + | |
| 149 | 135 | /** |
| 150 | 136 | * @param PluginOptions $plugin_options |
| 151 | 137 | * |
| 152 | 138 | * @return bool |
| @@ -152,27 +138,25 @@ | ||
| 152 | 138 | * @return bool |
| 153 | 139 | */ |
| 154 | 140 | private function update_urls( PluginOptions $plugin_options ): bool { |
| 155 | 141 | $siteurl = get_option( 'siteurl' ); |
| 156 | - $home = get_option( 'home' ); | |
| 142 | + $home = get_option( 'home' ); | |
| 157 | 143 | |
| 158 | - if ( empty( $siteurl ) || empty( $home ) ) { | |
| 144 | + if( empty( $siteurl ) || empty( $home ) ) { | |
| 159 | 145 | return false; |
| 160 | 146 | } |
| 161 | 147 | |
| 162 | - if ( $plugin_options->get_force_https() ) { | |
| 163 | - $siteurl = str_replace( 'http://', 'https://', $siteurl ); | |
| 164 | - $home = str_replace( 'http://', 'https://', $home ); | |
| 148 | + if( $plugin_options->get_force_https() ) { | |
| 149 | + $siteurl = str_replace( 'http://', 'https://', $siteurl); | |
| 150 | + $home = str_replace( 'http://', 'https://', $home); | |
| 165 | 151 | } |
| 166 | 152 | |
| 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 | - } | |
| 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); | |
| 175 | 159 | } |
| 176 | 160 | |
| 177 | 161 | update_option( 'siteurl', $siteurl ); |
| 178 | 162 | update_option( 'home', $home ); |
| @@ -185,20 +169,20 @@ | ||
| 185 | 169 | * |
| 186 | 170 | * @return mixed |
| 187 | 171 | */ |
| 188 | 172 | private function add_www_to_url( string $url ): string { |
| 189 | - $parsed_url = wp_parse_url( $url ); | |
| 173 | + $parsedUrl = parse_url( $url ); | |
| 190 | 174 | |
| 191 | - if ( isset( $parsed_url['host'] ) ) { | |
| 192 | - $host = $parsed_url['host']; | |
| 175 | + if ( isset( $parsedUrl['host'] ) ) { | |
| 176 | + $host = $parsedUrl['host']; | |
| 193 | 177 | |
| 194 | - if ( strpos( $host, 'www.' ) !== 0 ) { | |
| 178 | + if ( strpos($host, 'www.') !== 0 ) { | |
| 195 | 179 | $host = 'www.' . $host; |
| 196 | 180 | } |
| 197 | 181 | |
| 198 | - $parsed_url['host'] = $host; | |
| 182 | + $parsedUrl['host'] = $host; | |
| 199 | 183 | |
| 200 | - return $this->rebuild_url( $parsed_url ); | |
| 184 | + return $this->rebuild_url($parsedUrl); | |
| 201 | 185 | } |
| 202 | 186 | |
| 203 | 187 | return $url; |
| 204 | 188 | } |
| @@ -208,13 +192,13 @@ | ||
| 208 | 192 | * |
| 209 | 193 | * @return string |
| 210 | 194 | */ |
| 211 | 195 | 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'] : ''; | |
| 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'] : ''; | |
| 217 | 201 | |
| 218 | 202 | return "$scheme$host$path$query$fragment"; |
| 219 | 203 | } |
| 220 | 204 | |
| @@ -221,18 +205,18 @@ | ||
| 221 | 205 | /** |
| 222 | 206 | * @return string |
| 223 | 207 | */ |
| 224 | 208 | private function get_latest_wordpress_version(): string { |
| 225 | - $newest_wordpress_version = get_transient( 'hostinger_newest_wordpress_version' ); | |
| 209 | + $newest_wordpress_version = get_transient('hostinger_newest_wordpress_version'); | |
| 226 | 210 | |
| 227 | - if ( $newest_wordpress_version !== false ) { | |
| 211 | + if ($newest_wordpress_version !== false) { | |
| 228 | 212 | return $newest_wordpress_version; |
| 229 | 213 | } |
| 230 | 214 | |
| 231 | 215 | $newest_wordpress_version = $this->fetch_wordpress_version(); |
| 232 | 216 | |
| 233 | - if ( ! empty( $newest_wordpress_version ) ) { | |
| 234 | - set_transient( 'hostinger_newest_wordpress_version', $newest_wordpress_version, 86400 ); | |
| 217 | + if(!empty($newest_wordpress_version)) { | |
| 218 | + set_transient('hostinger_newest_wordpress_version', $newest_wordpress_version, 86400); | |
| 235 | 219 | |
| 236 | 220 | return $newest_wordpress_version; |
| 237 | 221 | } |
| 238 | 222 | |
| @@ -242,28 +226,19 @@ | ||
| 242 | 226 | /** |
| 243 | 227 | * @return string |
| 244 | 228 | */ |
| 245 | 229 | private function fetch_wordpress_version(): string { |
| 246 | - $url = 'https://api.wordpress.org/core/version-check/1.7/'; | |
| 247 | - $response = wp_remote_get( $url ); | |
| 230 | + $url = "https://api.wordpress.org/core/version-check/1.7/"; | |
| 248 | 231 | |
| 249 | - if ( is_wp_error( $response ) ) { | |
| 250 | - return ''; | |
| 251 | - } | |
| 232 | + $response = file_get_contents($url); | |
| 252 | 233 | |
| 253 | - $response_body = wp_remote_retrieve_body( $response ); | |
| 234 | + if ($response !== false) { | |
| 235 | + $data = json_decode($response); | |
| 254 | 236 | |
| 255 | - if ( $response_body === false ) { | |
| 256 | - return ''; | |
| 237 | + return !empty($data->offers[0]->current) ? $data->offers[0]->current : ''; | |
| 257 | 238 | } |
| 258 | 239 | |
| 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']; | |
| 240 | + return ''; | |
| 266 | 241 | } |
| 267 | 242 | |
| 268 | 243 | /** |
| 269 | 244 | * @return bool |
| @@ -268,21 +243,21 @@ | ||
| 268 | 243 | /** |
| 269 | 244 | * @return bool |
| 270 | 245 | */ |
| 271 | 246 | private function is_eligible_www_redirect(): bool { |
| 272 | - $is_eligible_www_redirect = get_transient( 'hostinger_is_eligible_www_redirect' ); | |
| 247 | + $is_eligible_www_redirect = get_transient('hostinger_is_eligible_www_redirect'); | |
| 273 | 248 | |
| 274 | - if ( $is_eligible_www_redirect !== false ) { | |
| 249 | + if ($is_eligible_www_redirect !== false) { | |
| 275 | 250 | return $is_eligible_www_redirect; |
| 276 | 251 | } |
| 277 | 252 | |
| 278 | - $domain = str_replace( 'www.', '', get_option( 'siteurl' ) ); | |
| 253 | + $domain = str_replace('www.', '', get_option( 'siteurl' )); | |
| 279 | 254 | $www_domain = $this->add_www_to_url( $domain ); |
| 280 | 255 | |
| 281 | - $is_eligible_www_redirect = $this->check_domain_records( $domain, $www_domain ); | |
| 256 | + $is_eligible_www_redirect = $this->check_domain_records($domain, $www_domain); | |
| 282 | 257 | |
| 283 | - if ( isset( $is_eligible_www_redirect ) ) { | |
| 284 | - set_transient( 'hostinger_is_eligible_www_redirect', $is_eligible_www_redirect, 120 ); | |
| 258 | + if(isset($is_eligible_www_redirect)) { | |
| 259 | + set_transient('hostinger_is_eligible_www_redirect', $is_eligible_www_redirect, 120); | |
| 285 | 260 | |
| 286 | 261 | return $is_eligible_www_redirect; |
| 287 | 262 | } |
| 288 | 263 | |
| @@ -289,27 +264,13 @@ | ||
| 289 | 264 | return ''; |
| 290 | 265 | } |
| 291 | 266 | |
| 292 | 267 | /** |
| 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 | 268 | * @param string $domain_a |
| 308 | 269 | * @param string $domain_b |
| 309 | 270 | * |
| 310 | 271 | * @return bool |
| 311 | 272 | */ |
| 312 | - public function check_domain_records( string $domain_a, string $domain_b ): bool { | |
| 313 | - return ( gethostbyname( $domain_a ) === gethostbyname( $domain_b ) ); | |
| 273 | + private function check_domain_records(string $domain_a, string $domain_b): bool { | |
| 274 | + return (gethostbyname($domain_a) === gethostbyname($domain_b)); | |
| 314 | 275 | } |
| 315 | -} | |
| 276 | +} | |