PluginProbe ʕ •ᴥ•ʔ
Superb Addons: Blocks, Patterns, Pre-built Pages, Sliders, Popups, Free Forms, Animations & More / 3.0.8
Superb Addons: Blocks, Patterns, Pre-built Pages, Sliders, Popups, Free Forms, Animations & More v3.0.8
4.0.6 4.0.5 4.0.4 4.0.3 4.0.2 4.0.1 4.0.0 trunk 1.0.0 2.0.0 2.0.1 2.0.2 2.0.3 3.0 3.0.1 3.0.2 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.2 3.1.3 3.2.0 3.2.1 3.2.2 3.2.4 3.2.5 3.2.7 3.2.8 3.2.9 3.3.0 3.3.1 3.3.2 3.4.0 3.4.1 3.4.2 3.4.5 3.4.6 3.5.0 3.5.1 3.5.2 3.5.3 3.5.4 3.5.6 3.5.7 3.5.8 3.5.9 3.6.0 3.6.1 3.6.2 3.7.0 3.7.1
superb-blocks / src / library / controllers / class-request-controller.php
superb-blocks / src / library / controllers Last commit date
class-library-controller.php 2 years ago class-request-controller.php 2 years ago
class-request-controller.php
207 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\DomainShiftController;
12 use SuperbAddons\Data\Controllers\KeyController;
13 use SuperbAddons\Data\Controllers\OptionController;
14 use SuperbAddons\Data\Controllers\RestController;
15 use SuperbAddons\Data\Utils\CacheException;
16 use SuperbAddons\Data\Utils\CacheTypes;
17 use SuperbAddons\Data\Utils\ElementorCache;
18 use SuperbAddons\Data\Utils\GutenbergCache;
19 use SuperbAddons\Elementor\Controllers\ElementorController;
20 use SuperbAddons\Gutenberg\Controllers\GutenbergController;
21
22 class LibraryRequestController
23 {
24 const ELEMENTOR_LIST_ROUTE = '/elementor-list';
25 const ELEMENTOR_INSERT_ROUTE = '/elementor-insert';
26 const GUTENBERG_LIST_ROUTE = '/gutenberg-list';
27 const GUTENBERG_INSERT_ROUTE = '/gutenberg-insert';
28
29 const ELEMENTOR_ENDPOINT_BASE = 'elementor-library/';
30 const GUTENBERG_ENDPOINT_BASE = 'gutenberg-library/';
31
32 public function __construct()
33 {
34 RestController::AddRoute(self::ELEMENTOR_LIST_ROUTE, array(
35 'methods' => 'GET',
36 'permission_callback' => array($this, 'LibraryCallbackPermissionCheck'),
37 'callback' => array($this, 'ElementorListCallback'),
38 ));
39 RestController::AddRoute(self::ELEMENTOR_INSERT_ROUTE, array(
40 'methods' => 'GET',
41 'permission_callback' => array($this, 'LibraryCallbackPermissionCheck'),
42 'callback' => array($this, 'ElementorInsertCallback'),
43 ));
44 RestController::AddRoute(self::GUTENBERG_LIST_ROUTE, array(
45 'methods' => 'GET',
46 'permission_callback' => array($this, 'LibraryCallbackPermissionCheck'),
47 'callback' => array($this, 'GutenbergListCallback'),
48 ));
49 RestController::AddRoute(self::GUTENBERG_INSERT_ROUTE, array(
50 'methods' => 'GET',
51 'permission_callback' => array($this, 'LibraryCallbackPermissionCheck'),
52 'callback' => array($this, 'GutenbergInsertCallback'),
53 ));
54 }
55
56 public function LibraryCallbackPermissionCheck()
57 {
58 // Restrict endpoint to only users who have the proper capability.
59 if (!current_user_can(Capabilities::CONTRIBUTOR)) {
60 return new WP_Error('rest_forbidden', esc_html__('Unauthorized. Please check user permissions.', 'superbaddons'), array('status' => 401));
61 }
62
63 return true;
64 }
65
66 public function ElementorListCallback()
67 {
68 try {
69 $section_cache = CacheController::GetCache(ElementorCache::SECTIONS, CacheTypes::ELEMENTOR);
70 if (!!$section_cache) {
71 // Local cache accepted
72 $section_cache->premium = KeyController::HasPremiumKey();
73 return rest_ensure_response($section_cache);
74 }
75
76 return $this->ListHandler(self::ELEMENTOR_ENDPOINT_BASE, 'sections');
77 } catch (CacheException $cex) {
78 return new \WP_Error('internal_error_cache', 'Internal Cache Error: ' . esc_html($cex->getMessage()), array('status' => 500));
79 } catch (Exception $ex) {
80 LogController::HandleException($ex);
81 return new \WP_Error('internal_error_plugin', 'Internal Plugin Error', array('status' => 500));
82 }
83 }
84
85 public function GutenbergListCallback()
86 {
87 try {
88 $pattern_cache = CacheController::GetCache(GutenbergCache::PATTERNS, CacheTypes::GUTENBERG);
89 if (!!$pattern_cache) {
90 // Local cache accepted
91 $pattern_cache->premium = KeyController::HasPremiumKey();
92 return rest_ensure_response($pattern_cache);
93 }
94
95 return $this->ListHandler(self::GUTENBERG_ENDPOINT_BASE, 'patterns');
96 } catch (CacheException $cex) {
97 return new \WP_Error('internal_error_cache', 'Internal Cache Error: ' . esc_html($cex->getMessage()), array('status' => 500));
98 } catch (Exception $ex) {
99 LogController::HandleException($ex);
100 return new \WP_Error('internal_error_plugin', 'Internal Plugin Error', array('status' => 500));
101 }
102 }
103
104 private function ListHandler($endpoint, $item_type)
105 {
106 // Fetch data cache from service
107 $options_controller = new OptionController();
108 $license_key = $options_controller->GetKey();
109
110 $response = DomainShiftController::RemoteGet($endpoint . $item_type . '?action=list&key=' . $license_key);
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 if (!isset($request['id']) || !isset($request['package']) || $request['package'] === "premium" && !$license_key) {
160 return new \WP_Error('forbidden', 'Forbidden', array('status' => 403));
161 }
162
163 $stamp = $options_controller->GetStamp();
164 $collection = $request['package'] === 'premium' ? "premium" : "free";
165 $response = DomainShiftController::RemoteGet($endpoint . $item_type . '?action=insert&id=' . $request['id'] . '&collection=' . $collection . '&dm=' . urlencode(\home_url()) . '&key=' . urlencode($license_key) . '&stamp=' . absint($stamp));
166 ///
167 if (!is_array($response) || is_wp_error($response) || wp_remote_retrieve_response_code($response) !== 200) {
168 return new \WP_Error('service_unavailable', 'Plugin Service Unavailable', array('status' => 503));
169 }
170 ///
171 $data = json_decode($response['body'], true);
172 if (isset($data['code']) && isset($data['data']) && isset($data['message'])) {
173 $status = isset($data['data']['status']) ? $data['data']['status'] : 500;
174 return new \WP_Error($data['code'], $data['message'], array('status' => $status));
175 }
176 if (isset($data['level'])) {
177 KeyController::UpdateKeyType($data['level'], $data['active'], $data['expired']);
178 }
179 if (!isset($data['verified']) || !$data['verified']) {
180 KeyController::VerificationFailed();
181 }
182
183 $data['premium'] = KeyController::HasPremiumKey();
184
185 if (isset($data['access_failed'])) {
186 return rest_ensure_response($data);
187 }
188
189 switch ($endpoint) {
190 case self::ELEMENTOR_ENDPOINT_BASE:
191 $data = ElementorController::ElementorDataImportAction($data);
192 return rest_ensure_response($data['content']);
193 case self::GUTENBERG_ENDPOINT_BASE:
194 $data = GutenbergController::GutenbergDataImportAction($data);
195 return rest_ensure_response(["content" => $data['content']]);
196 default:
197 throw new Exception(__("Invalid endpoint specifier. Unable to import data.", "superbaddons"));
198 }
199
200 return new \WP_Error('internal_error_plugin', 'Unexpected Internal Plugin Error', array('status' => 500));
201 } catch (Exception $ex) {
202 LogController::HandleException($ex);
203 return new \WP_Error('internal_error_plugin', 'Internal Plugin Error', array('status' => 500));
204 }
205 }
206 }
207