| @@ -8,8 +8,9 @@ | ||
| 8 | 8 | use Elementor\Core\Settings\Page\Model; |
| 9 | 9 | |
| 10 | 10 | use Elementor\Plugin as ElementorPlugin; |
| 11 | 11 | use Elementor\TemplateLibrary\Source_Local as ElementorLocal; |
| 12 | +use Templately\Core\Importer\Utils\Utils; | |
| 12 | 13 | |
| 13 | 14 | class Elementor extends ElementorLocal { |
| 14 | 15 | /** |
| 15 | 16 | * Get template data. |
| @@ -20,18 +21,36 @@ | ||
| 20 | 21 | * |
| 21 | 22 | * @return array Remote Template data. |
| 22 | 23 | */ |
| 23 | 24 | public function get_data( array $args ) { |
| 25 | + Utils::add_gd_editor_filter(); | |
| 24 | 26 | ElementorPlugin::$instance->editor->set_edit_mode( true ); |
| 25 | 27 | |
| 28 | + // Ensure the Pro-promotion child-type fix is active for every import path (not just FSI), | |
| 29 | + // otherwise saving a template that uses an unavailable Elementor Pro widget (e.g. | |
| 30 | + // nested-carousel) fatals while Elementor resolves its nested children. See | |
| 31 | + // \Templately\Core\Platform\Elementor::filter_child_type(). | |
| 32 | + \Templately\Core\Platform\Elementor::register_child_type_filter(); | |
| 33 | + | |
| 26 | 34 | $args['content'] = $this->replace_elements_ids( $args['content'] ); |
| 27 | 35 | $args['content'] = $this->process_export_import_content( $args['content'], 'on_import' ); |
| 28 | 36 | |
| 29 | 37 | $args['content'] = $this->iterate_data( |
| 30 | 38 | $args['content'], function( $element_data ) { |
| 31 | - $element = ElementorPlugin::$instance->elements_manager->create_element_instance( $element_data ); | |
| 39 | + // A widget with null/missing widgetType causes get_widget_types(null) to return ALL | |
| 40 | + // registered widgets as an array. The subsequent get_default_args() call then crashes | |
| 41 | + // with "Call to a member function get_default_args() on array". Guard early. | |
| 42 | + if ( 'widget' === ( $element_data['elType'] ?? '' ) && ! isset( $element_data['widgetType'] ) ) { | |
| 43 | + return null; | |
| 44 | + } | |
| 32 | 45 | |
| 33 | - // If the widget/element isn't exist, like a plugin that creates a widget but deactivated | |
| 46 | + try { | |
| 47 | + $element = ElementorPlugin::$instance->elements_manager->create_element_instance( $element_data ); | |
| 48 | + } catch ( \Throwable $e ) { | |
| 49 | + return null; | |
| 50 | + } | |
| 51 | + | |
| 52 | + // If the widget/element doesn't exist, e.g. a deactivated plugin's widget | |
| 34 | 53 | if ( ! $element ) { |
| 35 | 54 | return null; |
| 36 | 55 | } |
| 37 | 56 | |
| @@ -64,24 +83,42 @@ | ||
| 64 | 83 | if ( ! is_array( $content ) ) { |
| 65 | 84 | return new WP_Error( 'file_error', 'Invalid File' ); |
| 66 | 85 | } |
| 67 | 86 | |
| 68 | - $page_settings = $this->page_settings( $data ); | |
| 87 | + // type is already resolved by Platform::resolve_library_type() before reaching here. | |
| 88 | + // Validate it exists; fall back to 'page_single' if somehow unregistered. | |
| 89 | + $type = $data['type'] ?? 'page_single'; | |
| 90 | + $registered_types = templately()->theme_builder::$templates_manager->get_template_types(); | |
| 91 | + if ( ! isset( $registered_types[ $type ] ) ) { | |
| 92 | + $type = 'page_single'; | |
| 93 | + } | |
| 69 | 94 | |
| 70 | - // TODO: type check for (theme builder) | |
| 95 | + $factory = new \Templately\Builder\Factory\TemplateFactory( 'elementor' ); | |
| 71 | 96 | |
| 72 | - $template_id = $this->save_item( [ | |
| 73 | - 'content' => $content, | |
| 74 | - 'title' => $data['title'], | |
| 75 | - 'type' => $data['type'], | |
| 76 | - 'page_settings' => $page_settings, | |
| 97 | + $template = $factory->create( $type, [ | |
| 98 | + 'post_title' => $data['title'], | |
| 99 | + 'post_status' => 'publish', | |
| 100 | + 'post_type' => 'templately_library', | |
| 77 | 101 | ] ); |
| 78 | 102 | |
| 79 | - if ( is_wp_error( $template_id ) ) { | |
| 80 | - return $template_id; | |
| 103 | + if ( is_wp_error( $template ) ) { | |
| 104 | + return $template; | |
| 81 | 105 | } |
| 82 | 106 | |
| 83 | - return $this->get_item( $template_id ); | |
| 107 | + $template->import( array_merge( $data, [ | |
| 108 | + 'import_settings' => [ | |
| 109 | + 'title' => $data['title'], | |
| 110 | + 'type' => $type, | |
| 111 | + ] | |
| 112 | + ] ) ); | |
| 113 | + | |
| 114 | + $post_id = $template->get_main_id(); | |
| 115 | + | |
| 116 | + return [ | |
| 117 | + 'template_id' => $post_id, | |
| 118 | + 'edit_link' => get_edit_post_link( $post_id, 'internal' ), | |
| 119 | + 'url' => get_permalink( $post_id ), | |
| 120 | + ]; | |
| 84 | 121 | } |
| 85 | 122 | |
| 86 | 123 | public function create_page( $template_data ){ |
| 87 | 124 | $page_settings = $this->page_settings( $template_data ); |
| @@ -93,10 +130,13 @@ | ||
| 93 | 130 | ]; |
| 94 | 131 | |
| 95 | 132 | $template_data = wp_parse_args( $template_data, $defaults ); |
| 96 | 133 | |
| 134 | + // Elementor has no 'block' document type; map it to 'section' (saved section). | |
| 135 | + $doc_type = ( 'block' === $template_data['type'] ) ? 'section' : $template_data['type']; | |
| 136 | + | |
| 97 | 137 | $document = ElementorPlugin::$instance->documents->create( |
| 98 | - $template_data['type'], | |
| 138 | + $doc_type, | |
| 99 | 139 | [ |
| 100 | 140 | 'post_title' => $template_data['post_title'], |
| 101 | 141 | 'post_status' => $template_data['status'], |
| 102 | 142 | 'post_type' => 'page', |