ApiHandler.php
2 weeks ago
FormsApiHandler.php
4 months ago
HostingApiHandler.php
2 months ago
IntegrationsApiHandler.php
2 months ago
ReachApiHandler.php
2 weeks ago
HostingApiHandler.php
60 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Hostinger\Reach\Api\Handlers; |
| 4 | |
| 5 | use Hostinger\WpHelper\Requests\Client; |
| 6 | use Hostinger\Reach\Functions; |
| 7 | use WP_REST_Response; |
| 8 | |
| 9 | if ( ! defined( 'ABSPATH' ) ) { |
| 10 | die; |
| 11 | } |
| 12 | |
| 13 | class HostingApiHandler extends ApiHandler { |
| 14 | |
| 15 | private Client $client; |
| 16 | |
| 17 | public function __construct( Functions $functions, Client $client ) { |
| 18 | parent::__construct( $functions ); |
| 19 | $this->client = $client; |
| 20 | } |
| 21 | |
| 22 | public function get_domain_details_handler(): WP_REST_Response { |
| 23 | // For non Hostinger Users prevent request and just return an empty array. |
| 24 | if ( ! $this->get_functions()->is_hostinger_user() ) { |
| 25 | return $this->handle_response( |
| 26 | array( |
| 27 | 'response' => array( |
| 28 | 'code' => 200, |
| 29 | ), |
| 30 | 'body' => wp_json_encode( array() ), |
| 31 | ) |
| 32 | ); |
| 33 | } |
| 34 | |
| 35 | $siteurl = get_option( 'siteurl' ); |
| 36 | $domain = apply_filters( 'hostinger_reach_domain', parse_url( $siteurl, PHP_URL_HOST ) ); |
| 37 | $transient_key = 'hostinger_reach_domain_details_' . md5( $domain ); |
| 38 | $disable_cache = apply_filters( 'hostinger_reach_disable_rest_cache', false ); |
| 39 | |
| 40 | if ( ! $disable_cache ) { |
| 41 | $cached_response = get_transient( $transient_key ); |
| 42 | if ( $cached_response !== false ) { |
| 43 | return $this->handle_response( $cached_response ); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | $response = $this->client->get( '/v3/wordpress/domain-details', array( 'domain' => $domain ) ); |
| 48 | |
| 49 | if ( is_wp_error( $response ) ) { |
| 50 | return $this->handle_wp_error( $response ); |
| 51 | } |
| 52 | |
| 53 | if ( ! $disable_cache ) { |
| 54 | set_transient( $transient_key, $response, MINUTE_IN_SECONDS ); |
| 55 | } |
| 56 | |
| 57 | return $this->handle_response( $response ); |
| 58 | } |
| 59 | } |
| 60 |