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