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 / kits / class-ajax-handlers.php

class-ajax-handlers.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/kits/class-ajax-handlers.php

450 lines 20.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\Kits;
4
5 use MasterAddons\Inc\Classes\Helper;
6
7 defined('ABSPATH') || exit;
8
9 /**
10 * Template Kits AJAX Handlers
11 */
12 class Ajax_Handlers
13 {
14 private static $_instance = null;
15
16 public function __construct()
17 {
18 // Register AJAX actions
19 add_action('wp_ajax_jltma_get_kit_templates_unified', [$this, 'get_kit_templates_unified']);
20 add_action('wp_ajax_jltma_import_single_template', [$this, 'import_single_template_kit']);
21 add_action('wp_ajax_jltma_refresh_template_kit_cache', [$this, 'refresh_template_kit_cache']);
22 add_action('wp_ajax_jltma_cache_kit_images', [$this, 'cache_kit_images']);
23 add_action('wp_ajax_jltma_get_kit_manifest', [$this, 'get_kit_manifest']);
24 add_action('wp_ajax_jltma_update_purchased_kits_urls', [$this, 'update_purchased_kits_urls']);
25 }
26
27 /**
28 * AJAX Handler for getting template kits data for the unified interface
29 */
30 public function get_kit_templates_unified()
31 {
32 if (!wp_verify_nonce( isset( $_POST['_wpnonce'] ) ? sanitize_text_field( wp_unslash( $_POST['_wpnonce'] ) ) : '', 'jltma_template_kits_nonce_action') || !current_user_can('manage_options')) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotValidated,WordPress.Security.NonceVerification.Missing -- collecting nonce for immediate verification
33 wp_send_json_error(['message' => 'Permission denied']);
34 return;
35 }
36
37 // Get parameters
38 $category = isset($_POST['category']) ? sanitize_text_field( wp_unslash( $_POST['category'] ) ) : 'all';
39 $search = isset($_POST['search']) ? sanitize_text_field( wp_unslash( $_POST['search'] ) ) : '';
40 $page = isset($_POST['page']) ? absint($_POST['page']) : 1;
41 $per_page = 12;
42
43 $force_refresh = isset($_POST['force_refresh']) && $_POST['force_refresh'] === 'true';
44
45 // Use Template Kit Cache
46 if (class_exists('MasterAddons\Inc\Classes\Template_Kit_Cache')) {
47 $cache_manager = \MasterAddons\Inc\Classes\Template_Kit_Cache::get_instance();
48
49 // Check if the get_purchased_kits method exists
50 $purchased_kits = [];
51 if (method_exists($cache_manager, 'get_purchased_kits')) {
52 static $purchased_kits_cache = null;
53 static $manifests_cache = [];
54 static $manifests_fully_loaded = false;
55
56 if ($purchased_kits_cache === null) {
57 $purchased_kits_cache = $cache_manager->get_purchased_kits();
58
59 if (!empty($purchased_kits_cache) && !$manifests_fully_loaded) {
60 foreach ($purchased_kits_cache as $pk) {
61 $pk_id = $pk['kit_id'] ?? '';
62 if ($pk_id && !isset($manifests_cache[$pk_id])) {
63 if (isset($pk['manifest']) && is_array($pk['manifest'])) {
64 $manifests_cache[$pk_id] = $pk['manifest'];
65 } else {
66 $manifest = $cache_manager->get_kit_manifest($pk_id);
67 if ($manifest) {
68 $manifests_cache[$pk_id] = $manifest;
69 }
70 }
71 }
72 }
73 $manifests_fully_loaded = true;
74 }
75 }
76 $purchased_kits = $purchased_kits_cache;
77 }
78
79 if (method_exists($cache_manager, 'get_category_kits')) {
80 $cached_kits = $cache_manager->get_category_kits($category, $force_refresh);
81 } else {
82 $cached_kits = $cache_manager->get_cached_kits($force_refresh, $category);
83 }
84
85 $templates = [];
86 $purchased_kit_ids = [];
87
88 if ($cached_kits !== null) {
89 // Process purchased kits first
90 if (!empty($purchased_kits)) {
91 foreach ($purchased_kits as $purchased_kit) {
92 $kit_id = $purchased_kit['kit_id'] ?? '';
93 if ($kit_id) {
94 $purchased_kit_ids[] = $kit_id;
95 }
96
97 if ($category !== 'all') {
98 $kit_categories = $purchased_kit['categories'] ?? ['purchased'];
99 if (!in_array($category, $kit_categories) && $category !== 'purchased') {
100 continue;
101 }
102 }
103
104 $manifest = $manifests_cache[$kit_id] ?? null;
105 $required_plugins = [];
106
107 if ($manifest && isset($manifest['required_plugins'])) {
108 $required_plugins = $manifest['required_plugins'];
109 } elseif (isset($purchased_kit['required_plugins'])) {
110 $required_plugins = $purchased_kit['required_plugins'];
111 } elseif (isset($purchased_kit['manifest']['required_plugins'])) {
112 $required_plugins = $purchased_kit['manifest']['required_plugins'];
113 }
114
115 $template = [
116 'kit_id' => $kit_id,
117 'kit_name' => $purchased_kit['kit_name'] ?? $purchased_kit['title'] ?? '',
118 'thumbnail' => $purchased_kit['thumbnail'] ?? '',
119 'preview_url' => $purchased_kit['preview_url'] ?? '',
120 'is_pro' => false,
121 'downloadable' => true,
122 'purchasable' => false,
123 'is_purchased' => true,
124 'purchase_url' => $purchased_kit['purchase_url'] ?? '',
125 'downloads' => $purchased_kit['downloads'] ?? 0,
126 'descriptions' => $purchased_kit['descriptions'] ?? $purchased_kit['description'] ?? '',
127 'categories' => $purchased_kit['categories'] ?? ['purchased'],
128 'keywords' => $purchased_kit['keywords'] ?? [],
129 'purchased_date' => $purchased_kit['purchased_date'] ?? '',
130 'required_plugins' => $required_plugins,
131 'manifest' => $manifest
132 ];
133
134 if (!empty($search)) {
135 $searchable = strtolower($template['kit_name'] . ' ' . $template['descriptions'] . ' ' . implode(' ', $template['keywords']));
136 if (stripos($searchable, strtolower($search)) === false) {
137 continue;
138 }
139 }
140
141 $templates[] = $template;
142 }
143 }
144
145 // Process cached kits
146 if ($cached_kits) {
147 $all_categories = [];
148 $kits_to_process = [];
149
150 if ($category === 'all' && is_array($cached_kits)) {
151 foreach ($cached_kits as $cat_key => $cat_kits) {
152 if (is_array($cat_kits)) {
153 $kits_to_process = array_merge($kits_to_process, $cat_kits);
154 }
155 }
156 } else {
157 $kits_to_process = is_array($cached_kits) ? $cached_kits : [];
158 }
159
160 foreach ($kits_to_process as $kit) {
161 $current_kit_id = $kit['kit_id'] ?? $kit['id'] ?? '';
162 if (in_array($current_kit_id, $purchased_kit_ids)) {
163 continue;
164 }
165
166 if (isset($kit['categories']) && is_string($kit['categories'])) {
167 $kit['categories'] = [$kit['categories']];
168 }
169
170 $template = [
171 'kit_id' => $kit['kit_id'] ?? $kit['id'] ?? '',
172 'kit_name' => $kit['kit_name'] ?? $kit['name'] ?? '',
173 'thumbnail' => $kit['thumbnail'] ?? '',
174 'preview_url' => $kit['preview_url'] ?? $kit['preview'] ?? '',
175 'is_pro' => $kit['is_pro'] ?? false,
176 'downloadable' => $kit['downloadable'] ?? true,
177 'purchasable' => $kit['purchasable'] ?? false,
178 'is_purchased' => false,
179 'purchase_url' => $kit['purchase_url'] ?? '',
180 'downloads' => $kit['downloads'] ?? 0,
181 'descriptions' => $kit['descriptions'] ?? $kit['description'] ?? '',
182 'categories' => $kit['categories'] ?? ['all'],
183 'keywords' => $kit['keywords'] ?? [],
184 ];
185
186 if (!empty($search)) {
187 $searchable = strtolower($template['kit_name'] . ' ' . $template['descriptions'] . ' ' . implode(' ', $template['keywords']));
188 if (stripos($searchable, strtolower($search)) === false) {
189 continue;
190 }
191 }
192
193 $templates[] = $template;
194
195 foreach ($template['categories'] as $cat) {
196 if (!isset($all_categories[$cat])) {
197 $all_categories[$cat] = 0;
198 }
199 $all_categories[$cat]++;
200 }
201 }
202 }
203 }
204
205 // Apply pagination
206 $total_templates = count($templates);
207 $offset = ($page - 1) * $per_page;
208 $paginated_templates = array_slice($templates, $offset, $per_page);
209
210 $response = [
211 'templates' => $paginated_templates,
212 'pagination' => [
213 'current_page' => $page,
214 'total_pages' => ceil($total_templates / $per_page),
215 'total_items' => $total_templates,
216 'has_more' => ($offset + $per_page) < $total_templates
217 ]
218 ];
219
220 wp_send_json_success($response);
221 } else {
222 wp_send_json_error(['message' => 'Template Kit cache not available']);
223 }
224 }
225
226 /**
227 * Import Single Template from Kit
228 */
229 public function import_single_template_kit()
230 {
231 if (!wp_verify_nonce( isset( $_POST['_wpnonce'] ) ? sanitize_text_field( wp_unslash( $_POST['_wpnonce'] ) ) : '', 'master_addons_nonce') || !current_user_can('import')) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotValidated,WordPress.Security.NonceVerification.Missing -- collecting nonce for immediate verification
232 wp_send_json_error(['message' => 'Permission denied']);
233 return;
234 }
235
236 $kit_id = isset($_POST['kit_id']) ? sanitize_text_field( wp_unslash( $_POST['kit_id'] ) ) : '';
237 $template_slug = isset($_POST['template_slug']) ? sanitize_text_field( wp_unslash( $_POST['template_slug'] ) ) : '';
238 $template_title = isset($_POST['template_title']) ? sanitize_text_field( wp_unslash( $_POST['template_title'] ) ) : '';
239
240 if (empty($kit_id) || empty($template_slug)) {
241 wp_send_json_error(['message' => 'Kit ID and template slug are required']);
242 return;
243 }
244
245 $cache_manager = null;
246 if (class_exists('MasterAddons\Inc\Classes\Template_Kit_Cache')) {
247 $cache_manager = \MasterAddons\Inc\Classes\Template_Kit_Cache::get_instance();
248 $cache_manager->cache_kit_all_images($kit_id);
249 }
250
251 $page_id = Importer::get_instance()->import_single_template_from_api($kit_id, $template_slug);
252
253 if ($page_id) {
254 if (!empty($template_title)) {
255 wp_update_post([
256 'ID' => $page_id,
257 'post_title' => $template_title
258 ]);
259 }
260
261 wp_send_json_success([
262 'message' => 'Template imported successfully',
263 'page_id' => $page_id,
264 'edit_url' => admin_url('post.php?post=' . $page_id . '&action=elementor')
265 ]);
266 } else {
267 wp_send_json_error(['message' => 'Failed to import template']);
268 }
269 }
270
271 /**
272 * AJAX handler for Template Kit cache refresh
273 */
274 public function refresh_template_kit_cache()
275 {
276 if (!wp_verify_nonce( isset( $_POST['_wpnonce'] ) ? sanitize_text_field( wp_unslash( $_POST['_wpnonce'] ) ) : '', 'jltma_template_kits_nonce_action') || !current_user_can('manage_options')) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotValidated,WordPress.Security.NonceVerification.Missing -- collecting nonce for immediate verification
277 wp_send_json_error(['message' => 'Permission denied']);
278 return;
279 }
280
281 if (class_exists('MasterAddons\Inc\Classes\Template_Kit_Cache')) {
282 $cache_manager = \MasterAddons\Inc\Classes\Template_Kit_Cache::get_instance();
283 $refreshed_data = $cache_manager->refresh_cache();
284 $stats = $cache_manager->get_cache_stats();
285
286 wp_send_json_success([
287 'message' => 'Template Kit cache refreshed successfully from API',
288 'total_kits' => $stats['total_kits'],
289 'total_templates' => $stats['total_templates'],
290 'refreshed_data' => $refreshed_data,
291 'cache_size' => $stats['cache_size'],
292 'last_update' => wp_date('Y-m-d H:i:s', $stats['last_update'] ?? time())
293 ]);
294 } else {
295 wp_send_json_error(['message' => 'Cache manager not available']);
296 }
297 }
298
299 /**
300 * AJAX handler for caching kit images
301 */
302 public function cache_kit_images()
303 {
304 if (!wp_verify_nonce( isset( $_POST['_wpnonce'] ) ? sanitize_text_field( wp_unslash( $_POST['_wpnonce'] ) ) : '', 'jltma_template_kits_nonce_action') || !current_user_can('manage_options')) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotValidated,WordPress.Security.NonceVerification.Missing -- collecting nonce for immediate verification
305 wp_send_json_error(['message' => 'Permission denied']);
306 return;
307 }
308
309 $kit_id = isset($_POST['kit_id']) ? sanitize_text_field( wp_unslash( $_POST['kit_id'] ) ) : '';
310 $kit_category = isset($_POST['kit_category']) ? sanitize_text_field( wp_unslash( $_POST['kit_category'] ) ) : 'all';
311
312 if (empty($kit_id)) {
313 wp_send_json_error(['message' => 'Kit ID is required']);
314 return;
315 }
316
317 if (!class_exists('MasterAddons\Inc\Classes\Template_Kit_Cache')) {
318 wp_send_json_error(['message' => 'Cache manager not available']);
319 return;
320 }
321
322 $cache_manager = \MasterAddons\Inc\Classes\Template_Kit_Cache::get_instance();
323
324 $config = null;
325 if (function_exists('MasterAddons\Inc\Admin\Templates\master_addons_templates')) {
326 $templates_instance = \MasterAddons\Inc\Admin\Templates\master_addons_templates();
327 if ($templates_instance && isset($templates_instance->config)) {
328 $config = $templates_instance->config->get('api');
329 }
330 }
331
332 if (!$config) {
333 wp_send_json_error(['message' => 'API configuration not available']);
334 return;
335 }
336
337 $api_url = $config['base'] . $config['path'] . '/template-kits/' . $kit_id . '/templates';
338
339 if (Helper::jltma_premium()) {
340 $api_url = add_query_arg('pro_enabled', 'true', $api_url);
341 }
342
343 $response = wp_remote_get($api_url, [
344 'timeout' => 60,
345 'sslverify' => false,
346 'headers' => [
347 'User-Agent' => 'Master Addons Template Kit Image Cache/' . \JLTMA_VER
348 ]
349 ]);
350
351 if (is_wp_error($response)) {
352 wp_send_json_error(['message' => 'Failed to fetch kit templates: ' . $response->get_error_message()]);
353 return;
354 }
355
356 $body = wp_remote_retrieve_body($response);
357 $data = json_decode($body, true);
358
359 if (!$data || !isset($data['templates'])) {
360 wp_send_json_error(['message' => 'Invalid response from API']);
361 return;
362 }
363
364 $cache_manager->save_kit_content($kit_id, $kit_category, $data['templates']);
365 $image_count = $cache_manager->cache_kit_all_images($kit_id, $data['templates']);
366
367 wp_send_json_success([
368 'message' => sprintf('Successfully cached %d images for kit %s', $image_count, $kit_id),
369 'image_count' => $image_count,
370 'kit_id' => $kit_id
371 ]);
372 }
373
374 /**
375 * AJAX handler for getting kit manifest with required plugins
376 */
377 public function get_kit_manifest()
378 {
379 if (!wp_verify_nonce( isset( $_POST['_wpnonce'] ) ? sanitize_text_field( wp_unslash( $_POST['_wpnonce'] ) ) : '', 'jltma_template_kits_nonce_action') || !current_user_can('manage_options')) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotValidated,WordPress.Security.NonceVerification.Missing -- collecting nonce for immediate verification
380 wp_send_json_error(['message' => 'Permission denied']);
381 return;
382 }
383
384 $kit_id = isset($_POST['kit_id']) ? sanitize_text_field( wp_unslash( $_POST['kit_id'] ) ) : '';
385
386 if (empty($kit_id)) {
387 wp_send_json_error(['message' => 'Kit ID is required']);
388 return;
389 }
390
391 if (!class_exists('MasterAddons\Inc\Classes\Template_Kit_Cache')) {
392 wp_send_json_error(['message' => 'Cache manager not available']);
393 return;
394 }
395
396 $cache_manager = \MasterAddons\Inc\Classes\Template_Kit_Cache::get_instance();
397 $manifest = $cache_manager->get_kit_manifest($kit_id);
398
399 if ($manifest) {
400 if (!isset($manifest['required_plugins']) && !isset($manifest['requirements'])) {
401 $manifest['required_plugins'] = [];
402 }
403
404 wp_send_json_success([
405 'manifest' => $manifest,
406 'kit_id' => $kit_id
407 ]);
408 } else {
409 wp_send_json_error(['message' => 'Manifest not found for kit']);
410 }
411 }
412
413 /**
414 * AJAX handler for updating existing purchased kits to remove remote URLs
415 */
416 public function update_purchased_kits_urls()
417 {
418 if (!wp_verify_nonce( isset( $_POST['_wpnonce'] ) ? sanitize_text_field( wp_unslash( $_POST['_wpnonce'] ) ) : '', 'jltma_template_kits_nonce_action') || !current_user_can('manage_options')) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotValidated,WordPress.Security.NonceVerification.Missing -- collecting nonce for immediate verification
419 wp_send_json_error(['message' => 'Permission denied']);
420 return;
421 }
422
423 if (!class_exists('MasterAddons\Inc\Classes\Template_Kit_Cache')) {
424 wp_send_json_error(['message' => 'Cache manager not available']);
425 return;
426 }
427
428 $cache_manager = \MasterAddons\Inc\Classes\Template_Kit_Cache::get_instance();
429
430 if (method_exists($cache_manager, 'update_existing_purchased_kits_urls')) {
431 $updated_count = $cache_manager->update_existing_purchased_kits_urls();
432
433 wp_send_json_success([
434 'message' => sprintf('Successfully updated %d kit(s) to use local URLs', $updated_count),
435 'updated_count' => $updated_count
436 ]);
437 } else {
438 wp_send_json_error(['message' => 'This feature is not available with the current cache manager']);
439 }
440 }
441
442 public static function get_instance()
443 {
444 if (is_null(self::$_instance)) {
445 self::$_instance = new self();
446 }
447 return self::$_instance;
448 }
449 }
450