| 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 |
|
| 13 |
class Elementor extends ElementorLocal { |
| 14 |
/** |
| 15 |
* Get template data. |
| 16 |
* |
| 17 |
* @inheritDoc |
| 18 |
* |
| 19 |
* @param array $args Custom template arguments. |
| 20 |
* |
| 21 |
* @return array Remote Template data. |
| 22 |
*/ |
| 23 |
public function get_data( array $args ) { |
| 24 |
ElementorPlugin::$instance->editor->set_edit_mode( true ); |
| 25 |
|
| 26 |
$args['content'] = $this->replace_elements_ids( $args['content'] ); |
| 27 |
$args['content'] = $this->process_export_import_content( $args['content'], 'on_import' ); |
| 28 |
|
| 29 |
$args['content'] = $this->iterate_data( |
| 30 |
$args['content'], function( $element_data ) { |
| 31 |
$element = ElementorPlugin::$instance->elements_manager->create_element_instance( $element_data ); |
| 32 |
|
| 33 |
// If the widget/element isn't exist, like a plugin that creates a widget but deactivated |
| 34 |
if ( ! $element ) { |
| 35 |
return null; |
| 36 |
} |
| 37 |
|
| 38 |
return $element_data; |
| 39 |
} |
| 40 |
); |
| 41 |
|
| 42 |
if (!isset($args['page_settings'])) { |
| 43 |
$args['page_settings'] = array(); |
| 44 |
} |
| 45 |
if(!isset($args['page_settings']["template"])){ |
| 46 |
$args['page_settings']["template"] = "elementor_header_footer"; |
| 47 |
} |
| 48 |
|
| 49 |
// $post_id = false; // FIXME: need to check later on. |
| 50 |
// $document = ElementorPlugin::$instance->documents->get( $post_id ); |
| 51 |
// if ( $document ) { |
| 52 |
// $args['content'] = $document->get_elements_raw_data( $args['content'], true ); |
| 53 |
// } |
| 54 |
return $args; |
| 55 |
} |
| 56 |
|
| 57 |
public function import_in_library( $data ) { |
| 58 |
if ( empty( $data ) ) { |
| 59 |
return new WP_Error( 'file_error', 'Invalid File' ); |
| 60 |
} |
| 61 |
|
| 62 |
$content = $data['content']; |
| 63 |
|
| 64 |
if ( ! is_array( $content ) ) { |
| 65 |
return new WP_Error( 'file_error', 'Invalid File' ); |
| 66 |
} |
| 67 |
|
| 68 |
$page_settings = $this->page_settings( $data ); |
| 69 |
|
| 70 |
// TODO: type check for (theme builder) |
| 71 |
|
| 72 |
$template_id = $this->save_item( [ |
| 73 |
'content' => $content, |
| 74 |
'title' => $data['title'], |
| 75 |
'type' => $data['type'], |
| 76 |
'page_settings' => $page_settings, |
| 77 |
] ); |
| 78 |
|
| 79 |
if ( is_wp_error( $template_id ) ) { |
| 80 |
return $template_id; |
| 81 |
} |
| 82 |
|
| 83 |
return $this->get_item( $template_id ); |
| 84 |
} |
| 85 |
|
| 86 |
public function create_page( $template_data ){ |
| 87 |
$page_settings = $this->page_settings( $template_data ); |
| 88 |
|
| 89 |
$defaults = [ |
| 90 |
'post_title' => isset( $template_data['page_title'] ) ? $template_data['page_title'] : 'Templately: ' . $template_data['title'], |
| 91 |
'page_settings' => $page_settings, |
| 92 |
'status' => current_user_can( 'publish_posts' ) ? 'publish' : 'pending', |
| 93 |
]; |
| 94 |
|
| 95 |
$template_data = wp_parse_args( $template_data, $defaults ); |
| 96 |
|
| 97 |
$document = ElementorPlugin::$instance->documents->create( |
| 98 |
$template_data['type'], |
| 99 |
[ |
| 100 |
'post_title' => $template_data['post_title'], |
| 101 |
'post_status' => $template_data['status'], |
| 102 |
'post_type' => 'page', |
| 103 |
] |
| 104 |
); |
| 105 |
|
| 106 |
if ( is_wp_error( $document ) ) { |
| 107 |
/** |
| 108 |
* @var WP_Error $document |
| 109 |
*/ |
| 110 |
return $document; |
| 111 |
} |
| 112 |
|
| 113 |
$document->save( [ |
| 114 |
'elements' => $template_data['content'], |
| 115 |
'settings' => $page_settings, |
| 116 |
] ); |
| 117 |
|
| 118 |
return $document->get_main_id(); |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* @param $template_data |
| 123 |
* @noinspection DuplicatedCode |
| 124 |
* |
| 125 |
* @return array |
| 126 |
*/ |
| 127 |
private function page_settings( $template_data ) { |
| 128 |
$page_settings = []; |
| 129 |
|
| 130 |
if (!isset($template_data['page_settings'])) { |
| 131 |
$template_data['page_settings'] = array(); |
| 132 |
} |
| 133 |
|
| 134 |
if(!isset($template_data['page_settings']["template"])){ |
| 135 |
$template_data['page_settings']["template"] = "elementor_header_footer"; |
| 136 |
} |
| 137 |
|
| 138 |
$page = new Model([ |
| 139 |
'id' => 0, |
| 140 |
'settings' => $template_data['page_settings'], |
| 141 |
]); |
| 142 |
|
| 143 |
$page_settings_data = $this->process_element_export_import_content($page, 'on_import'); |
| 144 |
|
| 145 |
if (!empty($page_settings_data['settings'])) { |
| 146 |
$page_settings = $page_settings_data['settings']; |
| 147 |
} |
| 148 |
|
| 149 |
return $page_settings; |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Iterate data. |
| 154 |
* |
| 155 |
* Accept any type of Elementor data and a callback function. The callback |
| 156 |
* function runs recursively for each element and his child elements. |
| 157 |
* |
| 158 |
* @since 1.0.0 |
| 159 |
* @access public |
| 160 |
* |
| 161 |
* @param array $data_container Any type of elementor data. |
| 162 |
* @param callable $callback A function to iterate data by. |
| 163 |
* @param array $args Array of args pointers for passing parameters in & out of the callback |
| 164 |
* |
| 165 |
* @return mixed Iterated data. |
| 166 |
*/ |
| 167 |
public function iterate_data( $data_container, $callback, $args = [] ) { |
| 168 |
if ( isset( $data_container['elType'] ) ) { |
| 169 |
if ( ! empty( $data_container['elements'] ) ) { |
| 170 |
$data_container['elements'] = $this->iterate_data( $data_container['elements'], $callback, $args ); |
| 171 |
} |
| 172 |
|
| 173 |
return call_user_func( $callback, $data_container, $args ); |
| 174 |
} |
| 175 |
|
| 176 |
foreach ( $data_container as $element_key => $element_value ) { |
| 177 |
$element_data = $this->iterate_data( $data_container[ $element_key ], $callback, $args ); |
| 178 |
|
| 179 |
if ( null === $element_data ) { |
| 180 |
unset($data_container[ $element_key ]); |
| 181 |
continue; |
| 182 |
} |
| 183 |
|
| 184 |
$data_container[ $element_key ] = $element_data; |
| 185 |
} |
| 186 |
|
| 187 |
return array_values($data_container); |
| 188 |
} |
| 189 |
|
| 190 |
} |
| 191 |
|
| 192 |
|