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 |