class-request-controller.php
233 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SuperbAddons\Library\Controllers; |
| 4 | |
| 5 | defined('ABSPATH') || exit(); |
| 6 | |
| 7 | use Exception; |
| 8 | use WP_Error; |
| 9 | use WP_REST_Server; |
| 10 | use SuperbAddons\Config\Capabilities; |
| 11 | use SuperbAddons\Data\Controllers\CacheController; |
| 12 | use SuperbAddons\Data\Controllers\DomainShiftController; |
| 13 | use SuperbAddons\Data\Controllers\KeyController; |
| 14 | use SuperbAddons\Data\Controllers\OptionController; |
| 15 | use SuperbAddons\Data\Controllers\RestController; |
| 16 | use SuperbAddons\Data\Utils\CacheException; |
| 17 | use SuperbAddons\Data\Utils\CacheTypes; |
| 18 | use SuperbAddons\Data\Utils\ElementorCache; |
| 19 | use SuperbAddons\Data\Utils\GutenbergCache; |
| 20 | use SuperbAddons\Elementor\Controllers\ElementorController; |
| 21 | use SuperbAddons\Gutenberg\Controllers\GutenbergController; |
| 22 | |
| 23 | |
| 24 | class LibraryRequestController |
| 25 | { |
| 26 | const ELEMENTOR_LIST_ROUTE = '/elementor-list'; |
| 27 | const ELEMENTOR_INSERT_ROUTE = '/elementor-insert'; |
| 28 | const GUTENBERG_LIST_ROUTE = '/gutenberg-list/'; |
| 29 | const GUTENBERG_INSERT_ROUTE = '/gutenberg-insert/'; |
| 30 | |
| 31 | const GUTENBERG_ROUTE_TYPE_PATTERNS = 'patterns'; |
| 32 | const GUTENBERG_ROUTE_TYPE_PAGES = 'pages'; |
| 33 | |
| 34 | const ELEMENTOR_ENDPOINT_BASE = 'elementor-library/'; |
| 35 | const GUTENBERG_ENDPOINT_BASE = 'gutenberg-library/'; |
| 36 | |
| 37 | public function __construct() |
| 38 | { |
| 39 | RestController::AddRoute(self::ELEMENTOR_LIST_ROUTE, array( |
| 40 | 'methods' => WP_REST_Server::READABLE, |
| 41 | 'permission_callback' => array($this, 'LibraryCallbackPermissionCheck'), |
| 42 | 'callback' => array($this, 'ElementorListCallback'), |
| 43 | )); |
| 44 | RestController::AddRoute(self::ELEMENTOR_INSERT_ROUTE, array( |
| 45 | 'methods' => WP_REST_Server::READABLE, |
| 46 | 'permission_callback' => array($this, 'LibraryCallbackPermissionCheck'), |
| 47 | 'callback' => array($this, 'ElementorInsertCallback'), |
| 48 | )); |
| 49 | RestController::AddRoute(self::GUTENBERG_LIST_ROUTE . '(?P<route_type>[\w]+)', array( |
| 50 | 'methods' => WP_REST_Server::READABLE, |
| 51 | 'permission_callback' => array($this, 'LibraryCallbackPermissionCheck'), |
| 52 | 'callback' => array($this, 'GutenbergListCallback'), |
| 53 | )); |
| 54 | RestController::AddRoute(self::GUTENBERG_INSERT_ROUTE . '(?P<route_type>[\w]+)', array( |
| 55 | 'methods' => WP_REST_Server::READABLE, |
| 56 | 'permission_callback' => array($this, 'LibraryCallbackPermissionCheck'), |
| 57 | 'callback' => array($this, 'GutenbergInsertCallback'), |
| 58 | )); |
| 59 | } |
| 60 | |
| 61 | public function LibraryCallbackPermissionCheck() |
| 62 | { |
| 63 | // Restrict endpoint to only users who have the proper capability. |
| 64 | if (!current_user_can(Capabilities::CONTRIBUTOR)) { |
| 65 | return new WP_Error('rest_forbidden', esc_html__('Unauthorized. Please check user permissions.', "superb-blocks"), array('status' => 401)); |
| 66 | } |
| 67 | |
| 68 | return true; |
| 69 | } |
| 70 | |
| 71 | public function ElementorListCallback() |
| 72 | { |
| 73 | try { |
| 74 | $section_cache = CacheController::GetCache(ElementorCache::SECTIONS, CacheTypes::ELEMENTOR); |
| 75 | if (!!$section_cache) { |
| 76 | // Local cache accepted |
| 77 | $section_cache->premium = KeyController::HasValidPremiumKey(); |
| 78 | return rest_ensure_response($section_cache); |
| 79 | } |
| 80 | |
| 81 | return $this->ListHandler(self::ELEMENTOR_ENDPOINT_BASE, 'sections', ElementorCache::SECTIONS); |
| 82 | } catch (CacheException $cex) { |
| 83 | return new \WP_Error('internal_error_cache', 'Internal Cache Error: ' . esc_html($cex->getMessage()), array('status' => 500)); |
| 84 | } catch (Exception $ex) { |
| 85 | LogController::HandleException($ex); |
| 86 | return new \WP_Error('internal_error_plugin', 'Internal Plugin Error', array('status' => 500)); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | public function GutenbergListCallback($request) |
| 91 | { |
| 92 | try { |
| 93 | if (!isset($request['route_type'])) { |
| 94 | return new \WP_Error('invalid_route_type', 'Invalid route type', array('status' => 400)); |
| 95 | } |
| 96 | $route_type = $request['route_type']; |
| 97 | $gutenberg_cache_option = self::GetGutenbergCacheOptionByRouteType($route_type); |
| 98 | if (!$gutenberg_cache_option) { |
| 99 | return new \WP_Error('invalid_route_type', 'Invalid route type', array('status' => 400)); |
| 100 | } |
| 101 | |
| 102 | $pattern_cache = CacheController::GetCache($gutenberg_cache_option, CacheTypes::GUTENBERG); |
| 103 | if (!!$pattern_cache) { |
| 104 | // Local cache accepted |
| 105 | $pattern_cache->premium = KeyController::HasValidPremiumKey(); |
| 106 | return rest_ensure_response($pattern_cache); |
| 107 | } |
| 108 | |
| 109 | return $this->ListHandler(self::GUTENBERG_ENDPOINT_BASE, $route_type, $gutenberg_cache_option); |
| 110 | } catch (CacheException $cex) { |
| 111 | return new \WP_Error('internal_error_cache', 'Internal Cache Error: ' . esc_html($cex->getMessage()), array('status' => 500)); |
| 112 | } catch (Exception $ex) { |
| 113 | LogController::HandleException($ex); |
| 114 | return new \WP_Error('internal_error_plugin', 'Internal Plugin Error', array('status' => 500)); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | private function GetGutenbergCacheOptionByRouteType($route_type) |
| 119 | { |
| 120 | // Sanitize route type |
| 121 | switch ($route_type) { |
| 122 | case self::GUTENBERG_ROUTE_TYPE_PATTERNS: |
| 123 | return GutenbergCache::PATTERNS; |
| 124 | case self::GUTENBERG_ROUTE_TYPE_PAGES: |
| 125 | return GutenbergCache::PAGES; |
| 126 | default: |
| 127 | return false; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | private function ListHandler($endpoint, $item_type, $cache_option) |
| 132 | { |
| 133 | // Fetch data cache from service |
| 134 | $options_controller = new OptionController(); |
| 135 | $license_key = $options_controller->GetKey(); |
| 136 | |
| 137 | $response = DomainShiftController::RemoteGet($endpoint . $item_type . '?action=list&key=' . $license_key); |
| 138 | /// |
| 139 | if (!is_array($response) || is_wp_error($response) || wp_remote_retrieve_response_code($response) !== 200) { |
| 140 | return new \WP_Error('service_unavailable', 'Plugin Service Unavailable', array('status' => 503)); |
| 141 | } |
| 142 | /// |
| 143 | $data = json_decode($response['body']); |
| 144 | if (isset($data->code) && isset($data->data) && isset($data->message)) { |
| 145 | $status = isset($data->data->status) ? $data->data->status : 500; |
| 146 | return new \WP_Error($data->code, $data->message, array('status' => $status)); |
| 147 | } |
| 148 | if (isset($data->level)) { |
| 149 | KeyController::UpdateKeyType($data->level, $data->active, $data->expired, $data->exceeded); |
| 150 | } |
| 151 | |
| 152 | // Cache data |
| 153 | CacheController::SetCache($cache_option, $data); |
| 154 | |
| 155 | $data->premium = KeyController::HasValidPremiumKey(); |
| 156 | // |
| 157 | return rest_ensure_response($data); |
| 158 | } |
| 159 | |
| 160 | public function ElementorInsertCallback($request) |
| 161 | { |
| 162 | return $this->InsertHandler($request, self::ELEMENTOR_ENDPOINT_BASE, 'sections'); |
| 163 | } |
| 164 | |
| 165 | public function GutenbergInsertCallback($request) |
| 166 | { |
| 167 | if (!isset($request['route_type'])) { |
| 168 | return new \WP_Error('invalid_route_type', 'Invalid route type', array('status' => 400)); |
| 169 | } |
| 170 | $route_type = $request['route_type']; |
| 171 | $gutenberg_cache_option = self::GetGutenbergCacheOptionByRouteType($route_type); |
| 172 | if (!$gutenberg_cache_option) { |
| 173 | return new \WP_Error('invalid_route_type', 'Invalid route type', array('status' => 400)); |
| 174 | } |
| 175 | |
| 176 | return $this->InsertHandler($request, self::GUTENBERG_ENDPOINT_BASE, $route_type); |
| 177 | } |
| 178 | |
| 179 | private function InsertHandler($request, $endpoint, $item_type) |
| 180 | { |
| 181 | try { |
| 182 | // |
| 183 | $options_controller = new OptionController(); |
| 184 | $license_key = $options_controller->GetKey(); |
| 185 | if (!isset($request['id']) || !isset($request['package']) || $request['package'] === "premium" && !$license_key) { |
| 186 | return new \WP_Error('forbidden', 'Forbidden', array('status' => 403)); |
| 187 | } |
| 188 | |
| 189 | $stamp = $options_controller->GetStamp(); |
| 190 | $collection = $request['package'] === 'premium' ? "premium" : "free"; |
| 191 | $response = DomainShiftController::RemoteGet($endpoint . $item_type . '?action=insert&id=' . $request['id'] . '&collection=' . $collection . '&dm=' . urlencode(\home_url()) . '&key=' . urlencode($license_key) . '&stamp=' . absint($stamp)); |
| 192 | /// |
| 193 | if (!is_array($response) || is_wp_error($response) || wp_remote_retrieve_response_code($response) !== 200) { |
| 194 | return new \WP_Error('service_unavailable', 'Plugin Service Unavailable', array('status' => 503)); |
| 195 | } |
| 196 | /// |
| 197 | $data = json_decode($response['body'], true); |
| 198 | if (isset($data['code']) && isset($data['data']) && isset($data['message'])) { |
| 199 | $status = isset($data['data']['status']) ? $data['data']['status'] : 500; |
| 200 | return new \WP_Error($data['code'], $data['message'], array('status' => $status)); |
| 201 | } |
| 202 | if (isset($data['level'])) { |
| 203 | KeyController::UpdateKeyType($data['level'], $data['active'], $data['expired'], $data['exceeded']); |
| 204 | } |
| 205 | if (!isset($data['verified']) || !$data['verified']) { |
| 206 | KeyController::VerificationFailed(); |
| 207 | } |
| 208 | |
| 209 | $data['premium'] = KeyController::HasValidPremiumKey(); |
| 210 | |
| 211 | if (isset($data['access_failed'])) { |
| 212 | return rest_ensure_response($data); |
| 213 | } |
| 214 | |
| 215 | switch ($endpoint) { |
| 216 | case self::ELEMENTOR_ENDPOINT_BASE: |
| 217 | $data = ElementorController::ElementorDataImportAction($data); |
| 218 | return rest_ensure_response($data['content']); |
| 219 | case self::GUTENBERG_ENDPOINT_BASE: |
| 220 | $data = GutenbergController::GutenbergDataImportAction($data); |
| 221 | return rest_ensure_response(["content" => $data['content'], "name" => esc_html(isset($data['title']) ? $data['title'] : '')]); |
| 222 | default: |
| 223 | throw new Exception(__("Invalid endpoint specifier. Unable to import data.", "superb-blocks")); |
| 224 | } |
| 225 | |
| 226 | return new \WP_Error('internal_error_plugin', 'Unexpected Internal Plugin Error', array('status' => 500)); |
| 227 | } catch (Exception $ex) { |
| 228 | LogController::HandleException($ex); |
| 229 | return new \WP_Error('internal_error_plugin', 'Internal Plugin Error', array('status' => 500)); |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 |