PluginProbe ʕ •ᴥ•ʔ
10Web Booster – Website speed optimization, Cache & Page Speed optimizer / trunk
10Web Booster – Website speed optimization, Cache & Page Speed optimizer vtrunk
2.33.0 2.30.5 2.30.7 2.30.9 2.31.10 2.31.8 2.32.11 2.32.21 2.32.3 2.32.4 2.32.7 2.6.31 2.6.40 2.6.42 2.6.7 2.7.37 2.7.44 2.7.47 2.8.18 2.8.19 2.8.32 2.8.34 2.8.35 2.9.23 2.9.24 2.9.25 2.9.27 v2.27.4 trunk 2.0.10 2.0.11 2.0.12 2.0.13 2.0.14 2.0.15 2.0.17 2.0.18 2.0.21 2.0.22 2.0.25 2.0.26 2.0.27 2.0.3 2.0.7 2.0.9 2.10.46 2.10.65 2.10.66 2.10.68 2.11.41 2.11.42 2.11.43 2.12.15 2.12.21 2.12.22 2.12.23 2.12.26 2.13.37 2.13.40 2.13.41 2.13.42 2.13.44 2.13.45 2.13.47 2.14.49 2.14.50 2.15.18 2.17.21 2.17.23 2.18.17 2.19.44 2.19.45 2.19.46 2.19.49 2.2.12 2.2.15 2.2.16 2.2.18 2.2.8 2.20.31 2.20.32 2.20.33 2.21.11 2.21.12 2.21.16 2.21.25 2.22.32 2.23.13 2.23.15 2.23.16 2.23.18 2.24.12 2.24.14 2.24.18 2.25.14 2.26.6 2.28.10 2.28.13 2.28.14 2.28.7 2.29.1 2.29.2 2.29.3 2.3.0 2.3.1 2.3.2 2.3.3 2.30.18
tenweb-speed-optimizer / vendor / 10web-utils / tenweb-image-optimizer / src / TenWebIO / Api.php
tenweb-speed-optimizer / vendor / 10web-utils / tenweb-image-optimizer / src / TenWebIO Last commit date
Exceptions 4 years ago Queue 2 years ago Views 2 years ago Api.php 2 years ago Attachments.php 2 years ago Backup.php 2 years ago CLI.php 3 years ago Compress.php 2 years ago CompressDataService.php 3 years ago CompressService.php 3 years ago Config.php 2 years ago Init.php 2 years ago LastCompress.php 3 years ago Logs.php 2 years ago PreInit.php 3 years ago Rest.php 2 years ago Settings.php 3 years ago Utils.php 1 year ago
Api.php
150 lines
1 <?php
2 namespace TenWebIO;
3
4 use TenWebIO\Exceptions\IOException;
5 use Tenweb_Authorization\Helper as AuthHelper;
6
7 class Api
8 {
9 const API_COMPRESS_ACTION = 'compress';
10 const API_COMPRESS_CUSTOM_ACTION = 'compress-custom';
11 const API_COMPRESS_SETTINGS_ACTION = 'compress_settings';
12 const API_COMPRESS_LOG_STORE_ACTION = 'compress_log_store';
13 const API_WEBP_CONVERT = 'webp_convert';
14 const API_DELETE_WEBP_CONVERTED = 'delete_webp_converted';
15 const API_GET_STAT = 'get_stat';
16 const API_REPORT = 'report';
17
18 private $auth;
19 private $api_action = null;
20 private $domain_id = 0;
21 private $workspace_id = 0;
22
23 private $response_status_code = 200;
24
25 private static $post_headers_data = array(
26 "Accept" => "application/x.10weboptimizer.v3+json"
27 );
28
29 /**
30 * @param $api_action
31 */
32 public function __construct($api_action)
33 {
34 $this->auth = AuthHelper::get_instance();
35 $this->api_action = $api_action;
36 $this->setDomainId();
37 $this->setWorkspaceId();
38 }
39
40
41 /**
42 * @param string $method
43 *
44 * @param array $request_data
45 * @param array $query
46 * @param array $headers
47 *
48 * @return array|false
49 */
50 public function apiRequest($method = "GET", $request_data = array(), $query = array(), $headers = array())
51 {
52 try {
53 if (Utils::ifWorkspaceOrDomainEmpty()) {
54 return false;
55 }
56 $data["body"] = $request_data;
57 $data["headers"] = $headers + self::$post_headers_data;
58 $data["headers"]["Authorization"] = get_site_option(TENWEBIO_MANAGER_PREFIX . '_access_token');
59 $data["method"] = $method;
60 $data["timeout"] = 50000;
61
62 $url = $this->getApiUrl($query);
63
64 Logs::setLog("api:" . $url . ":" . $method . ":request-body", $data);
65
66 //$this->auth->request($url, $data);
67 //$response = $this->auth->last_response;
68 $response = wp_remote_request($url, $data);
69
70 $this->response_status_code = wp_remote_retrieve_response_code($response);
71 if (is_wp_error($response) || $this->response_status_code !== 200 || empty($response['body'])) {
72 Logs::setLog("api:" . $url . ":" . $method . ":response-error-status", wp_remote_retrieve_response_code($response), 'error');
73 Logs::setLog("api:" . $url . ":" . $method . ":response-error", $response, 'error');
74
75 return false;
76 }
77 Logs::setLog("api:" . $url . ":" . $method . ":response-body", $response['body']);
78 $response = json_decode($response['body'], true);
79
80 return !empty($response['data']) ? $response['data'] : false;
81
82 } catch (\Exception $e) {
83 Logs::setLog("api:" . $this->api_action . ":" . $method . ":exception", $e->getMessage(), 'error');
84 }
85
86 return false;
87 }
88
89 /**
90 * @return int
91 */
92 public function getResponseStatusCode()
93 {
94 return $this->response_status_code;
95 }
96
97 /**
98 * @param array $query
99 *
100 * @return string
101 * @throws IOException
102 */
103 private function getApiUrl($query = array())
104 {
105 $api_action_url_map = $this->getActionUrlMap();
106 if (empty($api_action_url_map[$this->api_action])) {
107 throw new IOException('Wrong api action');
108 }
109 $api_url = $api_action_url_map[$this->api_action];
110 if (!empty($query)) {
111 $api_url = $api_url . '?' . http_build_query($query);
112 }
113
114 return $api_url;
115 }
116
117 /**
118 * @return string[]
119 */
120 private function getActionUrlMap()
121 {
122 return array(
123 self::API_COMPRESS_ACTION => TENWEBIO_API_URL . '/compress/workspaces/' . $this->workspace_id . '/domains/' . $this->domain_id . '/connected/optimize',
124 self::API_COMPRESS_CUSTOM_ACTION => TENWEBIO_API_URL . '/compress/workspaces/' . $this->workspace_id . '/domains/' . $this->domain_id . '/connected/optimize-custom',
125 self::API_COMPRESS_SETTINGS_ACTION => TENWEBIO_API_URL . '/compress/workspaces/' . $this->workspace_id . '/domains/' . $this->domain_id . '/optimize/data',
126 self::API_COMPRESS_LOG_STORE_ACTION => TENWEBIO_API_URL . '/compress/workspaces/' . $this->workspace_id . '/domains/' . $this->domain_id . '/logs/store',
127 self::API_WEBP_CONVERT => TENWEBIO_API_URL . '/compress/workspaces/' . $this->workspace_id . '/domains/' . $this->domain_id . '/convert-to-webp',
128 self::API_DELETE_WEBP_CONVERTED => TENWEBIO_API_URL . '/compress/workspaces/' . $this->workspace_id . '/domains/' . $this->domain_id . '/delete-converted-webp',
129 self::API_GET_STAT => TENWEBIO_API_URL . '/compress/workspaces/' . $this->workspace_id . '/domains/' . $this->domain_id . '/stat',
130 self::API_REPORT => TENWEBIO_API_URL . '/compress/workspaces/' . $this->workspace_id . '/domains/' . $this->domain_id . '/report',
131 );
132 }
133
134 /**
135 * @return void
136 */
137 private function setDomainId()
138 {
139 $this->domain_id = (int)get_option(TENWEBIO_MANAGER_PREFIX . '_domain_id', 0);
140 }
141
142 /**
143 * @return void
144 */
145 private function setWorkspaceId()
146 {
147 $this->workspace_id = (int)get_site_option(TENWEBIO_MANAGER_PREFIX . '_workspace_id', 0);
148 }
149 }
150