| 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 Main Handler |
| 11 |
* Uses unified React architecture with Template Library |
| 12 |
*/ |
| 13 |
class Template_Kits |
| 14 |
{ |
| 15 |
private static $_instance = null; |
| 16 |
|
| 17 |
public function __construct() |
| 18 |
{ |
| 19 |
add_action('admin_menu', [$this, 'add_templates_kit_menu'], 20); |
| 20 |
add_action('admin_enqueue_scripts', [$this, 'enqueue_assets']); |
| 21 |
add_filter('jltma_template_kits_data', [$this, 'get_template_kits'], 10, 3); |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Register Template Kits Menu |
| 26 |
*/ |
| 27 |
public function add_templates_kit_menu() |
| 28 |
{ |
| 29 |
add_submenu_page( |
| 30 |
'master-addons-settings', |
| 31 |
'Template Kits', |
| 32 |
'Template Kits', |
| 33 |
'manage_options', |
| 34 |
'jltma-template-kits', |
| 35 |
[$this, 'render_templates_kit_page'] |
| 36 |
); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Enqueue Template Kits JS & CSS on its admin page |
| 41 |
*/ |
| 42 |
public function enqueue_assets($hook) |
| 43 |
{ |
| 44 |
if (strpos($hook, 'jltma-template-kits') === false) { |
| 45 |
return; |
| 46 |
} |
| 47 |
|
| 48 |
// Enqueue registered assets from Assets_Manager |
| 49 |
wp_enqueue_style('jltma-page-importer'); |
| 50 |
wp_enqueue_style('jltma-template-kits-app'); |
| 51 |
wp_enqueue_script('jltma-template-kits-app'); |
| 52 |
|
| 53 |
wp_localize_script('jltma-template-kits-app', 'JLTMATemplatesKit', array( |
| 54 |
'ajaxurl' => admin_url('admin-ajax.php'), |
| 55 |
'nonce' => wp_create_nonce('jltma_template_kits_nonce_action'), |
| 56 |
'is_pro' => Helper::jltma_premium(), |
| 57 |
'pluginUrl' => JLTMA_URL, |
| 58 |
'strings' => array( |
| 59 |
'importSuccess' => __('Template Kit imported successfully!', 'master-addons'), |
| 60 |
'importError' => __('Failed to import template kit.', 'master-addons'), |
| 61 |
'previewTemplate' => __('Preview', 'master-addons'), |
| 62 |
'importTemplate' => __('Import Kit', 'master-addons'), |
| 63 |
'searchPlaceholder' => __('Search template kits...', 'master-addons'), |
| 64 |
), |
| 65 |
)); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Render Templates Kit Page |
| 70 |
*/ |
| 71 |
public function render_templates_kit_page() |
| 72 |
{ |
| 73 |
echo '<div class="wrap"><div id="jltma-template-kits-app"></div></div>'; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Get Template Kits Data |
| 78 |
*/ |
| 79 |
public function get_template_kits($cached_data = false, $force_refresh = false, $category = 'all') |
| 80 |
{ |
| 81 |
// If already have valid cached data and not forcing refresh, return it |
| 82 |
if ($cached_data !== false && !$force_refresh && is_array($cached_data) && isset($cached_data['kits'])) { |
| 83 |
return $cached_data; |
| 84 |
} |
| 85 |
|
| 86 |
// Use the Template Kit Cache Manager to get cached kits |
| 87 |
if (class_exists('MasterAddons\Inc\Classes\Template_Kit_Cache')) { |
| 88 |
$cache_manager = \MasterAddons\Inc\Classes\Template_Kit_Cache::get_instance(); |
| 89 |
|
| 90 |
// Get kits from cache or fetch from API if needed |
| 91 |
$kits = $cache_manager->get_cached_kits($force_refresh); |
| 92 |
|
| 93 |
// Return the cached data in the expected format |
| 94 |
if ($kits !== false && is_array($kits)) { |
| 95 |
return array( |
| 96 |
'kits' => $kits, |
| 97 |
'cached' => true, |
| 98 |
'timestamp' => time() |
| 99 |
); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
// Return empty kits array as fallback |
| 104 |
return array( |
| 105 |
'kits' => array(), |
| 106 |
'cached' => false, |
| 107 |
'timestamp' => time() |
| 108 |
); |
| 109 |
} |
| 110 |
|
| 111 |
public static function get_instance() |
| 112 |
{ |
| 113 |
if (is_null(self::$_instance)) { |
| 114 |
self::$_instance = new self(); |
| 115 |
} |
| 116 |
return self::$_instance; |
| 117 |
} |
| 118 |
} |
| 119 |
|