This repository has been archived by the owner on Dec 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wp-hstspreload-api.php
105 lines (82 loc) · 2.03 KB
/
wp-hstspreload-api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
/**
* WP-HSTS-Preload-API
*
* @link hstspreload.org API Docs
* @package WP-API-Libraries\WP-HSTS-Preload-API
*/
/*
* Plugin Name: WP HSTS Preload API
* Plugin URI: https://github.com/wp-api-libraries/wp-hstspreload-api
* Description: Perform API requests to HSTS Preload in WordPress.
* Author: WP API Libraries
* Version: 1.0.0
* Author URI: https://wp-api-libraries.com
* GitHub Plugin URI: https://github.com/wp-api-libraries/wp-hstspreload-api
* GitHub Branch: master
*/
/* Exit if accessed directly. */
defined( 'ABSPATH' ) || exit;
/* Check if class exists. */
if ( ! class_exists( 'HSTS_PreloadAPI' ) ) {
/**
* HSTS_PreloadAPI Class.
*
* @link hstspreload.org API Docs
*/
class HSTS_PreloadAPI {
/**
* BaseAPI Endpoint
*
* @var string
* @access protected
*/
protected $base_uri = 'https://hstspreload.org/api/v2/';
/**
* __construct function.
*
* @access public
* @return void
*/
public function __construct() {
}
/**
* Fetch the request from the API.
*
* @access private
* @param mixed $request Request URL.
* @return $body Body.
*/
private function fetch( $request ) {
$response = wp_remote_get( $request, array( 'timeout' => 20 ) );
$code = wp_remote_retrieve_response_code( $response );
if ( 200 !== $code ) {
return new WP_Error( 'response-error', sprintf( __( 'Server response code: %d', 'wp-hstspreload-api' ), $code ) );
}
$body = wp_remote_retrieve_body( $response );
return json_decode( $body );
}
/**
* Get Domain Status
*
* @access public
* @param mixed $domain Domain.
* @return void
*/
function get_domain_status( $domain ) {
$request = $this->base_uri . 'status?domain=' . $domain;
return $this->fetch( $request );
}
/**
* IS Domain Preloadable?
*
* @access public
* @param mixed $domain Domain.
* @return void
*/
function get_domain_preloadable( $domain ) {
$request = $this->base_uri . 'preloadable?domain=' . $domain;
return $this->fetch( $request );
}
}
}