| @@ -15,8 +15,9 @@ | ||
| 15 | 15 | use Elementor\Plugin; |
| 16 | 16 | use Elementor\Core\Kits\Documents\Kit; |
| 17 | 17 | use Elementor\Core\Settings\Manager as SettingsManager; |
| 18 | 18 | use Templately\Core\Importer\Elementor as ElementorImporter; |
| 19 | +use Templately\Core\Importer\Utils\Utils; | |
| 19 | 20 | use Elementor\TemplateLibrary\Source_Local as ElementorLocal; |
| 20 | 21 | |
| 21 | 22 | class Elementor extends Platform { |
| 22 | 23 | private $id = 'elementor'; |
| @@ -76,9 +77,9 @@ | ||
| 76 | 77 | * Checking the template eligibility for import. |
| 77 | 78 | * |
| 78 | 79 | * @return WP_Error|void |
| 79 | 80 | */ |
| 80 | - protected function is_eligible( $template, $types = [ 'page', 'section','block' ] ) { | |
| 81 | + protected function is_eligible( $template, $types = [ 'page', 'section', 'block' ] ) { | |
| 81 | 82 | if ( ! Helper::is_plugin_active( 'elementor-pro/elementor-pro.php' ) && |
| 82 | 83 | isset( $template['type'] ) && |
| 83 | 84 | ! in_array( $template['type'], $types, true ) |
| 84 | 85 | ) { |
| @@ -287,9 +288,9 @@ | ||
| 287 | 288 | 'visit' => get_permalink( $inserted_ID ) |
| 288 | 289 | ]; |
| 289 | 290 | } |
| 290 | 291 | |
| 291 | - public function import_in_library( $id, $importer = null, $settings = [] ) { | |
| 292 | + public function import_in_library( $id, $importer = null, $settings = [], $template_type = '', $type = '' ) { | |
| 292 | 293 | $template_data = $importer->get_content( $id, 'elementor', 'remote' ); |
| 293 | 294 | |
| 294 | 295 | if ( is_wp_error( $template_data ) ) { |
| 295 | 296 | return $template_data; |
| @@ -298,12 +299,21 @@ | ||
| 298 | 299 | if ( ! empty( $settings ) && ! empty( $template_data['content'] ) && is_array( $template_data['content'] ) ) { |
| 299 | 300 | $template_data['content'] = \Templately\Core\Importer\Utils\ElementorSettingsMerger::merge( $template_data['content'], $settings ); |
| 300 | 301 | } |
| 301 | 302 | |
| 302 | - if(trim($template_data['type']) == 'block'){ | |
| 303 | - $template_data['type'] = 'section'; | |
| 303 | + // Frontend-provided template_type (from item details API) takes precedence over | |
| 304 | + // whatever the fetched content JSON reports. | |
| 305 | + if ( ! empty( $template_type ) ) { | |
| 306 | + $template_data['template_type'] = $template_type; | |
| 304 | 307 | } |
| 305 | 308 | |
| 309 | + // Resolve the correct Builder Type using template_type before passing to the importer. | |
| 310 | + try { | |
| 311 | + $template_data['type'] = static::resolve_library_type( $template_data ); | |
| 312 | + } catch ( \InvalidArgumentException $e ) { | |
| 313 | + return Helper::error( 'unsupported_template_type', $e->getMessage(), 'import', 400 ); | |
| 314 | + } | |
| 315 | + | |
| 306 | 316 | $importer = new ElementorImporter; |
| 307 | 317 | $imported_template = $importer->import_in_library( $template_data ); |
| 308 | 318 | |
| 309 | 319 | if( is_wp_error( $imported_template ) ) { |
| @@ -309,12 +319,25 @@ | ||
| 309 | 319 | if( is_wp_error( $imported_template ) ) { |
| 310 | 320 | return $imported_template; |
| 311 | 321 | } |
| 312 | 322 | |
| 323 | + $post_id = $imported_template['template_id']; | |
| 324 | + | |
| 325 | + // Companion content for archive/fluent types — same side effects (Shop page, | |
| 326 | + // page_for_posts, LearnDash courses slug, fluent single-page template) the FSI | |
| 327 | + // Templates runner performs when importing these template types. | |
| 328 | + Utils::create_companion_content( $template_data['type'], $template_data['title'] ?? '', 'elementor' ); | |
| 329 | + | |
| 330 | + // Mark user as having imported a template | |
| 331 | + \Templately\Utils\Options::get_instance()->set( 'has_imported_template', true ); | |
| 332 | + | |
| 333 | + $document = Plugin::$instance->documents->get( $post_id ); | |
| 334 | + $elementor_edit_link = $document ? $document->get_edit_url() : admin_url( 'post.php?post=' . $post_id . '&action=elementor' ); | |
| 335 | + | |
| 313 | 336 | return [ |
| 314 | - 'post_id' => $imported_template['template_id'], | |
| 315 | - 'edit_link' => get_edit_post_link( $imported_template['template_id'], 'internal' ), | |
| 316 | - 'elementor_edit_link' => Plugin::$instance->documents->get( $imported_template['template_id'] )->get_edit_url(), | |
| 337 | + 'post_id' => $post_id, | |
| 338 | + 'edit_link' => get_edit_post_link( $post_id, 'internal' ), | |
| 339 | + 'elementor_edit_link' => $elementor_edit_link, | |
| 317 | 340 | 'visit' => $imported_template['url'] |
| 318 | 341 | ]; |
| 319 | 342 | } |
| 320 | 343 | |
| @@ -338,9 +361,13 @@ | ||
| 338 | 361 | |
| 339 | 362 | return $_response; |
| 340 | 363 | } |
| 341 | 364 | |
| 342 | - public function insert( $data ) { | |
| 365 | + public function insert( $data, $postId = 0, $settings = [] ) { | |
| 366 | + if ( ! empty( $settings ) && is_array( $settings ) && ! empty( $data['content'] ) && is_array( $data['content'] ) ) { | |
| 367 | + $data['content'] = \Templately\Core\Importer\Utils\ElementorSettingsMerger::merge( $data['content'], $settings ); | |
| 368 | + } | |
| 369 | + | |
| 343 | 370 | $importer = new ElementorImporter(); |
| 344 | 371 | |
| 345 | 372 | return $importer->get_data( $data ); |
| 346 | 373 | } |
| @@ -345,8 +372,28 @@ | ||
| 345 | 372 | return $importer->get_data( $data ); |
| 346 | 373 | } |
| 347 | 374 | |
| 348 | 375 | /** |
| 376 | + * Register the Pro-promotion child-type fix once per request. | |
| 377 | + * | |
| 378 | + * Templates can reference Elementor Pro widgets (e.g. nested-carousel) that aren't | |
| 379 | + * installed. Elementor substitutes a Pro_Widget_Promotion placeholder, but its | |
| 380 | + * get_child_type() can't resolve the original nested children — so saving such a | |
| 381 | + * template fatals with "get_default_args() on array". filter_child_type() restores | |
| 382 | + * the correct child type. FSI registers this in FullSiteImport; single-template | |
| 383 | + * imports go through Importer\Elementor::get_data(), which calls this so the filter | |
| 384 | + * is active for every import path. | |
| 385 | + */ | |
| 386 | + public static function register_child_type_filter() { | |
| 387 | + static $registered = false; | |
| 388 | + if ( $registered ) { | |
| 389 | + return; | |
| 390 | + } | |
| 391 | + $registered = true; | |
| 392 | + add_filter( 'elementor/element/get_child_type', [ __CLASS__, 'filter_child_type' ], 10, 3 ); | |
| 393 | + } | |
| 394 | + | |
| 395 | + /** | |
| 349 | 396 | * Filter Child Type |
| 350 | 397 | * |
| 351 | 398 | * @param $child_type |
| 352 | 399 | * @param $element_data |
| @@ -371,5 +418,5 @@ | ||
| 371 | 418 | } |
| 372 | 419 | |
| 373 | 420 | return $child_type; |
| 374 | 421 | } |
| 375 | -} | |
| 422 | +} | |