PluginProbe
Master Addons for Elementor – Elementor Addons, Widgets, Mega Menu Builder, Popup Builder, Widget Builder & Template Kits / 3.1.9
Master Addons for Elementor – Elementor Addons, Widgets, Mega Menu Builder, Popup Builder, Widget Builder & Template Kits v3.1.9
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 / classes / importer / class-jltma-templates-importer.php

class-jltma-templates-importer.php in Master Addons for Elementor – Elementor Addons, Widgets, Mega Menu Builder, Popup Builder, Widget Builder & Template Kits 3.1.9, at inc/classes/importer/class-jltma-templates-importer.php

266 lines 8.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace MasterAddons\Inc\Classes\Importer;
3
4 if (!defined('ABSPATH')) {
5 exit; // Exit if accessed directly.
6 }
7
8 /**
9 * Master Addons Templates Kit Importer
10 * Handles importing template kits from local JSON files
11 */
12 class Templates_Importer {
13
14 private $kit_id;
15 private $kit_path;
16 private $manifest_data;
17
18 public function __construct($kit_id) {
19 $this->kit_id = $kit_id;
20 $upload_dir = wp_upload_dir();
21 $this->kit_path = $upload_dir['basedir'] . '/master_addons/templates_kit/' . $kit_id . '/';
22 $this->load_manifest();
23 }
24
25 /**
26 * Load kit manifest data
27 */
28 private function load_manifest() {
29 $manifest_file = $this->kit_path . 'manifest.json';
30
31 if (!file_exists($manifest_file)) {
32 throw new \Exception('Template kit manifest not found: ' . esc_html($this->kit_id)); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- exception message used internally, not echoed directly
33 }
34
35 $manifest_content = file_get_contents($manifest_file);
36 $this->manifest_data = json_decode($manifest_content, true);
37
38 if (!$this->manifest_data) {
39 throw new \Exception('Invalid manifest data for kit: ' . esc_html($this->kit_id)); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- exception message used internally, not echoed directly
40 }
41 }
42
43 /**
44 * Import entire template kit
45 */
46 public function import_kit() {
47 try {
48 // Set up WordPress environment
49 $this->setup_import_environment();
50
51 // Import global settings
52 $this->import_global_settings();
53
54 // Import all pages/templates
55 $imported_pages = array();
56 foreach ($this->manifest_data['pages'] as $page_slug) {
57 $page_id = $this->import_template($page_slug);
58 if ($page_id) {
59 $imported_pages[$page_slug] = $page_id;
60 }
61 }
62
63 // Set home page if exists
64 if (isset($imported_pages['home'])) {
65 $this->set_home_page($imported_pages['home']);
66 }
67
68 // Mark imported content
69 $this->mark_imported_content($imported_pages);
70
71 return array(
72 'success' => true,
73 'imported_pages' => $imported_pages,
74 'message' => 'Template kit imported successfully!'
75 );
76
77 } catch (\Exception $e) {
78 return array(
79 'success' => false,
80 'message' => $e->getMessage()
81 );
82 }
83 }
84
85 /**
86 * Import single template
87 */
88 public function import_template($template_slug) {
89 $template_file = $this->kit_path . $template_slug . '.json';
90
91 if (!file_exists($template_file)) {
92 throw new \Exception('Template file not found: ' . esc_html($template_slug)); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- exception message used internally, not echoed directly
93 }
94
95 $template_content = file_get_contents($template_file);
96 $template_data = json_decode($template_content, true);
97
98 if (!$template_data) {
99 throw new \Exception('Invalid template data: ' . esc_html($template_slug)); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- exception message used internally, not echoed directly
100 }
101
102 // Create WordPress page
103 $post_type = $template_data['page_settings']['post_type'] ?? 'page';
104
105 $page_id = wp_insert_post(array(
106 'post_title' => $template_data['page_settings']['post_title'],
107 'post_type' => $post_type,
108 'post_status' => 'publish',
109 'post_content' => '', // Elementor handles content
110 'meta_input' => array(
111 '_elementor_edit_mode' => 'builder',
112 '_elementor_template_type' => 'wp-page',
113 '_elementor_version' => $template_data['version'],
114 '_elementor_data' => wp_slash(json_encode($template_data['content'])),
115 '_jltma_demo_import_item' => true,
116 '_jltma_import_kit_id' => $this->kit_id,
117 '_jltma_import_date' => current_time('mysql'),
118 '_wp_page_template' => 'elementor_canvas',
119 )
120 ));
121
122 if (is_wp_error($page_id)) {
123 throw new \Exception('Failed to create page: ' . esc_html($page_id->get_error_message())); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- exception message used internally, not echoed directly
124 }
125
126 return $page_id;
127 }
128
129 /**
130 * Setup import environment
131 */
132 private function setup_import_environment() {
133 // Disable image generation during import
134 add_filter('intermediate_image_sizes_advanced', '__return_empty_array');
135
136 // Increase memory and time limits for the import.
137 wp_raise_memory_limit('admin');
138 if (function_exists('set_time_limit')) {
139 set_time_limit(300);
140 }
141 }
142
143 /**
144 * Import global settings from manifest
145 */
146 private function import_global_settings() {
147 if (!isset($this->manifest_data['global_settings'])) {
148 return;
149 }
150
151 $global_settings = $this->manifest_data['global_settings'];
152
153 // Import Elementor global colors
154 if (isset($global_settings['colors'])) {
155 $this->import_elementor_colors($global_settings['colors']);
156 }
157
158 // Import Elementor global fonts
159 if (isset($global_settings['typography'])) {
160 $this->import_elementor_fonts($global_settings['typography']);
161 }
162 }
163
164 /**
165 * Import Elementor global colors
166 */
167 private function import_elementor_colors($colors) {
168 $elementor_scheme = get_option('elementor_scheme_color', array());
169
170 $color_mapping = array(
171 'primary' => '1',
172 'secondary' => '2',
173 'accent' => '3'
174 );
175
176 foreach ($colors as $color_name => $color_value) {
177 if (isset($color_mapping[$color_name])) {
178 $elementor_scheme[$color_mapping[$color_name]] = $color_value;
179 }
180 }
181
182 update_option('elementor_scheme_color', $elementor_scheme);
183 }
184
185 /**
186 * Import Elementor global fonts
187 */
188 private function import_elementor_fonts($typography) {
189 $elementor_scheme = get_option('elementor_scheme_typography', array());
190
191 if (isset($typography['primary_font'])) {
192 $elementor_scheme['1'] = array(
193 'font_family' => $typography['primary_font'],
194 'font_weight' => '400'
195 );
196 }
197
198 if (isset($typography['secondary_font'])) {
199 $elementor_scheme['2'] = array(
200 'font_family' => $typography['secondary_font'],
201 'font_weight' => '400'
202 );
203 }
204
205 update_option('elementor_scheme_typography', $elementor_scheme);
206 }
207
208 /**
209 * Set imported page as home page
210 */
211 private function set_home_page($page_id) {
212 update_option('show_on_front', 'page');
213 update_option('page_on_front', $page_id);
214 }
215
216 /**
217 * Mark content as imported for cleanup purposes
218 */
219 private function mark_imported_content($imported_pages) {
220 foreach ($imported_pages as $slug => $page_id) {
221 update_post_meta($page_id, '_jltma_demo_import_item', true);
222 update_post_meta($page_id, '_jltma_import_kit_id', $this->kit_id);
223 update_post_meta($page_id, '_jltma_import_date', current_time('mysql'));
224 }
225 }
226
227 /**
228 * Get kit manifest data
229 */
230 public function get_manifest() {
231 return $this->manifest_data;
232 }
233
234 /**
235 * Check if kit exists
236 */
237 public static function kit_exists($kit_id) {
238 $upload_dir = wp_upload_dir();
239 $kit_path = $upload_dir['basedir'] . '/master_addons/templates_kit/' . $kit_id . '/';
240 return file_exists($kit_path . 'manifest.json');
241 }
242
243 /**
244 * Get all available kits
245 */
246 public static function get_available_kits() {
247 $upload_dir = wp_upload_dir();
248 $kits_path = $upload_dir['basedir'] . '/master_addons/templates_kit/';
249 $kits = array();
250
251 if (is_dir($kits_path)) {
252 $directories = scandir($kits_path);
253 foreach ($directories as $dir) {
254 if ($dir !== '.' && $dir !== '..' && is_dir($kits_path . $dir)) {
255 $manifest_file = $kits_path . $dir . '/manifest.json';
256 if (file_exists($manifest_file)) {
257 $kits[] = $dir;
258 }
259 }
260 }
261 }
262
263 return $kits;
264 }
265 }
266