PluginProbe
Master Addons for Elementor – Elementor Addons, Widgets, Mega Menu Builder, Popup Builder, Widget Builder & Template Kits / 3.1.2
Master Addons for Elementor – Elementor Addons, Widgets, Mega Menu Builder, Popup Builder, Widget Builder & Template Kits v3.1.2
3.2.2 3.2.3 3.2.1 3.2.0 3.1.9 3.1.8 3.1.7 3.1.6 3.1.5 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.9 trunk 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.1.3 1.1.4 1.1.5 All 174 releases
master-addons / inc / admin / templates / includes / sources / api.php

api.php in Master Addons for Elementor – Elementor Addons, Widgets, Mega Menu Builder, Popup Builder, Widget Builder & Template Kits 3.1.2, at inc/admin/templates/includes/sources/api.php

425 lines 9.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace MasterAddons\Inc\Admin\Templates\Includes\Sources;
4
5 use MasterAddons\Inc\Admin\Templates;
6
7 if (!defined('ABSPATH')) {
8 exit; // Exit if accessed directly.
9 }
10
11 class Api extends Base
12 {
13
14 private $_object_cache = array();
15
16 public function get_slug()
17 {
18 return 'master-api';
19 }
20
21 public function get_version()
22 {
23
24 $key = $this->get_slug() . '_version';
25 $version = get_transient($key);
26 $version = false;
27
28 if (!$version) {
29 $version = Templates\master_addons_templates()->api->get_info('api_version');
30 set_transient($key, $version, DAY_IN_SECONDS);
31 }
32
33 return $version;
34 }
35
36 public function get_items($tab = null)
37 {
38
39 if (!$tab) {
40
41 return array();
42 }
43
44 // Try enhanced file-based cache first
45 if (class_exists('MasterAddons\Inc\Classes\Template_Library_Cache')) {
46 $cache_manager = \MasterAddons\Inc\Classes\Template_Library_Cache::get_instance();
47 $cached_templates = $cache_manager->get_cached_templates($tab);
48
49 if ($cached_templates !== false) {
50 return array_values($cached_templates);
51 }
52 }
53
54 // Fallback to existing transient cache
55 $cached = $this->get_templates_cache();
56
57 if (!empty($cached[$tab])) {
58
59 return array_values($cached[$tab]);
60 }
61
62 $templates = $this->remote_get_templates($tab);
63
64 if (!$templates) {
65 return array();
66 }
67
68 if (empty($cached)) {
69 $cached = array();
70 }
71
72 $cached[$tab] = $templates;
73
74 $this->set_templates_cache($cached);
75
76 return $templates;
77 }
78
79 public function prepare_items_tab($tab = '')
80 {
81
82 if (!empty($this->_object_cache[$tab])) {
83 return $this->_object_cache[$tab];
84 }
85
86 $result = array(
87 'templates' => array(),
88 'categories' => array(),
89 'keywords' => array(),
90 );
91
92 $templates_cache = $this->get_templates_cache();
93 $categories_cache = $this->get_categories_cache();
94 $keywords_cache = $this->get_keywords_cache();
95
96 if (empty($templates_cache)) {
97 $templates_cache = array();
98 }
99
100 if (empty($categories_cache)) {
101 $categories_cache = array();
102 }
103
104 if (empty($keywords_cache)) {
105 $keywords_cache = array();
106 }
107
108 $result['templates'] = $this->remote_get_templates($tab);
109 $result['templates'] = $this->remote_get_categories($tab);
110 $result['templates'] = $this->remote_get_keywords($tab);
111
112 $templates_cache[$tab] = $result['templates'];
113 $categories_cache[$tab] = $result['categories'];
114 $keywords_cache[$tab] = $result['keywords'];
115
116 $this->set_templates_cache($templates_cache);
117 $this->set_categories_cache($categories_cache);
118 $this->set_keywords_cache($keywords_cache);
119
120 $this->_object_cache[$tab] = $result;
121
122 return $result;
123 }
124
125 public function remote_get_templates($tab)
126 {
127
128 $api_url = Templates\master_addons_templates()->api->api_url('templates');
129
130 if (!$api_url) {
131 return false;
132 }
133
134 $response = wp_remote_get($api_url . $tab, array(
135 'timeout' => 60,
136 'sslverify' => false
137 ));
138
139 $body = wp_remote_retrieve_body($response);
140
141 if (!$body) {
142 return false;
143 }
144
145 $body = json_decode($body, true);
146
147 if (!isset($body['success']) || true !== $body['success']) {
148 return false;
149 }
150
151 if (empty($body['templates'])) {
152 return false;
153 }
154
155 // Update thumbnail URLs to use cache folder first, then remote fallback
156 foreach ($body['templates'] as &$template) {
157 if (class_exists('Template_Kit_Cache')) {
158 $cache_manager = Template_Kit_Cache::get_instance();
159 $cached_thumbnail = $cache_manager->get_kit_thumbnail_url('', $template['title'], $template['thumbnail']);
160 if ($cached_thumbnail) {
161 $template['thumbnail'] = $cached_thumbnail;
162 }
163 }
164 }
165
166 return $body['templates'];
167 }
168
169 public function remote_get_categories($tab)
170 {
171
172 $api_url = Templates\master_addons_templates()->api->api_url('categories');
173
174 if (!$api_url) {
175 return false;
176 }
177
178 $response = wp_remote_get($api_url . $tab, array(
179 'timeout' => 60,
180 'sslverify' => false
181 ));
182
183 $body = wp_remote_retrieve_body($response);
184
185 if (!$body) {
186 return false;
187 }
188
189 $body = json_decode($body, true);
190
191 if (!isset($body['success']) || true !== $body['success']) {
192 return false;
193 }
194
195 if (empty($body['terms'])) {
196 return false;
197 }
198
199 return $body['terms'];
200 }
201
202 public function remote_get_keywords($tab)
203 {
204
205 $api_url = Templates\master_addons_templates()->api->api_url('keywords');
206
207 if (!$api_url) {
208 return false;
209 }
210
211 $response = wp_remote_get($api_url . $tab, array(
212 'timeout' => 60,
213 'sslverify' => false
214 ));
215
216 $body = wp_remote_retrieve_body($response);
217
218 if (!$body) {
219 return false;
220 }
221
222 $body = json_decode($body, true);
223
224 if (!isset($body['success']) || true !== $body['success']) {
225 return false;
226 }
227
228 if (empty($body['terms'])) {
229 return false;
230 }
231
232 return $body['terms'];
233 }
234
235 public function get_categories($tab = null)
236 {
237
238 if (!$tab) {
239 return array();
240 }
241
242 // Try enhanced file-based cache first
243 if (class_exists('MasterAddons\Inc\Classes\Template_Library_Cache')) {
244 $cache_manager = \MasterAddons\Inc\Classes\Template_Library_Cache::get_instance();
245 $cached_categories = $cache_manager->get_cached_categories($tab);
246
247 if ($cached_categories !== false) {
248 return $this->prepare_categories($cached_categories);
249 }
250 }
251
252 // Fallback to existing transient cache
253 $cached = $this->get_categories_cache();
254
255 if (!empty($cached[$tab])) {
256 return $this->prepare_categories($cached[$tab]);
257 }
258
259 $categories = $this->remote_get_categories($tab);
260
261 if (!$categories) {
262 return array();
263 }
264
265 if (empty($cached)) {
266 $cached = array();
267 }
268
269 $cached[$tab] = $categories;
270
271 $this->set_categories_cache($cached);
272
273 return $this->prepare_categories($categories);
274 }
275
276 public function prepare_categories($categories)
277 {
278
279 $result = array();
280
281 foreach ($categories as $slug => $title) {
282 $result[] = array(
283 'slug' => $slug,
284 'title' => $title,
285 );
286 }
287
288 return $result;
289 }
290
291 public function get_keywords($tab = null)
292 {
293
294 if (!$tab) {
295 return array();
296 }
297
298 // Try enhanced file-based cache first
299 if (class_exists('MasterAddons\Inc\Classes\Template_Library_Cache')) {
300 $cache_manager = \MasterAddons\Inc\Classes\Template_Library_Cache::get_instance();
301 $cached_keywords = $cache_manager->get_cached_keywords($tab);
302
303 if ($cached_keywords !== false) {
304 return $cached_keywords;
305 }
306 }
307
308 // Fallback to existing transient cache
309 $cached = $this->get_keywords_cache();
310
311 if (!empty($cached[$tab])) {
312 return $cached[$tab];
313 }
314
315 $keywords = $this->remote_get_keywords($tab);
316
317 if (!$keywords) {
318 return array();
319 }
320
321 if (empty($cached)) {
322 $cached = array();
323 }
324
325 $cached[$tab] = $keywords;
326
327 $this->set_keywords_cache($cached);
328
329 return $keywords;
330 }
331
332 public function get_item($template_id, $tab = false)
333 {
334
335 $id = str_replace($this->id_prefix(), '', $template_id);
336
337 if (!$tab) {
338 $tab = isset($_REQUEST['tab']) ? sanitize_key($_REQUEST['tab']) : false; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only tab parameter for template display
339 }
340
341 // Try enhanced file-based cache first
342 if (class_exists('MasterAddons\Inc\Classes\Template_Library_Cache')) {
343 $cache_manager = \MasterAddons\Inc\Classes\Template_Library_Cache::get_instance();
344 $cached_template = $cache_manager->get_cached_template($id, $tab);
345
346 if ($cached_template !== false) {
347 return $cached_template;
348 }
349 }
350
351 $license_key = Templates\master_addons_templates()->config->get('key');
352
353 $api_url = Templates\master_addons_templates()->api->api_url('template');
354
355
356 if (!$api_url) {
357 wp_send_json_success(array(
358 'licenseError' => true,
359 ));
360 }
361
362 $request = add_query_arg(
363 array(
364 'license' => $license_key,
365 'url' => urlencode(home_url('/')),
366 ),
367 $api_url . $id
368 );
369
370 $response = wp_remote_get($request, array(
371 'timeout' => 60,
372 'sslverify' => false
373 ));
374
375 $body = wp_remote_retrieve_body($response);
376
377 if (empty($body)) {
378 wp_send_json_error(array(
379 'message' => 'Empty API response',
380 ));
381 }
382
383 $body = json_decode($body, true);
384
385 if (!$body || !isset($body['success'])) {
386 wp_send_json_error(array(
387 'message' => 'Invalid API response format',
388 'response' => $body,
389 'request_url' => $request
390 ));
391 }
392
393 $content = isset($body['content']) ? $body['content'] : '';
394 // $content = isset($body['content']) ? sanitize_text_field($body['content']) : ''; // @not_sure
395
396 $type = isset($body['type']) ? sanitize_text_field($body['type']) : '';
397 $license = isset($body['license']) ? sanitize_text_field($body['license']) : '';
398
399 if (!empty($content)) {
400 $content = $this->replace_elements_ids($content);
401 $content = $this->process_export_import_content($content, 'on_import');
402 }
403
404 $result = array(
405 'page_settings' => array(),
406 'type' => $type,
407 'license' => $license,
408 'content' => $content
409 );
410
411 // Cache the successful result
412 if (class_exists('MasterAddons\Inc\Classes\Template_Library_Cache')) {
413 $cache_manager = \MasterAddons\Inc\Classes\Template_Library_Cache::get_instance();
414 $cache_manager->cache_template_data($id, $tab, $result);
415 }
416
417 return $result;
418 }
419
420 public function transient_lifetime()
421 {
422 return DAY_IN_SECONDS;
423 }
424 }
425