| 1 |
<?php |
| 2 |
|
| 3 |
namespace Templately\Core\Importer\Runners; |
| 4 |
|
| 5 |
use Elementor\Plugin; |
| 6 |
use Elementor\TemplateLibrary\Source_Local; |
| 7 |
use Exception; |
| 8 |
use Templately\Core\Importer\Utils\Utils; |
| 9 |
use Templately\Utils\Helper; |
| 10 |
|
| 11 |
class ElementorContent extends BaseRunner { |
| 12 |
public function get_name(): string { |
| 13 |
return 'content'; |
| 14 |
} |
| 15 |
|
| 16 |
public function get_label(): string { |
| 17 |
return __( 'Elementor', 'templately' ); |
| 18 |
} |
| 19 |
|
| 20 |
public function should_run( $data, $imported_data = [] ): bool { |
| 21 |
return $this->manifest['platform'] === 'elementor' && ! empty( $this->manifest['content'] ); |
| 22 |
} |
| 23 |
|
| 24 |
public function should_log(): bool { |
| 25 |
return true; |
| 26 |
} |
| 27 |
|
| 28 |
public function get_action(): string { |
| 29 |
return 'updateLog'; |
| 30 |
} |
| 31 |
|
| 32 |
public function log_message(): string { |
| 33 |
return __( 'Importing Elementor Page and Post Templates', 'templately' ); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Get kit by ID with backward compatibility. |
| 38 |
* |
| 39 |
* The get_kit() method was introduced in Elementor 3.13.0. |
| 40 |
* For older versions, we fall back to using documents->get() directly. |
| 41 |
* |
| 42 |
* @param \Elementor\Core\Kits\Manager $kits_manager The kits manager instance. |
| 43 |
* @param int $kit_id The kit ID to retrieve. |
| 44 |
* @return \Elementor\Core\Kits\Documents\Kit|null The kit document or null. |
| 45 |
*/ |
| 46 |
private function get_kit_with_fallback( $kits_manager, $kit_id ) { |
| 47 |
// Check if the get_kit method exists (Elementor 3.13.0+) |
| 48 |
if ( method_exists( $kits_manager, 'get_kit' ) ) { |
| 49 |
return $kits_manager->get_kit( $kit_id ); |
| 50 |
} |
| 51 |
|
| 52 |
// Fallback for older Elementor versions (< 3.13.0) |
| 53 |
// This replicates the old get_active_kit() behavior |
| 54 |
return Plugin::$instance->documents->get( $kit_id ); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Create a new kit with backward compatibility. |
| 59 |
* |
| 60 |
* The create_new_kit() method was introduced in Elementor 3.13.0. |
| 61 |
* For older versions, we fall back to using documents->create() directly. |
| 62 |
* |
| 63 |
* @param \Elementor\Core\Kits\Manager $kits_manager The kits manager instance. |
| 64 |
* @param string $kit_name The name for the new kit. |
| 65 |
* @param array $settings The kit settings. |
| 66 |
* @param bool $active Whether to set this kit as active. |
| 67 |
* @return int The created kit ID. |
| 68 |
*/ |
| 69 |
private function create_new_kit_with_fallback( $kits_manager, $kit_name = '', $settings = [], $active = true ) { |
| 70 |
// Check if the create_new_kit method exists (Elementor 3.13.0+) |
| 71 |
if ( method_exists( $kits_manager, 'create_new_kit' ) ) { |
| 72 |
return $kits_manager->create_new_kit( $kit_name, $settings, $active ); |
| 73 |
} |
| 74 |
|
| 75 |
// Fallback for older Elementor versions (< 3.13.0) |
| 76 |
$kit_name = $kit_name ? $kit_name : esc_html__( 'Custom', 'templately' ); |
| 77 |
|
| 78 |
$kit = Plugin::$instance->documents->create( 'kit', [ |
| 79 |
'post_type' => Source_Local::CPT, |
| 80 |
'post_title' => $kit_name, |
| 81 |
'post_status' => 'publish', |
| 82 |
] ); |
| 83 |
|
| 84 |
$kit_id = $kit->get_id(); |
| 85 |
|
| 86 |
// Apply settings if provided |
| 87 |
if ( ! empty( $settings ) ) { |
| 88 |
$kit->save( [ 'settings' => $settings ] ); |
| 89 |
} |
| 90 |
|
| 91 |
// Set as active if requested |
| 92 |
if ( $active ) { |
| 93 |
update_option( $kits_manager::OPTION_ACTIVE, $kit_id ); |
| 94 |
} |
| 95 |
|
| 96 |
return $kit_id; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Create a default kit with backward compatibility. |
| 101 |
* |
| 102 |
* The create_default() method was made public in Elementor 3.12.0. |
| 103 |
* For older versions, we fall back to using documents->create() directly. |
| 104 |
* |
| 105 |
* @param \Elementor\Core\Kits\Manager $kits_manager The kits manager instance. |
| 106 |
* @return int The created kit ID. |
| 107 |
*/ |
| 108 |
private function create_default_kit_with_fallback( $kits_manager ) { |
| 109 |
// Check if create_default is publicly accessible (Elementor 3.12.0+) |
| 110 |
if ( method_exists( $kits_manager, 'create_default' ) && is_callable( [ $kits_manager, 'create_default' ] ) ) { |
| 111 |
return $kits_manager->create_default(); |
| 112 |
} |
| 113 |
|
| 114 |
// Fallback for older Elementor versions (< 3.12.0) |
| 115 |
$kit = Plugin::$instance->documents->create( 'kit', [ |
| 116 |
'post_type' => Source_Local::CPT, |
| 117 |
'post_title' => esc_html__( 'Default Kit', 'templately' ), |
| 118 |
'post_status' => 'publish', |
| 119 |
] ); |
| 120 |
|
| 121 |
return $kit->get_id(); |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* @throws Exception |
| 126 |
*/ |
| 127 |
public function import( $data, $imported_data ): array { |
| 128 |
$results = $data["imported_data"]["content"] ?? []; |
| 129 |
$contents = $this->manifest['content']; |
| 130 |
$path = $this->dir_path . 'content' . DIRECTORY_SEPARATOR; |
| 131 |
|
| 132 |
|
| 133 |
// $total = array_reduce( $contents, function ( $carry, $item ) { |
| 134 |
// return $carry + count( $item ); |
| 135 |
// }, 0 ); |
| 136 |
// $processed = 0; |
| 137 |
|
| 138 |
/** |
| 139 |
* Check if there is any active kit? |
| 140 |
* If not, create one. |
| 141 |
*/ |
| 142 |
|
| 143 |
$kits_manager = Plugin::$instance->kits_manager; |
| 144 |
|
| 145 |
$active_kit = $kits_manager->get_active_id(); |
| 146 |
$kit = $this->get_kit_with_fallback( $kits_manager, $active_kit ); |
| 147 |
$old_logo = $kit->get_settings('site_logo'); |
| 148 |
|
| 149 |
if(isset($this->manifest['has_settings']) && $this->manifest['has_settings'] && !$this->is_key_processed('global_colors', 'settings')){ |
| 150 |
// backing up the active kit id before updating the new one |
| 151 |
if(!get_option("__templately_" . $kits_manager::OPTION_ACTIVE)){ |
| 152 |
add_option("__templately_" . $kits_manager::OPTION_ACTIVE, $active_kit, '', 'no'); |
| 153 |
} |
| 154 |
else{ |
| 155 |
update_option("__templately_" . $kits_manager::OPTION_ACTIVE, $active_kit, 'no'); |
| 156 |
} |
| 157 |
|
| 158 |
$file = $this->dir_path . "settings.json"; |
| 159 |
$settings = Utils::read_json_file( $file ); |
| 160 |
|
| 161 |
if(isset($settings['site_name'])){ |
| 162 |
unset($settings['site_name']); |
| 163 |
} |
| 164 |
if(isset($settings['site_description'])){ |
| 165 |
unset($settings['site_description']); |
| 166 |
} |
| 167 |
if(!empty($data['color'])){ |
| 168 |
if (!empty($settings['system_colors'])) { |
| 169 |
foreach ($settings['system_colors'] as $key => $color) { |
| 170 |
$settings['system_colors'][$key]['color'] = $data['color'][$color['_id']] ?? $color['color']; |
| 171 |
} |
| 172 |
} |
| 173 |
if (!empty($settings['custom_colors'])) { |
| 174 |
foreach ($settings['custom_colors'] as $key => $color) { |
| 175 |
$settings['custom_colors'][$key]['color'] = $data['color'][$color['_id']] ?? $color['color']; |
| 176 |
} |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
if(!empty($data['typography'])){ |
| 181 |
if (!empty($settings['system_typography'])) { |
| 182 |
foreach ($settings['system_typography'] as $key => &$typography) { |
| 183 |
if(!empty($data['typography'][$typography['_id']])){ |
| 184 |
$typography = array_merge($typography, $data['typography'][$typography['_id']]); |
| 185 |
} |
| 186 |
} |
| 187 |
} |
| 188 |
if (!empty($settings['custom_typography'])) { |
| 189 |
foreach ($settings['custom_typography'] as $key => &$typography) { |
| 190 |
if(!empty($data['typography'][$typography['_id']])){ |
| 191 |
$typography = array_merge($typography, $data['typography'][$typography['_id']]); |
| 192 |
} |
| 193 |
} |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
if (!empty($data['logo']['id'])) { |
| 198 |
$settings['site_logo'] = $data['logo']; |
| 199 |
Utils::backup_option_value( 'site_logo' ); |
| 200 |
$this->origin->update_imported_list('attachment', $data['logo']['id']); |
| 201 |
} elseif (!empty($data['logo']['url']) && strpos($data['logo']['url'], 'data:image/') === 0) { |
| 202 |
// Upload base64 data URL |
| 203 |
$site_logo = Utils::upload_logo_base64($data['logo']['url'], $this->session_id); |
| 204 |
|
| 205 |
if(!empty($site_logo['id'])){ |
| 206 |
$settings['site_logo'] = $site_logo; |
| 207 |
Utils::backup_option_value( 'site_logo' ); |
| 208 |
$this->origin->update_imported_list('attachment', $site_logo['id']); |
| 209 |
} |
| 210 |
} elseif (!empty($data['logo'])) { |
| 211 |
$settings['site_logo'] = $old_logo; |
| 212 |
|
| 213 |
// If there's no old logo id, try to upload a new logo |
| 214 |
if (empty($old_logo['id'])) { |
| 215 |
// Upload regular URL |
| 216 |
$site_logo = Utils::upload_logo($data['logo'], $this->session_id); |
| 217 |
|
| 218 |
// If the upload was successful, use the new logo, otherwise use the old one |
| 219 |
if(!empty($site_logo['id'])){ |
| 220 |
$settings['site_logo'] = $site_logo; |
| 221 |
Utils::backup_option_value( 'site_logo' ); |
| 222 |
$this->origin->update_imported_list('attachment', $site_logo['id']); |
| 223 |
} |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
$kit_id = $this->create_new_kit_with_fallback( $kits_manager, $this->manifest['name'], $settings, true ); |
| 229 |
|
| 230 |
$kit = $this->get_kit_with_fallback( $kits_manager, $kit_id ); |
| 231 |
|
| 232 |
// $kit->update_settings( ['site_logo' => $settings['site_logo']] ); |
| 233 |
|
| 234 |
// Create an array with the post ID and the new title |
| 235 |
$post_data = array( |
| 236 |
'ID' => $kit_id, |
| 237 |
'post_title' => $this->manifest['name'] . " Kit", |
| 238 |
); |
| 239 |
// Update the post |
| 240 |
wp_update_post( $post_data ); |
| 241 |
|
| 242 |
$this->mark_key_processed('global_colors', 'settings'); |
| 243 |
} |
| 244 |
|
| 245 |
$active_kit = $kits_manager->get_active_id(); |
| 246 |
$kit = $this->get_kit_with_fallback( $kits_manager, $active_kit ); |
| 247 |
|
| 248 |
if ( ! $kit->get_id() ) { |
| 249 |
$kit_id = $this->create_default_kit_with_fallback( $kits_manager ); |
| 250 |
update_option( $kits_manager::OPTION_ACTIVE, $kit_id ); |
| 251 |
$kit = $this->get_kit_with_fallback( $kits_manager, $kit_id ); |
| 252 |
} |
| 253 |
|
| 254 |
// $processed = 0; |
| 255 |
$total = array_reduce($contents, function($carry, $item) { |
| 256 |
return $carry + count($item); |
| 257 |
}, 0); |
| 258 |
|
| 259 |
$results = $this->loop( $contents, function($post_type, $post, $results ) use($path, $imported_data, $total) { |
| 260 |
// Inner loop accumulates results for this post_type |
| 261 |
$inner_result = $this->loop( $post, function($id, $content_settings, $result ) use ($post_type, $results, $path, $imported_data, $total) { |
| 262 |
if ( post_type_exists( $post_type ) ) { |
| 263 |
|
| 264 |
$import = $this->import_post_type_content( $id, $post_type, $path, $imported_data, $content_settings ); |
| 265 |
|
| 266 |
if ( ! $import ) { |
| 267 |
$result[ $post_type ]['failed'][ $id ] = $import; |
| 268 |
} else { |
| 269 |
Utils::import_page_settings( $import, $content_settings ); |
| 270 |
$result[ $post_type ]['succeed'][ $id ] = $import; |
| 271 |
} |
| 272 |
|
| 273 |
// Broadcast Log - merge with outer results for accurate counting |
| 274 |
$merged = array_merge($results, $result); |
| 275 |
$processed = 0; |
| 276 |
foreach ($merged as $type => $data) { |
| 277 |
if (is_array($data)) { |
| 278 |
$processed += count($data['succeed'] ?? []) + count($data['failed'] ?? []); |
| 279 |
} |
| 280 |
} |
| 281 |
$progress = $total > 0 ? floor( ( 100 * $processed ) / $total ) : 100; |
| 282 |
$this->log( $progress ); |
| 283 |
} |
| 284 |
|
| 285 |
return $result; |
| 286 |
}, $post_type); |
| 287 |
|
| 288 |
// Non-recursive merge of inner result with outer results |
| 289 |
$results = array_merge($results, $inner_result); |
| 290 |
return $results; |
| 291 |
}); |
| 292 |
|
| 293 |
return [ 'content' => $results ]; |
| 294 |
} |
| 295 |
|
| 296 |
/** |
| 297 |
* @throws Exception |
| 298 |
*/ |
| 299 |
private function import_post_type_content( $id, $post_type, $path, $imported_data, $content_settings ) { |
| 300 |
try { |
| 301 |
$template = $this->factory->create( $content_settings['doc_type'], [ |
| 302 |
'post_title' => $content_settings['title'], |
| 303 |
'post_status' => 'publish', |
| 304 |
'post_type' => $post_type, |
| 305 |
] ); |
| 306 |
|
| 307 |
$file = $path . $post_type . DIRECTORY_SEPARATOR . "{$id}.json"; |
| 308 |
$post_data = Utils::read_json_file( $file ); |
| 309 |
|
| 310 |
if ( ! empty( $content_settings['data'] ) || $this->is_ai_content($id) ) { |
| 311 |
/** |
| 312 |
* TODO: |
| 313 |
* |
| 314 |
* We can check if there is any data for settings. |
| 315 |
* if yes: ignore content from insert. |
| 316 |
* |
| 317 |
* Process the content while finalizing. |
| 318 |
*/ |
| 319 |
// $this->json->prepare( $post_data['content'], $id, $content_settings['data'], $imported_data ); |
| 320 |
|
| 321 |
$post_data['content'] = []; |
| 322 |
} |
| 323 |
|
| 324 |
unset($content_settings['conditions']); |
| 325 |
$post_data['import_settings'] = $content_settings; |
| 326 |
|
| 327 |
$template->import( $post_data ); |
| 328 |
|
| 329 |
return $template->get_main_id(); |
| 330 |
} catch ( Exception $e ) { |
| 331 |
return false; |
| 332 |
} |
| 333 |
} |
| 334 |
} |