PluginProbe
Hostinger Tools / 3.0.79
Hostinger Tools v3.0.79
3.0.79 3.0.78 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 All 110 releases
hostinger / includes / Cdn / SoftwareIdResolver.php

SoftwareIdResolver.php in Hostinger Tools 3.0.79, at includes/Cdn/SoftwareIdResolver.php

141 lines 3.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Hostinger\Cdn;
4
5 use Hostinger\WpHelper\Config;
6 use Hostinger\WpHelper\Requests\Client;
7 use TypeError;
8
9 defined( 'ABSPATH' ) || exit;
10
11 class SoftwareIdResolver {
12 public const OPTION_NAME = 'hostinger_sfid';
13
14 private const LOOKUP_ENDPOINT = '/api/v1/installations';
15 private const LOOKUP_TIMEOUT = 5;
16 private const LOOKUP_FAILED_TRANSIENT = 'hostinger_sfid_lookup_failed';
17 private const LOOKUP_FAILED_TTL = 600;
18
19 private Client $client;
20
21 public function __construct( Client $client ) {
22 $this->client = $client;
23 }
24
25 public function resolve(): ?string {
26 if ( defined( 'HOSTINGER_SOFTWARE_ID_OVERRIDE' ) ) {
27 $override = $this->normalize( HOSTINGER_SOFTWARE_ID_OVERRIDE );
28
29 if ( $override !== null ) {
30 return $override;
31 }
32 }
33
34 $stored = $this->normalize( get_option( self::OPTION_NAME ) );
35
36 if ( $stored !== null ) {
37 return $stored;
38 }
39
40 $from_config = $this->from_config();
41
42 if ( $from_config !== null ) {
43 update_option( self::OPTION_NAME, $from_config, true );
44
45 return $from_config;
46 }
47
48 return $this->from_lookup();
49 }
50
51 public function site_domain(): ?string {
52 $host = wp_parse_url( (string) get_option( 'siteurl' ), PHP_URL_HOST );
53
54 if ( ! is_string( $host ) || $host === '' ) {
55 return null;
56 }
57
58 $host = preg_replace( '/^www\./', '', $host );
59
60 if ( ! filter_var( $host, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME ) ) {
61 return null;
62 }
63
64 return $host;
65 }
66
67 public function forget(): void {
68 delete_option( self::OPTION_NAME );
69 delete_transient( self::LOOKUP_FAILED_TRANSIENT );
70 }
71
72 private function from_config(): ?string {
73 try {
74 $value = ( new Config() )->getConfigValue( 'software_id', '' );
75 } catch ( TypeError $e ) {
76 return null;
77 }
78
79 return $this->normalize( $value );
80 }
81
82 private function from_lookup(): ?string {
83 if ( get_transient( self::LOOKUP_FAILED_TRANSIENT ) ) {
84 return null;
85 }
86
87 $domain = $this->site_domain();
88
89 if ( $domain === null ) {
90 return null;
91 }
92
93 $response = $this->client->get(
94 self::LOOKUP_ENDPOINT,
95 array( 'domain' => $domain ),
96 array( Config::DOMAIN_HEADER => $domain ),
97 self::LOOKUP_TIMEOUT
98 );
99
100 $software_id = $this->id_from_response( $response );
101
102 if ( $software_id === null ) {
103 set_transient( self::LOOKUP_FAILED_TRANSIENT, 1, self::LOOKUP_FAILED_TTL );
104
105 return null;
106 }
107
108 update_option( self::OPTION_NAME, $software_id, true );
109
110 return $software_id;
111 }
112
113 private function id_from_response( $response ): ?string {
114 if ( is_wp_error( $response ) ) {
115 return null;
116 }
117
118 if ( wp_remote_retrieve_response_code( $response ) !== 200 ) {
119 return null;
120 }
121
122 $data = json_decode( wp_remote_retrieve_body( $response ), true );
123
124 if ( ! is_array( $data ) || ! isset( $data['data'][0]['id'] ) ) {
125 return null;
126 }
127
128 return $this->normalize( $data['data'][0]['id'] );
129 }
130
131 private function normalize( $raw ): ?string {
132 if ( ! is_scalar( $raw ) || ! ctype_digit( (string) $raw ) ) {
133 return null;
134 }
135
136 $software_id = (string) absint( $raw );
137
138 return $software_id === '0' ? null : $software_id;
139 }
140 }
141