| @@ -8,9 +8,8 @@ | ||
| 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; | |
| 13 | 12 | |
| 14 | 13 | class Elementor extends ElementorLocal { |
| 15 | 14 | /** |
| 16 | 15 | * Get template data. |
| @@ -21,51 +20,13 @@ | ||
| 21 | 20 | * |
| 22 | 21 | * @return array Remote Template data. |
| 23 | 22 | */ |
| 24 | 23 | public function get_data( array $args ) { |
| 25 | - Utils::add_gd_editor_filter(); | |
| 26 | 24 | ElementorPlugin::$instance->editor->set_edit_mode( true ); |
| 27 | 25 | |
| 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 | - | |
| 34 | 26 | $args['content'] = $this->replace_elements_ids( $args['content'] ); |
| 35 | 27 | $args['content'] = $this->process_export_import_content( $args['content'], 'on_import' ); |
| 36 | 28 | |
| 37 | - $args['content'] = $this->iterate_data( | |
| 38 | - $args['content'], function( $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 | - } | |
| 45 | - | |
| 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 | |
| 53 | - if ( ! $element ) { | |
| 54 | - return null; | |
| 55 | - } | |
| 56 | - | |
| 57 | - return $element_data; | |
| 58 | - } | |
| 59 | - ); | |
| 60 | - | |
| 61 | - if (!isset($args['page_settings'])) { | |
| 62 | - $args['page_settings'] = array(); | |
| 63 | - } | |
| 64 | - if(!isset($args['page_settings']["template"])){ | |
| 65 | - $args['page_settings']["template"] = "elementor_header_footer"; | |
| 66 | - } | |
| 67 | - | |
| 68 | 29 | // $post_id = false; // FIXME: need to check later on. |
| 69 | 30 | // $document = ElementorPlugin::$instance->documents->get( $post_id ); |
| 70 | 31 | // if ( $document ) { |
| 71 | 32 | // $args['content'] = $document->get_elements_raw_data( $args['content'], true ); |
| @@ -83,42 +44,22 @@ | ||
| 83 | 44 | if ( ! is_array( $content ) ) { |
| 84 | 45 | return new WP_Error( 'file_error', 'Invalid File' ); |
| 85 | 46 | } |
| 86 | 47 | |
| 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 | - } | |
| 48 | + $page_settings = $this->page_settings( $data ); | |
| 94 | 49 | |
| 95 | - $factory = new \Templately\Builder\Factory\TemplateFactory( 'elementor' ); | |
| 96 | - | |
| 97 | - $template = $factory->create( $type, [ | |
| 98 | - 'post_title' => $data['title'], | |
| 99 | - 'post_status' => 'publish', | |
| 100 | - 'post_type' => 'templately_library', | |
| 50 | + $template_id = $this->save_item( [ | |
| 51 | + 'content' => $content, | |
| 52 | + 'title' => $data['title'], | |
| 53 | + 'type' => $data['type'], | |
| 54 | + 'page_settings' => $page_settings, | |
| 101 | 55 | ] ); |
| 102 | 56 | |
| 103 | - if ( is_wp_error( $template ) ) { | |
| 104 | - return $template; | |
| 57 | + if ( is_wp_error( $template_id ) ) { | |
| 58 | + return $template_id; | |
| 105 | 59 | } |
| 106 | 60 | |
| 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 | - ]; | |
| 61 | + return $this->get_item( $template_id ); | |
| 121 | 62 | } |
| 122 | 63 | |
| 123 | 64 | public function create_page( $template_data ){ |
| 124 | 65 | $page_settings = $this->page_settings( $template_data ); |
| @@ -130,13 +71,10 @@ | ||
| 130 | 71 | ]; |
| 131 | 72 | |
| 132 | 73 | $template_data = wp_parse_args( $template_data, $defaults ); |
| 133 | 74 | |
| 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 | - | |
| 137 | 75 | $document = ElementorPlugin::$instance->documents->create( |
| 138 | - $doc_type, | |
| 76 | + $template_data['type'], | |
| 139 | 77 | [ |
| 140 | 78 | 'post_title' => $template_data['post_title'], |
| 141 | 79 | 'post_status' => $template_data['status'], |
| 142 | 80 | 'post_type' => 'page', |
| @@ -166,66 +104,22 @@ | ||
| 166 | 104 | */ |
| 167 | 105 | private function page_settings( $template_data ) { |
| 168 | 106 | $page_settings = []; |
| 169 | 107 | |
| 170 | - if (!isset($template_data['page_settings'])) { | |
| 171 | - $template_data['page_settings'] = array(); | |
| 172 | - } | |
| 108 | + if ( ! empty( $template_data['page_settings'] ) ) { | |
| 109 | + $page = new Model( [ | |
| 110 | + 'id' => 0, | |
| 111 | + 'settings' => $template_data['page_settings'], | |
| 112 | + ] ); | |
| 173 | 113 | |
| 174 | - if(!isset($template_data['page_settings']["template"])){ | |
| 175 | - $template_data['page_settings']["template"] = "elementor_header_footer"; | |
| 176 | - } | |
| 114 | + $page_settings_data = $this->process_element_export_import_content( $page, 'on_import' ); | |
| 177 | 115 | |
| 178 | - $page = new Model([ | |
| 179 | - 'id' => 0, | |
| 180 | - 'settings' => $template_data['page_settings'], | |
| 181 | - ]); | |
| 182 | - | |
| 183 | - $page_settings_data = $this->process_element_export_import_content($page, 'on_import'); | |
| 184 | - | |
| 185 | - if (!empty($page_settings_data['settings'])) { | |
| 186 | - $page_settings = $page_settings_data['settings']; | |
| 116 | + if ( ! empty( $page_settings_data['settings'] ) ) { | |
| 117 | + $page_settings = $page_settings_data['settings']; | |
| 118 | + } | |
| 187 | 119 | } |
| 188 | 120 | |
| 189 | 121 | return $page_settings; |
| 190 | - } | |
| 191 | - | |
| 192 | - /** | |
| 193 | - * Iterate data. | |
| 194 | - * | |
| 195 | - * Accept any type of Elementor data and a callback function. The callback | |
| 196 | - * function runs recursively for each element and his child elements. | |
| 197 | - * | |
| 198 | - * @since 1.0.0 | |
| 199 | - * @access public | |
| 200 | - * | |
| 201 | - * @param array $data_container Any type of elementor data. | |
| 202 | - * @param callable $callback A function to iterate data by. | |
| 203 | - * @param array $args Array of args pointers for passing parameters in & out of the callback | |
| 204 | - * | |
| 205 | - * @return mixed Iterated data. | |
| 206 | - */ | |
| 207 | - public function iterate_data( $data_container, $callback, $args = [] ) { | |
| 208 | - if ( isset( $data_container['elType'] ) ) { | |
| 209 | - if ( ! empty( $data_container['elements'] ) ) { | |
| 210 | - $data_container['elements'] = $this->iterate_data( $data_container['elements'], $callback, $args ); | |
| 211 | - } | |
| 212 | - | |
| 213 | - return call_user_func( $callback, $data_container, $args ); | |
| 214 | - } | |
| 215 | - | |
| 216 | - foreach ( $data_container as $element_key => $element_value ) { | |
| 217 | - $element_data = $this->iterate_data( $data_container[ $element_key ], $callback, $args ); | |
| 218 | - | |
| 219 | - if ( null === $element_data ) { | |
| 220 | - unset($data_container[ $element_key ]); | |
| 221 | - continue; | |
| 222 | - } | |
| 223 | - | |
| 224 | - $data_container[ $element_key ] = $element_data; | |
| 225 | - } | |
| 226 | - | |
| 227 | - return array_values($data_container); | |
| 228 | 122 | } |
| 229 | 123 | |
| 230 | 124 | } |
| 231 | 125 | |