PluginProbe
Powerkit – Supercharge your WordPress Site / 2.3.9
Powerkit – Supercharge your WordPress Site v2.3.9
3.1.1 3.1.0 3.0.9 3.0.8 trunk 2.2.6 2.2.7 2.2.8 2.2.9 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 2.3.5 2.3.6 2.3.7 2.3.8 2.3.9 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 All 87 releases
powerkit / modules / typekit-fonts / helpers / helper-typekit-fonts-api.php

helper-typekit-fonts-api.php in Powerkit – Supercharge your WordPress Site 2.3.9, at modules/typekit-fonts/helpers/helper-typekit-fonts-api.php

108 lines 2.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Typekit Client
4 *
5 * @package Powerkit
6 * @subpackage Modules/Helper
7 */
8
9 /**
10 * Typekit Client Class
11 */
12 class Powerkit_Typekit_Api {
13
14 /**
15 * Timeout.
16 *
17 * @var int $timeout Timeout.
18 */
19 private $timeout = 30;
20
21 /**
22 * Api link.
23 *
24 * @var string $api link.
25 */
26 private $api = 'https://typekit.com/api/v1/json/kits/';
27
28 /**
29 * Create a new instance of the client
30 *
31 * @param number $timeout Connection timeout in seconds (default is 30 seconds).
32 */
33 public function __construct( $timeout = 30 ) {
34 $this->timeout = $timeout;
35 }
36
37 /**
38 * Make a request and read the response. If succesful,
39 * a tuple of HTTP status code and response data is
40 * returned. If an error occurs NULL is returned.
41 *
42 * @param number $path Path.
43 * @param string $token Token.
44 * @return (number, string)|null
45 */
46 private function make_request( $path, $token ) {
47
48 $remote_get = wp_remote_get(
49 $path, array(
50 'timeout' => $this->timeout,
51 'httpversion' => '1.1',
52 'headers' => array(
53 'Accept' => 'application/json',
54 'Host' => 'typekit.com',
55 'X-Typekit-Token' => $token,
56 ),
57 )
58 );
59
60 if ( ! is_wp_error( $remote_get ) || wp_remote_retrieve_response_code( $remote_get ) === 200 ) {
61 return array( '200', $remote_get['body'] );
62 } else {
63 return null;
64 }
65 }
66
67 /**
68 * Get one or more kits. If kit identifier is not given
69 * all kits are returned.
70 *
71 * @param string $id The kit identifier (optional).
72 * @param string $token Your Typekit API token (optional).
73 * @param bool $cached Cache result.
74 * @return string|null NULL if retrieving the kit(s) failed, otherwise it return the data.
75 */
76 public function get( $id = null, $token = null, $cached = true ) {
77
78 $transient = sprintf( 'pk_typekit_%s_s', $id, $token );
79
80 $data = $cached ? get_option( $transient ) : null;
81
82 if ( null === $data || false === $data ) {
83
84 if ( ! is_null( $id ) ) {
85 if ( ! is_null( $token ) ) {
86 $result = $this->make_request( $this->api . $id . '/', $token );
87 } else {
88 $result = $this->make_request( $this->api . $id . '/published', $token );
89 }
90 } else {
91 $result = $this->make_request( $this->api, $token );
92 }
93
94 if ( ! is_null( $result ) ) {
95 list($status, $data) = $result;
96
97 if ( '200' === $status ) {
98 $data = json_decode( $data, true );
99 }
100 }
101
102 update_option( $transient, $data, false );
103 }
104
105 return $data;
106 }
107 }
108