| 1 |
<?php |
| 2 |
|
| 3 |
namespace Templately\Core\Importer; |
| 4 |
|
| 5 |
use WP_Error; |
| 6 |
use function wp_parse_args; |
| 7 |
use function current_user_can; |
| 8 |
use Elementor\Core\Settings\Page\Model; |
| 9 |
|
| 10 |
use Elementor\Plugin as ElementorPlugin; |
| 11 |
use Elementor\TemplateLibrary\Source_Local as ElementorLocal; |
| 12 |
use Templately\Core\Importer\Utils\Utils; |
| 13 |
|
| 14 |
class Elementor extends ElementorLocal { |
| 15 |
/** |
| 16 |
* Get template data. |
| 17 |
* |
| 18 |
* @inheritDoc |
| 19 |
* |
| 20 |
* @param array $args Custom template arguments. |
| 21 |
* |
| 22 |
* @return array Remote Template data. |
| 23 |
*/ |
| 24 |
public function get_data( array $args ) { |
| 25 |
Utils::add_gd_editor_filter(); |
| 26 |
ElementorPlugin::$instance->editor->set_edit_mode( true ); |
| 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 |
|
| 34 |
$args['content'] = $this->replace_elements_ids( $args['content'] ); |
| 35 |
$args['content'] = $this->process_export_import_content( $args['content'], 'on_import' ); |
| 36 |
|
| 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 |
// $post_id = false; // FIXME: need to check later on. |
| 69 |
// $document = ElementorPlugin::$instance->documents->get( $post_id ); |
| 70 |
// if ( $document ) { |
| 71 |
// $args['content'] = $document->get_elements_raw_data( $args['content'], true ); |
| 72 |
// } |
| 73 |
return $args; |
| 74 |
} |
| 75 |
|
| 76 |
public function import_in_library( $data ) { |
| 77 |
if ( empty( $data ) ) { |
| 78 |
return new WP_Error( 'file_error', 'Invalid File' ); |
| 79 |
} |
| 80 |
|
| 81 |
$content = $data['content']; |
| 82 |
|
| 83 |
if ( ! is_array( $content ) ) { |
| 84 |
return new WP_Error( 'file_error', 'Invalid File' ); |
| 85 |
} |
| 86 |
|
| 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 |
} |
| 94 |
|
| 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', |
| 101 |
] ); |
| 102 |
|
| 103 |
if ( is_wp_error( $template ) ) { |
| 104 |
return $template; |
| 105 |
} |
| 106 |
|
| 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 |
]; |
| 121 |
} |
| 122 |
|
| 123 |
public function create_page( $template_data ){ |
| 124 |
$page_settings = $this->page_settings( $template_data ); |
| 125 |
|
| 126 |
$defaults = [ |
| 127 |
'post_title' => isset( $template_data['page_title'] ) ? $template_data['page_title'] : 'Templately: ' . $template_data['title'], |
| 128 |
'page_settings' => $page_settings, |
| 129 |
'status' => current_user_can( 'publish_posts' ) ? 'publish' : 'pending', |
| 130 |
]; |
| 131 |
|
| 132 |
$template_data = wp_parse_args( $template_data, $defaults ); |
| 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 |
|
| 137 |
$document = ElementorPlugin::$instance->documents->create( |
| 138 |
$doc_type, |
| 139 |
[ |
| 140 |
'post_title' => $template_data['post_title'], |
| 141 |
'post_status' => $template_data['status'], |
| 142 |
'post_type' => 'page', |
| 143 |
] |
| 144 |
); |
| 145 |
|
| 146 |
if ( is_wp_error( $document ) ) { |
| 147 |
/** |
| 148 |
* @var WP_Error $document |
| 149 |
*/ |
| 150 |
return $document; |
| 151 |
} |
| 152 |
|
| 153 |
$document->save( [ |
| 154 |
'elements' => $template_data['content'], |
| 155 |
'settings' => $page_settings, |
| 156 |
] ); |
| 157 |
|
| 158 |
return $document->get_main_id(); |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* @param $template_data |
| 163 |
* @noinspection DuplicatedCode |
| 164 |
* |
| 165 |
* @return array |
| 166 |
*/ |
| 167 |
private function page_settings( $template_data ) { |
| 168 |
$page_settings = []; |
| 169 |
|
| 170 |
if (!isset($template_data['page_settings'])) { |
| 171 |
$template_data['page_settings'] = array(); |
| 172 |
} |
| 173 |
|
| 174 |
if(!isset($template_data['page_settings']["template"])){ |
| 175 |
$template_data['page_settings']["template"] = "elementor_header_footer"; |
| 176 |
} |
| 177 |
|
| 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']; |
| 187 |
} |
| 188 |
|
| 189 |
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 |
} |
| 229 |
|
| 230 |
} |
| 231 |
|
| 232 |
|